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