1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/soplex.cc Thu Nov 05 15:50:01 2009 +0100
1.3 @@ -0,0 +1,465 @@
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/soplex.h>
1.24 +
1.25 +#include <soplex.h>
1.26 +#include <spxout.h>
1.27 +
1.28 +
1.29 +///\file
1.30 +///\brief Implementation of the LEMON-SOPLEX lp solver interface.
1.31 +namespace lemon {
1.32 +
1.33 + SoplexLp::SoplexLp() {
1.34 + soplex = new soplex::SoPlex;
1.35 + messageLevel(MESSAGE_NOTHING);
1.36 + }
1.37 +
1.38 + SoplexLp::~SoplexLp() {
1.39 + delete soplex;
1.40 + }
1.41 +
1.42 + SoplexLp::SoplexLp(const SoplexLp& lp) {
1.43 + rows = lp.rows;
1.44 + cols = lp.cols;
1.45 +
1.46 + soplex = new soplex::SoPlex;
1.47 + (*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex);
1.48 +
1.49 + _col_names = lp._col_names;
1.50 + _col_names_ref = lp._col_names_ref;
1.51 +
1.52 + _row_names = lp._row_names;
1.53 + _row_names_ref = lp._row_names_ref;
1.54 +
1.55 + messageLevel(MESSAGE_NOTHING);
1.56 + }
1.57 +
1.58 + void SoplexLp::_clear_temporals() {
1.59 + _primal_values.clear();
1.60 + _dual_values.clear();
1.61 + }
1.62 +
1.63 + SoplexLp* SoplexLp::newSolver() const {
1.64 + SoplexLp* newlp = new SoplexLp();
1.65 + return newlp;
1.66 + }
1.67 +
1.68 + SoplexLp* SoplexLp::cloneSolver() const {
1.69 + SoplexLp* newlp = new SoplexLp(*this);
1.70 + return newlp;
1.71 + }
1.72 +
1.73 + const char* SoplexLp::_solverName() const { return "SoplexLp"; }
1.74 +
1.75 + int SoplexLp::_addCol() {
1.76 + soplex::LPCol c;
1.77 + c.setLower(-soplex::infinity);
1.78 + c.setUpper(soplex::infinity);
1.79 + soplex->addCol(c);
1.80 +
1.81 + _col_names.push_back(std::string());
1.82 +
1.83 + return soplex->nCols() - 1;
1.84 + }
1.85 +
1.86 + int SoplexLp::_addRow() {
1.87 + soplex::LPRow r;
1.88 + r.setLhs(-soplex::infinity);
1.89 + r.setRhs(soplex::infinity);
1.90 + soplex->addRow(r);
1.91 +
1.92 + _row_names.push_back(std::string());
1.93 +
1.94 + return soplex->nRows() - 1;
1.95 + }
1.96 +
1.97 + int SoplexLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
1.98 + soplex::DSVector v;
1.99 + for (ExprIterator it = b; it != e; ++it) {
1.100 + v.add(it->first, it->second);
1.101 + }
1.102 + soplex::LPRow r(l, v, u);
1.103 + soplex->addRow(r);
1.104 +
1.105 + _row_names.push_back(std::string());
1.106 +
1.107 + return soplex->nRows() - 1;
1.108 + }
1.109 +
1.110 +
1.111 + void SoplexLp::_eraseCol(int i) {
1.112 + soplex->removeCol(i);
1.113 + _col_names_ref.erase(_col_names[i]);
1.114 + _col_names[i] = _col_names.back();
1.115 + _col_names_ref[_col_names.back()] = i;
1.116 + _col_names.pop_back();
1.117 + }
1.118 +
1.119 + void SoplexLp::_eraseRow(int i) {
1.120 + soplex->removeRow(i);
1.121 + _row_names_ref.erase(_row_names[i]);
1.122 + _row_names[i] = _row_names.back();
1.123 + _row_names_ref[_row_names.back()] = i;
1.124 + _row_names.pop_back();
1.125 + }
1.126 +
1.127 + void SoplexLp::_eraseColId(int i) {
1.128 + cols.eraseIndex(i);
1.129 + cols.relocateIndex(i, cols.maxIndex());
1.130 + }
1.131 + void SoplexLp::_eraseRowId(int i) {
1.132 + rows.eraseIndex(i);
1.133 + rows.relocateIndex(i, rows.maxIndex());
1.134 + }
1.135 +
1.136 + void SoplexLp::_getColName(int c, std::string &name) const {
1.137 + name = _col_names[c];
1.138 + }
1.139 +
1.140 + void SoplexLp::_setColName(int c, const std::string &name) {
1.141 + _col_names_ref.erase(_col_names[c]);
1.142 + _col_names[c] = name;
1.143 + if (!name.empty()) {
1.144 + _col_names_ref.insert(std::make_pair(name, c));
1.145 + }
1.146 + }
1.147 +
1.148 + int SoplexLp::_colByName(const std::string& name) const {
1.149 + std::map<std::string, int>::const_iterator it =
1.150 + _col_names_ref.find(name);
1.151 + if (it != _col_names_ref.end()) {
1.152 + return it->second;
1.153 + } else {
1.154 + return -1;
1.155 + }
1.156 + }
1.157 +
1.158 + void SoplexLp::_getRowName(int r, std::string &name) const {
1.159 + name = _row_names[r];
1.160 + }
1.161 +
1.162 + void SoplexLp::_setRowName(int r, const std::string &name) {
1.163 + _row_names_ref.erase(_row_names[r]);
1.164 + _row_names[r] = name;
1.165 + if (!name.empty()) {
1.166 + _row_names_ref.insert(std::make_pair(name, r));
1.167 + }
1.168 + }
1.169 +
1.170 + int SoplexLp::_rowByName(const std::string& name) const {
1.171 + std::map<std::string, int>::const_iterator it =
1.172 + _row_names_ref.find(name);
1.173 + if (it != _row_names_ref.end()) {
1.174 + return it->second;
1.175 + } else {
1.176 + return -1;
1.177 + }
1.178 + }
1.179 +
1.180 +
1.181 + void SoplexLp::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
1.182 + for (int j = 0; j < soplex->nCols(); ++j) {
1.183 + soplex->changeElement(i, j, 0.0);
1.184 + }
1.185 + for(ExprIterator it = b; it != e; ++it) {
1.186 + soplex->changeElement(i, it->first, it->second);
1.187 + }
1.188 + }
1.189 +
1.190 + void SoplexLp::_getRowCoeffs(int i, InsertIterator b) const {
1.191 + const soplex::SVector& vec = soplex->rowVector(i);
1.192 + for (int k = 0; k < vec.size(); ++k) {
1.193 + *b = std::make_pair(vec.index(k), vec.value(k));
1.194 + ++b;
1.195 + }
1.196 + }
1.197 +
1.198 + void SoplexLp::_setColCoeffs(int j, ExprIterator b, ExprIterator e) {
1.199 + for (int i = 0; i < soplex->nRows(); ++i) {
1.200 + soplex->changeElement(i, j, 0.0);
1.201 + }
1.202 + for(ExprIterator it = b; it != e; ++it) {
1.203 + soplex->changeElement(it->first, j, it->second);
1.204 + }
1.205 + }
1.206 +
1.207 + void SoplexLp::_getColCoeffs(int i, InsertIterator b) const {
1.208 + const soplex::SVector& vec = soplex->colVector(i);
1.209 + for (int k = 0; k < vec.size(); ++k) {
1.210 + *b = std::make_pair(vec.index(k), vec.value(k));
1.211 + ++b;
1.212 + }
1.213 + }
1.214 +
1.215 + void SoplexLp::_setCoeff(int i, int j, Value value) {
1.216 + soplex->changeElement(i, j, value);
1.217 + }
1.218 +
1.219 + SoplexLp::Value SoplexLp::_getCoeff(int i, int j) const {
1.220 + return soplex->rowVector(i)[j];
1.221 + }
1.222 +
1.223 + void SoplexLp::_setColLowerBound(int i, Value value) {
1.224 + LEMON_ASSERT(value != INF, "Invalid bound");
1.225 + soplex->changeLower(i, value != -INF ? value : -soplex::infinity);
1.226 + }
1.227 +
1.228 + SoplexLp::Value SoplexLp::_getColLowerBound(int i) const {
1.229 + double value = soplex->lower(i);
1.230 + return value != -soplex::infinity ? value : -INF;
1.231 + }
1.232 +
1.233 + void SoplexLp::_setColUpperBound(int i, Value value) {
1.234 + LEMON_ASSERT(value != -INF, "Invalid bound");
1.235 + soplex->changeUpper(i, value != INF ? value : soplex::infinity);
1.236 + }
1.237 +
1.238 + SoplexLp::Value SoplexLp::_getColUpperBound(int i) const {
1.239 + double value = soplex->upper(i);
1.240 + return value != soplex::infinity ? value : INF;
1.241 + }
1.242 +
1.243 + void SoplexLp::_setRowLowerBound(int i, Value lb) {
1.244 + LEMON_ASSERT(lb != INF, "Invalid bound");
1.245 + soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, soplex->rhs(i));
1.246 + }
1.247 +
1.248 + SoplexLp::Value SoplexLp::_getRowLowerBound(int i) const {
1.249 + double res = soplex->lhs(i);
1.250 + return res == -soplex::infinity ? -INF : res;
1.251 + }
1.252 +
1.253 + void SoplexLp::_setRowUpperBound(int i, Value ub) {
1.254 + LEMON_ASSERT(ub != -INF, "Invalid bound");
1.255 + soplex->changeRange(i, soplex->lhs(i), ub != INF ? ub : soplex::infinity);
1.256 + }
1.257 +
1.258 + SoplexLp::Value SoplexLp::_getRowUpperBound(int i) const {
1.259 + double res = soplex->rhs(i);
1.260 + return res == soplex::infinity ? INF : res;
1.261 + }
1.262 +
1.263 + void SoplexLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
1.264 + for (int j = 0; j < soplex->nCols(); ++j) {
1.265 + soplex->changeObj(j, 0.0);
1.266 + }
1.267 + for (ExprIterator it = b; it != e; ++it) {
1.268 + soplex->changeObj(it->first, it->second);
1.269 + }
1.270 + }
1.271 +
1.272 + void SoplexLp::_getObjCoeffs(InsertIterator b) const {
1.273 + for (int j = 0; j < soplex->nCols(); ++j) {
1.274 + Value coef = soplex->obj(j);
1.275 + if (coef != 0.0) {
1.276 + *b = std::make_pair(j, coef);
1.277 + ++b;
1.278 + }
1.279 + }
1.280 + }
1.281 +
1.282 + void SoplexLp::_setObjCoeff(int i, Value obj_coef) {
1.283 + soplex->changeObj(i, obj_coef);
1.284 + }
1.285 +
1.286 + SoplexLp::Value SoplexLp::_getObjCoeff(int i) const {
1.287 + return soplex->obj(i);
1.288 + }
1.289 +
1.290 + SoplexLp::SolveExitStatus SoplexLp::_solve() {
1.291 +
1.292 + _clear_temporals();
1.293 +
1.294 + _applyMessageLevel();
1.295 +
1.296 + soplex::SPxSolver::Status status = soplex->solve();
1.297 +
1.298 + switch (status) {
1.299 + case soplex::SPxSolver::OPTIMAL:
1.300 + case soplex::SPxSolver::INFEASIBLE:
1.301 + case soplex::SPxSolver::UNBOUNDED:
1.302 + return SOLVED;
1.303 + default:
1.304 + return UNSOLVED;
1.305 + }
1.306 + }
1.307 +
1.308 + SoplexLp::Value SoplexLp::_getPrimal(int i) const {
1.309 + if (_primal_values.empty()) {
1.310 + _primal_values.resize(soplex->nCols());
1.311 + soplex::Vector pv(_primal_values.size(), &_primal_values.front());
1.312 + soplex->getPrimal(pv);
1.313 + }
1.314 + return _primal_values[i];
1.315 + }
1.316 +
1.317 + SoplexLp::Value SoplexLp::_getDual(int i) const {
1.318 + if (_dual_values.empty()) {
1.319 + _dual_values.resize(soplex->nRows());
1.320 + soplex::Vector dv(_dual_values.size(), &_dual_values.front());
1.321 + soplex->getDual(dv);
1.322 + }
1.323 + return _dual_values[i];
1.324 + }
1.325 +
1.326 + SoplexLp::Value SoplexLp::_getPrimalValue() const {
1.327 + return soplex->objValue();
1.328 + }
1.329 +
1.330 + SoplexLp::VarStatus SoplexLp::_getColStatus(int i) const {
1.331 + switch (soplex->getBasisColStatus(i)) {
1.332 + case soplex::SPxSolver::BASIC:
1.333 + return BASIC;
1.334 + case soplex::SPxSolver::ON_UPPER:
1.335 + return UPPER;
1.336 + case soplex::SPxSolver::ON_LOWER:
1.337 + return LOWER;
1.338 + case soplex::SPxSolver::FIXED:
1.339 + return FIXED;
1.340 + case soplex::SPxSolver::ZERO:
1.341 + return FREE;
1.342 + default:
1.343 + LEMON_ASSERT(false, "Wrong column status");
1.344 + return VarStatus();
1.345 + }
1.346 + }
1.347 +
1.348 + SoplexLp::VarStatus SoplexLp::_getRowStatus(int i) const {
1.349 + switch (soplex->getBasisRowStatus(i)) {
1.350 + case soplex::SPxSolver::BASIC:
1.351 + return BASIC;
1.352 + case soplex::SPxSolver::ON_UPPER:
1.353 + return UPPER;
1.354 + case soplex::SPxSolver::ON_LOWER:
1.355 + return LOWER;
1.356 + case soplex::SPxSolver::FIXED:
1.357 + return FIXED;
1.358 + case soplex::SPxSolver::ZERO:
1.359 + return FREE;
1.360 + default:
1.361 + LEMON_ASSERT(false, "Wrong row status");
1.362 + return VarStatus();
1.363 + }
1.364 + }
1.365 +
1.366 + SoplexLp::Value SoplexLp::_getPrimalRay(int i) const {
1.367 + if (_primal_ray.empty()) {
1.368 + _primal_ray.resize(soplex->nCols());
1.369 + soplex::Vector pv(_primal_ray.size(), &_primal_ray.front());
1.370 + soplex->getDualfarkas(pv);
1.371 + }
1.372 + return _primal_ray[i];
1.373 + }
1.374 +
1.375 + SoplexLp::Value SoplexLp::_getDualRay(int i) const {
1.376 + if (_dual_ray.empty()) {
1.377 + _dual_ray.resize(soplex->nRows());
1.378 + soplex::Vector dv(_dual_ray.size(), &_dual_ray.front());
1.379 + soplex->getDualfarkas(dv);
1.380 + }
1.381 + return _dual_ray[i];
1.382 + }
1.383 +
1.384 + SoplexLp::ProblemType SoplexLp::_getPrimalType() const {
1.385 + switch (soplex->status()) {
1.386 + case soplex::SPxSolver::OPTIMAL:
1.387 + return OPTIMAL;
1.388 + case soplex::SPxSolver::UNBOUNDED:
1.389 + return UNBOUNDED;
1.390 + case soplex::SPxSolver::INFEASIBLE:
1.391 + return INFEASIBLE;
1.392 + default:
1.393 + return UNDEFINED;
1.394 + }
1.395 + }
1.396 +
1.397 + SoplexLp::ProblemType SoplexLp::_getDualType() const {
1.398 + switch (soplex->status()) {
1.399 + case soplex::SPxSolver::OPTIMAL:
1.400 + return OPTIMAL;
1.401 + case soplex::SPxSolver::UNBOUNDED:
1.402 + return UNBOUNDED;
1.403 + case soplex::SPxSolver::INFEASIBLE:
1.404 + return INFEASIBLE;
1.405 + default:
1.406 + return UNDEFINED;
1.407 + }
1.408 + }
1.409 +
1.410 + void SoplexLp::_setSense(Sense sense) {
1.411 + switch (sense) {
1.412 + case MIN:
1.413 + soplex->changeSense(soplex::SPxSolver::MINIMIZE);
1.414 + break;
1.415 + case MAX:
1.416 + soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
1.417 + }
1.418 + }
1.419 +
1.420 + SoplexLp::Sense SoplexLp::_getSense() const {
1.421 + switch (soplex->spxSense()) {
1.422 + case soplex::SPxSolver::MAXIMIZE:
1.423 + return MAX;
1.424 + case soplex::SPxSolver::MINIMIZE:
1.425 + return MIN;
1.426 + default:
1.427 + LEMON_ASSERT(false, "Wrong sense.");
1.428 + return SoplexLp::Sense();
1.429 + }
1.430 + }
1.431 +
1.432 + void SoplexLp::_clear() {
1.433 + soplex->clear();
1.434 + _col_names.clear();
1.435 + _col_names_ref.clear();
1.436 + _row_names.clear();
1.437 + _row_names_ref.clear();
1.438 + cols.clear();
1.439 + rows.clear();
1.440 + _clear_temporals();
1.441 + }
1.442 +
1.443 + void SoplexLp::_messageLevel(MessageLevel level) {
1.444 + switch (level) {
1.445 + case MESSAGE_NOTHING:
1.446 + _message_level = -1;
1.447 + break;
1.448 + case MESSAGE_ERROR:
1.449 + _message_level = soplex::SPxOut::ERROR;
1.450 + break;
1.451 + case MESSAGE_WARNING:
1.452 + _message_level = soplex::SPxOut::WARNING;
1.453 + break;
1.454 + case MESSAGE_NORMAL:
1.455 + _message_level = soplex::SPxOut::INFO2;
1.456 + break;
1.457 + case MESSAGE_VERBOSE:
1.458 + _message_level = soplex::SPxOut::DEBUG;
1.459 + break;
1.460 + }
1.461 + }
1.462 +
1.463 + void SoplexLp::_applyMessageLevel() {
1.464 + soplex::Param::setVerbose(_message_level);
1.465 + }
1.466 +
1.467 +} //namespace lemon
1.468 +