lemon/cbc.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 17 Mar 2010 10:23:17 +0100
branch1.1
changeset 723 1248d23d6e93
parent 559 3314f58e7b25
child 781 ee581a0ecfbf
permissions -rw-r--r--
Merge bugfix #356 to branch 1.1
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@568
    58
    messageLevel(MESSAGE_NOTHING);
deba@559
    59
  }
deba@559
    60
deba@559
    61
  CbcMip::CbcMip(const CbcMip& other) {
deba@559
    62
    _prob = new CoinModel(*other._prob);
deba@568
    63
    _prob->setProblemName("LEMON");
deba@559
    64
    _osi_solver = 0;
deba@559
    65
    _cbc_model = 0;
deba@568
    66
    messageLevel(MESSAGE_NOTHING);
deba@559
    67
  }
deba@559
    68
deba@559
    69
  CbcMip::~CbcMip() {
deba@559
    70
    delete _prob;
deba@559
    71
    if (_osi_solver) delete _osi_solver;
deba@559
    72
    if (_cbc_model) delete _cbc_model;
deba@559
    73
  }
deba@559
    74
deba@559
    75
  const char* CbcMip::_solverName() const { return "CbcMip"; }
deba@559
    76
deba@559
    77
  int CbcMip::_addCol() {
deba@559
    78
    _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
deba@559
    79
    return _prob->numberColumns() - 1;
deba@559
    80
  }
deba@559
    81
deba@559
    82
  CbcMip* CbcMip::newSolver() const {
deba@559
    83
    CbcMip* newlp = new CbcMip;
deba@559
    84
    return newlp;
deba@559
    85
  }
deba@559
    86
deba@559
    87
  CbcMip* CbcMip::cloneSolver() const {
deba@559
    88
    CbcMip* copylp = new CbcMip(*this);
deba@559
    89
    return copylp;
deba@559
    90
  }
deba@559
    91
deba@559
    92
  int CbcMip::_addRow() {
deba@559
    93
    _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
deba@559
    94
    return _prob->numberRows() - 1;
deba@559
    95
  }
deba@559
    96
deba@559
    97
deba@559
    98
  void CbcMip::_eraseCol(int i) {
deba@559
    99
    _prob->deleteColumn(i);
deba@559
   100
  }
deba@559
   101
deba@559
   102
  void CbcMip::_eraseRow(int i) {
deba@559
   103
    _prob->deleteRow(i);
deba@559
   104
  }
deba@559
   105
deba@559
   106
  void CbcMip::_eraseColId(int i) {
deba@559
   107
    cols.eraseIndex(i);
deba@559
   108
  }
deba@559
   109
deba@559
   110
  void CbcMip::_eraseRowId(int i) {
deba@559
   111
    rows.eraseIndex(i);
deba@559
   112
  }
deba@559
   113
deba@559
   114
  void CbcMip::_getColName(int c, std::string& name) const {
deba@559
   115
    name = _prob->getColumnName(c);
deba@559
   116
  }
deba@559
   117
deba@559
   118
  void CbcMip::_setColName(int c, const std::string& name) {
deba@559
   119
    _prob->setColumnName(c, name.c_str());
deba@559
   120
  }
deba@559
   121
deba@559
   122
  int CbcMip::_colByName(const std::string& name) const {
deba@559
   123
    return _prob->column(name.c_str());
deba@559
   124
  }
deba@559
   125
deba@559
   126
  void CbcMip::_getRowName(int r, std::string& name) const {
deba@559
   127
    name = _prob->getRowName(r);
deba@559
   128
  }
deba@559
   129
deba@559
   130
  void CbcMip::_setRowName(int r, const std::string& name) {
deba@559
   131
    _prob->setRowName(r, name.c_str());
deba@559
   132
  }
deba@559
   133
deba@559
   134
  int CbcMip::_rowByName(const std::string& name) const {
deba@559
   135
    return _prob->row(name.c_str());
deba@559
   136
  }
