lemon/cplex.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 12 Sep 2012 17:29:50 +0200
branch1.2
changeset 1154 2da3de1c0ebe
parent 956 141f9c0db4a3
parent 1140 8d281761dea4
child 1183 2c48ba00fccd
permissions -rw-r--r--
Merge bugfix #448 to branch 1.2
alpar@484
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@484
     2
 *
alpar@484
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@484
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
alpar@484
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@484
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@484
     8
 *
alpar@484
     9
 * Permission to use, modify and distribute this software is granted
alpar@484
    10
 * provided that this copyright notice appears in all copies. For
alpar@484
    11
 * precise terms see the accompanying LICENSE file.
alpar@484
    12
 *
alpar@484
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@484
    14
 * express or implied, and with no claim as to its suitability for any
alpar@484
    15
 * purpose.
alpar@484
    16
 *
alpar@484
    17
 */
alpar@484
    18
alpar@484
    19
#include <iostream>
alpar@484
    20
#include <vector>
alpar@484
    21
#include <cstring>
alpar@484
    22
alpar@484
    23
#include <lemon/cplex.h>
alpar@484
    24
alpar@484
    25
extern "C" {
alpar@484
    26
#include <ilcplex/cplex.h>
alpar@484
    27
}
alpar@484
    28
alpar@484
    29
alpar@484
    30
///\file
alpar@484
    31
///\brief Implementation of the LEMON-CPLEX lp solver interface.
alpar@484
    32
namespace lemon {
alpar@484
    33
alpar@484
    34
  CplexEnv::LicenseError::LicenseError(int status) {
alpar@484
    35
    if (!CPXgeterrorstring(0, status, _message)) {
alpar@484
    36
      std::strcpy(_message, "Cplex unknown error");
alpar@484
    37
    }
alpar@484
    38
  }
alpar@484
    39
alpar@484
    40
  CplexEnv::CplexEnv() {
alpar@484
    41
    int status;
alpar@484
    42
    _cnt = new int;
alpar@484
    43
    _env = CPXopenCPLEX(&status);
alpar@484
    44
    if (_env == 0) {
alpar@484
    45
      delete _cnt;
alpar@484
    46
      _cnt = 0;
alpar@484
    47
      throw LicenseError(status);
alpar@484
    48
    }
alpar@484
    49
  }
alpar@484
    50
alpar@484
    51
  CplexEnv::CplexEnv(const CplexEnv& other) {
alpar@484
    52
    _env = other._env;
alpar@484
    53
    _cnt = other._cnt;
alpar@484
    54
    ++(*_cnt);
alpar@484
    55
  }
alpar@484
    56
alpar@484
    57
  CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
alpar@484
    58
    _env = other._env;
alpar@484
    59
    _cnt = other._cnt;
alpar@484
    60
    ++(*_cnt);
alpar@484
    61
    return *this;
alpar@484
    62
  }
alpar@484
    63
alpar@484
    64
  CplexEnv::~CplexEnv() {
alpar@484
    65
    --(*_cnt);
alpar@484
    66
    if (*_cnt == 0) {
alpar@484
    67
      delete _cnt;
alpar@484
    68
      CPXcloseCPLEX(&_env);
alpar@484
    69
    }
alpar@484
    70
  }
alpar@484
    71
alpar@484
    72
  CplexBase::CplexBase() : LpBase() {
alpar@484
    73
    int status;
alpar@484
    74
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
deba@623
    75
    messageLevel(MESSAGE_NOTHING);
alpar@484
    76
  }
alpar@484
    77
alpar@484
    78
  CplexBase::CplexBase(const CplexEnv& env)
alpar@484
    79
    : LpBase(), _env(env) {
alpar@484
    80
    int status;
alpar@484
    81
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
deba@623
    82
    messageLevel(MESSAGE_NOTHING);
alpar@484
    83
  }
alpar@484
    84
alpar@484
    85
  CplexBase::CplexBase(const CplexBase& cplex)
alpar@484
    86
    : LpBase() {
alpar@484
    87
    int status;
alpar@484
    88
    _prob = CPXcloneprob(cplexEnv(), cplex._prob, &status);
alpar@484
    89
    rows = cplex.rows;
alpar@484
    90
    cols = cplex.cols;
deba@623
    91
    messageLevel(MESSAGE_NOTHING);
alpar@484
    92
  }
alpar@484
    93
alpar@484
    94
  CplexBase::~CplexBase() {
alpar@484
    95
    CPXfreeprob(cplexEnv(),&_prob);
alpar@484
    96
  }
alpar@484
    97
alpar@484
    98
  int CplexBase::_addCol() {
alpar@484
    99
    int i = CPXgetnumcols(cplexEnv(), _prob);
alpar@484
   100
    double lb = -INF, ub = INF;
alpar@484
   101
    CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0);
alpar@484
   102
    return i;
alpar@484
   103
  }
alpar@484
   104
alpar@484
   105
alpar@484
   106
  int CplexBase::_addRow() {
alpar@484
   107
    int i = CPXgetnumrows(cplexEnv(), _prob);
alpar@484
   108
    const double ub = INF;
alpar@484
   109
    const char s = 'L';
alpar@484
   110
    CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
alpar@484
   111
    return i;
alpar@484
   112
  }
alpar@484
   113
alpar@956
   114
  int CplexBase::_addRow(Value lb, ExprIterator b,
deba@793
   115
                         ExprIterator e, Value ub) {
deba@793
   116
    int i = CPXgetnumrows(cplexEnv(), _prob);
deba@793
   117
    if (lb == -INF) {
deba@793
   118
      const char s = 'L';
deba@793
   119
      CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
deba@793
   120
    } else if (ub == INF) {
deba@793
   121
      const char s = 'G';
deba@793
   122
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
deba@793
   123
    } else if (lb == ub){
deba@793
   124
      const char s = 'E';
deba@793
   125
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
deba@793
   126
    } else {
deba@793
   127
      const char s = 'R';
deba@793
   128
      double len = ub - lb;
deba@793
   129
      CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0);
deba@793
   130
    }
deba@793
   131
deba@793
   132
    std::vector<int> indices;
deba@793
   133
    std::vector<int> rowlist;
deba@793
   134
    std::vector<Value> values;
deba@793
   135
