[2313] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2391] | 5 | * Copyright (C) 2003-2007 |
---|
[2313] | 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(); |
---|
[2386] | 47 | (*static_cast<soplex::SPxLP*>(newlp->soplex)) = *soplex; |
---|
[2313] | 48 | return *newlp; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | int LpSoplex::_addCol() { |
---|
[2386] | 52 | soplex::LPCol c; |
---|
| 53 | c.setLower(-soplex::infinity); |
---|
| 54 | c.setUpper(soplex::infinity); |
---|
| 55 | soplex->addCol(c); |
---|
[2313] | 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() { |
---|
[2386] | 65 | soplex::LPRow r; |
---|
| 66 | r.setLhs(-soplex::infinity); |
---|
| 67 | r.setRhs(soplex::infinity); |
---|
| 68 | soplex->addRow(r); |
---|
[2313] | 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); |
---|
[2368] | 79 | invColNames.erase(colNames[i]); |
---|
[2363] | 80 | colNames[i] = colNames.back(); |
---|
[2368] | 81 | invColNames[colNames.back()] = i; |
---|
[2363] | 82 | colNames.pop_back(); |
---|
| 83 | primal_value[i] = primal_value.back(); |
---|
| 84 | primal_value.pop_back(); |
---|
| 85 | solved = false; |
---|
[2313] | 86 | } |
---|
| 87 | |
---|
| 88 | void LpSoplex::_eraseRow(int i) { |
---|
| 89 | soplex->removeRow(i); |
---|
[2363] | 90 | dual_value[i] = dual_value.back(); |
---|
| 91 | dual_value.pop_back(); |
---|
| 92 | solved = false; |
---|
[2313] | 93 | } |
---|
| 94 | |
---|
[2386] | 95 | void LpSoplex::_getColName(int c, std::string &name) const { |
---|
| 96 | name = colNames[c]; |
---|
[2313] | 97 | } |
---|
| 98 | |
---|
[2386] | 99 | void LpSoplex::_setColName(int c, const std::string &name) { |
---|
| 100 | invColNames.erase(colNames[c]); |
---|
| 101 | colNames[c] = name; |
---|
[2366] | 102 | if (!name.empty()) { |
---|
[2386] | 103 | invColNames.insert(std::make_pair(name, c)); |
---|
[2366] | 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | int LpSoplex::_colByName(const std::string& name) const { |
---|
| 108 | std::map<std::string, int>::const_iterator it = |
---|
| 109 | invColNames.find(name); |
---|
| 110 | if (it != invColNames.end()) { |
---|
| 111 | return it->second; |
---|
| 112 | } else { |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
[2313] | 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
[2364] | 118 | void LpSoplex::_setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e) { |
---|
[2313] | 119 | for (int j = 0; j < soplex->nCols(); ++j) { |
---|
| 120 | soplex->changeElement(i, j, 0.0); |
---|
| 121 | } |
---|
[2364] | 122 | for(ConstRowIterator it = b; it != e; ++it) { |
---|
[2313] | 123 | soplex->changeElement(i, it->first, it->second); |
---|
| 124 | } |
---|
[2363] | 125 | solved = false; |
---|
[2313] | 126 | } |
---|
[2364] | 127 | |
---|
[2366] | 128 | void LpSoplex::_getRowCoeffs(int i, RowIterator b) const { |
---|
[2364] | 129 | const soplex::SVector& vec = soplex->rowVector(i); |
---|
| 130 | for (int k = 0; k < vec.size(); ++k) { |
---|
| 131 | *b = std::make_pair(vec.index(k), vec.value(k)); |
---|
| 132 | ++b; |
---|
| 133 | } |
---|
| 134 | } |
---|
[2313] | 135 | |
---|
[2364] | 136 | void LpSoplex::_setColCoeffs(int j, ConstColIterator b, ConstColIterator e) { |
---|
[2313] | 137 | for (int i = 0; i < soplex->nRows(); ++i) { |
---|
| 138 | soplex->changeElement(i, j, 0.0); |
---|
| 139 | } |
---|
[2364] | 140 | for(ConstColIterator it = b; it != e; ++it) { |
---|
[2313] | 141 | soplex->changeElement(it->first, j, it->second); |
---|
| 142 | } |
---|
[2363] | 143 | solved = false; |
---|
[2313] | 144 | } |
---|
[2364] | 145 | |
---|
[2366] | 146 | void LpSoplex::_getColCoeffs(int i, ColIterator b) const { |
---|
[2364] | 147 | const soplex::SVector& vec = soplex->colVector(i); |
---|
| 148 | for (int k = 0; k < vec.size(); ++k) { |
---|
| 149 | *b = std::make_pair(vec.index(k), vec.value(k)); |
---|
| 150 | ++b; |
---|
| 151 | } |
---|
| 152 | } |
---|
[2313] | 153 | |
---|
[2363] | 154 | void LpSoplex::_setCoeff(int i, int j, Value value) { |
---|
| 155 | soplex->changeElement(i, j, value); |
---|
| 156 | solved = false; |
---|
| 157 | } |
---|
| 158 | |
---|
[2366] | 159 | LpSoplex::Value LpSoplex::_getCoeff(int i, int j) const { |
---|
[2363] | 160 | return soplex->rowVector(i)[j]; |
---|
[2313] | 161 | } |
---|
| 162 | |
---|
| 163 | void LpSoplex::_setColLowerBound(int i, Value value) { |
---|
[2363] | 164 | soplex->changeLower(i, value != -INF ? value : -soplex::infinity); |
---|
| 165 | solved = false; |
---|
[2313] | 166 | } |
---|
| 167 | |
---|
[2366] | 168 | LpSoplex::Value LpSoplex::_getColLowerBound(int i) const { |
---|
[2363] | 169 | double value = soplex->lower(i); |
---|
| 170 | return value != -soplex::infinity ? value : -INF; |
---|
| 171 | } |
---|
| 172 | |
---|
[2313] | 173 | void LpSoplex::_setColUpperBound(int i, Value value) { |
---|
[2363] | 174 | soplex->changeUpper(i, value != INF ? value : soplex::infinity); |
---|
| 175 | solved = false; |
---|
| 176 | } |
---|
| 177 | |
---|
[2366] | 178 | LpSoplex::Value LpSoplex::_getColUpperBound(int i) const { |
---|
[2363] | 179 | double value = soplex->upper(i); |
---|
| 180 | return value != soplex::infinity ? value : INF; |
---|
[2313] | 181 | } |
---|
| 182 | |
---|
| 183 | void LpSoplex::_setRowBounds(int i, Value lb, Value ub) { |
---|
[2363] | 184 | soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, |
---|
| 185 | ub != INF ? ub : soplex::infinity); |
---|
| 186 | solved = false; |
---|
| 187 | } |
---|
[2366] | 188 | void LpSoplex::_getRowBounds(int i, Value &lower, Value &upper) const { |
---|
[2363] | 189 | lower = soplex->lhs(i); |
---|
| 190 | if (lower == -soplex::infinity) lower = -INF; |
---|
| 191 | upper = soplex->rhs(i); |
---|
| 192 | if (upper == -soplex::infinity) upper = INF; |
---|
[2313] | 193 | } |
---|
| 194 | |
---|
| 195 | void LpSoplex::_setObjCoeff(int i, Value obj_coef) { |
---|
| 196 | soplex->changeObj(i, obj_coef); |
---|
[2363] | 197 | solved = false; |
---|
| 198 | } |
---|
| 199 | |
---|
[2366] | 200 | LpSoplex::Value LpSoplex::_getObjCoeff(int i) const { |
---|
[2363] | 201 | return soplex->obj(i); |
---|
[2313] | 202 | } |
---|
| 203 | |
---|
| 204 | void LpSoplex::_clearObj() { |
---|
| 205 | for (int i = 0; i < soplex->nCols(); ++i) { |
---|
| 206 | soplex->changeObj(i, 0.0); |
---|
| 207 | } |
---|
[2363] | 208 | solved = false; |
---|
[2313] | 209 | } |
---|
| 210 | |
---|
| 211 | LpSoplex::SolveExitStatus LpSoplex::_solve() { |
---|
| 212 | soplex::SPxSolver::Status status = soplex->solve(); |
---|
| 213 | |
---|
[2363] | 214 | soplex::Vector pv(primal_value.size(), &primal_value[0]); |
---|
[2313] | 215 | soplex->getPrimal(pv); |
---|
| 216 | |
---|
[2363] | 217 | soplex::Vector dv(dual_value.size(), &dual_value[0]); |
---|
[2313] | 218 | soplex->getDual(dv); |
---|
| 219 | |
---|
| 220 | switch (status) { |
---|
| 221 | case soplex::SPxSolver::OPTIMAL: |
---|
| 222 | case soplex::SPxSolver::INFEASIBLE: |
---|
| 223 | case soplex::SPxSolver::UNBOUNDED: |
---|
[2363] | 224 | solved = true; |
---|
[2313] | 225 | return SOLVED; |
---|
| 226 | default: |
---|
| 227 | return UNSOLVED; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
[2366] | 231 | LpSoplex::Value LpSoplex::_getPrimal(int i) const { |
---|
[2363] | 232 | return primal_value[i]; |
---|
[2313] | 233 | } |
---|
| 234 | |
---|
[2366] | 235 | LpSoplex::Value LpSoplex::_getDual(int i) const { |
---|
[2363] | 236 | return dual_value[i]; |
---|
[2313] | 237 | } |
---|
| 238 | |
---|
[2366] | 239 | LpSoplex::Value LpSoplex::_getPrimalValue() const { |
---|
[2313] | 240 | return soplex->objValue(); |
---|
| 241 | } |
---|
| 242 | |
---|
[2366] | 243 | bool LpSoplex::_isBasicCol(int i) const { |
---|
[2313] | 244 | return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC; |
---|
| 245 | } |
---|
| 246 | |
---|
[2366] | 247 | LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() const { |
---|
[2363] | 248 | if (!solved) return UNDEFINED; |
---|
[2313] | 249 | switch (soplex->status()) { |
---|
| 250 | case soplex::SPxSolver::OPTIMAL: |
---|
| 251 | return OPTIMAL; |
---|
| 252 | case soplex::SPxSolver::UNBOUNDED: |
---|
| 253 | return INFINITE; |
---|
| 254 | case soplex::SPxSolver::INFEASIBLE: |
---|
| 255 | return INFEASIBLE; |
---|
| 256 | default: |
---|
| 257 | return UNDEFINED; |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
[2366] | 261 | LpSoplex::SolutionStatus LpSoplex::_getDualStatus() const { |
---|
[2363] | 262 | if (!solved) return UNDEFINED; |
---|
| 263 | switch (soplex->status()) { |
---|
| 264 | case soplex::SPxSolver::OPTIMAL: |
---|
[2313] | 265 | return OPTIMAL; |
---|
[2363] | 266 | case soplex::SPxSolver::UNBOUNDED: |
---|
[2313] | 267 | return INFEASIBLE; |
---|
[2363] | 268 | default: |
---|
[2313] | 269 | return UNDEFINED; |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
[2366] | 273 | LpSoplex::ProblemTypes LpSoplex::_getProblemType() const { |
---|
[2363] | 274 | if (!solved) return UNKNOWN; |
---|
| 275 | switch (soplex->status()) { |
---|
| 276 | case soplex::SPxSolver::OPTIMAL: |
---|
[2313] | 277 | return PRIMAL_DUAL_FEASIBLE; |
---|
[2363] | 278 | case soplex::SPxSolver::UNBOUNDED: |
---|
[2313] | 279 | return PRIMAL_FEASIBLE_DUAL_INFEASIBLE; |
---|
[2363] | 280 | default: |
---|
[2313] | 281 | return UNKNOWN; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | void LpSoplex::_setMax() { |
---|
| 286 | soplex->changeSense(soplex::SPxSolver::MAXIMIZE); |
---|
[2363] | 287 | solved = false; |
---|
[2313] | 288 | } |
---|
| 289 | void LpSoplex::_setMin() { |
---|
| 290 | soplex->changeSense(soplex::SPxSolver::MINIMIZE); |
---|
[2363] | 291 | solved = false; |
---|
[2313] | 292 | } |
---|
[2366] | 293 | bool LpSoplex::_isMax() const { |
---|
[2363] | 294 | return soplex->spxSense() == soplex::SPxSolver::MAXIMIZE; |
---|
| 295 | } |
---|
| 296 | |
---|
[2313] | 297 | |
---|
| 298 | } //namespace lemon |
---|
| 299 | |
---|