lemon/cplex.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 05 Nov 2009 10:01:02 +0100
changeset 779 c160bf9f18ef
parent 551 9d0d7e20f76d
child 746 e4554cd6b2bf
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
 *
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();
alpar@461
    96
alpar@461
    97
    virtual void _eraseCol(int i);
alpar@461
    98
    virtual void _eraseRow(int i);
alpar@461
    99
alpar@461
   100
    virtual void _eraseColId(int i);
alpar@461
   101
    virtual void _eraseRowId(int i);
alpar@461
   102
alpar@461
   103
    virtual void _getColName(int col, std::string& name) const;
alpar@461
   104
    virtual void _setColName(int col, const std::string& name);
alpar@461
   105
    virtual int _colByName(const std::string& name) const;
alpar@461
   106
alpar@461
   107
    virtual void _getRowName(int row, std::string& name) const;
alpar@461
   108
    virtual void _setRowName(int row, const std::string& name);
alpar@461
   109
    virtual int _rowByName(const std::string& name) const;
alpar@461
   110
alpar@461
   111
    virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@461
   112
    virtual void _getRowCoeffs(int i, InsertIterator b) const;
alpar@461
   113
alpar@461
   114
    virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e);
alpar@461
   115
    virtual void _getColCoeffs(int i, InsertIterator b) const;
alpar@461
   116
alpar@461
   117
    virtual void _setCoeff(int row, int col, Value value);
alpar@461
   118
    virtual Value _getCoeff(int row, int col) const;
alpar@461
   119
alpar@461
   120
    virtual void _setColLowerBound(int i, Value value);
alpar@461
   121
    virtual Value _getColLowerBound(int i) const;
alpar@461
   122
alpar@461
   123
    virtual void _setColUpperBound(int i, Value value);
alpar@461
   124
    virtual Value _getColUpperBound(int i) const;
alpar@461
   125
alpar@461
   126
  private:
alpar@461
   127
    void _set_row_bounds(int i, Value lb, Value ub);
alpar@461
   128
  protected:
alpar@461
   129
alpar@461
   130
    virtual void _setRowLowerBound(int i, Value value);
alpar@461
   131
    virtual Value _getRowLowerBound(int i) const;
alpar@461
   132
alpar@461
   133
    virtual void _setRowUpperBound(int i, Value value);
alpar@461
   134
    virtual Value _getRowUpperBound(int i) const;
alpar@461
   135
alpar@461
   136
    virtual void _setObjCoeffs(ExprIterator b, ExprIterator e);
alpar@461
   137
    virtual void _getObjCoeffs(InsertIterator b) const;
alpar@461
   138
alpar@461
   139
    virtual void _setObjCoeff(int i, Value obj_coef);
alpar@461
   140
    virtual Value _getObjCoeff(int i) const;
alpar@461
   141
alpar@461
   142
    virtual void _setSense(Sense sense);
alpar@461
   143
    virtual Sense _getSense() const;
alpar@461
   144
alpar@461
   145
    virtual void _clear();
alpar@461
   146
deba@576
   147
    virtual void _messageLevel(MessageLevel level);
deba@576
   148
    void _applyMessageLevel();
deba@576
   149
deba@576
   150
    bool _message_enabled;
deba@576
   151
alpar@461
   152
  public:
alpar@461
   153
alpar@461
   154
    /// Returns the used \c CplexEnv instance
alpar@461
   155
    const CplexEnv& env() const { return _env; }
deba@576
   156
deba@576
   157
    /// \brief Returns the const cpxenv pointer
alpar@461
   158
    ///
deba@576
   159
    /// \note The cpxenv might be destructed with the solver.
alpar@461
   160
    const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
alpar@461
   161
deba@576
   162
    /// \brief Returns the const cpxenv pointer
deba@576
   163
    ///
deba@576
   164
    /// \note The cpxenv might be destructed with the solver.
deba@576
   165
    cpxenv* cplexEnv() { return _env.cplexEnv(); }
deba@576
   166
deba@576
   167
    /// Returns the cplex problem object
alpar@461
   168
    cpxlp* cplexLp() { return _prob; }
deba@576
   169
    /// Returns the cplex problem object
alpar@461
   170
    const cpxlp* cplexLp() const { return _prob; }
alpar@461
   171
alpar@461
   172
  };
alpar@461
   173
alpar@461
   174
  /// \brief Interface for the CPLEX LP solver
alpar@461
   175
  ///
alpar@461
   176
  /// This class implements an interface for the CPLEX LP solver.
alpar@461
   177
  ///\ingroup lp_group