deba@793
   136
    for(ExprIterator it=b; it!=e; ++it) {
deba@793
   137
      indices.push_back(it->first);
deba@793
   138
      values.push_back(it->second);
deba@793
   139
      rowlist.push_back(i);
deba@793
   140
    }
deba@793
   141
deba@793
   142
    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
deba@793
   143
                   &rowlist.front(), &indices.front(), &values.front());
deba@793
   144
deba@793
   145
    return i;
deba@793
   146
  }
alpar@484
   147
alpar@484
   148
  void CplexBase::_eraseCol(int i) {
alpar@484
   149
    CPXdelcols(cplexEnv(), _prob, i, i);
alpar@484
   150
  }
alpar@484
   151
alpar@484
   152
  void CplexBase::_eraseRow(int i) {
alpar@484
   153
    CPXdelrows(cplexEnv(), _prob, i, i);
alpar@484
   154
  }
alpar@484
   155
alpar@484
   156
  void CplexBase::_eraseColId(int i) {
alpar@484
   157
    cols.eraseIndex(i);
alpar@484
   158
    cols.shiftIndices(i);
alpar@484
   159
  }
alpar@484
   160
  void CplexBase::_eraseRowId(int i) {
alpar@484
   161
    rows.eraseIndex(i);
alpar@484
   162
    rows.shiftIndices(i);
alpar@484
   163
  }
alpar@484
   164
alpar@484
   165
  void CplexBase::_getColName(int col, std::string &name) const {
alpar@484
   166
    int size;
alpar@484
   167
    CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col);
alpar@484
   168
    if (size == 0) {
alpar@484
   169
      name.clear();
alpar@484
   170
      return;
alpar@484
   171
    }
alpar@484
   172
alpar@484
   173
    size *= -1;
alpar@484
   174
    std::vector<char> buf(size);
alpar@484
   175
    char *cname;
alpar@484
   176
    int tmp;
alpar@484
   177
    CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size,
alpar@484
   178
                  &tmp, col, col);
alpar@484
   179
    name = cname;
alpar@484
   180
  }
alpar@484
   181
alpar@484
   182
  void CplexBase::_setColName(int col, const std::string &name) {
alpar@484
   183
    char *cname;
alpar@484
   184
    cname = const_cast<char*>(name.c_str());
alpar@484
   185
    CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname);
alpar@484
   186
  }
alpar@484
   187
alpar@484
   188
  int CplexBase::_colByName(const std::string& name) const {
alpar@484
   189
    int index;
alpar@484
   190
    if (CPXgetcolindex(cplexEnv(), _prob,
alpar@484
   191
                       const_cast<char*>(name.c_str()), &index) == 0) {
alpar@484
   192
      return index;
alpar@484
   193
    }
alpar@484
   194
    return -1;
alpar@484
   195
  }
alpar@484
   196
alpar@484
   197
  void CplexBase::_getRowName(int row, std::string &name) const {
alpar@484
   198
    int size;
alpar@484
   199
    CPXgetrowname(cplexEnv(), _prob, 0, 0, 0, &size, row, row);
alpar@484
   200
    if (size == 0) {
alpar@484
   201
      name.clear();
alpar@484
   202
      return;
alpar@484
   203
    }
alpar@484
   204
alpar@484
   205
    size *= -1;
alpar@484
   206
    std::vector<char> buf(size);
alpar@484
   207
    char *cname;
alpar@484
   208
    int tmp;
alpar@484
   209
    CPXgetrowname(cplexEnv(), _prob, &cname, &buf.front(), size,
alpar@484
   210
                  &tmp, row, row);
alpar@484
   211
    name = cname;
alpar@484
   212
  }
alpar@484
   213
alpar@484
   214
  void CplexBase::_setRowName(int row, const std::string &name) {
alpar@484
   215
    char *cname;
alpar@484
   216
    cname = const_cast<char*>(name.c_str());
alpar@484
   217
    CPXchgrowname(cplexEnv(), _prob, 1, &row, &cname);
alpar@484
   218
  }
alpar@484
   219
alpar@484
   220
  int CplexBase::_rowByName(const std::string& name) const {
alpar@484
   221
    int index;
alpar@484
   222
    if (CPXgetrowindex(cplexEnv(), _prob,
alpar@484
   223
                       const_cast<char*>(name.c_str()), &index) == 0) {
alpar@484
   224
      return index;
alpar@484
   225
    }
alpar@484
   226
    return -1;
alpar@484
   227
  }
alpar@484
   228
alpar@484
   229
  void CplexBase::_setRowCoeffs(int i, ExprIterator b,
alpar@484
   230
                                      ExprIterator e)
alpar@484
   231
  {
alpar@484
   232
    std::vector<int> indices;
alpar@484
   233
    std::vector<int> rowlist;
alpar@484
   234
    std::vector<Value> values;
alpar@484
   235
alpar@484
   236
    for(ExprIterator it=b; it!=e; ++it) {
alpar@484
   237
      indices.push_back(it->first);
alpar@484
   238
      values.push_back(it->second);
alpar@484
   239
      rowlist.push_back(i);
alpar@484
   240
    }
alpar@484
   241
alpar@484
   242
    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
alpar@484
   243
                   &rowlist.front(), &indices.front(), &values.front());
alpar@484
   244
  }
alpar@484
   245
alpar@484
   246
  void CplexBase::_getRowCoeffs(int i, InsertIterator b) const {
alpar@484
   247
    int tmp1, tmp2, tmp3, length;
alpar@484
   248
    CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
alpar@484
   249
alpar@484
   250
    length = -length;
alpar@484
   251
    std::vector<int> indices(length);
alpar@484
   252
    std::vector<double> values(length);
alpar@484
   253
alpar@484
   254
    CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2,
alpar@484
   255
               &indices.front(), &values.front(),
alpar@484
   256
               length, &tmp3, i, i);
alpar@484
   257
alpar@484
   258
    for (int i = 0; i < length; ++i) {
alpar@484
   259
      *b = std::make_pair(indices[i], values[i]);
alpar@484
   260
      ++b;
alpar@484
   261
    }
alpar@484
   262
  }
alpar@484
   263
