1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/cplex.h Thu Dec 10 17:05:35 2009 +0100
1.3 @@ -0,0 +1,276 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2009
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_CPLEX_H
1.23 +#define LEMON_CPLEX_H
1.24 +
1.25 +///\file
1.26 +///\brief Header of the LEMON-CPLEX lp solver interface.
1.27 +
1.28 +#include <lemon/lp_base.h>
1.29 +
1.30 +struct cpxenv;
1.31 +struct cpxlp;
1.32 +
1.33 +namespace lemon {
1.34 +
1.35 + /// \brief Reference counted wrapper around cpxenv pointer
1.36 + ///
1.37 + /// The cplex uses environment object which is responsible for
1.38 + /// checking the proper license usage. This class provides a simple
1.39 + /// interface for share the environment object between different
1.40 + /// problems.
1.41 + class CplexEnv {
1.42 + friend class CplexBase;
1.43 + private:
1.44 + cpxenv* _env;
1.45 + mutable int* _cnt;
1.46 +
1.47 + public:
1.48 +
1.49 + /// \brief This exception is thrown when the license check is not
1.50 + /// sufficient
1.51 + class LicenseError : public Exception {
1.52 + friend class CplexEnv;
1.53 + private:
1.54 +
1.55 + LicenseError(int status);
1.56 + char _message[510];
1.57 +
1.58 + public:
1.59 +
1.60 + /// The short error message
1.61 + virtual const char* what() const throw() {
1.62 + return _message;
1.63 + }
1.64 + };
1.65 +
1.66 + /// Constructor
1.67 + CplexEnv();
1.68 + /// Shallow copy constructor
1.69 + CplexEnv(const CplexEnv&);
1.70 + /// Shallow assignement
1.71 + CplexEnv& operator=(const CplexEnv&);
1.72 + /// Destructor
1.73 + virtual ~CplexEnv();
1.74 +
1.75 + protected:
1.76 +
1.77 + cpxenv* cplexEnv() { return _env; }
1.78 + const cpxenv* cplexEnv() const { return _env; }
1.79 + };
1.80 +
1.81 + /// \brief Base interface for the CPLEX LP and MIP solver
1.82 + ///
1.83 + /// This class implements the common interface of the CPLEX LP and
1.84 + /// MIP solvers.
1.85 + /// \ingroup lp_group
1.86 + class CplexBase : virtual public LpBase {
1.87 + protected:
1.88 +
1.89 + CplexEnv _env;
1.90 + cpxlp* _prob;
1.91 +
1.92 + CplexBase();
1.93 + CplexBase(const CplexEnv&);
1.94 + CplexBase(const CplexBase &);
1.95 + virtual ~CplexBase();
1.96 +
1.97 + virtual int _addCol();
1.98 + virtual int _addRow();
1.99 +
1.100 + virtual void _eraseCol(int i);
1.101 + virtual void _eraseRow(int i);
1.102 +
1.103 + virtual void _eraseColId(int i);
1.104 + virtual void _eraseRowId(int i);
1.105 +
1.106 + virtual void _getColName(int col, std::string& name) const;
1.107 + virtual void _setColName(int col, const std::string& name);
1.108 + virtual int _colByName(const std::string& name) const;
1.109 +
1.110 + virtual void _getRowName(int row, std::string& name) const;
1.111 + virtual void _setRowName(int row, const std::string& name);
1.112 + virtual int _rowByName(const std::string& name) const;
1.113 +
1.114 + virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
1.115 + virtual void _getRowCoeffs(int i, InsertIterator b) const;
1.116 +
1.117 + virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
1.118 + virtual void _getColCoeffs(int i, InsertIterator b) const;
1.119 +
1.120 + virtual void _setCoeff(int row, int col, Value value);
1.121 + virtual Value _getCoeff(int row, int col) const;
1.122 +
1.123 + virtual void _setColLowerBound(int i, Value value);
1.124 + virtual Value _getColLowerBound(int i) const;
1.125 +
1.126 + virtual void _setColUpperBound(int i, Value value);
1.127 + virtual Value _getColUpperBound(int i) const;
1.128 +
1.129 + private:
1.130 + void _set_row_bounds(int i, Value lb, Value ub);
1.131 + protected:
1.132 +
1.133 + virtual void _setRowLowerBound(int i, Value value);
1.134 + virtual Value _getRowLowerBound(int i) const;
1.135 +
1.136 + virtual void _setRowUpperBound(int i, Value value);
1.137 + virtual Value _getRowUpperBound(int i) const;
1.138 +
1.139 + virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
1.140 + virtual void _getObjCoeffs(InsertIterator b) const;
1.141 +
1.142 + virtual void _setObjCoeff(int i, Value obj_coef);
1.143 + virtual Value _getObjCoeff(int i) const;
1.144 +
1.145 + virtual void _setSense(Sense sense);
1.146 + virtual Sense _getSense() const;
1.147 +
1.148 + virtual void _clear();
1.149 +
1.150 + virtual void _messageLevel(MessageLevel level);
1.151 + void _applyMessageLevel();
1.152 +
1.153 + bool _message_enabled;
1.154 +
1.155 + public:
1.156 +
1.157 + /// Returns the used \c CplexEnv instance
1.158 + const CplexEnv& env() const { return _env; }
1.159 +
1.160 + /// \brief Returns the const cpxenv pointer
1.161 + ///
1.162 + /// \note The cpxenv might be destructed with the solver.
1.163 + const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
1.164 +
1.165 + /// \brief Returns the const cpxenv pointer
1.166 + ///
1.167 + /// \note The cpxenv might be destructed with the solver.
1.168 + cpxenv* cplexEnv() { return _env.cplexEnv(); }
1.169 +
1.170 + /// Returns the cplex problem object
1.171 + cpxlp* cplexLp() { return _prob; }
1.172 + /// Returns the cplex problem object
1.173 + const cpxlp* cplexLp() const { return _prob; }
1.174 +
1.175 + };
1.176 +
1.177 + /// \brief Interface for the CPLEX LP solver
1.178 + ///
1.179 + /// This class implements an interface for the CPLEX LP solver.
1.180 + ///\ingroup lp_group
1.181 + class CplexLp : public LpSolver, public CplexBase {
1.182 + public:
1.183 + /// \e
1.184 + CplexLp();
1.185 + /// \e
1.186 + CplexLp(const CplexEnv&);
1.187 + /// \e
1.188 + CplexLp(const CplexLp&);
1.189 + /// \e
1.190 + virtual ~CplexLp();
1.191 +
1.192 + /// \e
1.193 + virtual CplexLp* cloneSolver() const;
1.194 + /// \e
1.195 + virtual CplexLp* newSolver() const;
1.196 +
1.197 + private:
1.198 +
1.199 + // these values cannot retrieved element by element
1.200 + mutable std::vector<int> _col_status;
1.201 + mutable std::vector<int> _row_status;
1.202 +
1.203 + mutable std::vector<Value> _primal_ray;
1.204 + mutable std::vector<Value> _dual_ray;
1.205 +
1.206 + void _clear_temporals();
1.207 +
1.208 + SolveExitStatus convertStatus(int status);
1.209 +
1.210 + protected:
1.211 +
1.212 + virtual const char* _solverName() const;
1.213 +
1.214 + virtual SolveExitStatus _solve();
1.215 + virtual Value _getPrimal(int i) const;
1.216 + virtual Value _getDual(int i) const;
1.217 + virtual Value _getPrimalValue() const;
1.218 +
1.219 + virtual VarStatus _getColStatus(int i) const;
1.220 + virtual VarStatus _getRowStatus(int i) const;
1.221 +
1.222 + virtual Value _getPrimalRay(int i) const;
1.223 + virtual Value _getDualRay(int i) const;
1.224 +
1.225 + virtual ProblemType _getPrimalType() const;
1.226 + virtual ProblemType _getDualType() const;
1.227 +
1.228 + public:
1.229 +
1.230 + /// Solve with primal simplex method
1.231 + SolveExitStatus solvePrimal();
1.232 +
1.233 + /// Solve with dual simplex method
1.234 + SolveExitStatus solveDual();
1.235 +
1.236 + /// Solve with barrier method
1.237 + SolveExitStatus solveBarrier();
1.238 +
1.239 + };
1.240 +
1.241 + /// \brief Interface for the CPLEX MIP solver
1.242 + ///
1.243 + /// This class implements an interface for the CPLEX MIP solver.
1.244 + ///\ingroup lp_group
1.245 + class CplexMip : public MipSolver, public CplexBase {
1.246 + public:
1.247 + /// \e
1.248 + CplexMip();
1.249 + /// \e
1.250 + CplexMip(const CplexEnv&);
1.251 + /// \e
1.252 + CplexMip(const CplexMip&);
1.253 + /// \e
1.254 + virtual ~CplexMip();
1.255 +
1.256 + /// \e
1.257 + virtual CplexMip* cloneSolver() const;
1.258 + /// \e
1.259 + virtual CplexMip* newSolver() const;
1.260 +
1.261 + protected:
1.262 +
1.263 +
1.264 + virtual const char* _solverName() const;
1.265 +
1.266 + virtual ColTypes _getColType(int col) const;
1.267 + virtual void _setColType(int col, ColTypes col_type);
1.268 +
1.269 + virtual SolveExitStatus _solve();
1.270 + virtual ProblemType _getType() const;
1.271 + virtual Value _getSol(int i) const;
1.272 + virtual Value _getSolValue() const;
1.273 +
1.274 + };
1.275 +
1.276 +} //END OF NAMESPACE LEMON
1.277 +
1.278 +#endif //LEMON_CPLEX_H
1.279 +