lemon/lp_glpk.h
changeset 459 ed54c0d13df0
parent 458 7afc121e0689
equal deleted inserted replaced
0:e7cab6aba6f1 1:11d28249361a
    33 #endif
    33 #endif
    34 
    34 
    35 namespace lemon {
    35 namespace lemon {
    36 
    36 
    37 
    37 
       
    38   /// \brief Base interface for the GLPK LP and MIP solver
       
    39   ///
       
    40   /// This class implements the common interface of the GLPK LP and MIP solver.
       
    41   /// \ingroup lp_group
       
    42   class GlpkBase : virtual public LpBase {
       
    43   protected:
       
    44 
       
    45     typedef glp_prob LPX;
       
    46     glp_prob* lp;
       
    47 
       
    48     GlpkBase();
       
    49     GlpkBase(const GlpkBase&);
       
    50     virtual ~GlpkBase();
       
    51 
       
    52   protected:
       
    53 
       
    54     virtual int _addCol();
       
    55     virtual int _addRow();
       
    56 
       
    57     virtual void _eraseCol(int i);
       
    58     virtual void _eraseRow(int i);
       
    59 
       
    60     virtual void _eraseColId(int i);
       
    61     virtual void _eraseRowId(int i);
       
    62 
       
    63     virtual void _getColName(int col, std::string& name) const;
       
    64     virtual void _setColName(int col, const std::string& name);
       
    65     virtual int _colByName(const std::string& name) const;
       
    66 
       
    67     virtual void _getRowName(int row, std::string& name) const;
       
    68     virtual void _setRowName(int row, const std::string& name);
       
    69     virtual int _rowByName(const std::string& name) const;
       
    70 
       
    71     virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
       
    72     virtual void _getRowCoeffs(int i, InsertIterator b) const;
       
    73 
       
    74     virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
       
    75     virtual void _getColCoeffs(int i, InsertIterator b) const;
       
    76 
       
    77     virtual void _setCoeff(int row, int col, Value value);
       
    78     virtual Value _getCoeff(int row, int col) const;
       
    79 
       
    80     virtual void _setColLowerBound(int i, Value value);
       
    81     virtual Value _getColLowerBound(int i) const;
       
    82 
       
    83     virtual void _setColUpperBound(int i, Value value);
       
    84     virtual Value _getColUpperBound(int i) const;
       
    85 
       
    86     virtual void _setRowLowerBound(int i, Value value);
       
    87     virtual Value _getRowLowerBound(int i) const;
       
    88 
       
    89     virtual void _setRowUpperBound(int i, Value value);
       
    90     virtual Value _getRowUpperBound(int i) const;
       
    91 
       
    92     virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
       
    93     virtual void _getObjCoeffs(InsertIterator b) const;
       
    94 
       
    95     virtual void _setObjCoeff(int i, Value obj_coef);
       
    96     virtual Value _getObjCoeff(int i) const;
       
    97 
       
    98     virtual void _setSense(Sense);
       
    99     virtual Sense _getSense() const;
       
   100 
       
   101     virtual void _clear();
       
   102 
       
   103   public:
       
   104 
       
   105     ///Pointer to the underlying GLPK data structure.
       
   106     LPX *lpx() {return lp;}
       
   107     ///Const pointer to the underlying GLPK data structure.
       
   108     const LPX *lpx() const {return lp;}
       
   109 
       
   110     ///Returns the constraint identifier understood by GLPK.
       
   111     int lpxRow(Row r) const { return rows(id(r)); }
       
   112 
       
   113     ///Returns the variable identifier understood by GLPK.
       
   114     int lpxCol(Col c) const { return cols(id(c)); }
       
   115 
       
   116   };
       
   117 
    38   /// \brief Interface for the GLPK LP solver
   118   /// \brief Interface for the GLPK LP solver
    39   ///
   119   ///
    40   /// This class implements an interface for the GLPK LP solver.
   120   /// This class implements an interface for the GLPK LP solver.
    41   ///\ingroup lp_group
   121   ///\ingroup lp_group
    42   class LpGlpk : virtual public LpSolverBase {
   122   class LpGlpk : public GlpkBase, public LpSolver {
    43   protected:
   123   public:
    44 
   124 
    45     typedef glp_prob LPX;
   125     ///\e
    46     glp_prob* lp;
       
    47     bool solved;
       
    48 
       
    49   public:
       
    50 
       
    51     typedef LpSolverBase Parent;
       
    52 
       
    53     LpGlpk();
   126     LpGlpk();
    54     LpGlpk(const LpGlpk &);
   127     ///\e
    55     ~LpGlpk();
   128     LpGlpk(const LpGlpk&);
    56 
   129 
    57   protected:
   130   private:
    58     virtual LpSolverBase* _newLp();
   131 
    59     virtual LpSolverBase* _copyLp();
   132     mutable std::vector<double> _primal_ray;
    60 
   133     mutable std::vector<double> _dual_ray;
    61     virtual int _addCol();
   134 
    62     virtual int _addRow();
   135     void _clear_temporals();
    63     virtual void _eraseCol(int i);
   136 
    64     virtual void _eraseRow(int i);
   137   protected:
    65     virtual void _getColName(int col, std::string & name) const;
   138 
    66     virtual void _setColName(int col, const std::string & name);
   139     virtual LpGlpk* _cloneSolver() const;
    67     virtual int _colByName(const std::string& name) const;
   140     virtual LpGlpk* _newSolver() const;
    68     virtual void _setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e);
   141 
    69     virtual void _getRowCoeffs(int i, RowIterator b) const;
   142     virtual const char* _solverName() const;
    70     virtual void _setColCoeffs(int i, ConstColIterator b, ConstColIterator e);
   143 
    71     virtual void _getColCoeffs(int i, ColIterator b) const;
       
    72     virtual void _setCoeff(int row, int col, Value value);
       
    73     virtual Value _getCoeff(int row, int col) const;
       
    74 
       
    75     virtual void _setColLowerBound(int i, Value value);
       
    76     virtual Value _getColLowerBound(int i) const;
       
    77     virtual void _setColUpperBound(int i, Value value);
       
    78     virtual Value _getColUpperBound(int i) const;
       
    79 
       
    80     virtual void _setRowBounds(int i, Value lower, Value upper);
       
    81     virtual void _getRowBounds(int i, Value &lb, Value &ub) const;
       
    82     virtual void _setObjCoeff(int i, Value obj_coef);
       
    83     virtual Value _getObjCoeff(int i) const;
       
    84     virtual void _clearObj();
       
    85 
       
    86     ///\e
       
    87 
       
    88     ///\todo It should be clarified
       
    89     ///
       
    90     virtual SolveExitStatus _solve();
   144     virtual SolveExitStatus _solve();
    91     virtual Value _getPrimal(int i) const;
   145     virtual Value _getPrimal(int i) const;
    92     virtual Value _getDual(int i) const;
   146     virtual Value _getDual(int i) const;
       
   147 
    93     virtual Value _getPrimalValue() const;
   148     virtual Value _getPrimalValue() const;
    94     virtual bool _isBasicCol(int i) const;
   149 
    95     ///\e
   150     virtual VarStatus _getColStatus(int i) const;
       
   151     virtual VarStatus _getRowStatus(int i) const;
       
   152 
       
   153     virtual Value _getPrimalRay(int i) const;
       
   154     virtual Value _getDualRay(int i) const;
    96 
   155 
    97     ///\todo It should be clarified
   156     ///\todo It should be clarified
    98     ///
   157     ///
    99     virtual SolutionStatus _getPrimalStatus() const;
   158     virtual ProblemType _getPrimalType() const;
   100     virtual SolutionStatus _getDualStatus() const;
   159     virtual ProblemType _getDualType() const;
   101     virtual ProblemTypes _getProblemType() const;
   160 
   102 
   161   public:
   103     virtual void _setMax();
   162 
   104     virtual void _setMin();
   163     ///Solve with primal simplex
   105 
   164     SolveExitStatus solvePrimal();
   106     virtual bool _isMax() const;
   165 
   107 
   166     ///Solve with dual simplex
   108   public:
   167     SolveExitStatus solveDual();
   109     ///Set the verbosity of the messages
   168 
   110 
       
   111     ///Set the verbosity of the messages
       
   112     ///
       
   113     ///\param m is the level of the messages output by the solver routines.
       
   114     ///The possible values are:
       
   115     ///- 0 --- no output (default value)
       
   116     ///- 1 --- error messages only
       
   117     ///- 2 --- normal output
       
   118     ///- 3 --- full output (includes informational messages)
       
   119     void messageLevel(int m);
       
   120     ///Turns on or off the presolver
   169     ///Turns on or off the presolver
   121 
   170 
   122     ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   171     ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   123     ///
   172     ///
   124     ///The presolver is off by default.
   173     ///The presolver is off by default.
   125     void presolver(bool b);
   174     void presolver(bool b);
   126 
   175 
   127     ///Pointer to the underlying GLPK data structure.
   176     ///Enum for \c messageLevel() parameter
   128     LPX *lpx() {return lp;}
   177     enum MessageLevel {
   129 
   178       /// no output (default value)
   130     ///Returns the constraint identifier understood by GLPK.
   179       MESSAGE_NO_OUTPUT = 0,
   131     int lpxRow(Row r) { return _lpId(r); }
   180       /// error messages only
   132 
   181       MESSAGE_ERROR_MESSAGE = 1,
   133     ///Returns the variable identifier understood by GLPK.
   182       /// normal output
   134     int lpxCol(Col c) { return _lpId(c); }
   183       MESSAGE_NORMAL_OUTPUT = 2,
       
   184       /// full output (includes informational messages)
       
   185       MESSAGE_FULL_OUTPUT = 3
       
   186     };
       
   187 
       
   188   private:
       
   189 
       
   190     MessageLevel _message_level;
       
   191 
       
   192   public:
       
   193 
       
   194     ///Set the verbosity of the messages
       
   195 
       
   196     ///Set the verbosity of the messages
       
   197     ///
       
   198     ///\param m is the level of the messages output by the solver routines.
       
   199     void messageLevel(MessageLevel m);
   135   };
   200   };
       
   201 
       
   202   /// \brief Interface for the GLPK MIP solver
       
   203   ///
       
   204   /// This class implements an interface for the GLPK MIP solver.
       
   205   ///\ingroup lp_group
       
   206   class MipGlpk : public GlpkBase, public MipSolver {
       
   207   public:
       
   208 
       
   209     ///\e
       
   210     MipGlpk();
       
   211     ///\e
       
   212     MipGlpk(const MipGlpk&);
       
   213 
       
   214   protected:
       
   215 
       
   216     virtual MipGlpk* _cloneSolver() const;
       
   217     virtual MipGlpk* _newSolver() const;
       
   218 
       
   219     virtual const char* _solverName() const;
       
   220 
       
   221     virtual ColTypes _getColType(int col) const;
       
   222     virtual void _setColType(int col, ColTypes col_type);
       
   223 
       
   224     virtual SolveExitStatus _solve();
       
   225     virtual ProblemType _getType() const;
       
   226     virtual Value _getSol(int i) const;
       
   227     virtual Value _getSolValue() const;
       
   228 
       
   229     ///Enum for \c messageLevel() parameter
       
   230     enum MessageLevel {
       
   231       /// no output (default value)
       
   232       MESSAGE_NO_OUTPUT = 0,
       
   233       /// error messages only
       
   234       MESSAGE_ERROR_MESSAGE = 1,
       
   235       /// normal output
       
   236       MESSAGE_NORMAL_OUTPUT = 2,
       
   237       /// full output (includes informational messages)
       
   238       MESSAGE_FULL_OUTPUT = 3
       
   239     };
       
   240 
       
   241   private:
       
   242 
       
   243     MessageLevel _message_level;
       
   244 
       
   245   public:
       
   246 
       
   247     ///Set the verbosity of the messages
       
   248 
       
   249     ///Set the verbosity of the messages
       
   250     ///
       
   251     ///\param m is the level of the messages output by the solver routines.
       
   252     void messageLevel(MessageLevel m);
       
   253   };
       
   254 
       
   255 
   136 } //END OF NAMESPACE LEMON
   256 } //END OF NAMESPACE LEMON
   137 
   257 
   138 #endif //LEMON_LP_GLPK_H
   258 #endif //LEMON_LP_GLPK_H
   139 
   259