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