alpar@484
   264
  void CplexBase::_setColCoeffs(int i, ExprIterator b, ExprIterator e) {
alpar@484
   265
    std::vector<int> indices;
alpar@484
   266
    std::vector<int> collist;
alpar@484
   267
    std::vector<Value> values;
alpar@484
   268
alpar@484
   269
    for(ExprIterator it=b; it!=e; ++it) {
alpar@484
   270
      indices.push_back(it->first);
alpar@484
   271
      values.push_back(it->second);
alpar@484
   272
      collist.push_back(i);
alpar@484
   273
    }
alpar@484
   274
alpar@484
   275
    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
alpar@484
   276
                   &indices.front(), &collist.front(), &values.front());
alpar@484
   277
  }
alpar@484
   278
alpar@484
   279
  void CplexBase::_getColCoeffs(int i, InsertIterator b) const {
alpar@484
   280
alpar@484
   281
    int tmp1, tmp2, tmp3, length;
alpar@484
   282
    CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
alpar@484
   283
alpar@484
   284
    length = -length;
alpar@484
   285
    std::vector<int> indices(length);
alpar@484
   286
    std::vector<double> values(length);
alpar@484
   287
alpar@484
   288
    CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2,
alpar@484
   289
               &indices.front(), &values.front(),
alpar@484
   290
               length, &tmp3, i, i);
alpar@484
   291
alpar@484
   292
    for (int i = 0; i < length; ++i) {
alpar@484
   293
      *b = std::make_pair(indices[i], values[i]);
alpar@484
   294
      ++b;
alpar@484
   295
    }
alpar@484
   296
alpar@484
   297
  }
alpar@484
   298
alpar@484
   299
  void CplexBase::_setCoeff(int row, int col, Value value) {
alpar@484
   300
    CPXchgcoef(cplexEnv(), _prob, row, col, value);
alpar@484
   301
  }
alpar@484
   302
alpar@484
   303
  CplexBase::Value CplexBase::_getCoeff(int row, int col) const {
alpar@484
   304
    CplexBase::Value value;
alpar@484
   305
    CPXgetcoef(cplexEnv(), _prob, row, col, &value);
alpar@484
   306
    return value;
alpar@484
   307
  }
alpar@484
   308
alpar@484
   309
  void CplexBase::_setColLowerBound(int i, Value value) {
alpar@484
   310
    const char s = 'L';
alpar@484
   311
    CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
alpar@484
   312
  }
alpar@484
   313
alpar@484
   314
  CplexBase::Value CplexBase::_getColLowerBound(int i) const {
alpar@484
   315
    CplexBase::Value res;
alpar@484
   316
    CPXgetlb(cplexEnv(), _prob, &res, i, i);
alpar@484
   317
    return res <= -CPX_INFBOUND ? -INF : res;
alpar@484
   318
  }
alpar@484
   319
alpar@484
   320
  void CplexBase::_setColUpperBound(int i, Value value)
alpar@484
   321
  {
alpar@484
   322
    const char s = 'U';
alpar@484
   323
    CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
alpar@484
   324
  }
alpar@484
   325
alpar@484
   326
  CplexBase::Value CplexBase::_getColUpperBound(int i) const {
alpar@484
   327
    CplexBase::Value res;
alpar@484
   328
    CPXgetub(cplexEnv(), _prob, &res, i, i);
alpar@484
   329
    return res >= CPX_INFBOUND ? INF : res;
alpar@484
   330
  }
alpar@484
   331
alpar@484
   332
  CplexBase::Value CplexBase::_getRowLowerBound(int i) const {
alpar@484
   333
    char s;
alpar@484
   334
    CPXgetsense(cplexEnv(), _prob, &s, i, i);
alpar@484
   335
    CplexBase::Value res;
alpar@484
   336
alpar@484
   337
    switch (s) {
alpar@484
   338
    case 'G':
alpar@484
   339
    case 'R':
alpar@484
   340
    case 'E':
alpar@484
   341
      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
alpar@484
   342
      return res <= -CPX_INFBOUND ? -INF : res;
alpar@484
   343
    default:
alpar@484
   344
      return -INF;
alpar@484
   345
    }
alpar@484
   346
  }
alpar@484
   347
alpar@484
   348
  CplexBase::Value CplexBase::_getRowUpperBound(int i) const {
alpar@484
   349
    char s;
alpar@484
   350
    CPXgetsense(cplexEnv(), _prob, &s, i, i);
alpar@484
   351
    CplexBase::Value res;
alpar@484
   352
alpar@484
   353
    switch (s) {
alpar@484
   354
    case 'L':
alpar@484
   355
    case 'E':
alpar@484
   356
      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
alpar@484
   357
      return res >= CPX_INFBOUND ? INF : res;
alpar@484
   358
    case 'R':
alpar@484
   359
      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
alpar@484
   360
      {
alpar@484
   361
        double rng;
alpar@484
   362
        CPXgetrngval(cplexEnv(), _prob, &rng, i, i);
alpar@484
   363
        res += rng;
alpar@484
   364
      }
alpar@484
   365
      return res >= CPX_INFBOUND ? INF : res;
alpar@484
   366
    default:
alpar@484
   367
      return INF;
alpar@484
   368
    }
alpar@484
   369
  }
alpar@484
   370
alpar@484
   371
  //This is easier to implement
alpar@484
   372
  void CplexBase::_set_row_bounds(int i, Value lb, Value ub) {
alpar@484
   373
    if (lb == -INF) {
alpar@484
   374
      const char s = 'L';
alpar@484
   375
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
alpar@484
   376
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &ub);
alpar@484
   377
    } else if (ub == INF) {
alpar@484
   378
      const char s = 'G';
alpar@484
   379
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
alpar@484
   380
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
alpar@484
   381
    } else if (lb == ub){
alpar@484
   382
      const char s = 'E';
alpar@484
   383
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
alpar@484
   384
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
alpar@484
   385
    } else {
alpar@484
   386
      const char s = 'R';
alpar@484
   387
      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
alpar@484
   388
      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
alpar@484
   389
      double len = ub - lb;
alpar@484
   390
      CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
alpar@484
   391
    }
alpar@484
   392
  }
alpar@484
   393
alpar@484
   394
  void CplexBase::_setRowLowerBound(int i, Value lb)
alpar@484
   395
  {
alpar@484
   396
    LEMON_ASSERT(lb != INF, "Invalid bound");
alpar@484
   397
    _set_row_bounds(i, lb, CplexBase::_getRowUpperBound(i));
alpar@484
   398
  }
