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