1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2009 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
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. |
---|
12 | * |
---|
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 |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef LEMON_CPLEX_H |
---|
20 | #define LEMON_CPLEX_H |
---|
21 | |
---|
22 | ///\file |
---|
23 | ///\brief Header of the LEMON-CPLEX lp solver interface. |
---|
24 | |
---|
25 | #include <lemon/lp_base.h> |
---|
26 | |
---|
27 | struct cpxenv; |
---|
28 | struct cpxlp; |
---|
29 | |
---|
30 | namespace lemon { |
---|
31 | |
---|
32 | /// \brief Reference counted wrapper around cpxenv pointer |
---|
33 | /// |
---|
34 | /// The cplex uses environment object which is responsible for |
---|
35 | /// checking the proper license usage. This class provides a simple |
---|
36 | /// interface for share the environment object between different |
---|
37 | /// problems. |
---|
38 | class CplexEnv { |
---|
39 | friend class CplexBase; |
---|
40 | private: |
---|
41 | cpxenv* _env; |
---|
42 | mutable int* _cnt; |
---|
43 | |
---|
44 | public: |
---|
45 | |
---|
46 | /// \brief This exception is thrown when the license check is not |
---|
47 | /// sufficient |
---|
48 | class LicenseError : public Exception { |
---|
49 | friend class CplexEnv; |
---|
50 | private: |
---|
51 | |
---|
52 | LicenseError(int status); |
---|
53 | char _message[510]; |
---|
54 | |
---|
55 | public: |
---|
56 | |
---|
57 | /// The short error message |
---|
58 | virtual const char* what() const throw() { |
---|
59 | return _message; |
---|
60 | } |
---|
61 | }; |
---|
62 | |
---|
63 | /// Constructor |
---|
64 | CplexEnv(); |
---|
65 | /// Shallow copy constructor |
---|
66 | CplexEnv(const CplexEnv&); |
---|
67 | /// Shallow assignement |
---|
68 | CplexEnv& operator=(const CplexEnv&); |
---|
69 | /// Destructor |
---|
70 | virtual ~CplexEnv(); |
---|
71 | |
---|
72 | protected: |
---|
73 | |
---|
74 | cpxenv* cplexEnv() { return _env; } |
---|
75 | const cpxenv* cplexEnv() const { return _env; } |
---|
76 | }; |
---|
77 | |
---|
78 | /// \brief Base interface for the CPLEX LP and MIP solver |
---|
79 | /// |
---|
80 | /// This class implements the common interface of the CPLEX LP and |
---|
81 | /// MIP solvers. |
---|
82 | /// \ingroup lp_group |
---|
83 | class CplexBase : virtual public LpBase { |
---|
84 | protected: |
---|
85 | |
---|
86 | CplexEnv _env; |
---|
87 | cpxlp* _prob; |
---|
88 | |
---|
89 | CplexBase(); |
---|
90 | CplexBase(const CplexEnv&); |
---|
91 | CplexBase(const CplexBase &); |
---|
92 | virtual ~CplexBase(); |
---|
93 | |
---|
94 | virtual int _addCol(); |
---|
95 | virtual int _addRow(); |
---|
96 | virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
---|
97 | |
---|
98 | virtual void _eraseCol(int i); |
---|
99 | virtual void _eraseRow(int i); |
---|
100 | |
---|
101 | virtual void _eraseColId(int i); |
---|
102 | virtual void _eraseRowId(int i); |
---|
103 | |
---|
104 | virtual void _getColName(int col, std::string& name) const; |
---|
105 | virtual void _setColName(int col, const std::string& name); |
---|
106 | virtual int _colByName(const std::string& name) const; |
---|
107 | |
---|
108 | virtual void _getRowName(int row, std::string& name) const; |
---|
109 | virtual void _setRowName(int row, const std::string& name); |
---|
110 | virtual int _rowByName(const std::string& name) const; |
---|
111 | |
---|
112 | virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
---|
113 | virtual void _getRowCoeffs(int i, InsertIterator b) const; |
---|
114 | |
---|
115 | virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
---|
116 | virtual void _getColCoeffs(int i, InsertIterator b) const; |
---|
117 | |
---|
118 | virtual void _setCoeff(int row, int col, Value value); |
---|
119 | virtual Value _getCoeff(int row, int col) const; |
---|
120 | |
---|
121 | virtual void _setColLowerBound(int i, Value value); |
---|
122 | virtual Value _getColLowerBound(int i) const; |
---|
123 | |
---|
124 | virtual void _setColUpperBound(int i, Value value); |
---|
125 | virtual Value _getColUpperBound(int i) const; |
---|
126 | |
---|
127 | private: |
---|
128 | void _set_row_bounds(int i, Value lb, Value ub); |
---|
129 | protected: |
---|
130 | |
---|
131 | virtual void _setRowLowerBound(int i, Value value); |
---|
132 | virtual Value _getRowLowerBound(int i) const; |
---|
133 | |
---|
134 | virtual void _setRowUpperBound(int i, Value value); |
---|
135 | virtual Value _getRowUpperBound(int i) const; |
---|
136 | |
---|
137 | virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
---|
138 | virtual void _getObjCoeffs(InsertIterator b) const; |
---|
139 | |
---|
140 | virtual void _setObjCoeff(int i, Value obj_coef); |
---|
141 | virtual Value _getObjCoeff(int i) const; |
---|
142 | |
---|
143 | virtual void _setSense(Sense sense); |
---|
144 | virtual Sense _getSense() const; |
---|
145 | |
---|
146 | virtual void _clear(); |
---|
147 | |
---|
148 | virtual void _messageLevel(MessageLevel level); |
---|
149 | void _applyMessageLevel(); |
---|
150 | |
---|
151 | bool _message_enabled; |
---|
152 | |
---|
153 | void _write(std::string file, std::string format) const; |
---|
154 | |
---|
155 | public: |
---|
156 | |
---|
157 | /// Returns the used \c CplexEnv instance |
---|
158 | const CplexEnv& env() const { return _env; } |
---|
159 | |
---|
160 | /// \brief Returns the const cpxenv pointer |
---|
161 | /// |
---|
162 | /// \note The cpxenv might be destructed with the solver. |
---|
163 | const cpxenv* cplexEnv() const { return _env.cplexEnv(); } |
---|
164 | |
---|
165 | /// \brief Returns the const cpxenv pointer |
---|
166 | /// |
---|
167 | /// \note The cpxenv might be destructed with the solver. |
---|
168 | cpxenv* cplexEnv() { return _env.cplexEnv(); } |
---|
169 | |
---|
170 | /// Returns the cplex problem object |
---|
171 | cpxlp* cplexLp() { return _prob; } |
---|
172 | /// Returns the cplex problem object |
---|
173 | const cpxlp* cplexLp() const { return _prob; } |
---|
174 | |
---|
175 | #ifdef DOXYGEN |
---|
176 | /// Write the problem or the solution to a file in the given format |
---|
177 | |
---|
178 | /// This function writes the problem or the solution |
---|
179 | /// to a file in the given format. |
---|
180 | /// Trying to write in an unsupported format will trigger |
---|
181 | /// \ref UnsupportedFormatError. |
---|
182 | /// \param file The file path |
---|
183 | /// \param format The output file format. |
---|
184 | /// Supportted formats are "MPS", "LP" and "SOL". |
---|
185 | void write(std::string file, std::string format = "MPS") const {} |
---|
186 | #endif |
---|
187 | |
---|
188 | }; |
---|
189 | |
---|
190 | /// \brief Interface for the CPLEX LP solver |
---|
191 | /// |
---|
192 | /// This class implements an interface for the CPLEX LP solver. |
---|
193 | ///\ingroup lp_group |
---|
194 | class CplexLp : public LpSolver, public CplexBase { |
---|
195 | public: |
---|
196 | /// \e |
---|
197 | CplexLp(); |
---|
198 | /// \e |
---|
199 | CplexLp(const CplexEnv&); |
---|
200 | /// \e |
---|
201 | CplexLp(const CplexLp&); |
---|
202 | /// \e |
---|
203 | virtual ~CplexLp(); |
---|
204 | |
---|
205 | /// \e |
---|
206 | virtual CplexLp* cloneSolver() const; |
---|
207 | /// \e |
---|
208 | virtual CplexLp* newSolver() const; |
---|
209 | |
---|
210 | private: |
---|
211 | |
---|
212 | // these values cannot retrieved element by element |
---|
213 | mutable std::vector<int> _col_status; |
---|
214 | mutable std::vector<int> _row_status; |
---|
215 | |
---|
216 | mutable std::vector<Value> _primal_ray; |
---|
217 | mutable std::vector<Value> _dual_ray; |
---|
218 | |
---|
219 | void _clear_temporals(); |
---|
220 | |
---|
221 | SolveExitStatus convertStatus(int status); |
---|
222 | |
---|
223 | protected: |
---|
224 | |
---|
225 | virtual const char* _solverName() const; |
---|
226 | |
---|
227 | virtual SolveExitStatus _solve(); |
---|
228 | virtual Value _getPrimal(int i) const; |
---|
229 | virtual Value _getDual(int i) const; |
---|
230 | virtual Value _getPrimalValue() const; |
---|
231 | |
---|
232 | virtual VarStatus _getColStatus(int i) const; |
---|
233 | virtual VarStatus _getRowStatus(int i) const; |
---|
234 | |
---|
235 | virtual Value _getPrimalRay(int i) const; |
---|
236 | virtual Value _getDualRay(int i) const; |
---|
237 | |
---|
238 | virtual ProblemType _getPrimalType() const; |
---|
239 | virtual ProblemType _getDualType() const; |
---|
240 | |
---|
241 | public: |
---|
242 | |
---|
243 | /// Solve with primal simplex method |
---|
244 | SolveExitStatus solvePrimal(); |
---|
245 | |
---|
246 | /// Solve with dual simplex method |
---|
247 | SolveExitStatus solveDual(); |
---|
248 | |
---|
249 | /// Solve with barrier method |
---|
250 | SolveExitStatus solveBarrier(); |
---|
251 | |
---|
252 | }; |
---|
253 | |
---|
254 | /// \brief Interface for the CPLEX MIP solver |
---|
255 | /// |
---|
256 | /// This class implements an interface for the CPLEX MIP solver. |
---|
257 | ///\ingroup lp_group |
---|
258 | class CplexMip : public MipSolver, public CplexBase { |
---|
259 | public: |
---|
260 | /// \e |
---|
261 | CplexMip(); |
---|
262 | /// \e |
---|
263 | CplexMip(const CplexEnv&); |
---|
264 | /// \e |
---|
265 | CplexMip(const CplexMip&); |
---|
266 | /// \e |
---|
267 | virtual ~CplexMip(); |
---|
268 | |
---|
269 | /// \e |
---|
270 | virtual CplexMip* cloneSolver() const; |
---|
271 | /// \e |
---|
272 | virtual CplexMip* newSolver() const; |
---|
273 | |
---|
274 | protected: |
---|
275 | |
---|
276 | |
---|
277 | virtual const char* _solverName() const; |
---|
278 | |
---|
279 | virtual ColTypes _getColType(int col) const; |
---|
280 | virtual void _setColType(int col, ColTypes col_type); |
---|
281 | |
---|
282 | virtual SolveExitStatus _solve(); |
---|
283 | virtual ProblemType _getType() const; |
---|
284 | virtual Value _getSol(int i) const; |
---|
285 | virtual Value _getSolValue() const; |
---|
286 | |
---|
287 | }; |
---|
288 | |
---|
289 | } //END OF NAMESPACE LEMON |
---|
290 | |
---|
291 | #endif //LEMON_CPLEX_H |
---|
292 | |
---|