deba@559
   137
deba@559
   138
  void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
deba@559
   139
    for (ExprIterator it = b; it != e; ++it) {
deba@559
   140
      _prob->setElement(i, it->first, it->second);
deba@559
   141
    }
deba@559
   142
  }
deba@559
   143
deba@559
   144
  void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
deba@559
   145
    int length = _prob->numberRows();
deba@559
   146
deba@559
   147
    std::vector<int> indices(length);
deba@559
   148
    std::vector<Value> values(length);
deba@559
   149
deba@559
   150
    length = _prob->getRow(ix, &indices[0], &values[0]);
deba@559
   151
deba@559
   152
    for (int i = 0; i < length; ++i) {
deba@559
   153
      *b = std::make_pair(indices[i], values[i]);
deba@559
   154
      ++b;
deba@559
   155
    }
deba@559
   156
  }
deba@559
   157
deba@559
   158
  void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
deba@559
   159
    for (ExprIterator it = b; it != e; ++it) {
deba@559
   160
      _prob->setElement(it->first, ix, it->second);
deba@559
   161
    }
deba@559
   162
  }
deba@559
   163
deba@559
   164
  void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
deba@559
   165
    int length = _prob->numberColumns();
deba@559
   166
deba@559
   167
    std::vector<int> indices(length);
deba@559
   168
    std::vector<Value> values(length);
deba@559
   169
deba@559
   170
    length = _prob->getColumn(ix, &indices[0], &values[0]);
deba@559
   171
deba@559
   172
    for (int i = 0; i < length; ++i) {
deba@559
   173
      *b = std::make_pair(indices[i], values[i]);
deba@559
   174
      ++b;
deba@559
   175
    }
deba@559
   176
  }
deba@559
   177
deba@559
   178
  void CbcMip::_setCoeff(int ix, int jx, Value value) {
deba@559
   179
    _prob->setElement(ix, jx, value);
deba@559
   180
  }
deba@559
   181
deba@559
   182
  CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
deba@559
   183
    return _prob->getElement(ix, jx);
deba@559
   184
  }
deba@559
   185
deba@559
   186
deba@559
   187
  void CbcMip::_setColLowerBound(int i, Value lo) {
deba@559
   188
    LEMON_ASSERT(lo != INF, "Invalid bound");
deba@559
   189
    _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
deba@559
   190
  }
deba@559
   191
deba@559
   192
  CbcMip::Value CbcMip::_getColLowerBound(int i) const {
deba@559
   193
    double val = _prob->getColumnLower(i);
deba@559
   194
    return val == - COIN_DBL_MAX ? - INF : val;
deba@559
   195
  }
deba@559
   196
deba@559
   197
  void CbcMip::_setColUpperBound(int i, Value up) {
deba@559
   198
    LEMON_ASSERT(up != -INF, "Invalid bound");
deba@559
   199
    _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
deba@559
   200
  }
deba@559
   201
deba@559
   202
  CbcMip::Value CbcMip::_getColUpperBound(int i) const {
deba@559
   203
    double val = _prob->getColumnUpper(i);
deba@559
   204
    return val == COIN_DBL_MAX ? INF : val;
deba@559
   205
  }
deba@559
   206
deba@559
   207
  void CbcMip::_setRowLowerBound(int i, Value lo) {
deba@559
   208
    LEMON_ASSERT(lo != INF, "Invalid bound");
deba@559
   209
    _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
deba@559
   210
  }
deba@559
   211
deba@559
   212
  CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
deba@559
   213
    double val = _prob->getRowLower(i);
deba@559
   214
    return val == - COIN_DBL_MAX ? - INF : val;
deba@559
   215
  }
deba@559
   216
deba@559
   217
  void CbcMip::_setRowUpperBound(int i, Value up) {
deba@559
   218
    LEMON_ASSERT(up != -INF, "Invalid bound");
deba@559
   219
    _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
deba@559
   220
  }
