lemon/glpk.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 23 Feb 2009 11:49:57 +0000
changeset 510 28b154307c0d
parent 461 08d495d48089
child 525 0fec6a017ead
child 528 9db62975c32b
permissions -rw-r--r--
DIMACS solver utility (#226)
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
alpar@461
    29
#ifndef _GLP_PROB
alpar@461
    30
#define _GLP_PROB
alpar@461
    31
typedef struct { double _prob; } glp_prob;
alpar@461
    32
/* LP/MIP problem object */
alpar@461
    33
#endif
alpar@461
    34
alpar@461
    35
namespace lemon {
alpar@461
    36
alpar@461
    37
alpar@461
    38
  /// \brief Base interface for the GLPK LP and MIP solver
alpar@461
    39
  ///
alpar@461
    40
  /// This class implements the common interface of the GLPK LP and MIP solver.
alpar@461
    41
  /// \ingroup lp_group
alpar@461
    42
  class GlpkBase : virtual public LpBase {
alpar@461
    43
  protected:
alpar@461
    44
alpar@461
    45
    typedef glp_prob LPX;
alpar@461
    46
    glp_prob* lp;
alpar@461
    47
alpar@461
    48
    GlpkBase();
alpar@461
    49
    GlpkBase(const GlpkBase&);
alpar@461
    50
    virtual ~GlpkBase();
alpar@461
    51
alpar@461
    52
  protected:
alpar@461
    53
alpar@461
    54
    virtual int _addCol();
alpar@461
    55
    virtual int _addRow();
alpar@461
    56
alpar@461
    57
    virtual void _eraseCol(int i);
alpar@461
    58
    virtual void _eraseRow(int i);
alpar@461
    59
alpar@461
    60
    virtual void _eraseColId(int i);
alpar@461
    61
    virtual void _eraseRowId(int i);
alpar@461
    62
alpar@461
    63
    virtual void _getColName(int col, std::string& name) const;
alpar@461
    64
    virtual void _setColName(int col, const std::string& name);
alpar@461
    65
    virtual int _colByName(const std::string& name) const;
alpar@461
    66
alpar@461
    67
    virtual void _getRowName(int row, std::string& name) const;
alpar@461
    68
    virtual void _setRowName(int row, const std::string& name);
alpar@461
    69
    virtual int _rowByName(const std::string& name) const;
alpar@461
    70
alpar@461
    71
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@461
    72
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
alpar@461
    73
alpar@461
    74
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@461
    75
    virtual void _getColCoeffs(int i, InsertIterator b) const;
alpar@461
    76
alpar@461
    77
    virtual void _setCoeff(int row, int col, Value value);
alpar@461
    78
    virtual Value _getCoeff(int row, int col) const;
alpar@461
    79
alpar@461
    80
    virtual void _setColLowerBound(int i, Value value);
alpar@461
    81
    virtual Value _getColLowerBound(int i) const;
alpar@461
    82
alpar@461
    83
    virtual void _setColUpperBound(int i, Value value);
alpar@461
    84
    virtual Value _getColUpperBound(int i) const;
alpar@461
    85
alpar@461
    86
    virtual void _setRowLowerBound(int i, Value value);
alpar@461
    87
    virtual Value _getRowLowerBound(int i) const;
alpar@461
    88
alpar@461
    89
    virtual void _setRowUpperBound(int i, Value value);
alpar@461
    90
    virtual Value _getRowUpperBound(int i) const;
alpar@461
    91
alpar@461
    92
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
alpar@461
    93
    virtual void _getObjCoeffs(InsertIterator b) const;
alpar@461
    94
alpar@461
    95
    virtual void _setObjCoeff(int i, Value obj_coef);
alpar@461
    96
    virtual Value _getObjCoeff(int i) const;
alpar@461
    97
alpar@461
    98
    virtual void _setSense(Sense);
alpar@461
    99
    virtual Sense _getSense() const;
alpar@461
   100
alpar@461
   101
    virtual void _clear();
alpar@461
   102
alpar@461
   103
  public:
alpar@461
   104
alpar@461
   105
    ///Pointer to the underlying GLPK data structure.
alpar@461
   106
    LPX *lpx() {return lp;}
alpar@461
   107
    ///Const pointer to the underlying GLPK data structure.
alpar@461
   108
    const LPX *lpx() const {return lp;}
alpar@461
   109
alpar@461
   110
    ///Returns the constraint identifier understood by GLPK.
alpar@461
   111
    int lpxRow(Row r) const { return rows(id(r)); }
alpar@461
   112
alpar@461
   113
    ///Returns the variable identifier understood by GLPK.
alpar@461
   114
    int lpxCol(Col c) const { return cols(id(c)); }
alpar@461
   115
alpar@461
   116
  };
alpar@461
   117
alpar@461
   118
  /// \brief Interface for the GLPK LP solver
alpar@461
   119
  ///
alpar@461
   120
  /// This class implements an interface for the GLPK LP solver.
alpar@461
   121
  ///\ingroup lp_group
alpar@462
   122
  class GlpkLp : public GlpkBase, public LpSolver {
alpar@461
   123
  public:
alpar@461
   124
alpar@461
   125
    ///\e
alpar@462
   126
    GlpkLp();
alpar@461
   127
    ///\e
alpar@462
   128
    GlpkLp(const GlpkLp&);
alpar@461
   129
alpar@461
   130
  private:
alpar@461
   131
alpar@461
   132
    mutable std::vector<double> _primal_ray;
alpar@461
   133
    mutable std::vector<double> _dual_ray;
alpar@461
   134
alpar@461
   135
    void _clear_temporals();
alpar@461
   136
alpar@461
   137
  protected:
alpar@461
   138
alpar@462
   139
    virtual GlpkLp* _cloneSolver() const;
alpar@462
   140
    virtual GlpkLp* _newSolver() const;
alpar@461
   141
alpar@461
   142
    virtual const char* _solverName() const;
alpar@461
   143
alpar@461
   144
    virtual SolveExitStatus _solve();
alpar@461
   145
    virtual Value _getPrimal(int i) const;
alpar@461
   146
    virtual Value _getDual(int i) const;
alpar@461
   147
alpar@461
   148
    virtual Value _getPrimalValue() const;
alpar@461
   149
alpar@461
   150
    virtual VarStatus _getColStatus(int i) const;
alpar@461
   151
    virtual VarStatus _getRowStatus(int i) const;
alpar@461
   152
alpar@461
   153
    virtual Value _getPrimalRay(int i) const;
alpar@461
   154
    virtual Value _getDualRay(int i) const;
alpar@461
   155
alpar@461
   156
    ///\todo It should be clarified
alpar@461
   157
    ///
alpar@461
   158
    virtual ProblemType _getPrimalType() const;
alpar@461
   159
    virtual ProblemType _getDualType() const;
alpar@461
   160
alpar@461
   161
  public:
alpar@461
   162
alpar@461
   163
    ///Solve with primal simplex
alpar@461
   164
    SolveExitStatus solvePrimal();
alpar@461
   165
alpar@461
   166
    ///Solve with dual simplex
alpar@461
   167
    SolveExitStatus solveDual();
alpar@461
   168
alpar@461
   169
    ///Turns on or off the presolver
alpar@461
   170
alpar@461
   171
    ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
alpar@461
   172
    ///
alpar@461
   173
    ///The presolver is off by default.
alpar@461
   174
    void presolver(bool b);
alpar@461
   175
alpar@461
   176
    ///Enum for \c messageLevel() parameter
alpar@461
   177
    enum MessageLevel {
alpar@461
   178
      /// no output (default value)
alpar@461
   179
      MESSAGE_NO_OUTPUT = 0,
alpar@461
   180
      /// error messages only
alpar@461
   181
      MESSAGE_ERROR_MESSAGE = 1,
alpar@461
   182
      /// normal output
alpar@461
   183
      MESSAGE_NORMAL_OUTPUT = 2,
alpar@461
   184
      /// full output (includes informational messages)
alpar@461
   185
      MESSAGE_FULL_OUTPUT = 3
alpar@461
   186
    };
alpar@461
   187
alpar@461
   188
  private:
alpar@461
   189
alpar@461
   190
    MessageLevel _message_level;
alpar@461
   191
alpar@461
   192
  public:
alpar@461
   193
alpar@461
   194
    ///Set the verbosity of the messages
alpar@461
   195
alpar@461
   196
    ///Set the verbosity of the messages
alpar@461
   197
    ///
alpar@461
   198
    ///\param m is the level of the messages output by the solver routines.
alpar@461
   199
    void messageLevel(MessageLevel m);
alpar@461
   200
  };
alpar@461
   201
alpar@461
   202
  /// \brief Interface for the GLPK MIP solver
alpar@461
   203
  ///
alpar@461
   204
  /// This class implements an interface for the GLPK MIP solver.
alpar@461
   205
  ///\ingroup lp_group
alpar@462
   206
  class GlpkMip : public GlpkBase, public MipSolver {
alpar@461
   207
  public:
alpar@461
   208
alpar@461
   209
    ///\e
alpar@462
   210
    GlpkMip();
alpar@461
   211
    ///\e
alpar@462
   212
    GlpkMip(const GlpkMip&);
alpar@461
   213
alpar@461
   214
  protected:
alpar@461
   215
alpar@462
   216
    virtual GlpkMip* _cloneSolver() const;
alpar@462
   217
    virtual GlpkMip* _newSolver() const;
alpar@461
   218
alpar@461
   219
    virtual const char* _solverName() const;
alpar@461
   220
alpar@461
   221
    virtual ColTypes _getColType(int col) const;
alpar@461
   222
    virtual void _setColType(int col, ColTypes col_type);
alpar@461
   223
alpar@461
   224
    virtual SolveExitStatus _solve();
alpar@461
   225
    virtual ProblemType _getType() const;
alpar@461
   226
    virtual Value _getSol(int i) const;
alpar@461
   227
    virtual Value _getSolValue() const;
alpar@461
   228
alpar@461
   229
    ///Enum for \c messageLevel() parameter
alpar@461
   230
    enum MessageLevel {
alpar@461
   231
      /// no output (default value)
alpar@461
   232
      MESSAGE_NO_OUTPUT = 0,
alpar@461
   233
      /// error messages only
alpar@461
   234
      MESSAGE_ERROR_MESSAGE = 1,
alpar@461
   235
      /// normal output
alpar@461
   236
      MESSAGE_NORMAL_OUTPUT = 2,
alpar@461
   237
      /// full output (includes informational messages)
alpar@461
   238
      MESSAGE_FULL_OUTPUT = 3
alpar@461
   239
    };
alpar@461
   240
alpar@461
   241
  private:
alpar@461
   242
alpar@461
   243
    MessageLevel _message_level;
alpar@461
   244
alpar@461
   245
  public:
alpar@461
   246
alpar@461
   247
    ///Set the verbosity of the messages
alpar@461
   248
alpar@461
   249
    ///Set the verbosity of the messages
alpar@461
   250
    ///
alpar@461
   251
    ///\param m is the level of the messages output by the solver routines.
alpar@461
   252
    void messageLevel(MessageLevel m);
alpar@461
   253
  };
alpar@461
   254
alpar@461
   255
alpar@461
   256
} //END OF NAMESPACE LEMON
alpar@461
   257
alpar@461
   258
#endif //LEMON_GLPK_H
alpar@461
   259