alpar@484
   399
alpar@484
   400
  void CplexBase::_setRowUpperBound(int i, Value ub)
alpar@484
   401
  {
alpar@484
   402
alpar@484
   403
    LEMON_ASSERT(ub != -INF, "Invalid bound");
alpar@484
   404
    _set_row_bounds(i, CplexBase::_getRowLowerBound(i), ub);
alpar@484
   405
  }
alpar@484
   406
alpar@484
   407
  void CplexBase::_setObjCoeffs(ExprIterator b, ExprIterator e)
alpar@484
   408
  {
alpar@484
   409
    std::vector<int> indices;
alpar@484
   410
    std::vector<Value> values;
alpar@484
   411
    for(ExprIterator it=b; it!=e; ++it) {
alpar@484
   412
      indices.push_back(it->first);
alpar@484
   413
      values.push_back(it->second);
alpar@484
   414
    }
alpar@484
   415
    CPXchgobj(cplexEnv(), _prob, values.size(),
alpar@484
   416
              &indices.front(), &values.front());
alpar@484
   417
alpar@484
   418
  }
alpar@484
   419
alpar@484
   420
  void CplexBase::_getObjCoeffs(InsertIterator b) const
alpar@484
   421
  {
alpar@484
   422
    int num = CPXgetnumcols(cplexEnv(), _prob);
alpar@484
   423
    std::vector<Value> x(num);
alpar@484
   424
alpar@484
   425
    CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1);
alpar@484
   426
    for (int i = 0; i < num; ++i) {
alpar@484
   427
      if (x[i] != 0.0) {
alpar@484
   428
        *b = std::make_pair(i, x[i]);
alpar@484
   429
        ++b;
alpar@484
   430
      }
alpar@484
   431
    }
alpar@484
   432
  }
alpar@484
   433
alpar@484
   434
  void CplexBase::_setObjCoeff(int i, Value obj_coef)
alpar@484
   435
  {
alpar@484
   436
    CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef);
alpar@484
   437
  }
alpar@484
   438
alpar@484
   439
  CplexBase::Value CplexBase::_getObjCoeff(int i) const
alpar@484
   440
  {
alpar@484
   441
    Value x;
alpar@484
   442
    CPXgetobj(cplexEnv(), _prob, &x, i, i);
alpar@484
   443
    return x;
alpar@484
   444
  }
alpar@484
   445
alpar@484
   446
  void CplexBase::_setSense(CplexBase::Sense sense) {
alpar@484
   447
    switch (sense) {
alpar@484
   448
    case MIN:
alpar@484
   449
      CPXchgobjsen(cplexEnv(), _prob, CPX_MIN);
alpar@484
   450
      break;
alpar@484
   451
    case MAX:
alpar@484
   452
      CPXchgobjsen(cplexEnv(), _prob, CPX_MAX);
alpar@484
   453
      break;
alpar@484
   454
    }
alpar@484
   455
  }
alpar@484
   456
alpar@484
   457
  CplexBase::Sense CplexBase::_getSense() const {
alpar@484
   458
    switch (CPXgetobjsen(cplexEnv(), _prob)) {
alpar@484
   459
    case CPX_MIN:
alpar@484
   460
      return MIN;
alpar@484
   461
    case CPX_MAX:
alpar@484
   462
      return MAX;
alpar@484
   463
    default:
alpar@484
   464
      LEMON_ASSERT(false, "Invalid sense");
alpar@484
   465
      return CplexBase::Sense();
alpar@484
   466
    }
alpar@484
   467
  }
alpar@484
   468
alpar@484
   469
  void CplexBase::_clear() {
alpar@484
   470
    CPXfreeprob(cplexEnv(),&_prob);
alpar@484
   471
    int status;
alpar@484
   472
    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
alpar@484
   473
  }
alpar@484
   474
deba@623
   475
  void CplexBase::_messageLevel(MessageLevel level) {
deba@623
   476
    switch (level) {
deba@623
   477
    case MESSAGE_NOTHING:
deba@623
   478
      _message_enabled = false;
deba@623
   479
      break;
deba@623
   480
    case MESSAGE_ERROR:
deba@623
   481
    case MESSAGE_WARNING:
deba@623
   482
    case MESSAGE_NORMAL:
deba@623
   483
    case MESSAGE_VERBOSE:
deba@623
   484
      _message_enabled = true;
deba@623
   485
      break;
deba@623
   486
    }
deba@623
   487
  }
deba@623
   488
deba@623
   489
  void CplexBase::_applyMessageLevel() {
alpar@956
   490
    CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND,
deba@623
   491
                   _message_enabled ? CPX_ON : CPX_OFF);
deba@623
   492
  }
deba@623
   493
alpar@485
   494
  // CplexLp members
alpar@484
   495
alpar@485
   496
  CplexLp::CplexLp()
deba@598
   497
    : LpBase(), LpSolver(), CplexBase() {}
alpar@484
   498
alpar@485
   499
  CplexLp::CplexLp(const CplexEnv& env)
deba@598
   500
    : LpBase(), LpSolver(), CplexBase(env) {}
alpar@484
   501
alpar@485
   502
  CplexLp::CplexLp(const CplexLp& other)
deba@598
   503
    : LpBase(), LpSolver(), CplexBase(other) {}
alpar@484
   504
alpar@485
   505
  CplexLp::~CplexLp() {}
alpar@484
   506
alpar@587
   507
  CplexLp* CplexLp::newSolver() const { return new CplexLp; }
alpar@587
   508
  CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
alpar@484
   509
alpar@485
   510
  const char* CplexLp::_solverName() const { return "CplexLp"; }
alpar@484
   511
alpar@485
   512
  void CplexLp::_clear_temporals() {
alpar@484
   513
    _col_status.clear();
alpar@484
   514
    _row_status.clear();
alpar@484
   515
    _primal_ray.clear();
alpar@484
   516
    _dual_ray.clear();
alpar@484
   517
  }
alpar@484
   518
alpar@484
   519
  // The routine returns zero unless an error occurred during the
alpar@484
   520
  // optimization. Examples of errors include exhausting available
alpar@484
   521
  // memory (CPXERR_NO_MEMORY) or encountering invalid data in the
alpar@484
   522
  // CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a
alpar@484
   523
  // user-specified CPLEX limit, or proving the model infeasible or
