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