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