lemon/cbc.cc
author Balazs Dezso <deba@inf.elte.hu>
Wed, 01 Apr 2009 22:58:58 +0200
changeset 614 3314f58e7b25
child 623 745e182d0139
permissions -rw-r--r--
Add CBC support (#204)
deba@614
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@614
     2
 *
deba@614
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@614
     4
 *
deba@614
     5
 * Copyright (C) 2003-2009
deba@614
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@614
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@614
     8
 *
deba@614
     9
 * Permission to use, modify and distribute this software is granted
deba@614
    10
 * provided that this copyright notice appears in all copies. For
deba@614
    11
 * precise terms see the accompanying LICENSE file.
deba@614
    12
 *
deba@614
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@614
    14
 * express or implied, and with no claim as to its suitability for any
deba@614
    15
 * purpose.
deba@614
    16
 *
deba@614
    17
 */
deba@614
    18
deba@614
    19
///\file
deba@614
    20
///\brief Implementation of the CBC MIP solver interface.
deba@614
    21
deba@614
    22
#include "cbc.h"
deba@614
    23
deba@614
    24
#include <coin/CoinModel.hpp>
deba@614
    25
#include <coin/CbcModel.hpp>
deba@614
    26
#include <coin/OsiSolverInterface.hpp>
deba@614
    27
deba@614
    28
#ifdef COIN_HAS_CLP
deba@614
    29
#include "coin/OsiClpSolverInterface.hpp"
deba@614
    30
#endif
deba@614
    31
#ifdef COIN_HAS_OSL
deba@614
    32
#include "coin/OsiOslSolverInterface.hpp"
deba@614
    33
#endif
deba@614
    34
deba@614
    35
#include "coin/CbcCutGenerator.hpp"
deba@614
    36
#include "coin/CbcHeuristicLocal.hpp"
deba@614
    37
#include "coin/CbcHeuristicGreedy.hpp"
deba@614
    38
#include "coin/CbcHeuristicFPump.hpp"
deba@614
    39
#include "coin/CbcHeuristicRINS.hpp"
deba@614
    40
deba@614
    41
#include "coin/CglGomory.hpp"
deba@614
    42
#include "coin/CglProbing.hpp"
deba@614
    43
#include "coin/CglKnapsackCover.hpp"
deba@614
    44
#include "coin/CglOddHole.hpp"
deba@614
    45
#include "coin/CglClique.hpp"
deba@614
    46
#include "coin/CglFlowCover.hpp"
deba@614
    47
#include "coin/CglMixedIntegerRounding.hpp"
deba@614
    48
deba@614
    49
#include "coin/CbcHeuristic.hpp"
deba@614
    50
deba@614
    51
namespace lemon {
deba@614
    52
deba@614
    53
  CbcMip::CbcMip() {
deba@614
    54
    _prob = new CoinModel();
deba@614
    55
    _prob->setProblemName("LEMON");
deba@614
    56
    _osi_solver = 0;
deba@614
    57
    _cbc_model = 0;
deba@614
    58
  }
deba@614
    59
deba@614
    60
  CbcMip::CbcMip(const CbcMip& other) {
deba@614
    61
    _prob = new CoinModel(*other._prob);
deba@614
    62
    _osi_solver = 0;
deba@614
    63
    _cbc_model = 0;
deba@614
    64
  }
deba@614
    65
deba@614
    66
  CbcMip::~CbcMip() {
deba@614
    67
    delete _prob;
deba@614
    68
    if (_osi_solver) delete _osi_solver;
deba@614
    69
    if (_cbc_model) delete _cbc_model;
deba@614
    70
  }
deba@614
    71
deba@614
    72
  const char* CbcMip::_solverName() const { return "CbcMip"; }
deba@614
    73
deba@614
    74
  int CbcMip::_addCol() {
deba@614
    75
    _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
deba@614
    76
    return _prob->numberColumns() - 1;
deba@614
    77
  }
deba@614
    78
deba@614
    79
  CbcMip* CbcMip::newSolver() const {
deba@614
    80
    CbcMip* newlp = new CbcMip;
deba@614
    81
    return newlp;
deba@614
    82
  }
deba@614
    83
deba@614
    84
  CbcMip* CbcMip::cloneSolver() const {
deba@614
    85
    CbcMip* copylp = new CbcMip(*this);
deba@614
    86
    return copylp;
deba@614
    87
  }
deba@614
    88
deba@614
    89
  int CbcMip::_addRow() {
deba@614
    90
    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
deba@614
    91
    return _prob->numberRows() - 1;
deba@614
    92
  }
deba@614
    93
deba@614
    94
deba@614
    95
  void CbcMip::_eraseCol(int i) {
deba@614
    96
    _prob->deleteColumn(i);
deba@614
    97
  }
deba@614
    98
deba@614
    99
  void CbcMip::_eraseRow(int i) {
deba@614
   100
    _prob->deleteRow(i);
deba@614
   101
  }
deba@614
   102
deba@614
   103
  void CbcMip::_eraseColId(int i) {
deba@614
   104
    cols.eraseIndex(i);
deba@614
   105
  }
deba@614
   106
deba@614
   107
  void CbcMip::_eraseRowId(int i) {
deba@614
   108
    rows.eraseIndex(i);
deba@614
   109
  }
deba@614
   110
deba@614
   111
  void CbcMip::_getColName(int c, std::string& name) const {
deba@614
   112
    name = _prob->getColumnName(c);
deba@614
   113
  }
deba@614
   114
deba@614
   115
  void CbcMip::_setColName(int c, const std::string& name) {
deba@614
   116
    _prob->setColumnName(c, name.c_str());
deba@614
   117
  }
deba@614
   118
deba@614
   119
  int CbcMip::_colByName(const std::string& name) const {
deba@614
   120
    return _prob->column(name.c_str());
deba@614
   121
  }
deba@614
   122
deba@614
   123
  void CbcMip::_getRowName(int r, std::string& name) const {
deba@614
   124
    name = _prob->getRowName(r);
deba@614
   125
  }
deba@614
   126
deba@614
   127
  void CbcMip::_setRowName(int r, const std::string& name) {
deba@614
   128
    _prob->setRowName(r, name.c_str());
deba@614
   129
  }
deba@614
   130
deba@614
   131
  int CbcMip::_rowByName(const std::string& name) const {
deba@614
   132
    return _prob->row(name.c_str());
deba@614
   133
  }
deba@614
   134
deba@614
   135
  void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
deba@614
   136
    for (ExprIterator it = b; it != e; ++it) {
deba@614
   137
      _prob->setElement(i, it->first, it->second);
deba@614
   138
    }
deba@614
   139
  }
deba@614
   140
deba@614
   141
  void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
deba@614
   142
    int length = _prob->numberRows();
deba@614
   143
deba@614
   144
    std::vector<int> indices(length);
deba@614
   145
    std::vector<Value> values(length);
deba@614
   146
deba@614
   147
    length = _prob->getRow(ix, &indices[0], &values[0]);
deba@614
   148
deba@614
   149
    for (int i = 0; i < length; ++i) {
deba@614
   150
      *b = std::make_pair(indices[i], values[i]);
deba@614
   151
      ++b;
deba@614
   152
    }
deba@614
   153
  }
