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