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@484: * Copyright (C) 2003-2008 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: #include alpar@484: alpar@484: alpar@484: ///\file alpar@484: ///\brief Implementation of the LEMON-SOPLEX lp solver interface. alpar@484: namespace lemon { alpar@484: alpar@485: SoplexLp::SoplexLp() { alpar@484: soplex = new soplex::SoPlex; alpar@484: } alpar@484: alpar@485: SoplexLp::~SoplexLp() { alpar@484: delete soplex; alpar@484: } alpar@484: alpar@485: SoplexLp::SoplexLp(const SoplexLp& lp) { alpar@484: rows = lp.rows; alpar@484: cols = lp.cols; alpar@484: alpar@484: soplex = new soplex::SoPlex; alpar@484: (*static_cast(soplex)) = *(lp.soplex); alpar@484: alpar@484: _col_names = lp._col_names; alpar@484: _col_names_ref = lp._col_names_ref; alpar@484: alpar@484: _row_names = lp._row_names; alpar@484: _row_names_ref = lp._row_names_ref; alpar@484: alpar@484: } alpar@484: alpar@485: void SoplexLp::_clear_temporals() { alpar@484: _primal_values.clear(); alpar@484: _dual_values.clear(); alpar@484: } alpar@484: alpar@485: SoplexLp* SoplexLp::_newSolver() const { alpar@485: SoplexLp* newlp = new SoplexLp(); alpar@484: return newlp; alpar@484: } alpar@484: alpar@485: SoplexLp* SoplexLp::_cloneSolver() const { alpar@485: SoplexLp* newlp = new SoplexLp(*this); alpar@484: return newlp; alpar@484: } alpar@484: alpar@485: const char* SoplexLp::_solverName() const { return "SoplexLp"; } alpar@484: alpar@485: int SoplexLp::_addCol() { alpar@484: soplex::LPCol c; alpar@484: c.setLower(-soplex::infinity); alpar@484: c.setUpper(soplex::infinity); alpar@484: soplex->addCol(c); alpar@484: alpar@484: _col_names.push_back(std::string()); alpar@484: alpar@484: return soplex->nCols() - 1; alpar@484: } alpar@484: alpar@485: int SoplexLp::_addRow() { alpar@484: soplex::LPRow r; alpar@484: r.setLhs(-soplex::infinity); alpar@484: r.setRhs(soplex::infinity); alpar@484: soplex->addRow(r); alpar@484: alpar@484: _row_names.push_back(std::string()); alpar@484: alpar@484: return soplex->nRows() - 1; alpar@484: } alpar@484: alpar@484: alpar@485: void SoplexLp::_eraseCol(int i) { alpar@484: soplex->removeCol(i); alpar@484: _col_names_ref.erase(_col_names[i]); alpar@484: _col_names[i] = _col_names.back(); alpar@484: _col_names_ref[_col_names.back()] = i; alpar@484: _col_names.pop_back(); alpar@484: } alpar@484: alpar@485: void SoplexLp::_eraseRow(int i) { alpar@484: soplex->removeRow(i); alpar@484: _row_names_ref.erase(_row_names[i]); alpar@484: _row_names[i] = _row_names.back(); alpar@484: _row_names_ref[_row_names.back()] = i; alpar@484: _row_names.pop_back(); alpar@484: } alpar@484: alpar@485: void SoplexLp::_eraseColId(int i) { alpar@484: cols.eraseIndex(i); alpar@484: cols.relocateIndex(i, cols.maxIndex()); alpar@484: } alpar@485: void SoplexLp::_eraseRowId(int i) { alpar@484: rows.eraseIndex(i); alpar@484: rows.relocateIndex(i, rows.maxIndex()); alpar@484: } alpar@484: alpar@485: void SoplexLp::_getColName(int c, std::string &name) const { alpar@484: name = _col_names[c]; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setColName(int c, const std::string &name) { alpar@484: _col_names_ref.erase(_col_names[c]); alpar@484: _col_names[c] = name; alpar@484: if (!name.empty()) { alpar@484: _col_names_ref.insert(std::make_pair(name, c)); alpar@484: } alpar@484: } alpar@484: alpar@485: int SoplexLp::_colByName(const std::string& name) const { alpar@484: std::map::const_iterator it = alpar@484: _col_names_ref.find(name); alpar@484: if (it != _col_names_ref.end()) { alpar@484: return it->second; alpar@484: } else { alpar@484: return -1; alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_getRowName(int r, std::string &name) const { alpar@484: name = _row_names[r]; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setRowName(int r, const std::string &name) { alpar@484: _row_names_ref.erase(_row_names[r]); alpar@484: _row_names[r] = name; alpar@484: if (!name.empty()) { alpar@484: _row_names_ref.insert(std::make_pair(name, r)); alpar@484: } alpar@484: } alpar@484: alpar@485: int SoplexLp::_rowByName(const std::string& name) const { alpar@484: std::map::const_iterator it = alpar@484: _row_names_ref.find(name); alpar@484: if (it != _row_names_ref.end()) { alpar@484: return it->second; alpar@484: } else { alpar@484: return -1; alpar@484: } alpar@484: } alpar@484: alpar@484: alpar@485: void SoplexLp::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) { alpar@484: for (int j = 0; j < soplex->nCols(); ++j) { alpar@484: soplex->changeElement(i, j, 0.0); alpar@484: } alpar@484: for(ExprIterator it = b; it != e; ++it) { alpar@484: soplex->changeElement(i, it->first, it->second); alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_getRowCoeffs(int i, InsertIterator b) const { alpar@484: const soplex::SVector& vec = soplex->rowVector(i); alpar@484: for (int k = 0; k < vec.size(); ++k) { alpar@484: *b = std::make_pair(vec.index(k), vec.value(k)); alpar@484: ++b; alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_setColCoeffs(int j, ExprIterator b, ExprIterator e) { alpar@484: for (int i = 0; i < soplex->nRows(); ++i) { alpar@484: soplex->changeElement(i, j, 0.0); alpar@484: } alpar@484: for(ExprIterator it = b; it != e; ++it) { alpar@484: soplex->changeElement(it->first, j, it->second); alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_getColCoeffs(int i, InsertIterator b) const { alpar@484: const soplex::SVector& vec = soplex->colVector(i); alpar@484: for (int k = 0; k < vec.size(); ++k) { alpar@484: *b = std::make_pair(vec.index(k), vec.value(k)); alpar@484: ++b; alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_setCoeff(int i, int j, Value value) { alpar@484: soplex->changeElement(i, j, value); alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getCoeff(int i, int j) const { alpar@484: return soplex->rowVector(i)[j]; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setColLowerBound(int i, Value value) { alpar@484: LEMON_ASSERT(value != INF, "Invalid bound"); alpar@484: soplex->changeLower(i, value != -INF ? value : -soplex::infinity); alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getColLowerBound(int i) const { alpar@484: double value = soplex->lower(i); alpar@484: return value != -soplex::infinity ? value : -INF; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setColUpperBound(int i, Value value) { alpar@484: LEMON_ASSERT(value != -INF, "Invalid bound"); alpar@484: soplex->changeUpper(i, value != INF ? value : soplex::infinity); alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getColUpperBound(int i) const { alpar@484: double value = soplex->upper(i); alpar@484: return value != soplex::infinity ? value : INF; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setRowLowerBound(int i, Value lb) { alpar@484: LEMON_ASSERT(lb != INF, "Invalid bound"); alpar@484: soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, soplex->rhs(i)); alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getRowLowerBound(int i) const { alpar@484: double res = soplex->lhs(i); alpar@484: return res == -soplex::infinity ? -INF : res; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setRowUpperBound(int i, Value ub) { alpar@484: LEMON_ASSERT(ub != -INF, "Invalid bound"); alpar@484: soplex->changeRange(i, soplex->lhs(i), ub != INF ? ub : soplex::infinity); alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getRowUpperBound(int i) const { alpar@484: double res = soplex->rhs(i); alpar@484: return res == soplex::infinity ? INF : res; alpar@484: } alpar@484: alpar@485: void SoplexLp::_setObjCoeffs(ExprIterator b, ExprIterator e) { alpar@484: for (int j = 0; j < soplex->nCols(); ++j) { alpar@484: soplex->changeObj(j, 0.0); alpar@484: } alpar@484: for (ExprIterator it = b; it != e; ++it) { alpar@484: soplex->changeObj(it->first, it->second); alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_getObjCoeffs(InsertIterator b) const { alpar@484: for (int j = 0; j < soplex->nCols(); ++j) { alpar@484: Value coef = soplex->obj(j); alpar@484: if (coef != 0.0) { alpar@484: *b = std::make_pair(j, coef); alpar@484: ++b; alpar@484: } alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_setObjCoeff(int i, Value obj_coef) { alpar@484: soplex->changeObj(i, obj_coef); alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getObjCoeff(int i) const { alpar@484: return soplex->obj(i); alpar@484: } alpar@484: alpar@485: SoplexLp::SolveExitStatus SoplexLp::_solve() { alpar@484: alpar@484: _clear_temporals(); alpar@484: alpar@484: soplex::SPxSolver::Status status = soplex->solve(); alpar@484: alpar@484: switch (status) { alpar@484: case soplex::SPxSolver::OPTIMAL: alpar@484: case soplex::SPxSolver::INFEASIBLE: alpar@484: case soplex::SPxSolver::UNBOUNDED: alpar@484: return SOLVED; alpar@484: default: alpar@484: return UNSOLVED; alpar@484: } alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getPrimal(int i) const { alpar@484: if (_primal_values.empty()) { alpar@484: _primal_values.resize(soplex->nCols()); alpar@484: soplex::Vector pv(_primal_values.size(), &_primal_values.front()); alpar@484: soplex->getPrimal(pv); alpar@484: } alpar@484: return _primal_values[i]; alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getDual(int i) const { alpar@484: if (_dual_values.empty()) { alpar@484: _dual_values.resize(soplex->nRows()); alpar@484: soplex::Vector dv(_dual_values.size(), &_dual_values.front()); alpar@484: soplex->getDual(dv); alpar@484: } alpar@484: return _dual_values[i]; alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getPrimalValue() const { alpar@484: return soplex->objValue(); alpar@484: } alpar@484: alpar@485: SoplexLp::VarStatus SoplexLp::_getColStatus(int i) const { alpar@484: switch (soplex->getBasisColStatus(i)) { alpar@484: case soplex::SPxSolver::BASIC: alpar@484: return BASIC; alpar@484: case soplex::SPxSolver::ON_UPPER: alpar@484: return UPPER; alpar@484: case soplex::SPxSolver::ON_LOWER: alpar@484: return LOWER; alpar@484: case soplex::SPxSolver::FIXED: alpar@484: return FIXED; alpar@484: case soplex::SPxSolver::ZERO: 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: SoplexLp::VarStatus SoplexLp::_getRowStatus(int i) const { alpar@484: switch (soplex->getBasisRowStatus(i)) { alpar@484: case soplex::SPxSolver::BASIC: alpar@484: return BASIC; alpar@484: case soplex::SPxSolver::ON_UPPER: alpar@484: return UPPER; alpar@484: case soplex::SPxSolver::ON_LOWER: alpar@484: return LOWER; alpar@484: case soplex::SPxSolver::FIXED: alpar@484: return FIXED; alpar@484: case soplex::SPxSolver::ZERO: 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@485: SoplexLp::Value SoplexLp::_getPrimalRay(int i) const { alpar@484: if (_primal_ray.empty()) { alpar@484: _primal_ray.resize(soplex->nCols()); alpar@484: soplex::Vector pv(_primal_ray.size(), &_primal_ray.front()); alpar@484: soplex->getDualfarkas(pv); alpar@484: } alpar@484: return _primal_ray[i]; alpar@484: } alpar@484: alpar@485: SoplexLp::Value SoplexLp::_getDualRay(int i) const { alpar@484: if (_dual_ray.empty()) { alpar@484: _dual_ray.resize(soplex->nRows()); alpar@484: soplex::Vector dv(_dual_ray.size(), &_dual_ray.front()); alpar@484: soplex->getDualfarkas(dv); alpar@484: } alpar@484: return _dual_ray[i]; alpar@484: } alpar@484: alpar@485: SoplexLp::ProblemType SoplexLp::_getPrimalType() const { alpar@484: switch (soplex->status()) { alpar@484: case soplex::SPxSolver::OPTIMAL: alpar@484: return OPTIMAL; alpar@484: case soplex::SPxSolver::UNBOUNDED: alpar@484: return UNBOUNDED; alpar@484: case soplex::SPxSolver::INFEASIBLE: alpar@484: return INFEASIBLE; alpar@484: default: alpar@484: return UNDEFINED; alpar@484: } alpar@484: } alpar@484: alpar@485: SoplexLp::ProblemType SoplexLp::_getDualType() const { alpar@484: switch (soplex->status()) { alpar@484: case soplex::SPxSolver::OPTIMAL: alpar@484: return OPTIMAL; alpar@484: case soplex::SPxSolver::UNBOUNDED: alpar@484: return UNBOUNDED; alpar@484: case soplex::SPxSolver::INFEASIBLE: alpar@484: return INFEASIBLE; alpar@484: default: alpar@484: return UNDEFINED; alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_setSense(Sense sense) { alpar@484: switch (sense) { alpar@484: case MIN: alpar@484: soplex->changeSense(soplex::SPxSolver::MINIMIZE); alpar@484: break; alpar@484: case MAX: alpar@484: soplex->changeSense(soplex::SPxSolver::MAXIMIZE); alpar@484: } alpar@484: } alpar@484: alpar@485: SoplexLp::Sense SoplexLp::_getSense() const { alpar@484: switch (soplex->spxSense()) { alpar@484: case soplex::SPxSolver::MAXIMIZE: alpar@484: return MAX; alpar@484: case soplex::SPxSolver::MINIMIZE: alpar@484: return MIN; alpar@484: default: alpar@484: LEMON_ASSERT(false, "Wrong sense."); alpar@485: return SoplexLp::Sense(); alpar@484: } alpar@484: } alpar@484: alpar@485: void SoplexLp::_clear() { alpar@484: soplex->clear(); alpar@484: _col_names.clear(); alpar@484: _col_names_ref.clear(); alpar@484: _row_names.clear(); alpar@484: _row_names_ref.clear(); alpar@484: cols.clear(); alpar@484: rows.clear(); alpar@484: _clear_temporals(); alpar@484: } alpar@484: alpar@484: } //namespace lemon alpar@484: