1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GLPK_H |
20 | 20 |
#define LEMON_GLPK_H |
21 | 21 |
|
22 | 22 |
///\file |
23 | 23 |
///\brief Header of the LEMON-GLPK lp solver interface. |
24 | 24 |
///\ingroup lp_group |
25 | 25 |
|
26 | 26 |
#include <lemon/lp_base.h> |
27 | 27 |
|
28 |
// forward declaration |
|
29 |
#if !defined _GLP_PROB && !defined GLP_PROB |
|
30 |
#define _GLP_PROB |
|
31 |
#define GLP_PROB |
|
32 |
typedef struct { double _opaque_prob; } glp_prob; |
|
33 |
/* LP/MIP problem object */ |
|
34 |
#endif |
|
35 |
|
|
36 | 28 |
namespace lemon { |
37 | 29 |
|
30 |
namespace _solver_bits { |
|
31 |
class VoidPtr { |
|
32 |
private: |
|
33 |
void *_ptr; |
|
34 |
public: |
|
35 |
VoidPtr() : _ptr(0) {} |
|
36 |
|
|
37 |
template <typename T> |
|
38 |
VoidPtr(T* ptr) : _ptr(reinterpret_cast<void*>(ptr)) {} |
|
39 |
|
|
40 |
template <typename T> |
|
41 |
VoidPtr& operator=(T* ptr) { |
|
42 |
_ptr = reinterpret_cast<void*>(ptr); |
|
43 |
return *this; |
|
44 |
} |
|
45 |
|
|
46 |
template <typename T> |
|
47 |
operator T*() const { return reinterpret_cast<T*>(_ptr); } |
|
48 |
}; |
|
49 |
} |
|
38 | 50 |
|
39 | 51 |
/// \brief Base interface for the GLPK LP and MIP solver |
40 | 52 |
/// |
41 | 53 |
/// This class implements the common interface of the GLPK LP and MIP solver. |
42 | 54 |
/// \ingroup lp_group |
43 | 55 |
class GlpkBase : virtual public LpBase { |
44 | 56 |
protected: |
45 | 57 |
|
46 |
typedef glp_prob LPX; |
|
47 |
glp_prob* lp; |
|
58 |
_solver_bits::VoidPtr lp; |
|
48 | 59 |
|
49 | 60 |
GlpkBase(); |
50 | 61 |
GlpkBase(const GlpkBase&); |
51 | 62 |
virtual ~GlpkBase(); |
52 | 63 |
|
53 | 64 |
protected: |
54 | 65 |
|
55 | 66 |
virtual int _addCol(); |
56 | 67 |
virtual int _addRow(); |
57 | 68 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
58 | 69 |
|
59 | 70 |
virtual void _eraseCol(int i); |
60 | 71 |
virtual void _eraseRow(int i); |
61 | 72 |
|
62 | 73 |
virtual void _eraseColId(int i); |
63 | 74 |
virtual void _eraseRowId(int i); |
64 | 75 |
|
65 | 76 |
virtual void _getColName(int col, std::string& name) const; |
66 | 77 |
virtual void _setColName(int col, const std::string& name); |
67 | 78 |
virtual int _colByName(const std::string& name) const; |
68 | 79 |
|
69 | 80 |
virtual void _getRowName(int row, std::string& name) const; |
70 | 81 |
virtual void _setRowName(int row, const std::string& name); |
71 | 82 |
virtual int _rowByName(const std::string& name) const; |
72 | 83 |
|
73 | 84 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
74 | 85 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
75 | 86 |
|
76 | 87 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
77 | 88 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
78 | 89 |
|
79 | 90 |
virtual void _setCoeff(int row, int col, Value value); |
80 | 91 |
virtual Value _getCoeff(int row, int col) const; |
81 | 92 |
|
82 | 93 |
virtual void _setColLowerBound(int i, Value value); |
83 | 94 |
virtual Value _getColLowerBound(int i) const; |
84 | 95 |
|
85 | 96 |
virtual void _setColUpperBound(int i, Value value); |
86 | 97 |
virtual Value _getColUpperBound(int i) const; |
87 | 98 |
|
88 | 99 |
virtual void _setRowLowerBound(int i, Value value); |
89 | 100 |
virtual Value _getRowLowerBound(int i) const; |
90 | 101 |
|
91 | 102 |
virtual void _setRowUpperBound(int i, Value value); |
92 | 103 |
virtual Value _getRowUpperBound(int i) const; |
93 | 104 |
|
94 | 105 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
95 | 106 |
virtual void _getObjCoeffs(InsertIterator b) const; |
96 | 107 |
|
97 | 108 |
virtual void _setObjCoeff(int i, Value obj_coef); |
98 | 109 |
virtual Value _getObjCoeff(int i) const; |
99 | 110 |
|
100 | 111 |
virtual void _setSense(Sense); |
101 | 112 |
virtual Sense _getSense() const; |
102 | 113 |
|
103 | 114 |
virtual void _clear(); |
104 | 115 |
|
105 | 116 |
virtual void _messageLevel(MessageLevel level); |
106 | 117 |
|
107 | 118 |
private: |
108 | 119 |
|
109 | 120 |
static void freeEnv(); |
110 | 121 |
|
111 | 122 |
struct FreeEnvHelper { |
112 | 123 |
~FreeEnvHelper() { |
113 | 124 |
freeEnv(); |
114 | 125 |
} |
115 | 126 |
}; |
116 | 127 |
|
117 | 128 |
static FreeEnvHelper freeEnvHelper; |
118 | 129 |
|
119 | 130 |
protected: |
120 | 131 |
|
121 | 132 |
int _message_level; |
122 | 133 |
|
123 | 134 |
public: |
124 | 135 |
|
125 | 136 |
///Pointer to the underlying GLPK data structure. |
126 |
|
|
137 |
_solver_bits::VoidPtr lpx() {return lp;} |
|
127 | 138 |
///Const pointer to the underlying GLPK data structure. |
128 |
|
|
139 |
_solver_bits::VoidPtr lpx() const {return lp;} |
|
129 | 140 |
|
130 | 141 |
///Returns the constraint identifier understood by GLPK. |
131 | 142 |
int lpxRow(Row r) const { return rows(id(r)); } |
132 | 143 |
|
133 | 144 |
///Returns the variable identifier understood by GLPK. |
134 | 145 |
int lpxCol(Col c) const { return cols(id(c)); } |
135 | 146 |
|
136 | 147 |
}; |
137 | 148 |
|
138 | 149 |
/// \brief Interface for the GLPK LP solver |
139 | 150 |
/// |
140 | 151 |
/// This class implements an interface for the GLPK LP solver. |
141 | 152 |
///\ingroup lp_group |
142 | 153 |
class GlpkLp : public LpSolver, public GlpkBase { |
143 | 154 |
public: |
144 | 155 |
|
145 | 156 |
///\e |
146 | 157 |
GlpkLp(); |
147 | 158 |
///\e |
148 | 159 |
GlpkLp(const GlpkLp&); |
149 | 160 |
|
150 | 161 |
///\e |
151 | 162 |
virtual GlpkLp* cloneSolver() const; |
152 | 163 |
///\e |
153 | 164 |
virtual GlpkLp* newSolver() const; |
154 | 165 |
|
155 | 166 |
private: |
156 | 167 |
|
157 | 168 |
mutable std::vector<double> _primal_ray; |
158 | 169 |
mutable std::vector<double> _dual_ray; |
159 | 170 |
|
160 | 171 |
void _clear_temporals(); |
161 | 172 |
|
162 | 173 |
protected: |
163 | 174 |
|
164 | 175 |
virtual const char* _solverName() const; |
165 | 176 |
|
166 | 177 |
virtual SolveExitStatus _solve(); |
167 | 178 |
virtual Value _getPrimal(int i) const; |
168 | 179 |
virtual Value _getDual(int i) const; |
169 | 180 |
|
170 | 181 |
virtual Value _getPrimalValue() const; |
171 | 182 |
|
172 | 183 |
virtual VarStatus _getColStatus(int i) const; |
173 | 184 |
virtual VarStatus _getRowStatus(int i) const; |
174 | 185 |
|
175 | 186 |
virtual Value _getPrimalRay(int i) const; |
176 | 187 |
virtual Value _getDualRay(int i) const; |
177 | 188 |
|
178 | 189 |
virtual ProblemType _getPrimalType() const; |
179 | 190 |
virtual ProblemType _getDualType() const; |
180 | 191 |
|
181 | 192 |
public: |
182 | 193 |
|
183 | 194 |
///Solve with primal simplex |
184 | 195 |
SolveExitStatus solvePrimal(); |
185 | 196 |
|
186 | 197 |
///Solve with dual simplex |
187 | 198 |
SolveExitStatus solveDual(); |
188 | 199 |
|
189 | 200 |
private: |
190 | 201 |
|
191 | 202 |
bool _presolve; |
192 | 203 |
|
193 | 204 |
public: |
194 | 205 |
|
195 | 206 |
///Turns on or off the presolver |
196 | 207 |
|
197 | 208 |
///Turns on (\c b is \c true) or off (\c b is \c false) the presolver |
198 | 209 |
/// |
199 | 210 |
///The presolver is off by default. |
200 | 211 |
void presolver(bool presolve); |
201 | 212 |
|
202 | 213 |
}; |
203 | 214 |
|
204 | 215 |
/// \brief Interface for the GLPK MIP solver |
205 | 216 |
/// |
206 | 217 |
/// This class implements an interface for the GLPK MIP solver. |
207 | 218 |
///\ingroup lp_group |
208 | 219 |
class GlpkMip : public MipSolver, public GlpkBase { |
209 | 220 |
public: |
210 | 221 |
|
211 | 222 |
///\e |
212 | 223 |
GlpkMip(); |
213 | 224 |
///\e |
214 | 225 |
GlpkMip(const GlpkMip&); |
215 | 226 |
|
216 | 227 |
virtual GlpkMip* cloneSolver() const; |
217 | 228 |
virtual GlpkMip* newSolver() const; |
218 | 229 |
|
219 | 230 |
protected: |
220 | 231 |
|
221 | 232 |
virtual const char* _solverName() const; |
222 | 233 |
|
223 | 234 |
virtual ColTypes _getColType(int col) const; |
224 | 235 |
virtual void _setColType(int col, ColTypes col_type); |
225 | 236 |
|
226 | 237 |
virtual SolveExitStatus _solve(); |
227 | 238 |
virtual ProblemType _getType() const; |
228 | 239 |
virtual Value _getSol(int i) const; |
229 | 240 |
virtual Value _getSolValue() const; |
230 | 241 |
|
231 | 242 |
}; |
232 | 243 |
|
233 | 244 |
|
234 | 245 |
} //END OF NAMESPACE LEMON |
235 | 246 |
|
236 | 247 |
#endif //LEMON_GLPK_H |
237 | 248 |
|
0 comments (0 inline)