deba@2313: /* -*- C++ -*- deba@2313: * deba@2313: * This file is a part of LEMON, a generic C++ optimization library deba@2313: * deba@2313: * Copyright (C) 2003-2006 deba@2313: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@2313: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@2313: * deba@2313: * Permission to use, modify and distribute this software is granted deba@2313: * provided that this copyright notice appears in all copies. For deba@2313: * precise terms see the accompanying LICENSE file. deba@2313: * deba@2313: * This software is provided "AS IS" with no warranty of any kind, deba@2313: * express or implied, and with no claim as to its suitability for any deba@2313: * purpose. deba@2313: * deba@2313: */ deba@2313: deba@2313: #include deba@2313: #include deba@2313: deba@2313: #include deba@2313: deba@2313: deba@2313: ///\file deba@2313: ///\brief Implementation of the LEMON-SOPLEX lp solver interface. deba@2313: namespace lemon { deba@2313: deba@2313: LpSoplex::LpSoplex() : LpSolverBase() { deba@2363: rows.setIdHandler(relocateIdHandler); deba@2363: cols.setIdHandler(relocateIdHandler); deba@2313: soplex = new soplex::SoPlex; deba@2363: solved = false; deba@2313: } deba@2313: deba@2313: LpSoplex::~LpSoplex() { deba@2313: delete soplex; deba@2313: } deba@2313: deba@2313: LpSolverBase &LpSoplex::_newLp() { deba@2313: LpSoplex* newlp = new LpSoplex(); deba@2313: return *newlp; deba@2313: } deba@2313: deba@2313: LpSolverBase &LpSoplex::_copyLp() { deba@2313: LpSoplex* newlp = new LpSoplex(); deba@2313: ((soplex::SPxLP&)*(newlp->soplex)) = *soplex; deba@2313: return *newlp; deba@2313: } deba@2313: deba@2313: int LpSoplex::_addCol() { deba@2313: soplex::LPCol col; deba@2363: col.setLower(-soplex::infinity); deba@2363: col.setUpper(soplex::infinity); deba@2313: soplex->addCol(col); deba@2313: deba@2313: colNames.push_back(std::string()); deba@2363: primal_value.push_back(0.0); deba@2363: solved = false; deba@2313: deba@2313: return soplex->nCols() - 1; deba@2313: } deba@2313: deba@2313: int LpSoplex::_addRow() { deba@2313: soplex::LPRow row; deba@2363: row.setLhs(-soplex::infinity); deba@2363: row.setRhs(soplex::infinity); deba@2313: soplex->addRow(row); deba@2313: deba@2363: dual_value.push_back(0.0); deba@2363: solved = false; deba@2313: deba@2313: return soplex->nRows() - 1; deba@2313: } deba@2313: deba@2313: deba@2313: void LpSoplex::_eraseCol(int i) { deba@2313: soplex->removeCol(i); deba@2363: colNames[i] = colNames.back(); deba@2363: colNames.pop_back(); deba@2363: primal_value[i] = primal_value.back(); deba@2363: primal_value.pop_back(); deba@2363: solved = false; deba@2313: } deba@2313: deba@2313: void LpSoplex::_eraseRow(int i) { deba@2313: soplex->removeRow(i); deba@2363: dual_value[i] = dual_value.back(); deba@2363: dual_value.pop_back(); deba@2363: solved = false; deba@2313: } deba@2313: deba@2313: void LpSoplex::_getColName(int col, std::string &name) { deba@2313: name = colNames[col]; deba@2313: } deba@2313: deba@2313: void LpSoplex::_setColName(int col, const std::string &name) { deba@2313: colNames[col] = name; deba@2313: } deba@2313: deba@2313: deba@2313: void LpSoplex::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) { deba@2313: for (int j = 0; j < soplex->nCols(); ++j) { deba@2313: soplex->changeElement(i, j, 0.0); deba@2313: } deba@2313: for(LpRowIterator it = b; it != e; ++it) { deba@2313: soplex->changeElement(i, it->first, it->second); deba@2313: } deba@2363: solved = false; deba@2313: } deba@2313: deba@2313: void LpSoplex::_setColCoeffs(int j, LpColIterator b, LpColIterator e) { deba@2313: for (int i = 0; i < soplex->nRows(); ++i) { deba@2313: soplex->changeElement(i, j, 0.0); deba@2313: } deba@2313: for(LpColIterator it = b; it != e; ++it) { deba@2313: soplex->changeElement(it->first, j, it->second); deba@2313: } deba@2363: solved = false; deba@2313: } deba@2313: deba@2363: void LpSoplex::_setCoeff(int i, int j, Value value) { deba@2363: soplex->changeElement(i, j, value); deba@2363: solved = false; deba@2363: } deba@2363: deba@2363: LpSoplex::Value LpSoplex::_getCoeff(int i, int j) { deba@2363: return soplex->rowVector(i)[j]; deba@2313: } deba@2313: deba@2313: void LpSoplex::_setColLowerBound(int i, Value value) { deba@2363: soplex->changeLower(i, value != -INF ? value : -soplex::infinity); deba@2363: solved = false; deba@2313: } deba@2313: deba@2363: LpSoplex::Value LpSoplex::_getColLowerBound(int i) { deba@2363: double value = soplex->lower(i); deba@2363: return value != -soplex::infinity ? value : -INF; deba@2363: } deba@2363: deba@2313: void LpSoplex::_setColUpperBound(int i, Value value) { deba@2363: soplex->changeUpper(i, value != INF ? value : soplex::infinity); deba@2363: solved = false; deba@2363: } deba@2363: deba@2363: LpSoplex::Value LpSoplex::_getColUpperBound(int i) { deba@2363: double value = soplex->upper(i); deba@2363: return value != soplex::infinity ? value : INF; deba@2313: } deba@2313: deba@2313: void LpSoplex::_setRowBounds(int i, Value lb, Value ub) { deba@2363: soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, deba@2363: ub != INF ? ub : soplex::infinity); deba@2363: solved = false; deba@2363: } deba@2363: void LpSoplex::_getRowBounds(int i, Value &lower, Value &upper) { deba@2363: lower = soplex->lhs(i); deba@2363: if (lower == -soplex::infinity) lower = -INF; deba@2363: upper = soplex->rhs(i); deba@2363: if (upper == -soplex::infinity) upper = INF; deba@2313: } deba@2313: deba@2313: void LpSoplex::_setObjCoeff(int i, Value obj_coef) { deba@2313: soplex->changeObj(i, obj_coef); deba@2363: solved = false; deba@2363: } deba@2363: deba@2363: LpSoplex::Value LpSoplex::_getObjCoeff(int i) { deba@2363: return soplex->obj(i); deba@2313: } deba@2313: deba@2313: void LpSoplex::_clearObj() { deba@2313: for (int i = 0; i < soplex->nCols(); ++i) { deba@2313: soplex->changeObj(i, 0.0); deba@2313: } deba@2363: solved = false; deba@2313: } deba@2313: deba@2313: LpSoplex::SolveExitStatus LpSoplex::_solve() { deba@2313: soplex::SPxSolver::Status status = soplex->solve(); deba@2313: deba@2363: soplex::Vector pv(primal_value.size(), &primal_value[0]); deba@2313: soplex->getPrimal(pv); deba@2313: deba@2363: soplex::Vector dv(dual_value.size(), &dual_value[0]); deba@2313: soplex->getDual(dv); deba@2313: deba@2313: switch (status) { deba@2313: case soplex::SPxSolver::OPTIMAL: deba@2313: case soplex::SPxSolver::INFEASIBLE: deba@2313: case soplex::SPxSolver::UNBOUNDED: deba@2363: solved = true; deba@2313: return SOLVED; deba@2313: default: deba@2313: return UNSOLVED; deba@2313: } deba@2313: } deba@2313: deba@2313: LpSoplex::Value LpSoplex::_getPrimal(int i) { deba@2363: return primal_value[i]; deba@2313: } deba@2313: deba@2313: LpSoplex::Value LpSoplex::_getDual(int i) { deba@2363: return dual_value[i]; deba@2313: } deba@2313: deba@2313: LpSoplex::Value LpSoplex::_getPrimalValue() { deba@2313: return soplex->objValue(); deba@2313: } deba@2313: deba@2313: bool LpSoplex::_isBasicCol(int i) { deba@2313: return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC; deba@2313: } deba@2313: deba@2313: LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() { deba@2363: if (!solved) return UNDEFINED; deba@2313: switch (soplex->status()) { deba@2313: case soplex::SPxSolver::OPTIMAL: deba@2313: return OPTIMAL; deba@2313: case soplex::SPxSolver::UNBOUNDED: deba@2313: return INFINITE; deba@2313: case soplex::SPxSolver::INFEASIBLE: deba@2313: return INFEASIBLE; deba@2313: default: deba@2313: return UNDEFINED; deba@2313: } deba@2313: } deba@2313: deba@2313: LpSoplex::SolutionStatus LpSoplex::_getDualStatus() { deba@2363: if (!solved) return UNDEFINED; deba@2363: switch (soplex->status()) { deba@2363: case soplex::SPxSolver::OPTIMAL: deba@2313: return OPTIMAL; deba@2363: case soplex::SPxSolver::UNBOUNDED: deba@2313: return INFEASIBLE; deba@2363: default: deba@2313: return UNDEFINED; deba@2313: } deba@2313: } deba@2313: deba@2313: LpSoplex::ProblemTypes LpSoplex::_getProblemType() { deba@2363: if (!solved) return UNKNOWN; deba@2363: switch (soplex->status()) { deba@2363: case soplex::SPxSolver::OPTIMAL: deba@2313: return PRIMAL_DUAL_FEASIBLE; deba@2363: case soplex::SPxSolver::UNBOUNDED: deba@2313: return PRIMAL_FEASIBLE_DUAL_INFEASIBLE; deba@2363: default: deba@2313: return UNKNOWN; deba@2313: } deba@2313: } deba@2313: deba@2313: void LpSoplex::_setMax() { deba@2313: soplex->changeSense(soplex::SPxSolver::MAXIMIZE); deba@2363: solved = false; deba@2313: } deba@2313: void LpSoplex::_setMin() { deba@2313: soplex->changeSense(soplex::SPxSolver::MINIMIZE); deba@2363: solved = false; deba@2313: } deba@2363: bool LpSoplex::_isMax() { deba@2363: return soplex->spxSense() == soplex::SPxSolver::MAXIMIZE; deba@2363: } deba@2363: deba@2313: deba@2313: } //namespace lemon deba@2313: