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_CPLEX_H alpar@461: #define LEMON_CPLEX_H alpar@461: alpar@461: ///\file alpar@461: ///\brief Header of the LEMON-CPLEX lp solver interface. alpar@461: alpar@461: #include alpar@461: alpar@461: struct cpxenv; alpar@461: struct cpxlp; alpar@461: alpar@461: namespace lemon { alpar@461: alpar@461: /// \brief Reference counted wrapper around cpxenv pointer alpar@461: /// alpar@461: /// The cplex uses environment object which is responsible for alpar@461: /// checking the proper license usage. This class provides a simple alpar@461: /// interface for share the environment object between different alpar@461: /// problems. alpar@461: class CplexEnv { alpar@461: friend class CplexBase; alpar@461: private: alpar@461: cpxenv* _env; alpar@461: mutable int* _cnt; alpar@461: alpar@461: public: alpar@461: alpar@461: /// \brief This exception is thrown when the license check is not alpar@461: /// sufficient alpar@461: class LicenseError : public Exception { alpar@461: friend class CplexEnv; alpar@461: private: alpar@461: alpar@461: LicenseError(int status); alpar@461: char _message[510]; alpar@461: alpar@461: public: alpar@461: alpar@461: /// The short error message alpar@461: virtual const char* what() const throw() { alpar@461: return _message; alpar@461: } alpar@461: }; alpar@461: alpar@461: /// Constructor alpar@461: CplexEnv(); alpar@461: /// Shallow copy constructor alpar@461: CplexEnv(const CplexEnv&); alpar@461: /// Shallow assignement alpar@461: CplexEnv& operator=(const CplexEnv&); alpar@461: /// Destructor alpar@461: virtual ~CplexEnv(); alpar@461: alpar@461: protected: alpar@461: alpar@461: cpxenv* cplexEnv() { return _env; } alpar@461: const cpxenv* cplexEnv() const { return _env; } alpar@461: }; alpar@461: alpar@461: /// \brief Base interface for the CPLEX LP and MIP solver alpar@461: /// alpar@461: /// This class implements the common interface of the CPLEX LP and alpar@461: /// MIP solvers. alpar@461: /// \ingroup lp_group alpar@461: class CplexBase : virtual public LpBase { alpar@461: protected: alpar@461: alpar@461: CplexEnv _env; alpar@461: cpxlp* _prob; alpar@461: alpar@461: CplexBase(); alpar@461: CplexBase(const CplexEnv&); alpar@461: CplexBase(const CplexBase &); alpar@461: virtual ~CplexBase(); 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: private: alpar@461: void _set_row_bounds(int i, Value lb, Value ub); alpar@461: protected: 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 sense); alpar@461: virtual Sense _getSense() const; alpar@461: alpar@461: virtual void _clear(); alpar@461: alpar@461: public: alpar@461: alpar@461: /// Returns the used \c CplexEnv instance alpar@461: const CplexEnv& env() const { return _env; } alpar@461: /// alpar@461: const cpxenv* cplexEnv() const { return _env.cplexEnv(); } alpar@461: alpar@461: cpxlp* cplexLp() { return _prob; } alpar@461: const cpxlp* cplexLp() const { return _prob; } alpar@461: alpar@461: }; alpar@461: alpar@461: /// \brief Interface for the CPLEX LP solver alpar@461: /// alpar@461: /// This class implements an interface for the CPLEX LP solver. alpar@461: ///\ingroup lp_group alpar@528: class CplexLp : public LpSolver, public CplexBase { alpar@461: public: alpar@461: /// \e alpar@462: CplexLp(); alpar@461: /// \e alpar@462: CplexLp(const CplexEnv&); alpar@461: /// \e alpar@462: CplexLp(const CplexLp&); alpar@461: /// \e alpar@462: virtual ~CplexLp(); alpar@461: alpar@528: /// \e alpar@528: virtual CplexLp* cloneSolver() const; alpar@528: /// \e alpar@528: virtual CplexLp* newSolver() const; alpar@528: alpar@461: private: alpar@461: alpar@461: // these values cannot retrieved element by element alpar@461: mutable std::vector _col_status; alpar@461: mutable std::vector _row_status; alpar@461: alpar@461: mutable std::vector _primal_ray; alpar@461: mutable std::vector _dual_ray; alpar@461: alpar@461: void _clear_temporals(); alpar@461: alpar@461: SolveExitStatus convertStatus(int status); 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: 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 method alpar@461: SolveExitStatus solvePrimal(); alpar@461: alpar@461: /// Solve with dual simplex method alpar@461: SolveExitStatus solveDual(); alpar@461: alpar@461: /// Solve with barrier method alpar@461: SolveExitStatus solveBarrier(); alpar@461: alpar@461: }; alpar@461: alpar@461: /// \brief Interface for the CPLEX MIP solver alpar@461: /// alpar@461: /// This class implements an interface for the CPLEX MIP solver. alpar@461: ///\ingroup lp_group alpar@528: class CplexMip : public MipSolver, public CplexBase { alpar@461: public: alpar@461: /// \e alpar@462: CplexMip(); alpar@461: /// \e alpar@462: CplexMip(const CplexEnv&); alpar@461: /// \e alpar@462: CplexMip(const CplexMip&); alpar@461: /// \e alpar@462: virtual ~CplexMip(); alpar@461: alpar@461: protected: alpar@461: alpar@462: virtual CplexMip* _cloneSolver() const; alpar@462: virtual CplexMip* _newSolver() const; 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: } //END OF NAMESPACE LEMON alpar@461: alpar@461: #endif //LEMON_CPLEX_H alpar@461: