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-CPLEX lp solver interface.
25 #include <lemon/lp_base.h>
26 #include <lemon/bits/lock.h>
33 /// \brief Reference counted wrapper around cpxenv pointer
35 /// The cplex uses environment object which is responsible for
36 /// checking the proper license usage. This class provides a simple
37 /// interface for share the environment object between different
40 friend class CplexBase;
44 mutable bits::Lock* _cnt_lock;
51 /// \brief This exception is thrown when the license check is not
53 class LicenseError : public Exception {
54 friend class CplexEnv;
57 LicenseError(int status);
62 /// The short error message
63 virtual const char* what() const throw() {
70 /// Shallow copy constructor
71 CplexEnv(const CplexEnv&);
72 /// Shallow assignement
73 CplexEnv& operator=(const CplexEnv&);
79 cpxenv* cplexEnv() { return _env; }
80 const cpxenv* cplexEnv() const { return _env; }
83 /// \brief Base interface for the CPLEX LP and MIP solver
85 /// This class implements the common interface of the CPLEX LP and
88 class CplexBase : virtual public LpBase {
95 CplexBase(const CplexEnv&);
96 CplexBase(const CplexBase &);
99 virtual int _addCol();
100 virtual int _addRow();
101 virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
103 virtual void _eraseCol(int i);
104 virtual void _eraseRow(int i);
106 virtual void _eraseColId(int i);
107 virtual void _eraseRowId(int i);
109 virtual void _getColName(int col, std::string& name) const;
110 virtual void _setColName(int col, const std::string& name);
111 virtual int _colByName(const std::string& name) const;
113 virtual void _getRowName(int row, std::string& name) const;
114 virtual void _setRowName(int row, const std::string& name);
115 virtual int _rowByName(const std::string& name) const;
117 virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
118 virtual void _getRowCoeffs(int i, InsertIterator b) const;
120 virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
121 virtual void _getColCoeffs(int i, InsertIterator b) const;
123 virtual void _setCoeff(int row, int col, Value value);
124 virtual Value _getCoeff(int row, int col) const;
126 virtual void _setColLowerBound(int i, Value value);
127 virtual Value _getColLowerBound(int i) const;
129 virtual void _setColUpperBound(int i, Value value);
130 virtual Value _getColUpperBound(int i) const;
133 void _set_row_bounds(int i, Value lb, Value ub);
136 virtual void _setRowLowerBound(int i, Value value);
137 virtual Value _getRowLowerBound(int i) const;
139 virtual void _setRowUpperBound(int i, Value value);
140 virtual Value _getRowUpperBound(int i) const;
142 virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
143 virtual void _getObjCoeffs(InsertIterator b) const;
145 virtual void _setObjCoeff(int i, Value obj_coef);
146 virtual Value _getObjCoeff(int i) const;
148 virtual void _setSense(Sense sense);
149 virtual Sense _getSense() const;
151 virtual void _clear();
153 virtual void _messageLevel(MessageLevel level);
154 void _applyMessageLevel();
156 bool _message_enabled;
158 void _write(std::string file, std::string format) const;
162 /// Returns the used \c CplexEnv instance
163 const CplexEnv& env() const { return _env; }
165 /// \brief Returns the const cpxenv pointer
167 /// \note The cpxenv might be destructed with the solver.
168 const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
170 /// \brief Returns the const cpxenv pointer
172 /// \note The cpxenv might be destructed with the solver.
173 cpxenv* cplexEnv() { return _env.cplexEnv(); }
175 /// Returns the cplex problem object
176 cpxlp* cplexLp() { return _prob; }
177 /// Returns the cplex problem object
178 const cpxlp* cplexLp() const { return _prob; }
181 /// Write the problem or the solution to a file in the given format
183 /// This function writes the problem or the solution
184 /// to a file in the given format.
185 /// Trying to write in an unsupported format will trigger
186 /// \ref lemon::LpBase::UnsupportedFormatError "UnsupportedFormatError".
187 /// \param file The file path
188 /// \param format The output file format.
189 /// Supportted formats are "MPS", "LP" and "SOL".
190 void write(std::string file, std::string format = "MPS") const {}
195 /// \brief Interface for the CPLEX LP solver
197 /// This class implements an interface for the CPLEX LP solver.
199 class CplexLp : public LpSolver, public CplexBase {
204 CplexLp(const CplexEnv&);
206 CplexLp(const CplexLp&);
211 virtual CplexLp* cloneSolver() const;
213 virtual CplexLp* newSolver() const;
217 // these values cannot retrieved element by element
218 mutable std::vector<int> _col_status;
219 mutable std::vector<int> _row_status;
221 mutable std::vector<Value> _primal_ray;
222 mutable std::vector<Value> _dual_ray;
224 void _clear_temporals();
226 SolveExitStatus convertStatus(int status);
230 virtual const char* _solverName() const;
232 virtual SolveExitStatus _solve();
233 virtual Value _getPrimal(int i) const;
234 virtual Value _getDual(int i) const;
235 virtual Value _getPrimalValue() const;
237 virtual VarStatus _getColStatus(int i) const;
238 virtual VarStatus _getRowStatus(int i) const;
240 virtual Value _getPrimalRay(int i) const;
241 virtual Value _getDualRay(int i) const;
243 virtual ProblemType _getPrimalType() const;
244 virtual ProblemType _getDualType() const;
248 /// Solve with primal simplex method
249 SolveExitStatus solvePrimal();
251 /// Solve with dual simplex method
252 SolveExitStatus solveDual();
254 /// Solve with barrier method
255 SolveExitStatus solveBarrier();
259 /// \brief Interface for the CPLEX MIP solver
261 /// This class implements an interface for the CPLEX MIP solver.
263 class CplexMip : public MipSolver, public CplexBase {
268 CplexMip(const CplexEnv&);
270 CplexMip(const CplexMip&);
275 virtual CplexMip* cloneSolver() const;
277 virtual CplexMip* newSolver() const;
282 virtual const char* _solverName() const;
284 virtual ColTypes _getColType(int col) const;
285 virtual void _setColType(int col, ColTypes col_type);
287 virtual SolveExitStatus _solve();
288 virtual ProblemType _getType() const;
289 virtual Value _getSol(int i) const;
290 virtual Value _getSolValue() const;
294 } //END OF NAMESPACE LEMON
296 #endif //LEMON_CPLEX_H