lemon/cplex.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 15 Oct 2018 23:39:22 +0200
changeset 1172 d7e25df22e88
parent 1092 dceba191c00d
permissions -rw-r--r--
Fix warnings emitted by g++ 7.3.1 (#614)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2013
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_CPLEX_H
    20 #define LEMON_CPLEX_H
    21 
    22 ///\file
    23 ///\brief Header of the LEMON-CPLEX lp solver interface.
    24 
    25 #include <lemon/lp_base.h>
    26 #include <lemon/bits/lock.h>
    27 
    28 struct cpxenv;
    29 struct cpxlp;
    30 
    31 namespace lemon {
    32 
    33   /// \brief Reference counted wrapper around cpxenv pointer
    34   ///
    35   /// The cplex uses environment object which is responsible for
    36   /// checking the proper license usage. This class provides a simple
    37   /// interface for share the environment object between different
    38   /// problems.
    39   class CplexEnv {
    40     friend class CplexBase;
    41   private:
    42     cpxenv* _env;
    43     mutable int* _cnt;
    44     mutable bits::Lock* _cnt_lock;
    45 
    46     void incCnt();
    47     void decCnt();
    48     
    49   public:
    50 
    51     /// \brief This exception is thrown when the license check is not
    52     /// sufficient
    53     class LicenseError : public Exception {
    54       friend class CplexEnv;
    55     private:
    56 
    57       LicenseError(int status);
    58       char _message[510];
    59 
    60     public:
    61 
    62       /// The short error message
    63       virtual const char* what() const throw() {
    64         return _message;
    65       }
    66     };
    67 
    68     /// Constructor
    69     CplexEnv();
    70     /// Shallow copy constructor
    71     CplexEnv(const CplexEnv&);
    72     /// Shallow assignement
    73     CplexEnv& operator=(const CplexEnv&);
    74     /// Destructor
    75     virtual ~CplexEnv();
    76 
    77   protected:
    78 
    79     cpxenv* cplexEnv() { return _env; }
    80     const cpxenv* cplexEnv() const { return _env; }
    81   };
    82 
    83   /// \brief Base interface for the CPLEX LP and MIP solver
    84   ///
    85   /// This class implements the common interface of the CPLEX LP and
    86   /// MIP solvers.
    87   /// \ingroup lp_group
    88   class CplexBase : virtual public LpBase {
    89   protected:
    90 
    91     CplexEnv _env;
    92     cpxlp* _prob;
    93 
    94     CplexBase();
    95     CplexBase(const CplexEnv&);
    96     CplexBase(const CplexBase &);
    97     virtual ~CplexBase();
    98 
    99     virtual int _addCol();
   100     virtual int _addRow();
   101     virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
   102 
   103     virtual void _eraseCol(int i);
   104     virtual void _eraseRow(int i);
   105 
   106     virtual void _eraseColId(int i);
   107     virtual void _eraseRowId(int i);
   108 
   109     virtual void _getColName(int col, std::string& name) const;
   110     virtual void _setColName(int col, const std::string& name);
   111     virtual int _colByName(const std::string& name) const;
   112 
   113     virtual void _getRowName(int row, std::string& name) const;
   114     virtual void _setRowName(int row, const std::string& name);
   115     virtual int _rowByName(const std::string& name) const;
   116 
   117     virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
   118     virtual void _getRowCoeffs(int i, InsertIterator b) const;
   119 
   120     virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
   121     virtual void _getColCoeffs(int i, InsertIterator b) const;
   122 
   123     virtual void _setCoeff(int row, int col, Value value);
   124     virtual Value _getCoeff(int row, int col) const;
   125 
   126     virtual void _setColLowerBound(int i, Value value);
   127     virtual Value _getColLowerBound(int i) const;
   128 
   129     virtual void _setColUpperBound(int i, Value value);
   130     virtual Value _getColUpperBound(int i) const;
   131 
   132   private:
   133     void _set_row_bounds(int i, Value lb, Value ub);
   134   protected:
   135 
   136     virtual void _setRowLowerBound(int i, Value value);
   137     virtual Value _getRowLowerBound(int i) const;
   138 
   139     virtual void _setRowUpperBound(int i, Value value);
   140     virtual Value _getRowUpperBound(int i) const;
   141 
   142     virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
   143     virtual void _getObjCoeffs(InsertIterator b) const;
   144 
   145     virtual void _setObjCoeff(int i, Value obj_coef);
   146     virtual Value _getObjCoeff(int i) const;
   147 
   148     virtual void _setSense(Sense sense);
   149     virtual Sense _getSense() const;
   150 
   151     virtual void _clear();
   152 
   153     virtual void _messageLevel(MessageLevel level);
   154     void _applyMessageLevel();
   155 
   156     bool _message_enabled;
   157 
   158     void _write(std::string file, std::string format) const;
   159 
   160   public:
   161 
   162     /// Returns the used \c CplexEnv instance
   163     const CplexEnv& env() const { return _env; }
   164 
   165     /// \brief Returns the const cpxenv pointer
   166     ///
   167     /// \note The cpxenv might be destructed with the solver.
   168     const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
   169 
   170     /// \brief Returns the const cpxenv pointer
   171     ///
   172     /// \note The cpxenv might be destructed with the solver.
   173     cpxenv* cplexEnv() { return _env.cplexEnv(); }
   174 
   175     /// Returns the cplex problem object
   176     cpxlp* cplexLp() { return _prob; }
   177     /// Returns the cplex problem object
   178     const cpxlp* cplexLp() const { return _prob; }
   179 
   180 #ifdef DOXYGEN
   181     /// Write the problem or the solution to a file in the given format
   182 
   183     /// This function writes the problem or the solution
   184     /// to a file in the given format.
   185     /// Trying to write in an unsupported format will trigger
   186     /// \ref lemon::LpBase::UnsupportedFormatError "UnsupportedFormatError".
   187     /// \param file The file path
   188     /// \param format The output file format.
   189     /// Supportted formats are "MPS", "LP" and "SOL".
   190     void write(std::string file, std::string format = "MPS") const {}
   191 #endif
   192 
   193   };
   194 
   195   /// \brief Interface for the CPLEX LP solver
   196   ///
   197   /// This class implements an interface for the CPLEX LP solver.
   198   ///\ingroup lp_group
   199   class CplexLp : public LpSolver, public CplexBase {
   200   public:
   201     /// \e
   202     CplexLp();
   203     /// \e
   204     CplexLp(const CplexEnv&);
   205     /// \e
   206     CplexLp(const CplexLp&);
   207     /// \e
   208     virtual ~CplexLp();
   209 
   210     /// \e
   211     virtual CplexLp* cloneSolver() const;
   212     /// \e
   213     virtual CplexLp* newSolver() const;
   214 
   215   private:
   216 
   217     // these values cannot retrieved element by element
   218     mutable std::vector<int> _col_status;
   219     mutable std::vector<int> _row_status;
   220 
   221     mutable std::vector<Value> _primal_ray;
   222     mutable std::vector<Value> _dual_ray;
   223 
   224     void _clear_temporals();
   225 
   226     SolveExitStatus convertStatus(int status);
   227 
   228   protected:
   229 
   230     virtual const char* _solverName() const;
   231 
   232     virtual SolveExitStatus _solve();
   233     virtual Value _getPrimal(int i) const;
   234     virtual Value _getDual(int i) const;
   235     virtual Value _getPrimalValue() const;
   236 
   237     virtual VarStatus _getColStatus(int i) const;
   238     virtual VarStatus _getRowStatus(int i) const;
   239 
   240     virtual Value _getPrimalRay(int i) const;
   241     virtual Value _getDualRay(int i) const;
   242 
   243     virtual ProblemType _getPrimalType() const;
   244     virtual ProblemType _getDualType() const;
   245 
   246   public:
   247 
   248     /// Solve with primal simplex method
   249     SolveExitStatus solvePrimal();
   250 
   251     /// Solve with dual simplex method
   252     SolveExitStatus solveDual();
   253 
   254     /// Solve with barrier method
   255     SolveExitStatus solveBarrier();
   256 
   257   };
   258 
   259   /// \brief Interface for the CPLEX MIP solver
   260   ///
   261   /// This class implements an interface for the CPLEX MIP solver.
   262   ///\ingroup lp_group
   263   class CplexMip : public MipSolver, public CplexBase {
   264   public:
   265     /// \e
   266     CplexMip();
   267     /// \e
   268     CplexMip(const CplexEnv&);
   269     /// \e
   270     CplexMip(const CplexMip&);
   271     /// \e
   272     virtual ~CplexMip();
   273 
   274     /// \e
   275     virtual CplexMip* cloneSolver() const;
   276     /// \e
   277     virtual CplexMip* newSolver() const;
   278 
   279   protected:
   280 
   281 
   282     virtual const char* _solverName() const;
   283 
   284     virtual ColTypes _getColType(int col) const;
   285     virtual void _setColType(int col, ColTypes col_type);
   286 
   287     virtual SolveExitStatus _solve();
   288     virtual ProblemType _getType() const;
   289     virtual Value _getSol(int i) const;
   290     virtual Value _getSolValue() const;
   291 
   292   };
   293 
   294 } //END OF NAMESPACE LEMON
   295 
   296 #endif //LEMON_CPLEX_H
   297