lemon/glpk.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 31 Aug 2009 07:22:26 +0200
changeset 692 33f417de9e70
parent 576 745e182d0139
child 746 e4554cd6b2bf
child 832 5100072d83ca
permissions -rw-r--r--
Merge
     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 #if !defined _GLP_PROB && !defined GLP_PROB
    30 #define _GLP_PROB
    31 #define GLP_PROB
    32 typedef struct { double _opaque_prob; } glp_prob;
    33 /* LP/MIP problem object */
    34 #endif
    35 
    36 namespace lemon {
    37 
    38 
    39   /// \brief Base interface for the GLPK LP and MIP solver
    40   ///
    41   /// This class implements the common interface of the GLPK LP and MIP solver.
    42   /// \ingroup lp_group
    43   class GlpkBase : virtual public LpBase {
    44   protected:
    45 
    46     typedef glp_prob LPX;
    47     glp_prob* lp;
    48 
    49     GlpkBase();
    50     GlpkBase(const GlpkBase&);
    51     virtual ~GlpkBase();
    52 
    53   protected:
    54 
    55     virtual int _addCol();
    56     virtual int _addRow();
    57 
    58     virtual void _eraseCol(int i);
    59     virtual void _eraseRow(int i);
    60 
    61     virtual void _eraseColId(int i);
    62     virtual void _eraseRowId(int i);
    63 
    64     virtual void _getColName(int col, std::string& name) const;
    65     virtual void _setColName(int col, const std::string& name);
    66     virtual int _colByName(const std::string& name) const;
    67 
    68     virtual void _getRowName(int row, std::string& name) const;
    69     virtual void _setRowName(int row, const std::string& name);
    70     virtual int _rowByName(const std::string& name) const;
    71 
    72     virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
    73     virtual void _getRowCoeffs(int i, InsertIterator b) const;
    74 
    75     virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
    76     virtual void _getColCoeffs(int i, InsertIterator b) const;
    77 
    78     virtual void _setCoeff(int row, int col, Value value);
    79     virtual Value _getCoeff(int row, int col) const;
    80 
    81     virtual void _setColLowerBound(int i, Value value);
    82     virtual Value _getColLowerBound(int i) const;
    83 
    84     virtual void _setColUpperBound(int i, Value value);
    85     virtual Value _getColUpperBound(int i) const;
    86 
    87     virtual void _setRowLowerBound(int i, Value value);
    88     virtual Value _getRowLowerBound(int i) const;
    89 
    90     virtual void _setRowUpperBound(int i, Value value);
    91     virtual Value _getRowUpperBound(int i) const;
    92 
    93     virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
    94     virtual void _getObjCoeffs(InsertIterator b) const;
    95 
    96     virtual void _setObjCoeff(int i, Value obj_coef);
    97     virtual Value _getObjCoeff(int i) const;
    98 
    99     virtual void _setSense(Sense);
   100     virtual Sense _getSense() const;
   101 
   102     virtual void _clear();
   103 
   104     virtual void _messageLevel(MessageLevel level);
   105 
   106   private:
   107 
   108     static void freeEnv();
   109 
   110     struct FreeEnvHelper {
   111       ~FreeEnvHelper() {
   112         freeEnv();
   113       }
   114     };
   115     
   116     static FreeEnvHelper freeEnvHelper;
   117 
   118   protected:
   119     
   120     int _message_level;
   121     
   122   public:
   123 
   124     ///Pointer to the underlying GLPK data structure.
   125     LPX *lpx() {return lp;}
   126     ///Const pointer to the underlying GLPK data structure.
   127     const LPX *lpx() const {return lp;}
   128 
   129     ///Returns the constraint identifier understood by GLPK.
   130     int lpxRow(Row r) const { return rows(id(r)); }
   131 
   132     ///Returns the variable identifier understood by GLPK.
   133     int lpxCol(Col c) const { return cols(id(c)); }
   134 
   135   };
   136 
   137   /// \brief Interface for the GLPK LP solver
   138   ///
   139   /// This class implements an interface for the GLPK LP solver.
   140   ///\ingroup lp_group
   141   class GlpkLp : public LpSolver, public GlpkBase {
   142   public:
   143 
   144     ///\e
   145     GlpkLp();
   146     ///\e
   147     GlpkLp(const GlpkLp&);
   148 
   149     ///\e
   150     virtual GlpkLp* cloneSolver() const;
   151     ///\e
   152     virtual GlpkLp* newSolver() const;
   153 
   154   private:
   155 
   156     mutable std::vector<double> _primal_ray;
   157     mutable std::vector<double> _dual_ray;
   158 
   159     void _clear_temporals();
   160 
   161   protected:
   162 
   163     virtual const char* _solverName() const;
   164 
   165     virtual SolveExitStatus _solve();
   166     virtual Value _getPrimal(int i) const;
   167     virtual Value _getDual(int i) const;
   168 
   169     virtual Value _getPrimalValue() const;
   170 
   171     virtual VarStatus _getColStatus(int i) const;
   172     virtual VarStatus _getRowStatus(int i) const;
   173 
   174     virtual Value _getPrimalRay(int i) const;
   175     virtual Value _getDualRay(int i) const;
   176 
   177     virtual ProblemType _getPrimalType() const;
   178     virtual ProblemType _getDualType() const;
   179 
   180   public:
   181 
   182     ///Solve with primal simplex
   183     SolveExitStatus solvePrimal();
   184 
   185     ///Solve with dual simplex
   186     SolveExitStatus solveDual();
   187 
   188   private:
   189 
   190     bool _presolve;
   191 
   192   public:
   193 
   194     ///Turns on or off the presolver
   195 
   196     ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   197     ///
   198     ///The presolver is off by default.
   199     void presolver(bool presolve);
   200 
   201   };
   202 
   203   /// \brief Interface for the GLPK MIP solver
   204   ///
   205   /// This class implements an interface for the GLPK MIP solver.
   206   ///\ingroup lp_group
   207   class GlpkMip : public MipSolver, public GlpkBase {
   208   public:
   209 
   210     ///\e
   211     GlpkMip();
   212     ///\e
   213     GlpkMip(const GlpkMip&);
   214 
   215     virtual GlpkMip* cloneSolver() const;
   216     virtual GlpkMip* newSolver() const;
   217 
   218   protected:
   219 
   220     virtual const char* _solverName() const;
   221 
   222     virtual ColTypes _getColType(int col) const;
   223     virtual void _setColType(int col, ColTypes col_type);
   224 
   225     virtual SolveExitStatus _solve();
   226     virtual ProblemType _getType() const;
   227     virtual Value _getSol(int i) const;
   228     virtual Value _getSolValue() const;
   229 
   230   };
   231 
   232 
   233 } //END OF NAMESPACE LEMON
   234 
   235 #endif //LEMON_GLPK_H
   236