deba@559
   221
deba@559
   222
  CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
deba@559
   223
    double val = _prob->getRowUpper(i);
deba@559
   224
    return val == COIN_DBL_MAX ? INF : val;
deba@559
   225
  }
deba@559
   226
deba@559
   227
  void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
deba@559
   228
    int num = _prob->numberColumns();
deba@559
   229
    for (int i = 0; i < num; ++i) {
deba@559
   230
      _prob->setColumnObjective(i, 0.0);
deba@559
   231
    }
deba@559
   232
    for (ExprIterator it = b; it != e; ++it) {
deba@559
   233
      _prob->setColumnObjective(it->first, it->second);
deba@559
   234
    }
deba@559
   235
  }
deba@559
   236
deba@559
   237
  void CbcMip::_getObjCoeffs(InsertIterator b) const {
deba@559
   238
    int num = _prob->numberColumns();
deba@559
   239
    for (int i = 0; i < num; ++i) {
deba@559
   240
      Value coef = _prob->getColumnObjective(i);
deba@559
   241
      if (coef != 0.0) {
deba@559
   242
        *b = std::make_pair(i, coef);
deba@559
   243
        ++b;
deba@559
   244
      }
deba@559
   245
    }
deba@559
   246
  }
deba@559
   247
deba@559
   248
  void CbcMip::_setObjCoeff(int i, Value obj_coef) {
deba@559
   249
    _prob->setColumnObjective(i, obj_coef);
deba@559
   250
  }
deba@559
   251
deba@559
   252
  CbcMip::Value CbcMip::_getObjCoeff(int i) const {
deba@559
   253
    return _prob->getColumnObjective(i);
deba@559
   254
  }
deba@559
   255
