lemon/clp.cc
changeset 784 1a7fe3bef514
parent 576 745e182d0139
child 877 141f9c0db4a3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/clp.cc	Thu Nov 05 15:50:01 2009 +0100
     1.3 @@ -0,0 +1,466 @@
     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-2008
     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 <lemon/clp.h>
    1.23 +#include <coin/ClpSimplex.hpp>
    1.24 +
    1.25 +namespace lemon {
    1.26 +
    1.27 +  ClpLp::ClpLp() {
    1.28 +    _prob = new ClpSimplex();
    1.29 +    _init_temporals();
    1.30 +    messageLevel(MESSAGE_NOTHING);
    1.31 +  }
    1.32 +
    1.33 +  ClpLp::ClpLp(const ClpLp& other) {
    1.34 +    _prob = new ClpSimplex(*other._prob);
    1.35 +    rows = other.rows;
    1.36 +    cols = other.cols;
    1.37 +    _init_temporals();
    1.38 +    messageLevel(MESSAGE_NOTHING);
    1.39 +  }
    1.40 +
    1.41 +  ClpLp::~ClpLp() {
    1.42 +    delete _prob;
    1.43 +    _clear_temporals();
    1.44 +  }
    1.45 +
    1.46 +  void ClpLp::_init_temporals() {
    1.47 +    _primal_ray = 0;
    1.48 +    _dual_ray = 0;
    1.49 +  }
    1.50 +
    1.51 +  void ClpLp::_clear_temporals() {
    1.52 +    if (_primal_ray) {
    1.53 +      delete[] _primal_ray;
    1.54 +      _primal_ray = 0;
    1.55 +    }
    1.56 +    if (_dual_ray) {
    1.57 +      delete[] _dual_ray;
    1.58 +      _dual_ray = 0;
    1.59 +    }
    1.60 +  }
    1.61 +
    1.62 +  ClpLp* ClpLp::newSolver() const {
    1.63 +    ClpLp* newlp = new ClpLp;
    1.64 +    return newlp;
    1.65 +  }
    1.66 +
    1.67 +  ClpLp* ClpLp::cloneSolver() const {
    1.68 +    ClpLp* copylp = new ClpLp(*this);
    1.69 +    return copylp;
    1.70 +  }
    1.71 +
    1.72 +  const char* ClpLp::_solverName() const { return "ClpLp"; }
    1.73 +
    1.74 +  int ClpLp::_addCol() {
    1.75 +    _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0);
    1.76 +    return _prob->numberColumns() - 1;
    1.77 +  }
    1.78 +
    1.79 +  int ClpLp::_addRow() {
    1.80 +    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
    1.81 +    return _prob->numberRows() - 1;
    1.82 +  }
    1.83 +
    1.84 +  int ClpLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
    1.85 +    std::vector<int> indexes;
    1.86 +    std::vector<Value> values;
    1.87 +
    1.88 +    for(ExprIterator it = b; it != e; ++it) {
    1.89 +      indexes.push_back(it->first);
    1.90 +      values.push_back(it->second);
    1.91 +    }
    1.92 +
    1.93 +    _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
    1.94 +    return _prob->numberRows() - 1;
    1.95 +  }
    1.96 +
    1.97 +
    1.98 +  void ClpLp::_eraseCol(int c) {
    1.99 +    _col_names_ref.erase(_prob->getColumnName(c));
   1.100 +    _prob->deleteColumns(1, &c);
   1.101 +  }
   1.102 +
   1.103 +  void ClpLp::_eraseRow(int r) {
   1.104 +    _row_names_ref.erase(_prob->getRowName(r));
   1.105 +    _prob->deleteRows(1, &r);
   1.106 +  }
   1.107 +
   1.108 +  void ClpLp::_eraseColId(int i) {
   1.109 +    cols.eraseIndex(i);
   1.110 +    cols.shiftIndices(i);
   1.111 +  }
   1.112 +
   1.113 +  void ClpLp::_eraseRowId(int i) {
   1.114 +    rows.eraseIndex(i);
   1.115 +    rows.shiftIndices(i);
   1.116 +  }
   1.117 +
   1.118 +  void ClpLp::_getColName(int c, std::string& name) const {
   1.119 +    name = _prob->getColumnName(c);
   1.120 +  }
   1.121 +
   1.122 +  void ClpLp::_setColName(int c, const std::string& name) {
   1.123 +    _prob->setColumnName(c, const_cast<std::string&>(name));
   1.124 +    _col_names_ref[name] = c;
   1.125 +  }
   1.126 +
   1.127 +  int ClpLp::_colByName(const std::string& name) const {
   1.128 +    std::map<std::string, int>::const_iterator it = _col_names_ref.find(name);
   1.129 +    return it != _col_names_ref.end() ? it->second : -1;
   1.130 +  }
   1.131 +
   1.132 +  void ClpLp::_getRowName(int r, std::string& name) const {
   1.133 +    name = _prob->getRowName(r);
   1.134 +  }
   1.135 +
   1.136 +  void ClpLp::_setRowName(int r, const std::string& name) {
   1.137 +    _prob->setRowName(r, const_cast<std::string&>(name));
   1.138 +    _row_names_ref[name] = r;
   1.139 +  }
   1.140 +
   1.141 +  int ClpLp::_rowByName(const std::string& name) const {
   1.142 +    std::map<std::string, int>::const_iterator it = _row_names_ref.find(name);
   1.143 +    return it != _row_names_ref.end() ? it->second : -1;
   1.144 +  }
   1.145 +
   1.146 +
   1.147 +  void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
   1.148 +    std::map<int, Value> coeffs;
   1.149 +
   1.150 +    int n = _prob->clpMatrix()->getNumCols();
   1.151 +
   1.152 +    const int* indices = _prob->clpMatrix()->getIndices();
   1.153 +    const double* elements = _prob->clpMatrix()->getElements();
   1.154 +
   1.155 +    for (int i = 0; i < n; ++i) {
   1.156 +      CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
   1.157 +      CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
   1.158 +
   1.159 +      const int* it = std::lower_bound(indices + begin, indices + end, ix);
   1.160 +      if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
   1.161 +        coeffs[i] = 0.0;
   1.162 +      }
   1.163 +    }
   1.164 +
   1.165 +    for (ExprIterator it = b; it != e; ++it) {
   1.166 +      coeffs[it->first] = it->second;
   1.167 +    }
   1.168 +
   1.169 +    for (std::map<int, Value>::iterator it = coeffs.begin();
   1.170 +         it != coeffs.end(); ++it) {
   1.171 +      _prob->modifyCoefficient(ix, it->first, it->second);
   1.172 +    }
   1.173 +  }
   1.174 +
   1.175 +  void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const {
   1.176 +    int n = _prob->clpMatrix()->getNumCols();
   1.177 +
   1.178 +    const int* indices = _prob->clpMatrix()->getIndices();
   1.179 +    const double* elements = _prob->clpMatrix()->getElements();
   1.180 +
   1.181 +    for (int i = 0; i < n; ++i) {
   1.182 +      CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
   1.183 +      CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
   1.184 +
   1.185 +      const int* it = std::lower_bound(indices + begin, indices + end, ix);
   1.186 +      if (it != indices + end && *it == ix) {
   1.187 +        *b = std::make_pair(i, elements[it - indices]);
   1.188 +      }
   1.189 +    }
   1.190 +  }
   1.191 +
   1.192 +  void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
   1.193 +    std::map<int, Value> coeffs;
   1.194 +
   1.195 +    CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
   1.196 +    CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
   1.197 +
   1.198 +    const int* indices = _prob->clpMatrix()->getIndices();
   1.199 +    const double* elements = _prob->clpMatrix()->getElements();
   1.200 +
   1.201 +    for (CoinBigIndex i = begin; i != end; ++i) {
   1.202 +      if (elements[i] != 0.0) {
   1.203 +        coeffs[indices[i]] = 0.0;
   1.204 +      }
   1.205 +    }
   1.206 +    for (ExprIterator it = b; it != e; ++it) {
   1.207 +      coeffs[it->first] = it->second;
   1.208 +    }
   1.209 +    for (std::map<int, Value>::iterator it = coeffs.begin();
   1.210 +         it != coeffs.end(); ++it) {
   1.211 +      _prob->modifyCoefficient(it->first, ix, it->second);
   1.212 +    }
   1.213 +  }
   1.214 +
   1.215 +  void ClpLp::_getColCoeffs(int ix, InsertIterator b) const {
   1.216 +    CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
   1.217 +    CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
   1.218 +
   1.219 +    const int* indices = _prob->clpMatrix()->getIndices();
   1.220 +    const double* elements = _prob->clpMatrix()->getElements();
   1.221 +
   1.222 +    for (CoinBigIndex i = begin; i != end; ++i) {
   1.223 +      *b = std::make_pair(indices[i], elements[i]);
   1.224 +      ++b;
   1.225 +    }
   1.226 +  }
   1.227 +
   1.228 +  void ClpLp::_setCoeff(int ix, int jx, Value value) {
   1.229 +    _prob->modifyCoefficient(ix, jx, value);
   1.230 +  }
   1.231 +
   1.232 +  ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const {
   1.233 +    CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
   1.234 +    CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
   1.235 +
   1.236 +    const int* indices = _prob->clpMatrix()->getIndices();
   1.237 +    const double* elements = _prob->clpMatrix()->getElements();
   1.238 +
   1.239 +    const int* it = std::lower_bound(indices + begin, indices + end, jx);
   1.240 +    if (it != indices + end && *it == jx) {
   1.241 +      return elements[it - indices];
   1.242 +    } else {
   1.243 +      return 0.0;
   1.244 +    }
   1.245 +  }
   1.246 +
   1.247 +  void ClpLp::_setColLowerBound(int i, Value lo) {
   1.248 +    _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
   1.249 +  }
   1.250 +
   1.251 +  ClpLp::Value ClpLp::_getColLowerBound(int i) const {
   1.252 +    double val = _prob->getColLower()[i];
   1.253 +    return val == - COIN_DBL_MAX ? - INF : val;
   1.254 +  }
   1.255 +
   1.256 +  void ClpLp::_setColUpperBound(int i, Value up) {
   1.257 +    _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
   1.258 +  }
   1.259 +
   1.260 +  ClpLp::Value ClpLp::_getColUpperBound(int i) const {
   1.261 +    double val = _prob->getColUpper()[i];
   1.262 +    return val == COIN_DBL_MAX ? INF : val;
   1.263 +  }
   1.264 +
   1.265 +  void ClpLp::_setRowLowerBound(int i, Value lo) {
   1.266 +    _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
   1.267 +  }
   1.268 +
   1.269 +  ClpLp::Value ClpLp::_getRowLowerBound(int i) const {
   1.270 +    double val = _prob->getRowLower()[i];
   1.271 +    return val == - COIN_DBL_MAX ? - INF : val;
   1.272 +  }
   1.273 +
   1.274 +  void ClpLp::_setRowUpperBound(int i, Value up) {
   1.275 +    _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
   1.276 +  }
   1.277 +
   1.278 +  ClpLp::Value ClpLp::_getRowUpperBound(int i) const {
   1.279 +    double val = _prob->getRowUpper()[i];
   1.280 +    return val == COIN_DBL_MAX ? INF : val;
   1.281 +  }
   1.282 +
   1.283 +  void ClpLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
   1.284 +    int num = _prob->clpMatrix()->getNumCols();
   1.285 +    for (int i = 0; i < num; ++i) {
   1.286 +      _prob->setObjectiveCoefficient(i, 0.0);
   1.287 +    }
   1.288 +    for (ExprIterator it = b; it != e; ++it) {
   1.289 +      _prob->setObjectiveCoefficient(it->first, it->second);
   1.290 +    }
   1.291 +  }
   1.292 +
   1.293 +  void ClpLp::_getObjCoeffs(InsertIterator b) const {
   1.294 +    int num = _prob->clpMatrix()->getNumCols();
   1.295 +    for (int i = 0; i < num; ++i) {
   1.296 +      Value coef = _prob->getObjCoefficients()[i];
   1.297 +      if (coef != 0.0) {
   1.298 +        *b = std::make_pair(i, coef);
   1.299 +        ++b;
   1.300 +      }
   1.301 +    }
   1.302 +  }
   1.303 +
   1.304 +  void ClpLp::_setObjCoeff(int i, Value obj_coef) {
   1.305 +    _prob->setObjectiveCoefficient(i, obj_coef);
   1.306 +  }
   1.307 +
   1.308 +  ClpLp::Value ClpLp::_getObjCoeff(int i) const {
   1.309 +    return _prob->getObjCoefficients()[i];
   1.310 +  }
   1.311 +
   1.312 +  ClpLp::SolveExitStatus ClpLp::_solve() {
   1.313 +    return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
   1.314 +  }
   1.315 +
   1.316 +  ClpLp::SolveExitStatus ClpLp::solvePrimal() {
   1.317 +    return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
   1.318 +  }
   1.319 +
   1.320 +  ClpLp::SolveExitStatus ClpLp::solveDual() {
   1.321 +    return _prob->dual() >= 0 ? SOLVED : UNSOLVED;
   1.322 +  }
   1.323 +
   1.324 +  ClpLp::SolveExitStatus ClpLp::solveBarrier() {
   1.325 +    return _prob->barrier() >= 0 ? SOLVED : UNSOLVED;
   1.326 +  }
   1.327 +
   1.328 +  ClpLp::Value ClpLp::_getPrimal(int i) const {
   1.329 +    return _prob->primalColumnSolution()[i];
   1.330 +  }
   1.331 +  ClpLp::Value ClpLp::_getPrimalValue() const {
   1.332 +    return _prob->objectiveValue();
   1.333 +  }
   1.334 +
   1.335 +  ClpLp::Value ClpLp::_getDual(int i) const {
   1.336 +    return _prob->dualRowSolution()[i];
   1.337 +  }
   1.338 +
   1.339 +  ClpLp::Value ClpLp::_getPrimalRay(int i) const {
   1.340 +    if (!_primal_ray) {
   1.341 +      _primal_ray = _prob->unboundedRay();
   1.342 +      LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided");
   1.343 +    }
   1.344 +    return _primal_ray[i];
   1.345 +  }
   1.346 +
   1.347 +  ClpLp::Value ClpLp::_getDualRay(int i) const {
   1.348 +    if (!_dual_ray) {
   1.349 +      _dual_ray = _prob->infeasibilityRay();
   1.350 +      LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided");
   1.351 +    }
   1.352 +    return _dual_ray[i];
   1.353 +  }
   1.354 +
   1.355 +  ClpLp::VarStatus ClpLp::_getColStatus(int i) const {
   1.356 +    switch (_prob->getColumnStatus(i)) {
   1.357 +    case ClpSimplex::basic:
   1.358 +      return BASIC;
   1.359 +    case ClpSimplex::isFree:
   1.360 +      return FREE;
   1.361 +    case ClpSimplex::atUpperBound:
   1.362 +      return UPPER;
   1.363 +    case ClpSimplex::atLowerBound:
   1.364 +      return LOWER;
   1.365 +    case ClpSimplex::isFixed:
   1.366 +      return FIXED;
   1.367 +    case ClpSimplex::superBasic:
   1.368 +      return FREE;
   1.369 +    default:
   1.370 +      LEMON_ASSERT(false, "Wrong column status");
   1.371 +      return VarStatus();
   1.372 +    }
   1.373 +  }
   1.374 +
   1.375 +  ClpLp::VarStatus ClpLp::_getRowStatus(int i) const {
   1.376 +    switch (_prob->getColumnStatus(i)) {
   1.377 +    case ClpSimplex::basic:
   1.378 +      return BASIC;
   1.379 +    case ClpSimplex::isFree:
   1.380 +      return FREE;
   1.381 +    case ClpSimplex::atUpperBound:
   1.382 +      return UPPER;
   1.383 +    case ClpSimplex::atLowerBound:
   1.384 +      return LOWER;
   1.385 +    case ClpSimplex::isFixed:
   1.386 +      return FIXED;
   1.387 +    case ClpSimplex::superBasic:
   1.388 +      return FREE;
   1.389 +    default:
   1.390 +      LEMON_ASSERT(false, "Wrong row status");
   1.391 +      return VarStatus();
   1.392 +    }
   1.393 +  }
   1.394 +
   1.395 +
   1.396 +  ClpLp::ProblemType ClpLp::_getPrimalType() const {
   1.397 +    if (_prob->isProvenOptimal()) {
   1.398 +      return OPTIMAL;
   1.399 +    } else if (_prob->isProvenPrimalInfeasible()) {
   1.400 +      return INFEASIBLE;
   1.401 +    } else if (_prob->isProvenDualInfeasible()) {
   1.402 +      return UNBOUNDED;
   1.403 +    } else {
   1.404 +      return UNDEFINED;
   1.405 +    }
   1.406 +  }
   1.407 +
   1.408 +  ClpLp::ProblemType ClpLp::_getDualType() const {
   1.409 +    if (_prob->isProvenOptimal()) {
   1.410 +      return OPTIMAL;
   1.411 +    } else if (_prob->isProvenDualInfeasible()) {
   1.412 +      return INFEASIBLE;
   1.413 +    } else if (_prob->isProvenPrimalInfeasible()) {
   1.414 +      return INFEASIBLE;
   1.415 +    } else {
   1.416 +      return UNDEFINED;
   1.417 +    }
   1.418 +  }
   1.419 +
   1.420 +  void ClpLp::_setSense(ClpLp::Sense sense) {
   1.421 +    switch (sense) {
   1.422 +    case MIN:
   1.423 +      _prob->setOptimizationDirection(1);
   1.424 +      break;
   1.425 +    case MAX:
   1.426 +      _prob->setOptimizationDirection(-1);
   1.427 +      break;
   1.428 +    }
   1.429 +  }
   1.430 +
   1.431 +  ClpLp::Sense ClpLp::_getSense() const {
   1.432 +    double dir = _prob->optimizationDirection();
   1.433 +    if (dir > 0.0) {
   1.434 +      return MIN;
   1.435 +    } else {
   1.436 +      return MAX;
   1.437 +    }
   1.438 +  }
   1.439 +
   1.440 +  void ClpLp::_clear() {
   1.441 +    delete _prob;
   1.442 +    _prob = new ClpSimplex();
   1.443 +    rows.clear();
   1.444 +    cols.clear();
   1.445 +    _col_names_ref.clear();
   1.446 +    _clear_temporals();
   1.447 +  }
   1.448 +
   1.449 +  void ClpLp::_messageLevel(MessageLevel level) {
   1.450 +    switch (level) {
   1.451 +    case MESSAGE_NOTHING:
   1.452 +      _prob->setLogLevel(0);
   1.453 +      break;
   1.454 +    case MESSAGE_ERROR:
   1.455 +      _prob->setLogLevel(1);
   1.456 +      break;
   1.457 +    case MESSAGE_WARNING:
   1.458 +      _prob->setLogLevel(2);
   1.459 +      break;
   1.460 +    case MESSAGE_NORMAL:
   1.461 +      _prob->setLogLevel(3);
   1.462 +      break;
   1.463 +    case MESSAGE_VERBOSE:
   1.464 +      _prob->setLogLevel(4);
   1.465 +      break;
   1.466 +    }
   1.467 +  }
   1.468 +
   1.469 +} //END OF NAMESPACE LEMON