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>
29 #include "coin/OsiClpSolverInterface.hpp"
32 #include "coin/OsiOslSolverInterface.hpp"
35 #include "coin/CbcCutGenerator.hpp"
36 #include "coin/CbcHeuristicLocal.hpp"
37 #include "coin/CbcHeuristicGreedy.hpp"
38 #include "coin/CbcHeuristicFPump.hpp"
39 #include "coin/CbcHeuristicRINS.hpp"
41 #include "coin/CglGomory.hpp"
42 #include "coin/CglProbing.hpp"
43 #include "coin/CglKnapsackCover.hpp"
44 #include "coin/CglOddHole.hpp"
45 #include "coin/CglClique.hpp"
46 #include "coin/CglFlowCover.hpp"
47 #include "coin/CglMixedIntegerRounding.hpp"
49 #include "coin/CbcHeuristic.hpp"
54 _prob = new CoinModel();
55 _prob->setProblemName("LEMON");
60 CbcMip::CbcMip(const CbcMip& other) {
61 _prob = new CoinModel(*other._prob);
68 if (_osi_solver) delete _osi_solver;
69 if (_cbc_model) delete _cbc_model;
72 const char* CbcMip::_solverName() const { return "CbcMip"; }
74 int CbcMip::_addCol() {
75 _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false);
76 return _prob->numberColumns() - 1;
79 CbcMip* CbcMip::newSolver() const {
80 CbcMip* newlp = new CbcMip;
84 CbcMip* CbcMip::cloneSolver() const {
85 CbcMip* copylp = new CbcMip(*this);
89 int CbcMip::_addRow() {
90 _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
91 return _prob->numberRows() - 1;
95 void CbcMip::_eraseCol(int i) {
96 _prob->deleteColumn(i);
99 void CbcMip::_eraseRow(int i) {
103 void CbcMip::_eraseColId(int i) {
107 void CbcMip::_eraseRowId(int i) {
111 void CbcMip::_getColName(int c, std::string& name) const {
112 name = _prob->getColumnName(c);
115 void CbcMip::_setColName(int c, const std::string& name) {
116 _prob->setColumnName(c, name.c_str());
119 int CbcMip::_colByName(const std::string& name) const {
120 return _prob->column(name.c_str());
123 void CbcMip::_getRowName(int r, std::string& name) const {
124 name = _prob->getRowName(r);
127 void CbcMip::_setRowName(int r, const std::string& name) {
128 _prob->setRowName(r, name.c_str());
131 int CbcMip::_rowByName(const std::string& name) const {
132 return _prob->row(name.c_str());
135 void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
136 for (ExprIterator it = b; it != e; ++it) {
137 _prob->setElement(i, it->first, it->second);
141 void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const {
142 int length = _prob->numberRows();
144 std::vector<int> indices(length);
145 std::vector<Value> values(length);
147 length = _prob->getRow(ix, &indices[0], &values[0]);
149 for (int i = 0; i < length; ++i) {
150 *b = std::make_pair(indices[i], values[i]);
155 void CbcMip::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
156 for (ExprIterator it = b; it != e; ++it) {
157 _prob->setElement(it->first, ix, it->second);
161 void CbcMip::_getColCoeffs(int ix, InsertIterator b) const {
162 int length = _prob->numberColumns();
164 std::vector<int> indices(length);
165 std::vector<Value> values(length);
167 length = _prob->getColumn(ix, &indices[0], &values[0]);
169 for (int i = 0; i < length; ++i) {
170 *b = std::make_pair(indices[i], values[i]);
175 void CbcMip::_setCoeff(int ix, int jx, Value value) {
176 _prob->setElement(ix, jx, value);
179 CbcMip::Value CbcMip::_getCoeff(int ix, int jx) const {
180 return _prob->getElement(ix, jx);
184 void CbcMip::_setColLowerBound(int i, Value lo) {
185 LEMON_ASSERT(lo != INF, "Invalid bound");
186 _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
189 CbcMip::Value CbcMip::_getColLowerBound(int i) const {
190 double val = _prob->getColumnLower(i);
191 return val == - COIN_DBL_MAX ? - INF : val;
194 void CbcMip::_setColUpperBound(int i, Value up) {
195 LEMON_ASSERT(up != -INF, "Invalid bound");
196 _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
199 CbcMip::Value CbcMip::_getColUpperBound(int i) const {
200 double val = _prob->getColumnUpper(i);
201 return val == COIN_DBL_MAX ? INF : val;
204 void CbcMip::_setRowLowerBound(int i, Value lo) {
205 LEMON_ASSERT(lo != INF, "Invalid bound");
206 _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
209 CbcMip::Value CbcMip::_getRowLowerBound(int i) const {
210 double val = _prob->getRowLower(i);
211 return val == - COIN_DBL_MAX ? - INF : val;
214 void CbcMip::_setRowUpperBound(int i, Value up) {
215 LEMON_ASSERT(up != -INF, "Invalid bound");
216 _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
219 CbcMip::Value CbcMip::_getRowUpperBound(int i) const {
220 double val = _prob->getRowUpper(i);
221 return val == COIN_DBL_MAX ? INF : val;
224 void CbcMip::_setObjCoeffs(ExprIterator b, ExprIterator e) {
225 int num = _prob->numberColumns();
226 for (int i = 0; i < num; ++i) {
227 _prob->setColumnObjective(i, 0.0);
229 for (ExprIterator it = b; it != e; ++it) {
230 _prob->setColumnObjective(it->first, it->second);
234 void CbcMip::_getObjCoeffs(InsertIterator b) const {
235 int num = _prob->numberColumns();
236 for (int i = 0; i < num; ++i) {
237 Value coef = _prob->getColumnObjective(i);
239 *b = std::make_pair(i, coef);
245 void CbcMip::_setObjCoeff(int i, Value obj_coef) {
246 _prob->setColumnObjective(i, obj_coef);
249 CbcMip::Value CbcMip::_getObjCoeff(int i) const {
250 return _prob->getColumnObjective(i);
253 CbcMip::SolveExitStatus CbcMip::_solve() {
259 _osi_solver = new OsiClpSolverInterface();
261 _osi_solver = new OsiOslSolverInterface();
263 #error Cannot instantiate Osi solver
266 _osi_solver->loadFromCoinModel(*_prob);
271 _cbc_model= new CbcModel(*_osi_solver);
273 switch (_message_level) {
274 case MESSAGE_NO_OUTPUT:
275 _osi_solver->messageHandler()->setLogLevel(0);
276 _cbc_model->setLogLevel(0);
278 case MESSAGE_ERROR_MESSAGE:
279 _osi_solver->messageHandler()->setLogLevel(1);
280 _cbc_model->setLogLevel(1);
282 case MESSAGE_NORMAL_OUTPUT:
283 _osi_solver->messageHandler()->setLogLevel(2);
284 _cbc_model->setLogLevel(2);
286 case MESSAGE_FULL_OUTPUT:
287 _osi_solver->messageHandler()->setLogLevel(3);
288 _cbc_model->setLogLevel(3);
292 _cbc_model->initialSolve();
293 _cbc_model->solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
295 if (!_cbc_model->isInitialSolveAbandoned() &&
296 _cbc_model->isInitialSolveProvenOptimal() &&
297 !_cbc_model->isInitialSolveProvenPrimalInfeasible() &&
298 !_cbc_model->isInitialSolveProvenDualInfeasible()) {
300 CglProbing generator1;
301 generator1.setUsingObjective(true);
302 generator1.setMaxPass(3);
303 generator1.setMaxProbe(100);
304 generator1.setMaxLook(50);
305 generator1.setRowCuts(3);
306 _cbc_model->addCutGenerator(&generator1, -1, "Probing");
308 CglGomory generator2;
309 generator2.setLimit(300);
310 _cbc_model->addCutGenerator(&generator2, -1, "Gomory");
312 CglKnapsackCover generator3;
313 _cbc_model->addCutGenerator(&generator3, -1, "Knapsack");
315 CglOddHole generator4;
316 generator4.setMinimumViolation(0.005);
317 generator4.setMinimumViolationPer(0.00002);
318 generator4.setMaximumEntries(200);
319 _cbc_model->addCutGenerator(&generator4, -1, "OddHole");
321 CglClique generator5;
322 generator5.setStarCliqueReport(false);
323 generator5.setRowCliqueReport(false);
324 _cbc_model->addCutGenerator(&generator5, -1, "Clique");
326 CglMixedIntegerRounding mixedGen;
327 _cbc_model->addCutGenerator(&mixedGen, -1, "MixedIntegerRounding");
329 CglFlowCover flowGen;
330 _cbc_model->addCutGenerator(&flowGen, -1, "FlowCover");
333 OsiClpSolverInterface* osiclp =
334 dynamic_cast<OsiClpSolverInterface*>(_cbc_model->solver());
335 if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
336 osiclp->setupForRepeatedUse(2, 0);
340 CbcRounding heuristic1(*_cbc_model);
341 heuristic1.setWhen(3);
342 _cbc_model->addHeuristic(&heuristic1);
344 CbcHeuristicLocal heuristic2(*_cbc_model);
345 heuristic2.setWhen(3);
346 _cbc_model->addHeuristic(&heuristic2);
348 CbcHeuristicGreedyCover heuristic3(*_cbc_model);
349 heuristic3.setAlgorithm(11);
350 heuristic3.setWhen(3);
351 _cbc_model->addHeuristic(&heuristic3);
353 CbcHeuristicFPump heuristic4(*_cbc_model);
354 heuristic4.setWhen(3);
355 _cbc_model->addHeuristic(&heuristic4);
357 CbcHeuristicRINS heuristic5(*_cbc_model);
358 heuristic5.setWhen(3);
359 _cbc_model->addHeuristic(&heuristic5);
361 if (_cbc_model->getNumCols() < 500) {
362 _cbc_model->setMaximumCutPassesAtRoot(-100);
363 } else if (_cbc_model->getNumCols() < 5000) {
364 _cbc_model->setMaximumCutPassesAtRoot(100);
366 _cbc_model->setMaximumCutPassesAtRoot(20);
369 if (_cbc_model->getNumCols() < 5000) {
370 _cbc_model->setNumberStrong(10);
373 _cbc_model->solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
374 _cbc_model->branchAndBound();
377 if (_cbc_model->isAbandoned()) {
384 CbcMip::Value CbcMip::_getSol(int i) const {
385 return _cbc_model->getColSolution()[i];
388 CbcMip::Value CbcMip::_getSolValue() const {
389 return _cbc_model->getObjValue();
392 CbcMip::ProblemType CbcMip::_getType() const {
393 if (_cbc_model->isProvenOptimal()) {
395 } else if (_cbc_model->isContinuousUnbounded()) {
401 void CbcMip::_setSense(Sense sense) {
404 _prob->setOptimizationDirection(1.0);
407 _prob->setOptimizationDirection(- 1.0);
412 CbcMip::Sense CbcMip::_getSense() const {
413 if (_prob->optimizationDirection() > 0.0) {
415 } else if (_prob->optimizationDirection() < 0.0) {
418 LEMON_ASSERT(false, "Wrong sense");
419 return CbcMip::Sense();
423 void CbcMip::_setColType(int i, CbcMip::ColTypes col_type) {
426 _prob->setInteger(i);
429 _prob->setContinuous(i);
432 LEMON_ASSERT(false, "Wrong sense");
436 CbcMip::ColTypes CbcMip::_getColType(int i) const {
437 return _prob->getColumnIsInteger(i) ? INTEGER : REAL;
440 void CbcMip::_clear() {
451 _prob = new CoinModel();
456 void CbcMip::messageLevel(MessageLevel m) {
460 } //END OF NAMESPACE LEMON