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: * deba@598: * Copyright (C) 2003-2009 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_CPLEX_H alpar@484: #define LEMON_CPLEX_H alpar@484: alpar@484: ///\file alpar@484: ///\brief Header of the LEMON-CPLEX lp solver interface. alpar@484: alpar@484: #include alpar@484: alpar@484: struct cpxenv; alpar@484: struct cpxlp; alpar@484: alpar@484: namespace lemon { alpar@484: alpar@484: /// \brief Reference counted wrapper around cpxenv pointer alpar@484: /// alpar@484: /// The cplex uses environment object which is responsible for alpar@484: /// checking the proper license usage. This class provides a simple alpar@484: /// interface for share the environment object between different alpar@484: /// problems. alpar@484: class CplexEnv { alpar@484: friend class CplexBase; alpar@484: private: alpar@484: cpxenv* _env; alpar@484: mutable int* _cnt; alpar@484: alpar@484: public: alpar@484: alpar@484: /// \brief This exception is thrown when the license check is not alpar@484: /// sufficient alpar@484: class LicenseError : public Exception { alpar@484: friend class CplexEnv; alpar@484: private: alpar@484: alpar@484: LicenseError(int status); alpar@484: char _message[510]; alpar@484: alpar@484: public: alpar@484: alpar@484: /// The short error message alpar@484: virtual const char* what() const throw() { alpar@484: return _message; alpar@484: } alpar@484: }; alpar@484: alpar@484: /// Constructor alpar@484: CplexEnv(); alpar@484: /// Shallow copy constructor alpar@484: CplexEnv(const CplexEnv&); alpar@484: /// Shallow assignement alpar@484: CplexEnv& operator=(const CplexEnv&); alpar@484: /// Destructor alpar@484: virtual ~CplexEnv(); alpar@484: alpar@484: protected: alpar@484: alpar@484: cpxenv* cplexEnv() { return _env; } alpar@484: const cpxenv* cplexEnv() const { return _env; } alpar@484: }; alpar@484: alpar@484: /// \brief Base interface for the CPLEX LP and MIP solver alpar@484: /// alpar@484: /// This class implements the common interface of the CPLEX LP and deba@598: /// MIP solvers. alpar@484: /// \ingroup lp_group alpar@484: class CplexBase : virtual public LpBase { alpar@484: protected: alpar@484: alpar@484: CplexEnv _env; alpar@484: cpxlp* _prob; alpar@484: alpar@484: CplexBase(); alpar@484: CplexBase(const CplexEnv&); alpar@484: CplexBase(const CplexBase &); alpar@484: virtual ~CplexBase(); 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: private: alpar@484: void _set_row_bounds(int i, Value lb, Value ub); alpar@484: protected: 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 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: void _applyMessageLevel(); deba@623: deba@623: bool _message_enabled; deba@623: alpar@1231: void _write(std::string file, std::string format) const; alpar@1231: alpar@484: public: alpar@484: alpar@484: /// Returns the used \c CplexEnv instance alpar@484: const CplexEnv& env() const { return _env; } deba@623: deba@623: /// \brief Returns the const cpxenv pointer alpar@484: /// deba@623: /// \note The cpxenv might be destructed with the solver. alpar@484: const cpxenv* cplexEnv() const { return _env.cplexEnv(); } alpar@484: deba@623: /// \brief Returns the const cpxenv pointer deba@623: /// deba@623: /// \note The cpxenv might be destructed with the solver. deba@623: cpxenv* cplexEnv() { return _env.cplexEnv(); } deba@623: deba@623: /// Returns the cplex problem object alpar@484: cpxlp* cplexLp() { return _prob; } deba@623: /// Returns the cplex problem object alpar@484: const cpxlp* cplexLp() const { return _prob; } alpar@484: alpar@1231: #ifdef DOXYGEN alpar@1231: /// Write the problem or the solution to a file in the given format alpar@1231: alpar@1231: /// This function writes the problem or the solution alpar@1231: /// to a file in the given format. alpar@1231: /// Trying to write in an unsupported format will trigger alpar@1250: /// \ref lemon::LpBase::UnsupportedFormatError "UnsupportedFormatError". alpar@1231: /// \param file The file path alpar@1231: /// \param format The output file format. alpar@1231: /// Supportted formats are "MPS", "LP" and "SOL". alpar@1231: void write(std::string file, std::string format = "MPS") const {} alpar@1231: #endif alpar@1231: alpar@484: }; alpar@484: alpar@484: /// \brief Interface for the CPLEX LP solver alpar@484: /// alpar@484: /// This class implements an interface for the CPLEX LP solver. alpar@484: ///\ingroup lp_group alpar@587: class CplexLp : public LpSolver, public CplexBase { alpar@484: public: alpar@484: /// \e alpar@485: CplexLp(); alpar@484: /// \e alpar@485: CplexLp(const CplexEnv&); alpar@484: /// \e alpar@485: CplexLp(const CplexLp&); alpar@484: /// \e alpar@485: virtual ~CplexLp(); alpar@484: alpar@587: /// \e alpar@587: virtual CplexLp* cloneSolver() const; alpar@587: /// \e alpar@587: virtual CplexLp* newSolver() const; alpar@587: alpar@484: private: alpar@484: alpar@484: // these values cannot retrieved element by element alpar@484: mutable std::vector _col_status; alpar@484: mutable std::vector _row_status; 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: SolveExitStatus convertStatus(int status); 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: 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 method alpar@484: SolveExitStatus solvePrimal(); alpar@484: alpar@484: /// Solve with dual simplex method alpar@484: SolveExitStatus solveDual(); alpar@484: alpar@484: /// Solve with barrier method alpar@484: SolveExitStatus solveBarrier(); alpar@484: alpar@484: }; alpar@484: alpar@484: /// \brief Interface for the CPLEX MIP solver alpar@484: /// alpar@484: /// This class implements an interface for the CPLEX MIP solver. alpar@484: ///\ingroup lp_group alpar@587: class CplexMip : public MipSolver, public CplexBase { alpar@484: public: alpar@484: /// \e alpar@485: CplexMip(); alpar@484: /// \e alpar@485: CplexMip(const CplexEnv&); alpar@484: /// \e alpar@485: CplexMip(const CplexMip&); alpar@484: /// \e alpar@485: virtual ~CplexMip(); alpar@484: deba@598: /// \e deba@598: virtual CplexMip* cloneSolver() const; deba@598: /// \e deba@598: virtual CplexMip* newSolver() const; deba@598: alpar@484: protected: alpar@484: 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: } //END OF NAMESPACE LEMON alpar@484: alpar@484: #endif //LEMON_CPLEX_H alpar@484: