lemon/cplex.cc
changeset 802 994c7df296c9
parent 551 9d0d7e20f76d
child 746 e4554cd6b2bf
child 955 8d281761dea4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/cplex.cc	Thu Dec 10 17:05:35 2009 +0100
     1.3 @@ -0,0 +1,951 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2009
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +#include <vector>
    1.24 +#include <cstring>
    1.25 +
    1.26 +#include <lemon/cplex.h>
    1.27 +
    1.28 +extern "C" {
    1.29 +#include <ilcplex/cplex.h>
    1.30 +}
    1.31 +
    1.32 +
    1.33 +///\file
    1.34 +///\brief Implementation of the LEMON-CPLEX lp solver interface.
    1.35 +namespace lemon {
    1.36 +
    1.37 +  CplexEnv::LicenseError::LicenseError(int status) {
    1.38 +    if (!CPXgeterrorstring(0, status, _message)) {
    1.39 +      std::strcpy(_message, "Cplex unknown error");
    1.40 +    }
    1.41 +  }
    1.42 +
    1.43 +  CplexEnv::CplexEnv() {
    1.44 +    int status;
    1.45 +    _cnt = new int;
    1.46 +    _env = CPXopenCPLEX(&status);
    1.47 +    if (_env == 0) {
    1.48 +      delete _cnt;
    1.49 +      _cnt = 0;
    1.50 +      throw LicenseError(status);
    1.51 +    }
    1.52 +  }
    1.53 +
    1.54 +  CplexEnv::CplexEnv(const CplexEnv& other) {
    1.55 +    _env = other._env;
    1.56 +    _cnt = other._cnt;
    1.57 +    ++(*_cnt);
    1.58 +  }
    1.59 +
    1.60 +  CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
    1.61 +    _env = other._env;
    1.62 +    _cnt = other._cnt;
    1.63 +    ++(*_cnt);
    1.64 +    return *this;
    1.65 +  }
    1.66 +
    1.67 +  CplexEnv::~CplexEnv() {
    1.68 +    --(*_cnt);
    1.69 +    if (*_cnt == 0) {
    1.70 +      delete _cnt;
    1.71 +      CPXcloseCPLEX(&_env);
    1.72 +    }
    1.73 +  }
    1.74 +
    1.75 +  CplexBase::CplexBase() : LpBase() {
    1.76 +    int status;
    1.77 +    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
    1.78 +    messageLevel(MESSAGE_NOTHING);
    1.79 +  }
    1.80 +
    1.81 +  CplexBase::CplexBase(const CplexEnv& env)
    1.82 +    : LpBase(), _env(env) {
    1.83 +    int status;
    1.84 +    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
    1.85 +    messageLevel(MESSAGE_NOTHING);
    1.86 +  }
    1.87 +
    1.88 +  CplexBase::CplexBase(const CplexBase& cplex)
    1.89 +    : LpBase() {
    1.90 +    int status;
    1.91 +    _prob = CPXcloneprob(cplexEnv(), cplex._prob, &status);
    1.92 +    rows = cplex.rows;
    1.93 +    cols = cplex.cols;
    1.94 +    messageLevel(MESSAGE_NOTHING);
    1.95 +  }
    1.96 +
    1.97 +  CplexBase::~CplexBase() {
    1.98 +    CPXfreeprob(cplexEnv(),&_prob);
    1.99 +  }
   1.100 +
   1.101 +  int CplexBase::_addCol() {
   1.102 +    int i = CPXgetnumcols(cplexEnv(), _prob);
   1.103 +    double lb = -INF, ub = INF;
   1.104 +    CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0);
   1.105 +    return i;
   1.106 +  }
   1.107 +
   1.108 +
   1.109 +  int CplexBase::_addRow() {
   1.110 +    int i = CPXgetnumrows(cplexEnv(), _prob);
   1.111 +    const double ub = INF;
   1.112 +    const char s = 'L';
   1.113 +    CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
   1.114 +    return i;
   1.115 +  }
   1.116 +
   1.117 +
   1.118 +  void CplexBase::_eraseCol(int i) {
   1.119 +    CPXdelcols(cplexEnv(), _prob, i, i);
   1.120 +  }
   1.121 +
   1.122 +  void CplexBase::_eraseRow(int i) {
   1.123 +    CPXdelrows(cplexEnv(), _prob, i, i);
   1.124 +  }
   1.125 +
   1.126 +  void CplexBase::_eraseColId(int i) {
   1.127 +    cols.eraseIndex(i);
   1.128 +    cols.shiftIndices(i);
   1.129 +  }
   1.130 +  void CplexBase::_eraseRowId(int i) {
   1.131 +    rows.eraseIndex(i);
   1.132 +    rows.shiftIndices(i);
   1.133 +  }
   1.134 +
   1.135 +  void CplexBase::_getColName(int col, std::string &name) const {
   1.136 +    int size;
   1.137 +    CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col);
   1.138 +    if (size == 0) {
   1.139 +      name.clear();
   1.140 +      return;
   1.141 +    }
   1.142 +
   1.143 +    size *= -1;
   1.144 +    std::vector<char> buf(size);
   1.145 +    char *cname;
   1.146 +    int tmp;
   1.147 +    CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size,
   1.148 +                  &tmp, col, col);
   1.149 +    name = cname;
   1.150 +  }
   1.151 +
   1.152 +  void CplexBase::_setColName(int col, const std::string &name) {
   1.153 +    char *cname;
   1.154 +    cname = const_cast<char*>(name.c_str());
   1.155 +    CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname);
   1.156 +  }
   1.157 +
   1.158 +  int CplexBase::_colByName(const std::string& name) const {
   1.159 +    int index;
   1.160 +    if (CPXgetcolindex(cplexEnv(), _prob,
   1.161 +                       const_cast<char*>(name.c_str()), &index) == 0) {
   1.162 +      return index;
   1.163 +    }
   1.164 +    return -1;
   1.165 +  }
   1.166 +
   1.167 +  void CplexBase::_getRowName(int row, std::string &name) const {
   1.168 +    int size;
   1.169 +    CPXgetrowname(cplexEnv(), _prob, 0, 0, 0, &size, row, row);
   1.170 +    if (size == 0) {
   1.171 +      name.clear();
   1.172 +      return;
   1.173 +    }
   1.174 +
   1.175 +    size *= -1;
   1.176 +    std::vector<char> buf(size);
   1.177 +    char *cname;
   1.178 +    int tmp;
   1.179 +    CPXgetrowname(cplexEnv(), _prob, &cname, &buf.front(), size,
   1.180 +                  &tmp, row, row);
   1.181 +    name = cname;
   1.182 +  }
   1.183 +
   1.184 +  void CplexBase::_setRowName(int row, const std::string &name) {
   1.185 +    char *cname;
   1.186 +    cname = const_cast<char*>(name.c_str());
   1.187 +    CPXchgrowname(cplexEnv(), _prob, 1, &row, &cname);
   1.188 +  }
   1.189 +
   1.190 +  int CplexBase::_rowByName(const std::string& name) const {
   1.191 +    int index;
   1.192 +    if (CPXgetrowindex(cplexEnv(), _prob,
   1.193 +                       const_cast<char*>(name.c_str()), &index) == 0) {
   1.194 +      return index;
   1.195 +    }
   1.196 +    return -1;
   1.197 +  }
   1.198 +
   1.199 +  void CplexBase::_setRowCoeffs(int i, ExprIterator b,
   1.200 +                                      ExprIterator e)
   1.201 +  {
   1.202 +    std::vector<int> indices;
   1.203 +    std::vector<int> rowlist;
   1.204 +    std::vector<Value> values;
   1.205 +
   1.206 +    for(ExprIterator it=b; it!=e; ++it) {
   1.207 +      indices.push_back(it->first);
   1.208 +      values.push_back(it->second);
   1.209 +      rowlist.push_back(i);
   1.210 +    }
   1.211 +
   1.212 +    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
   1.213 +                   &rowlist.front(), &indices.front(), &values.front());
   1.214 +  }
   1.215 +
   1.216 +  void CplexBase::_getRowCoeffs(int i, InsertIterator b) const {
   1.217 +    int tmp1, tmp2, tmp3, length;
   1.218 +    CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
   1.219 +
   1.220 +    length = -length;
   1.221 +    std::vector<int> indices(length);
   1.222 +    std::vector<double> values(length);
   1.223 +
   1.224 +    CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2,
   1.225 +               &indices.front(), &values.front(),
   1.226 +               length, &tmp3, i, i);
   1.227 +
   1.228 +    for (int i = 0; i < length; ++i) {
   1.229 +      *b = std::make_pair(indices[i], values[i]);
   1.230 +      ++b;
   1.231 +    }
   1.232 +  }
   1.233 +
   1.234 +  void CplexBase::_setColCoeffs(int i, ExprIterator b, ExprIterator e) {
   1.235 +    std::vector<int> indices;
   1.236 +    std::vector<int> collist;
   1.237 +    std::vector<Value> values;
   1.238 +
   1.239 +    for(ExprIterator it=b; it!=e; ++it) {
   1.240 +      indices.push_back(it->first);
   1.241 +      values.push_back(it->second);
   1.242 +      collist.push_back(i);
   1.243 +    }
   1.244 +
   1.245 +    CPXchgcoeflist(cplexEnv(), _prob, values.size(),
   1.246 +                   &indices.front(), &collist.front(), &values.front());
   1.247 +  }
   1.248 +
   1.249 +  void CplexBase::_getColCoeffs(int i, InsertIterator b) const {
   1.250 +
   1.251 +    int tmp1, tmp2, tmp3, length;
   1.252 +    CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
   1.253 +
   1.254 +    length = -length;
   1.255 +    std::vector<int> indices(length);
   1.256 +    std::vector<double> values(length);
   1.257 +
   1.258 +    CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2,
   1.259 +               &indices.front(), &values.front(),
   1.260 +               length, &tmp3, i, i);
   1.261 +
   1.262 +    for (int i = 0; i < length; ++i) {
   1.263 +      *b = std::make_pair(indices[i], values[i]);
   1.264 +      ++b;
   1.265 +    }
   1.266 +
   1.267 +  }
   1.268 +
   1.269 +  void CplexBase::_setCoeff(int row, int col, Value value) {
   1.270 +    CPXchgcoef(cplexEnv(), _prob, row, col, value);
   1.271 +  }
   1.272 +
   1.273 +  CplexBase::Value CplexBase::_getCoeff(int row, int col) const {
   1.274 +    CplexBase::Value value;
   1.275 +    CPXgetcoef(cplexEnv(), _prob, row, col, &value);
   1.276 +    return value;
   1.277 +  }
   1.278 +
   1.279 +  void CplexBase::_setColLowerBound(int i, Value value) {
   1.280 +    const char s = 'L';
   1.281 +    CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
   1.282 +  }
   1.283 +
   1.284 +  CplexBase::Value CplexBase::_getColLowerBound(int i) const {
   1.285 +    CplexBase::Value res;
   1.286 +    CPXgetlb(cplexEnv(), _prob, &res, i, i);
   1.287 +    return res <= -CPX_INFBOUND ? -INF : res;
   1.288 +  }
   1.289 +
   1.290 +  void CplexBase::_setColUpperBound(int i, Value value)
   1.291 +  {
   1.292 +    const char s = 'U';
   1.293 +    CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
   1.294 +  }
   1.295 +
   1.296 +  CplexBase::Value CplexBase::_getColUpperBound(int i) const {
   1.297 +    CplexBase::Value res;
   1.298 +    CPXgetub(cplexEnv(), _prob, &res, i, i);
   1.299 +    return res >= CPX_INFBOUND ? INF : res;
   1.300 +  }
   1.301 +
   1.302 +  CplexBase::Value CplexBase::_getRowLowerBound(int i) const {
   1.303 +    char s;
   1.304 +    CPXgetsense(cplexEnv(), _prob, &s, i, i);
   1.305 +    CplexBase::Value res;
   1.306 +
   1.307 +    switch (s) {
   1.308 +    case 'G':
   1.309 +    case 'R':
   1.310 +    case 'E':
   1.311 +      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
   1.312 +      return res <= -CPX_INFBOUND ? -INF : res;
   1.313 +    default:
   1.314 +      return -INF;
   1.315 +    }
   1.316 +  }
   1.317 +
   1.318 +  CplexBase::Value CplexBase::_getRowUpperBound(int i) const {
   1.319 +    char s;
   1.320 +    CPXgetsense(cplexEnv(), _prob, &s, i, i);
   1.321 +    CplexBase::Value res;
   1.322 +
   1.323 +    switch (s) {
   1.324 +    case 'L':
   1.325 +    case 'E':
   1.326 +      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
   1.327 +      return res >= CPX_INFBOUND ? INF : res;
   1.328 +    case 'R':
   1.329 +      CPXgetrhs(cplexEnv(), _prob, &res, i, i);
   1.330 +      {
   1.331 +        double rng;
   1.332 +        CPXgetrngval(cplexEnv(), _prob, &rng, i, i);
   1.333 +        res += rng;
   1.334 +      }
   1.335 +      return res >= CPX_INFBOUND ? INF : res;
   1.336 +    default:
   1.337 +      return INF;
   1.338 +    }
   1.339 +  }
   1.340 +
   1.341 +  //This is easier to implement
   1.342 +  void CplexBase::_set_row_bounds(int i, Value lb, Value ub) {
   1.343 +    if (lb == -INF) {
   1.344 +      const char s = 'L';
   1.345 +      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
   1.346 +      CPXchgrhs(cplexEnv(), _prob, 1, &i, &ub);
   1.347 +    } else if (ub == INF) {
   1.348 +      const char s = 'G';
   1.349 +      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
   1.350 +      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
   1.351 +    } else if (lb == ub){
   1.352 +      const char s = 'E';
   1.353 +      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
   1.354 +      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
   1.355 +    } else {
   1.356 +      const char s = 'R';
   1.357 +      CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
   1.358 +      CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
   1.359 +      double len = ub - lb;
   1.360 +      CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
   1.361 +    }
   1.362 +  }
   1.363 +
   1.364 +  void CplexBase::_setRowLowerBound(int i, Value lb)
   1.365 +  {
   1.366 +    LEMON_ASSERT(lb != INF, "Invalid bound");
   1.367 +    _set_row_bounds(i, lb, CplexBase::_getRowUpperBound(i));
   1.368 +  }
   1.369 +
   1.370 +  void CplexBase::_setRowUpperBound(int i, Value ub)
   1.371 +  {
   1.372 +
   1.373 +    LEMON_ASSERT(ub != -INF, "Invalid bound");
   1.374 +    _set_row_bounds(i, CplexBase::_getRowLowerBound(i), ub);
   1.375 +  }
   1.376 +
   1.377 +  void CplexBase::_setObjCoeffs(ExprIterator b, ExprIterator e)
   1.378 +  {
   1.379 +    std::vector<int> indices;
   1.380 +    std::vector<Value> values;
   1.381 +    for(ExprIterator it=b; it!=e; ++it) {
   1.382 +      indices.push_back(it->first);
   1.383 +      values.push_back(it->second);
   1.384 +    }
   1.385 +    CPXchgobj(cplexEnv(), _prob, values.size(),
   1.386 +              &indices.front(), &values.front());
   1.387 +
   1.388 +  }
   1.389 +
   1.390 +  void CplexBase::_getObjCoeffs(InsertIterator b) const
   1.391 +  {
   1.392 +    int num = CPXgetnumcols(cplexEnv(), _prob);
   1.393 +    std::vector<Value> x(num);
   1.394 +
   1.395 +    CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1);
   1.396 +    for (int i = 0; i < num; ++i) {
   1.397 +      if (x[i] != 0.0) {
   1.398 +        *b = std::make_pair(i, x[i]);
   1.399 +        ++b;
   1.400 +      }
   1.401 +    }
   1.402 +  }
   1.403 +
   1.404 +  void CplexBase::_setObjCoeff(int i, Value obj_coef)
   1.405 +  {
   1.406 +    CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef);
   1.407 +  }
   1.408 +
   1.409 +  CplexBase::Value CplexBase::_getObjCoeff(int i) const
   1.410 +  {
   1.411 +    Value x;
   1.412 +    CPXgetobj(cplexEnv(), _prob, &x, i, i);
   1.413 +    return x;
   1.414 +  }
   1.415 +
   1.416 +  void CplexBase::_setSense(CplexBase::Sense sense) {
   1.417 +    switch (sense) {
   1.418 +    case MIN:
   1.419 +      CPXchgobjsen(cplexEnv(), _prob, CPX_MIN);
   1.420 +      break;
   1.421 +    case MAX:
   1.422 +      CPXchgobjsen(cplexEnv(), _prob, CPX_MAX);
   1.423 +      break;
   1.424 +    }
   1.425 +  }
   1.426 +
   1.427 +  CplexBase::Sense CplexBase::_getSense() const {
   1.428 +    switch (CPXgetobjsen(cplexEnv(), _prob)) {
   1.429 +    case CPX_MIN:
   1.430 +      return MIN;
   1.431 +    case CPX_MAX:
   1.432 +      return MAX;
   1.433 +    default:
   1.434 +      LEMON_ASSERT(false, "Invalid sense");
   1.435 +      return CplexBase::Sense();
   1.436 +    }
   1.437 +  }
   1.438 +
   1.439 +  void CplexBase::_clear() {
   1.440 +    CPXfreeprob(cplexEnv(),&_prob);
   1.441 +    int status;
   1.442 +    _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
   1.443 +    rows.clear();
   1.444 +    cols.clear();
   1.445 +  }
   1.446 +
   1.447 +  void CplexBase::_messageLevel(MessageLevel level) {
   1.448 +    switch (level) {
   1.449 +    case MESSAGE_NOTHING:
   1.450 +      _message_enabled = false;
   1.451 +      break;
   1.452 +    case MESSAGE_ERROR:
   1.453 +    case MESSAGE_WARNING:
   1.454 +    case MESSAGE_NORMAL:
   1.455 +    case MESSAGE_VERBOSE:
   1.456 +      _message_enabled = true;
   1.457 +      break;
   1.458 +    }
   1.459 +  }
   1.460 +
   1.461 +  void CplexBase::_applyMessageLevel() {
   1.462 +    CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND, 
   1.463 +                   _message_enabled ? CPX_ON : CPX_OFF);
   1.464 +  }
   1.465 +
   1.466 +  // CplexLp members
   1.467 +
   1.468 +  CplexLp::CplexLp()
   1.469 +    : LpBase(), LpSolver(), CplexBase() {}
   1.470 +
   1.471 +  CplexLp::CplexLp(const CplexEnv& env)
   1.472 +    : LpBase(), LpSolver(), CplexBase(env) {}
   1.473 +
   1.474 +  CplexLp::CplexLp(const CplexLp& other)
   1.475 +    : LpBase(), LpSolver(), CplexBase(other) {}
   1.476 +
   1.477 +  CplexLp::~CplexLp() {}
   1.478 +
   1.479 +  CplexLp* CplexLp::newSolver() const { return new CplexLp; }
   1.480 +  CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
   1.481 +
   1.482 +  const char* CplexLp::_solverName() const { return "CplexLp"; }
   1.483 +
   1.484 +  void CplexLp::_clear_temporals() {
   1.485 +    _col_status.clear();
   1.486 +    _row_status.clear();
   1.487 +    _primal_ray.clear();
   1.488 +    _dual_ray.clear();
   1.489 +  }
   1.490 +
   1.491 +  // The routine returns zero unless an error occurred during the
   1.492 +  // optimization. Examples of errors include exhausting available
   1.493 +  // memory (CPXERR_NO_MEMORY) or encountering invalid data in the
   1.494 +  // CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a
   1.495 +  // user-specified CPLEX limit, or proving the model infeasible or
   1.496 +  // unbounded, are not considered errors. Note that a zero return
   1.497 +  // value does not necessarily mean that a solution exists. Use query
   1.498 +  // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
   1.499 +  // further information about the status of the optimization.
   1.500 +  CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
   1.501 +#if CPX_VERSION >= 800
   1.502 +    if (status == 0) {
   1.503 +      switch (CPXgetstat(cplexEnv(), _prob)) {
   1.504 +      case CPX_STAT_OPTIMAL:
   1.505 +      case CPX_STAT_INFEASIBLE:
   1.506 +      case CPX_STAT_UNBOUNDED:
   1.507 +        return SOLVED;
   1.508 +      default:
   1.509 +        return UNSOLVED;
   1.510 +      }
   1.511 +    } else {
   1.512 +      return UNSOLVED;
   1.513 +    }
   1.514 +#else
   1.515 +    if (status == 0) {
   1.516 +      //We want to exclude some cases
   1.517 +      switch (CPXgetstat(cplexEnv(), _prob)) {
   1.518 +      case CPX_OBJ_LIM:
   1.519 +      case CPX_IT_LIM_FEAS:
   1.520 +      case CPX_IT_LIM_INFEAS:
   1.521 +      case CPX_TIME_LIM_FEAS:
   1.522 +      case CPX_TIME_LIM_INFEAS:
   1.523 +        return UNSOLVED;
   1.524 +      default:
   1.525 +        return SOLVED;
   1.526 +      }
   1.527 +    } else {
   1.528 +      return UNSOLVED;
   1.529 +    }
   1.530 +#endif
   1.531 +  }
   1.532 +
   1.533 +  CplexLp::SolveExitStatus CplexLp::_solve() {
   1.534 +    _clear_temporals();
   1.535 +    _applyMessageLevel();
   1.536 +    return convertStatus(CPXlpopt(cplexEnv(), _prob));
   1.537 +  }
   1.538 +
   1.539 +  CplexLp::SolveExitStatus CplexLp::solvePrimal() {
   1.540 +    _clear_temporals();
   1.541 +    _applyMessageLevel();
   1.542 +    return convertStatus(CPXprimopt(cplexEnv(), _prob));
   1.543 +  }
   1.544 +
   1.545 +  CplexLp::SolveExitStatus CplexLp::solveDual() {
   1.546 +    _clear_temporals();
   1.547 +    _applyMessageLevel();
   1.548 +    return convertStatus(CPXdualopt(cplexEnv(), _prob));
   1.549 +  }
   1.550 +
   1.551 +  CplexLp::SolveExitStatus CplexLp::solveBarrier() {
   1.552 +    _clear_temporals();
   1.553 +    _applyMessageLevel();
   1.554 +    return convertStatus(CPXbaropt(cplexEnv(), _prob));
   1.555 +  }
   1.556 +
   1.557 +  CplexLp::Value CplexLp::_getPrimal(int i) const {
   1.558 +    Value x;
   1.559 +    CPXgetx(cplexEnv(), _prob, &x, i, i);
   1.560 +    return x;
   1.561 +  }
   1.562 +
   1.563 +  CplexLp::Value CplexLp::_getDual(int i) const {
   1.564 +    Value y;
   1.565 +    CPXgetpi(cplexEnv(), _prob, &y, i, i);
   1.566 +    return y;
   1.567 +  }
   1.568 +
   1.569 +  CplexLp::Value CplexLp::_getPrimalValue() const {
   1.570 +    Value objval;
   1.571 +    CPXgetobjval(cplexEnv(), _prob, &objval);
   1.572 +    return objval;
   1.573 +  }
   1.574 +
   1.575 +  CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
   1.576 +    if (_col_status.empty()) {
   1.577 +      _col_status.resize(CPXgetnumcols(cplexEnv(), _prob));
   1.578 +      CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0);
   1.579 +    }
   1.580 +    switch (_col_status[i]) {
   1.581 +    case CPX_BASIC:
   1.582 +      return BASIC;
   1.583 +    case CPX_FREE_SUPER:
   1.584 +      return FREE;
   1.585 +    case CPX_AT_LOWER:
   1.586 +      return LOWER;
   1.587 +    case CPX_AT_UPPER:
   1.588 +      return UPPER;
   1.589 +    default:
   1.590 +      LEMON_ASSERT(false, "Wrong column status");
   1.591 +      return CplexLp::VarStatus();
   1.592 +    }
   1.593 +  }
   1.594 +
   1.595 +  CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
   1.596 +    if (_row_status.empty()) {
   1.597 +      _row_status.resize(CPXgetnumrows(cplexEnv(), _prob));
   1.598 +      CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front());
   1.599 +    }
   1.600 +    switch (_row_status[i]) {
   1.601 +    case CPX_BASIC:
   1.602 +      return BASIC;
   1.603 +    case CPX_AT_LOWER:
   1.604 +      {
   1.605 +        char s;
   1.606 +        CPXgetsense(cplexEnv(), _prob, &s, i, i);
   1.607 +        return s != 'L' ? LOWER : UPPER;
   1.608 +      }
   1.609 +    case CPX_AT_UPPER:
   1.610 +      return UPPER;
   1.611 +    default:
   1.612 +      LEMON_ASSERT(false, "Wrong row status");
   1.613 +      return CplexLp::VarStatus();
   1.614 +    }
   1.615 +  }
   1.616 +
   1.617 +  CplexLp::Value CplexLp::_getPrimalRay(int i) const {
   1.618 +    if (_primal_ray.empty()) {
   1.619 +      _primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob));
   1.620 +      CPXgetray(cplexEnv(), _prob, &_primal_ray.front());
   1.621 +    }
   1.622 +    return _primal_ray[i];
   1.623 +  }
   1.624 +
   1.625 +  CplexLp::Value CplexLp::_getDualRay(int i) const {
   1.626 +    if (_dual_ray.empty()) {
   1.627 +
   1.628 +    }
   1.629 +    return _dual_ray[i];
   1.630 +  }
   1.631 +
   1.632 +  // Cplex 7.0 status values
   1.633 +  // This table lists the statuses, returned by the CPXgetstat()
   1.634 +  // routine, for solutions to LP problems or mixed integer problems. If
   1.635 +  // no solution exists, the return value is zero.
   1.636 +
   1.637 +  // For Simplex, Barrier
   1.638 +  // 1          CPX_OPTIMAL
   1.639 +  //          Optimal solution found
   1.640 +  // 2          CPX_INFEASIBLE
   1.641 +  //          Problem infeasible
   1.642 +  // 3    CPX_UNBOUNDED
   1.643 +  //          Problem unbounded
   1.644 +  // 4          CPX_OBJ_LIM
   1.645 +  //          Objective limit exceeded in Phase II
   1.646 +  // 5          CPX_IT_LIM_FEAS
   1.647 +  //          Iteration limit exceeded in Phase II
   1.648 +  // 6          CPX_IT_LIM_INFEAS
   1.649 +  //          Iteration limit exceeded in Phase I
   1.650 +  // 7          CPX_TIME_LIM_FEAS
   1.651 +  //          Time limit exceeded in Phase II
   1.652 +  // 8          CPX_TIME_LIM_INFEAS
   1.653 +  //          Time limit exceeded in Phase I
   1.654 +  // 9          CPX_NUM_BEST_FEAS
   1.655 +  //          Problem non-optimal, singularities in Phase II
   1.656 +  // 10         CPX_NUM_BEST_INFEAS
   1.657 +  //          Problem non-optimal, singularities in Phase I
   1.658 +  // 11         CPX_OPTIMAL_INFEAS
   1.659 +  //          Optimal solution found, unscaled infeasibilities
   1.660 +  // 12         CPX_ABORT_FEAS
   1.661 +  //          Aborted in Phase II
   1.662 +  // 13         CPX_ABORT_INFEAS
   1.663 +  //          Aborted in Phase I
   1.664 +  // 14          CPX_ABORT_DUAL_INFEAS
   1.665 +  //          Aborted in barrier, dual infeasible
   1.666 +  // 15          CPX_ABORT_PRIM_INFEAS
   1.667 +  //          Aborted in barrier, primal infeasible
   1.668 +  // 16          CPX_ABORT_PRIM_DUAL_INFEAS
   1.669 +  //          Aborted in barrier, primal and dual infeasible
   1.670 +  // 17          CPX_ABORT_PRIM_DUAL_FEAS
   1.671 +  //          Aborted in barrier, primal and dual feasible
   1.672 +  // 18          CPX_ABORT_CROSSOVER
   1.673 +  //          Aborted in crossover
   1.674 +  // 19          CPX_INForUNBD
   1.675 +  //          Infeasible or unbounded
   1.676 +  // 20   CPX_PIVOT
   1.677 +  //       User pivot used
   1.678 +  //
   1.679 +  // Pending return values
   1.680 +  // ??case CPX_ABORT_DUAL_INFEAS
   1.681 +  // ??case CPX_ABORT_CROSSOVER
   1.682 +  // ??case CPX_INForUNBD
   1.683 +  // ??case CPX_PIVOT
   1.684 +
   1.685 +  //Some more interesting stuff:
   1.686 +
   1.687 +  // CPX_PARAM_PROBMETHOD  1062  int  LPMETHOD
   1.688 +  // 0 Automatic
   1.689 +  // 1 Primal Simplex
   1.690 +  // 2 Dual Simplex
   1.691 +  // 3 Network Simplex
   1.692 +  // 4 Standard Barrier
   1.693 +  // Default: 0
   1.694 +  // Description: Method for linear optimization.
   1.695 +  // Determines which algorithm is used when CPXlpopt() (or "optimize"
   1.696 +  // in the Interactive Optimizer) is called. Currently the behavior of
   1.697 +  // the "Automatic" setting is that CPLEX simply invokes the dual
   1.698 +  // simplex method, but this capability may be expanded in the future
   1.699 +  // so that CPLEX chooses the method based on problem characteristics
   1.700 +#if CPX_VERSION < 900
   1.701 +  void statusSwitch(CPXENVptr cplexEnv(),int& stat){
   1.702 +    int lpmethod;
   1.703 +    CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod);
   1.704 +    if (lpmethod==2){
   1.705 +      if (stat==CPX_UNBOUNDED){
   1.706 +        stat=CPX_INFEASIBLE;
   1.707 +      }
   1.708 +      else{
   1.709 +        if (stat==CPX_INFEASIBLE)
   1.710 +          stat=CPX_UNBOUNDED;
   1.711 +      }
   1.712 +    }
   1.713 +  }
   1.714 +#else
   1.715 +  void statusSwitch(CPXENVptr,int&){}
   1.716 +#endif
   1.717 +
   1.718 +  CplexLp::ProblemType CplexLp::_getPrimalType() const {
   1.719 +    // Unboundedness not treated well: the following is from cplex 9.0 doc
   1.720 +    // About Unboundedness
   1.721 +
   1.722 +    // The treatment of models that are unbounded involves a few
   1.723 +    // subtleties. Specifically, a declaration of unboundedness means that
   1.724 +    // ILOG CPLEX has determined that the model has an unbounded
   1.725 +    // ray. Given any feasible solution x with objective z, a multiple of
   1.726 +    // the unbounded ray can be added to x to give a feasible solution
   1.727 +    // with objective z-1 (or z+1 for maximization models). Thus, if a
   1.728 +    // feasible solution exists, then the optimal objective is
   1.729 +    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
   1.730 +    // a feasible solution exists. Users can call the routine CPXsolninfo
   1.731 +    // to determine whether ILOG CPLEX has also concluded that the model
   1.732 +    // has a feasible solution.
   1.733 +
   1.734 +    int stat = CPXgetstat(cplexEnv(), _prob);
   1.735 +#if CPX_VERSION >= 800
   1.736 +    switch (stat)
   1.737 +      {
   1.738 +      case CPX_STAT_OPTIMAL:
   1.739 +        return OPTIMAL;
   1.740 +      case CPX_STAT_UNBOUNDED:
   1.741 +        return UNBOUNDED;
   1.742 +      case CPX_STAT_INFEASIBLE:
   1.743 +        return INFEASIBLE;
   1.744 +      default:
   1.745 +        return UNDEFINED;
   1.746 +      }
   1.747 +#else
   1.748 +    statusSwitch(cplexEnv(),stat);
   1.749 +    //CPXgetstat(cplexEnv(), _prob);
   1.750 +    switch (stat) {
   1.751 +    case 0:
   1.752 +      return UNDEFINED; //Undefined
   1.753 +    case CPX_OPTIMAL://Optimal
   1.754 +      return OPTIMAL;
   1.755 +    case CPX_UNBOUNDED://Unbounded
   1.756 +      return INFEASIBLE;//In case of dual simplex
   1.757 +      //return UNBOUNDED;
   1.758 +    case CPX_INFEASIBLE://Infeasible
   1.759 +      //    case CPX_IT_LIM_INFEAS:
   1.760 +      //     case CPX_TIME_LIM_INFEAS:
   1.761 +      //     case CPX_NUM_BEST_INFEAS:
   1.762 +      //     case CPX_OPTIMAL_INFEAS:
   1.763 +      //     case CPX_ABORT_INFEAS:
   1.764 +      //     case CPX_ABORT_PRIM_INFEAS:
   1.765 +      //     case CPX_ABORT_PRIM_DUAL_INFEAS:
   1.766 +      return UNBOUNDED;//In case of dual simplex
   1.767 +      //return INFEASIBLE;
   1.768 +      //     case CPX_OBJ_LIM:
   1.769 +      //     case CPX_IT_LIM_FEAS:
   1.770 +      //     case CPX_TIME_LIM_FEAS:
   1.771 +      //     case CPX_NUM_BEST_FEAS:
   1.772 +      //     case CPX_ABORT_FEAS:
   1.773 +      //     case CPX_ABORT_PRIM_DUAL_FEAS:
   1.774 +      //       return FEASIBLE;
   1.775 +    default:
   1.776 +      return UNDEFINED; //Everything else comes here
   1.777 +      //FIXME error
   1.778 +    }
   1.779 +#endif
   1.780 +  }
   1.781 +
   1.782 +  // Cplex 9.0 status values
   1.783 +  // CPX_STAT_ABORT_DUAL_OBJ_LIM
   1.784 +  // CPX_STAT_ABORT_IT_LIM
   1.785 +  // CPX_STAT_ABORT_OBJ_LIM
   1.786 +  // CPX_STAT_ABORT_PRIM_OBJ_LIM
   1.787 +  // CPX_STAT_ABORT_TIME_LIM
   1.788 +  // CPX_STAT_ABORT_USER
   1.789 +  // CPX_STAT_FEASIBLE_RELAXED
   1.790 +  // CPX_STAT_INFEASIBLE
   1.791 +  // CPX_STAT_INForUNBD
   1.792 +  // CPX_STAT_NUM_BEST
   1.793 +  // CPX_STAT_OPTIMAL
   1.794 +  // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
   1.795 +  // CPX_STAT_OPTIMAL_INFEAS
   1.796 +  // CPX_STAT_OPTIMAL_RELAXED
   1.797 +  // CPX_STAT_UNBOUNDED
   1.798 +
   1.799 +  CplexLp::ProblemType CplexLp::_getDualType() const {
   1.800 +    int stat = CPXgetstat(cplexEnv(), _prob);
   1.801 +#if CPX_VERSION >= 800
   1.802 +    switch (stat) {
   1.803 +    case CPX_STAT_OPTIMAL:
   1.804 +      return OPTIMAL;
   1.805 +    case CPX_STAT_UNBOUNDED:
   1.806 +      return INFEASIBLE;
   1.807 +    default:
   1.808 +      return UNDEFINED;
   1.809 +    }
   1.810 +#else
   1.811 +    statusSwitch(cplexEnv(),stat);
   1.812 +    switch (stat) {
   1.813 +    case 0:
   1.814 +      return UNDEFINED; //Undefined
   1.815 +    case CPX_OPTIMAL://Optimal
   1.816 +      return OPTIMAL;
   1.817 +    case CPX_UNBOUNDED:
   1.818 +      return INFEASIBLE;
   1.819 +    default:
   1.820 +      return UNDEFINED; //Everything else comes here
   1.821 +      //FIXME error
   1.822 +    }
   1.823 +#endif
   1.824 +  }
   1.825 +
   1.826 +  // CplexMip members
   1.827 +
   1.828 +  CplexMip::CplexMip()
   1.829 +    : LpBase(), MipSolver(), CplexBase() {
   1.830 +
   1.831 +#if CPX_VERSION < 800
   1.832 +    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
   1.833 +#else
   1.834 +    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
   1.835 +#endif
   1.836 +  }
   1.837 +
   1.838 +  CplexMip::CplexMip(const CplexEnv& env)
   1.839 +    : LpBase(), MipSolver(), CplexBase(env) {
   1.840 +
   1.841 +#if CPX_VERSION < 800
   1.842 +    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MIP);
   1.843 +#else
   1.844 +    CPXchgprobtype(cplexEnv(),  _prob, CPXPROB_MILP);
   1.845 +#endif
   1.846 +
   1.847 +  }
   1.848 +
   1.849 +  CplexMip::CplexMip(const CplexMip& other)
   1.850 +    : LpBase(), MipSolver(), CplexBase(other) {}
   1.851 +
   1.852 +  CplexMip::~CplexMip() {}
   1.853 +
   1.854 +  CplexMip* CplexMip::newSolver() const { return new CplexMip; }
   1.855 +  CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }
   1.856 +
   1.857 +  const char* CplexMip::_solverName() const { return "CplexMip"; }
   1.858 +
   1.859 +  void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
   1.860 +
   1.861 +    // Note If a variable is to be changed to binary, a call to CPXchgbds
   1.862 +    // should also be made to change the bounds to 0 and 1.
   1.863 +
   1.864 +    switch (col_type){
   1.865 +    case INTEGER: {
   1.866 +      const char t = 'I';
   1.867 +      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
   1.868 +    } break;
   1.869 +    case REAL: {
   1.870 +      const char t = 'C';
   1.871 +      CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
   1.872 +    } break;
   1.873 +    default:
   1.874 +      break;
   1.875 +    }
   1.876 +  }
   1.877 +
   1.878 +  CplexMip::ColTypes CplexMip::_getColType(int i) const {
   1.879 +    char t;
   1.880 +    CPXgetctype (cplexEnv(), _prob, &t, i, i);
   1.881 +    switch (t) {
   1.882 +    case 'I':
   1.883 +      return INTEGER;
   1.884 +    case 'C':
   1.885 +      return REAL;
   1.886 +    default:
   1.887 +      LEMON_ASSERT(false, "Invalid column type");
   1.888 +      return ColTypes();
   1.889 +    }
   1.890 +
   1.891 +  }
   1.892 +
   1.893 +  CplexMip::SolveExitStatus CplexMip::_solve() {
   1.894 +    int status;
   1.895 +    _applyMessageLevel();
   1.896 +    status = CPXmipopt (cplexEnv(), _prob);
   1.897 +    if (status==0)
   1.898 +      return SOLVED;
   1.899 +    else
   1.900 +      return UNSOLVED;
   1.901 +
   1.902 +  }
   1.903 +
   1.904 +
   1.905 +  CplexMip::ProblemType CplexMip::_getType() const {
   1.906 +
   1.907 +    int stat = CPXgetstat(cplexEnv(), _prob);
   1.908 +
   1.909 +    //Fortunately, MIP statuses did not change for cplex 8.0
   1.910 +    switch (stat) {
   1.911 +    case CPXMIP_OPTIMAL:
   1.912 +      // Optimal integer solution has been found.
   1.913 +    case CPXMIP_OPTIMAL_TOL:
   1.914 +      // Optimal soluton with the tolerance defined by epgap or epagap has
   1.915 +      // been found.
   1.916 +      return OPTIMAL;
   1.917 +      //This also exists in later issues
   1.918 +      //    case CPXMIP_UNBOUNDED:
   1.919 +      //return UNBOUNDED;
   1.920 +      case CPXMIP_INFEASIBLE:
   1.921 +        return INFEASIBLE;
   1.922 +    default:
   1.923 +      return UNDEFINED;
   1.924 +    }
   1.925 +    //Unboundedness not treated well: the following is from cplex 9.0 doc
   1.926 +    // About Unboundedness
   1.927 +
   1.928 +    // The treatment of models that are unbounded involves a few
   1.929 +    // subtleties. Specifically, a declaration of unboundedness means that
   1.930 +    // ILOG CPLEX has determined that the model has an unbounded
   1.931 +    // ray. Given any feasible solution x with objective z, a multiple of
   1.932 +    // the unbounded ray can be added to x to give a feasible solution
   1.933 +    // with objective z-1 (or z+1 for maximization models). Thus, if a
   1.934 +    // feasible solution exists, then the optimal objective is
   1.935 +    // unbounded. Note that ILOG CPLEX has not necessarily concluded that
   1.936 +    // a feasible solution exists. Users can call the routine CPXsolninfo
   1.937 +    // to determine whether ILOG CPLEX has also concluded that the model
   1.938 +    // has a feasible solution.
   1.939 +  }
   1.940 +
   1.941 +  CplexMip::Value CplexMip::_getSol(int i) const {
   1.942 +    Value x;
   1.943 +    CPXgetmipx(cplexEnv(), _prob, &x, i, i);
   1.944 +    return x;
   1.945 +  }
   1.946 +
   1.947 +  CplexMip::Value CplexMip::_getSolValue() const {
   1.948 +    Value objval;
   1.949 +    CPXgetmipobjval(cplexEnv(), _prob, &objval);
   1.950 +    return objval;
   1.951 +  }
   1.952 +
   1.953 +} //namespace lemon
   1.954 +