1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
23 ///\brief Header of the LEMON-GLPK lp solver interface.
26 #include <lemon/lp_base.h>
28 // forward declaration
31 typedef struct { double _prob; } glp_prob;
32 /* LP/MIP problem object */
38 /// \brief Base interface for the GLPK LP and MIP solver
40 /// This class implements the common interface of the GLPK LP and MIP solver.
42 class GlpkBase : virtual public LpBase {
49 GlpkBase(const GlpkBase&);
54 virtual int _addCol();
55 virtual int _addRow();
57 virtual void _eraseCol(int i);
58 virtual void _eraseRow(int i);
60 virtual void _eraseColId(int i);
61 virtual void _eraseRowId(int i);
63 virtual void _getColName(int col, std::string& name) const;
64 virtual void _setColName(int col, const std::string& name);
65 virtual int _colByName(const std::string& name) const;
67 virtual void _getRowName(int row, std::string& name) const;
68 virtual void _setRowName(int row, const std::string& name);
69 virtual int _rowByName(const std::string& name) const;
71 virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
72 virtual void _getRowCoeffs(int i, InsertIterator b) const;
74 virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
75 virtual void _getColCoeffs(int i, InsertIterator b) const;
77 virtual void _setCoeff(int row, int col, Value value);
78 virtual Value _getCoeff(int row, int col) const;
80 virtual void _setColLowerBound(int i, Value value);
81 virtual Value _getColLowerBound(int i) const;
83 virtual void _setColUpperBound(int i, Value value);
84 virtual Value _getColUpperBound(int i) const;
86 virtual void _setRowLowerBound(int i, Value value);
87 virtual Value _getRowLowerBound(int i) const;
89 virtual void _setRowUpperBound(int i, Value value);
90 virtual Value _getRowUpperBound(int i) const;
92 virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
93 virtual void _getObjCoeffs(InsertIterator b) const;
95 virtual void _setObjCoeff(int i, Value obj_coef);
96 virtual Value _getObjCoeff(int i) const;
98 virtual void _setSense(Sense);
99 virtual Sense _getSense() const;
101 virtual void _clear();
105 ///Pointer to the underlying GLPK data structure.
106 LPX *lpx() {return lp;}
107 ///Const pointer to the underlying GLPK data structure.
108 const LPX *lpx() const {return lp;}
110 ///Returns the constraint identifier understood by GLPK.
111 int lpxRow(Row r) const { return rows(id(r)); }
113 ///Returns the variable identifier understood by GLPK.
114 int lpxCol(Col c) const { return cols(id(c)); }
118 /// \brief Interface for the GLPK LP solver
120 /// This class implements an interface for the GLPK LP solver.
122 class GlpkLp : public LpSolver, public GlpkBase {
128 GlpkLp(const GlpkLp&);
131 virtual GlpkLp* cloneSolver() const;
133 virtual GlpkLp* newSolver() const;
137 mutable std::vector<double> _primal_ray;
138 mutable std::vector<double> _dual_ray;
140 void _clear_temporals();
144 virtual const char* _solverName() const;
146 virtual SolveExitStatus _solve();
147 virtual Value _getPrimal(int i) const;
148 virtual Value _getDual(int i) const;
150 virtual Value _getPrimalValue() const;
152 virtual VarStatus _getColStatus(int i) const;
153 virtual VarStatus _getRowStatus(int i) const;
155 virtual Value _getPrimalRay(int i) const;
156 virtual Value _getDualRay(int i) const;
158 ///\todo It should be clarified
160 virtual ProblemType _getPrimalType() const;
161 virtual ProblemType _getDualType() const;
165 ///Solve with primal simplex
166 SolveExitStatus solvePrimal();
168 ///Solve with dual simplex
169 SolveExitStatus solveDual();
171 ///Turns on or off the presolver
173 ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
175 ///The presolver is off by default.
176 void presolver(bool b);
178 ///Enum for \c messageLevel() parameter
180 /// no output (default value)
181 MESSAGE_NO_OUTPUT = 0,
182 /// error messages only
183 MESSAGE_ERROR_MESSAGE = 1,
185 MESSAGE_NORMAL_OUTPUT = 2,
186 /// full output (includes informational messages)
187 MESSAGE_FULL_OUTPUT = 3
192 MessageLevel _message_level;
196 ///Set the verbosity of the messages
198 ///Set the verbosity of the messages
200 ///\param m is the level of the messages output by the solver routines.
201 void messageLevel(MessageLevel m);
204 /// \brief Interface for the GLPK MIP solver
206 /// This class implements an interface for the GLPK MIP solver.
208 class GlpkMip : public MipSolver, public GlpkBase {
214 GlpkMip(const GlpkMip&);
216 virtual GlpkMip* cloneSolver() const;
217 virtual GlpkMip* newSolver() const;
221 virtual const char* _solverName() const;
223 virtual ColTypes _getColType(int col) const;
224 virtual void _setColType(int col, ColTypes col_type);
226 virtual SolveExitStatus _solve();
227 virtual ProblemType _getType() const;
228 virtual Value _getSol(int i) const;
229 virtual Value _getSolValue() const;
231 ///Enum for \c messageLevel() parameter
233 /// no output (default value)
234 MESSAGE_NO_OUTPUT = 0,
235 /// error messages only
236 MESSAGE_ERROR_MESSAGE = 1,
238 MESSAGE_NORMAL_OUTPUT = 2,
239 /// full output (includes informational messages)
240 MESSAGE_FULL_OUTPUT = 3
245 MessageLevel _message_level;
249 ///Set the verbosity of the messages
251 ///Set the verbosity of the messages
253 ///\param m is the level of the messages output by the solver routines.
254 void messageLevel(MessageLevel m);
258 } //END OF NAMESPACE LEMON
260 #endif //LEMON_GLPK_H