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