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