1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    20 ///\brief Implementation of the CBC MIP solver interface.
 
    24 #include <coin/CoinModel.hpp>
 
    25 #include <coin/CbcModel.hpp>
 
    26 #include <coin/OsiSolverInterface.hpp>
 
    29 #include "coin/OsiClpSolverInterface.hpp"
 
    32 #include "coin/OsiOslSolverInterface.hpp"
 
    35 #include "coin/CbcCutGenerator.hpp"
 
    36 #include "coin/CbcHeuristicLocal.hpp"
 
    37 #include "coin/CbcHeuristicGreedy.hpp"
 
    38 #include "coin/CbcHeuristicFPump.hpp"
 
    39 #include "coin/CbcHeuristicRINS.hpp"
 
    41 #include "coin/CglGomory.hpp"
 
    42 #include "coin/CglProbing.hpp"
 
    43 #include "coin/CglKnapsackCover.hpp"
 
    44 #include "coin/CglOddHole.hpp"
 
    45 #include "coin/CglClique.hpp"
 
    46 #include "coin/CglFlowCover.hpp"
 
    47 #include "coin/CglMixedIntegerRounding.hpp"
 
    49 #include "coin/CbcHeuristic.hpp"
 
    54     _prob = new CoinModel();
 
    55     _prob->setProblemName("LEMON");
 
    58     messageLevel(MESSAGE_NOTHING);
 
    61   CbcMip::CbcMip(const CbcMip& other) {
 
    62     _prob = new CoinModel(*other._prob);
 
    63     _prob->setProblemName("LEMON");
 
    66     messageLevel(MESSAGE_NOTHING);
 
    71     if (_osi_solver) delete _osi_solver;
 
    72     if (_cbc_model) delete _cbc_model;
 
    75   const char* CbcMip::_solverName() const { return "CbcMip"; }
 
    77   int CbcMip::_addCol() {
 
    78     _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
 
    79     return _prob->numberColumns() - 1;
 
    82   CbcMip* CbcMip::newSolver() const {
 
    83     CbcMip* newlp = new CbcMip;
 
    87   CbcMip* CbcMip::cloneSolver() const {
 
    88     CbcMip* copylp = new CbcMip(*this);
 
    92   int CbcMip::_addRow() {
 
    93     _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
 
    94     return _prob->numberRows() - 1;
 
    98   void CbcMip::_eraseCol(int i) {
 
    99     _prob->deleteColumn(i);
 
   102   void CbcMip::_eraseRow(int i) {
 
   106   void CbcMip::_eraseColId(int i) {
 
   110   void CbcMip::_eraseRowId(int i) {
 
   114   void CbcMip::_getColName(int c, std::string& name) const {
 
   115     name = _prob->getColumnName(c);
 
   118   void CbcMip::_setColName(int c, const std::string& name) {
 
   119     _prob->setColumnName(c, name.c_str());
 
   122   int CbcMip::_colByName(const std::string& name) const {
 
   123     return _prob->column(name.c_str());
 
   126   void CbcMip::_getRowName(int r, std::string& name) const {
 
   127     name = _prob->getRowName(r);
 
   130   void CbcMip::_setRowName(int r, const std::string& name) {
 
   131     _prob->setRowName(r, name.c_str());
 
   134   int CbcMip::_rowByName(const std::string& name) const {
 
   135     return _prob->row(name.c_str());
 
   138   void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
 
   139     for (ExprIterator it = b; it != e; ++it) {
 
   140       _prob->setElement(i, it->first, it->second);
 
   144   void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
 
   145     int length = _prob->numberRows();
 
   147     std::vector<int> indices(length);
 
   148     std::vector<Value> values(length);
 
   150     length = _prob->getRow(ix, &indices[0], &values[0]);
 
   152     for (int i = 0; i < length; ++i) {
 
   153       *b = std::make_pair(indices[i], values[i]);
 
   158   void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
 
   159     for (ExprIterator it = b; it != e; ++it) {
 
   160       _prob->setElement(it->first, ix, it->second);
 
   164   void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
 
   165     int length = _prob->numberColumns();
 
   167     std::vector<int> indices(length);
 
   168     std::vector<Value> values(length);
 
   170     length = _prob->getColumn(ix, &indices[0], &values[0]);
 
   172     for (int i = 0; i < length; ++i) {
 
   173       *b = std::make_pair(indices[i], values[i]);
 
   178   void CbcMip::_setCoeff(int ix, int jx, Value value) {
 
   179     _prob->setElement(ix, jx, value);
 
   182   CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
 
   183     return _prob->getElement(ix, jx);
 
   187   void CbcMip::_setColLowerBound(int i, Value lo) {
 
   188     LEMON_ASSERT(lo != INF, "Invalid bound");
 
   189     _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
 
   192   CbcMip::Value CbcMip::_getColLowerBound(int i) const {
 
   193     double val = _prob->getColumnLower(i);
 
   194     return val == - COIN_DBL_MAX ? - INF : val;
 
   197   void CbcMip::_setColUpperBound(int i, Value up) {
 
   198     LEMON_ASSERT(up != -INF, "Invalid bound");
 
   199     _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
 
   202   CbcMip::Value CbcMip::_getColUpperBound(int i) const {
 
   203     double val = _prob->getColumnUpper(i);
 
   204     return val == COIN_DBL_MAX ? INF : val;
 
   207   void CbcMip::_setRowLowerBound(int i, Value lo) {
 
   208     LEMON_ASSERT(lo != INF, "Invalid bound");
 
   209     _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
 
   212   CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
 
   213     double val = _prob->getRowLower(i);
 
   214     return val == - COIN_DBL_MAX ? - INF : val;
 
   217   void CbcMip::_setRowUpperBound(int i, Value up) {
 
   218     LEMON_ASSERT(up != -INF, "Invalid bound");
 
   219     _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
 
   222   CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
 
   223     double val = _prob->getRowUpper(i);
 
   224     return val == COIN_DBL_MAX ? INF : val;
 
   227   void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
 
   228     int num = _prob->numberColumns();
 
   229     for (int i = 0; i < num; ++i) {
 
   230       _prob->setColumnObjective(i, 0.0);
 
   232     for (ExprIterator it = b; it != e; ++it) {
 
   233       _prob->setColumnObjective(it->first, it->second);
 
   237   void CbcMip::_getObjCoeffs(InsertIterator b) const {
 
   238     int num = _prob->numberColumns();
 
   239     for (int i = 0; i < num; ++i) {
 
   240       Value coef = _prob->getColumnObjective(i);
 
   242         *b = std::make_pair(i, coef);
 
   248   void CbcMip::_setObjCoeff(int i, Value obj_coef) {
 
   249     _prob->setColumnObjective(i, obj_coef);
 
   252   CbcMip::Value CbcMip::_getObjCoeff(int i) const {
 
   253     return _prob->getColumnObjective(i);
 
   256   CbcMip::SolveExitStatus CbcMip::_solve() {
 
   262     _osi_solver = new OsiClpSolverInterface();
 
   264     _osi_solver = new OsiOslSolverInterface();
 
   266 #error Cannot instantiate Osi solver
 
   269     _osi_solver->loadFromCoinModel(*_prob);
 
   274     _cbc_model= new CbcModel(*_osi_solver);
 
   276     _osi_solver->messageHandler()->setLogLevel(_message_level);
 
   277     _cbc_model->setLogLevel(_message_level);
 
   279     _cbc_model->initialSolve();
 
   280     _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
 
   282     if (!_cbc_model->isInitialSolveAbandoned() &&
 
   283         _cbc_model->isInitialSolveProvenOptimal() &&
 
   284         !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
 
   285         !_cbc_model->isInitialSolveProvenDualInfeasible()) {
 
   287       CglProbing generator1;
 
   288       generator1.setUsingObjective(true);
 
   289       generator1.setMaxPass(3);
 
   290       generator1.setMaxProbe(100);
 
   291       generator1.setMaxLook(50);
 
   292       generator1.setRowCuts(3);
 
   293       _cbc_model->addCutGenerator(&generator1, -1, "Probing");
 
   295       CglGomory generator2;
 
   296       generator2.setLimit(300);
 
   297       _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
 
   299       CglKnapsackCover generator3;
 
   300       _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
 
   302       CglOddHole generator4;
 
   303       generator4.setMinimumViolation(0.005);
 
   304       generator4.setMinimumViolationPer(0.00002);
 
   305       generator4.setMaximumEntries(200);
 
   306       _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
 
   308       CglClique generator5;
 
   309       generator5.setStarCliqueReport(false);
 
   310       generator5.setRowCliqueReport(false);
 
   311       _cbc_model->addCutGenerator(&generator5, -1, "Clique");
 
   313       CglMixedIntegerRounding mixedGen;
 
   314       _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
 
   316       CglFlowCover flowGen;
 
   317       _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
 
   320       OsiClpSolverInterface* osiclp =
 
   321         dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
 
   322       if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
 
   323         osiclp->setupForRepeatedUse(2, 0);
 
   327       CbcRounding heuristic1(*_cbc_model);
 
   328       heuristic1.setWhen(3);
 
   329       _cbc_model->addHeuristic(&heuristic1);
 
   331       CbcHeuristicLocal heuristic2(*_cbc_model);
 
   332       heuristic2.setWhen(3);
 
   333       _cbc_model->addHeuristic(&heuristic2);
 
   335       CbcHeuristicGreedyCover heuristic3(*_cbc_model);
 
   336       heuristic3.setAlgorithm(11);
 
   337       heuristic3.setWhen(3);
 
   338       _cbc_model->addHeuristic(&heuristic3);
 
   340       CbcHeuristicFPump heuristic4(*_cbc_model);
 
   341       heuristic4.setWhen(3);
 
   342       _cbc_model->addHeuristic(&heuristic4);
 
   344       CbcHeuristicRINS heuristic5(*_cbc_model);
 
   345       heuristic5.setWhen(3);
 
   346       _cbc_model->addHeuristic(&heuristic5);
 
   348       if (_cbc_model->getNumCols() < 500) {
 
   349         _cbc_model->setMaximumCutPassesAtRoot(-100);
 
   350       } else if (_cbc_model->getNumCols() < 5000) {
 
   351         _cbc_model->setMaximumCutPassesAtRoot(100);
 
   353         _cbc_model->setMaximumCutPassesAtRoot(20);
 
   356       if (_cbc_model->getNumCols() < 5000) {
 
   357         _cbc_model->setNumberStrong(10);
 
   360       _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
 
   361       _cbc_model->branchAndBound();
 
   364     if (_cbc_model->isAbandoned()) {
 
   371   CbcMip::Value CbcMip::_getSol(int i) const {
 
   372     return _cbc_model->getColSolution()[i];
 
   375   CbcMip::Value CbcMip::_getSolValue() const {
 
   376     return _cbc_model->getObjValue();
 
   379   CbcMip::ProblemType CbcMip::_getType() const {
 
   380     if (_cbc_model->isProvenOptimal()) {
 
   382     } else if (_cbc_model->isContinuousUnbounded()) {
 
   388   void CbcMip::_setSense(Sense sense) {
 
   391       _prob->setOptimizationDirection(1.0);
 
   394       _prob->setOptimizationDirection(- 1.0);
 
   399   CbcMip::Sense CbcMip::_getSense() const {
 
   400     if (_prob->optimizationDirection() > 0.0) {
 
   402     } else if (_prob->optimizationDirection() < 0.0) {
 
   405       LEMON_ASSERT(false, "Wrong sense");
 
   406       return CbcMip::Sense();
 
   410   void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
 
   413       _prob->setInteger(i);
 
   416       _prob->setContinuous(i);
 
   419       LEMON_ASSERT(false, "Wrong sense");
 
   423   CbcMip::ColTypes CbcMip::_getColType(int i) const {
 
   424     return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
 
   427   void CbcMip::_clear() {
 
   438     _prob = new CoinModel();
 
   443   void CbcMip::_messageLevel(MessageLevel level) {
 
   445     case MESSAGE_NOTHING:
 
   451     case MESSAGE_WARNING:
 
   457     case MESSAGE_VERBOSE:
 
   463 } //END OF NAMESPACE LEMON