lemon/lp_glpk.h
changeset 459 ed54c0d13df0
parent 458 7afc121e0689
     1.1 --- a/lemon/lp_glpk.h	Tue Dec 02 21:40:33 2008 +0100
     1.2 +++ b/lemon/lp_glpk.h	Tue Dec 02 22:48:28 2008 +0100
     1.3 @@ -35,88 +35,137 @@
     1.4  namespace lemon {
     1.5  
     1.6  
     1.7 -  /// \brief Interface for the GLPK LP solver
     1.8 +  /// \brief Base interface for the GLPK LP and MIP solver
     1.9    ///
    1.10 -  /// This class implements an interface for the GLPK LP solver.
    1.11 -  ///\ingroup lp_group
    1.12 -  class LpGlpk : virtual public LpSolverBase {
    1.13 +  /// This class implements the common interface of the GLPK LP and MIP solver.
    1.14 +  /// \ingroup lp_group
    1.15 +  class GlpkBase : virtual public LpBase {
    1.16    protected:
    1.17  
    1.18      typedef glp_prob LPX;
    1.19      glp_prob* lp;
    1.20 -    bool solved;
    1.21  
    1.22 -  public:
    1.23 -
    1.24 -    typedef LpSolverBase Parent;
    1.25 -
    1.26 -    LpGlpk();
    1.27 -    LpGlpk(const LpGlpk &);
    1.28 -    ~LpGlpk();
    1.29 +    GlpkBase();
    1.30 +    GlpkBase(const GlpkBase&);
    1.31 +    virtual ~GlpkBase();
    1.32  
    1.33    protected:
    1.34 -    virtual LpSolverBase* _newLp();
    1.35 -    virtual LpSolverBase* _copyLp();
    1.36  
    1.37      virtual int _addCol();
    1.38      virtual int _addRow();
    1.39 +
    1.40      virtual void _eraseCol(int i);
    1.41      virtual void _eraseRow(int i);
    1.42 -    virtual void _getColName(int col, std::string & name) const;
    1.43 -    virtual void _setColName(int col, const std::string & name);
    1.44 +
    1.45 +    virtual void _eraseColId(int i);
    1.46 +    virtual void _eraseRowId(int i);
    1.47 +
    1.48 +    virtual void _getColName(int col, std::string& name) const;
    1.49 +    virtual void _setColName(int col, const std::string& name);
    1.50      virtual int _colByName(const std::string& name) const;
    1.51 -    virtual void _setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e);
    1.52 -    virtual void _getRowCoeffs(int i, RowIterator b) const;
    1.53 -    virtual void _setColCoeffs(int i, ConstColIterator b, ConstColIterator e);
    1.54 -    virtual void _getColCoeffs(int i, ColIterator b) const;
    1.55 +
    1.56 +    virtual void _getRowName(int row, std::string& name) const;
    1.57 +    virtual void _setRowName(int row, const std::string& name);
    1.58 +    virtual int _rowByName(const std::string& name) const;
    1.59 +
    1.60 +    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
    1.61 +    virtual void _getRowCoeffs(int i, InsertIterator b) const;
    1.62 +
    1.63 +    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
    1.64 +    virtual void _getColCoeffs(int i, InsertIterator b) const;
    1.65 +
    1.66      virtual void _setCoeff(int row, int col, Value value);
    1.67      virtual Value _getCoeff(int row, int col) const;
    1.68  
    1.69      virtual void _setColLowerBound(int i, Value value);
    1.70      virtual Value _getColLowerBound(int i) const;
    1.71 +
    1.72      virtual void _setColUpperBound(int i, Value value);
    1.73      virtual Value _getColUpperBound(int i) const;
    1.74  
    1.75 -    virtual void _setRowBounds(int i, Value lower, Value upper);
    1.76 -    virtual void _getRowBounds(int i, Value &lb, Value &ub) const;
    1.77 +    virtual void _setRowLowerBound(int i, Value value);
    1.78 +    virtual Value _getRowLowerBound(int i) const;
    1.79 +
    1.80 +    virtual void _setRowUpperBound(int i, Value value);
    1.81 +    virtual Value _getRowUpperBound(int i) const;
    1.82 +
    1.83 +    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
    1.84 +    virtual void _getObjCoeffs(InsertIterator b) const;
    1.85 +
    1.86      virtual void _setObjCoeff(int i, Value obj_coef);
    1.87      virtual Value _getObjCoeff(int i) const;
    1.88 -    virtual void _clearObj();
    1.89 +
    1.90 +    virtual void _setSense(Sense);
    1.91 +    virtual Sense _getSense() const;
    1.92 +
    1.93 +    virtual void _clear();
    1.94 +
    1.95 +  public:
    1.96 +
    1.97 +    ///Pointer to the underlying GLPK data structure.
    1.98 +    LPX *lpx() {return lp;}
    1.99 +    ///Const pointer to the underlying GLPK data structure.
   1.100 +    const LPX *lpx() const {return lp;}
   1.101 +
   1.102 +    ///Returns the constraint identifier understood by GLPK.
   1.103 +    int lpxRow(Row r) const { return rows(id(r)); }
   1.104 +
   1.105 +    ///Returns the variable identifier understood by GLPK.
   1.106 +    int lpxCol(Col c) const { return cols(id(c)); }
   1.107 +
   1.108 +  };
   1.109 +
   1.110 +  /// \brief Interface for the GLPK LP solver
   1.111 +  ///
   1.112 +  /// This class implements an interface for the GLPK LP solver.
   1.113 +  ///\ingroup lp_group
   1.114 +  class LpGlpk : public GlpkBase, public LpSolver {
   1.115 +  public:
   1.116  
   1.117      ///\e
   1.118 +    LpGlpk();
   1.119 +    ///\e
   1.120 +    LpGlpk(const LpGlpk&);
   1.121 +
   1.122 +  private:
   1.123 +
   1.124 +    mutable std::vector<double> _primal_ray;
   1.125 +    mutable std::vector<double> _dual_ray;
   1.126 +
   1.127 +    void _clear_temporals();
   1.128 +
   1.129 +  protected:
   1.130 +
   1.131 +    virtual LpGlpk* _cloneSolver() const;
   1.132 +    virtual LpGlpk* _newSolver() const;
   1.133 +
   1.134 +    virtual const char* _solverName() const;
   1.135 +
   1.136 +    virtual SolveExitStatus _solve();
   1.137 +    virtual Value _getPrimal(int i) const;
   1.138 +    virtual Value _getDual(int i) const;
   1.139 +
   1.140 +    virtual Value _getPrimalValue() const;
   1.141 +
   1.142 +    virtual VarStatus _getColStatus(int i) const;
   1.143 +    virtual VarStatus _getRowStatus(int i) const;
   1.144 +
   1.145 +    virtual Value _getPrimalRay(int i) const;
   1.146 +    virtual Value _getDualRay(int i) const;
   1.147  
   1.148      ///\todo It should be clarified
   1.149      ///
   1.150 -    virtual SolveExitStatus _solve();
   1.151 -    virtual Value _getPrimal(int i) const;
   1.152 -    virtual Value _getDual(int i) const;
   1.153 -    virtual Value _getPrimalValue() const;
   1.154 -    virtual bool _isBasicCol(int i) const;
   1.155 -    ///\e
   1.156 -
   1.157 -    ///\todo It should be clarified
   1.158 -    ///
   1.159 -    virtual SolutionStatus _getPrimalStatus() const;
   1.160 -    virtual SolutionStatus _getDualStatus() const;
   1.161 -    virtual ProblemTypes _getProblemType() const;
   1.162 -
   1.163 -    virtual void _setMax();
   1.164 -    virtual void _setMin();
   1.165 -
   1.166 -    virtual bool _isMax() const;
   1.167 +    virtual ProblemType _getPrimalType() const;
   1.168 +    virtual ProblemType _getDualType() const;
   1.169  
   1.170    public:
   1.171 -    ///Set the verbosity of the messages
   1.172  
   1.173 -    ///Set the verbosity of the messages
   1.174 -    ///
   1.175 -    ///\param m is the level of the messages output by the solver routines.
   1.176 -    ///The possible values are:
   1.177 -    ///- 0 --- no output (default value)
   1.178 -    ///- 1 --- error messages only
   1.179 -    ///- 2 --- normal output
   1.180 -    ///- 3 --- full output (includes informational messages)
   1.181 -    void messageLevel(int m);
   1.182 +    ///Solve with primal simplex
   1.183 +    SolveExitStatus solvePrimal();
   1.184 +
   1.185 +    ///Solve with dual simplex
   1.186 +    SolveExitStatus solveDual();
   1.187 +
   1.188      ///Turns on or off the presolver
   1.189  
   1.190      ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   1.191 @@ -124,15 +173,86 @@
   1.192      ///The presolver is off by default.
   1.193      void presolver(bool b);
   1.194  
   1.195 -    ///Pointer to the underlying GLPK data structure.
   1.196 -    LPX *lpx() {return lp;}
   1.197 +    ///Enum for \c messageLevel() parameter
   1.198 +    enum MessageLevel {
   1.199 +      /// no output (default value)
   1.200 +      MESSAGE_NO_OUTPUT = 0,
   1.201 +      /// error messages only
   1.202 +      MESSAGE_ERROR_MESSAGE = 1,
   1.203 +      /// normal output
   1.204 +      MESSAGE_NORMAL_OUTPUT = 2,
   1.205 +      /// full output (includes informational messages)
   1.206 +      MESSAGE_FULL_OUTPUT = 3
   1.207 +    };
   1.208  
   1.209 -    ///Returns the constraint identifier understood by GLPK.
   1.210 -    int lpxRow(Row r) { return _lpId(r); }
   1.211 +  private:
   1.212  
   1.213 -    ///Returns the variable identifier understood by GLPK.
   1.214 -    int lpxCol(Col c) { return _lpId(c); }
   1.215 +    MessageLevel _message_level;
   1.216 +
   1.217 +  public:
   1.218 +
   1.219 +    ///Set the verbosity of the messages
   1.220 +
   1.221 +    ///Set the verbosity of the messages
   1.222 +    ///
   1.223 +    ///\param m is the level of the messages output by the solver routines.
   1.224 +    void messageLevel(MessageLevel m);
   1.225    };
   1.226 +
   1.227 +  /// \brief Interface for the GLPK MIP solver
   1.228 +  ///
   1.229 +  /// This class implements an interface for the GLPK MIP solver.
   1.230 +  ///\ingroup lp_group
   1.231 +  class MipGlpk : public GlpkBase, public MipSolver {
   1.232 +  public:
   1.233 +
   1.234 +    ///\e
   1.235 +    MipGlpk();
   1.236 +    ///\e
   1.237 +    MipGlpk(const MipGlpk&);
   1.238 +
   1.239 +  protected:
   1.240 +
   1.241 +    virtual MipGlpk* _cloneSolver() const;
   1.242 +    virtual MipGlpk* _newSolver() const;
   1.243 +
   1.244 +    virtual const char* _solverName() const;
   1.245 +
   1.246 +    virtual ColTypes _getColType(int col) const;
   1.247 +    virtual void _setColType(int col, ColTypes col_type);
   1.248 +
   1.249 +    virtual SolveExitStatus _solve();
   1.250 +    virtual ProblemType _getType() const;
   1.251 +    virtual Value _getSol(int i) const;
   1.252 +    virtual Value _getSolValue() const;
   1.253 +
   1.254 +    ///Enum for \c messageLevel() parameter
   1.255 +    enum MessageLevel {
   1.256 +      /// no output (default value)
   1.257 +      MESSAGE_NO_OUTPUT = 0,
   1.258 +      /// error messages only
   1.259 +      MESSAGE_ERROR_MESSAGE = 1,
   1.260 +      /// normal output
   1.261 +      MESSAGE_NORMAL_OUTPUT = 2,
   1.262 +      /// full output (includes informational messages)
   1.263 +      MESSAGE_FULL_OUTPUT = 3
   1.264 +    };
   1.265 +
   1.266 +  private:
   1.267 +
   1.268 +    MessageLevel _message_level;
   1.269 +
   1.270 +  public:
   1.271 +
   1.272 +    ///Set the verbosity of the messages
   1.273 +
   1.274 +    ///Set the verbosity of the messages
   1.275 +    ///
   1.276 +    ///\param m is the level of the messages output by the solver routines.
   1.277 +    void messageLevel(MessageLevel m);
   1.278 +  };
   1.279 +
   1.280 +
   1.281  } //END OF NAMESPACE LEMON
   1.282  
   1.283  #endif //LEMON_LP_GLPK_H