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