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