[2313] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include<iostream> |
---|
| 20 | #include<lemon/lp_soplex.h> |
---|
| 21 | |
---|
| 22 | #include <soplex/soplex.h> |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | ///\file |
---|
| 26 | ///\brief Implementation of the LEMON-SOPLEX lp solver interface. |
---|
| 27 | namespace lemon { |
---|
| 28 | |
---|
| 29 | LpSoplex::LpSoplex() : LpSolverBase() { |
---|
[2363] | 30 | rows.setIdHandler(relocateIdHandler); |
---|
| 31 | cols.setIdHandler(relocateIdHandler); |
---|
[2313] | 32 | soplex = new soplex::SoPlex; |
---|
[2363] | 33 | solved = false; |
---|
[2313] | 34 | } |
---|
| 35 | |
---|
| 36 | LpSoplex::~LpSoplex() { |
---|
| 37 | delete soplex; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | LpSolverBase &LpSoplex::_newLp() { |
---|
| 41 | LpSoplex* newlp = new LpSoplex(); |
---|
| 42 | return *newlp; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | LpSolverBase &LpSoplex::_copyLp() { |
---|
| 46 | LpSoplex* newlp = new LpSoplex(); |
---|
| 47 | ((soplex::SPxLP&)*(newlp->soplex)) = *soplex; |
---|
| 48 | return *newlp; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | int LpSoplex::_addCol() { |
---|
| 52 | soplex::LPCol col; |
---|
[2363] | 53 | col.setLower(-soplex::infinity); |
---|
| 54 | col.setUpper(soplex::infinity); |
---|
[2313] | 55 | soplex->addCol(col); |
---|
| 56 | |
---|
| 57 | colNames.push_back(std::string()); |
---|
[2363] | 58 | primal_value.push_back(0.0); |
---|
| 59 | solved = false; |
---|
[2313] | 60 | |
---|
| 61 | return soplex->nCols() - 1; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | int LpSoplex::_addRow() { |
---|
| 65 | soplex::LPRow row; |
---|
[2363] | 66 | row.setLhs(-soplex::infinity); |
---|
| 67 | row.setRhs(soplex::infinity); |
---|
[2313] | 68 | soplex->addRow(row); |
---|
| 69 | |
---|
[2363] | 70 | dual_value.push_back(0.0); |
---|
| 71 | solved = false; |
---|
[2313] | 72 | |
---|
| 73 | return soplex->nRows() - 1; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | void LpSoplex::_eraseCol(int i) { |
---|
| 78 | soplex->removeCol(i); |
---|
[2363] | 79 | colNames[i] = colNames.back(); |
---|
| 80 | colNames.pop_back(); |
---|
| 81 | primal_value[i] = primal_value.back(); |
---|
| 82 | primal_value.pop_back(); |
---|
| 83 | solved = false; |
---|
[2313] | 84 | } |
---|
| 85 | |
---|
| 86 | void LpSoplex::_eraseRow(int i) { |
---|
| 87 | soplex->removeRow(i); |
---|
[2363] | 88 | dual_value[i] = dual_value.back(); |
---|
| 89 | dual_value.pop_back(); |
---|
| 90 | solved = false; |
---|
[2313] | 91 | } |
---|
| 92 | |
---|
[2366] | 93 | void LpSoplex::_getColName(int col, std::string &name) const { |
---|
[2313] | 94 | name = colNames[col]; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void LpSoplex::_setColName(int col, const std::string &name) { |
---|
[2366] | 98 | invColNames.erase(colNames[col]); |
---|
[2313] | 99 | colNames[col] = name; |
---|
[2366] | 100 | if (!name.empty()) { |
---|
| 101 | invColNames.insert(std::make_pair(name, col)); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | int LpSoplex::_colByName(const std::string& name) const { |
---|
| 106 | std::map<std::string, int>::const_iterator it = |
---|
| 107 | invColNames.find(name); |
---|
| 108 | if (it != invColNames.end()) { |
---|
| 109 | return it->second; |
---|
| 110 | } else { |
---|
| 111 | return -1; |
---|
| 112 | } |
---|
[2313] | 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
[2364] | 116 | void LpSoplex::_setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e) { |
---|
[2313] | 117 | for (int j = 0; j < soplex->nCols(); ++j) { |
---|
| 118 | soplex->changeElement(i, j, 0.0); |
---|
| 119 | } |
---|
[2364] | 120 | for(ConstRowIterator it = b; it != e; ++it) { |
---|
[2313] | 121 | soplex->changeElement(i, it->first, it->second); |
---|
| 122 | } |
---|
[2363] | 123 | solved = false; |
---|
[2313] | 124 | } |
---|
[2364] | 125 | |
---|
[2366] | 126 | void LpSoplex::_getRowCoeffs(int i, RowIterator b) const { |
---|
[2364] | 127 | const soplex::SVector& vec = soplex->rowVector(i); |
---|
| 128 | for (int k = 0; k < vec.size(); ++k) { |
---|
| 129 | *b = std::make_pair(vec.index(k), vec.value(k)); |
---|
| 130 | ++b; |
---|
| 131 | } |
---|
| 132 | } |
---|
[2313] | 133 | |
---|
[2364] | 134 | void LpSoplex::_setColCoeffs(int j, ConstColIterator b, ConstColIterator e) { |
---|
[2313] | 135 | for (int i = 0; i < soplex->nRows(); ++i) { |
---|
| 136 | soplex->changeElement(i, j, 0.0); |
---|
| 137 | } |
---|
[2364] | 138 | for(ConstColIterator it = b; it != e; ++it) { |
---|
[2313] | 139 | soplex->changeElement(it->first, j, it->second); |
---|
| 140 | } |
---|
[2363] | 141 | solved = false; |
---|
[2313] | 142 | } |
---|
[2364] | 143 | |
---|
[2366] | 144 | void LpSoplex::_getColCoeffs(int i, ColIterator b) const { |
---|
[2364] | 145 | const soplex::SVector& vec = soplex->colVector(i); |
---|
| 146 | for (int k = 0; k < vec.size(); ++k) { |
---|
| 147 | *b = std::make_pair(vec.index(k), vec.value(k)); |
---|
| 148 | ++b; |
---|
| 149 | } |
---|
| 150 | } |
---|
[2313] | 151 | |
---|
[2363] | 152 | void LpSoplex::_setCoeff(int i, int j, Value value) { |
---|
| 153 | soplex->changeElement(i, j, value); |
---|
| 154 | solved = false; |
---|
| 155 | } |
---|
| 156 | |
---|
[2366] | 157 | LpSoplex::Value LpSoplex::_getCoeff(int i, int j) const { |
---|
[2363] | 158 | return soplex->rowVector(i)[j]; |
---|
[2313] | 159 | } |
---|
| 160 | |
---|
| 161 | void LpSoplex::_setColLowerBound(int i, Value value) { |
---|
[2363] | 162 | soplex->changeLower(i, value != -INF ? value : -soplex::infinity); |
---|
| 163 | solved = false; |
---|
[2313] | 164 | } |
---|
| 165 | |
---|
[2366] | 166 | LpSoplex::Value LpSoplex::_getColLowerBound(int i) const { |
---|
[2363] | 167 | double value = soplex->lower(i); |
---|
| 168 | return value != -soplex::infinity ? value : -INF; |
---|
| 169 | } |
---|
| 170 | |
---|
[2313] | 171 | void LpSoplex::_setColUpperBound(int i, Value value) { |
---|
[2363] | 172 | soplex->changeUpper(i, value != INF ? value : soplex::infinity); |
---|
| 173 | solved = false; |
---|
| 174 | } |
---|
| 175 | |
---|
[2366] | 176 | LpSoplex::Value LpSoplex::_getColUpperBound(int i) const { |
---|
[2363] | 177 | double value = soplex->upper(i); |
---|
| 178 | return value != soplex::infinity ? value : INF; |
---|
[2313] | 179 | } |
---|
| 180 | |
---|
| 181 | void LpSoplex::_setRowBounds(int i, Value lb, Value ub) { |
---|
[2363] | 182 | soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, |
---|
| 183 | ub != INF ? ub : soplex::infinity); |
---|
| 184 | solved = false; |
---|
| 185 | } |
---|
[2366] | 186 | void LpSoplex::_getRowBounds(int i, Value &lower, Value &upper) const { |
---|
[2363] | 187 | lower = soplex->lhs(i); |
---|
| 188 | if (lower == -soplex::infinity) lower = -INF; |
---|
| 189 | upper = soplex->rhs(i); |
---|
| 190 | if (upper == -soplex::infinity) upper = INF; |
---|
[2313] | 191 | } |
---|
| 192 | |
---|
| 193 | void LpSoplex::_setObjCoeff(int i, Value obj_coef) { |
---|
| 194 | soplex->changeObj(i, obj_coef); |
---|
[2363] | 195 | solved = false; |
---|
| 196 | } |
---|
| 197 | |
---|
[2366] | 198 | LpSoplex::Value LpSoplex::_getObjCoeff(int i) const { |
---|
[2363] | 199 | return soplex->obj(i); |
---|
[2313] | 200 | } |
---|
| 201 | |
---|
| 202 | void LpSoplex::_clearObj() { |
---|
| 203 | for (int i = 0; i < soplex->nCols(); ++i) { |
---|
| 204 | soplex->changeObj(i, 0.0); |
---|
| 205 | } |
---|
[2363] | 206 | solved = false; |
---|
[2313] | 207 | } |
---|
| 208 | |
---|
| 209 | LpSoplex::SolveExitStatus LpSoplex::_solve() { |
---|
| 210 | soplex::SPxSolver::Status status = soplex->solve(); |
---|
| 211 | |
---|
[2363] | 212 | soplex::Vector pv(primal_value.size(), &primal_value[0]); |
---|
[2313] | 213 | soplex->getPrimal(pv); |
---|
| 214 | |
---|
[2363] | 215 | soplex::Vector dv(dual_value.size(), &dual_value[0]); |
---|
[2313] | 216 | soplex->getDual(dv); |
---|
| 217 | |
---|
| 218 | switch (status) { |
---|
| 219 | case soplex::SPxSolver::OPTIMAL: |
---|
| 220 | case soplex::SPxSolver::INFEASIBLE: |
---|
| 221 | case soplex::SPxSolver::UNBOUNDED: |
---|
[2363] | 222 | solved = true; |
---|
[2313] | 223 | return SOLVED; |
---|
| 224 | default: |
---|
| 225 | return UNSOLVED; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
[2366] | 229 | LpSoplex::Value LpSoplex::_getPrimal(int i) const { |
---|
[2363] | 230 | return primal_value[i]; |
---|
[2313] | 231 | } |
---|
| 232 | |
---|
[2366] | 233 | LpSoplex::Value LpSoplex::_getDual(int i) const { |
---|
[2363] | 234 | return dual_value[i]; |
---|
[2313] | 235 | } |
---|
| 236 | |
---|
[2366] | 237 | LpSoplex::Value LpSoplex::_getPrimalValue() const { |
---|
[2313] | 238 | return soplex->objValue(); |
---|
| 239 | } |
---|
| 240 | |
---|
[2366] | 241 | bool LpSoplex::_isBasicCol(int i) const { |
---|
[2313] | 242 | return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC; |
---|
| 243 | } |
---|
| 244 | |
---|
[2366] | 245 | LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() const { |
---|
[2363] | 246 | if (!solved) return UNDEFINED; |
---|
[2313] | 247 | switch (soplex->status()) { |
---|
| 248 | case soplex::SPxSolver::OPTIMAL: |
---|
| 249 | return OPTIMAL; |
---|
| 250 | case soplex::SPxSolver::UNBOUNDED: |
---|
| 251 | return INFINITE; |
---|
| 252 | case soplex::SPxSolver::INFEASIBLE: |
---|
| 253 | return INFEASIBLE; |
---|
| 254 | default: |
---|
| 255 | return UNDEFINED; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
[2366] | 259 | LpSoplex::SolutionStatus LpSoplex::_getDualStatus() const { |
---|
[2363] | 260 | if (!solved) return UNDEFINED; |
---|
| 261 | switch (soplex->status()) { |
---|
| 262 | case soplex::SPxSolver::OPTIMAL: |
---|
[2313] | 263 | return OPTIMAL; |
---|
[2363] | 264 | case soplex::SPxSolver::UNBOUNDED: |
---|
[2313] | 265 | return INFEASIBLE; |
---|
[2363] | 266 | default: |
---|
[2313] | 267 | return UNDEFINED; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
[2366] | 271 | LpSoplex::ProblemTypes LpSoplex::_getProblemType() const { |
---|
[2363] | 272 | if (!solved) return UNKNOWN; |
---|
| 273 | switch (soplex->status()) { |
---|
| 274 | case soplex::SPxSolver::OPTIMAL: |
---|
[2313] | 275 | return PRIMAL_DUAL_FEASIBLE; |
---|
[2363] | 276 | case soplex::SPxSolver::UNBOUNDED: |
---|
[2313] | 277 | return PRIMAL_FEASIBLE_DUAL_INFEASIBLE; |
---|
[2363] | 278 | default: |
---|
[2313] | 279 | return UNKNOWN; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | void LpSoplex::_setMax() { |
---|
| 284 | soplex->changeSense(soplex::SPxSolver::MAXIMIZE); |
---|
[2363] | 285 | solved = false; |
---|
[2313] | 286 | } |
---|
| 287 | void LpSoplex::_setMin() { |
---|
| 288 | soplex->changeSense(soplex::SPxSolver::MINIMIZE); |
---|
[2363] | 289 | solved = false; |
---|
[2313] | 290 | } |
---|
[2366] | 291 | bool LpSoplex::_isMax() const { |
---|
[2363] | 292 | return soplex->spxSense() == soplex::SPxSolver::MAXIMIZE; |
---|
| 293 | } |
---|
| 294 | |
---|
[2313] | 295 | |
---|
| 296 | } //namespace lemon |
---|
| 297 | |
---|