deba@614
   154
deba@614
   155
  void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
deba@614
   156
    for (ExprIterator it = b; it != e; ++it) {
deba@614
   157
      _prob->setElement(it->first, ix, it->second);
deba@614
   158
    }
deba@614
   159
  }
deba@614
   160
deba@614
   161
  void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
deba@614
   162
    int length = _prob->numberColumns();
deba@614
   163
deba@614
   164
    std::vector<int> indices(length);
deba@614
   165
    std::vector<Value> values(length);
deba@614
   166
deba@614
   167
    length = _prob->getColumn(ix, &indices[0], &values[0]);
deba@614
   168
deba@614
   169
    for (int i = 0; i < length; ++i) {
deba@614
   170
      *b = std::make_pair(indices[i], values[i]);
deba@614
   171
      ++b;
deba@614
   172
    }
deba@614
   173
  }
deba@614
   174
deba@614
   175
  void CbcMip::_setCoeff(int ix, int jx, Value value) {
deba@614
   176
    _prob->setElement(ix, jx, value);
deba@614
   177
  }
deba@614
   178
deba@614
   179
  CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
deba@614
   180
    return _prob->getElement(ix, jx);
deba@614
   181
  }
deba@614
   182
deba@614
   183
deba@614
   184
  void CbcMip::_setColLowerBound(int i, Value lo) {
deba@614
   185
    LEMON_ASSERT(lo != INF, "Invalid bound");
deba@614
   186
    _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
deba@614
   187
  }
deba@614
   188
