1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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/lp_clp.h>
20 #include <coin/ClpSimplex.hpp>
25 _prob = new ClpSimplex();
27 messageLevel(MESSAGE_NO_OUTPUT);
30 LpClp::LpClp(const LpClp& other) {
31 _prob = new ClpSimplex(*other._prob);
35 messageLevel(MESSAGE_NO_OUTPUT);
43 void LpClp::_init_temporals() {
48 void LpClp::_clear_temporals() {
59 LpClp* LpClp::_newSolver() const {
60 LpClp* newlp = new LpClp;
64 LpClp* LpClp::_cloneSolver() const {
65 LpClp* copylp = new LpClp(*this);
69 const char* LpClp::_solverName() const { return "LpClp"; }
71 int LpClp::_addCol() {
72 _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0);
73 return _prob->numberColumns() - 1;
76 int LpClp::_addRow() {
77 _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
78 return _prob->numberRows() - 1;
82 void LpClp::_eraseCol(int c) {
83 _col_names_ref.erase(_prob->getColumnName(c));
84 _prob->deleteColumns(1, &c);
87 void LpClp::_eraseRow(int r) {
88 _row_names_ref.erase(_prob->getRowName(r));
89 _prob->deleteRows(1, &r);
92 void LpClp::_eraseColId(int i) {
97 void LpClp::_eraseRowId(int i) {
102 void LpClp::_getColName(int c, std::string& name) const {
103 name = _prob->getColumnName(c);
106 void LpClp::_setColName(int c, const std::string& name) {
107 _prob->setColumnName(c, const_cast<std::string&>(name));
108 _col_names_ref[name] = c;
111 int LpClp::_colByName(const std::string& name) const {
112 std::map<std::string, int>::const_iterator it = _col_names_ref.find(name);
113 return it != _col_names_ref.end() ? it->second : -1;
116 void LpClp::_getRowName(int r, std::string& name) const {
117 name = _prob->getRowName(r);
120 void LpClp::_setRowName(int r, const std::string& name) {
121 _prob->setRowName(r, const_cast<std::string&>(name));
122 _row_names_ref[name] = r;
125 int LpClp::_rowByName(const std::string& name) const {
126 std::map<std::string, int>::const_iterator it = _row_names_ref.find(name);
127 return it != _row_names_ref.end() ? it->second : -1;
131 void LpClp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
132 std::map<int, Value> coeffs;
134 int n = _prob->clpMatrix()->getNumCols();
136 const int* indices = _prob->clpMatrix()->getIndices();
137 const double* elements = _prob->clpMatrix()->getElements();
139 for (int i = 0; i < n; ++i) {
140 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
141 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
143 const int* it = std::lower_bound(indices + begin, indices + end, ix);
144 if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
149 for (ExprIterator it = b; it != e; ++it) {
150 coeffs[it->first] = it->second;
153 for (std::map<int, Value>::iterator it = coeffs.begin();
154 it != coeffs.end(); ++it) {
155 _prob->modifyCoefficient(ix, it->first, it->second);
159 void LpClp::_getRowCoeffs(int ix, InsertIterator b) const {
160 int n = _prob->clpMatrix()->getNumCols();
162 const int* indices = _prob->clpMatrix()->getIndices();
163 const double* elements = _prob->clpMatrix()->getElements();
165 for (int i = 0; i < n; ++i) {
166 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
167 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
169 const int* it = std::lower_bound(indices + begin, indices + end, ix);
170 if (it != indices + end && *it == ix) {
171 *b = std::make_pair(i, elements[it - indices]);
176 void LpClp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
177 std::map<int, Value> coeffs;
179 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
180 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
182 const int* indices = _prob->clpMatrix()->getIndices();
183 const double* elements = _prob->clpMatrix()->getElements();
185 for (CoinBigIndex i = begin; i != end; ++i) {
186 if (elements[i] != 0.0) {
187 coeffs[indices[i]] = 0.0;
190 for (ExprIterator it = b; it != e; ++it) {
191 coeffs[it->first] = it->second;
193 for (std::map<int, Value>::iterator it = coeffs.begin();
194 it != coeffs.end(); ++it) {
195 _prob->modifyCoefficient(it->first, ix, it->second);
199 void LpClp::_getColCoeffs(int ix, InsertIterator b) const {
200 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
201 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
203 const int* indices = _prob->clpMatrix()->getIndices();
204 const double* elements = _prob->clpMatrix()->getElements();
206 for (CoinBigIndex i = begin; i != end; ++i) {
207 *b = std::make_pair(indices[i], elements[i]);
212 void LpClp::_setCoeff(int ix, int jx, Value value) {
213 _prob->modifyCoefficient(ix, jx, value);
216 LpClp::Value LpClp::_getCoeff(int ix, int jx) const {
217 CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
218 CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
220 const int* indices = _prob->clpMatrix()->getIndices();
221 const double* elements = _prob->clpMatrix()->getElements();
223 const int* it = std::lower_bound(indices + begin, indices + end, jx);
224 if (it != indices + end && *it == jx) {
225 return elements[it - indices];
231 void LpClp::_setColLowerBound(int i, Value lo) {
232 _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
235 LpClp::Value LpClp::_getColLowerBound(int i) const {
236 double val = _prob->getColLower()[i];
237 return val == - COIN_DBL_MAX ? - INF : val;
240 void LpClp::_setColUpperBound(int i, Value up) {
241 _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
244 LpClp::Value LpClp::_getColUpperBound(int i) const {
245 double val = _prob->getColUpper()[i];
246 return val == COIN_DBL_MAX ? INF : val;
249 void LpClp::_setRowLowerBound(int i, Value lo) {
250 _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
253 LpClp::Value LpClp::_getRowLowerBound(int i) const {
254 double val = _prob->getRowLower()[i];
255 return val == - COIN_DBL_MAX ? - INF : val;
258 void LpClp::_setRowUpperBound(int i, Value up) {
259 _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
262 LpClp::Value LpClp::_getRowUpperBound(int i) const {
263 double val = _prob->getRowUpper()[i];
264 return val == COIN_DBL_MAX ? INF : val;
267 void LpClp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
268 int num = _prob->clpMatrix()->getNumCols();
269 for (int i = 0; i < num; ++i) {
270 _prob->setObjectiveCoefficient(i, 0.0);
272 for (ExprIterator it = b; it != e; ++it) {
273 _prob->setObjectiveCoefficient(it->first, it->second);
277 void LpClp::_getObjCoeffs(InsertIterator b) const {
278 int num = _prob->clpMatrix()->getNumCols();
279 for (int i = 0; i < num; ++i) {
280 Value coef = _prob->getObjCoefficients()[i];
282 *b = std::make_pair(i, coef);
288 void LpClp::_setObjCoeff(int i, Value obj_coef) {
289 _prob->setObjectiveCoefficient(i, obj_coef);
292 LpClp::Value LpClp::_getObjCoeff(int i) const {
293 return _prob->getObjCoefficients()[i];
296 LpClp::SolveExitStatus LpClp::_solve() {
297 return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
300 LpClp::SolveExitStatus LpClp::solvePrimal() {
301 return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
304 LpClp::SolveExitStatus LpClp::solveDual() {
305 return _prob->dual() >= 0 ? SOLVED : UNSOLVED;
308 LpClp::SolveExitStatus LpClp::solveBarrier() {
309 return _prob->barrier() >= 0 ? SOLVED : UNSOLVED;
312 LpClp::Value LpClp::_getPrimal(int i) const {
313 return _prob->primalColumnSolution()[i];
315 LpClp::Value LpClp::_getPrimalValue() const {
316 return _prob->objectiveValue();
319 LpClp::Value LpClp::_getDual(int i) const {
320 return _prob->dualRowSolution()[i];
323 LpClp::Value LpClp::_getPrimalRay(int i) const {
325 _primal_ray = _prob->unboundedRay();
326 LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided");
328 return _primal_ray[i];
331 LpClp::Value LpClp::_getDualRay(int i) const {
333 _dual_ray = _prob->infeasibilityRay();
334 LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided");
339 LpClp::VarStatus LpClp::_getColStatus(int i) const {
340 switch (_prob->getColumnStatus(i)) {
341 case ClpSimplex::basic:
343 case ClpSimplex::isFree:
345 case ClpSimplex::atUpperBound:
347 case ClpSimplex::atLowerBound:
349 case ClpSimplex::isFixed:
351 case ClpSimplex::superBasic:
354 LEMON_ASSERT(false, "Wrong column status");
359 LpClp::VarStatus LpClp::_getRowStatus(int i) const {
360 switch (_prob->getColumnStatus(i)) {
361 case ClpSimplex::basic:
363 case ClpSimplex::isFree:
365 case ClpSimplex::atUpperBound:
367 case ClpSimplex::atLowerBound:
369 case ClpSimplex::isFixed:
371 case ClpSimplex::superBasic:
374 LEMON_ASSERT(false, "Wrong row status");
380 LpClp::ProblemType LpClp::_getPrimalType() const {
381 if (_prob->isProvenOptimal()) {
383 } else if (_prob->isProvenPrimalInfeasible()) {
385 } else if (_prob->isProvenDualInfeasible()) {
392 LpClp::ProblemType LpClp::_getDualType() const {
393 if (_prob->isProvenOptimal()) {
395 } else if (_prob->isProvenDualInfeasible()) {
397 } else if (_prob->isProvenPrimalInfeasible()) {
404 void LpClp::_setSense(LpClp::Sense sense) {
407 _prob->setOptimizationDirection(1);
410 _prob->setOptimizationDirection(-1);
415 LpClp::Sense LpClp::_getSense() const {
416 double dir = _prob->optimizationDirection();
424 void LpClp::_clear() {
426 _prob = new ClpSimplex();
429 _col_names_ref.clear();
433 void LpClp::messageLevel(MessageLevel m) {
434 _prob->setLogLevel(static_cast<int>(m));
437 } //END OF NAMESPACE LEMON