lemon/glpk.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 15 Mar 2011 19:32:21 +0100
changeset 1047 ddd3c0d3d9bf
parent 902 d2bc45e8f6f2
child 1231 1782aa72495a
permissions -rw-r--r--
Implement the scaling Price Refinement heuristic in CostScaling (#417)
instead of Early Termination.

These two heuristics are similar, but the newer one is faster
and not only makes it possible to skip some epsilon phases, but
it can improve the performance of the other phases, as well.
alpar@484
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@484
     2
 *
alpar@484
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@484
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
alpar@484
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@484
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@484
     8
 *
alpar@484
     9
 * Permission to use, modify and distribute this software is granted
alpar@484
    10
 * provided that this copyright notice appears in all copies. For
alpar@484
    11
 * precise terms see the accompanying LICENSE file.
alpar@484
    12
 *
alpar@484
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@484
    14
 * express or implied, and with no claim as to its suitability for any
alpar@484
    15
 * purpose.
alpar@484
    16
 *
alpar@484
    17
 */
alpar@484
    18
alpar@484
    19
#ifndef LEMON_GLPK_H
alpar@484
    20
#define LEMON_GLPK_H
alpar@484
    21
alpar@484
    22
///\file
alpar@484
    23
///\brief Header of the LEMON-GLPK lp solver interface.
alpar@484
    24
///\ingroup lp_group
alpar@484
    25
alpar@484
    26
#include <lemon/lp_base.h>
alpar@484
    27
alpar@484
    28
namespace lemon {
alpar@484
    29
deba@900
    30
  namespace _solver_bits {
deba@900
    31
    class VoidPtr {
deba@900
    32
    private:
alpar@956
    33
      void *_ptr;
deba@900
    34
    public:
deba@900
    35
      VoidPtr() : _ptr(0) {}
deba@900
    36
deba@900
    37
      template <typename T>
deba@900
    38
      VoidPtr(T* ptr) : _ptr(reinterpret_cast<void*>(ptr)) {}
deba@900
    39
deba@900
    40
      template <typename T>
alpar@956
    41
      VoidPtr& operator=(T* ptr) {
alpar@956
    42
        _ptr = reinterpret_cast<void*>(ptr);
deba@900
    43
        return *this;
deba@900
    44
      }
deba@900
    45
deba@900
    46
      template <typename T>
deba@900
    47
      operator T*() const { return reinterpret_cast<T*>(_ptr); }
deba@900
    48
    };
deba@900
    49
  }
alpar@484
    50
alpar@484
    51
  /// \brief Base interface for the GLPK LP and MIP solver
alpar@484
    52
  ///
alpar@484
    53
  /// This class implements the common interface of the GLPK LP and MIP solver.
alpar@484
    54
  /// \ingroup lp_group
alpar@484
    55
  class GlpkBase : virtual public LpBase {
alpar@484
    56
  protected:
alpar@484
    57
deba@900
    58
    _solver_bits::VoidPtr lp;
alpar@484
    59
alpar@484
    60
    GlpkBase();
alpar@484
    61
    GlpkBase(const GlpkBase&);
alpar@484
    62
    virtual ~GlpkBase();
alpar@484
    63
alpar@484
    64
  protected:
alpar@484
    65
alpar@484
    66
    virtual int _addCol();
alpar@484
    67
    virtual int _addRow();
deba@793
    68
    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
alpar@484
    69
alpar@484
    70
    virtual void _eraseCol(int i);
alpar@484
    71
    virtual void _eraseRow(int i);
alpar@484
    72
alpar@484
    73
    virtual void _eraseColId(int i);
alpar@484
    74
    virtual void _eraseRowId(int i);
alpar@484
    75
alpar@484
    76
    virtual void _getColName(int col, std::string& name) const;
alpar@484
    77
    virtual void _setColName(int col, const std::string& name);
alpar@484
    78
    virtual int _colByName(const std::string& name) const;
alpar@484
    79
alpar@484
    80
    virtual void _getRowName(int row, std::string& name) const;
alpar@484
    81
    virtual void _setRowName(int row, const std::string& name);
alpar@484
    82
    virtual int _rowByName(const std::string& name) const;
alpar@484
    83
alpar@484
    84
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@484
    85
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
alpar@484
    86
alpar@484
    87
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@484
    88
    virtual void _getColCoeffs(int i, InsertIterator b) const;
alpar@484
    89
alpar@484
    90
    virtual void _setCoeff(int row, int col, Value value);
alpar@484
    91
    virtual Value _getCoeff(int row, int col) const;
alpar@484
    92
alpar@484
    93
    virtual void _setColLowerBound(int i, Value value);
alpar@484
    94
    virtual Value _getColLowerBound(int i) const;
alpar@484
    95
alpar@484
    96
    virtual void _setColUpperBound(int i, Value value);
alpar@484
    97
    virtual Value _getColUpperBound(int i) const;
alpar@484
    98
alpar@484
    99
    virtual void _setRowLowerBound(int i, Value value);
alpar@484
   100
    virtual Value _getRowLowerBound(int i) const;
alpar@484
   101
alpar@484
   102
    virtual void _setRowUpperBound(int i, Value value);
alpar@484
   103
    virtual Value _getRowUpperBound(int i) const;
alpar@484
   104
alpar@484
   105
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
alpar@484
   106
    virtual void _getObjCoeffs(InsertIterator b) const;
alpar@484
   107
alpar@484
   108
    virtual void _setObjCoeff(int i, Value obj_coef);
alpar@484
   109
    virtual Value _getObjCoeff(int i) const;
alpar@484
   110
alpar@484
   111
    virtual void _setSense(Sense);
alpar@484
   112
    virtual Sense _getSense() const;
alpar@484
   113
alpar@484
   114
    virtual void _clear();
alpar@484
   115
deba@623
   116
    virtual void _messageLevel(MessageLevel level);
deba@623
   117
deba@585
   118
  private:
deba@585
   119
deba@585
   120
    static void freeEnv();
deba@585
   121
deba@585
   122
    struct FreeEnvHelper {
deba@585
   123
      ~FreeEnvHelper() {
deba@585
   124
        freeEnv();
deba@585
   125
      }
deba@585
   126
    };
alpar@956
   127
deba@585
   128
    static FreeEnvHelper freeEnvHelper;
deba@623
   129
deba@623
   130
  protected:
alpar@956
   131
deba@623
   132
    int _message_level;
alpar@956
   133
alpar@484
   134
  public:
alpar@484
   135
alpar@484
   136
    ///Pointer to the underlying GLPK data structure.
deba@900
   137
    _solver_bits::VoidPtr lpx() {return lp;}
alpar@484
   138
    ///Const pointer to the underlying GLPK data structure.
deba@900
   139
    _solver_bits::VoidPtr lpx() const {return lp;}
alpar@484
   140
alpar@484
   141
    ///Returns the constraint identifier understood by GLPK.
alpar@484
   142
    int lpxRow(Row r) const { return rows(id(r)); }
alpar@484
   143
alpar@484
   144
    ///Returns the variable identifier understood by GLPK.
alpar@484
   145
    int lpxCol(Col c) const { return cols(id(c)); }
alpar@484
   146
alpar@484
   147
  };
alpar@484
   148
alpar@484
   149
  /// \brief Interface for the GLPK LP solver
alpar@484
   150
  ///
alpar@484
   151
  /// This class implements an interface for the GLPK LP solver.
alpar@484
   152
  ///\ingroup lp_group
alpar@587
   153
  class GlpkLp : public LpSolver, public GlpkBase {
alpar@484
   154
  public:
alpar@484
   155
alpar@484
   156
    ///\e
alpar@485
   157
    GlpkLp();
alpar@484
   158
    ///\e
alpar@485
   159
    GlpkLp(const GlpkLp&);
alpar@484
   160
alpar@587
   161
    ///\e
alpar@587
   162
    virtual GlpkLp* cloneSolver() const;
alpar@587
   163
    ///\e
alpar@587
   164
    virtual GlpkLp* newSolver() const;
alpar@587
   165
alpar@484
   166
  private:
alpar@484
   167
alpar@484
   168
    mutable std::vector<double> _primal_ray;
alpar@484
   169
    mutable std::vector<double> _dual_ray;
alpar@484
   170
alpar@484
   171
    void _clear_temporals();
alpar@484
   172
alpar@484
   173
  protected:
alpar@484
   174
alpar@484
   175
    virtual const char* _solverName() const;
alpar@484
   176
alpar@484
   177
    virtual SolveExitStatus _solve();
alpar@484
   178
    virtual Value _getPrimal(int i) const;
alpar@484
   179
    virtual Value _getDual(int i) const;
alpar@484
   180
alpar@484
   181
    virtual Value _getPrimalValue() const;
alpar@484
   182
alpar@484
   183
    virtual VarStatus _getColStatus(int i) const;
alpar@484
   184
    virtual VarStatus _getRowStatus(int i) const;
alpar@484
   185
alpar@484
   186
    virtual Value _getPrimalRay(int i) const;
alpar@484
   187
    virtual Value _getDualRay(int i) const;
alpar@484
   188
alpar@484
   189
    virtual ProblemType _getPrimalType() const;
alpar@484
   190
    virtual ProblemType _getDualType() const;
alpar@484
   191
alpar@484
   192
  public:
alpar@484
   193
alpar@484
   194
    ///Solve with primal simplex
alpar@484
   195
    SolveExitStatus solvePrimal();
alpar@484
   196
alpar@484
   197
    ///Solve with dual simplex
alpar@484
   198
    SolveExitStatus solveDual();
alpar@484
   199
deba@612
   200
  private:
deba@612
   201
deba@612
   202
    bool _presolve;
deba@612
   203
deba@612
   204
  public:
deba@612
   205
alpar@484
   206
    ///Turns on or off the presolver
alpar@484
   207
alpar@484
   208
    ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
alpar@484
   209
    ///
alpar@484
   210
    ///The presolver is off by default.
deba@612
   211
    void presolver(bool presolve);
alpar@484
   212
alpar@484
   213
  };
alpar@484
   214
alpar@484
   215
  /// \brief Interface for the GLPK MIP solver
alpar@484
   216
  ///
alpar@484
   217
  /// This class implements an interface for the GLPK MIP solver.
alpar@484
   218
  ///\ingroup lp_group
alpar@587
   219
  class GlpkMip : public MipSolver, public GlpkBase {
alpar@484
   220
  public:
alpar@484
   221
alpar@484
   222
    ///\e
alpar@485
   223
    GlpkMip();
alpar@484
   224
    ///\e
alpar@485
   225
    GlpkMip(const GlpkMip&);
alpar@484
   226
alpar@587
   227
    virtual GlpkMip* cloneSolver() const;
alpar@587
   228
    virtual GlpkMip* newSolver() const;
alpar@587
   229
alpar@484
   230
  protected:
alpar@484
   231
alpar@484
   232
    virtual const char* _solverName() const;
alpar@484
   233
alpar@484
   234
    virtual ColTypes _getColType(int col) const;
alpar@484
   235
    virtual void _setColType(int col, ColTypes col_type);
alpar@484
   236
alpar@484
   237
    virtual SolveExitStatus _solve();
alpar@484
   238
    virtual ProblemType _getType() const;
alpar@484
   239
    virtual Value _getSol(int i) const;
alpar@484
   240
    virtual Value _getSolValue() const;
alpar@484
   241
alpar@484
   242
  };
alpar@484
   243
alpar@484
   244
alpar@484
   245
} //END OF NAMESPACE LEMON
alpar@484
   246
alpar@484
   247
#endif //LEMON_GLPK_H
alpar@484
   248