deba@614
   189
  CbcMip::Value CbcMip::_getColLowerBound(int i) const {
deba@614
   190
    double val = _prob->getColumnLower(i);
deba@614
   191
    return val == - COIN_DBL_MAX ? - INF : val;
deba@614
   192
  }
deba@614
   193
deba@614
   194
  void CbcMip::_setColUpperBound(int i, Value up) {
deba@614
   195
    LEMON_ASSERT(up != -INF, "Invalid bound");
deba@614
   196
    _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
deba@614
   197
  }
deba@614
   198
deba@614
   199
  CbcMip::Value CbcMip::_getColUpperBound(int i) const {
deba@614
   200
    double val = _prob->getColumnUpper(i);
deba@614
   201
    return val == COIN_DBL_MAX ? INF : val;
deba@614
   202
  }
deba@614
   203
deba@614
   204
  void CbcMip::_setRowLowerBound(int i, Value lo) {
deba@614
   205
    LEMON_ASSERT(lo != INF, "Invalid bound");
deba@614
   206
    _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
deba@614
   207
  }
deba@614
   208
deba@614
   209
  CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
deba@614
   210
    double val = _prob->getRowLower(i);
deba@614
   211
    return val == - COIN_DBL_MAX ? - INF : val;
deba@614
   212
  }
deba@614
   213
deba@614
   214
  void CbcMip::_setRowUpperBound(int i, Value up) {
deba@614
   215
    LEMON_ASSERT(up != -INF, "Invalid bound");
deba@614
   216
    _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
deba@614
   217
  }
deba@614
   218
deba@614
   219
  CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
deba@614
   220
    double val = _prob->getRowUpper(i);
deba@614
   221
    return val == COIN_DBL_MAX ? INF : val;
deba@614
   222
  }
deba@614
   223
deba@614
   224
  void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
deba@614
   225
    int num = _prob->numberColumns();
deba@614
   226
    for (int i = 0; i < num; ++i) {
deba@614
   227
      _prob->setColumnObjective(i, 0.0);
deba@614
   228
    }
deba@614
   229
    for (ExprIterator it = b; it != e; ++it) {
deba@614
   230
      _prob->setColumnObjective(it->first, it->second);
deba@614
   231
    }
deba@614
   232
  }
deba@614
   233
deba@614
   234
  void CbcMip::_getObjCoeffs(InsertIterator b) const {
deba@614
   235
    int num = _prob->numberColumns();
deba@614
   236
    for (int i = 0; i < num; ++i) {
deba@614
   237
      Value coef = _prob->getColumnObjective(i);
deba@614
   238
      if (coef != 0.0) {
deba@614
   239
        *b = std::make_pair(i, coef);
deba@614
   240
        ++b;
deba@614
   241
      }
deba@614
   242
    }
deba@614
   243
  }
deba@614
   244
deba@614
   245
  void CbcMip::_setObjCoeff(int i, Value obj_coef) {
deba@614
   246
    _prob->setColumnObjective(i, obj_coef);
deba@614
   247
  }
deba@614
   248
deba@614
   249
  CbcMip::Value CbcMip::_getObjCoeff(int i) const {
deba@614
   250
    return _prob->getColumnObjective(i);
deba@614
   251
  }
deba@614
   252
