lemon/cbc.cc
changeset 567 3314f58e7b25
child 576 745e182d0139
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/cbc.cc	Wed Apr 01 22:58:58 2009 +0200
     1.3 @@ -0,0 +1,460 @@
     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 +///\file
    1.23 +///\brief Implementation of the CBC MIP solver interface.
    1.24 +
    1.25 +#include "cbc.h"
    1.26 +
    1.27 +#include <coin/CoinModel.hpp>
    1.28 +#include <coin/CbcModel.hpp>
    1.29 +#include <coin/OsiSolverInterface.hpp>
    1.30 +
    1.31 +#ifdef COIN_HAS_CLP
    1.32 +#include "coin/OsiClpSolverInterface.hpp"
    1.33 +#endif
    1.34 +#ifdef COIN_HAS_OSL
    1.35 +#include "coin/OsiOslSolverInterface.hpp"
    1.36 +#endif
    1.37 +
    1.38 +#include "coin/CbcCutGenerator.hpp"
    1.39 +#include "coin/CbcHeuristicLocal.hpp"
    1.40 +#include "coin/CbcHeuristicGreedy.hpp"
    1.41 +#include "coin/CbcHeuristicFPump.hpp"
    1.42 +#include "coin/CbcHeuristicRINS.hpp"
    1.43 +
    1.44 +#include "coin/CglGomory.hpp"
    1.45 +#include "coin/CglProbing.hpp"
    1.46 +#include "coin/CglKnapsackCover.hpp"
    1.47 +#include "coin/CglOddHole.hpp"
    1.48 +#include "coin/CglClique.hpp"
    1.49 +#include "coin/CglFlowCover.hpp"
    1.50 +#include "coin/CglMixedIntegerRounding.hpp"
    1.51 +
    1.52 +#include "coin/CbcHeuristic.hpp"
    1.53 +
    1.54 +namespace lemon {
    1.55 +
    1.56 +  CbcMip::CbcMip() {
    1.57 +    _prob = new CoinModel();
    1.58 +    _prob->setProblemName("LEMON");
    1.59 +    _osi_solver = 0;
    1.60 +    _cbc_model = 0;
    1.61 +  }
    1.62 +
    1.63 +  CbcMip::CbcMip(const CbcMip& other) {
    1.64 +    _prob = new CoinModel(*other._prob);
    1.65 +    _osi_solver = 0;
    1.66 +    _cbc_model = 0;
    1.67 +  }
    1.68 +
    1.69 +  CbcMip::~CbcMip() {
    1.70 +    delete _prob;
    1.71 +    if (_osi_solver) delete _osi_solver;
    1.72 +    if (_cbc_model) delete _cbc_model;
    1.73 +  }
    1.74 +
    1.75 +  const char* CbcMip::_solverName() const { return "CbcMip"; }
    1.76 +
    1.77 +  int CbcMip::_addCol() {
    1.78 +    _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
    1.79 +    return _prob->numberColumns() - 1;
    1.80 +  }
    1.81 +
    1.82 +  CbcMip* CbcMip::newSolver() const {
    1.83 +    CbcMip* newlp = new CbcMip;
    1.84 +    return newlp;
    1.85 +  }
    1.86 +
    1.87 +  CbcMip* CbcMip::cloneSolver() const {
    1.88 +    CbcMip* copylp = new CbcMip(*this);
    1.89 +    return copylp;
    1.90 +  }
    1.91 +
    1.92 +  int CbcMip::_addRow() {
    1.93 +    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
    1.94 +    return _prob->numberRows() - 1;
    1.95 +  }
    1.96 +
    1.97 +
    1.98 +  void CbcMip::_eraseCol(int i) {
    1.99 +    _prob->deleteColumn(i);
   1.100 +  }
   1.101 +
   1.102 +  void CbcMip::_eraseRow(int i) {
   1.103 +    _prob->deleteRow(i);
   1.104 +  }
   1.105 +
   1.106 +  void CbcMip::_eraseColId(int i) {
   1.107 +    cols.eraseIndex(i);
   1.108 +  }
   1.109 +
   1.110 +  void CbcMip::_eraseRowId(int i) {
   1.111 +    rows.eraseIndex(i);
   1.112 +  }
   1.113 +
   1.114 +  void CbcMip::_getColName(int c, std::string& name) const {
   1.115 +    name = _prob->getColumnName(c);
   1.116 +  }
   1.117 +
   1.118 +  void CbcMip::_setColName(int c, const std::string& name) {
   1.119 +    _prob->setColumnName(c, name.c_str());
   1.120 +  }
   1.121 +
   1.122 +  int CbcMip::_colByName(const std::string& name) const {
   1.123 +    return _prob->column(name.c_str());
   1.124 +  }
   1.125 +
   1.126 +  void CbcMip::_getRowName(int r, std::string& name) const {
   1.127 +    name = _prob->getRowName(r);
   1.128 +  }
   1.129 +
   1.130 +  void CbcMip::_setRowName(int r, const std::string& name) {
   1.131 +    _prob->setRowName(r, name.c_str());
   1.132 +  }
   1.133 +
   1.134 +  int CbcMip::_rowByName(const std::string& name) const {
   1.135 +    return _prob->row(name.c_str());
   1.136 +  }
   1.137 +
   1.138 +  void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
   1.139 +    for (ExprIterator it = b; it != e; ++it) {
   1.140 +      _prob->setElement(i, it->first, it->second);
   1.141 +    }
   1.142 +  }
   1.143 +
   1.144 +  void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
   1.145 +    int length = _prob->numberRows();
   1.146 +
   1.147 +    std::vector<int> indices(length);
   1.148 +    std::vector<Value> values(length);
   1.149 +
   1.150 +    length = _prob->getRow(ix, &indices[0], &values[0]);
   1.151 +
   1.152 +    for (int i = 0; i < length; ++i) {
   1.153 +      *b = std::make_pair(indices[i], values[i]);
   1.154 +      ++b;
   1.155 +    }
   1.156 +  }
   1.157 +
   1.158 +  void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
   1.159 +    for (ExprIterator it = b; it != e; ++it) {
   1.160 +      _prob->setElement(it->first, ix, it->second);
   1.161 +    }
   1.162 +  }
   1.163 +
   1.164 +  void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
   1.165 +    int length = _prob->numberColumns();
   1.166 +
   1.167 +    std::vector<int> indices(length);
   1.168 +    std::vector<Value> values(length);
   1.169 +
   1.170 +    length = _prob->getColumn(ix, &indices[0], &values[0]);
   1.171 +
   1.172 +    for (int i = 0; i < length; ++i) {
   1.173 +      *b = std::make_pair(indices[i], values[i]);
   1.174 +      ++b;
   1.175 +    }
   1.176 +  }
   1.177 +
   1.178 +  void CbcMip::_setCoeff(int ix, int jx, Value value) {
   1.179 +    _prob->setElement(ix, jx, value);
   1.180 +  }
   1.181 +
   1.182 +  CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
   1.183 +    return _prob->getElement(ix, jx);
   1.184 +  }
   1.185 +
   1.186 +
   1.187 +  void CbcMip::_setColLowerBound(int i, Value lo) {
   1.188 +    LEMON_ASSERT(lo != INF, "Invalid bound");
   1.189 +    _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
   1.190 +  }
   1.191 +
   1.192 +  CbcMip::Value CbcMip::_getColLowerBound(int i) const {
   1.193 +    double val = _prob->getColumnLower(i);
   1.194 +    return val == - COIN_DBL_MAX ? - INF : val;
   1.195 +  }
   1.196 +
   1.197 +  void CbcMip::_setColUpperBound(int i, Value up) {
   1.198 +    LEMON_ASSERT(up != -INF, "Invalid bound");
   1.199 +    _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
   1.200 +  }
   1.201 +
   1.202 +  CbcMip::Value CbcMip::_getColUpperBound(int i) const {
   1.203 +    double val = _prob->getColumnUpper(i);
   1.204 +    return val == COIN_DBL_MAX ? INF : val;
   1.205 +  }
   1.206 +
   1.207 +  void CbcMip::_setRowLowerBound(int i, Value lo) {
   1.208 +    LEMON_ASSERT(lo != INF, "Invalid bound");
   1.209 +    _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
   1.210 +  }
   1.211 +
   1.212 +  CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
   1.213 +    double val = _prob->getRowLower(i);
   1.214 +    return val == - COIN_DBL_MAX ? - INF : val;
   1.215 +  }
   1.216 +
   1.217 +  void CbcMip::_setRowUpperBound(int i, Value up) {
   1.218 +    LEMON_ASSERT(up != -INF, "Invalid bound");
   1.219 +    _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
   1.220 +  }
   1.221 +
   1.222 +  CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
   1.223 +    double val = _prob->getRowUpper(i);
   1.224 +    return val == COIN_DBL_MAX ? INF : val;
   1.225 +  }
   1.226 +
   1.227 +  void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
   1.228 +    int num = _prob->numberColumns();
   1.229 +    for (int i = 0; i < num; ++i) {
   1.230 +      _prob->setColumnObjective(i, 0.0);
   1.231 +    }
   1.232 +    for (ExprIterator it = b; it != e; ++it) {
   1.233 +      _prob->setColumnObjective(it->first, it->second);
   1.234 +    }
   1.235 +  }
   1.236 +
   1.237 +  void CbcMip::_getObjCoeffs(InsertIterator b) const {
   1.238 +    int num = _prob->numberColumns();
   1.239 +    for (int i = 0; i < num; ++i) {
   1.240 +      Value coef = _prob->getColumnObjective(i);
   1.241 +      if (coef != 0.0) {
   1.242 +        *b = std::make_pair(i, coef);
   1.243 +        ++b;
   1.244 +      }
   1.245 +    }
   1.246 +  }
   1.247 +
   1.248 +  void CbcMip::_setObjCoeff(int i, Value obj_coef) {
   1.249 +    _prob->setColumnObjective(i, obj_coef);
   1.250 +  }
   1.251 +
   1.252 +  CbcMip::Value CbcMip::_getObjCoeff(int i) const {
   1.253 +    return _prob->getColumnObjective(i);
   1.254 +  }
   1.255 +
   1.256 +  CbcMip::SolveExitStatus CbcMip::_solve() {
   1.257 +
   1.258 +    if (_osi_solver) {
   1.259 +      delete _osi_solver;
   1.260 +    }
   1.261 +#ifdef COIN_HAS_CLP
   1.262 +    _osi_solver = new OsiClpSolverInterface();
   1.263 +#elif COIN_HAS_OSL
   1.264 +    _osi_solver = new OsiOslSolverInterface();
   1.265 +#else
   1.266 +#error Cannot instantiate Osi solver
   1.267 +#endif
   1.268 +
   1.269 +    _osi_solver->loadFromCoinModel(*_prob);
   1.270 +
   1.271 +    if (_cbc_model) {
   1.272 +      delete _cbc_model;
   1.273 +    }
   1.274 +    _cbc_model= new CbcModel(*_osi_solver);
   1.275 +
   1.276 +    switch (_message_level) {
   1.277 +    case MESSAGE_NO_OUTPUT:
   1.278 +      _osi_solver->messageHandler()->setLogLevel(0);
   1.279 +      _cbc_model->setLogLevel(0);
   1.280 +      break;
   1.281 +    case MESSAGE_ERROR_MESSAGE:
   1.282 +      _osi_solver->messageHandler()->setLogLevel(1);
   1.283 +      _cbc_model->setLogLevel(1);
   1.284 +      break;
   1.285 +    case MESSAGE_NORMAL_OUTPUT:
   1.286 +      _osi_solver->messageHandler()->setLogLevel(2);
   1.287 +      _cbc_model->setLogLevel(2);
   1.288 +      break;
   1.289 +    case MESSAGE_FULL_OUTPUT:
   1.290 +      _osi_solver->messageHandler()->setLogLevel(3);
   1.291 +      _cbc_model->setLogLevel(3);
   1.292 +      break;
   1.293 +    }
   1.294 +
   1.295 +    _cbc_model->initialSolve();
   1.296 +    _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
   1.297 +
   1.298 +    if (!_cbc_model->isInitialSolveAbandoned() &&
   1.299 +        _cbc_model->isInitialSolveProvenOptimal() &&
   1.300 +        !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
   1.301 +        !_cbc_model->isInitialSolveProvenDualInfeasible()) {
   1.302 +
   1.303 +      CglProbing generator1;
   1.304 +      generator1.setUsingObjective(true);
   1.305 +      generator1.setMaxPass(3);
   1.306 +      generator1.setMaxProbe(100);
   1.307 +      generator1.setMaxLook(50);
   1.308 +      generator1.setRowCuts(3);
   1.309 +      _cbc_model->addCutGenerator(&generator1, -1, "Probing");
   1.310 +
   1.311 +      CglGomory generator2;
   1.312 +      generator2.setLimit(300);
   1.313 +      _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
   1.314 +
   1.315 +      CglKnapsackCover generator3;
   1.316 +      _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
   1.317 +
   1.318 +      CglOddHole generator4;
   1.319 +      generator4.setMinimumViolation(0.005);
   1.320 +      generator4.setMinimumViolationPer(0.00002);
   1.321 +      generator4.setMaximumEntries(200);
   1.322 +      _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
   1.323 +
   1.324 +      CglClique generator5;
   1.325 +      generator5.setStarCliqueReport(false);
   1.326 +      generator5.setRowCliqueReport(false);
   1.327 +      _cbc_model->addCutGenerator(&generator5, -1, "Clique");
   1.328 +
   1.329 +      CglMixedIntegerRounding mixedGen;
   1.330 +      _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
   1.331 +
   1.332 +      CglFlowCover flowGen;
   1.333 +      _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
   1.334 +
   1.335 +#ifdef COIN_HAS_CLP
   1.336 +      OsiClpSolverInterface* osiclp =
   1.337 +        dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
   1.338 +      if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
   1.339 +        osiclp->setupForRepeatedUse(2, 0);
   1.340 +      }
   1.341 +#endif
   1.342 +
   1.343 +      CbcRounding heuristic1(*_cbc_model);
   1.344 +      heuristic1.setWhen(3);
   1.345 +      _cbc_model->addHeuristic(&heuristic1);
   1.346 +
   1.347 +      CbcHeuristicLocal heuristic2(*_cbc_model);
   1.348 +      heuristic2.setWhen(3);
   1.349 +      _cbc_model->addHeuristic(&heuristic2);
   1.350 +
   1.351 +      CbcHeuristicGreedyCover heuristic3(*_cbc_model);
   1.352 +      heuristic3.setAlgorithm(11);
   1.353 +      heuristic3.setWhen(3);
   1.354 +      _cbc_model->addHeuristic(&heuristic3);
   1.355 +
   1.356 +      CbcHeuristicFPump heuristic4(*_cbc_model);
   1.357 +      heuristic4.setWhen(3);
   1.358 +      _cbc_model->addHeuristic(&heuristic4);
   1.359 +
   1.360 +      CbcHeuristicRINS heuristic5(*_cbc_model);
   1.361 +      heuristic5.setWhen(3);
   1.362 +      _cbc_model->addHeuristic(&heuristic5);
   1.363 +
   1.364 +      if (_cbc_model->getNumCols() < 500) {
   1.365 +        _cbc_model->setMaximumCutPassesAtRoot(-100);
   1.366 +      } else if (_cbc_model->getNumCols() < 5000) {
   1.367 +        _cbc_model->setMaximumCutPassesAtRoot(100);
   1.368 +      } else {
   1.369 +        _cbc_model->setMaximumCutPassesAtRoot(20);
   1.370 +      }
   1.371 +
   1.372 +      if (_cbc_model->getNumCols() < 5000) {
   1.373 +        _cbc_model->setNumberStrong(10);
   1.374 +      }
   1.375 +
   1.376 +      _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
   1.377 +      _cbc_model->branchAndBound();
   1.378 +    }
   1.379 +
   1.380 +    if (_cbc_model->isAbandoned()) {
   1.381 +      return UNSOLVED;
   1.382 +    } else {
   1.383 +      return SOLVED;
   1.384 +    }
   1.385 +  }
   1.386 +
   1.387 +  CbcMip::Value CbcMip::_getSol(int i) const {
   1.388 +    return _cbc_model->getColSolution()[i];
   1.389 +  }
   1.390 +
   1.391 +  CbcMip::Value CbcMip::_getSolValue() const {
   1.392 +    return _cbc_model->getObjValue();
   1.393 +  }
   1.394 +
   1.395 +  CbcMip::ProblemType CbcMip::_getType() const {
   1.396 +    if (_cbc_model->isProvenOptimal()) {
   1.397 +      return OPTIMAL;
   1.398 +    } else if (_cbc_model->isContinuousUnbounded()) {
   1.399 +      return UNBOUNDED;
   1.400 +    }
   1.401 +    return FEASIBLE;
   1.402 +  }
   1.403 +
   1.404 +  void CbcMip::_setSense(Sense sense) {
   1.405 +    switch (sense) {
   1.406 +    case MIN:
   1.407 +      _prob->setOptimizationDirection(1.0);
   1.408 +      break;
   1.409 +    case MAX:
   1.410 +      _prob->setOptimizationDirection(- 1.0);
   1.411 +      break;
   1.412 +    }
   1.413 +  }
   1.414 +
   1.415 +  CbcMip::Sense CbcMip::_getSense() const {
   1.416 +    if (_prob->optimizationDirection() > 0.0) {
   1.417 +      return MIN;
   1.418 +    } else if (_prob->optimizationDirection() < 0.0) {
   1.419 +      return MAX;
   1.420 +    } else {
   1.421 +      LEMON_ASSERT(false, "Wrong sense");
   1.422 +      return CbcMip::Sense();
   1.423 +    }
   1.424 +  }
   1.425 +
   1.426 +  void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
   1.427 +    switch (col_type){
   1.428 +    case INTEGER:
   1.429 +      _prob->setInteger(i);
   1.430 +      break;
   1.431 +    case REAL:
   1.432 +      _prob->setContinuous(i);
   1.433 +      break;
   1.434 +    default:;
   1.435 +      LEMON_ASSERT(false, "Wrong sense");
   1.436 +    }
   1.437 +  }
   1.438 +
   1.439 +  CbcMip::ColTypes CbcMip::_getColType(int i) const {
   1.440 +    return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
   1.441 +  }
   1.442 +
   1.443 +  void CbcMip::_clear() {
   1.444 +    delete _prob;
   1.445 +    if (_osi_solver) {
   1.446 +      delete _osi_solver;
   1.447 +      _osi_solver = 0;
   1.448 +    }
   1.449 +    if (_cbc_model) {
   1.450 +      delete _cbc_model;
   1.451 +      _cbc_model = 0;
   1.452 +    }
   1.453 +
   1.454 +    _prob = new CoinModel();
   1.455 +    rows.clear();
   1.456 +    cols.clear();
   1.457 +  }
   1.458 +
   1.459 +  void CbcMip::messageLevel(MessageLevel m) {
   1.460 +    _message_level = m;
   1.461 +  }
   1.462 +
   1.463 +} //END OF NAMESPACE LEMON