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