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