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