lemon/glpk.h
author Balazs Dezso <deba@google.com>
Thu, 08 Aug 2013 22:56:10 +0200
changeset 1089 552e3d1242c6
parent 650 a8dfe89b7719
child 833 d2bc45e8f6f2
permissions -rw-r--r--
Fix biNodeConnected() function (#439)
     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 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 
    69     virtual void _eraseCol(int i);
    70     virtual void _eraseRow(int i);
    71 
    72     virtual void _eraseColId(int i);
    73     virtual void _eraseRowId(int i);
    74 
    75     virtual void _getColName(int col, std::string& name) const;
    76     virtual void _setColName(int col, const std::string& name);
    77     virtual int _colByName(const std::string& name) const;
    78 
    79     virtual void _getRowName(int row, std::string& name) const;
    80     virtual void _setRowName(int row, const std::string& name);
    81     virtual int _rowByName(const std::string& name) const;
    82 
    83     virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
    84     virtual void _getRowCoeffs(int i, InsertIterator b) const;
    85 
    86     virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
    87     virtual void _getColCoeffs(int i, InsertIterator b) const;
    88 
    89     virtual void _setCoeff(int row, int col, Value value);
    90     virtual Value _getCoeff(int row, int col) const;
    91 
    92     virtual void _setColLowerBound(int i, Value value);
    93     virtual Value _getColLowerBound(int i) const;
    94 
    95     virtual void _setColUpperBound(int i, Value value);
    96     virtual Value _getColUpperBound(int i) const;
    97 
    98     virtual void _setRowLowerBound(int i, Value value);
    99     virtual Value _getRowLowerBound(int i) const;
   100 
   101     virtual void _setRowUpperBound(int i, Value value);
   102     virtual Value _getRowUpperBound(int i) const;
   103 
   104     virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
   105     virtual void _getObjCoeffs(InsertIterator b) const;
   106 
   107     virtual void _setObjCoeff(int i, Value obj_coef);
   108     virtual Value _getObjCoeff(int i) const;
   109 
   110     virtual void _setSense(Sense);
   111     virtual Sense _getSense() const;
   112 
   113     virtual void _clear();
   114 
   115     virtual void _messageLevel(MessageLevel level);
   116 
   117   private:
   118 
   119     static void freeEnv();
   120 
   121     struct FreeEnvHelper {
   122       ~FreeEnvHelper() {
   123         freeEnv();
   124       }
   125     };
   126     
   127     static FreeEnvHelper freeEnvHelper;
   128 
   129   protected:
   130     
   131     int _message_level;
   132     
   133   public:
   134 
   135     ///Pointer to the underlying GLPK data structure.
   136     _solver_bits::VoidPtr lpx() {return lp;}
   137     ///Const pointer to the underlying GLPK data structure.
   138     _solver_bits::VoidPtr lpx() const {return lp;}
   139 
   140     ///Returns the constraint identifier understood by GLPK.
   141     int lpxRow(Row r) const { return rows(id(r)); }
   142 
   143     ///Returns the variable identifier understood by GLPK.
   144     int lpxCol(Col c) const { return cols(id(c)); }
   145 
   146   };
   147 
   148   /// \brief Interface for the GLPK LP solver
   149   ///
   150   /// This class implements an interface for the GLPK LP solver.
   151   ///\ingroup lp_group
   152   class GlpkLp : public LpSolver, public GlpkBase {
   153   public:
   154 
   155     ///\e
   156     GlpkLp();
   157     ///\e
   158     GlpkLp(const GlpkLp&);
   159 
   160     ///\e
   161     virtual GlpkLp* cloneSolver() const;
   162     ///\e
   163     virtual GlpkLp* newSolver() const;
   164 
   165   private:
   166 
   167     mutable std::vector<double> _primal_ray;
   168     mutable std::vector<double> _dual_ray;
   169 
   170     void _clear_temporals();
   171 
   172   protected:
   173 
   174     virtual const char* _solverName() const;
   175 
   176     virtual SolveExitStatus _solve();
   177     virtual Value _getPrimal(int i) const;
   178     virtual Value _getDual(int i) const;
   179 
   180     virtual Value _getPrimalValue() const;
   181 
   182     virtual VarStatus _getColStatus(int i) const;
   183     virtual VarStatus _getRowStatus(int i) const;
   184 
   185     virtual Value _getPrimalRay(int i) const;
   186     virtual Value _getDualRay(int i) const;
   187 
   188     virtual ProblemType _getPrimalType() const;
   189     virtual ProblemType _getDualType() const;
   190 
   191   public:
   192 
   193     ///Solve with primal simplex
   194     SolveExitStatus solvePrimal();
   195 
   196     ///Solve with dual simplex
   197     SolveExitStatus solveDual();
   198 
   199   private:
   200 
   201     bool _presolve;
   202 
   203   public:
   204 
   205     ///Turns on or off the presolver
   206 
   207     ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   208     ///
   209     ///The presolver is off by default.
   210     void presolver(bool presolve);
   211 
   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   };
   242 
   243 
   244 } //END OF NAMESPACE LEMON
   245 
   246 #endif //LEMON_GLPK_H
   247