lemon/cplex.h
changeset 784 1a7fe3bef514
parent 576 745e182d0139
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/cplex.h	Thu Nov 05 15:50:01 2009 +0100
     1.3 @@ -0,0 +1,277 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2009
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_CPLEX_H
    1.23 +#define LEMON_CPLEX_H
    1.24 +
    1.25 +///\file
    1.26 +///\brief Header of the LEMON-CPLEX lp solver interface.
    1.27 +
    1.28 +#include <lemon/lp_base.h>
    1.29 +
    1.30 +struct cpxenv;
    1.31 +struct cpxlp;
    1.32 +
    1.33 +namespace lemon {
    1.34 +
    1.35 +  /// \brief Reference counted wrapper around cpxenv pointer
    1.36 +  ///
    1.37 +  /// The cplex uses environment object which is responsible for
    1.38 +  /// checking the proper license usage. This class provides a simple
    1.39 +  /// interface for share the environment object between different
    1.40 +  /// problems.
    1.41 +  class CplexEnv {
    1.42 +    friend class CplexBase;
    1.43 +  private:
    1.44 +    cpxenv* _env;
    1.45 +    mutable int* _cnt;
    1.46 +
    1.47 +  public:
    1.48 +
    1.49 +    /// \brief This exception is thrown when the license check is not
    1.50 +    /// sufficient
    1.51 +    class LicenseError : public Exception {
    1.52 +      friend class CplexEnv;
    1.53 +    private:
    1.54 +
    1.55 +      LicenseError(int status);
    1.56 +      char _message[510];
    1.57 +
    1.58 +    public:
    1.59 +
    1.60 +      /// The short error message
    1.61 +      virtual const char* what() const throw() {
    1.62 +        return _message;
    1.63 +      }
    1.64 +    };
    1.65 +
    1.66 +    /// Constructor
    1.67 +    CplexEnv();
    1.68 +    /// Shallow copy constructor
    1.69 +    CplexEnv(const CplexEnv&);
    1.70 +    /// Shallow assignement
    1.71 +    CplexEnv& operator=(const CplexEnv&);
    1.72 +    /// Destructor
    1.73 +    virtual ~CplexEnv();
    1.74 +
    1.75 +  protected:
    1.76 +
    1.77 +    cpxenv* cplexEnv() { return _env; }
    1.78 +    const cpxenv* cplexEnv() const { return _env; }
    1.79 +  };
    1.80 +
    1.81 +  /// \brief Base interface for the CPLEX LP and MIP solver
    1.82 +  ///
    1.83 +  /// This class implements the common interface of the CPLEX LP and
    1.84 +  /// MIP solvers.
    1.85 +  /// \ingroup lp_group
    1.86 +  class CplexBase : virtual public LpBase {
    1.87 +  protected:
    1.88 +
    1.89 +    CplexEnv _env;
    1.90 +    cpxlp* _prob;
    1.91 +
    1.92 +    CplexBase();
    1.93 +    CplexBase(const CplexEnv&);
    1.94 +    CplexBase(const CplexBase &);
    1.95 +    virtual ~CplexBase();
    1.96 +
    1.97 +    virtual int _addCol();
    1.98 +    virtual int _addRow();
    1.99 +    virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u);
   1.100 +
   1.101 +    virtual void _eraseCol(int i);
   1.102 +    virtual void _eraseRow(int i);
   1.103 +
   1.104 +    virtual void _eraseColId(int i);
   1.105 +    virtual void _eraseRowId(int i);
   1.106 +
   1.107 +    virtual void _getColName(int col, std::string& name) const;
   1.108 +    virtual void _setColName(int col, const std::string& name);
   1.109 +    virtual int _colByName(const std::string& name) const;
   1.110 +
   1.111 +    virtual void _getRowName(int row, std::string& name) const;
   1.112 +    virtual void _setRowName(int row, const std::string& name);
   1.113 +    virtual int _rowByName(const std::string& name) const;
   1.114 +
   1.115 +    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
   1.116 +    virtual void _getRowCoeffs(int i, InsertIterator b) const;
   1.117 +
   1.118 +    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
   1.119 +    virtual void _getColCoeffs(int i, InsertIterator b) const;
   1.120 +
   1.121 +    virtual void _setCoeff(int row, int col, Value value);
   1.122 +    virtual Value _getCoeff(int row, int col) const;
   1.123 +
   1.124 +    virtual void _setColLowerBound(int i, Value value);
   1.125 +    virtual Value _getColLowerBound(int i) const;
   1.126 +
   1.127 +    virtual void _setColUpperBound(int i, Value value);
   1.128 +    virtual Value _getColUpperBound(int i) const;
   1.129 +
   1.130 +  private:
   1.131 +    void _set_row_bounds(int i, Value lb, Value ub);
   1.132 +  protected:
   1.133 +
   1.134 +    virtual void _setRowLowerBound(int i, Value value);
   1.135 +    virtual Value _getRowLowerBound(int i) const;
   1.136 +
   1.137 +    virtual void _setRowUpperBound(int i, Value value);
   1.138 +    virtual Value _getRowUpperBound(int i) const;
   1.139 +
   1.140 +    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
   1.141 +    virtual void _getObjCoeffs(InsertIterator b) const;
   1.142 +
   1.143 +    virtual void _setObjCoeff(int i, Value obj_coef);
   1.144 +    virtual Value _getObjCoeff(int i) const;
   1.145 +
   1.146 +    virtual void _setSense(Sense sense);
   1.147 +    virtual Sense _getSense() const;
   1.148 +
   1.149 +    virtual void _clear();
   1.150 +
   1.151 +    virtual void _messageLevel(MessageLevel level);
   1.152 +    void _applyMessageLevel();
   1.153 +
   1.154 +    bool _message_enabled;
   1.155 +
   1.156 +  public:
   1.157 +
   1.158 +    /// Returns the used \c CplexEnv instance
   1.159 +    const CplexEnv& env() const { return _env; }
   1.160 +
   1.161 +    /// \brief Returns the const cpxenv pointer
   1.162 +    ///
   1.163 +    /// \note The cpxenv might be destructed with the solver.
   1.164 +    const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
   1.165 +
   1.166 +    /// \brief Returns the const cpxenv pointer
   1.167 +    ///
   1.168 +    /// \note The cpxenv might be destructed with the solver.
   1.169 +    cpxenv* cplexEnv() { return _env.cplexEnv(); }
   1.170 +
   1.171 +    /// Returns the cplex problem object
   1.172 +    cpxlp* cplexLp() { return _prob; }
   1.173 +    /// Returns the cplex problem object
   1.174 +    const cpxlp* cplexLp() const { return _prob; }
   1.175 +
   1.176 +  };
   1.177 +
   1.178 +  /// \brief Interface for the CPLEX LP solver
   1.179 +  ///
   1.180 +  /// This class implements an interface for the CPLEX LP solver.
   1.181 +  ///\ingroup lp_group
   1.182 +  class CplexLp : public LpSolver, public CplexBase {
   1.183 +  public:
   1.184 +    /// \e
   1.185 +    CplexLp();
   1.186 +    /// \e
   1.187 +    CplexLp(const CplexEnv&);
   1.188 +    /// \e
   1.189 +    CplexLp(const CplexLp&);
   1.190 +    /// \e
   1.191 +    virtual ~CplexLp();
   1.192 +
   1.193 +    /// \e
   1.194 +    virtual CplexLp* cloneSolver() const;
   1.195 +    /// \e
   1.196 +    virtual CplexLp* newSolver() const;
   1.197 +
   1.198 +  private:
   1.199 +
   1.200 +    // these values cannot retrieved element by element
   1.201 +    mutable std::vector<int> _col_status;
   1.202 +    mutable std::vector<int> _row_status;
   1.203 +
   1.204 +    mutable std::vector<Value> _primal_ray;
   1.205 +    mutable std::vector<Value> _dual_ray;
   1.206 +
   1.207 +    void _clear_temporals();
   1.208 +
   1.209 +    SolveExitStatus convertStatus(int status);
   1.210 +
   1.211 +  protected:
   1.212 +
   1.213 +    virtual const char* _solverName() const;
   1.214 +
   1.215 +    virtual SolveExitStatus _solve();
   1.216 +    virtual Value _getPrimal(int i) const;
   1.217 +    virtual Value _getDual(int i) const;
   1.218 +    virtual Value _getPrimalValue() const;
   1.219 +
   1.220 +    virtual VarStatus _getColStatus(int i) const;
   1.221 +    virtual VarStatus _getRowStatus(int i) const;
   1.222 +
   1.223 +    virtual Value _getPrimalRay(int i) const;
   1.224 +    virtual Value _getDualRay(int i) const;
   1.225 +
   1.226 +    virtual ProblemType _getPrimalType() const;
   1.227 +    virtual ProblemType _getDualType() const;
   1.228 +
   1.229 +  public:
   1.230 +
   1.231 +    /// Solve with primal simplex method
   1.232 +    SolveExitStatus solvePrimal();
   1.233 +
   1.234 +    /// Solve with dual simplex method
   1.235 +    SolveExitStatus solveDual();
   1.236 +
   1.237 +    /// Solve with barrier method
   1.238 +    SolveExitStatus solveBarrier();
   1.239 +
   1.240 +  };
   1.241 +
   1.242 +  /// \brief Interface for the CPLEX MIP solver
   1.243 +  ///
   1.244 +  /// This class implements an interface for the CPLEX MIP solver.
   1.245 +  ///\ingroup lp_group
   1.246 +  class CplexMip : public MipSolver, public CplexBase {
   1.247 +  public:
   1.248 +    /// \e
   1.249 +    CplexMip();
   1.250 +    /// \e
   1.251 +    CplexMip(const CplexEnv&);
   1.252 +    /// \e
   1.253 +    CplexMip(const CplexMip&);
   1.254 +    /// \e
   1.255 +    virtual ~CplexMip();
   1.256 +
   1.257 +    /// \e
   1.258 +    virtual CplexMip* cloneSolver() const;
   1.259 +    /// \e
   1.260 +    virtual CplexMip* newSolver() const;
   1.261 +
   1.262 +  protected:
   1.263 +
   1.264 +
   1.265 +    virtual const char* _solverName() const;
   1.266 +
   1.267 +    virtual ColTypes _getColType(int col) const;
   1.268 +    virtual void _setColType(int col, ColTypes col_type);
   1.269 +
   1.270 +    virtual SolveExitStatus _solve();
   1.271 +    virtual ProblemType _getType() const;
   1.272 +    virtual Value _getSol(int i) const;
   1.273 +    virtual Value _getSolValue() const;
   1.274 +
   1.275 +  };
   1.276 +
   1.277 +} //END OF NAMESPACE LEMON
   1.278 +
   1.279 +#endif //LEMON_CPLEX_H
   1.280 +