deba@614
   253
  CbcMip::SolveExitStatus CbcMip::_solve() {
deba@614
   254
deba@614
   255
    if (_osi_solver) {
deba@614
   256
      delete _osi_solver;
deba@614
   257
    }
deba@614
   258
#ifdef COIN_HAS_CLP
deba@614
   259
    _osi_solver = new OsiClpSolverInterface();
deba@614
   260
#elif COIN_HAS_OSL
deba@614
   261
    _osi_solver = new OsiOslSolverInterface();
deba@614
   262
#else
deba@614
   263
#error Cannot instantiate Osi solver
deba@614
   264
#endif
deba@614
   265
deba@614
   266
    _osi_solver->loadFromCoinModel(*_prob);
deba@614
   267
deba@614
   268
    if (_cbc_model) {
deba@614
   269
      delete _cbc_model;
deba@614
   270
    }
deba@614
   271
    _cbc_model= new CbcModel(*_osi_solver);
deba@614
   272
deba@614
   273
    switch (_message_level) {
deba@614
   274
    case MESSAGE_NO_OUTPUT:
deba@614
   275
      _osi_solver->messageHandler()->setLogLevel(0);
deba@614
   276
      _cbc_model->setLogLevel(0);
deba@614
   277
      break;
deba@614
   278
    case MESSAGE_ERROR_MESSAGE:
deba@614
   279
      _osi_solver->messageHandler()->setLogLevel(1);
deba@614
   280
      _cbc_model->setLogLevel(1);
deba@614
   281
      break;
deba@614
   282
    case MESSAGE_NORMAL_OUTPUT:
deba@614
   283
      _osi_solver->messageHandler()->setLogLevel(2);
deba@614
   284
      _cbc_model->setLogLevel(2);
deba@614
   285
      break;
deba@614
   286
    case MESSAGE_FULL_OUTPUT:
deba@614
   287
      _osi_solver->messageHandler()->setLogLevel(3);
deba@614
   288
      _cbc_model->setLogLevel(3);
deba@614
   289
      break;
deba@614
   290
    }
deba@614
   291
deba@614
   292
    _cbc_model->initialSolve();
deba@614
   293
    _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
deba@614
   294
deba@614
   295
    if (!_cbc_model->isInitialSolveAbandoned() &&
deba@614
   296
        _cbc_model->isInitialSolveProvenOptimal() &&
deba@614
   297
        !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
deba@614
   298
        !_cbc_model->isInitialSolveProvenDualInfeasible()) {
deba@614
   299
deba@614
   300
      CglProbing generator1;
deba@614
   301
      generator1.setUsingObjective(true);
deba@614
   302
      generator1.setMaxPass(3);
deba@614
   303
      generator1.setMaxProbe(100);
deba@614
   304
      generator1.setMaxLook(50);
deba@614
   305
      generator1.setRowCuts(3);
deba@614
   306
      _cbc_model->addCutGenerator(&generator1, -1, "Probing");
deba@614
   307
deba@614
   308
      CglGomory generator2;
deba@614
   309
      generator2.setLimit(300);
deba@614
   310
      _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
deba@614
   311
deba@614
   312
      CglKnapsackCover generator3;
deba@614
   313
      _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
deba@614
   314
deba@614
   315
      CglOddHole generator4;
deba@614
   316
      generator4.setMinimumViolation(0.005);
deba@614
   317
      generator4.setMinimumViolationPer(0.00002);
deba@614
   318
      generator4.setMaximumEntries(200);
deba@614
   319
      _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
deba@614
   320
deba@614
   321
      CglClique generator5;
deba@614
   322
      generator5.setStarCliqueReport(false);
deba@614
   323
      generator5.setRowCliqueReport(false);
deba@614
   324
      _cbc_model->addCutGenerator(&generator5, -1, "Clique");
deba@614
   325
deba@614
   326
      CglMixedIntegerRounding mixedGen;
deba@614
   327
      _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
deba@614
   328
deba@614
   329
      CglFlowCover flowGen;
deba@614
   330
      _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
deba@614
   331
deba@614
   332
#ifdef COIN_HAS_CLP
deba@614
   333
      OsiClpSolverInterface* osiclp =
deba@614
   334
        dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
deba@614
   335
      if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
deba@614
   336
        osiclp->setupForRepeatedUse(2, 0);
deba@614
   337
      }
deba@614
   338
#endif
deba@614
   339
deba@614
   340
      CbcRounding heuristic1(*_cbc_model);
deba@614
   341
      heuristic1.setWhen(3);
deba@614
   342
      _cbc_model->addHeuristic(&heuristic1);
deba@614
   343
deba@614
   344
      CbcHeuristicLocal heuristic2(*_cbc_model);
deba@614
   345
      heuristic2.setWhen(3);
deba@614
   346
      _cbc_model->addHeuristic(&heuristic2);
deba@614
   347
deba@614
   348
      CbcHeuristicGreedyCover heuristic3(*_cbc_model);
deba@614
   349
      heuristic3.setAlgorithm(11);
deba@614
   350
      heuristic3.setWhen(3);
deba@614
   351
      _cbc_model->addHeuristic(&heuristic3);
deba@614
   352
deba@614
   353
      CbcHeuristicFPump heuristic4(*_cbc_model);
deba@614
   354
      heuristic4.setWhen(3);
deba@614
   355
      _cbc_model->addHeuristic(&heuristic4);
deba@614
   356
deba@614
   357
      CbcHeuristicRINS heuristic5(*_cbc_model);
deba@614
   358
      heuristic5.setWhen(3);
deba@614
   359
      _cbc_model->addHeuristic(&heuristic5);
deba@614
   360
deba@614
   361
      if (_cbc_model->getNumCols() < 500) {
deba@614
   362
        _cbc_model->setMaximumCutPassesAtRoot(-100);
deba@614
   363
      } else if (_cbc_model->getNumCols() < 5000) {
deba@614
   364
        _cbc_model->setMaximumCutPassesAtRoot(100);
deba@614
   365
      } else {
deba@614
   366
        _cbc_model->setMaximumCutPassesAtRoot(20);
deba@614
   367
      }
deba@614
   368
deba@614
   369
      if (_cbc_model->getNumCols() < 5000) {
deba@614
   370
        _cbc_model->setNumberStrong(10);
deba@614
   371
      }
deba@614
   372
deba@614
   373
      _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
deba@614
   374
      _cbc_model->branchAndBound();
deba@614
   375
    }