alpar@540
   178
  class CplexLp : public LpSolver, public CplexBase {
alpar@461
   179
  public:
alpar@461
   180
    /// \e
alpar@462
   181
    CplexLp();
alpar@461
   182
    /// \e
alpar@462
   183
    CplexLp(const CplexEnv&);
alpar@461
   184
    /// \e
alpar@462
   185
    CplexLp(const CplexLp&);
alpar@461
   186
    /// \e
alpar@462
   187
    virtual ~CplexLp();
alpar@461
   188
alpar@540
   189
    /// \e
alpar@540
   190
    virtual CplexLp* cloneSolver() const;
alpar@540
   191
    /// \e
alpar@540
   192
    virtual CplexLp* newSolver() const;
alpar@540
   193
alpar@461
   194
  private:
alpar@461
   195
alpar@461
   196
    // these values cannot retrieved element by element
alpar@461
   197
    mutable std::vector<int> _col_status;
alpar@461
   198
    mutable std::vector<int> _row_status;
alpar@461
   199
alpar@461
   200
    mutable std::vector<Value> _primal_ray;
alpar@461
   201
    mutable std::vector<Value> _dual_ray;
alpar@461
   202
alpar@461
   203
    void _clear_temporals();
alpar@461
   204
alpar@461
   205
    SolveExitStatus convertStatus(int status);
alpar@461
   206
alpar@461
   207
  protected:
alpar@461
   208
alpar@461
   209
    virtual const char* _solverName() const;
alpar@461
   210
alpar@461
   211
    virtual SolveExitStatus _solve();
alpar@461
   212
    virtual Value _getPrimal(int i) const;
alpar@461
   213
    virtual Value _getDual(int i) const;
alpar@461
   214
    virtual Value _getPrimalValue() const;
alpar@461
   215
alpar@461
   216
    virtual VarStatus _getColStatus(int i) const;
alpar@461
   217
    virtual VarStatus _getRowStatus(int i) const;
alpar@461
   218
alpar@461
   219
    virtual Value _getPrimalRay(int i) const;
alpar@461
   220
    virtual Value _getDualRay(int i) const;
alpar@461
   221
alpar@461
   222
    virtual ProblemType _getPrimalType() const;
alpar@461
   223
    virtual ProblemType _getDualType() const;
alpar@461
   224
alpar@461
   225
  public:
alpar@461
   226
alpar@461
   227
    /// Solve with primal simplex method
alpar@461
   228
    SolveExitStatus solvePrimal();
alpar@461
   229
alpar@461
   230
    /// Solve with dual simplex method
alpar@461
   231
    SolveExitStatus solveDual();
alpar@461
   232
alpar@461
   233
    /// Solve with barrier method
alpar@461
   234
    SolveExitStatus solveBarrier();
alpar@461
   235
alpar@461
   236
  };
alpar@461
   237
alpar@461
   238
  /// \brief Interface for the CPLEX MIP solver
alpar@461
   239
  ///
alpar@461
   240
  /// This class implements an interface for the CPLEX MIP solver.
alpar@461
   241
  ///\ingroup lp_group
alpar@540
   242
  class CplexMip : public MipSolver, public CplexBase {
alpar@461
   243
  public:
alpar@461
   244
    /// \e
alpar@462
   245
    CplexMip();
alpar@461
   246
    /// \e
alpar@462
   247
    CplexMip(const CplexEnv&);
alpar@461
   248
    /// \e
alpar@462
   249
    CplexMip(const CplexMip&);
alpar@461
   250
    /// \e
alpar@462
   251
    virtual ~CplexMip();
alpar@461
   252
deba@551
   253
    /// \e
deba@551
   254
    virtual CplexMip* cloneSolver() const;
deba@551
   255
    /// \e
deba@551
   256
    virtual CplexMip* newSolver() const;
deba@551
   257
alpar@461
   258
  protected:
alpar@461
   259
alpar@461
   260
alpar@461
   261
    virtual const char* _solverName() const;
alpar@461
   262
alpar@461
   263
    virtual ColTypes _getColType(int col) const;
alpar@461
   264
    virtual void _setColType(int col, ColTypes col_type);
alpar@461
   265
alpar@461
   266
    virtual SolveExitStatus _solve();
alpar@461
   267
    virtual ProblemType _getType() const;
alpar@461
   268
    virtual Value _getSol(int i) const;
alpar@461
   269
    virtual Value _getSolValue() const;
alpar@461
   270
alpar@461
   271
  };
alpar@461
   272
alpar@461
   273
} //END OF NAMESPACE LEMON
alpar@461
   274
alpar@461
   275
#endif //LEMON_CPLEX_H
alpar@461
   276