alpar@484
   524
  // unbounded, are not considered errors. Note that a zero return
alpar@484
   525
  // value does not necessarily mean that a solution exists. Use query
alpar@484
   526
  // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
alpar@484
   527
  // further information about the status of the optimization.
alpar@485
   528
  CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
alpar@484
   529
#if CPX_VERSION >= 800
alpar@484
   530
    if (status == 0) {
alpar@484
   531
      switch (CPXgetstat(cplexEnv(), _prob)) {
alpar@484
   532
      case CPX_STAT_OPTIMAL:
alpar@484
   533
      case CPX_STAT_INFEASIBLE:
alpar@484
   534
      case CPX_STAT_UNBOUNDED:
alpar@484
   535
        return SOLVED;
alpar@484
   536
      default:
alpar@484
   537
        return UNSOLVED;
alpar@484
   538
      }
alpar@484
   539
    } else {
alpar@484
   540
      return UNSOLVED;
alpar@484
   541
    }
alpar@484
   542
#else
alpar@484
   543
    if (status == 0) {
alpar@484
   544
      //We want to exclude some cases
alpar@484
   545
      switch (CPXgetstat(cplexEnv(), _prob)) {
alpar@484
   546
      case CPX_OBJ_LIM:
alpar@484
   547
      case CPX_IT_LIM_FEAS:
alpar@484
   548
      case CPX_IT_LIM_INFEAS:
alpar@484
   549
      case CPX_TIME_LIM_FEAS:
alpar@484
   550
      case CPX_TIME_LIM_INFEAS:
alpar@484
   551
        return UNSOLVED;
alpar@484
   552
      default:
alpar@484
   553
        return SOLVED;
alpar@484
   554
      }
alpar@484
   555
    } else {
alpar@484
   556
      return UNSOLVED;
alpar@484
   557
    }
alpar@484
   558
#endif
alpar@484
   559
  }
alpar@484
   560
alpar@485
   561
  CplexLp::SolveExitStatus CplexLp::_solve() {
alpar@484
   562
    _clear_temporals();
deba@623
   563
    _applyMessageLevel();
alpar@484
   564
    return convertStatus(CPXlpopt(cplexEnv(), _prob));
alpar@484
   565
  }
alpar@484
   566
alpar@485
   567
  CplexLp::SolveExitStatus CplexLp::solvePrimal() {
alpar@484
   568
    _clear_temporals();
deba@623
   569
    _applyMessageLevel();
alpar@484
   570
    return convertStatus(CPXprimopt(cplexEnv(), _prob));
alpar@484
   571
  }
alpar@484
   572
alpar@485
   573
  CplexLp::SolveExitStatus CplexLp::solveDual() {
alpar@484
   574
    _clear_temporals();
deba@623
   575
    _applyMessageLevel();
alpar@484
   576
    return convertStatus(CPXdualopt(cplexEnv(), _prob));
alpar@484
   577
  }
alpar@484
   578
alpar@485
   579
  CplexLp::SolveExitStatus CplexLp::solveBarrier() {
alpar@484
   580
    _clear_temporals();
deba@623
   581
    _applyMessageLevel();
alpar@484
   582
    return convertStatus(CPXbaropt(cplexEnv(), _prob));
alpar@484
   583
  }
alpar@484
   584
alpar@485
   585
  CplexLp::Value CplexLp::_getPrimal(int i) const {
alpar@484
   586
    Value x;
alpar@484
   587
    CPXgetx(cplexEnv(), _prob, &x, i, i);
alpar@484
   588
    return x;
alpar@484
   589
  }
alpar@484
   590
alpar@485
   591
  CplexLp::Value CplexLp::_getDual(int i) const {
alpar@484
   592
    Value y;
alpar@484
   593
    CPXgetpi(cplexEnv(), _prob, &y, i, i);
alpar@484
   594
    return y;
alpar@484
   595
  }
alpar@484
   596
alpar@485
   597
  CplexLp::Value CplexLp::_getPrimalValue() const {
alpar@484
   598
    Value objval;
alpar@484
   599
    CPXgetobjval(cplexEnv(), _prob, &objval);
alpar@484
   600
    return objval;
alpar@484
   601
  }
alpar@484
   602
alpar@485
   603
  CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
alpar@484
   604
    if (_col_status.empty()) {
alpar@484
   605
      _col_status.resize(CPXgetnumcols(cplexEnv(), _prob));
alpar@484
   606
      CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0);
alpar@484
   607
    }
alpar@484
   608
    switch (_col_status[i]) {
alpar@484
   609
    case CPX_BASIC:
alpar@484
   610
      return BASIC;
alpar@484
   611
    case CPX_FREE_SUPER:
alpar@484
   612
      return FREE;
alpar@484
   613
    case CPX_AT_LOWER:
alpar@484
   614
      return LOWER;
alpar@484
   615
    case CPX_AT_UPPER:
alpar@484
   616
      return UPPER;
alpar@484
   617
    default:
alpar@484
   618
      LEMON_ASSERT(false, "Wrong column status");
alpar@485
   619
      return CplexLp::VarStatus();
alpar@484
   620
    }
alpar@484
   621
  }
alpar@484
   622
alpar@485
   623
  CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
alpar@484
   624
    if (_row_status.empty()) {
alpar@484
   625
      _row_status.resize(CPXgetnumrows(cplexEnv(), _prob));
alpar@484
   626
      CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front());
alpar@484
   627
    }
alpar@484
   628
    switch (_row_status[i]) {
alpar@484
   629
    case CPX_BASIC:
alpar@484
   630
      return BASIC;
alpar@484
   631
    case CPX_AT_LOWER:
alpar@484
   632
      {
alpar@484
   633
        char s;
alpar@484
   634
        CPXgetsense(cplexEnv(), _prob, &s, i, i);
alpar@484
   635
        return s != 'L' ? LOWER : UPPER;
alpar@484
   636
      }
alpar@484
   637
    case CPX_AT_UPPER:
alpar@484
   638
      return UPPER;
alpar@484
   639
    default:
alpar@484
   640
      LEMON_ASSERT(false, "Wrong row status");
alpar@485
   641
      return CplexLp::VarStatus();
alpar@484
   642
    }
alpar@484
   643
  }
alpar@484
   644
