1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
20 ///\brief Implementation of the CBC MIP solver interface.
24 #include <coin/CoinModel.hpp>
25 #include <coin/CbcModel.hpp>
26 #include <coin/OsiSolverInterface.hpp>
28 #include "coin/OsiClpSolverInterface.hpp"
30 #include "coin/CbcCutGenerator.hpp"
31 #include "coin/CbcHeuristicLocal.hpp"
32 #include "coin/CbcHeuristicGreedy.hpp"
33 #include "coin/CbcHeuristicFPump.hpp"
34 #include "coin/CbcHeuristicRINS.hpp"
36 #include "coin/CglGomory.hpp"
37 #include "coin/CglProbing.hpp"
38 #include "coin/CglKnapsackCover.hpp"
39 #include "coin/CglOddHole.hpp"
40 #include "coin/CglClique.hpp"
41 #include "coin/CglFlowCover.hpp"
42 #include "coin/CglMixedIntegerRounding.hpp"
44 #include "coin/CbcHeuristic.hpp"
49 _prob = new CoinModel();
50 _prob->setProblemName("LEMON");
53 messageLevel(MESSAGE_NOTHING);
56 CbcMip::CbcMip(const CbcMip& other) {
57 _prob = new CoinModel(*other._prob);
58 _prob->setProblemName("LEMON");
61 messageLevel(MESSAGE_NOTHING);
66 if (_osi_solver) delete _osi_solver;
67 if (_cbc_model) delete _cbc_model;
70 const char* CbcMip::_solverName() const { return "CbcMip"; }
72 int CbcMip::_addCol() {
73 _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
74 return _prob->numberColumns() - 1;
77 CbcMip* CbcMip::newSolver() const {
78 CbcMip* newlp = new CbcMip;
82 CbcMip* CbcMip::cloneSolver() const {
83 CbcMip* copylp = new CbcMip(*this);
87 int CbcMip::_addRow() {
88 _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
89 return _prob->numberRows() - 1;
92 int CbcMip::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
93 std::vector<int> indexes;
94 std::vector<Value> values;
96 for(ExprIterator it = b; it != e; ++it) {
97 indexes.push_back(it->first);
98 values.push_back(it->second);
101 _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
102 return _prob->numberRows() - 1;
105 void CbcMip::_eraseCol(int i) {
106 _prob->deleteColumn(i);
109 void CbcMip::_eraseRow(int i) {
113 void CbcMip::_eraseColId(int i) {
117 void CbcMip::_eraseRowId(int i) {
121 void CbcMip::_getColName(int c, std::string& name) const {
122 name = _prob->getColumnName(c);
125 void CbcMip::_setColName(int c, const std::string& name) {
126 _prob->setColumnName(c, name.c_str());
129 int CbcMip::_colByName(const std::string& name) const {
130 return _prob->column(name.c_str());
133 void CbcMip::_getRowName(int r, std::string& name) const {
134 name = _prob->getRowName(r);
137 void CbcMip::_setRowName(int r, const std::string& name) {
138 _prob->setRowName(r, name.c_str());
141 int CbcMip::_rowByName(const std::string& name) const {
142 return _prob->row(name.c_str());
145 void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
146 for (ExprIterator it = b; it != e; ++it) {
147 _prob->setElement(i, it->first, it->second);
151 void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
152 int length = _prob->numberRows();
154 std::vector<int> indices(length);
155 std::vector<Value> values(length);
157 length = _prob->getRow(ix, &indices[0], &values[0]);
159 for (int i = 0; i < length; ++i) {
160 *b = std::make_pair(indices[i], values[i]);
165 void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
166 for (ExprIterator it = b; it != e; ++it) {
167 _prob->setElement(it->first, ix, it->second);
171 void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
172 int length = _prob->numberColumns();
174 std::vector<int> indices(length);
175 std::vector<Value> values(length);
177 length = _prob->getColumn(ix, &indices[0], &values[0]);
179 for (int i = 0; i < length; ++i) {
180 *b = std::make_pair(indices[i], values[i]);
185 void CbcMip::_setCoeff(int ix, int jx, Value value) {
186 _prob->setElement(ix, jx, value);
189 CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
190 return _prob->getElement(ix, jx);
194 void CbcMip::_setColLowerBound(int i, Value lo) {
195 LEMON_ASSERT(lo != INF, "Invalid bound");
196 _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
199 CbcMip::Value CbcMip::_getColLowerBound(int i) const {
200 double val = _prob->getColumnLower(i);
201 return val == - COIN_DBL_MAX ? - INF : val;
204 void CbcMip::_setColUpperBound(int i, Value up) {
205 LEMON_ASSERT(up != -INF, "Invalid bound");
206 _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
209 CbcMip::Value CbcMip::_getColUpperBound(int i) const {
210 double val = _prob->getColumnUpper(i);
211 return val == COIN_DBL_MAX ? INF : val;
214 void CbcMip::_setRowLowerBound(int i, Value lo) {
215 LEMON_ASSERT(lo != INF, "Invalid bound");
216 _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
219 CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
220 double val = _prob->getRowLower(i);
221 return val == - COIN_DBL_MAX ? - INF : val;
224 void CbcMip::_setRowUpperBound(int i, Value up) {
225 LEMON_ASSERT(up != -INF, "Invalid bound");
226 _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
229 CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
230 double val = _prob->getRowUpper(i);
231 return val == COIN_DBL_MAX ? INF : val;
234 void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
235 int num = _prob->numberColumns();
236 for (int i = 0; i < num; ++i) {
237 _prob->setColumnObjective(i, 0.0);
239 for (ExprIterator it = b; it != e; ++it) {
240 _prob->setColumnObjective(it->first, it->second);
244 void CbcMip::_getObjCoeffs(InsertIterator b) const {
245 int num = _prob->numberColumns();
246 for (int i = 0; i < num; ++i) {
247 Value coef = _prob->getColumnObjective(i);
249 *b = std::make_pair(i, coef);
255 void CbcMip::_setObjCoeff(int i, Value obj_coef) {
256 _prob->setColumnObjective(i, obj_coef);
259 CbcMip::Value CbcMip::_getObjCoeff(int i) const {
260 return _prob->getColumnObjective(i);
263 CbcMip::SolveExitStatus CbcMip::_solve() {
268 _osi_solver = new OsiClpSolverInterface();
270 _osi_solver->loadFromCoinModel(*_prob);
275 _cbc_model= new CbcModel(*_osi_solver);
277 _osi_solver->messageHandler()->setLogLevel(_message_level);
278 _cbc_model->setLogLevel(_message_level);
280 _cbc_model->initialSolve();
281 _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
283 if (!_cbc_model->isInitialSolveAbandoned() &&
284 _cbc_model->isInitialSolveProvenOptimal() &&
285 !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
286 !_cbc_model->isInitialSolveProvenDualInfeasible()) {
288 CglProbing generator1;
289 generator1.setUsingObjective(true);
290 generator1.setMaxPass(3);
291 generator1.setMaxProbe(100);
292 generator1.setMaxLook(50);
293 generator1.setRowCuts(3);
294 _cbc_model->addCutGenerator(&generator1, -1, "Probing");
296 CglGomory generator2;
297 generator2.setLimit(300);
298 _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
300 CglKnapsackCover generator3;
301 _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
303 CglOddHole generator4;
304 generator4.setMinimumViolation(0.005);
305 generator4.setMinimumViolationPer(0.00002);
306 generator4.setMaximumEntries(200);
307 _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
309 CglClique generator5;
310 generator5.setStarCliqueReport(false);
311 generator5.setRowCliqueReport(false);
312 _cbc_model->addCutGenerator(&generator5, -1, "Clique");
314 CglMixedIntegerRounding mixedGen;
315 _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
317 CglFlowCover flowGen;
318 _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
320 OsiClpSolverInterface* osiclp =
321 dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
322 if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
323 osiclp->setupForRepeatedUse(2, 0);
326 CbcRounding heuristic1(*_cbc_model);
327 heuristic1.setWhen(3);
328 _cbc_model->addHeuristic(&heuristic1);
330 CbcHeuristicLocal heuristic2(*_cbc_model);
331 heuristic2.setWhen(3);
332 _cbc_model->addHeuristic(&heuristic2);
334 CbcHeuristicGreedyCover heuristic3(*_cbc_model);
335 heuristic3.setAlgorithm(11);
336 heuristic3.setWhen(3);
337 _cbc_model->addHeuristic(&heuristic3);
339 CbcHeuristicFPump heuristic4(*_cbc_model);
340 heuristic4.setWhen(3);
341 _cbc_model->addHeuristic(&heuristic4);
343 CbcHeuristicRINS heuristic5(*_cbc_model);
344 heuristic5.setWhen(3);
345 _cbc_model->addHeuristic(&heuristic5);
347 if (_cbc_model->getNumCols() < 500) {
348 _cbc_model->setMaximumCutPassesAtRoot(-100);
349 } else if (_cbc_model->getNumCols() < 5000) {
350 _cbc_model->setMaximumCutPassesAtRoot(100);
352 _cbc_model->setMaximumCutPassesAtRoot(20);
355 if (_cbc_model->getNumCols() < 5000) {
356 _cbc_model->setNumberStrong(10);
359 _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
360 _cbc_model->branchAndBound();
363 if (_cbc_model->isAbandoned()) {
370 CbcMip::Value CbcMip::_getSol(int i) const {
371 return _cbc_model->getColSolution()[i];
374 CbcMip::Value CbcMip::_getSolValue() const {
375 return _cbc_model->getObjValue();
378 CbcMip::ProblemType CbcMip::_getType() const {
379 if (_cbc_model->isProvenOptimal()) {
381 } else if (_cbc_model->isContinuousUnbounded()) {
387 void CbcMip::_setSense(Sense sense) {
390 _prob->setOptimizationDirection(1.0);
393 _prob->setOptimizationDirection(- 1.0);
398 CbcMip::Sense CbcMip::_getSense() const {
399 if (_prob->optimizationDirection() > 0.0) {
401 } else if (_prob->optimizationDirection() < 0.0) {
404 LEMON_ASSERT(false, "Wrong sense");
405 return CbcMip::Sense();
409 void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
412 _prob->setInteger(i);
415 _prob->setContinuous(i);
418 LEMON_ASSERT(false, "Wrong sense");
422 CbcMip::ColTypes CbcMip::_getColType(int i) const {
423 return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
426 void CbcMip::_clear() {
437 _prob = new CoinModel();
440 void CbcMip::_messageLevel(MessageLevel level) {
442 case MESSAGE_NOTHING:
448 case MESSAGE_WARNING:
454 case MESSAGE_VERBOSE:
460 } //END OF NAMESPACE LEMON