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