lemon/lp_cplex.h
changeset 502 17cabb114d52
parent 458 7afc121e0689
equal deleted inserted replaced
0:9da538481152 1:4edd5e3a5b96
    27 struct cpxenv;
    27 struct cpxenv;
    28 struct cpxlp;
    28 struct cpxlp;
    29 
    29 
    30 namespace lemon {
    30 namespace lemon {
    31 
    31 
    32 
    32   /// \brief Reference counted wrapper around cpxenv pointer
    33   /// \brief Interface for the CPLEX solver
    33   ///
    34   ///
    34   /// The cplex uses environment object which is responsible for
    35   /// This class implements an interface for the CPLEX LP solver.
    35   /// checking the proper license usage. This class provides a simple
    36   class LpCplex :virtual public LpSolverBase {
    36   /// interface for share the environment object between different
    37 
    37   /// problems.
    38   public:
    38   class CplexEnv {
    39 
    39     friend class CplexBase;
    40     typedef LpSolverBase Parent;
    40   private:
    41 
    41     cpxenv* _env;
    42     /// \e
    42     mutable int* _cnt;
    43     int status;
    43 
    44     cpxenv* env;
    44   public:
    45     cpxlp* lp;
    45 
    46 
    46     /// \brief This exception is thrown when the license check is not
    47 
    47     /// sufficient
    48     /// \e
    48     class LicenseError : public Exception {
    49     LpCplex();
    49       friend class CplexEnv;
    50     /// \e
    50     private:
    51     LpCplex(const LpCplex&);
    51 
    52     /// \e
    52       LicenseError(int status);
    53     ~LpCplex();
    53       char _message[510];
    54 
    54 
    55   protected:
    55     public:
    56     virtual LpSolverBase* _newLp();
    56 
    57     virtual LpSolverBase* _copyLp();
    57       /// The short error message
    58 
    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();
    59 
    93 
    60     virtual int _addCol();
    94     virtual int _addCol();
    61     virtual int _addRow();
    95     virtual int _addRow();
       
    96 
    62     virtual void _eraseCol(int i);
    97     virtual void _eraseCol(int i);
    63     virtual void _eraseRow(int i);
    98     virtual void _eraseRow(int i);
    64     virtual void _getColName(int col, std::string & name) const;
    99 
    65     virtual void _setColName(int col, const std::string & name);
   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);
    66     virtual int _colByName(const std::string& name) const;
   105     virtual int _colByName(const std::string& name) const;
    67     virtual void _setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e);
   106 
    68     virtual void _getRowCoeffs(int i, RowIterator b) const;
   107     virtual void _getRowName(int row, std::string& name) const;
    69     virtual void _setColCoeffs(int i, ConstColIterator b, ConstColIterator e);
   108     virtual void _setRowName(int row, const std::string& name);
    70     virtual void _getColCoeffs(int i, ColIterator b) const;
   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 
    71     virtual void _setCoeff(int row, int col, Value value);
   117     virtual void _setCoeff(int row, int col, Value value);
    72     virtual Value _getCoeff(int row, int col) const;
   118     virtual Value _getCoeff(int row, int col) const;
    73 
   119 
    74     virtual void _setColLowerBound(int i, Value value);
   120     virtual void _setColLowerBound(int i, Value value);
    75     virtual Value _getColLowerBound(int i) const;
   121     virtual Value _getColLowerBound(int i) const;
       
   122 
    76     virtual void _setColUpperBound(int i, Value value);
   123     virtual void _setColUpperBound(int i, Value value);
    77     virtual Value _getColUpperBound(int i) const;
   124     virtual Value _getColUpperBound(int i) const;
    78 
   125 
    79 //     virtual void _setRowLowerBound(int i, Value value);
   126   private:
    80 //     virtual void _setRowUpperBound(int i, Value value);
   127     void _set_row_bounds(int i, Value lb, Value ub);
    81     virtual void _setRowBounds(int i, Value lower, Value upper);
   128   protected:
    82     virtual void _getRowBounds(int i, Value &lb, Value &ub) const;
   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 
    83     virtual void _setObjCoeff(int i, Value obj_coef);
   139     virtual void _setObjCoeff(int i, Value obj_coef);
    84     virtual Value _getObjCoeff(int i) const;
   140     virtual Value _getObjCoeff(int i) const;
    85     virtual void _clearObj();
   141 
    86 
   142     virtual void _setSense(Sense sense);
       
   143     virtual Sense _getSense() const;
       
   144 
       
   145     virtual void _clear();
       
   146 
       
   147   public:
       
   148 
       
   149     /// Returns the used \c CplexEnv instance
       
   150     const CplexEnv& env() const { return _env; }
       
   151     ///
       
   152     const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
       
   153 
       
   154     cpxlp* cplexLp() { return _prob; }
       
   155     const cpxlp* cplexLp() const { return _prob; }
       
   156 
       
   157   };
       
   158 
       
   159   /// \brief Interface for the CPLEX LP solver
       
   160   ///
       
   161   /// This class implements an interface for the CPLEX LP solver.
       
   162   ///\ingroup lp_group
       
   163   class LpCplex : public CplexBase, public LpSolver {
       
   164   public:
       
   165     /// \e
       
   166     LpCplex();
       
   167     /// \e
       
   168     LpCplex(const CplexEnv&);
       
   169     /// \e
       
   170     LpCplex(const LpCplex&);
       
   171     /// \e
       
   172     virtual ~LpCplex();
       
   173 
       
   174   private:
       
   175 
       
   176     // these values cannot retrieved element by element
       
   177     mutable std::vector<int> _col_status;
       
   178     mutable std::vector<int> _row_status;
       
   179 
       
   180     mutable std::vector<Value> _primal_ray;
       
   181     mutable std::vector<Value> _dual_ray;
       
   182 
       
   183     void _clear_temporals();
       
   184 
       
   185     SolveExitStatus convertStatus(int status);
       
   186 
       
   187   protected:
       
   188 
       
   189     virtual LpCplex* _cloneSolver() const;
       
   190     virtual LpCplex* _newSolver() const;
       
   191 
       
   192     virtual const char* _solverName() const;
    87 
   193 
    88     virtual SolveExitStatus _solve();
   194     virtual SolveExitStatus _solve();
    89     virtual Value _getPrimal(int i) const;
   195     virtual Value _getPrimal(int i) const;
    90     virtual Value _getDual(int i) const;
   196     virtual Value _getDual(int i) const;
    91     virtual Value _getPrimalValue() const;
   197     virtual Value _getPrimalValue() const;
    92     virtual bool _isBasicCol(int i) const;
   198 
    93 
   199     virtual VarStatus _getColStatus(int i) const;
    94     virtual SolutionStatus _getPrimalStatus() const;
   200     virtual VarStatus _getRowStatus(int i) const;
    95     virtual SolutionStatus _getDualStatus() const;
   201 
    96     virtual ProblemTypes _getProblemType() const;
   202     virtual Value _getPrimalRay(int i) const;
    97 
   203     virtual Value _getDualRay(int i) const;
    98 
   204 
    99     virtual void _setMax();
   205     virtual ProblemType _getPrimalType() const;
   100     virtual void _setMin();
   206     virtual ProblemType _getDualType() const;
   101 
   207 
   102     virtual bool _isMax() const;
   208   public:
   103 
   209 
   104   public:
   210     /// Solve with primal simplex method
   105 
   211     SolveExitStatus solvePrimal();
   106     cpxenv* cplexEnv() { return env; }
   212 
   107     cpxlp* cplexLp() { return lp; }
   213     /// Solve with dual simplex method
   108 
   214     SolveExitStatus solveDual();
   109   };
   215 
       
   216     /// Solve with barrier method
       
   217     SolveExitStatus solveBarrier();
       
   218 
       
   219   };
       
   220 
       
   221   /// \brief Interface for the CPLEX MIP solver
       
   222   ///
       
   223   /// This class implements an interface for the CPLEX MIP solver.
       
   224   ///\ingroup lp_group
       
   225   class MipCplex : public CplexBase, public MipSolver {
       
   226   public:
       
   227     /// \e
       
   228     MipCplex();
       
   229     /// \e
       
   230     MipCplex(const CplexEnv&);
       
   231     /// \e
       
   232     MipCplex(const MipCplex&);
       
   233     /// \e
       
   234     virtual ~MipCplex();
       
   235 
       
   236   protected:
       
   237 
       
   238     virtual MipCplex* _cloneSolver() const;
       
   239     virtual MipCplex* _newSolver() const;
       
   240 
       
   241     virtual const char* _solverName() const;
       
   242 
       
   243     virtual ColTypes _getColType(int col) const;
       
   244     virtual void _setColType(int col, ColTypes col_type);
       
   245 
       
   246     virtual SolveExitStatus _solve();
       
   247     virtual ProblemType _getType() const;
       
   248     virtual Value _getSol(int i) const;
       
   249     virtual Value _getSolValue() const;
       
   250 
       
   251   };
       
   252 
   110 } //END OF NAMESPACE LEMON
   253 } //END OF NAMESPACE LEMON
   111 
   254 
   112 #endif //LEMON_LP_CPLEX_H
   255 #endif //LEMON_LP_CPLEX_H
   113 
   256