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