lemon/lp_soplex.cc
changeset 481 7afc121e0689
child 482 ed54c0d13df0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/lp_soplex.cc	Tue Dec 02 21:40:33 2008 +0100
     1.3 @@ -0,0 +1,316 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include<iostream>
    1.23 +#include<lemon/lp_soplex.h>
    1.24 +
    1.25 +#include <soplex/soplex.h>
    1.26 +
    1.27 +
    1.28 +///\file
    1.29 +///\brief Implementation of the LEMON-SOPLEX lp solver interface.
    1.30 +namespace lemon {
    1.31 +
    1.32 +  LpSoplex::LpSoplex() : LpSolverBase() {
    1.33 +    rows.setIdHandler(relocateIdHandler);
    1.34 +    cols.setIdHandler(relocateIdHandler);
    1.35 +    soplex = new soplex::SoPlex;
    1.36 +    solved = false;
    1.37 +  }
    1.38 +
    1.39 +  LpSoplex::~LpSoplex() {
    1.40 +    delete soplex;
    1.41 +  }
    1.42 +
    1.43 +  LpSoplex::LpSoplex(const LpSoplex& lp) : LpSolverBase() {
    1.44 +    rows = lp.rows;
    1.45 +    rows.setIdHandler(relocateIdHandler);
    1.46 +
    1.47 +    cols = lp.cols;
    1.48 +    cols.setIdHandler(relocateIdHandler);
    1.49 +
    1.50 +    soplex = new soplex::SoPlex;
    1.51 +    (*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex);
    1.52 +
    1.53 +    colNames = lp.colNames;
    1.54 +    invColNames = lp.invColNames;
    1.55 +
    1.56 +    primal_value = lp.primal_value;
    1.57 +    dual_value = lp.dual_value;
    1.58 +
    1.59 +  }
    1.60 +
    1.61 +  LpSolverBase* LpSoplex::_newLp() {
    1.62 +    LpSoplex* newlp = new LpSoplex();
    1.63 +    return newlp;
    1.64 +  }
    1.65 +
    1.66 +  LpSolverBase* LpSoplex::_copyLp() {
    1.67 +    LpSoplex* newlp = new LpSoplex(*this);
    1.68 +    return newlp;
    1.69 +  }
    1.70 +
    1.71 +  int LpSoplex::_addCol() {
    1.72 +    soplex::LPCol c;
    1.73 +    c.setLower(-soplex::infinity);
    1.74 +    c.setUpper(soplex::infinity);
    1.75 +    soplex->addCol(c);
    1.76 +
    1.77 +    colNames.push_back(std::string());
    1.78 +    primal_value.push_back(0.0);
    1.79 +    solved = false;
    1.80 +
    1.81 +    return soplex->nCols() - 1;
    1.82 +  }
    1.83 +
    1.84 +  int LpSoplex::_addRow() {
    1.85 +    soplex::LPRow r;
    1.86 +    r.setLhs(-soplex::infinity);
    1.87 +    r.setRhs(soplex::infinity);
    1.88 +    soplex->addRow(r);
    1.89 +
    1.90 +    dual_value.push_back(0.0);
    1.91 +    solved = false;
    1.92 +
    1.93 +    return soplex->nRows() - 1;
    1.94 +  }
    1.95 +
    1.96 +
    1.97 +  void LpSoplex::_eraseCol(int i) {
    1.98 +    soplex->removeCol(i);
    1.99 +    invColNames.erase(colNames[i]);
   1.100 +    colNames[i] = colNames.back();
   1.101 +    invColNames[colNames.back()] = i;
   1.102 +    colNames.pop_back();
   1.103 +    primal_value[i] = primal_value.back();
   1.104 +    primal_value.pop_back();
   1.105 +    solved = false;
   1.106 +  }
   1.107 +
   1.108 +  void LpSoplex::_eraseRow(int i) {
   1.109 +    soplex->removeRow(i);
   1.110 +    dual_value[i] = dual_value.back();
   1.111 +    dual_value.pop_back();
   1.112 +    solved = false;
   1.113 +  }
   1.114 +
   1.115 +  void LpSoplex::_getColName(int c, std::string &name) const {
   1.116 +    name = colNames[c];
   1.117 +  }
   1.118 +
   1.119 +  void LpSoplex::_setColName(int c, const std::string &name) {
   1.120 +    invColNames.erase(colNames[c]);
   1.121 +    colNames[c] = name;
   1.122 +    if (!name.empty()) {
   1.123 +      invColNames.insert(std::make_pair(name, c));
   1.124 +    }
   1.125 +  }
   1.126 +
   1.127 +  int LpSoplex::_colByName(const std::string& name) const {
   1.128 +    std::map<std::string, int>::const_iterator it =
   1.129 +      invColNames.find(name);
   1.130 +    if (it != invColNames.end()) {
   1.131 +      return it->second;
   1.132 +    } else {
   1.133 +      return -1;
   1.134 +    }
   1.135 +  }
   1.136 +
   1.137 +
   1.138 +  void LpSoplex::_setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e) {
   1.139 +    for (int j = 0; j < soplex->nCols(); ++j) {
   1.140 +      soplex->changeElement(i, j, 0.0);
   1.141 +    }
   1.142 +    for(ConstRowIterator it = b; it != e; ++it) {
   1.143 +      soplex->changeElement(i, it->first, it->second);
   1.144 +    }
   1.145 +    solved = false;
   1.146 +  }
   1.147 +
   1.148 +  void LpSoplex::_getRowCoeffs(int i, RowIterator b) const {
   1.149 +    const soplex::SVector& vec = soplex->rowVector(i);
   1.150 +    for (int k = 0; k < vec.size(); ++k) {
   1.151 +      *b = std::make_pair(vec.index(k), vec.value(k));
   1.152 +      ++b;
   1.153 +    }
   1.154 +  }
   1.155 +
   1.156 +  void LpSoplex::_setColCoeffs(int j, ConstColIterator b, ConstColIterator e) {
   1.157 +    for (int i = 0; i < soplex->nRows(); ++i) {
   1.158 +      soplex->changeElement(i, j, 0.0);
   1.159 +    }
   1.160 +    for(ConstColIterator it = b; it != e; ++it) {
   1.161 +      soplex->changeElement(it->first, j, it->second);
   1.162 +    }
   1.163 +    solved = false;
   1.164 +  }
   1.165 +
   1.166 +  void LpSoplex::_getColCoeffs(int i, ColIterator b) const {
   1.167 +    const soplex::SVector& vec = soplex->colVector(i);
   1.168 +    for (int k = 0; k < vec.size(); ++k) {
   1.169 +      *b = std::make_pair(vec.index(k), vec.value(k));
   1.170 +      ++b;
   1.171 +    }
   1.172 +  }
   1.173 +
   1.174 +  void LpSoplex::_setCoeff(int i, int j, Value value) {
   1.175 +    soplex->changeElement(i, j, value);
   1.176 +    solved = false;
   1.177 +  }
   1.178 +
   1.179 +  LpSoplex::Value LpSoplex::_getCoeff(int i, int j) const {
   1.180 +    return soplex->rowVector(i)[j];
   1.181 +  }
   1.182 +
   1.183 +  void LpSoplex::_setColLowerBound(int i, Value value) {
   1.184 +    soplex->changeLower(i, value != -INF ? value : -soplex::infinity);
   1.185 +    solved = false;
   1.186 +  }
   1.187 +
   1.188 +  LpSoplex::Value LpSoplex::_getColLowerBound(int i) const {
   1.189 +    double value = soplex->lower(i);
   1.190 +    return value != -soplex::infinity ? value : -INF;
   1.191 +  }
   1.192 +
   1.193 +  void LpSoplex::_setColUpperBound(int i, Value value) {
   1.194 +    soplex->changeUpper(i, value != INF ? value : soplex::infinity);
   1.195 +    solved = false;
   1.196 +  }
   1.197 +
   1.198 +  LpSoplex::Value LpSoplex::_getColUpperBound(int i) const {
   1.199 +    double value = soplex->upper(i);
   1.200 +    return value != soplex::infinity ? value : INF;
   1.201 +  }
   1.202 +
   1.203 +  void LpSoplex::_setRowBounds(int i, Value lb, Value ub) {
   1.204 +    soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity,
   1.205 +                        ub != INF ? ub : soplex::infinity);
   1.206 +    solved = false;
   1.207 +  }
   1.208 +  void LpSoplex::_getRowBounds(int i, Value &lower, Value &upper) const {
   1.209 +    lower = soplex->lhs(i);
   1.210 +    if (lower == -soplex::infinity) lower = -INF;
   1.211 +    upper = soplex->rhs(i);
   1.212 +    if (upper == -soplex::infinity) upper = INF;
   1.213 +  }
   1.214 +
   1.215 +  void LpSoplex::_setObjCoeff(int i, Value obj_coef) {
   1.216 +    soplex->changeObj(i, obj_coef);
   1.217 +    solved = false;
   1.218 +  }
   1.219 +
   1.220 +  LpSoplex::Value LpSoplex::_getObjCoeff(int i) const {
   1.221 +    return soplex->obj(i);
   1.222 +  }
   1.223 +
   1.224 +  void LpSoplex::_clearObj() {
   1.225 +    for (int i = 0; i < soplex->nCols(); ++i) {
   1.226 +      soplex->changeObj(i, 0.0);
   1.227 +    }
   1.228 +    solved = false;
   1.229 +  }
   1.230 +
   1.231 +  LpSoplex::SolveExitStatus LpSoplex::_solve() {
   1.232 +    soplex::SPxSolver::Status status = soplex->solve();
   1.233 +
   1.234 +    soplex::Vector pv(primal_value.size(), &primal_value[0]);
   1.235 +    soplex->getPrimal(pv);
   1.236 +
   1.237 +    soplex::Vector dv(dual_value.size(), &dual_value[0]);
   1.238 +    soplex->getDual(dv);
   1.239 +
   1.240 +    switch (status) {
   1.241 +    case soplex::SPxSolver::OPTIMAL:
   1.242 +    case soplex::SPxSolver::INFEASIBLE:
   1.243 +    case soplex::SPxSolver::UNBOUNDED:
   1.244 +      solved = true;
   1.245 +      return SOLVED;
   1.246 +    default:
   1.247 +      return UNSOLVED;
   1.248 +    }
   1.249 +  }
   1.250 +
   1.251 +  LpSoplex::Value LpSoplex::_getPrimal(int i) const {
   1.252 +    return primal_value[i];
   1.253 +  }
   1.254 +
   1.255 +  LpSoplex::Value LpSoplex::_getDual(int i) const {
   1.256 +    return dual_value[i];
   1.257 +  }
   1.258 +
   1.259 +  LpSoplex::Value LpSoplex::_getPrimalValue() const {
   1.260 +    return soplex->objValue();
   1.261 +  }
   1.262 +
   1.263 +  bool LpSoplex::_isBasicCol(int i) const {
   1.264 +    return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC;
   1.265 +  }
   1.266 +
   1.267 +  LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() const {
   1.268 +    if (!solved) return UNDEFINED;
   1.269 +    switch (soplex->status()) {
   1.270 +    case soplex::SPxSolver::OPTIMAL:
   1.271 +      return OPTIMAL;
   1.272 +    case soplex::SPxSolver::UNBOUNDED:
   1.273 +      return INFINITE;
   1.274 +    case soplex::SPxSolver::INFEASIBLE:
   1.275 +      return INFEASIBLE;
   1.276 +    default:
   1.277 +      return UNDEFINED;
   1.278 +    }
   1.279 +  }
   1.280 +
   1.281 +  LpSoplex::SolutionStatus LpSoplex::_getDualStatus() const {
   1.282 +    if (!solved) return UNDEFINED;
   1.283 +    switch (soplex->status()) {
   1.284 +    case soplex::SPxSolver::OPTIMAL:
   1.285 +      return OPTIMAL;
   1.286 +    case soplex::SPxSolver::UNBOUNDED:
   1.287 +      return INFEASIBLE;
   1.288 +    default:
   1.289 +      return UNDEFINED;
   1.290 +    }
   1.291 +  }
   1.292 +
   1.293 +  LpSoplex::ProblemTypes LpSoplex::_getProblemType() const {
   1.294 +    if (!solved) return UNKNOWN;
   1.295 +    switch (soplex->status()) {
   1.296 +    case soplex::SPxSolver::OPTIMAL:
   1.297 +      return PRIMAL_DUAL_FEASIBLE;
   1.298 +    case soplex::SPxSolver::UNBOUNDED:
   1.299 +      return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
   1.300 +    default:
   1.301 +      return UNKNOWN;
   1.302 +    }
   1.303 +  }
   1.304 +
   1.305 +  void LpSoplex::_setMax() {
   1.306 +    soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
   1.307 +    solved = false;
   1.308 +  }
   1.309 +  void LpSoplex::_setMin() {
   1.310 +    soplex->changeSense(soplex::SPxSolver::MINIMIZE);
   1.311 +    solved = false;
   1.312 +  }
   1.313 +  bool LpSoplex::_isMax() const {
   1.314 +    return soplex->spxSense() == soplex::SPxSolver::MAXIMIZE;
   1.315 +  }
   1.316 +
   1.317 +
   1.318 +} //namespace lemon
   1.319 +