lemon/glpk.h
changeset 461 08d495d48089
child 462 9b082b3fb33f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/glpk.h	Mon Jan 12 12:26:01 2009 +0000
     1.3 @@ -0,0 +1,259 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_GLPK_H
    1.23 +#define LEMON_GLPK_H
    1.24 +
    1.25 +///\file
    1.26 +///\brief Header of the LEMON-GLPK lp solver interface.
    1.27 +///\ingroup lp_group
    1.28 +
    1.29 +#include <lemon/lp_base.h>
    1.30 +
    1.31 +// forward declaration
    1.32 +#ifndef _GLP_PROB
    1.33 +#define _GLP_PROB
    1.34 +typedef struct { double _prob; } glp_prob;
    1.35 +/* LP/MIP problem object */
    1.36 +#endif
    1.37 +
    1.38 +namespace lemon {
    1.39 +
    1.40 +
    1.41 +  /// \brief Base interface for the GLPK LP and MIP solver
    1.42 +  ///
    1.43 +  /// This class implements the common interface of the GLPK LP and MIP solver.
    1.44 +  /// \ingroup lp_group
    1.45 +  class GlpkBase : virtual public LpBase {
    1.46 +  protected:
    1.47 +
    1.48 +    typedef glp_prob LPX;
    1.49 +    glp_prob* lp;
    1.50 +
    1.51 +    GlpkBase();
    1.52 +    GlpkBase(const GlpkBase&);
    1.53 +    virtual ~GlpkBase();
    1.54 +
    1.55 +  protected:
    1.56 +
    1.57 +    virtual int _addCol();
    1.58 +    virtual int _addRow();
    1.59 +
    1.60 +    virtual void _eraseCol(int i);
    1.61 +    virtual void _eraseRow(int i);
    1.62 +
    1.63 +    virtual void _eraseColId(int i);
    1.64 +    virtual void _eraseRowId(int i);
    1.65 +
    1.66 +    virtual void _getColName(int col, std::string& name) const;
    1.67 +    virtual void _setColName(int col, const std::string& name);
    1.68 +    virtual int _colByName(const std::string& name) const;
    1.69 +
    1.70 +    virtual void _getRowName(int row, std::string& name) const;
    1.71 +    virtual void _setRowName(int row, const std::string& name);
    1.72 +    virtual int _rowByName(const std::string& name) const;
    1.73 +
    1.74 +    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
    1.75 +    virtual void _getRowCoeffs(int i, InsertIterator b) const;
    1.76 +
    1.77 +    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
    1.78 +    virtual void _getColCoeffs(int i, InsertIterator b) const;
    1.79 +
    1.80 +    virtual void _setCoeff(int row, int col, Value value);
    1.81 +    virtual Value _getCoeff(int row, int col) const;
    1.82 +
    1.83 +    virtual void _setColLowerBound(int i, Value value);
    1.84 +    virtual Value _getColLowerBound(int i) const;
    1.85 +
    1.86 +    virtual void _setColUpperBound(int i, Value value);
    1.87 +    virtual Value _getColUpperBound(int i) const;
    1.88 +
    1.89 +    virtual void _setRowLowerBound(int i, Value value);
    1.90 +    virtual Value _getRowLowerBound(int i) const;
    1.91 +
    1.92 +    virtual void _setRowUpperBound(int i, Value value);
    1.93 +    virtual Value _getRowUpperBound(int i) const;
    1.94 +
    1.95 +    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
    1.96 +    virtual void _getObjCoeffs(InsertIterator b) const;
    1.97 +
    1.98 +    virtual void _setObjCoeff(int i, Value obj_coef);
    1.99 +    virtual Value _getObjCoeff(int i) const;
   1.100 +
   1.101 +    virtual void _setSense(Sense);
   1.102 +    virtual Sense _getSense() const;
   1.103 +
   1.104 +    virtual void _clear();
   1.105 +
   1.106 +  public:
   1.107 +
   1.108 +    ///Pointer to the underlying GLPK data structure.
   1.109 +    LPX *lpx() {return lp;}
   1.110 +    ///Const pointer to the underlying GLPK data structure.
   1.111 +    const LPX *lpx() const {return lp;}
   1.112 +
   1.113 +    ///Returns the constraint identifier understood by GLPK.
   1.114 +    int lpxRow(Row r) const { return rows(id(r)); }
   1.115 +
   1.116 +    ///Returns the variable identifier understood by GLPK.
   1.117 +    int lpxCol(Col c) const { return cols(id(c)); }
   1.118 +
   1.119 +  };
   1.120 +
   1.121 +  /// \brief Interface for the GLPK LP solver
   1.122 +  ///
   1.123 +  /// This class implements an interface for the GLPK LP solver.
   1.124 +  ///\ingroup lp_group
   1.125 +  class LpGlpk : public GlpkBase, public LpSolver {
   1.126 +  public:
   1.127 +
   1.128 +    ///\e
   1.129 +    LpGlpk();
   1.130 +    ///\e
   1.131 +    LpGlpk(const LpGlpk&);
   1.132 +
   1.133 +  private:
   1.134 +
   1.135 +    mutable std::vector<double> _primal_ray;
   1.136 +    mutable std::vector<double> _dual_ray;
   1.137 +
   1.138 +    void _clear_temporals();
   1.139 +
   1.140 +  protected:
   1.141 +
   1.142 +    virtual LpGlpk* _cloneSolver() const;
   1.143 +    virtual LpGlpk* _newSolver() const;
   1.144 +
   1.145 +    virtual const char* _solverName() const;
   1.146 +
   1.147 +    virtual SolveExitStatus _solve();
   1.148 +    virtual Value _getPrimal(int i) const;
   1.149 +    virtual Value _getDual(int i) const;
   1.150 +
   1.151 +    virtual Value _getPrimalValue() const;
   1.152 +
   1.153 +    virtual VarStatus _getColStatus(int i) const;
   1.154 +    virtual VarStatus _getRowStatus(int i) const;
   1.155 +
   1.156 +    virtual Value _getPrimalRay(int i) const;
   1.157 +    virtual Value _getDualRay(int i) const;
   1.158 +
   1.159 +    ///\todo It should be clarified
   1.160 +    ///
   1.161 +    virtual ProblemType _getPrimalType() const;
   1.162 +    virtual ProblemType _getDualType() const;
   1.163 +
   1.164 +  public:
   1.165 +
   1.166 +    ///Solve with primal simplex
   1.167 +    SolveExitStatus solvePrimal();
   1.168 +
   1.169 +    ///Solve with dual simplex
   1.170 +    SolveExitStatus solveDual();
   1.171 +
   1.172 +    ///Turns on or off the presolver
   1.173 +
   1.174 +    ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   1.175 +    ///
   1.176 +    ///The presolver is off by default.
   1.177 +    void presolver(bool b);
   1.178 +
   1.179 +    ///Enum for \c messageLevel() parameter
   1.180 +    enum MessageLevel {
   1.181 +      /// no output (default value)
   1.182 +      MESSAGE_NO_OUTPUT = 0,
   1.183 +      /// error messages only
   1.184 +      MESSAGE_ERROR_MESSAGE = 1,
   1.185 +      /// normal output
   1.186 +      MESSAGE_NORMAL_OUTPUT = 2,
   1.187 +      /// full output (includes informational messages)
   1.188 +      MESSAGE_FULL_OUTPUT = 3
   1.189 +    };
   1.190 +
   1.191 +  private:
   1.192 +
   1.193 +    MessageLevel _message_level;
   1.194 +
   1.195 +  public:
   1.196 +
   1.197 +    ///Set the verbosity of the messages
   1.198 +
   1.199 +    ///Set the verbosity of the messages
   1.200 +    ///
   1.201 +    ///\param m is the level of the messages output by the solver routines.
   1.202 +    void messageLevel(MessageLevel m);
   1.203 +  };
   1.204 +
   1.205 +  /// \brief Interface for the GLPK MIP solver
   1.206 +  ///
   1.207 +  /// This class implements an interface for the GLPK MIP solver.
   1.208 +  ///\ingroup lp_group
   1.209 +  class MipGlpk : public GlpkBase, public MipSolver {
   1.210 +  public:
   1.211 +
   1.212 +    ///\e
   1.213 +    MipGlpk();
   1.214 +    ///\e
   1.215 +    MipGlpk(const MipGlpk&);
   1.216 +
   1.217 +  protected:
   1.218 +
   1.219 +    virtual MipGlpk* _cloneSolver() const;
   1.220 +    virtual MipGlpk* _newSolver() const;
   1.221 +
   1.222 +    virtual const char* _solverName() const;
   1.223 +
   1.224 +    virtual ColTypes _getColType(int col) const;
   1.225 +    virtual void _setColType(int col, ColTypes col_type);
   1.226 +
   1.227 +    virtual SolveExitStatus _solve();
   1.228 +    virtual ProblemType _getType() const;
   1.229 +    virtual Value _getSol(int i) const;
   1.230 +    virtual Value _getSolValue() const;
   1.231 +
   1.232 +    ///Enum for \c messageLevel() parameter
   1.233 +    enum MessageLevel {
   1.234 +      /// no output (default value)
   1.235 +      MESSAGE_NO_OUTPUT = 0,
   1.236 +      /// error messages only
   1.237 +      MESSAGE_ERROR_MESSAGE = 1,
   1.238 +      /// normal output
   1.239 +      MESSAGE_NORMAL_OUTPUT = 2,
   1.240 +      /// full output (includes informational messages)
   1.241 +      MESSAGE_FULL_OUTPUT = 3
   1.242 +    };
   1.243 +
   1.244 +  private:
   1.245 +
   1.246 +    MessageLevel _message_level;
   1.247 +
   1.248 +  public:
   1.249 +
   1.250 +    ///Set the verbosity of the messages
   1.251 +
   1.252 +    ///Set the verbosity of the messages
   1.253 +    ///
   1.254 +    ///\param m is the level of the messages output by the solver routines.
   1.255 +    void messageLevel(MessageLevel m);
   1.256 +  };
   1.257 +
   1.258 +
   1.259 +} //END OF NAMESPACE LEMON
   1.260 +
   1.261 +#endif //LEMON_GLPK_H
   1.262 +