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