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