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