alpar@461: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@461:  *
alpar@461:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@461:  *
alpar@461:  * Copyright (C) 2003-2008
alpar@461:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@461:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@461:  *
alpar@461:  * Permission to use, modify and distribute this software is granted
alpar@461:  * provided that this copyright notice appears in all copies. For
alpar@461:  * precise terms see the accompanying LICENSE file.
alpar@461:  *
alpar@461:  * This software is provided "AS IS" with no warranty of any kind,
alpar@461:  * express or implied, and with no claim as to its suitability for any
alpar@461:  * purpose.
alpar@461:  *
alpar@461:  */
alpar@461: 
alpar@461: #ifndef LEMON_GLPK_H
alpar@461: #define LEMON_GLPK_H
alpar@461: 
alpar@461: ///\file
alpar@461: ///\brief Header of the LEMON-GLPK lp solver interface.
alpar@461: ///\ingroup lp_group
alpar@461: 
alpar@461: #include <lemon/lp_base.h>
alpar@461: 
alpar@461: // forward declaration
deba@650: #if !defined _GLP_PROB && !defined GLP_PROB
alpar@461: #define _GLP_PROB
deba@650: #define GLP_PROB
deba@650: typedef struct { double _opaque_prob; } glp_prob;
alpar@461: /* LP/MIP problem object */
alpar@461: #endif
alpar@461: 
alpar@461: namespace lemon {
alpar@461: 
alpar@461: 
alpar@461:   /// \brief Base interface for the GLPK LP and MIP solver
alpar@461:   ///
alpar@461:   /// This class implements the common interface of the GLPK LP and MIP solver.
alpar@461:   /// \ingroup lp_group
alpar@461:   class GlpkBase : virtual public LpBase {
alpar@461:   protected:
alpar@461: 
alpar@461:     typedef glp_prob LPX;
alpar@461:     glp_prob* lp;
alpar@461: 
alpar@461:     GlpkBase();
alpar@461:     GlpkBase(const GlpkBase&);
alpar@461:     virtual ~GlpkBase();
alpar@461: 
alpar@461:   protected:
alpar@461: 
alpar@461:     virtual int _addCol();
alpar@461:     virtual int _addRow();
alpar@461: 
alpar@461:     virtual void _eraseCol(int i);
alpar@461:     virtual void _eraseRow(int i);
alpar@461: 
alpar@461:     virtual void _eraseColId(int i);
alpar@461:     virtual void _eraseRowId(int i);
alpar@461: 
alpar@461:     virtual void _getColName(int col, std::string& name) const;
alpar@461:     virtual void _setColName(int col, const std::string& name);
alpar@461:     virtual int _colByName(const std::string& name) const;
alpar@461: 
alpar@461:     virtual void _getRowName(int row, std::string& name) const;
alpar@461:     virtual void _setRowName(int row, const std::string& name);
alpar@461:     virtual int _rowByName(const std::string& name) const;
alpar@461: 
alpar@461:     virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@461:     virtual void _getRowCoeffs(int i, InsertIterator b) const;
alpar@461: 
alpar@461:     virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@461:     virtual void _getColCoeffs(int i, InsertIterator b) const;
alpar@461: 
alpar@461:     virtual void _setCoeff(int row, int col, Value value);
alpar@461:     virtual Value _getCoeff(int row, int col) const;
alpar@461: 
alpar@461:     virtual void _setColLowerBound(int i, Value value);
alpar@461:     virtual Value _getColLowerBound(int i) const;
alpar@461: 
alpar@461:     virtual void _setColUpperBound(int i, Value value);
alpar@461:     virtual Value _getColUpperBound(int i) const;
alpar@461: 
alpar@461:     virtual void _setRowLowerBound(int i, Value value);
alpar@461:     virtual Value _getRowLowerBound(int i) const;
alpar@461: 
alpar@461:     virtual void _setRowUpperBound(int i, Value value);
alpar@461:     virtual Value _getRowUpperBound(int i) const;
alpar@461: 
alpar@461:     virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
alpar@461:     virtual void _getObjCoeffs(InsertIterator b) const;
alpar@461: 
alpar@461:     virtual void _setObjCoeff(int i, Value obj_coef);
alpar@461:     virtual Value _getObjCoeff(int i) const;
alpar@461: 
alpar@461:     virtual void _setSense(Sense);
alpar@461:     virtual Sense _getSense() const;
alpar@461: 
alpar@461:     virtual void _clear();
alpar@461: 
deba@576:     virtual void _messageLevel(MessageLevel level);
deba@576: 
deba@538:   private:
deba@538: 
deba@538:     static void freeEnv();
deba@538: 
deba@538:     struct FreeEnvHelper {
deba@538:       ~FreeEnvHelper() {
deba@538:         freeEnv();
deba@538:       }
deba@538:     };
deba@538:     
deba@538:     static FreeEnvHelper freeEnvHelper;
deba@576: 
deba@576:   protected:
deba@576:     
deba@576:     int _message_level;
deba@538:     
alpar@461:   public:
alpar@461: 
alpar@461:     ///Pointer to the underlying GLPK data structure.
alpar@461:     LPX *lpx() {return lp;}
alpar@461:     ///Const pointer to the underlying GLPK data structure.
alpar@461:     const LPX *lpx() const {return lp;}
alpar@461: 
alpar@461:     ///Returns the constraint identifier understood by GLPK.
alpar@461:     int lpxRow(Row r) const { return rows(id(r)); }
alpar@461: 
alpar@461:     ///Returns the variable identifier understood by GLPK.
alpar@461:     int lpxCol(Col c) const { return cols(id(c)); }
alpar@461: 
alpar@461:   };
alpar@461: 
alpar@461:   /// \brief Interface for the GLPK LP solver
alpar@461:   ///
alpar@461:   /// This class implements an interface for the GLPK LP solver.
alpar@461:   ///\ingroup lp_group
alpar@540:   class GlpkLp : public LpSolver, public GlpkBase {
alpar@461:   public:
alpar@461: 
alpar@461:     ///\e
alpar@462:     GlpkLp();
alpar@461:     ///\e
alpar@462:     GlpkLp(const GlpkLp&);
alpar@461: 
alpar@540:     ///\e
alpar@540:     virtual GlpkLp* cloneSolver() const;
alpar@540:     ///\e
alpar@540:     virtual GlpkLp* newSolver() const;
alpar@540: 
alpar@461:   private:
alpar@461: 
alpar@461:     mutable std::vector<double> _primal_ray;
alpar@461:     mutable std::vector<double> _dual_ray;
alpar@461: 
alpar@461:     void _clear_temporals();
alpar@461: 
alpar@461:   protected:
alpar@461: 
alpar@461:     virtual const char* _solverName() const;
alpar@461: 
alpar@461:     virtual SolveExitStatus _solve();
alpar@461:     virtual Value _getPrimal(int i) const;
alpar@461:     virtual Value _getDual(int i) const;
alpar@461: 
alpar@461:     virtual Value _getPrimalValue() const;
alpar@461: 
alpar@461:     virtual VarStatus _getColStatus(int i) const;
alpar@461:     virtual VarStatus _getRowStatus(int i) const;
alpar@461: 
alpar@461:     virtual Value _getPrimalRay(int i) const;
alpar@461:     virtual Value _getDualRay(int i) const;
alpar@461: 
alpar@461:     virtual ProblemType _getPrimalType() const;
alpar@461:     virtual ProblemType _getDualType() const;
alpar@461: 
alpar@461:   public:
alpar@461: 
alpar@461:     ///Solve with primal simplex
alpar@461:     SolveExitStatus solvePrimal();
alpar@461: 
alpar@461:     ///Solve with dual simplex
alpar@461:     SolveExitStatus solveDual();
alpar@461: 
deba@565:   private:
deba@565: 
deba@565:     bool _presolve;
deba@565: 
deba@565:   public:
deba@565: 
alpar@461:     ///Turns on or off the presolver
alpar@461: 
alpar@461:     ///Turns on (\c b is \c true) or off (\c b is \c false) the presolver
alpar@461:     ///
alpar@461:     ///The presolver is off by default.
deba@565:     void presolver(bool presolve);
alpar@461: 
alpar@461:   };
alpar@461: 
alpar@461:   /// \brief Interface for the GLPK MIP solver
alpar@461:   ///
alpar@461:   /// This class implements an interface for the GLPK MIP solver.
alpar@461:   ///\ingroup lp_group
alpar@540:   class GlpkMip : public MipSolver, public GlpkBase {
alpar@461:   public:
alpar@461: 
alpar@461:     ///\e
alpar@462:     GlpkMip();
alpar@461:     ///\e
alpar@462:     GlpkMip(const GlpkMip&);
alpar@461: 
alpar@540:     virtual GlpkMip* cloneSolver() const;
alpar@540:     virtual GlpkMip* newSolver() const;
alpar@540: 
alpar@461:   protected:
alpar@461: 
alpar@461:     virtual const char* _solverName() const;
alpar@461: 
alpar@461:     virtual ColTypes _getColType(int col) const;
alpar@461:     virtual void _setColType(int col, ColTypes col_type);
alpar@461: 
alpar@461:     virtual SolveExitStatus _solve();
alpar@461:     virtual ProblemType _getType() const;
alpar@461:     virtual Value _getSol(int i) const;
alpar@461:     virtual Value _getSolValue() const;
alpar@461: 
alpar@461:   };
alpar@461: 
alpar@461: 
alpar@461: } //END OF NAMESPACE LEMON
alpar@461: 
alpar@461: #endif //LEMON_GLPK_H
alpar@461: