lemon/lp_cplex.h
changeset 459 ed54c0d13df0
parent 458 7afc121e0689
     1.1 --- a/lemon/lp_cplex.h	Tue Dec 02 21:40:33 2008 +0100
     1.2 +++ b/lemon/lp_cplex.h	Tue Dec 02 22:48:28 2008 +0100
     1.3 @@ -29,84 +29,227 @@
     1.4  
     1.5  namespace lemon {
     1.6  
     1.7 -
     1.8 -  /// \brief Interface for the CPLEX solver
     1.9 +  /// \brief Reference counted wrapper around cpxenv pointer
    1.10    ///
    1.11 -  /// This class implements an interface for the CPLEX LP solver.
    1.12 -  class LpCplex :virtual public LpSolverBase {
    1.13 +  /// The cplex uses environment object which is responsible for
    1.14 +  /// checking the proper license usage. This class provides a simple
    1.15 +  /// interface for share the environment object between different
    1.16 +  /// problems.
    1.17 +  class CplexEnv {
    1.18 +    friend class CplexBase;
    1.19 +  private:
    1.20 +    cpxenv* _env;
    1.21 +    mutable int* _cnt;
    1.22  
    1.23    public:
    1.24  
    1.25 -    typedef LpSolverBase Parent;
    1.26 +    /// \brief This exception is thrown when the license check is not
    1.27 +    /// sufficient
    1.28 +    class LicenseError : public Exception {
    1.29 +      friend class CplexEnv;
    1.30 +    private:
    1.31  
    1.32 -    /// \e
    1.33 -    int status;
    1.34 -    cpxenv* env;
    1.35 -    cpxlp* lp;
    1.36 +      LicenseError(int status);
    1.37 +      char _message[510];
    1.38  
    1.39 +    public:
    1.40  
    1.41 -    /// \e
    1.42 -    LpCplex();
    1.43 -    /// \e
    1.44 -    LpCplex(const LpCplex&);
    1.45 -    /// \e
    1.46 -    ~LpCplex();
    1.47 +      /// The short error message
    1.48 +      virtual const char* what() const throw() {
    1.49 +        return _message;
    1.50 +      }
    1.51 +    };
    1.52 +
    1.53 +    /// Constructor
    1.54 +    CplexEnv();
    1.55 +    /// Shallow copy constructor
    1.56 +    CplexEnv(const CplexEnv&);
    1.57 +    /// Shallow assignement
    1.58 +    CplexEnv& operator=(const CplexEnv&);
    1.59 +    /// Destructor
    1.60 +    virtual ~CplexEnv();
    1.61  
    1.62    protected:
    1.63 -    virtual LpSolverBase* _newLp();
    1.64 -    virtual LpSolverBase* _copyLp();
    1.65  
    1.66 +    cpxenv* cplexEnv() { return _env; }
    1.67 +    const cpxenv* cplexEnv() const { return _env; }
    1.68 +  };
    1.69 +
    1.70 +  /// \brief Base interface for the CPLEX LP and MIP solver
    1.71 +  ///
    1.72 +  /// This class implements the common interface of the CPLEX LP and
    1.73 +  /// MIP solvers.  
    1.74 +  /// \ingroup lp_group
    1.75 +  class CplexBase : virtual public LpBase {
    1.76 +  protected:
    1.77 +
    1.78 +    CplexEnv _env;
    1.79 +    cpxlp* _prob;
    1.80 +
    1.81 +    CplexBase();
    1.82 +    CplexBase(const CplexEnv&);
    1.83 +    CplexBase(const CplexBase &);
    1.84 +    virtual ~CplexBase();
    1.85  
    1.86      virtual int _addCol();
    1.87      virtual int _addRow();
    1.88 +
    1.89      virtual void _eraseCol(int i);
    1.90      virtual void _eraseRow(int i);
    1.91 -    virtual void _getColName(int col, std::string & name) const;
    1.92 -    virtual void _setColName(int col, const std::string & name);
    1.93 +
    1.94 +    virtual void _eraseColId(int i);
    1.95 +    virtual void _eraseRowId(int i);
    1.96 +
    1.97 +    virtual void _getColName(int col, std::string& name) const;
    1.98 +    virtual void _setColName(int col, const std::string& name);
    1.99      virtual int _colByName(const std::string& name) const;
   1.100 -    virtual void _setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e);
   1.101 -    virtual void _getRowCoeffs(int i, RowIterator b) const;
   1.102 -    virtual void _setColCoeffs(int i, ConstColIterator b, ConstColIterator e);
   1.103 -    virtual void _getColCoeffs(int i, ColIterator b) const;
   1.104 +
   1.105 +    virtual void _getRowName(int row, std::string& name) const;
   1.106 +    virtual void _setRowName(int row, const std::string& name);
   1.107 +    virtual int _rowByName(const std::string& name) const;
   1.108 +
   1.109 +    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
   1.110 +    virtual void _getRowCoeffs(int i, InsertIterator b) const;
   1.111 +
   1.112 +    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
   1.113 +    virtual void _getColCoeffs(int i, InsertIterator b) const;
   1.114 +
   1.115      virtual void _setCoeff(int row, int col, Value value);
   1.116      virtual Value _getCoeff(int row, int col) const;
   1.117  
   1.118      virtual void _setColLowerBound(int i, Value value);
   1.119      virtual Value _getColLowerBound(int i) const;
   1.120 +
   1.121      virtual void _setColUpperBound(int i, Value value);
   1.122      virtual Value _getColUpperBound(int i) const;
   1.123  
   1.124 -//     virtual void _setRowLowerBound(int i, Value value);
   1.125 -//     virtual void _setRowUpperBound(int i, Value value);
   1.126 -    virtual void _setRowBounds(int i, Value lower, Value upper);
   1.127 -    virtual void _getRowBounds(int i, Value &lb, Value &ub) const;
   1.128 +  private:
   1.129 +    void _set_row_bounds(int i, Value lb, Value ub);
   1.130 +  protected:
   1.131 +
   1.132 +    virtual void _setRowLowerBound(int i, Value value);
   1.133 +    virtual Value _getRowLowerBound(int i) const;
   1.134 +
   1.135 +    virtual void _setRowUpperBound(int i, Value value);
   1.136 +    virtual Value _getRowUpperBound(int i) const;
   1.137 +
   1.138 +    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
   1.139 +    virtual void _getObjCoeffs(InsertIterator b) const;
   1.140 +
   1.141      virtual void _setObjCoeff(int i, Value obj_coef);
   1.142      virtual Value _getObjCoeff(int i) const;
   1.143 -    virtual void _clearObj();
   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 +  public:
   1.151 +
   1.152 +    /// Returns the used \c CplexEnv instance
   1.153 +    const CplexEnv& env() const { return _env; }
   1.154 +    ///
   1.155 +    const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
   1.156 +
   1.157 +    cpxlp* cplexLp() { return _prob; }
   1.158 +    const cpxlp* cplexLp() const { return _prob; }
   1.159 +
   1.160 +  };
   1.161 +
   1.162 +  /// \brief Interface for the CPLEX LP solver
   1.163 +  ///
   1.164 +  /// This class implements an interface for the CPLEX LP solver.
   1.165 +  ///\ingroup lp_group
   1.166 +  class LpCplex : public CplexBase, public LpSolver {
   1.167 +  public:
   1.168 +    /// \e
   1.169 +    LpCplex();
   1.170 +    /// \e
   1.171 +    LpCplex(const CplexEnv&);
   1.172 +    /// \e
   1.173 +    LpCplex(const LpCplex&);
   1.174 +    /// \e
   1.175 +    virtual ~LpCplex();
   1.176 +
   1.177 +  private:
   1.178 +
   1.179 +    // these values cannot retrieved element by element
   1.180 +    mutable std::vector<int> _col_status;
   1.181 +    mutable std::vector<int> _row_status;
   1.182 +
   1.183 +    mutable std::vector<Value> _primal_ray;
   1.184 +    mutable std::vector<Value> _dual_ray;
   1.185 +
   1.186 +    void _clear_temporals();
   1.187 +
   1.188 +    SolveExitStatus convertStatus(int status);
   1.189 +
   1.190 +  protected:
   1.191 +
   1.192 +    virtual LpCplex* _cloneSolver() const;
   1.193 +    virtual LpCplex* _newSolver() const;
   1.194 +
   1.195 +    virtual const char* _solverName() const;
   1.196  
   1.197      virtual SolveExitStatus _solve();
   1.198      virtual Value _getPrimal(int i) const;
   1.199      virtual Value _getDual(int i) const;
   1.200      virtual Value _getPrimalValue() const;
   1.201 -    virtual bool _isBasicCol(int i) const;
   1.202  
   1.203 -    virtual SolutionStatus _getPrimalStatus() const;
   1.204 -    virtual SolutionStatus _getDualStatus() const;
   1.205 -    virtual ProblemTypes _getProblemType() const;
   1.206 +    virtual VarStatus _getColStatus(int i) const;
   1.207 +    virtual VarStatus _getRowStatus(int i) const;
   1.208  
   1.209 +    virtual Value _getPrimalRay(int i) const;
   1.210 +    virtual Value _getDualRay(int i) const;
   1.211  
   1.212 -    virtual void _setMax();
   1.213 -    virtual void _setMin();
   1.214 -
   1.215 -    virtual bool _isMax() const;
   1.216 +    virtual ProblemType _getPrimalType() const;
   1.217 +    virtual ProblemType _getDualType() const;
   1.218  
   1.219    public:
   1.220  
   1.221 -    cpxenv* cplexEnv() { return env; }
   1.222 -    cpxlp* cplexLp() { return lp; }
   1.223 +    /// Solve with primal simplex method
   1.224 +    SolveExitStatus solvePrimal();
   1.225 +
   1.226 +    /// Solve with dual simplex method
   1.227 +    SolveExitStatus solveDual();
   1.228 +
   1.229 +    /// Solve with barrier method
   1.230 +    SolveExitStatus solveBarrier();
   1.231  
   1.232    };
   1.233 +
   1.234 +  /// \brief Interface for the CPLEX MIP solver
   1.235 +  ///
   1.236 +  /// This class implements an interface for the CPLEX MIP solver.
   1.237 +  ///\ingroup lp_group
   1.238 +  class MipCplex : public CplexBase, public MipSolver {
   1.239 +  public:
   1.240 +    /// \e
   1.241 +    MipCplex();
   1.242 +    /// \e
   1.243 +    MipCplex(const CplexEnv&);
   1.244 +    /// \e
   1.245 +    MipCplex(const MipCplex&);
   1.246 +    /// \e
   1.247 +    virtual ~MipCplex();
   1.248 +
   1.249 +  protected:
   1.250 +
   1.251 +    virtual MipCplex* _cloneSolver() const;
   1.252 +    virtual MipCplex* _newSolver() const;
   1.253 +
   1.254 +    virtual const char* _solverName() const;
   1.255 +
   1.256 +    virtual ColTypes _getColType(int col) const;
   1.257 +    virtual void _setColType(int col, ColTypes col_type);
   1.258 +
   1.259 +    virtual SolveExitStatus _solve();
   1.260 +    virtual ProblemType _getType() const;
   1.261 +    virtual Value _getSol(int i) const;
   1.262 +    virtual Value _getSolValue() const;
   1.263 +
   1.264 +  };
   1.265 +
   1.266  } //END OF NAMESPACE LEMON
   1.267  
   1.268  #endif //LEMON_LP_CPLEX_H