1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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>
32 /// \brief Reference counted wrapper around cpxenv pointer
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
39 friend class CplexBase;
46 /// \brief This exception is thrown when the license check is not
48 class LicenseError : public Exception {
49 friend class CplexEnv;
52 LicenseError(int status);
57 /// The short error message
58 virtual const char* what() const throw() {
65 /// Shallow copy constructor
66 CplexEnv(const CplexEnv&);
67 /// Shallow assignement
68 CplexEnv& operator=(const CplexEnv&);
74 cpxenv* cplexEnv() { return _env; }
75 const cpxenv* cplexEnv() const { return _env; }
78 /// \brief Base interface for the CPLEX LP and MIP solver
80 /// This class implements the common interface of the CPLEX LP and
83 class CplexBase : virtual public LpBase {
90 CplexBase(const CplexEnv&);
91 CplexBase(const CplexBase &);
94 virtual int _addCol();
95 virtual int _addRow();
97 virtual void _eraseCol(int i);
98 virtual void _eraseRow(int i);
100 virtual void _eraseColId(int i);
101 virtual void _eraseRowId(int i);
103 virtual void _getColName(int col, std::string& name) const;
104 virtual void _setColName(int col, const std::string& name);
105 virtual int _colByName(const std::string& name) const;
107 virtual void _getRowName(int row, std::string& name) const;
108 virtual void _setRowName(int row, const std::string& name);
109 virtual int _rowByName(const std::string& name) const;
111 virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
112 virtual void _getRowCoeffs(int i, InsertIterator b) const;
114 virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
115 virtual void _getColCoeffs(int i, InsertIterator b) const;
117 virtual void _setCoeff(int row, int col, Value value);
118 virtual Value _getCoeff(int row, int col) const;
120 virtual void _setColLowerBound(int i, Value value);
121 virtual Value _getColLowerBound(int i) const;
123 virtual void _setColUpperBound(int i, Value value);
124 virtual Value _getColUpperBound(int i) const;
127 void _set_row_bounds(int i, Value lb, Value ub);
130 virtual void _setRowLowerBound(int i, Value value);
131 virtual Value _getRowLowerBound(int i) const;
133 virtual void _setRowUpperBound(int i, Value value);
134 virtual Value _getRowUpperBound(int i) const;
136 virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
137 virtual void _getObjCoeffs(InsertIterator b) const;
139 virtual void _setObjCoeff(int i, Value obj_coef);
140 virtual Value _getObjCoeff(int i) const;
142 virtual void _setSense(Sense sense);
143 virtual Sense _getSense() const;
145 virtual void _clear();
149 /// Returns the used \c CplexEnv instance
150 const CplexEnv& env() const { return _env; }
152 const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
154 cpxlp* cplexLp() { return _prob; }
155 const cpxlp* cplexLp() const { return _prob; }
159 /// \brief Interface for the CPLEX LP solver
161 /// This class implements an interface for the CPLEX LP solver.
163 class CplexLp : public LpSolver, public CplexBase {
168 CplexLp(const CplexEnv&);
170 CplexLp(const CplexLp&);
175 virtual CplexLp* cloneSolver() const;
177 virtual CplexLp* newSolver() const;
181 // these values cannot retrieved element by element
182 mutable std::vector<int> _col_status;
183 mutable std::vector<int> _row_status;
185 mutable std::vector<Value> _primal_ray;
186 mutable std::vector<Value> _dual_ray;
188 void _clear_temporals();
190 SolveExitStatus convertStatus(int status);
194 virtual const char* _solverName() const;
196 virtual SolveExitStatus _solve();
197 virtual Value _getPrimal(int i) const;
198 virtual Value _getDual(int i) const;
199 virtual Value _getPrimalValue() const;
201 virtual VarStatus _getColStatus(int i) const;
202 virtual VarStatus _getRowStatus(int i) const;
204 virtual Value _getPrimalRay(int i) const;
205 virtual Value _getDualRay(int i) const;
207 virtual ProblemType _getPrimalType() const;
208 virtual ProblemType _getDualType() const;
212 /// Solve with primal simplex method
213 SolveExitStatus solvePrimal();
215 /// Solve with dual simplex method
216 SolveExitStatus solveDual();
218 /// Solve with barrier method
219 SolveExitStatus solveBarrier();
223 /// \brief Interface for the CPLEX MIP solver
225 /// This class implements an interface for the CPLEX MIP solver.
227 class CplexMip : public MipSolver, public CplexBase {
232 CplexMip(const CplexEnv&);
234 CplexMip(const CplexMip&);
239 virtual CplexMip* cloneSolver() const;
241 virtual CplexMip* newSolver() const;
246 virtual const char* _solverName() const;
248 virtual ColTypes _getColType(int col) const;
249 virtual void _setColType(int col, ColTypes col_type);
251 virtual SolveExitStatus _solve();
252 virtual ProblemType _getType() const;
253 virtual Value _getSol(int i) const;
254 virtual Value _getSolValue() const;
258 } //END OF NAMESPACE LEMON
260 #endif //LEMON_CPLEX_H