lemon/cplex.h
author Balazs Dezso <deba@inf.elte.hu>
Wed, 01 Apr 2009 22:58:58 +0200
changeset 567 3314f58e7b25
parent 540 9db62975c32b
child 576 745e182d0139
permissions -rw-r--r--
Add CBC support (#204)
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
alpar@461
   147
  public:
alpar@461
   148
alpar@461
   149
    /// Returns the used \c CplexEnv instance
alpar@461
   150
    const CplexEnv& env() const { return _env; }
alpar@461
   151
    ///
alpar@461
   152
    const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
alpar@461
   153
alpar@461
   154
    cpxlp* cplexLp() { return _prob; }
alpar@461
   155
    const cpxlp* cplexLp() const { return _prob; }
alpar@461
   156
alpar@461
   157
  };
alpar@461
   158
alpar@461
   159
  /// \brief Interface for the CPLEX LP solver
alpar@461
   160
  ///
alpar@461
   161
  /// This class implements an interface for the CPLEX LP solver.
alpar@461
   162
  ///\ingroup lp_group
alpar@540
   163
  class CplexLp : public LpSolver, public CplexBase {
alpar@461
   164
  public:
alpar@461
   165
    /// \e
alpar@462
   166
    CplexLp();
alpar@461
   167
    /// \e
alpar@462
   168
    CplexLp(const CplexEnv&);
alpar@461
   169
    /// \e
alpar@462
   170
    CplexLp(const CplexLp&);
alpar@461
   171
    /// \e
alpar@462
   172
    virtual ~CplexLp();
alpar@461
   173
alpar@540
   174
    /// \e
alpar@540
   175
    virtual CplexLp* cloneSolver() const;
alpar@540
   176
    /// \e
alpar@540
   177
    virtual CplexLp* newSolver() const;
alpar@540
   178
alpar@461
   179
  private:
alpar@461
   180
alpar@461
   181
    // these values cannot retrieved element by element
alpar@461
   182
    mutable std::vector<int> _col_status;
alpar@461
   183
    mutable std::vector<int> _row_status;
alpar@461
   184
alpar@461
   185
    mutable std::vector<Value> _primal_ray;
alpar@461
   186
    mutable std::vector<Value> _dual_ray;
alpar@461
   187
alpar@461
   188
    void _clear_temporals();
alpar@461
   189
alpar@461
   190
    SolveExitStatus convertStatus(int status);
alpar@461
   191
alpar@461
   192
  protected:
alpar@461
   193
alpar@461
   194
    virtual const char* _solverName() const;
alpar@461
   195
alpar@461
   196
    virtual SolveExitStatus _solve();
alpar@461
   197
    virtual Value _getPrimal(int i) const;
alpar@461
   198
    virtual Value _getDual(int i) const;
alpar@461
   199
    virtual Value _getPrimalValue() const;
alpar@461
   200
alpar@461
   201
    virtual VarStatus _getColStatus(int i) const;
alpar@461
   202
    virtual VarStatus _getRowStatus(int i) const;
alpar@461
   203
alpar@461
   204
    virtual Value _getPrimalRay(int i) const;
alpar@461
   205
    virtual Value _getDualRay(int i) const;
alpar@461
   206
alpar@461
   207
    virtual ProblemType _getPrimalType() const;
alpar@461
   208
    virtual ProblemType _getDualType() const;
alpar@461
   209
alpar@461
   210
  public:
alpar@461
   211
alpar@461
   212
    /// Solve with primal simplex method
alpar@461
   213
    SolveExitStatus solvePrimal();
alpar@461
   214
alpar@461
   215
    /// Solve with dual simplex method
alpar@461
   216
    SolveExitStatus solveDual();
alpar@461
   217
alpar@461
   218
    /// Solve with barrier method
alpar@461
   219
    SolveExitStatus solveBarrier();
alpar@461
   220
alpar@461
   221
  };
alpar@461
   222
alpar@461
   223
  /// \brief Interface for the CPLEX MIP solver
alpar@461
   224
  ///
alpar@461
   225
  /// This class implements an interface for the CPLEX MIP solver.
alpar@461
   226
  ///\ingroup lp_group
alpar@540
   227
  class CplexMip : public MipSolver, public CplexBase {
alpar@461
   228
  public:
alpar@461
   229
    /// \e
alpar@462
   230
    CplexMip();
alpar@461
   231
    /// \e
alpar@462
   232
    CplexMip(const CplexEnv&);
alpar@461
   233
    /// \e
alpar@462
   234
    CplexMip(const CplexMip&);
alpar@461
   235
    /// \e
alpar@462
   236
    virtual ~CplexMip();
alpar@461
   237
deba@551
   238
    /// \e
deba@551
   239
    virtual CplexMip* cloneSolver() const;
deba@551
   240
    /// \e
deba@551
   241
    virtual CplexMip* newSolver() const;
deba@551
   242
alpar@461
   243
  protected:
alpar@461
   244
alpar@461
   245
alpar@461
   246
    virtual const char* _solverName() const;
alpar@461
   247
alpar@461
   248
    virtual ColTypes _getColType(int col) const;
alpar@461
   249
    virtual void _setColType(int col, ColTypes col_type);
alpar@461
   250
alpar@461
   251
    virtual SolveExitStatus _solve();
alpar@461
   252
    virtual ProblemType _getType() const;
alpar@461
   253
    virtual Value _getSol(int i) const;
alpar@461
   254
    virtual Value _getSolValue() const;
alpar@461
   255
alpar@461
   256
  };
alpar@461
   257
alpar@461
   258
} //END OF NAMESPACE LEMON
alpar@461
   259
alpar@461
   260
#endif //LEMON_CPLEX_H
alpar@461
   261