alpar@485
   645
  CplexLp::Value CplexLp::_getPrimalRay(int i) const {
alpar@484
   646
    if (_primal_ray.empty()) {
alpar@484
   647
      _primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob));
alpar@484
   648
      CPXgetray(cplexEnv(), _prob, &_primal_ray.front());
alpar@484
   649
    }
alpar@484
   650
    return _primal_ray[i];
alpar@484
   651
  }
alpar@484
   652
alpar@485
   653
  CplexLp::Value CplexLp::_getDualRay(int i) const {
alpar@484
   654
    if (_dual_ray.empty()) {
alpar@484
   655
alpar@484
   656
    }
alpar@484
   657
    return _dual_ray[i];
alpar@484
   658
  }
alpar@484
   659
deba@623
   660
  // Cplex 7.0 status values
alpar@484
   661
  // This table lists the statuses, returned by the CPXgetstat()
alpar@484
   662
  // routine, for solutions to LP problems or mixed integer problems. If
alpar@484
   663
  // no solution exists, the return value is zero.
alpar@484
   664
alpar@484
   665
  // For Simplex, Barrier
alpar@484
   666
  // 1          CPX_OPTIMAL
alpar@484
   667
  //          Optimal solution found
alpar@484
   668
  // 2          CPX_INFEASIBLE
alpar@484
   669
  //          Problem infeasible
alpar@484
   670
  // 3    CPX_UNBOUNDED
alpar@484
   671
  //          Problem unbounded
alpar@484
   672
  // 4          CPX_OBJ_LIM
alpar@484
   673
  //          Objective limit exceeded in Phase II
alpar@484
   674
  // 5          CPX_IT_LIM_FEAS
alpar@484
   675
  //          Iteration limit exceeded in Phase II
alpar@484
   676
  // 6          CPX_IT_LIM_INFEAS
alpar@484
   677
  //          Iteration limit exceeded in Phase I
alpar@484
   678
  // 7          CPX_TIME_LIM_FEAS
alpar@484
   679
  //          Time limit exceeded in Phase II
alpar@484
   680
  // 8          CPX_TIME_LIM_INFEAS
alpar@484
   681
  //          Time limit exceeded in Phase I
alpar@484
   682
  // 9          CPX_NUM_BEST_FEAS
alpar@484
   683
  //          Problem non-optimal, singularities in Phase II
alpar@484
   684
  // 10         CPX_NUM_BEST_INFEAS
alpar@484
   685
  //          Problem non-optimal, singularities in Phase I
alpar@484
   686
  // 11         CPX_OPTIMAL_INFEAS
alpar@484
   687
  //          Optimal solution found, unscaled infeasibilities
alpar@484
   688
  // 12         CPX_ABORT_FEAS
alpar@484
   689
  //          Aborted in Phase II
alpar@484
   690
  // 13         CPX_ABORT_INFEAS
alpar@484
   691
  //          Aborted in Phase I
alpar@484
   692
  // 14          CPX_ABORT_DUAL_INFEAS
alpar@484
   693
  //          Aborted in barrier, dual infeasible
alpar@484
   694
  // 15          CPX_ABORT_PRIM_INFEAS
alpar@484
   695
  //          Aborted in barrier, primal infeasible
alpar@484
   696
  // 16          CPX_ABORT_PRIM_DUAL_INFEAS
alpar@484
   697
  //          Aborted in barrier, primal and dual infeasible
alpar@484
   698
  // 17          CPX_ABORT_PRIM_DUAL_FEAS
alpar@484
   699
  //          Aborted in barrier, primal and dual feasible
alpar@484
   700
  // 18          CPX_ABORT_CROSSOVER
alpar@484
   701
  //          Aborted in crossover
alpar@484
   702
  // 19          CPX_INForUNBD
alpar@484
   703
  //          Infeasible or unbounded
alpar@484
   704
  // 20   CPX_PIVOT
alpar@484
   705
  //       User pivot used
alpar@484
   706
  //
deba@623
   707
  // Pending return values
alpar@484
   708
  // ??case CPX_ABORT_DUAL_INFEAS
alpar@484
   709
  // ??case CPX_ABORT_CROSSOVER
alpar@484
   710
  // ??case CPX_INForUNBD
alpar@484
   711
  // ??case CPX_PIVOT
alpar@484
   712
alpar@484
   713
  //Some more interesting stuff:
alpar@484
   714
alpar@484
   715
  // CPX_PARAM_PROBMETHOD  1062  int  LPMETHOD
alpar@484
   716
  // 0 Automatic
alpar@484
   717
  // 1 Primal Simplex
alpar@484
   718
  // 2 Dual Simplex
alpar@484
   719
  // 3 Network Simplex
alpar@484
   720
  // 4 Standard Barrier
alpar@484
   721
  // Default: 0
alpar@484
   722
  // Description: Method for linear optimization.
alpar@484
   723
  // Determines which algorithm is used when CPXlpopt() (or "optimize"
alpar@484
   724
  // in the Interactive Optimizer) is called. Currently the behavior of
alpar@484
   725
  // the "Automatic" setting is that CPLEX simply invokes the dual
alpar@484
   726
  // simplex method, but this capability may be expanded in the future
alpar@484
   727
  // so that CPLEX chooses the method based on problem characteristics
alpar@484
   728
#if CPX_VERSION < 900
alpar@484
   729
  void statusSwitch(CPXENVptr cplexEnv(),int& stat){
alpar@484
   730
    int lpmethod;
alpar@484
   731
    CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod);
alpar@484
   732
    if (lpmethod==2){
alpar@484
   733
      if (stat==CPX_UNBOUNDED){
alpar@484
   734
        stat=CPX_INFEASIBLE;
alpar@484
   735
      }
alpar@484
   736
      else{
alpar@484
   737
        if (stat==CPX_INFEASIBLE)
alpar@484
   738
          stat=CPX_UNBOUNDED;
alpar@484
   739
      }
alpar@484
   740
    }
alpar@484
   741
  }
alpar@484
   742
#else
alpar@484
   743
  void statusSwitch(CPXENVptr,int&){}
alpar@484
   744
#endif
alpar@484
   745
