1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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
19 #include <lemon/clp.h>
20 #include <coin/ClpSimplex.hpp>
25 _prob = new ClpSimplex();
27 messageLevel(MESSAGE_NOTHING);
30 ClpLp::ClpLp(const ClpLp& other) {
31 _prob = new ClpSimplex(*other._prob);
35 messageLevel(MESSAGE_NOTHING);
43 void ClpLp::_init_temporals() {
48 void ClpLp::_clear_temporals() {
59 ClpLp* ClpLp::newSolver() const {
60 ClpLp* newlp = new ClpLp;
64 ClpLp* ClpLp::cloneSolver() const {
65 ClpLp* copylp = new ClpLp(*this);
69 const char* ClpLp::_solverName() const { return "ClpLp"; }
71 int ClpLp::_addCol() {
72 _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0);
73 return _prob->numberColumns() - 1;
76 int ClpLp::_addRow() {
77 _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
78 return _prob->numberRows() - 1;
81 int ClpLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
82 std::vector<int> indexes;
83 std::vector<Value> values;
85 for(ExprIterator it = b; it != e; ++it) {
86 indexes.push_back(it->first);
87 values.push_back(it->second);
90 _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u);
91 return _prob->numberRows() - 1;
95 void ClpLp::_eraseCol(int c) {
96 _col_names_ref.erase(_prob->getColumnName(c));
97 _prob->deleteColumns(1, &c);
100 void ClpLp::_eraseRow(int r) {
101 _row_names_ref.erase(_prob->getRowName(r));
102 _prob->deleteRows(1, &r);
105 void ClpLp::_eraseColId(int i) {
107 _cols.shiftIndices(i);
110 void ClpLp::_eraseRowId(int i) {
112 _rows.shiftIndices(i);
115 void ClpLp::_getColName(int c, std::string& name) const {
116 name = _prob->getColumnName(c);
119 void ClpLp::_setColName(int c, const std::string& name) {
120 _prob->setColumnName(c, const_cast<std::string&>(name));
121 _col_names_ref[name] = c;
124 int ClpLp::_colByName(const std::string& name) const {
125 std::map<std::string, int>::const_iterator it = _col_names_ref.find(name);
126 return it != _col_names_ref.end() ? it->second : -1;
129 void ClpLp::_getRowName(int r, std::string& name) const {
130 name = _prob->getRowName(r);
133 void ClpLp::_setRowName(int r, const std::string& name) {
134 _prob->setRowName(r, const_cast<std::string&>(name));
135 _row_names_ref[name] = r;
138 int ClpLp::_rowByName(const std::string& name) const {
139 std::map<std::string, int>::const_iterator it = _row_names_ref.find(name);
140 return it != _row_names_ref.end() ? it->second : -1;
144 void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
145 std::map<int, Value> coeffs;
147 int n = _prob->clpMatrix()->getNumCols();
149 const int* indices = _prob->clpMatrix()->getIndices();
150 const double* elements = _prob->clpMatrix()->getElements();
152 for (int i = 0; i < n; ++i) {
153 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
154 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
156 const int* it = std::lower_bound(indices + begin, indices + end, ix);
157 if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
162 for (ExprIterator it = b; it != e; ++it) {
163 coeffs[it->first] = it->second;
166 for (std::map<int, Value>::iterator it = coeffs.begin();
167 it != coeffs.end(); ++it) {
168 _prob->modifyCoefficient(ix, it->first, it->second);
172 void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const {
173 int n = _prob->clpMatrix()->getNumCols();
175 const int* indices = _prob->clpMatrix()->getIndices();
176 const double* elements = _prob->clpMatrix()->getElements();
178 for (int i = 0; i < n; ++i) {
179 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
180 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
182 const int* it = std::lower_bound(indices + begin, indices + end, ix);
183 if (it != indices + end && *it == ix) {
184 *b = std::make_pair(i, elements[it - indices]);
189 void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
190 std::map<int, Value> coeffs;
192 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
193 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
195 const int* indices = _prob->clpMatrix()->getIndices();
196 const double* elements = _prob->clpMatrix()->getElements();
198 for (CoinBigIndex i = begin; i != end; ++i) {
199 if (elements[i] != 0.0) {
200 coeffs[indices[i]] = 0.0;
203 for (ExprIterator it = b; it != e; ++it) {
204 coeffs[it->first] = it->second;
206 for (std::map<int, Value>::iterator it = coeffs.begin();
207 it != coeffs.end(); ++it) {
208 _prob->modifyCoefficient(it->first, ix, it->second);
212 void ClpLp::_getColCoeffs(int ix, InsertIterator b) const {
213 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
214 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
216 const int* indices = _prob->clpMatrix()->getIndices();
217 const double* elements = _prob->clpMatrix()->getElements();
219 for (CoinBigIndex i = begin; i != end; ++i) {
220 *b = std::make_pair(indices[i], elements[i]);
225 void ClpLp::_setCoeff(int ix, int jx, Value value) {
226 _prob->modifyCoefficient(ix, jx, value);
229 ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const {
230 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
231 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
233 const int* indices = _prob->clpMatrix()->getIndices();
234 const double* elements = _prob->clpMatrix()->getElements();
236 const int* it = std::lower_bound(indices + begin, indices + end, jx);
237 if (it != indices + end && *it == jx) {
238 return elements[it - indices];
244 void ClpLp::_setColLowerBound(int i, Value lo) {
245 _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
248 ClpLp::Value ClpLp::_getColLowerBound(int i) const {
249 double val = _prob->getColLower()[i];
250 return val == - COIN_DBL_MAX ? - INF : val;
253 void ClpLp::_setColUpperBound(int i, Value up) {
254 _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
257 ClpLp::Value ClpLp::_getColUpperBound(int i) const {
258 double val = _prob->getColUpper()[i];
259 return val == COIN_DBL_MAX ? INF : val;
262 void ClpLp::_setRowLowerBound(int i, Value lo) {
263 _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
266 ClpLp::Value ClpLp::_getRowLowerBound(int i) const {
267 double val = _prob->getRowLower()[i];
268 return val == - COIN_DBL_MAX ? - INF : val;
271 void ClpLp::_setRowUpperBound(int i, Value up) {
272 _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
275 ClpLp::Value ClpLp::_getRowUpperBound(int i) const {
276 double val = _prob->getRowUpper()[i];
277 return val == COIN_DBL_MAX ? INF : val;
280 void ClpLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
281 int num = _prob->clpMatrix()->getNumCols();
282 for (int i = 0; i < num; ++i) {
283 _prob->setObjectiveCoefficient(i, 0.0);
285 for (ExprIterator it = b; it != e; ++it) {
286 _prob->setObjectiveCoefficient(it->first, it->second);
290 void ClpLp::_getObjCoeffs(InsertIterator b) const {
291 int num = _prob->clpMatrix()->getNumCols();
292 for (int i = 0; i < num; ++i) {
293 Value coef = _prob->getObjCoefficients()[i];
295 *b = std::make_pair(i, coef);
301 void ClpLp::_setObjCoeff(int i, Value obj_coef) {
302 _prob->setObjectiveCoefficient(i, obj_coef);
305 ClpLp::Value ClpLp::_getObjCoeff(int i) const {
306 return _prob->getObjCoefficients()[i];
309 ClpLp::SolveExitStatus ClpLp::_solve() {
310 return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
313 ClpLp::SolveExitStatus ClpLp::solvePrimal() {
314 return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
317 ClpLp::SolveExitStatus ClpLp::solveDual() {
318 return _prob->dual() >= 0 ? SOLVED : UNSOLVED;
321 ClpLp::SolveExitStatus ClpLp::solveBarrier() {
322 return _prob->barrier() >= 0 ? SOLVED : UNSOLVED;
325 ClpLp::Value ClpLp::_getPrimal(int i) const {
326 return _prob->primalColumnSolution()[i];
328 ClpLp::Value ClpLp::_getPrimalValue() const {
329 return _prob->objectiveValue();
332 ClpLp::Value ClpLp::_getDual(int i) const {
333 return _prob->dualRowSolution()[i];
336 ClpLp::Value ClpLp::_getPrimalRay(int i) const {
338 _primal_ray = _prob->unboundedRay();
339 LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided");
341 return _primal_ray[i];
344 ClpLp::Value ClpLp::_getDualRay(int i) const {
346 _dual_ray = _prob->infeasibilityRay();
347 LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided");
352 ClpLp::VarStatus ClpLp::_getColStatus(int i) const {
353 switch (_prob->getColumnStatus(i)) {
354 case ClpSimplex::basic:
356 case ClpSimplex::isFree:
358 case ClpSimplex::atUpperBound:
360 case ClpSimplex::atLowerBound:
362 case ClpSimplex::isFixed:
364 case ClpSimplex::superBasic:
367 LEMON_ASSERT(false, "Wrong column status");
372 ClpLp::VarStatus ClpLp::_getRowStatus(int i) const {
373 switch (_prob->getColumnStatus(i)) {
374 case ClpSimplex::basic:
376 case ClpSimplex::isFree:
378 case ClpSimplex::atUpperBound:
380 case ClpSimplex::atLowerBound:
382 case ClpSimplex::isFixed:
384 case ClpSimplex::superBasic:
387 LEMON_ASSERT(false, "Wrong row status");
393 ClpLp::ProblemType ClpLp::_getPrimalType() const {
394 if (_prob->isProvenOptimal()) {
396 } else if (_prob->isProvenPrimalInfeasible()) {
398 } else if (_prob->isProvenDualInfeasible()) {
405 ClpLp::ProblemType ClpLp::_getDualType() const {
406 if (_prob->isProvenOptimal()) {
408 } else if (_prob->isProvenDualInfeasible()) {
410 } else if (_prob->isProvenPrimalInfeasible()) {
417 void ClpLp::_setSense(ClpLp::Sense sense) {
420 _prob->setOptimizationDirection(1);
423 _prob->setOptimizationDirection(-1);
428 ClpLp::Sense ClpLp::_getSense() const {
429 double dir = _prob->optimizationDirection();
437 void ClpLp::_clear() {
439 _prob = new ClpSimplex();
440 _col_names_ref.clear();
444 void ClpLp::_messageLevel(MessageLevel level) {
446 case MESSAGE_NOTHING:
447 _prob->setLogLevel(0);
450 _prob->setLogLevel(1);
452 case MESSAGE_WARNING:
453 _prob->setLogLevel(2);
456 _prob->setLogLevel(3);
458 case MESSAGE_VERBOSE:
459 _prob->setLogLevel(4);
464 } //END OF NAMESPACE LEMON