lemon/glpk.h
changeset 802 994c7df296c9
parent 576 745e182d0139
child 746 e4554cd6b2bf
child 832 5100072d83ca
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/glpk.h	Thu Dec 10 17:05:35 2009 +0100
     1.3 @@ -0,0 +1,236 @@
     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 +#if !defined _GLP_PROB && !defined GLP_PROB
    1.33 +#define _GLP_PROB
    1.34 +#define GLP_PROB
    1.35 +typedef struct { double _opaque_prob; } glp_prob;
    1.36 +/* LP/MIP problem object */
    1.37 +#endif
    1.38 +
    1.39 +namespace lemon {
    1.40 +
    1.41 +
    1.42 +  /// \brief Base interface for the GLPK LP and MIP solver
    1.43 +  ///
    1.44 +  /// This class implements the common interface of the GLPK LP and MIP solver.
    1.45 +  /// \ingroup lp_group
    1.46 +  class GlpkBase : virtual public LpBase {
    1.47 +  protected:
    1.48 +
    1.49 +    typedef glp_prob LPX;
    1.50 +    glp_prob* lp;
    1.51 +
    1.52 +    GlpkBase();
    1.53 +    GlpkBase(const GlpkBase&);
    1.54 +    virtual ~GlpkBase();
    1.55 +
    1.56 +  protected:
    1.57 +
    1.58 +    virtual int _addCol();
    1.59 +    virtual int _addRow();
    1.60 +
    1.61 +    virtual void _eraseCol(int i);
    1.62 +    virtual void _eraseRow(int i);
    1.63 +
    1.64 +    virtual void _eraseColId(int i);
    1.65 +    virtual void _eraseRowId(int i);
    1.66 +
    1.67 +    virtual void _getColName(int col, std::string& name) const;
    1.68 +    virtual void _setColName(int col, const std::string& name);
    1.69 +    virtual int _colByName(const std::string& name) const;
    1.70 +
    1.71 +    virtual void _getRowName(int row, std::string& name) const;
    1.72 +    virtual void _setRowName(int row, const std::string& name);
    1.73 +    virtual int _rowByName(const std::string& name) const;
    1.74 +
    1.75 +    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
    1.76 +    virtual void _getRowCoeffs(int i, InsertIterator b) const;
    1.77 +
    1.78 +    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
    1.79 +    virtual void _getColCoeffs(int i, InsertIterator b) const;
    1.80 +
    1.81 +    virtual void _setCoeff(int row, int col, Value value);
    1.82 +    virtual Value _getCoeff(int row, int col) const;
    1.83 +
    1.84 +    virtual void _setColLowerBound(int i, Value value);
    1.85 +    virtual Value _getColLowerBound(int i) const;
    1.86 +
    1.87 +    virtual void _setColUpperBound(int i, Value value);
    1.88 +    virtual Value _getColUpperBound(int i) const;
    1.89 +
    1.90 +    virtual void _setRowLowerBound(int i, Value value);
    1.91 +    virtual Value _getRowLowerBound(int i) const;
    1.92 +
    1.93 +    virtual void _setRowUpperBound(int i, Value value);
    1.94 +    virtual Value _getRowUpperBound(int i) const;
    1.95 +
    1.96 +    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
    1.97 +    virtual void _getObjCoeffs(InsertIterator b) const;
    1.98 +
    1.99 +    virtual void _setObjCoeff(int i, Value obj_coef);
   1.100 +    virtual Value _getObjCoeff(int i) const;
   1.101 +
   1.102 +    virtual void _setSense(Sense);
   1.103 +    virtual Sense _getSense() const;
   1.104 +
   1.105 +    virtual void _clear();
   1.106 +
   1.107 +    virtual void _messageLevel(MessageLevel level);
   1.108 +
   1.109 +  private:
   1.110 +
   1.111 +    static void freeEnv();
   1.112 +
   1.113 +    struct FreeEnvHelper {
   1.114 +      ~FreeEnvHelper() {
   1.115 +        freeEnv();
   1.116 +      }
   1.117 +    };
   1.118 +    
   1.119 +    static FreeEnvHelper freeEnvHelper;
   1.120 +
   1.121 +  protected:
   1.122 +    
   1.123 +    int _message_level;
   1.124 +    
   1.125 +  public:
   1.126 +
   1.127 +    ///Pointer to the underlying GLPK data structure.
   1.128 +    LPX *lpx() {return lp;}
   1.129 +    ///Const pointer to the underlying GLPK data structure.
   1.130 +    const LPX *lpx() const {return lp;}
   1.131 +
   1.132 +    ///Returns the constraint identifier understood by GLPK.
   1.133 +    int lpxRow(Row r) const { return rows(id(r)); }
   1.134 +
   1.135 +    ///Returns the variable identifier understood by GLPK.
   1.136 +    int lpxCol(Col c) const { return cols(id(c)); }
   1.137 +
   1.138 +  };
   1.139 +
   1.140 +  /// \brief Interface for the GLPK LP solver
   1.141 +  ///
   1.142 +  /// This class implements an interface for the GLPK LP solver.
   1.143 +  ///\ingroup lp_group
   1.144 +  class GlpkLp : public LpSolver, public GlpkBase {
   1.145 +  public:
   1.146 +
   1.147 +    ///\e
   1.148 +    GlpkLp();
   1.149 +    ///\e
   1.150 +    GlpkLp(const GlpkLp&);
   1.151 +
   1.152 +    ///\e
   1.153 +    virtual GlpkLp* cloneSolver() const;
   1.154 +    ///\e
   1.155 +    virtual GlpkLp* newSolver() const;
   1.156 +
   1.157 +  private:
   1.158 +
   1.159 +    mutable std::vector<double> _primal_ray;
   1.160 +    mutable std::vector<double> _dual_ray;
   1.161 +
   1.162 +    void _clear_temporals();
   1.163 +
   1.164 +  protected:
   1.165 +
   1.166 +    virtual const char* _solverName() const;
   1.167 +
   1.168 +    virtual SolveExitStatus _solve();
   1.169 +    virtual Value _getPrimal(int i) const;
   1.170 +    virtual Value _getDual(int i) const;
   1.171 +
   1.172 +    virtual Value _getPrimalValue() const;
   1.173 +
   1.174 +    virtual VarStatus _getColStatus(int i) const;
   1.175 +    virtual VarStatus _getRowStatus(int i) const;
   1.176 +
   1.177 +    virtual Value _getPrimalRay(int i) const;
   1.178 +    virtual Value _getDualRay(int i) const;
   1.179 +
   1.180 +    virtual ProblemType _getPrimalType() const;
   1.181 +    virtual ProblemType _getDualType() const;
   1.182 +
   1.183 +  public:
   1.184 +
   1.185 +    ///Solve with primal simplex
   1.186 +    SolveExitStatus solvePrimal();
   1.187 +
   1.188 +    ///Solve with dual simplex
   1.189 +    SolveExitStatus solveDual();
   1.190 +
   1.191 +  private:
   1.192 +
   1.193 +    bool _presolve;
   1.194 +
   1.195 +  public:
   1.196 +
   1.197 +    ///Turns on or off the presolver
   1.198 +
   1.199 +    ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
   1.200 +    ///
   1.201 +    ///The presolver is off by default.
   1.202 +    void presolver(bool presolve);
   1.203 +
   1.204 +  };
   1.205 +
   1.206 +  /// \brief Interface for the GLPK MIP solver
   1.207 +  ///
   1.208 +  /// This class implements an interface for the GLPK MIP solver.
   1.209 +  ///\ingroup lp_group
   1.210 +  class GlpkMip : public MipSolver, public GlpkBase {
   1.211 +  public:
   1.212 +
   1.213 +    ///\e
   1.214 +    GlpkMip();
   1.215 +    ///\e
   1.216 +    GlpkMip(const GlpkMip&);
   1.217 +
   1.218 +    virtual GlpkMip* cloneSolver() const;
   1.219 +    virtual GlpkMip* newSolver() const;
   1.220 +
   1.221 +  protected:
   1.222 +
   1.223 +    virtual const char* _solverName() const;
   1.224 +
   1.225 +    virtual ColTypes _getColType(int col) const;
   1.226 +    virtual void _setColType(int col, ColTypes col_type);
   1.227 +
   1.228 +    virtual SolveExitStatus _solve();
   1.229 +    virtual ProblemType _getType() const;
   1.230 +    virtual Value _getSol(int i) const;
   1.231 +    virtual Value _getSolValue() const;
   1.232 +
   1.233 +  };
   1.234 +
   1.235 +
   1.236 +} //END OF NAMESPACE LEMON
   1.237 +
   1.238 +#endif //LEMON_GLPK_H
   1.239 +