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