deba@559
   256
  CbcMip::SolveExitStatus CbcMip::_solve() {
deba@559
   257
deba@559
   258
    if (_osi_solver) {
deba@559
   259
      delete _osi_solver;
deba@559
   260
    }
deba@559
   261
#ifdef COIN_HAS_CLP
deba@559
   262
    _osi_solver = new OsiClpSolverInterface();
deba@559
   263
#elif COIN_HAS_OSL
deba@559
   264
    _osi_solver = new OsiOslSolverInterface();
deba@559
   265
#else
deba@559
   266
#error Cannot instantiate Osi solver
deba@559
   267
#endif
deba@559
   268
deba@559
   269
    _osi_solver->loadFromCoinModel(*_prob);
deba@559
   270
deba@559
   271
    if (_cbc_model) {
deba@559
   272
      delete _cbc_model;
deba@559
   273
    }
deba@559
   274
    _cbc_model= new CbcModel(*_osi_solver);
deba@559
   275
deba@568
   276
    _osi_solver->messageHandler()->setLogLevel(_message_level);
deba@568
   277
    _cbc_model->setLogLevel(_message_level);
deba@559
   278
deba@559
   279
    _cbc_model->initialSolve();
deba@559
   280
    _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
deba@559
   281
deba@559
   282
    if (!_cbc_model->isInitialSolveAbandoned() &&
deba@559
   283
        _cbc_model->isInitialSolveProvenOptimal() &&
deba@559
   284
        !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
deba@559
   285
        !_cbc_model->isInitialSolveProvenDualInfeasible()) {
deba@559
   286
deba@559
   287
      CglProbing generator1;
deba@559
   288
      generator1.setUsingObjective(true);
deba@559
   289
      generator1.setMaxPass(3);
deba@559
   290
      generator1.setMaxProbe(100);
deba@559
   291
      generator1.setMaxLook(50);
deba@559
   292
      generator1.setRowCuts(3);
deba@559
   293
      _cbc_model->addCutGenerator(&generator1, -1, "Probing");
deba@559
   294
deba@559
   295
      CglGomory generator2;
deba@559
   296
      generator2.setLimit(300);
deba@559
   297
      _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
deba@559
   298
deba@559
   299
      CglKnapsackCover generator3;
deba@559
   300
      _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
deba@559
   301
deba@559
   302
      CglOddHole generator4;
deba@559
   303
      generator4.setMinimumViolation(0.005);
deba@559
   304
      generator4.setMinimumViolationPer(0.00002);
deba@559
   305
      generator4.setMaximumEntries(200);
deba@559
   306
      _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
deba@559
   307
deba@559
   308
      CglClique generator5;
deba@559
   309
      generator5.setStarCliqueReport(false);
deba@559
   310
      generator5.setRowCliqueReport(false);
deba@559
   311
      _cbc_model->addCutGenerator(&generator5, -1, "Clique");
deba@559
   312
deba@559
   313
      CglMixedIntegerRounding mixedGen;
deba@559
   314
      _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
deba@559
   315
deba@559
   316
      CglFlowCover flowGen;
deba@559
   317
      _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
deba@559
   318
deba@559
   319
#ifdef COIN_HAS_CLP
deba@559
   320
      OsiClpSolverInterface* osiclp =
deba@559
   321
        dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
deba@559
   322
      if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
deba@559
   323
        osiclp->setupForRepeatedUse(2, 0);
deba@559
   324
      }
deba@559
   325
#endif
deba@559
   326
deba@559
   327
      CbcRounding heuristic1(*_cbc_model);
deba@559
   328
      heuristic1.setWhen(3);
deba@559
   329
      _cbc_model->addHeuristic(&heuristic1);
deba@559
   330
deba@559
   331
      CbcHeuristicLocal heuristic2(*_cbc_model);
deba@559
   332
      heuristic2.setWhen(3);
deba@559
   333
      _cbc_model->addHeuristic(&heuristic2);
deba@559
   334
deba@559
   335
      CbcHeuristicGreedyCover heuristic3(*_cbc_model);
deba@559
   336
      heuristic3.setAlgorithm(11);
deba@559
   337
      heuristic3.setWhen(3);
deba@559
   338
      _cbc_model->addHeuristic(&heuristic3);
deba@559
   339
deba@559
   340
      CbcHeuristicFPump heuristic4(*_cbc_model);
deba@559
   341
      heuristic4.setWhen(3);
deba@559
   342
      _cbc_model->addHeuristic(&heuristic4);
deba@559
   343
deba@559
   344
      CbcHeuristicRINS heuristic5(*_cbc_model);
deba@559
   345
      heuristic5.setWhen(3);
deba@559
   346
      _cbc_model->addHeuristic(&heuristic5);
deba@559
   347
deba@559
   348
      if (_cbc_model->getNumCols() < 500) {
deba@559
   349
        _cbc_model->setMaximumCutPassesAtRoot(-100);
deba@559
   350
      } else if (_cbc_model->getNumCols() < 5000) {
deba@559
   351
        _cbc_model->setMaximumCutPassesAtRoot(100);
deba@559
   352
      } else {
deba@559
   353
        _cbc_model->setMaximumCutPassesAtRoot(20);
deba@559
   354
      }
deba@559
   355
deba@559
   356
      if (_cbc_model->getNumCols() < 5000) {
deba@559
   357
        _cbc_model->setNumberStrong(10);
deba@559
   358
      }
deba@559
   359
deba@559
   360
      _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
deba@559
   361
      _cbc_model->branchAndBound();
deba@559
   362
    }
deba@559
   363
deba@559
   364
    if (_cbc_model->isAbandoned()) {
deba@559
   365
      return UNSOLVED;
deba@559
   366
    } else {
deba@559
   367
      return SOLVED;
deba@559
   368
    }
deba@559
   369
  }
deba@559
   370
deba@559
   371
  CbcMip::Value CbcMip::_getSol(int i) const {
deba@559
   372
    return _cbc_model->getColSolution()[i];
deba@559
   373
  }
deba@559
   374