deba@614
   376
deba@614
   377
    if (_cbc_model->isAbandoned()) {
deba@614
   378
      return UNSOLVED;
deba@614
   379
    } else {
deba@614
   380
      return SOLVED;
deba@614
   381
    }
deba@614
   382
  }
deba@614
   383
deba@614
   384
  CbcMip::Value CbcMip::_getSol(int i) const {
deba@614
   385
    return _cbc_model->getColSolution()[i];
deba@614
   386
  }
deba@614
   387
deba@614
   388
  CbcMip::Value CbcMip::_getSolValue() const {
deba@614
   389
    return _cbc_model->getObjValue();
deba@614
   390
  }
deba@614
   391
deba@614
   392
  CbcMip::ProblemType CbcMip::_getType() const {
deba@614
   393
    if (_cbc_model->isProvenOptimal()) {
deba@614
   394
      return OPTIMAL;
deba@614
   395
    } else if (_cbc_model->isContinuousUnbounded()) {
deba@614
   396
      return UNBOUNDED;
deba@614
   397
    }
deba@614
   398
    return FEASIBLE;
deba@614
   399
  }
deba@614
   400
deba@614
   401
  void CbcMip::_setSense(Sense sense) {
deba@614
   402
    switch (sense) {
deba@614
   403
    case MIN:
deba@614
   404
      _prob->setOptimizationDirection(1.0);
deba@614
   405
      break;
deba@614
   406
    case MAX:
deba@614
   407
      _prob->setOptimizationDirection(- 1.0);
deba@614
   408
      break;
deba@614
   409
    }
deba@614
   410
  }
deba@614
   411
deba@614
   412
  CbcMip::Sense CbcMip::_getSense() const {
deba@614
   413
    if (_prob->optimizationDirection() > 0.0) {
deba@614
   414
      return MIN;
deba@614
   415
    } else if (_prob->optimizationDirection() < 0.0) {
deba@614
   416
      return MAX;
deba@614
   417
    } else {
deba@614
   418
      LEMON_ASSERT(false, "Wrong sense");
deba@614
   419
      return CbcMip::Sense();
deba@614
   420
    }
deba@614
   421
  }
deba@614
   422
deba@614
   423
  void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
deba@614
   424
    switch (col_type){
deba@614
   425
    case INTEGER:
deba@614
   426
      _prob->setInteger(i);
deba@614
   427
      break;
deba@614
   428
    case REAL:
deba@614
   429
      _prob->setContinuous(i);
deba@614
   430
      break;
deba@614
   431
    default:;
deba@614
   432
      LEMON_ASSERT(false, "Wrong sense");
deba@614
   433
    }
deba@614
   434
  }
deba@614
   435
deba@614
   436
  CbcMip::ColTypes CbcMip::_getColType(int i) const {
deba@614
   437
    return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
deba@614
   438
  }
deba@614
   439
deba@614
   440
  void CbcMip::_clear() {
deba@614
   441
    delete _prob;
deba@614
   442
    if (_osi_solver) {
deba@614
   443
      delete _osi_solver;
deba@614
   444
      _osi_solver = 0;
deba@614
   445
    }
deba@614
   446
    if (_cbc_model) {
deba@614
   447
      delete _cbc_model;
deba@614
   448
      _cbc_model = 0;
deba@614
   449
    }
deba@614
   450
deba@614
   451
    _prob = new CoinModel();
deba@614
   452
    rows.clear();
deba@614
   453
    cols.clear();
deba@614
   454
  }
deba@614
   455
deba@614
   456
  void CbcMip::messageLevel(MessageLevel m) {
deba@614
   457
    _message_level = m;
deba@614
   458
  }
deba@614
   459
deba@614
   460
} //END OF NAMESPACE LEMON