alpar@485
   746
  CplexLp::ProblemType CplexLp::_getPrimalType() const {
alpar@484
   747
    // Unboundedness not treated well: the following is from cplex 9.0 doc
alpar@484
   748
    // About Unboundedness
alpar@484
   749
alpar@484
   750
    // The treatment of models that are unbounded involves a few
alpar@484
   751
    // subtleties. Specifically, a declaration of unboundedness means that
alpar@484
   752
    // ILOG CPLEX has determined that the model has an unbounded
alpar@484
   753
    // ray. Given any feasible solution x with objective z, a multiple of
alpar@484
   754
    // the unbounded ray can be added to x to give a feasible solution
alpar@484
   755
    // with objective z-1 (or z+1 for maximization models). Thus, if a
alpar@484
   756
    // feasible solution exists, then the optimal objective is
alpar@484
   757
    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
alpar@484
   758
    // a feasible solution exists. Users can call the routine CPXsolninfo
alpar@484
   759
    // to determine whether ILOG CPLEX has also concluded that the model
alpar@484
   760
    // has a feasible solution.
alpar@484
   761
alpar@484
   762
    int stat = CPXgetstat(cplexEnv(), _prob);
alpar@484
   763
#if CPX_VERSION >= 800
alpar@484
   764
    switch (stat)
alpar@484
   765
      {
alpar@484
   766
      case CPX_STAT_OPTIMAL:
alpar@484
   767
        return OPTIMAL;
alpar@484
   768
      case CPX_STAT_UNBOUNDED:
alpar@484
   769
        return UNBOUNDED;
alpar@484
   770
      case CPX_STAT_INFEASIBLE:
alpar@484
   771
        return INFEASIBLE;
alpar@484
   772
      default:
alpar@484
   773
        return UNDEFINED;
alpar@484
   774
      }
alpar@484
   775
#else
alpar@484
   776
    statusSwitch(cplexEnv(),stat);
alpar@484
   777
    //CPXgetstat(cplexEnv(), _prob);
alpar@484
   778
    switch (stat) {
alpar@484
   779
    case 0:
alpar@484
   780
      return UNDEFINED; //Undefined
alpar@484
   781
    case CPX_OPTIMAL://Optimal
alpar@484
   782
      return OPTIMAL;
alpar@484
   783
    case CPX_UNBOUNDED://Unbounded
alpar@484
   784
      return INFEASIBLE;//In case of dual simplex
alpar@484
   785
      //return UNBOUNDED;
alpar@484
   786
    case CPX_INFEASIBLE://Infeasible
alpar@484
   787
      //    case CPX_IT_LIM_INFEAS:
alpar@484
   788
      //     case CPX_TIME_LIM_INFEAS:
alpar@484
   789
      //     case CPX_NUM_BEST_INFEAS:
alpar@484
   790
      //     case CPX_OPTIMAL_INFEAS:
alpar@484
   791
      //     case CPX_ABORT_INFEAS:
alpar@484
   792
      //     case CPX_ABORT_PRIM_INFEAS:
alpar@484
   793
      //     case CPX_ABORT_PRIM_DUAL_INFEAS:
alpar@484
   794
      return UNBOUNDED;//In case of dual simplex
alpar@484
   795
      //return INFEASIBLE;
alpar@484
   796
      //     case CPX_OBJ_LIM:
alpar@484
   797
      //     case CPX_IT_LIM_FEAS:
alpar@484
   798
      //     case CPX_TIME_LIM_FEAS:
alpar@484
   799
      //     case CPX_NUM_BEST_FEAS:
alpar@484
   800
      //     case CPX_ABORT_FEAS:
alpar@484
   801
      //     case CPX_ABORT_PRIM_DUAL_FEAS:
alpar@484
   802
      //       return FEASIBLE;
alpar@484
   803
    default:
alpar@484
   804
      return UNDEFINED; //Everything else comes here
alpar@484
   805
      //FIXME error
alpar@484
   806
    }
alpar@484
   807
#endif
alpar@484
   808
  }
alpar@484
   809
deba@623
   810
  // Cplex 9.0 status values
alpar@484
   811
  // CPX_STAT_ABORT_DUAL_OBJ_LIM
alpar@484
   812
  // CPX_STAT_ABORT_IT_LIM
alpar@484
   813
  // CPX_STAT_ABORT_OBJ_LIM
alpar@484
   814
  // CPX_STAT_ABORT_PRIM_OBJ_LIM
alpar@484
   815
  // CPX_STAT_ABORT_TIME_LIM
alpar@484
   816
  // CPX_STAT_ABORT_USER
alpar@484
   817
  // CPX_STAT_FEASIBLE_RELAXED
alpar@484
   818
  // CPX_STAT_INFEASIBLE
alpar@484
   819
  // CPX_STAT_INForUNBD
alpar@484
   820
  // CPX_STAT_NUM_BEST
alpar@484
   821
  // CPX_STAT_OPTIMAL
alpar@484
   822
  // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
alpar@484
   823
  // CPX_STAT_OPTIMAL_INFEAS
alpar@484
   824
  // CPX_STAT_OPTIMAL_RELAXED
alpar@484
   825
  // CPX_STAT_UNBOUNDED
alpar@484
   826
alpar@485
   827
  CplexLp::ProblemType CplexLp::_getDualType() const {
alpar@484
   828
    int stat = CPXgetstat(cplexEnv(), _prob);
alpar@484
   829
#if CPX_VERSION >= 800
alpar@484
   830
    switch (stat) {
alpar@484
   831
    case CPX_STAT_OPTIMAL:
alpar@484
   832
      return OPTIMAL;
alpar@484
   833
    case CPX_STAT_UNBOUNDED:
alpar@484
   834
      return INFEASIBLE;
alpar@484
   835
    default:
alpar@484
   836
      return UNDEFINED;
alpar@484
   837
    }
alpar@484
   838
#else
alpar@484
   839
    statusSwitch(cplexEnv(),stat);
alpar@484
   840
    switch (stat) {
alpar@484
   841
    case 0:
alpar@484
   842
      return UNDEFINED; //Undefined
alpar@484
   843
    case CPX_OPTIMAL://Optimal
alpar@484
   844
      return OPTIMAL;
alpar@484
   845
    case CPX_UNBOUNDED:
alpar@484
   846
      return INFEASIBLE;
alpar@484
   847
    default:
alpar@484
   848
      return UNDEFINED; //Everything else comes here
alpar@484
   849
      //FIXME error
alpar@484
   850
    }
alpar@484
   851
#endif
alpar@484
   852
  }