deba@559
   375
  CbcMip::Value CbcMip::_getSolValue() const {
deba@559
   376
    return _cbc_model->getObjValue();
deba@559
   377
  }
deba@559
   378
deba@559
   379
  CbcMip::ProblemType CbcMip::_getType() const {
deba@559
   380
    if (_cbc_model->isProvenOptimal()) {
deba@559
   381
      return OPTIMAL;
deba@559
   382
    } else if (_cbc_model->isContinuousUnbounded()) {
deba@559
   383
      return UNBOUNDED;
deba@559
   384
    }
deba@559
   385
    return FEASIBLE;
deba@559
   386
  }
deba@559
   387
deba@559
   388
  void CbcMip::_setSense(Sense sense) {
deba@559
   389
    switch (sense) {
deba@559
   390
    case MIN:
deba@559
   391
      _prob->setOptimizationDirection(1.0);
deba@559
   392
      break;
deba@559
   393
    case MAX:
deba@559
   394
      _prob->setOptimizationDirection(- 1.0);
deba@559
   395
      break;
deba@559
   396
    }
deba@559
   397
  }
deba@559
   398
deba@559
   399
  CbcMip::Sense CbcMip::_getSense() const {
deba@559
   400
    if (_prob->optimizationDirection() > 0.0) {
deba@559
   401
      return MIN;
deba@559
   402
    } else if (_prob->optimizationDirection() < 0.0) {
deba@559
   403
      return MAX;
deba@559
   404
    } else {
deba@559
   405
      LEMON_ASSERT(false, "Wrong sense");
deba@559
   406
      return CbcMip::Sense();
deba@559
   407
    }
deba@559
   408
  }
deba@559
   409
deba@559
   410
  void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
deba@559
   411
    switch (col_type){
deba@559
   412
    case INTEGER:
deba@559
   413
      _prob->setInteger(i);
deba@559
   414
      break;
deba@559
   415
    case REAL:
deba@559
   416
      _prob->setContinuous(i);
deba@559
   417
      break;
deba@559
   418
    default:;
deba@559
   419
      LEMON_ASSERT(false, "Wrong sense");
deba@559
   420
    }
deba@559
   421
  }
deba@559
   422
deba@559
   423
  CbcMip::ColTypes CbcMip::_getColType(int i) const {
deba@559
   424
    return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
deba@559
   425
  }
deba@559
   426
deba@559
   427
  void CbcMip::_clear() {
deba@559
   428
    delete _prob;
deba@559
   429
    if (_osi_solver) {
deba@559
   430
      delete _osi_solver;
deba@559
   431
      _osi_solver = 0;
deba@559
   432
    }
deba@559
   433
    if (_cbc_model) {
deba@559
   434
      delete _cbc_model;
deba@559
   435
      _cbc_model = 0;
deba@559
   436
    }
deba@559
   437
deba@559
   438
    _prob = new CoinModel();
deba@559
   439
    rows.clear();
deba@559
   440
    cols.clear();
deba@559
   441
  }
deba@559
   442
deba@568
   443
  void CbcMip::_messageLevel(MessageLevel level) {
deba@568
   444
    switch (level) {
deba@568
   445
    case MESSAGE_NOTHING:
deba@568
   446
      _message_level = 0;
deba@568
   447
      break;
deba@568
   448
    case MESSAGE_ERROR:
deba@568
   449
      _message_level = 1;
deba@568
   450
      break;
deba@568
   451
    case MESSAGE_WARNING:
deba@568
   452
      _message_level = 1;
deba@568
   453
      break;
deba@568
   454
    case MESSAGE_NORMAL:
deba@568
   455
      _message_level = 2;
deba@568
   456
      break;
deba@568
   457
    case MESSAGE_VERBOSE:
deba@568
   458
      _message_level = 3;
deba@568
   459
      break;
deba@568
   460
    }
deba@559
   461
  }
deba@559
   462
deba@559
   463
} //END OF NAMESPACE LEMON