alpar@484: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@484: * alpar@484: * This file is a part of LEMON, a generic C++ optimization library. alpar@484: * alpar@956: * Copyright (C) 2003-2010 alpar@484: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@484: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@484: * alpar@484: * Permission to use, modify and distribute this software is granted alpar@484: * provided that this copyright notice appears in all copies. For alpar@484: * precise terms see the accompanying LICENSE file. alpar@484: * alpar@484: * This software is provided "AS IS" with no warranty of any kind, alpar@484: * express or implied, and with no claim as to its suitability for any alpar@484: * purpose. alpar@484: * alpar@484: */ alpar@484: alpar@484: #include alpar@484: #include alpar@484: alpar@484: namespace lemon { alpar@484: alpar@485: ClpLp::ClpLp() { alpar@484: _prob = new ClpSimplex(); alpar@484: _init_temporals(); deba@623: messageLevel(MESSAGE_NOTHING); alpar@484: } alpar@484: alpar@485: ClpLp::ClpLp(const ClpLp& other) { alpar@484: _prob = new ClpSimplex(*other._prob); alpar@484: rows = other.rows; alpar@484: cols = other.cols; alpar@484: _init_temporals(); deba@623: messageLevel(MESSAGE_NOTHING); alpar@484: } alpar@484: alpar@485: ClpLp::~ClpLp() { alpar@484: delete _prob; alpar@484: _clear_temporals(); alpar@484: } alpar@484: alpar@485: void ClpLp::_init_temporals() { alpar@484: _primal_ray = 0; alpar@484: _dual_ray = 0; alpar@484: } alpar@484: alpar@485: void ClpLp::_clear_temporals() { alpar@484: if (_primal_ray) { alpar@484: delete[] _primal_ray; alpar@484: _primal_ray = 0; alpar@484: } alpar@484: if (_dual_ray) { alpar@484: delete[] _dual_ray; alpar@484: _dual_ray = 0; alpar@484: } alpar@484: } alpar@484: alpar@587: ClpLp* ClpLp::newSolver() const { alpar@485: ClpLp* newlp = new ClpLp; alpar@484: return newlp; alpar@484: } alpar@484: alpar@587: ClpLp* ClpLp::cloneSolver() const { alpar@485: ClpLp* copylp = new ClpLp(*this); alpar@484: return copylp; alpar@484: } alpar@484: alpar@485: const char* ClpLp::_solverName() const { return "ClpLp"; } alpar@484: alpar@485: int ClpLp::_addCol() { alpar@484: _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0); alpar@484: return _prob->numberColumns() - 1; alpar@484: } alpar@484: alpar@485: int ClpLp::_addRow() { alpar@484: _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX); alpar@484: return _prob->numberRows() - 1; alpar@484: } alpar@484: deba@793: int ClpLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) { deba@793: std::vector indexes; deba@793: std::vector values; deba@793: deba@793: for(ExprIterator it = b; it != e; ++it) { deba@793: indexes.push_back(it->first); deba@793: values.push_back(it->second); deba@793: } deba@793: deba@793: _prob->addRow(values.size(), &indexes.front(), &values.front(), l, u); deba@793: return _prob->numberRows() - 1; deba@793: } deba@793: alpar@484: alpar@485: void ClpLp::_eraseCol(int c) { alpar@484: _col_names_ref.erase(_prob->getColumnName(c)); alpar@484: _prob->deleteColumns(1, &c); alpar@484: } alpar@484: alpar@485: void ClpLp::_eraseRow(int r) { alpar@484: _row_names_ref.erase(_prob->getRowName(r)); alpar@484: _prob->deleteRows(1, &r); alpar@484: } alpar@484: alpar@485: void ClpLp::_eraseColId(int i) { alpar@484: cols.eraseIndex(i); alpar@484: cols.shiftIndices(i); alpar@484: } alpar@484: alpar@485: void ClpLp::_eraseRowId(int i) { alpar@484: rows.eraseIndex(i); alpar@484: rows.shiftIndices(i); alpar@484: } alpar@484: alpar@485: void ClpLp::_getColName(int c, std::string& name) const { alpar@484: name = _prob->getColumnName(c); alpar@484: } alpar@484: alpar@485: void ClpLp::_setColName(int c, const std::string& name) { alpar@484: _prob->setColumnName(c, const_cast(name)); alpar@484: _col_names_ref[name] = c; alpar@484: } alpar@484: alpar@485: int ClpLp::_colByName(const std::string& name) const { alpar@484: std::map::const_iterator it = _col_names_ref.find(name); alpar@484: return it != _col_names_ref.end() ? it->second : -1; alpar@484: } alpar@484: alpar@485: void ClpLp::_getRowName(int r, std::string& name) const { alpar@484: name = _prob->getRowName(r); alpar@484: } alpar@484: alpar@485: void ClpLp::_setRowName(int r, const std::string& name) { alpar@484: _prob->setRowName(r, const_cast(name)); alpar@484: _row_names_ref[name] = r; alpar@484: } alpar@484: alpar@485: int ClpLp::_rowByName(const std::string& name) const { alpar@484: std::map::const_iterator it = _row_names_ref.find(name); alpar@484: return it != _row_names_ref.end() ? it->second : -1; alpar@484: } alpar@484: alpar@484: alpar@485: void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) { alpar@484: std::map coeffs; alpar@484: alpar@484: int n = _prob->clpMatrix()->getNumCols(); alpar@484: alpar@484: const int* indices = _prob->clpMatrix()->getIndices(); alpar@484: const double* elements = _prob->clpMatrix()->getElements(); alpar@484: alpar@484: for (int i = 0; i < n; ++i) { alpar@484: CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; alpar@484: CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; alpar@484: alpar@484: const int* it = std::lower_bound(indices + begin, indices + end, ix); alpar@484: if (it != indices + end && *it == ix && elements[it - indices] != 0.0) { alpar@484: coeffs[i] = 0.0; alpar@484: } alpar@484: } alpar@484: alpar@484: for (ExprIterator it = b; it != e; ++it) { alpar@484: coeffs[it->first] = it->second; alpar@484: } alpar@484: alpar@484: for (std::map::iterator it = coeffs.begin(); alpar@484: it != coeffs.end(); ++it) { alpar@484: _prob->modifyCoefficient(ix, it->first, it->second); alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const { alpar@484: int n = _prob->clpMatrix()->getNumCols(); alpar@484: alpar@484: const int* indices = _prob->clpMatrix()->getIndices(); alpar@484: const double* elements = _prob->clpMatrix()->getElements(); alpar@484: alpar@484: for (int i = 0; i < n; ++i) { alpar@484: CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; alpar@484: CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; alpar@484: alpar@484: const int* it = std::lower_bound(indices + begin, indices + end, ix); alpar@484: if (it != indices + end && *it == ix) { alpar@484: *b = std::make_pair(i, elements[it - indices]); alpar@484: } alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) { alpar@484: std::map coeffs; alpar@484: alpar@484: CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; alpar@484: CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; alpar@484: alpar@484: const int* indices = _prob->clpMatrix()->getIndices(); alpar@484: const double* elements = _prob->clpMatrix()->getElements(); alpar@484: alpar@484: for (CoinBigIndex i = begin; i != end; ++i) { alpar@484: if (elements[i] != 0.0) { alpar@484: coeffs[indices[i]] = 0.0; alpar@484: } alpar@484: } alpar@484: for (ExprIterator it = b; it != e; ++it) { alpar@484: coeffs[it->first] = it->second; alpar@484: } alpar@484: for (std::map::iterator it = coeffs.begin(); alpar@484: it != coeffs.end(); ++it) { alpar@484: _prob->modifyCoefficient(it->first, ix, it->second); alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_getColCoeffs(int ix, InsertIterator b) const { alpar@484: CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; alpar@484: CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; alpar@484: alpar@484: const int* indices = _prob->clpMatrix()->getIndices(); alpar@484: const double* elements = _prob->clpMatrix()->getElements(); alpar@484: alpar@484: for (CoinBigIndex i = begin; i != end; ++i) { alpar@484: *b = std::make_pair(indices[i], elements[i]); alpar@484: ++b; alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_setCoeff(int ix, int jx, Value value) { alpar@484: _prob->modifyCoefficient(ix, jx, value); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const { alpar@484: CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; alpar@484: CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; alpar@484: alpar@484: const int* indices = _prob->clpMatrix()->getIndices(); alpar@484: const double* elements = _prob->clpMatrix()->getElements(); alpar@484: alpar@484: const int* it = std::lower_bound(indices + begin, indices + end, jx); alpar@484: if (it != indices + end && *it == jx) { alpar@484: return elements[it - indices]; alpar@484: } else { alpar@484: return 0.0; alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_setColLowerBound(int i, Value lo) { alpar@484: _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getColLowerBound(int i) const { alpar@484: double val = _prob->getColLower()[i]; alpar@484: return val == - COIN_DBL_MAX ? - INF : val; alpar@484: } alpar@484: alpar@485: void ClpLp::_setColUpperBound(int i, Value up) { alpar@484: _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getColUpperBound(int i) const { alpar@484: double val = _prob->getColUpper()[i]; alpar@484: return val == COIN_DBL_MAX ? INF : val; alpar@484: } alpar@484: alpar@485: void ClpLp::_setRowLowerBound(int i, Value lo) { alpar@484: _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getRowLowerBound(int i) const { alpar@484: double val = _prob->getRowLower()[i]; alpar@484: return val == - COIN_DBL_MAX ? - INF : val; alpar@484: } alpar@484: alpar@485: void ClpLp::_setRowUpperBound(int i, Value up) { alpar@484: _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getRowUpperBound(int i) const { alpar@484: double val = _prob->getRowUpper()[i]; alpar@484: return val == COIN_DBL_MAX ? INF : val; alpar@484: } alpar@484: alpar@485: void ClpLp::_setObjCoeffs(ExprIterator b, ExprIterator e) { alpar@484: int num = _prob->clpMatrix()->getNumCols(); alpar@484: for (int i = 0; i < num; ++i) { alpar@484: _prob->setObjectiveCoefficient(i, 0.0); alpar@484: } alpar@484: for (ExprIterator it = b; it != e; ++it) { alpar@484: _prob->setObjectiveCoefficient(it->first, it->second); alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_getObjCoeffs(InsertIterator b) const { alpar@484: int num = _prob->clpMatrix()->getNumCols(); alpar@484: for (int i = 0; i < num; ++i) { alpar@484: Value coef = _prob->getObjCoefficients()[i]; alpar@484: if (coef != 0.0) { alpar@484: *b = std::make_pair(i, coef); alpar@484: ++b; alpar@484: } alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_setObjCoeff(int i, Value obj_coef) { alpar@484: _prob->setObjectiveCoefficient(i, obj_coef); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getObjCoeff(int i) const { alpar@484: return _prob->getObjCoefficients()[i]; alpar@484: } alpar@484: alpar@485: ClpLp::SolveExitStatus ClpLp::_solve() { alpar@484: return _prob->primal() >= 0 ? SOLVED : UNSOLVED; alpar@484: } alpar@484: alpar@485: ClpLp::SolveExitStatus ClpLp::solvePrimal() { alpar@484: return _prob->primal() >= 0 ? SOLVED : UNSOLVED; alpar@484: } alpar@484: alpar@485: ClpLp::SolveExitStatus ClpLp::solveDual() { alpar@484: return _prob->dual() >= 0 ? SOLVED : UNSOLVED; alpar@484: } alpar@484: alpar@485: ClpLp::SolveExitStatus ClpLp::solveBarrier() { alpar@484: return _prob->barrier() >= 0 ? SOLVED : UNSOLVED; alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getPrimal(int i) const { alpar@484: return _prob->primalColumnSolution()[i]; alpar@484: } alpar@485: ClpLp::Value ClpLp::_getPrimalValue() const { alpar@484: return _prob->objectiveValue(); alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getDual(int i) const { alpar@484: return _prob->dualRowSolution()[i]; alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getPrimalRay(int i) const { alpar@484: if (!_primal_ray) { alpar@484: _primal_ray = _prob->unboundedRay(); alpar@484: LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided"); alpar@484: } alpar@484: return _primal_ray[i]; alpar@484: } alpar@484: alpar@485: ClpLp::Value ClpLp::_getDualRay(int i) const { alpar@484: if (!_dual_ray) { alpar@484: _dual_ray = _prob->infeasibilityRay(); alpar@484: LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided"); alpar@484: } alpar@484: return _dual_ray[i]; alpar@484: } alpar@484: alpar@485: ClpLp::VarStatus ClpLp::_getColStatus(int i) const { alpar@484: switch (_prob->getColumnStatus(i)) { alpar@484: case ClpSimplex::basic: alpar@484: return BASIC; alpar@484: case ClpSimplex::isFree: alpar@484: return FREE; alpar@484: case ClpSimplex::atUpperBound: alpar@484: return UPPER; alpar@484: case ClpSimplex::atLowerBound: alpar@484: return LOWER; alpar@484: case ClpSimplex::isFixed: alpar@484: return FIXED; alpar@484: case ClpSimplex::superBasic: alpar@484: return FREE; alpar@484: default: alpar@484: LEMON_ASSERT(false, "Wrong column status"); alpar@484: return VarStatus(); alpar@484: } alpar@484: } alpar@484: alpar@485: ClpLp::VarStatus ClpLp::_getRowStatus(int i) const { alpar@484: switch (_prob->getColumnStatus(i)) { alpar@484: case ClpSimplex::basic: alpar@484: return BASIC; alpar@484: case ClpSimplex::isFree: alpar@484: return FREE; alpar@484: case ClpSimplex::atUpperBound: alpar@484: return UPPER; alpar@484: case ClpSimplex::atLowerBound: alpar@484: return LOWER; alpar@484: case ClpSimplex::isFixed: alpar@484: return FIXED; alpar@484: case ClpSimplex::superBasic: alpar@484: return FREE; alpar@484: default: alpar@484: LEMON_ASSERT(false, "Wrong row status"); alpar@484: return VarStatus(); alpar@484: } alpar@484: } alpar@484: alpar@484: alpar@485: ClpLp::ProblemType ClpLp::_getPrimalType() const { alpar@484: if (_prob->isProvenOptimal()) { alpar@484: return OPTIMAL; alpar@484: } else if (_prob->isProvenPrimalInfeasible()) { alpar@484: return INFEASIBLE; alpar@484: } else if (_prob->isProvenDualInfeasible()) { alpar@484: return UNBOUNDED; alpar@484: } else { alpar@484: return UNDEFINED; alpar@484: } alpar@484: } alpar@484: alpar@485: ClpLp::ProblemType ClpLp::_getDualType() const { alpar@484: if (_prob->isProvenOptimal()) { alpar@484: return OPTIMAL; alpar@484: } else if (_prob->isProvenDualInfeasible()) { alpar@484: return INFEASIBLE; alpar@484: } else if (_prob->isProvenPrimalInfeasible()) { alpar@484: return INFEASIBLE; alpar@484: } else { alpar@484: return UNDEFINED; alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_setSense(ClpLp::Sense sense) { alpar@484: switch (sense) { alpar@484: case MIN: alpar@484: _prob->setOptimizationDirection(1); alpar@484: break; alpar@484: case MAX: alpar@484: _prob->setOptimizationDirection(-1); alpar@484: break; alpar@484: } alpar@484: } alpar@484: alpar@485: ClpLp::Sense ClpLp::_getSense() const { alpar@484: double dir = _prob->optimizationDirection(); alpar@484: if (dir > 0.0) { alpar@484: return MIN; alpar@484: } else { alpar@484: return MAX; alpar@484: } alpar@484: } alpar@484: alpar@485: void ClpLp::_clear() { alpar@484: delete _prob; alpar@484: _prob = new ClpSimplex(); alpar@484: _col_names_ref.clear(); alpar@484: _clear_temporals(); alpar@484: } alpar@484: deba@623: void ClpLp::_messageLevel(MessageLevel level) { deba@623: switch (level) { deba@623: case MESSAGE_NOTHING: deba@623: _prob->setLogLevel(0); deba@623: break; deba@623: case MESSAGE_ERROR: deba@623: _prob->setLogLevel(1); deba@623: break; deba@623: case MESSAGE_WARNING: deba@623: _prob->setLogLevel(2); deba@623: break; deba@623: case MESSAGE_NORMAL: deba@623: _prob->setLogLevel(3); deba@623: break; deba@623: case MESSAGE_VERBOSE: deba@623: _prob->setLogLevel(4); deba@623: break; deba@623: } alpar@484: } alpar@484: alpar@484: } //END OF NAMESPACE LEMON