alpar@484
   853
alpar@485
   854
  // CplexMip members
alpar@484
   855
alpar@485
   856
  CplexMip::CplexMip()
deba@598
   857
    : LpBase(), MipSolver(), CplexBase() {
alpar@484
   858
alpar@484
   859
#if CPX_VERSION < 800
alpar@484
   860
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
alpar@484
   861
#else
alpar@484
   862
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
alpar@484
   863
#endif
alpar@484
   864
  }
alpar@484
   865
alpar@485
   866
  CplexMip::CplexMip(const CplexEnv& env)
deba@598
   867
    : LpBase(), MipSolver(), CplexBase(env) {
alpar@484
   868
alpar@484
   869
#if CPX_VERSION < 800
alpar@484
   870
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
alpar@484
   871
#else
alpar@484
   872
    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
alpar@484
   873
#endif
alpar@484
   874
alpar@484
   875
  }
alpar@484
   876
alpar@485
   877
  CplexMip::CplexMip(const CplexMip& other)
deba@598
   878
    : LpBase(), MipSolver(), CplexBase(other) {}
alpar@484
   879
alpar@485
   880
  CplexMip::~CplexMip() {}
alpar@484
   881
alpar@587
   882
  CplexMip* CplexMip::newSolver() const { return new CplexMip; }
alpar@587
   883
  CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }
alpar@484
   884
alpar@485
   885
  const char* CplexMip::_solverName() const { return "CplexMip"; }
alpar@484
   886
alpar@485
   887
  void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
alpar@484
   888
alpar@484
   889
    // Note If a variable is to be changed to binary, a call to CPXchgbds
alpar@484
   890
    // should also be made to change the bounds to 0 and 1.
alpar@484
   891
alpar@484
   892
    switch (col_type){
alpar@484
   893
    case INTEGER: {
alpar@484
   894
      const char t = 'I';
alpar@484
   895
      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
alpar@484
   896
    } break;
alpar@484
   897
    case REAL: {
alpar@484
   898
      const char t = 'C';
alpar@484
   899
      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
alpar@484
   900
    } break;
alpar@484
   901
    default:
alpar@484
   902
      break;
alpar@484
   903
    }
alpar@484
   904
  }
alpar@484
   905
alpar@485
   906
  CplexMip::ColTypes CplexMip::_getColType(int i) const {
alpar@484
   907
    char t;
alpar@484
   908
    CPXgetctype (cplexEnv(), _prob, &t, i, i);
alpar@484
   909
    switch (t) {
alpar@484
   910
    case 'I':
alpar@484
   911
      return INTEGER;
alpar@484
   912
    case 'C':
alpar@484
   913
      return REAL;
alpar@484
   914
    default:
alpar@484
   915
      LEMON_ASSERT(false, "Invalid column type");
alpar@484
   916
      return ColTypes();
alpar@484
   917
    }
alpar@484
   918
alpar@484
   919
  }
alpar@484
   920
alpar@485
   921
  CplexMip::SolveExitStatus CplexMip::_solve() {
alpar@484
   922
    int status;
deba@623
   923
    _applyMessageLevel();
alpar@484
   924
    status = CPXmipopt (cplexEnv(), _prob);
alpar@484
   925
    if (status==0)
alpar@484
   926
      return SOLVED;
alpar@484
   927
    else
alpar@484
   928
      return UNSOLVED;
alpar@484
   929
alpar@484
   930
  }
alpar@484
   931
alpar@484
   932
alpar@485
   933
  CplexMip::ProblemType CplexMip::_getType() const {
alpar@484
   934
alpar@484
   935
    int stat = CPXgetstat(cplexEnv(), _prob);
alpar@484
   936
alpar@484
   937
    //Fortunately, MIP statuses did not change for cplex 8.0
alpar@484
   938
    switch (stat) {
alpar@484
   939
    case CPXMIP_OPTIMAL:
alpar@484
   940
      // Optimal integer solution has been found.
alpar@484
   941
    case CPXMIP_OPTIMAL_TOL:
alpar@484
   942
      // Optimal soluton with the tolerance defined by epgap or epagap has
alpar@484
   943
      // been found.
alpar@484
   944
      return OPTIMAL;
alpar@484
   945
      //This also exists in later issues
alpar@484
   946
      //    case CPXMIP_UNBOUNDED:
alpar@484
   947
      //return UNBOUNDED;
alpar@484
   948
      case CPXMIP_INFEASIBLE:
alpar@484
   949
        return INFEASIBLE;
alpar@484
   950
    default:
alpar@484
   951
      return UNDEFINED;
alpar@484
   952
    }
alpar@484
   953
    //Unboundedness not treated well: the following is from cplex 9.0 doc
alpar@484
   954
    // About Unboundedness
alpar@484
   955
alpar@484
   956
    // The treatment of models that are unbounded involves a few
alpar@484
   957
    // subtleties. Specifically, a declaration of unboundedness means that
alpar@484
   958
    // ILOG CPLEX has determined that the model has an unbounded
alpar@484
   959
    // ray. Given any feasible solution x with objective z, a multiple of
alpar@484
   960
    // the unbounded ray can be added to x to give a feasible solution
alpar@484
   961
    // with objective z-1 (or z+1 for maximization models). Thus, if a
alpar@484
   962
    // feasible solution exists, then the optimal objective is
alpar@484
   963
    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
alpar@484
   964
    // a feasible solution exists. Users can call the routine CPXsolninfo
alpar@484
   965
    // to determine whether ILOG CPLEX has also concluded that the model
alpar@484
   966
    // has a feasible solution.
alpar@484
   967
  }
alpar@484
   968
alpar@485
   969
  CplexMip::Value CplexMip::_getSol(int i) const {
alpar@484
   970
    Value x;
alpar@484
   971
    CPXgetmipx(cplexEnv(), _prob, &x, i, i);
alpar@484
   972
    return x;
alpar@484
   973
  }
alpar@484
   974
alpar@485
   975
  CplexMip::Value CplexMip::_getSolValue() const {
alpar@484
   976
    Value objval;
alpar@484
   977
    CPXgetmipobjval(cplexEnv(), _prob, &objval);
alpar@484
   978
    return objval;
alpar@484
   979
  }
alpar@484
   980
alpar@484
   981
} //namespace lemon
alpar@484
   982