lemon/glpk.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 29 Mar 2009 23:08:20 +0200
changeset 559 c5fd2d996909
parent 541 89e29e22d479
parent 538 ba124394367a
child 565 7ab97e2a0c33
permissions -rw-r--r--
Various doc improvements (#248)
- Rename all the ugly template parameters (too long and/or starting
with an underscore).
- Rename function parameters starting with an underscore.
- Extend the doc for many classes.
- Use LaTeX-style O(...) expressions only for the complicated ones.
- A lot of small unification changes.
- Small fixes.
- Some other improvements.
     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-2008
     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_GLPK_H
    20 #define LEMON_GLPK_H
    21 
    22 ///\file
    23 ///\brief Header of the LEMON-GLPK lp solver interface.
    24 ///\ingroup lp_group
    25 
    26 #include <lemon/lp_base.h>
    27 
    28 // forward declaration
    29 #ifndef _GLP_PROB
    30 #define _GLP_PROB
    31 typedef struct { double _prob; } glp_prob;
    32 /* LP/MIP problem object */
    33 #endif
    34 
    35 namespace lemon {
    36 
    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   private:
   104 
   105     static void freeEnv();
   106 
   107     struct FreeEnvHelper {
   108       ~FreeEnvHelper() {
   109         freeEnv();
   110       }
   111     };
   112     
   113     static FreeEnvHelper freeEnvHelper;
   114     
   115   public:
   116 
   117     ///Pointer to the underlying GLPK data structure.
   118     LPX *lpx() {return lp;}
   119     ///Const pointer to the underlying GLPK data structure.
   120     const LPX *lpx() const {return lp;}
   121 
   122     ///Returns the constraint identifier understood by GLPK.
   123     int lpxRow(Row r) const { return rows(id(r)); }
   124 
   125     ///Returns the variable identifier understood by GLPK.
   126     int lpxCol(Col c) const { return cols(id(c)); }
   127 
   128   };
   129 
   130   /// \brief Interface for the GLPK LP solver
   131   ///
   132   /// This class implements an interface for the GLPK LP solver.
   133   ///\ingroup lp_group
   134   class GlpkLp : public LpSolver, public GlpkBase {
   135   public:
   136 
   137     ///\e
   138     GlpkLp();
   139     ///\e
   140     GlpkLp(const GlpkLp&);
   141 
   142     ///\e
   143     virtual GlpkLp* cloneSolver() const;
   144     ///\e
   145     virtual GlpkLp* newSolver() const;
   146 
   147   private:
   148 
   149     mutable std::vector<double> _primal_ray;
   150     mutable std::vector<double> _dual_ray;
   151 
   152     void _clear_temporals();
   153 
   154   protected:
   155 
   156     virtual const char* _solverName() const;
   157 
   158     virtual SolveExitStatus _solve();
   159     virtual Value _getPrimal(int i) const;
   160     virtual Value _getDual(int i) const;
   161 
   162     virtual Value _getPrimalValue() const;
   163 
   164     virtual VarStatus _getColStatus(int i) const;
   165     virtual VarStatus _getRowStatus(int i) const;
   166 
   167     virtual Value _getPrimalRay(int i) const;
   168     virtual Value _getDualRay(int i) const;
   169 
   170     virtual ProblemType _getPrimalType() const;
   171     virtual ProblemType _getDualType() const;
   172 
   173   public:
   174 
   175     ///Solve with primal simplex
   176     SolveExitStatus solvePrimal();
   177 
   178     ///Solve with dual simplex
   179     SolveExitStatus solveDual();
   180 
   181     ///Turns on or off the presolver
   182 
   183     ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   184     ///
   185     ///The presolver is off by default.
   186     void presolver(bool b);
   187 
   188     ///Enum for \c messageLevel() parameter
   189     enum MessageLevel {
   190       /// no output (default value)
   191       MESSAGE_NO_OUTPUT = 0,
   192       /// error messages only
   193       MESSAGE_ERROR_MESSAGE = 1,
   194       /// normal output
   195       MESSAGE_NORMAL_OUTPUT = 2,
   196       /// full output (includes informational messages)
   197       MESSAGE_FULL_OUTPUT = 3
   198     };
   199 
   200   private:
   201 
   202     MessageLevel _message_level;
   203 
   204   public:
   205 
   206     ///Set the verbosity of the messages
   207 
   208     ///Set the verbosity of the messages
   209     ///
   210     ///\param m is the level of the messages output by the solver routines.
   211     void messageLevel(MessageLevel m);
   212   };
   213 
   214   /// \brief Interface for the GLPK MIP solver
   215   ///
   216   /// This class implements an interface for the GLPK MIP solver.
   217   ///\ingroup lp_group
   218   class GlpkMip : public MipSolver, public GlpkBase {
   219   public:
   220 
   221     ///\e
   222     GlpkMip();
   223     ///\e
   224     GlpkMip(const GlpkMip&);
   225 
   226     virtual GlpkMip* cloneSolver() const;
   227     virtual GlpkMip* newSolver() const;
   228 
   229   protected:
   230 
   231     virtual const char* _solverName() const;
   232 
   233     virtual ColTypes _getColType(int col) const;
   234     virtual void _setColType(int col, ColTypes col_type);
   235 
   236     virtual SolveExitStatus _solve();
   237     virtual ProblemType _getType() const;
   238     virtual Value _getSol(int i) const;
   239     virtual Value _getSolValue() const;
   240 
   241     ///Enum for \c messageLevel() parameter
   242     enum MessageLevel {
   243       /// no output (default value)
   244       MESSAGE_NO_OUTPUT = 0,
   245       /// error messages only
   246       MESSAGE_ERROR_MESSAGE = 1,
   247       /// normal output
   248       MESSAGE_NORMAL_OUTPUT = 2,
   249       /// full output (includes informational messages)
   250       MESSAGE_FULL_OUTPUT = 3
   251     };
   252 
   253   private:
   254 
   255     MessageLevel _message_level;
   256 
   257   public:
   258 
   259     ///Set the verbosity of the messages
   260 
   261     ///Set the verbosity of the messages
   262     ///
   263     ///\param m is the level of the messages output by the solver routines.
   264     void messageLevel(MessageLevel m);
   265   };
   266 
   267 
   268 } //END OF NAMESPACE LEMON
   269 
   270 #endif //LEMON_GLPK_H
   271