lemon/glpk.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 29 Jul 2020 14:56:10 +0200
changeset 1433 a278d16bd2d0
parent 1270 dceba191c00d
permissions -rw-r--r--
Fix clang compilation issue (#634)
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@1270
     5
 * Copyright (C) 2003-2013
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
alpar@1231
   118
    virtual void _write(std::string file, std::string format) const;
alpar@1231
   119
deba@585
   120
  private:
deba@585
   121
deba@585
   122
    static void freeEnv();
deba@585
   123
deba@585
   124
    struct FreeEnvHelper {
deba@585
   125
      ~FreeEnvHelper() {
deba@585
   126
        freeEnv();
deba@585
   127
      }
deba@585
   128
    };
alpar@956
   129
deba@585
   130
    static FreeEnvHelper freeEnvHelper;
deba@623
   131
deba@623
   132
  protected:
alpar@956
   133
deba@623
   134
    int _message_level;
alpar@956
   135
alpar@484
   136
  public:
alpar@484
   137
alpar@484
   138
    ///Pointer to the underlying GLPK data structure.
deba@900
   139
    _solver_bits::VoidPtr lpx() {return lp;}
alpar@484
   140
    ///Const pointer to the underlying GLPK data structure.
deba@900
   141
    _solver_bits::VoidPtr lpx() const {return lp;}
alpar@484
   142
alpar@484
   143
    ///Returns the constraint identifier understood by GLPK.
ggab90@1336
   144
    int lpxRow(Row r) const { return _rows(id(r)); }
alpar@484
   145
alpar@484
   146
    ///Returns the variable identifier understood by GLPK.
ggab90@1336
   147
    int lpxCol(Col c) const { return _cols(id(c)); }
alpar@484
   148
alpar@1231
   149
#ifdef DOXYGEN
alpar@1231
   150
    /// Write the problem or the solution to a file in the given format
alpar@1270
   151
alpar@1231
   152
    /// This function writes the problem or the solution
alpar@1231
   153
    /// to a file in the given format.
alpar@1231
   154
    /// Trying to write in an unsupported format will trigger
alpar@1250
   155
    /// \ref LpBase::UnsupportedFormatError.
alpar@1231
   156
    /// \param file The file path
alpar@1231
   157
    /// \param format The output file format.
alpar@1231
   158
    /// Supportted formats are "MPS" and "LP".
alpar@1231
   159
    void write(std::string file, std::string format = "MPS") const {}
alpar@1231
   160
#endif
alpar@1231
   161
alpar@484
   162
  };
alpar@484
   163
alpar@484
   164
  /// \brief Interface for the GLPK LP solver
alpar@484
   165
  ///
alpar@484
   166
  /// This class implements an interface for the GLPK LP solver.
alpar@484
   167
  ///\ingroup lp_group
alpar@587
   168
  class GlpkLp : public LpSolver, public GlpkBase {
alpar@484
   169
  public:
alpar@484
   170
alpar@484
   171
    ///\e
alpar@485
   172
    GlpkLp();
alpar@484
   173
    ///\e
alpar@485
   174
    GlpkLp(const GlpkLp&);
alpar@484
   175
alpar@587
   176
    ///\e
alpar@587
   177
    virtual GlpkLp* cloneSolver() const;
alpar@587
   178
    ///\e
alpar@587
   179
    virtual GlpkLp* newSolver() const;
alpar@587
   180
alpar@484
   181
  private:
alpar@484
   182
alpar@484
   183
    mutable std::vector<double> _primal_ray;
alpar@484
   184
    mutable std::vector<double> _dual_ray;
alpar@484
   185
alpar@484
   186
    void _clear_temporals();
alpar@484
   187
alpar@484
   188
  protected:
alpar@484
   189
alpar@484
   190
    virtual const char* _solverName() const;
alpar@484
   191
alpar@484
   192
    virtual SolveExitStatus _solve();
alpar@484
   193
    virtual Value _getPrimal(int i) const;
alpar@484
   194
    virtual Value _getDual(int i) const;
alpar@484
   195
alpar@484
   196
    virtual Value _getPrimalValue() const;
alpar@484
   197
alpar@484
   198
    virtual VarStatus _getColStatus(int i) const;
alpar@484
   199
    virtual VarStatus _getRowStatus(int i) const;
alpar@484
   200
alpar@484
   201
    virtual Value _getPrimalRay(int i) const;
alpar@484
   202
    virtual Value _getDualRay(int i) const;
alpar@484
   203
alpar@484
   204
    virtual ProblemType _getPrimalType() const;
alpar@484
   205
    virtual ProblemType _getDualType() const;
alpar@484
   206
alpar@484
   207
  public:
alpar@484
   208
alpar@484
   209
    ///Solve with primal simplex
alpar@484
   210
    SolveExitStatus solvePrimal();
alpar@484
   211
alpar@484
   212
    ///Solve with dual simplex
alpar@484
   213
    SolveExitStatus solveDual();
alpar@484
   214
deba@612
   215
  private:
deba@612
   216
deba@612
   217
    bool _presolve;
deba@612
   218
deba@612
   219
  public:
deba@612
   220
alpar@484
   221
    ///Turns on or off the presolver
alpar@484
   222
alpar@484
   223
    ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
alpar@484
   224
    ///
alpar@484
   225
    ///The presolver is off by default.
deba@612
   226
    void presolver(bool presolve);
alpar@484
   227
alpar@484
   228
  };
alpar@484
   229
alpar@484
   230
  /// \brief Interface for the GLPK MIP solver
alpar@484
   231
  ///
alpar@484
   232
  /// This class implements an interface for the GLPK MIP solver.
alpar@484
   233
  ///\ingroup lp_group
alpar@587
   234
  class GlpkMip : public MipSolver, public GlpkBase {
alpar@484
   235
  public:
alpar@484
   236
alpar@484
   237
    ///\e
alpar@485
   238
    GlpkMip();
alpar@484
   239
    ///\e
alpar@485
   240
    GlpkMip(const GlpkMip&);
alpar@484
   241
alpar@587
   242
    virtual GlpkMip* cloneSolver() const;
alpar@587
   243
    virtual GlpkMip* newSolver() const;
alpar@587
   244
alpar@484
   245
  protected:
alpar@484
   246
alpar@484
   247
    virtual const char* _solverName() const;
alpar@484
   248
alpar@484
   249
    virtual ColTypes _getColType(int col) const;
alpar@484
   250
    virtual void _setColType(int col, ColTypes col_type);
alpar@484
   251
alpar@484
   252
    virtual SolveExitStatus _solve();
alpar@484
   253
    virtual ProblemType _getType() const;
alpar@484
   254
    virtual Value _getSol(int i) const;
alpar@484
   255
    virtual Value _getSolValue() const;
alpar@484
   256
alpar@484
   257
  };
alpar@484
   258
alpar@484
   259
alpar@484
   260
} //END OF NAMESPACE LEMON
alpar@484
   261
alpar@484
   262
#endif //LEMON_GLPK_H
alpar@484
   263