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