1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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>
30 namespace _solver_bits {
35 VoidPtr() : _ptr(0) {}
38 VoidPtr(T* ptr) : _ptr(reinterpret_cast<void*>(ptr)) {}
41 VoidPtr& operator=(T* ptr) {
42 _ptr = reinterpret_cast<void*>(ptr);
47 operator T*() const { return reinterpret_cast<T*>(_ptr); }
51 /// \brief Base interface for the GLPK LP and MIP solver
53 /// This class implements the common interface of the GLPK LP and MIP solver.
55 class GlpkBase : virtual public LpBase {
58 _solver_bits::VoidPtr lp;
61 GlpkBase(const GlpkBase&);
66 virtual int _addCol();
67 virtual int _addRow();
68 virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
70 virtual void _eraseCol(int i);
71 virtual void _eraseRow(int i);
73 virtual void _eraseColId(int i);
74 virtual void _eraseRowId(int i);
76 virtual void _getColName(int col, std::string& name) const;
77 virtual void _setColName(int col, const std::string& name);
78 virtual int _colByName(const std::string& name) const;
80 virtual void _getRowName(int row, std::string& name) const;
81 virtual void _setRowName(int row, const std::string& name);
82 virtual int _rowByName(const std::string& name) const;
84 virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
85 virtual void _getRowCoeffs(int i, InsertIterator b) const;
87 virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
88 virtual void _getColCoeffs(int i, InsertIterator b) const;
90 virtual void _setCoeff(int row, int col, Value value);
91 virtual Value _getCoeff(int row, int col) const;
93 virtual void _setColLowerBound(int i, Value value);
94 virtual Value _getColLowerBound(int i) const;
96 virtual void _setColUpperBound(int i, Value value);
97 virtual Value _getColUpperBound(int i) const;
99 virtual void _setRowLowerBound(int i, Value value);
100 virtual Value _getRowLowerBound(int i) const;
102 virtual void _setRowUpperBound(int i, Value value);
103 virtual Value _getRowUpperBound(int i) const;
105 virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
106 virtual void _getObjCoeffs(InsertIterator b) const;
108 virtual void _setObjCoeff(int i, Value obj_coef);
109 virtual Value _getObjCoeff(int i) const;
111 virtual void _setSense(Sense);
112 virtual Sense _getSense() const;
114 virtual void _clear();
116 virtual void _messageLevel(MessageLevel level);
118 virtual void _write(std::string file, std::string format) const;
122 static void freeEnv();
124 struct FreeEnvHelper {
130 static FreeEnvHelper freeEnvHelper;
138 ///Pointer to the underlying GLPK data structure.
139 _solver_bits::VoidPtr lpx() {return lp;}
140 ///Const pointer to the underlying GLPK data structure.
141 _solver_bits::VoidPtr lpx() const {return lp;}
143 ///Returns the constraint identifier understood by GLPK.
144 int lpxRow(Row r) const { return rows(id(r)); }
146 ///Returns the variable identifier understood by GLPK.
147 int lpxCol(Col c) const { return cols(id(c)); }
150 /// Write the problem or the solution to a file in the given format
152 /// This function writes the problem or the solution
153 /// to a file in the given format.
154 /// Trying to write in an unsupported format will trigger
155 /// \ref LpBase::UnsupportedFormatError.
156 /// \param file The file path
157 /// \param format The output file format.
158 /// Supportted formats are "MPS" and "LP".
159 void write(std::string file, std::string format = "MPS") const {}
164 /// \brief Interface for the GLPK LP solver
166 /// This class implements an interface for the GLPK LP solver.
168 class GlpkLp : public LpSolver, public GlpkBase {
174 GlpkLp(const GlpkLp&);
177 virtual GlpkLp* cloneSolver() const;
179 virtual GlpkLp* newSolver() const;
183 mutable std::vector<double> _primal_ray;
184 mutable std::vector<double> _dual_ray;
186 void _clear_temporals();
190 virtual const char* _solverName() const;
192 virtual SolveExitStatus _solve();
193 virtual Value _getPrimal(int i) const;
194 virtual Value _getDual(int i) const;
196 virtual Value _getPrimalValue() const;
198 virtual VarStatus _getColStatus(int i) const;
199 virtual VarStatus _getRowStatus(int i) const;
201 virtual Value _getPrimalRay(int i) const;
202 virtual Value _getDualRay(int i) const;
204 virtual ProblemType _getPrimalType() const;
205 virtual ProblemType _getDualType() const;
209 ///Solve with primal simplex
210 SolveExitStatus solvePrimal();
212 ///Solve with dual simplex
213 SolveExitStatus solveDual();
221 ///Turns on or off the presolver
223 ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
225 ///The presolver is off by default.
226 void presolver(bool presolve);
230 /// \brief Interface for the GLPK MIP solver
232 /// This class implements an interface for the GLPK MIP solver.
234 class GlpkMip : public MipSolver, public GlpkBase {
240 GlpkMip(const GlpkMip&);
242 virtual GlpkMip* cloneSolver() const;
243 virtual GlpkMip* newSolver() const;
247 virtual const char* _solverName() const;
249 virtual ColTypes _getColType(int col) const;
250 virtual void _setColType(int col, ColTypes col_type);
252 virtual SolveExitStatus _solve();
253 virtual ProblemType _getType() const;
254 virtual Value _getSol(int i) const;
255 virtual Value _getSolValue() const;
260 } //END OF NAMESPACE LEMON
262 #endif //LEMON_GLPK_H