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