0
11
0
57
57
6
6
39
39
14
14
40
40
10
10
10
10
53
53
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/clp.h> |
| 20 | 20 |
#include <coin/ClpSimplex.hpp> |
| 21 | 21 |
|
| 22 | 22 |
namespace lemon {
|
| 23 | 23 |
|
| 24 |
|
|
| 24 |
ClpLp::ClpLp() {
|
|
| 25 | 25 |
_prob = new ClpSimplex(); |
| 26 | 26 |
_init_temporals(); |
| 27 | 27 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 28 | 28 |
} |
| 29 | 29 |
|
| 30 |
|
|
| 30 |
ClpLp::ClpLp(const ClpLp& other) {
|
|
| 31 | 31 |
_prob = new ClpSimplex(*other._prob); |
| 32 | 32 |
rows = other.rows; |
| 33 | 33 |
cols = other.cols; |
| 34 | 34 |
_init_temporals(); |
| 35 | 35 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 |
|
|
| 38 |
ClpLp::~ClpLp() {
|
|
| 39 | 39 |
delete _prob; |
| 40 | 40 |
_clear_temporals(); |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 |
void |
|
| 43 |
void ClpLp::_init_temporals() {
|
|
| 44 | 44 |
_primal_ray = 0; |
| 45 | 45 |
_dual_ray = 0; |
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 |
void |
|
| 48 |
void ClpLp::_clear_temporals() {
|
|
| 49 | 49 |
if (_primal_ray) {
|
| 50 | 50 |
delete[] _primal_ray; |
| 51 | 51 |
_primal_ray = 0; |
| 52 | 52 |
} |
| 53 | 53 |
if (_dual_ray) {
|
| 54 | 54 |
delete[] _dual_ray; |
| 55 | 55 |
_dual_ray = 0; |
| 56 | 56 |
} |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 |
LpClp* LpClp::_newSolver() const {
|
|
| 60 |
LpClp* newlp = new LpClp; |
|
| 59 |
ClpLp* ClpLp::_newSolver() const {
|
|
| 60 |
ClpLp* newlp = new ClpLp; |
|
| 61 | 61 |
return newlp; |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 |
LpClp* LpClp::_cloneSolver() const {
|
|
| 65 |
LpClp* copylp = new LpClp(*this); |
|
| 64 |
ClpLp* ClpLp::_cloneSolver() const {
|
|
| 65 |
ClpLp* copylp = new ClpLp(*this); |
|
| 66 | 66 |
return copylp; |
| 67 | 67 |
} |
| 68 | 68 |
|
| 69 |
const char* |
|
| 69 |
const char* ClpLp::_solverName() const { return "ClpLp"; }
|
|
| 70 | 70 |
|
| 71 |
int |
|
| 71 |
int ClpLp::_addCol() {
|
|
| 72 | 72 |
_prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0); |
| 73 | 73 |
return _prob->numberColumns() - 1; |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 |
int |
|
| 76 |
int ClpLp::_addRow() {
|
|
| 77 | 77 |
_prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX); |
| 78 | 78 |
return _prob->numberRows() - 1; |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
|
| 82 |
void |
|
| 82 |
void ClpLp::_eraseCol(int c) {
|
|
| 83 | 83 |
_col_names_ref.erase(_prob->getColumnName(c)); |
| 84 | 84 |
_prob->deleteColumns(1, &c); |
| 85 | 85 |
} |
| 86 | 86 |
|
| 87 |
void |
|
| 87 |
void ClpLp::_eraseRow(int r) {
|
|
| 88 | 88 |
_row_names_ref.erase(_prob->getRowName(r)); |
| 89 | 89 |
_prob->deleteRows(1, &r); |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 |
void |
|
| 92 |
void ClpLp::_eraseColId(int i) {
|
|
| 93 | 93 |
cols.eraseIndex(i); |
| 94 | 94 |
cols.shiftIndices(i); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 |
void |
|
| 97 |
void ClpLp::_eraseRowId(int i) {
|
|
| 98 | 98 |
rows.eraseIndex(i); |
| 99 | 99 |
rows.shiftIndices(i); |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 |
void |
|
| 102 |
void ClpLp::_getColName(int c, std::string& name) const {
|
|
| 103 | 103 |
name = _prob->getColumnName(c); |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 |
void |
|
| 106 |
void ClpLp::_setColName(int c, const std::string& name) {
|
|
| 107 | 107 |
_prob->setColumnName(c, const_cast<std::string&>(name)); |
| 108 | 108 |
_col_names_ref[name] = c; |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 |
int |
|
| 111 |
int ClpLp::_colByName(const std::string& name) const {
|
|
| 112 | 112 |
std::map<std::string, int>::const_iterator it = _col_names_ref.find(name); |
| 113 | 113 |
return it != _col_names_ref.end() ? it->second : -1; |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 |
void |
|
| 116 |
void ClpLp::_getRowName(int r, std::string& name) const {
|
|
| 117 | 117 |
name = _prob->getRowName(r); |
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 |
void |
|
| 120 |
void ClpLp::_setRowName(int r, const std::string& name) {
|
|
| 121 | 121 |
_prob->setRowName(r, const_cast<std::string&>(name)); |
| 122 | 122 |
_row_names_ref[name] = r; |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 |
int |
|
| 125 |
int ClpLp::_rowByName(const std::string& name) const {
|
|
| 126 | 126 |
std::map<std::string, int>::const_iterator it = _row_names_ref.find(name); |
| 127 | 127 |
return it != _row_names_ref.end() ? it->second : -1; |
| 128 | 128 |
} |
| 129 | 129 |
|
| 130 | 130 |
|
| 131 |
void |
|
| 131 |
void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
|
|
| 132 | 132 |
std::map<int, Value> coeffs; |
| 133 | 133 |
|
| 134 | 134 |
int n = _prob->clpMatrix()->getNumCols(); |
| 135 | 135 |
|
| 136 | 136 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 137 | 137 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 138 | 138 |
|
| 139 | 139 |
for (int i = 0; i < n; ++i) {
|
| 140 | 140 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; |
| 141 | 141 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; |
| 142 | 142 |
|
| 143 | 143 |
const int* it = std::lower_bound(indices + begin, indices + end, ix); |
| 144 | 144 |
if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
|
| 145 | 145 |
coeffs[i] = 0.0; |
| 146 | 146 |
} |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
for (ExprIterator it = b; it != e; ++it) {
|
| 150 | 150 |
coeffs[it->first] = it->second; |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
for (std::map<int, Value>::iterator it = coeffs.begin(); |
| 154 | 154 |
it != coeffs.end(); ++it) {
|
| 155 | 155 |
_prob->modifyCoefficient(ix, it->first, it->second); |
| 156 | 156 |
} |
| 157 | 157 |
} |
| 158 | 158 |
|
| 159 |
void |
|
| 159 |
void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const {
|
|
| 160 | 160 |
int n = _prob->clpMatrix()->getNumCols(); |
| 161 | 161 |
|
| 162 | 162 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 163 | 163 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 164 | 164 |
|
| 165 | 165 |
for (int i = 0; i < n; ++i) {
|
| 166 | 166 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; |
| 167 | 167 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; |
| 168 | 168 |
|
| 169 | 169 |
const int* it = std::lower_bound(indices + begin, indices + end, ix); |
| 170 | 170 |
if (it != indices + end && *it == ix) {
|
| 171 | 171 |
*b = std::make_pair(i, elements[it - indices]); |
| 172 | 172 |
} |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 |
void |
|
| 176 |
void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
|
|
| 177 | 177 |
std::map<int, Value> coeffs; |
| 178 | 178 |
|
| 179 | 179 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
| 180 | 180 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
| 181 | 181 |
|
| 182 | 182 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 183 | 183 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 184 | 184 |
|
| 185 | 185 |
for (CoinBigIndex i = begin; i != end; ++i) {
|
| 186 | 186 |
if (elements[i] != 0.0) {
|
| 187 | 187 |
coeffs[indices[i]] = 0.0; |
| 188 | 188 |
} |
| 189 | 189 |
} |
| 190 | 190 |
for (ExprIterator it = b; it != e; ++it) {
|
| 191 | 191 |
coeffs[it->first] = it->second; |
| 192 | 192 |
} |
| 193 | 193 |
for (std::map<int, Value>::iterator it = coeffs.begin(); |
| 194 | 194 |
it != coeffs.end(); ++it) {
|
| 195 | 195 |
_prob->modifyCoefficient(it->first, ix, it->second); |
| 196 | 196 |
} |
| 197 | 197 |
} |
| 198 | 198 |
|
| 199 |
void |
|
| 199 |
void ClpLp::_getColCoeffs(int ix, InsertIterator b) const {
|
|
| 200 | 200 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
| 201 | 201 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
| 202 | 202 |
|
| 203 | 203 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 204 | 204 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 205 | 205 |
|
| 206 | 206 |
for (CoinBigIndex i = begin; i != end; ++i) {
|
| 207 | 207 |
*b = std::make_pair(indices[i], elements[i]); |
| 208 | 208 |
++b; |
| 209 | 209 |
} |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 |
void |
|
| 212 |
void ClpLp::_setCoeff(int ix, int jx, Value value) {
|
|
| 213 | 213 |
_prob->modifyCoefficient(ix, jx, value); |
| 214 | 214 |
} |
| 215 | 215 |
|
| 216 |
|
|
| 216 |
ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const {
|
|
| 217 | 217 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
| 218 | 218 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
| 219 | 219 |
|
| 220 | 220 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 221 | 221 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 222 | 222 |
|
| 223 | 223 |
const int* it = std::lower_bound(indices + begin, indices + end, jx); |
| 224 | 224 |
if (it != indices + end && *it == jx) {
|
| 225 | 225 |
return elements[it - indices]; |
| 226 | 226 |
} else {
|
| 227 | 227 |
return 0.0; |
| 228 | 228 |
} |
| 229 | 229 |
} |
| 230 | 230 |
|
| 231 |
void |
|
| 231 |
void ClpLp::_setColLowerBound(int i, Value lo) {
|
|
| 232 | 232 |
_prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo); |
| 233 | 233 |
} |
| 234 | 234 |
|
| 235 |
|
|
| 235 |
ClpLp::Value ClpLp::_getColLowerBound(int i) const {
|
|
| 236 | 236 |
double val = _prob->getColLower()[i]; |
| 237 | 237 |
return val == - COIN_DBL_MAX ? - INF : val; |
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 |
void |
|
| 240 |
void ClpLp::_setColUpperBound(int i, Value up) {
|
|
| 241 | 241 |
_prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up); |
| 242 | 242 |
} |
| 243 | 243 |
|
| 244 |
|
|
| 244 |
ClpLp::Value ClpLp::_getColUpperBound(int i) const {
|
|
| 245 | 245 |
double val = _prob->getColUpper()[i]; |
| 246 | 246 |
return val == COIN_DBL_MAX ? INF : val; |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 |
void |
|
| 249 |
void ClpLp::_setRowLowerBound(int i, Value lo) {
|
|
| 250 | 250 |
_prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo); |
| 251 | 251 |
} |
| 252 | 252 |
|
| 253 |
|
|
| 253 |
ClpLp::Value ClpLp::_getRowLowerBound(int i) const {
|
|
| 254 | 254 |
double val = _prob->getRowLower()[i]; |
| 255 | 255 |
return val == - COIN_DBL_MAX ? - INF : val; |
| 256 | 256 |
} |
| 257 | 257 |
|
| 258 |
void |
|
| 258 |
void ClpLp::_setRowUpperBound(int i, Value up) {
|
|
| 259 | 259 |
_prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up); |
| 260 | 260 |
} |
| 261 | 261 |
|
| 262 |
|
|
| 262 |
ClpLp::Value ClpLp::_getRowUpperBound(int i) const {
|
|
| 263 | 263 |
double val = _prob->getRowUpper()[i]; |
| 264 | 264 |
return val == COIN_DBL_MAX ? INF : val; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 |
void |
|
| 267 |
void ClpLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
|
|
| 268 | 268 |
int num = _prob->clpMatrix()->getNumCols(); |
| 269 | 269 |
for (int i = 0; i < num; ++i) {
|
| 270 | 270 |
_prob->setObjectiveCoefficient(i, 0.0); |
| 271 | 271 |
} |
| 272 | 272 |
for (ExprIterator it = b; it != e; ++it) {
|
| 273 | 273 |
_prob->setObjectiveCoefficient(it->first, it->second); |
| 274 | 274 |
} |
| 275 | 275 |
} |
| 276 | 276 |
|
| 277 |
void |
|
| 277 |
void ClpLp::_getObjCoeffs(InsertIterator b) const {
|
|
| 278 | 278 |
int num = _prob->clpMatrix()->getNumCols(); |
| 279 | 279 |
for (int i = 0; i < num; ++i) {
|
| 280 | 280 |
Value coef = _prob->getObjCoefficients()[i]; |
| 281 | 281 |
if (coef != 0.0) {
|
| 282 | 282 |
*b = std::make_pair(i, coef); |
| 283 | 283 |
++b; |
| 284 | 284 |
} |
| 285 | 285 |
} |
| 286 | 286 |
} |
| 287 | 287 |
|
| 288 |
void |
|
| 288 |
void ClpLp::_setObjCoeff(int i, Value obj_coef) {
|
|
| 289 | 289 |
_prob->setObjectiveCoefficient(i, obj_coef); |
| 290 | 290 |
} |
| 291 | 291 |
|
| 292 |
|
|
| 292 |
ClpLp::Value ClpLp::_getObjCoeff(int i) const {
|
|
| 293 | 293 |
return _prob->getObjCoefficients()[i]; |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 |
|
|
| 296 |
ClpLp::SolveExitStatus ClpLp::_solve() {
|
|
| 297 | 297 |
return _prob->primal() >= 0 ? SOLVED : UNSOLVED; |
| 298 | 298 |
} |
| 299 | 299 |
|
| 300 |
|
|
| 300 |
ClpLp::SolveExitStatus ClpLp::solvePrimal() {
|
|
| 301 | 301 |
return _prob->primal() >= 0 ? SOLVED : UNSOLVED; |
| 302 | 302 |
} |
| 303 | 303 |
|
| 304 |
|
|
| 304 |
ClpLp::SolveExitStatus ClpLp::solveDual() {
|
|
| 305 | 305 |
return _prob->dual() >= 0 ? SOLVED : UNSOLVED; |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 |
|
|
| 308 |
ClpLp::SolveExitStatus ClpLp::solveBarrier() {
|
|
| 309 | 309 |
return _prob->barrier() >= 0 ? SOLVED : UNSOLVED; |
| 310 | 310 |
} |
| 311 | 311 |
|
| 312 |
|
|
| 312 |
ClpLp::Value ClpLp::_getPrimal(int i) const {
|
|
| 313 | 313 |
return _prob->primalColumnSolution()[i]; |
| 314 | 314 |
} |
| 315 |
|
|
| 315 |
ClpLp::Value ClpLp::_getPrimalValue() const {
|
|
| 316 | 316 |
return _prob->objectiveValue(); |
| 317 | 317 |
} |
| 318 | 318 |
|
| 319 |
|
|
| 319 |
ClpLp::Value ClpLp::_getDual(int i) const {
|
|
| 320 | 320 |
return _prob->dualRowSolution()[i]; |
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 |
|
|
| 323 |
ClpLp::Value ClpLp::_getPrimalRay(int i) const {
|
|
| 324 | 324 |
if (!_primal_ray) {
|
| 325 | 325 |
_primal_ray = _prob->unboundedRay(); |
| 326 | 326 |
LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided"); |
| 327 | 327 |
} |
| 328 | 328 |
return _primal_ray[i]; |
| 329 | 329 |
} |
| 330 | 330 |
|
| 331 |
|
|
| 331 |
ClpLp::Value ClpLp::_getDualRay(int i) const {
|
|
| 332 | 332 |
if (!_dual_ray) {
|
| 333 | 333 |
_dual_ray = _prob->infeasibilityRay(); |
| 334 | 334 |
LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided"); |
| 335 | 335 |
} |
| 336 | 336 |
return _dual_ray[i]; |
| 337 | 337 |
} |
| 338 | 338 |
|
| 339 |
|
|
| 339 |
ClpLp::VarStatus ClpLp::_getColStatus(int i) const {
|
|
| 340 | 340 |
switch (_prob->getColumnStatus(i)) {
|
| 341 | 341 |
case ClpSimplex::basic: |
| 342 | 342 |
return BASIC; |
| 343 | 343 |
case ClpSimplex::isFree: |
| 344 | 344 |
return FREE; |
| 345 | 345 |
case ClpSimplex::atUpperBound: |
| 346 | 346 |
return UPPER; |
| 347 | 347 |
case ClpSimplex::atLowerBound: |
| 348 | 348 |
return LOWER; |
| 349 | 349 |
case ClpSimplex::isFixed: |
| 350 | 350 |
return FIXED; |
| 351 | 351 |
case ClpSimplex::superBasic: |
| 352 | 352 |
return FREE; |
| 353 | 353 |
default: |
| 354 | 354 |
LEMON_ASSERT(false, "Wrong column status"); |
| 355 | 355 |
return VarStatus(); |
| 356 | 356 |
} |
| 357 | 357 |
} |
| 358 | 358 |
|
| 359 |
|
|
| 359 |
ClpLp::VarStatus ClpLp::_getRowStatus(int i) const {
|
|
| 360 | 360 |
switch (_prob->getColumnStatus(i)) {
|
| 361 | 361 |
case ClpSimplex::basic: |
| 362 | 362 |
return BASIC; |
| 363 | 363 |
case ClpSimplex::isFree: |
| 364 | 364 |
return FREE; |
| 365 | 365 |
case ClpSimplex::atUpperBound: |
| 366 | 366 |
return UPPER; |
| 367 | 367 |
case ClpSimplex::atLowerBound: |
| 368 | 368 |
return LOWER; |
| 369 | 369 |
case ClpSimplex::isFixed: |
| 370 | 370 |
return FIXED; |
| 371 | 371 |
case ClpSimplex::superBasic: |
| 372 | 372 |
return FREE; |
| 373 | 373 |
default: |
| 374 | 374 |
LEMON_ASSERT(false, "Wrong row status"); |
| 375 | 375 |
return VarStatus(); |
| 376 | 376 |
} |
| 377 | 377 |
} |
| 378 | 378 |
|
| 379 | 379 |
|
| 380 |
|
|
| 380 |
ClpLp::ProblemType ClpLp::_getPrimalType() const {
|
|
| 381 | 381 |
if (_prob->isProvenOptimal()) {
|
| 382 | 382 |
return OPTIMAL; |
| 383 | 383 |
} else if (_prob->isProvenPrimalInfeasible()) {
|
| 384 | 384 |
return INFEASIBLE; |
| 385 | 385 |
} else if (_prob->isProvenDualInfeasible()) {
|
| 386 | 386 |
return UNBOUNDED; |
| 387 | 387 |
} else {
|
| 388 | 388 |
return UNDEFINED; |
| 389 | 389 |
} |
| 390 | 390 |
} |
| 391 | 391 |
|
| 392 |
|
|
| 392 |
ClpLp::ProblemType ClpLp::_getDualType() const {
|
|
| 393 | 393 |
if (_prob->isProvenOptimal()) {
|
| 394 | 394 |
return OPTIMAL; |
| 395 | 395 |
} else if (_prob->isProvenDualInfeasible()) {
|
| 396 | 396 |
return INFEASIBLE; |
| 397 | 397 |
} else if (_prob->isProvenPrimalInfeasible()) {
|
| 398 | 398 |
return INFEASIBLE; |
| 399 | 399 |
} else {
|
| 400 | 400 |
return UNDEFINED; |
| 401 | 401 |
} |
| 402 | 402 |
} |
| 403 | 403 |
|
| 404 |
void |
|
| 404 |
void ClpLp::_setSense(ClpLp::Sense sense) {
|
|
| 405 | 405 |
switch (sense) {
|
| 406 | 406 |
case MIN: |
| 407 | 407 |
_prob->setOptimizationDirection(1); |
| 408 | 408 |
break; |
| 409 | 409 |
case MAX: |
| 410 | 410 |
_prob->setOptimizationDirection(-1); |
| 411 | 411 |
break; |
| 412 | 412 |
} |
| 413 | 413 |
} |
| 414 | 414 |
|
| 415 |
|
|
| 415 |
ClpLp::Sense ClpLp::_getSense() const {
|
|
| 416 | 416 |
double dir = _prob->optimizationDirection(); |
| 417 | 417 |
if (dir > 0.0) {
|
| 418 | 418 |
return MIN; |
| 419 | 419 |
} else {
|
| 420 | 420 |
return MAX; |
| 421 | 421 |
} |
| 422 | 422 |
} |
| 423 | 423 |
|
| 424 |
void |
|
| 424 |
void ClpLp::_clear() {
|
|
| 425 | 425 |
delete _prob; |
| 426 | 426 |
_prob = new ClpSimplex(); |
| 427 | 427 |
rows.clear(); |
| 428 | 428 |
cols.clear(); |
| 429 | 429 |
_col_names_ref.clear(); |
| 430 | 430 |
_clear_temporals(); |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 |
void |
|
| 433 |
void ClpLp::messageLevel(MessageLevel m) {
|
|
| 434 | 434 |
_prob->setLogLevel(static_cast<int>(m)); |
| 435 | 435 |
} |
| 436 | 436 |
|
| 437 | 437 |
} //END OF NAMESPACE LEMON |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CLP_H |
| 20 | 20 |
#define LEMON_CLP_H |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Header of the LEMON-CLP lp solver interface. |
| 24 | 24 |
|
| 25 | 25 |
#include <vector> |
| 26 | 26 |
#include <string> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/lp_base.h> |
| 29 | 29 |
|
| 30 | 30 |
class ClpSimplex; |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
/// \ingroup lp_group |
| 35 | 35 |
/// |
| 36 | 36 |
/// \brief Interface for the CLP solver |
| 37 | 37 |
/// |
| 38 | 38 |
/// This class implements an interface for the Clp LP solver. The |
| 39 | 39 |
/// Clp library is an object oriented lp solver library developed at |
| 40 | 40 |
/// the IBM. The CLP is part of the COIN-OR package and it can be |
| 41 | 41 |
/// used with Common Public License. |
| 42 |
class |
|
| 42 |
class ClpLp : public LpSolver {
|
|
| 43 | 43 |
protected: |
| 44 | 44 |
|
| 45 | 45 |
ClpSimplex* _prob; |
| 46 | 46 |
|
| 47 | 47 |
std::map<std::string, int> _col_names_ref; |
| 48 | 48 |
std::map<std::string, int> _row_names_ref; |
| 49 | 49 |
|
| 50 | 50 |
public: |
| 51 | 51 |
|
| 52 | 52 |
/// \e |
| 53 |
|
|
| 53 |
ClpLp(); |
|
| 54 | 54 |
/// \e |
| 55 |
|
|
| 55 |
ClpLp(const ClpLp&); |
|
| 56 | 56 |
/// \e |
| 57 |
~ |
|
| 57 |
~ClpLp(); |
|
| 58 | 58 |
|
| 59 | 59 |
protected: |
| 60 | 60 |
|
| 61 | 61 |
mutable double* _primal_ray; |
| 62 | 62 |
mutable double* _dual_ray; |
| 63 | 63 |
|
| 64 | 64 |
void _init_temporals(); |
| 65 | 65 |
void _clear_temporals(); |
| 66 | 66 |
|
| 67 | 67 |
protected: |
| 68 | 68 |
|
| 69 |
virtual LpClp* _newSolver() const; |
|
| 70 |
virtual LpClp* _cloneSolver() const; |
|
| 69 |
virtual ClpLp* _newSolver() const; |
|
| 70 |
virtual ClpLp* _cloneSolver() const; |
|
| 71 | 71 |
|
| 72 | 72 |
virtual const char* _solverName() const; |
| 73 | 73 |
|
| 74 | 74 |
virtual int _addCol(); |
| 75 | 75 |
virtual int _addRow(); |
| 76 | 76 |
|
| 77 | 77 |
virtual void _eraseCol(int i); |
| 78 | 78 |
virtual void _eraseRow(int i); |
| 79 | 79 |
|
| 80 | 80 |
virtual void _eraseColId(int i); |
| 81 | 81 |
virtual void _eraseRowId(int i); |
| 82 | 82 |
|
| 83 | 83 |
virtual void _getColName(int col, std::string& name) const; |
| 84 | 84 |
virtual void _setColName(int col, const std::string& name); |
| 85 | 85 |
virtual int _colByName(const std::string& name) const; |
| 86 | 86 |
|
| 87 | 87 |
virtual void _getRowName(int row, std::string& name) const; |
| 88 | 88 |
virtual void _setRowName(int row, const std::string& name); |
| 89 | 89 |
virtual int _rowByName(const std::string& name) const; |
| 90 | 90 |
|
| 91 | 91 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
| 92 | 92 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
| 93 | 93 |
|
| 94 | 94 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
| 95 | 95 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
| 96 | 96 |
|
| 97 | 97 |
virtual void _setCoeff(int row, int col, Value value); |
| 98 | 98 |
virtual Value _getCoeff(int row, int col) const; |
| 99 | 99 |
|
| 100 | 100 |
virtual void _setColLowerBound(int i, Value value); |
| 101 | 101 |
virtual Value _getColLowerBound(int i) const; |
| 102 | 102 |
virtual void _setColUpperBound(int i, Value value); |
| 103 | 103 |
virtual Value _getColUpperBound(int i) const; |
| 104 | 104 |
|
| 105 | 105 |
virtual void _setRowLowerBound(int i, Value value); |
| 106 | 106 |
virtual Value _getRowLowerBound(int i) const; |
| 107 | 107 |
virtual void _setRowUpperBound(int i, Value value); |
| 108 | 108 |
virtual Value _getRowUpperBound(int i) const; |
| 109 | 109 |
|
| 110 | 110 |
virtual void _setObjCoeffs(ExprIterator, ExprIterator); |
| 111 | 111 |
virtual void _getObjCoeffs(InsertIterator) const; |
| 112 | 112 |
|
| 113 | 113 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 114 | 114 |
virtual Value _getObjCoeff(int i) const; |
| 115 | 115 |
|
| 116 | 116 |
virtual void _setSense(Sense sense); |
| 117 | 117 |
virtual Sense _getSense() const; |
| 118 | 118 |
|
| 119 | 119 |
virtual SolveExitStatus _solve(); |
| 120 | 120 |
|
| 121 | 121 |
virtual Value _getPrimal(int i) const; |
| 122 | 122 |
virtual Value _getDual(int i) const; |
| 123 | 123 |
|
| 124 | 124 |
virtual Value _getPrimalValue() const; |
| 125 | 125 |
|
| 126 | 126 |
virtual Value _getPrimalRay(int i) const; |
| 127 | 127 |
virtual Value _getDualRay(int i) const; |
| 128 | 128 |
|
| 129 | 129 |
virtual VarStatus _getColStatus(int i) const; |
| 130 | 130 |
virtual VarStatus _getRowStatus(int i) const; |
| 131 | 131 |
|
| 132 | 132 |
virtual ProblemType _getPrimalType() const; |
| 133 | 133 |
virtual ProblemType _getDualType() const; |
| 134 | 134 |
| ... | ... |
@@ -377,549 +377,549 @@ |
| 377 | 377 |
values.push_back(it->second); |
| 378 | 378 |
} |
| 379 | 379 |
CPXchgobj(cplexEnv(), _prob, values.size(), |
| 380 | 380 |
&indices.front(), &values.front()); |
| 381 | 381 |
|
| 382 | 382 |
} |
| 383 | 383 |
|
| 384 | 384 |
void CplexBase::_getObjCoeffs(InsertIterator b) const |
| 385 | 385 |
{
|
| 386 | 386 |
int num = CPXgetnumcols(cplexEnv(), _prob); |
| 387 | 387 |
std::vector<Value> x(num); |
| 388 | 388 |
|
| 389 | 389 |
CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1); |
| 390 | 390 |
for (int i = 0; i < num; ++i) {
|
| 391 | 391 |
if (x[i] != 0.0) {
|
| 392 | 392 |
*b = std::make_pair(i, x[i]); |
| 393 | 393 |
++b; |
| 394 | 394 |
} |
| 395 | 395 |
} |
| 396 | 396 |
} |
| 397 | 397 |
|
| 398 | 398 |
void CplexBase::_setObjCoeff(int i, Value obj_coef) |
| 399 | 399 |
{
|
| 400 | 400 |
CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef); |
| 401 | 401 |
} |
| 402 | 402 |
|
| 403 | 403 |
CplexBase::Value CplexBase::_getObjCoeff(int i) const |
| 404 | 404 |
{
|
| 405 | 405 |
Value x; |
| 406 | 406 |
CPXgetobj(cplexEnv(), _prob, &x, i, i); |
| 407 | 407 |
return x; |
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 | 410 |
void CplexBase::_setSense(CplexBase::Sense sense) {
|
| 411 | 411 |
switch (sense) {
|
| 412 | 412 |
case MIN: |
| 413 | 413 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MIN); |
| 414 | 414 |
break; |
| 415 | 415 |
case MAX: |
| 416 | 416 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MAX); |
| 417 | 417 |
break; |
| 418 | 418 |
} |
| 419 | 419 |
} |
| 420 | 420 |
|
| 421 | 421 |
CplexBase::Sense CplexBase::_getSense() const {
|
| 422 | 422 |
switch (CPXgetobjsen(cplexEnv(), _prob)) {
|
| 423 | 423 |
case CPX_MIN: |
| 424 | 424 |
return MIN; |
| 425 | 425 |
case CPX_MAX: |
| 426 | 426 |
return MAX; |
| 427 | 427 |
default: |
| 428 | 428 |
LEMON_ASSERT(false, "Invalid sense"); |
| 429 | 429 |
return CplexBase::Sense(); |
| 430 | 430 |
} |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
void CplexBase::_clear() {
|
| 434 | 434 |
CPXfreeprob(cplexEnv(),&_prob); |
| 435 | 435 |
int status; |
| 436 | 436 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem"); |
| 437 | 437 |
rows.clear(); |
| 438 | 438 |
cols.clear(); |
| 439 | 439 |
} |
| 440 | 440 |
|
| 441 |
// |
|
| 441 |
// CplexLp members |
|
| 442 | 442 |
|
| 443 |
|
|
| 443 |
CplexLp::CplexLp() |
|
| 444 | 444 |
: LpBase(), CplexBase(), LpSolver() {}
|
| 445 | 445 |
|
| 446 |
|
|
| 446 |
CplexLp::CplexLp(const CplexEnv& env) |
|
| 447 | 447 |
: LpBase(), CplexBase(env), LpSolver() {}
|
| 448 | 448 |
|
| 449 |
|
|
| 449 |
CplexLp::CplexLp(const CplexLp& other) |
|
| 450 | 450 |
: LpBase(), CplexBase(other), LpSolver() {}
|
| 451 | 451 |
|
| 452 |
|
|
| 452 |
CplexLp::~CplexLp() {}
|
|
| 453 | 453 |
|
| 454 |
LpCplex* LpCplex::_newSolver() const { return new LpCplex; }
|
|
| 455 |
LpCplex* LpCplex::_cloneSolver() const {return new LpCplex(*this); }
|
|
| 454 |
CplexLp* CplexLp::_newSolver() const { return new CplexLp; }
|
|
| 455 |
CplexLp* CplexLp::_cloneSolver() const {return new CplexLp(*this); }
|
|
| 456 | 456 |
|
| 457 |
const char* |
|
| 457 |
const char* CplexLp::_solverName() const { return "CplexLp"; }
|
|
| 458 | 458 |
|
| 459 |
void |
|
| 459 |
void CplexLp::_clear_temporals() {
|
|
| 460 | 460 |
_col_status.clear(); |
| 461 | 461 |
_row_status.clear(); |
| 462 | 462 |
_primal_ray.clear(); |
| 463 | 463 |
_dual_ray.clear(); |
| 464 | 464 |
} |
| 465 | 465 |
|
| 466 | 466 |
// The routine returns zero unless an error occurred during the |
| 467 | 467 |
// optimization. Examples of errors include exhausting available |
| 468 | 468 |
// memory (CPXERR_NO_MEMORY) or encountering invalid data in the |
| 469 | 469 |
// CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a |
| 470 | 470 |
// user-specified CPLEX limit, or proving the model infeasible or |
| 471 | 471 |
// unbounded, are not considered errors. Note that a zero return |
| 472 | 472 |
// value does not necessarily mean that a solution exists. Use query |
| 473 | 473 |
// routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain |
| 474 | 474 |
// further information about the status of the optimization. |
| 475 |
|
|
| 475 |
CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
|
|
| 476 | 476 |
#if CPX_VERSION >= 800 |
| 477 | 477 |
if (status == 0) {
|
| 478 | 478 |
switch (CPXgetstat(cplexEnv(), _prob)) {
|
| 479 | 479 |
case CPX_STAT_OPTIMAL: |
| 480 | 480 |
case CPX_STAT_INFEASIBLE: |
| 481 | 481 |
case CPX_STAT_UNBOUNDED: |
| 482 | 482 |
return SOLVED; |
| 483 | 483 |
default: |
| 484 | 484 |
return UNSOLVED; |
| 485 | 485 |
} |
| 486 | 486 |
} else {
|
| 487 | 487 |
return UNSOLVED; |
| 488 | 488 |
} |
| 489 | 489 |
#else |
| 490 | 490 |
if (status == 0) {
|
| 491 | 491 |
//We want to exclude some cases |
| 492 | 492 |
switch (CPXgetstat(cplexEnv(), _prob)) {
|
| 493 | 493 |
case CPX_OBJ_LIM: |
| 494 | 494 |
case CPX_IT_LIM_FEAS: |
| 495 | 495 |
case CPX_IT_LIM_INFEAS: |
| 496 | 496 |
case CPX_TIME_LIM_FEAS: |
| 497 | 497 |
case CPX_TIME_LIM_INFEAS: |
| 498 | 498 |
return UNSOLVED; |
| 499 | 499 |
default: |
| 500 | 500 |
return SOLVED; |
| 501 | 501 |
} |
| 502 | 502 |
} else {
|
| 503 | 503 |
return UNSOLVED; |
| 504 | 504 |
} |
| 505 | 505 |
#endif |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 |
|
|
| 508 |
CplexLp::SolveExitStatus CplexLp::_solve() {
|
|
| 509 | 509 |
_clear_temporals(); |
| 510 | 510 |
return convertStatus(CPXlpopt(cplexEnv(), _prob)); |
| 511 | 511 |
} |
| 512 | 512 |
|
| 513 |
|
|
| 513 |
CplexLp::SolveExitStatus CplexLp::solvePrimal() {
|
|
| 514 | 514 |
_clear_temporals(); |
| 515 | 515 |
return convertStatus(CPXprimopt(cplexEnv(), _prob)); |
| 516 | 516 |
} |
| 517 | 517 |
|
| 518 |
|
|
| 518 |
CplexLp::SolveExitStatus CplexLp::solveDual() {
|
|
| 519 | 519 |
_clear_temporals(); |
| 520 | 520 |
return convertStatus(CPXdualopt(cplexEnv(), _prob)); |
| 521 | 521 |
} |
| 522 | 522 |
|
| 523 |
|
|
| 523 |
CplexLp::SolveExitStatus CplexLp::solveBarrier() {
|
|
| 524 | 524 |
_clear_temporals(); |
| 525 | 525 |
return convertStatus(CPXbaropt(cplexEnv(), _prob)); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 |
|
|
| 528 |
CplexLp::Value CplexLp::_getPrimal(int i) const {
|
|
| 529 | 529 |
Value x; |
| 530 | 530 |
CPXgetx(cplexEnv(), _prob, &x, i, i); |
| 531 | 531 |
return x; |
| 532 | 532 |
} |
| 533 | 533 |
|
| 534 |
|
|
| 534 |
CplexLp::Value CplexLp::_getDual(int i) const {
|
|
| 535 | 535 |
Value y; |
| 536 | 536 |
CPXgetpi(cplexEnv(), _prob, &y, i, i); |
| 537 | 537 |
return y; |
| 538 | 538 |
} |
| 539 | 539 |
|
| 540 |
|
|
| 540 |
CplexLp::Value CplexLp::_getPrimalValue() const {
|
|
| 541 | 541 |
Value objval; |
| 542 | 542 |
CPXgetobjval(cplexEnv(), _prob, &objval); |
| 543 | 543 |
return objval; |
| 544 | 544 |
} |
| 545 | 545 |
|
| 546 |
|
|
| 546 |
CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
|
|
| 547 | 547 |
if (_col_status.empty()) {
|
| 548 | 548 |
_col_status.resize(CPXgetnumcols(cplexEnv(), _prob)); |
| 549 | 549 |
CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0); |
| 550 | 550 |
} |
| 551 | 551 |
switch (_col_status[i]) {
|
| 552 | 552 |
case CPX_BASIC: |
| 553 | 553 |
return BASIC; |
| 554 | 554 |
case CPX_FREE_SUPER: |
| 555 | 555 |
return FREE; |
| 556 | 556 |
case CPX_AT_LOWER: |
| 557 | 557 |
return LOWER; |
| 558 | 558 |
case CPX_AT_UPPER: |
| 559 | 559 |
return UPPER; |
| 560 | 560 |
default: |
| 561 | 561 |
LEMON_ASSERT(false, "Wrong column status"); |
| 562 |
return |
|
| 562 |
return CplexLp::VarStatus(); |
|
| 563 | 563 |
} |
| 564 | 564 |
} |
| 565 | 565 |
|
| 566 |
|
|
| 566 |
CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
|
|
| 567 | 567 |
if (_row_status.empty()) {
|
| 568 | 568 |
_row_status.resize(CPXgetnumrows(cplexEnv(), _prob)); |
| 569 | 569 |
CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front()); |
| 570 | 570 |
} |
| 571 | 571 |
switch (_row_status[i]) {
|
| 572 | 572 |
case CPX_BASIC: |
| 573 | 573 |
return BASIC; |
| 574 | 574 |
case CPX_AT_LOWER: |
| 575 | 575 |
{
|
| 576 | 576 |
char s; |
| 577 | 577 |
CPXgetsense(cplexEnv(), _prob, &s, i, i); |
| 578 | 578 |
return s != 'L' ? LOWER : UPPER; |
| 579 | 579 |
} |
| 580 | 580 |
case CPX_AT_UPPER: |
| 581 | 581 |
return UPPER; |
| 582 | 582 |
default: |
| 583 | 583 |
LEMON_ASSERT(false, "Wrong row status"); |
| 584 |
return |
|
| 584 |
return CplexLp::VarStatus(); |
|
| 585 | 585 |
} |
| 586 | 586 |
} |
| 587 | 587 |
|
| 588 |
|
|
| 588 |
CplexLp::Value CplexLp::_getPrimalRay(int i) const {
|
|
| 589 | 589 |
if (_primal_ray.empty()) {
|
| 590 | 590 |
_primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob)); |
| 591 | 591 |
CPXgetray(cplexEnv(), _prob, &_primal_ray.front()); |
| 592 | 592 |
} |
| 593 | 593 |
return _primal_ray[i]; |
| 594 | 594 |
} |
| 595 | 595 |
|
| 596 |
|
|
| 596 |
CplexLp::Value CplexLp::_getDualRay(int i) const {
|
|
| 597 | 597 |
if (_dual_ray.empty()) {
|
| 598 | 598 |
|
| 599 | 599 |
} |
| 600 | 600 |
return _dual_ray[i]; |
| 601 | 601 |
} |
| 602 | 602 |
|
| 603 | 603 |
//7.5-os cplex statusai (Vigyazat: a 9.0-asei masok!) |
| 604 | 604 |
// This table lists the statuses, returned by the CPXgetstat() |
| 605 | 605 |
// routine, for solutions to LP problems or mixed integer problems. If |
| 606 | 606 |
// no solution exists, the return value is zero. |
| 607 | 607 |
|
| 608 | 608 |
// For Simplex, Barrier |
| 609 | 609 |
// 1 CPX_OPTIMAL |
| 610 | 610 |
// Optimal solution found |
| 611 | 611 |
// 2 CPX_INFEASIBLE |
| 612 | 612 |
// Problem infeasible |
| 613 | 613 |
// 3 CPX_UNBOUNDED |
| 614 | 614 |
// Problem unbounded |
| 615 | 615 |
// 4 CPX_OBJ_LIM |
| 616 | 616 |
// Objective limit exceeded in Phase II |
| 617 | 617 |
// 5 CPX_IT_LIM_FEAS |
| 618 | 618 |
// Iteration limit exceeded in Phase II |
| 619 | 619 |
// 6 CPX_IT_LIM_INFEAS |
| 620 | 620 |
// Iteration limit exceeded in Phase I |
| 621 | 621 |
// 7 CPX_TIME_LIM_FEAS |
| 622 | 622 |
// Time limit exceeded in Phase II |
| 623 | 623 |
// 8 CPX_TIME_LIM_INFEAS |
| 624 | 624 |
// Time limit exceeded in Phase I |
| 625 | 625 |
// 9 CPX_NUM_BEST_FEAS |
| 626 | 626 |
// Problem non-optimal, singularities in Phase II |
| 627 | 627 |
// 10 CPX_NUM_BEST_INFEAS |
| 628 | 628 |
// Problem non-optimal, singularities in Phase I |
| 629 | 629 |
// 11 CPX_OPTIMAL_INFEAS |
| 630 | 630 |
// Optimal solution found, unscaled infeasibilities |
| 631 | 631 |
// 12 CPX_ABORT_FEAS |
| 632 | 632 |
// Aborted in Phase II |
| 633 | 633 |
// 13 CPX_ABORT_INFEAS |
| 634 | 634 |
// Aborted in Phase I |
| 635 | 635 |
// 14 CPX_ABORT_DUAL_INFEAS |
| 636 | 636 |
// Aborted in barrier, dual infeasible |
| 637 | 637 |
// 15 CPX_ABORT_PRIM_INFEAS |
| 638 | 638 |
// Aborted in barrier, primal infeasible |
| 639 | 639 |
// 16 CPX_ABORT_PRIM_DUAL_INFEAS |
| 640 | 640 |
// Aborted in barrier, primal and dual infeasible |
| 641 | 641 |
// 17 CPX_ABORT_PRIM_DUAL_FEAS |
| 642 | 642 |
// Aborted in barrier, primal and dual feasible |
| 643 | 643 |
// 18 CPX_ABORT_CROSSOVER |
| 644 | 644 |
// Aborted in crossover |
| 645 | 645 |
// 19 CPX_INForUNBD |
| 646 | 646 |
// Infeasible or unbounded |
| 647 | 647 |
// 20 CPX_PIVOT |
| 648 | 648 |
// User pivot used |
| 649 | 649 |
// |
| 650 | 650 |
// Ezeket hova tegyem: |
| 651 | 651 |
// ??case CPX_ABORT_DUAL_INFEAS |
| 652 | 652 |
// ??case CPX_ABORT_CROSSOVER |
| 653 | 653 |
// ??case CPX_INForUNBD |
| 654 | 654 |
// ??case CPX_PIVOT |
| 655 | 655 |
|
| 656 | 656 |
//Some more interesting stuff: |
| 657 | 657 |
|
| 658 | 658 |
// CPX_PARAM_PROBMETHOD 1062 int LPMETHOD |
| 659 | 659 |
// 0 Automatic |
| 660 | 660 |
// 1 Primal Simplex |
| 661 | 661 |
// 2 Dual Simplex |
| 662 | 662 |
// 3 Network Simplex |
| 663 | 663 |
// 4 Standard Barrier |
| 664 | 664 |
// Default: 0 |
| 665 | 665 |
// Description: Method for linear optimization. |
| 666 | 666 |
// Determines which algorithm is used when CPXlpopt() (or "optimize" |
| 667 | 667 |
// in the Interactive Optimizer) is called. Currently the behavior of |
| 668 | 668 |
// the "Automatic" setting is that CPLEX simply invokes the dual |
| 669 | 669 |
// simplex method, but this capability may be expanded in the future |
| 670 | 670 |
// so that CPLEX chooses the method based on problem characteristics |
| 671 | 671 |
#if CPX_VERSION < 900 |
| 672 | 672 |
void statusSwitch(CPXENVptr cplexEnv(),int& stat){
|
| 673 | 673 |
int lpmethod; |
| 674 | 674 |
CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod); |
| 675 | 675 |
if (lpmethod==2){
|
| 676 | 676 |
if (stat==CPX_UNBOUNDED){
|
| 677 | 677 |
stat=CPX_INFEASIBLE; |
| 678 | 678 |
} |
| 679 | 679 |
else{
|
| 680 | 680 |
if (stat==CPX_INFEASIBLE) |
| 681 | 681 |
stat=CPX_UNBOUNDED; |
| 682 | 682 |
} |
| 683 | 683 |
} |
| 684 | 684 |
} |
| 685 | 685 |
#else |
| 686 | 686 |
void statusSwitch(CPXENVptr,int&){}
|
| 687 | 687 |
#endif |
| 688 | 688 |
|
| 689 |
|
|
| 689 |
CplexLp::ProblemType CplexLp::_getPrimalType() const {
|
|
| 690 | 690 |
// Unboundedness not treated well: the following is from cplex 9.0 doc |
| 691 | 691 |
// About Unboundedness |
| 692 | 692 |
|
| 693 | 693 |
// The treatment of models that are unbounded involves a few |
| 694 | 694 |
// subtleties. Specifically, a declaration of unboundedness means that |
| 695 | 695 |
// ILOG CPLEX has determined that the model has an unbounded |
| 696 | 696 |
// ray. Given any feasible solution x with objective z, a multiple of |
| 697 | 697 |
// the unbounded ray can be added to x to give a feasible solution |
| 698 | 698 |
// with objective z-1 (or z+1 for maximization models). Thus, if a |
| 699 | 699 |
// feasible solution exists, then the optimal objective is |
| 700 | 700 |
// unbounded. Note that ILOG CPLEX has not necessarily concluded that |
| 701 | 701 |
// a feasible solution exists. Users can call the routine CPXsolninfo |
| 702 | 702 |
// to determine whether ILOG CPLEX has also concluded that the model |
| 703 | 703 |
// has a feasible solution. |
| 704 | 704 |
|
| 705 | 705 |
int stat = CPXgetstat(cplexEnv(), _prob); |
| 706 | 706 |
#if CPX_VERSION >= 800 |
| 707 | 707 |
switch (stat) |
| 708 | 708 |
{
|
| 709 | 709 |
case CPX_STAT_OPTIMAL: |
| 710 | 710 |
return OPTIMAL; |
| 711 | 711 |
case CPX_STAT_UNBOUNDED: |
| 712 | 712 |
return UNBOUNDED; |
| 713 | 713 |
case CPX_STAT_INFEASIBLE: |
| 714 | 714 |
return INFEASIBLE; |
| 715 | 715 |
default: |
| 716 | 716 |
return UNDEFINED; |
| 717 | 717 |
} |
| 718 | 718 |
#else |
| 719 | 719 |
statusSwitch(cplexEnv(),stat); |
| 720 | 720 |
//CPXgetstat(cplexEnv(), _prob); |
| 721 | 721 |
//printf("A primal status: %d, CPX_OPTIMAL=%d \n",stat,CPX_OPTIMAL);
|
| 722 | 722 |
switch (stat) {
|
| 723 | 723 |
case 0: |
| 724 | 724 |
return UNDEFINED; //Undefined |
| 725 | 725 |
case CPX_OPTIMAL://Optimal |
| 726 | 726 |
return OPTIMAL; |
| 727 | 727 |
case CPX_UNBOUNDED://Unbounded |
| 728 | 728 |
return INFEASIBLE;//In case of dual simplex |
| 729 | 729 |
//return UNBOUNDED; |
| 730 | 730 |
case CPX_INFEASIBLE://Infeasible |
| 731 | 731 |
// case CPX_IT_LIM_INFEAS: |
| 732 | 732 |
// case CPX_TIME_LIM_INFEAS: |
| 733 | 733 |
// case CPX_NUM_BEST_INFEAS: |
| 734 | 734 |
// case CPX_OPTIMAL_INFEAS: |
| 735 | 735 |
// case CPX_ABORT_INFEAS: |
| 736 | 736 |
// case CPX_ABORT_PRIM_INFEAS: |
| 737 | 737 |
// case CPX_ABORT_PRIM_DUAL_INFEAS: |
| 738 | 738 |
return UNBOUNDED;//In case of dual simplex |
| 739 | 739 |
//return INFEASIBLE; |
| 740 | 740 |
// case CPX_OBJ_LIM: |
| 741 | 741 |
// case CPX_IT_LIM_FEAS: |
| 742 | 742 |
// case CPX_TIME_LIM_FEAS: |
| 743 | 743 |
// case CPX_NUM_BEST_FEAS: |
| 744 | 744 |
// case CPX_ABORT_FEAS: |
| 745 | 745 |
// case CPX_ABORT_PRIM_DUAL_FEAS: |
| 746 | 746 |
// return FEASIBLE; |
| 747 | 747 |
default: |
| 748 | 748 |
return UNDEFINED; //Everything else comes here |
| 749 | 749 |
//FIXME error |
| 750 | 750 |
} |
| 751 | 751 |
#endif |
| 752 | 752 |
} |
| 753 | 753 |
|
| 754 | 754 |
//9.0-as cplex verzio statusai |
| 755 | 755 |
// CPX_STAT_ABORT_DUAL_OBJ_LIM |
| 756 | 756 |
// CPX_STAT_ABORT_IT_LIM |
| 757 | 757 |
// CPX_STAT_ABORT_OBJ_LIM |
| 758 | 758 |
// CPX_STAT_ABORT_PRIM_OBJ_LIM |
| 759 | 759 |
// CPX_STAT_ABORT_TIME_LIM |
| 760 | 760 |
// CPX_STAT_ABORT_USER |
| 761 | 761 |
// CPX_STAT_FEASIBLE_RELAXED |
| 762 | 762 |
// CPX_STAT_INFEASIBLE |
| 763 | 763 |
// CPX_STAT_INForUNBD |
| 764 | 764 |
// CPX_STAT_NUM_BEST |
| 765 | 765 |
// CPX_STAT_OPTIMAL |
| 766 | 766 |
// CPX_STAT_OPTIMAL_FACE_UNBOUNDED |
| 767 | 767 |
// CPX_STAT_OPTIMAL_INFEAS |
| 768 | 768 |
// CPX_STAT_OPTIMAL_RELAXED |
| 769 | 769 |
// CPX_STAT_UNBOUNDED |
| 770 | 770 |
|
| 771 |
|
|
| 771 |
CplexLp::ProblemType CplexLp::_getDualType() const {
|
|
| 772 | 772 |
int stat = CPXgetstat(cplexEnv(), _prob); |
| 773 | 773 |
#if CPX_VERSION >= 800 |
| 774 | 774 |
switch (stat) {
|
| 775 | 775 |
case CPX_STAT_OPTIMAL: |
| 776 | 776 |
return OPTIMAL; |
| 777 | 777 |
case CPX_STAT_UNBOUNDED: |
| 778 | 778 |
return INFEASIBLE; |
| 779 | 779 |
default: |
| 780 | 780 |
return UNDEFINED; |
| 781 | 781 |
} |
| 782 | 782 |
#else |
| 783 | 783 |
statusSwitch(cplexEnv(),stat); |
| 784 | 784 |
switch (stat) {
|
| 785 | 785 |
case 0: |
| 786 | 786 |
return UNDEFINED; //Undefined |
| 787 | 787 |
case CPX_OPTIMAL://Optimal |
| 788 | 788 |
return OPTIMAL; |
| 789 | 789 |
case CPX_UNBOUNDED: |
| 790 | 790 |
return INFEASIBLE; |
| 791 | 791 |
default: |
| 792 | 792 |
return UNDEFINED; //Everything else comes here |
| 793 | 793 |
//FIXME error |
| 794 | 794 |
} |
| 795 | 795 |
#endif |
| 796 | 796 |
} |
| 797 | 797 |
|
| 798 |
// |
|
| 798 |
// CplexMip members |
|
| 799 | 799 |
|
| 800 |
|
|
| 800 |
CplexMip::CplexMip() |
|
| 801 | 801 |
: LpBase(), CplexBase(), MipSolver() {
|
| 802 | 802 |
|
| 803 | 803 |
#if CPX_VERSION < 800 |
| 804 | 804 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP); |
| 805 | 805 |
#else |
| 806 | 806 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP); |
| 807 | 807 |
#endif |
| 808 | 808 |
} |
| 809 | 809 |
|
| 810 |
|
|
| 810 |
CplexMip::CplexMip(const CplexEnv& env) |
|
| 811 | 811 |
: LpBase(), CplexBase(env), MipSolver() {
|
| 812 | 812 |
|
| 813 | 813 |
#if CPX_VERSION < 800 |
| 814 | 814 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP); |
| 815 | 815 |
#else |
| 816 | 816 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP); |
| 817 | 817 |
#endif |
| 818 | 818 |
|
| 819 | 819 |
} |
| 820 | 820 |
|
| 821 |
|
|
| 821 |
CplexMip::CplexMip(const CplexMip& other) |
|
| 822 | 822 |
: LpBase(), CplexBase(other), MipSolver() {}
|
| 823 | 823 |
|
| 824 |
|
|
| 824 |
CplexMip::~CplexMip() {}
|
|
| 825 | 825 |
|
| 826 |
MipCplex* MipCplex::_newSolver() const { return new MipCplex; }
|
|
| 827 |
MipCplex* MipCplex::_cloneSolver() const {return new MipCplex(*this); }
|
|
| 826 |
CplexMip* CplexMip::_newSolver() const { return new CplexMip; }
|
|
| 827 |
CplexMip* CplexMip::_cloneSolver() const {return new CplexMip(*this); }
|
|
| 828 | 828 |
|
| 829 |
const char* |
|
| 829 |
const char* CplexMip::_solverName() const { return "CplexMip"; }
|
|
| 830 | 830 |
|
| 831 |
void |
|
| 831 |
void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
|
|
| 832 | 832 |
|
| 833 | 833 |
// Note If a variable is to be changed to binary, a call to CPXchgbds |
| 834 | 834 |
// should also be made to change the bounds to 0 and 1. |
| 835 | 835 |
|
| 836 | 836 |
switch (col_type){
|
| 837 | 837 |
case INTEGER: {
|
| 838 | 838 |
const char t = 'I'; |
| 839 | 839 |
CPXchgctype (cplexEnv(), _prob, 1, &i, &t); |
| 840 | 840 |
} break; |
| 841 | 841 |
case REAL: {
|
| 842 | 842 |
const char t = 'C'; |
| 843 | 843 |
CPXchgctype (cplexEnv(), _prob, 1, &i, &t); |
| 844 | 844 |
} break; |
| 845 | 845 |
default: |
| 846 | 846 |
break; |
| 847 | 847 |
} |
| 848 | 848 |
} |
| 849 | 849 |
|
| 850 |
|
|
| 850 |
CplexMip::ColTypes CplexMip::_getColType(int i) const {
|
|
| 851 | 851 |
char t; |
| 852 | 852 |
CPXgetctype (cplexEnv(), _prob, &t, i, i); |
| 853 | 853 |
switch (t) {
|
| 854 | 854 |
case 'I': |
| 855 | 855 |
return INTEGER; |
| 856 | 856 |
case 'C': |
| 857 | 857 |
return REAL; |
| 858 | 858 |
default: |
| 859 | 859 |
LEMON_ASSERT(false, "Invalid column type"); |
| 860 | 860 |
return ColTypes(); |
| 861 | 861 |
} |
| 862 | 862 |
|
| 863 | 863 |
} |
| 864 | 864 |
|
| 865 |
|
|
| 865 |
CplexMip::SolveExitStatus CplexMip::_solve() {
|
|
| 866 | 866 |
int status; |
| 867 | 867 |
status = CPXmipopt (cplexEnv(), _prob); |
| 868 | 868 |
if (status==0) |
| 869 | 869 |
return SOLVED; |
| 870 | 870 |
else |
| 871 | 871 |
return UNSOLVED; |
| 872 | 872 |
|
| 873 | 873 |
} |
| 874 | 874 |
|
| 875 | 875 |
|
| 876 |
|
|
| 876 |
CplexMip::ProblemType CplexMip::_getType() const {
|
|
| 877 | 877 |
|
| 878 | 878 |
int stat = CPXgetstat(cplexEnv(), _prob); |
| 879 | 879 |
|
| 880 | 880 |
//Fortunately, MIP statuses did not change for cplex 8.0 |
| 881 | 881 |
switch (stat) {
|
| 882 | 882 |
case CPXMIP_OPTIMAL: |
| 883 | 883 |
// Optimal integer solution has been found. |
| 884 | 884 |
case CPXMIP_OPTIMAL_TOL: |
| 885 | 885 |
// Optimal soluton with the tolerance defined by epgap or epagap has |
| 886 | 886 |
// been found. |
| 887 | 887 |
return OPTIMAL; |
| 888 | 888 |
//This also exists in later issues |
| 889 | 889 |
// case CPXMIP_UNBOUNDED: |
| 890 | 890 |
//return UNBOUNDED; |
| 891 | 891 |
case CPXMIP_INFEASIBLE: |
| 892 | 892 |
return INFEASIBLE; |
| 893 | 893 |
default: |
| 894 | 894 |
return UNDEFINED; |
| 895 | 895 |
} |
| 896 | 896 |
//Unboundedness not treated well: the following is from cplex 9.0 doc |
| 897 | 897 |
// About Unboundedness |
| 898 | 898 |
|
| 899 | 899 |
// The treatment of models that are unbounded involves a few |
| 900 | 900 |
// subtleties. Specifically, a declaration of unboundedness means that |
| 901 | 901 |
// ILOG CPLEX has determined that the model has an unbounded |
| 902 | 902 |
// ray. Given any feasible solution x with objective z, a multiple of |
| 903 | 903 |
// the unbounded ray can be added to x to give a feasible solution |
| 904 | 904 |
// with objective z-1 (or z+1 for maximization models). Thus, if a |
| 905 | 905 |
// feasible solution exists, then the optimal objective is |
| 906 | 906 |
// unbounded. Note that ILOG CPLEX has not necessarily concluded that |
| 907 | 907 |
// a feasible solution exists. Users can call the routine CPXsolninfo |
| 908 | 908 |
// to determine whether ILOG CPLEX has also concluded that the model |
| 909 | 909 |
// has a feasible solution. |
| 910 | 910 |
} |
| 911 | 911 |
|
| 912 |
|
|
| 912 |
CplexMip::Value CplexMip::_getSol(int i) const {
|
|
| 913 | 913 |
Value x; |
| 914 | 914 |
CPXgetmipx(cplexEnv(), _prob, &x, i, i); |
| 915 | 915 |
return x; |
| 916 | 916 |
} |
| 917 | 917 |
|
| 918 |
|
|
| 918 |
CplexMip::Value CplexMip::_getSolValue() const {
|
|
| 919 | 919 |
Value objval; |
| 920 | 920 |
CPXgetmipobjval(cplexEnv(), _prob, &objval); |
| 921 | 921 |
return objval; |
| 922 | 922 |
} |
| 923 | 923 |
|
| 924 | 924 |
} //namespace lemon |
| 925 | 925 |
| ... | ... |
@@ -99,158 +99,158 @@ |
| 99 | 99 |
|
| 100 | 100 |
virtual void _eraseColId(int i); |
| 101 | 101 |
virtual void _eraseRowId(int i); |
| 102 | 102 |
|
| 103 | 103 |
virtual void _getColName(int col, std::string& name) const; |
| 104 | 104 |
virtual void _setColName(int col, const std::string& name); |
| 105 | 105 |
virtual int _colByName(const std::string& name) const; |
| 106 | 106 |
|
| 107 | 107 |
virtual void _getRowName(int row, std::string& name) const; |
| 108 | 108 |
virtual void _setRowName(int row, const std::string& name); |
| 109 | 109 |
virtual int _rowByName(const std::string& name) const; |
| 110 | 110 |
|
| 111 | 111 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
| 112 | 112 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
| 113 | 113 |
|
| 114 | 114 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
| 115 | 115 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
| 116 | 116 |
|
| 117 | 117 |
virtual void _setCoeff(int row, int col, Value value); |
| 118 | 118 |
virtual Value _getCoeff(int row, int col) const; |
| 119 | 119 |
|
| 120 | 120 |
virtual void _setColLowerBound(int i, Value value); |
| 121 | 121 |
virtual Value _getColLowerBound(int i) const; |
| 122 | 122 |
|
| 123 | 123 |
virtual void _setColUpperBound(int i, Value value); |
| 124 | 124 |
virtual Value _getColUpperBound(int i) const; |
| 125 | 125 |
|
| 126 | 126 |
private: |
| 127 | 127 |
void _set_row_bounds(int i, Value lb, Value ub); |
| 128 | 128 |
protected: |
| 129 | 129 |
|
| 130 | 130 |
virtual void _setRowLowerBound(int i, Value value); |
| 131 | 131 |
virtual Value _getRowLowerBound(int i) const; |
| 132 | 132 |
|
| 133 | 133 |
virtual void _setRowUpperBound(int i, Value value); |
| 134 | 134 |
virtual Value _getRowUpperBound(int i) const; |
| 135 | 135 |
|
| 136 | 136 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
| 137 | 137 |
virtual void _getObjCoeffs(InsertIterator b) const; |
| 138 | 138 |
|
| 139 | 139 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 140 | 140 |
virtual Value _getObjCoeff(int i) const; |
| 141 | 141 |
|
| 142 | 142 |
virtual void _setSense(Sense sense); |
| 143 | 143 |
virtual Sense _getSense() const; |
| 144 | 144 |
|
| 145 | 145 |
virtual void _clear(); |
| 146 | 146 |
|
| 147 | 147 |
public: |
| 148 | 148 |
|
| 149 | 149 |
/// Returns the used \c CplexEnv instance |
| 150 | 150 |
const CplexEnv& env() const { return _env; }
|
| 151 | 151 |
/// |
| 152 | 152 |
const cpxenv* cplexEnv() const { return _env.cplexEnv(); }
|
| 153 | 153 |
|
| 154 | 154 |
cpxlp* cplexLp() { return _prob; }
|
| 155 | 155 |
const cpxlp* cplexLp() const { return _prob; }
|
| 156 | 156 |
|
| 157 | 157 |
}; |
| 158 | 158 |
|
| 159 | 159 |
/// \brief Interface for the CPLEX LP solver |
| 160 | 160 |
/// |
| 161 | 161 |
/// This class implements an interface for the CPLEX LP solver. |
| 162 | 162 |
///\ingroup lp_group |
| 163 |
class |
|
| 163 |
class CplexLp : public CplexBase, public LpSolver {
|
|
| 164 | 164 |
public: |
| 165 | 165 |
/// \e |
| 166 |
|
|
| 166 |
CplexLp(); |
|
| 167 | 167 |
/// \e |
| 168 |
|
|
| 168 |
CplexLp(const CplexEnv&); |
|
| 169 | 169 |
/// \e |
| 170 |
|
|
| 170 |
CplexLp(const CplexLp&); |
|
| 171 | 171 |
/// \e |
| 172 |
virtual ~ |
|
| 172 |
virtual ~CplexLp(); |
|
| 173 | 173 |
|
| 174 | 174 |
private: |
| 175 | 175 |
|
| 176 | 176 |
// these values cannot retrieved element by element |
| 177 | 177 |
mutable std::vector<int> _col_status; |
| 178 | 178 |
mutable std::vector<int> _row_status; |
| 179 | 179 |
|
| 180 | 180 |
mutable std::vector<Value> _primal_ray; |
| 181 | 181 |
mutable std::vector<Value> _dual_ray; |
| 182 | 182 |
|
| 183 | 183 |
void _clear_temporals(); |
| 184 | 184 |
|
| 185 | 185 |
SolveExitStatus convertStatus(int status); |
| 186 | 186 |
|
| 187 | 187 |
protected: |
| 188 | 188 |
|
| 189 |
virtual LpCplex* _cloneSolver() const; |
|
| 190 |
virtual LpCplex* _newSolver() const; |
|
| 189 |
virtual CplexLp* _cloneSolver() const; |
|
| 190 |
virtual CplexLp* _newSolver() const; |
|
| 191 | 191 |
|
| 192 | 192 |
virtual const char* _solverName() const; |
| 193 | 193 |
|
| 194 | 194 |
virtual SolveExitStatus _solve(); |
| 195 | 195 |
virtual Value _getPrimal(int i) const; |
| 196 | 196 |
virtual Value _getDual(int i) const; |
| 197 | 197 |
virtual Value _getPrimalValue() const; |
| 198 | 198 |
|
| 199 | 199 |
virtual VarStatus _getColStatus(int i) const; |
| 200 | 200 |
virtual VarStatus _getRowStatus(int i) const; |
| 201 | 201 |
|
| 202 | 202 |
virtual Value _getPrimalRay(int i) const; |
| 203 | 203 |
virtual Value _getDualRay(int i) const; |
| 204 | 204 |
|
| 205 | 205 |
virtual ProblemType _getPrimalType() const; |
| 206 | 206 |
virtual ProblemType _getDualType() const; |
| 207 | 207 |
|
| 208 | 208 |
public: |
| 209 | 209 |
|
| 210 | 210 |
/// Solve with primal simplex method |
| 211 | 211 |
SolveExitStatus solvePrimal(); |
| 212 | 212 |
|
| 213 | 213 |
/// Solve with dual simplex method |
| 214 | 214 |
SolveExitStatus solveDual(); |
| 215 | 215 |
|
| 216 | 216 |
/// Solve with barrier method |
| 217 | 217 |
SolveExitStatus solveBarrier(); |
| 218 | 218 |
|
| 219 | 219 |
}; |
| 220 | 220 |
|
| 221 | 221 |
/// \brief Interface for the CPLEX MIP solver |
| 222 | 222 |
/// |
| 223 | 223 |
/// This class implements an interface for the CPLEX MIP solver. |
| 224 | 224 |
///\ingroup lp_group |
| 225 |
class |
|
| 225 |
class CplexMip : public CplexBase, public MipSolver {
|
|
| 226 | 226 |
public: |
| 227 | 227 |
/// \e |
| 228 |
|
|
| 228 |
CplexMip(); |
|
| 229 | 229 |
/// \e |
| 230 |
|
|
| 230 |
CplexMip(const CplexEnv&); |
|
| 231 | 231 |
/// \e |
| 232 |
|
|
| 232 |
CplexMip(const CplexMip&); |
|
| 233 | 233 |
/// \e |
| 234 |
virtual ~ |
|
| 234 |
virtual ~CplexMip(); |
|
| 235 | 235 |
|
| 236 | 236 |
protected: |
| 237 | 237 |
|
| 238 |
virtual MipCplex* _cloneSolver() const; |
|
| 239 |
virtual MipCplex* _newSolver() const; |
|
| 238 |
virtual CplexMip* _cloneSolver() const; |
|
| 239 |
virtual CplexMip* _newSolver() const; |
|
| 240 | 240 |
|
| 241 | 241 |
virtual const char* _solverName() const; |
| 242 | 242 |
|
| 243 | 243 |
virtual ColTypes _getColType(int col) const; |
| 244 | 244 |
virtual void _setColType(int col, ColTypes col_type); |
| 245 | 245 |
|
| 246 | 246 |
virtual SolveExitStatus _solve(); |
| 247 | 247 |
virtual ProblemType _getType() const; |
| 248 | 248 |
virtual Value _getSol(int i) const; |
| 249 | 249 |
virtual Value _getSolValue() const; |
| 250 | 250 |
|
| 251 | 251 |
}; |
| 252 | 252 |
|
| 253 | 253 |
} //END OF NAMESPACE LEMON |
| 254 | 254 |
|
| 255 | 255 |
#endif //LEMON_CPLEX_H |
| 256 | 256 |
| ... | ... |
@@ -461,492 +461,492 @@ |
| 461 | 461 |
return glp_get_row_ub(lp, i); |
| 462 | 462 |
default: |
| 463 | 463 |
return INF; |
| 464 | 464 |
} |
| 465 | 465 |
} |
| 466 | 466 |
|
| 467 | 467 |
void GlpkBase::_setObjCoeffs(ExprIterator b, ExprIterator e) {
|
| 468 | 468 |
for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
|
| 469 | 469 |
glp_set_obj_coef(lp, i, 0.0); |
| 470 | 470 |
} |
| 471 | 471 |
for (ExprIterator it = b; it != e; ++it) {
|
| 472 | 472 |
glp_set_obj_coef(lp, it->first, it->second); |
| 473 | 473 |
} |
| 474 | 474 |
} |
| 475 | 475 |
|
| 476 | 476 |
void GlpkBase::_getObjCoeffs(InsertIterator b) const {
|
| 477 | 477 |
for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
|
| 478 | 478 |
Value val = glp_get_obj_coef(lp, i); |
| 479 | 479 |
if (val != 0.0) {
|
| 480 | 480 |
*b = std::make_pair(i, val); |
| 481 | 481 |
++b; |
| 482 | 482 |
} |
| 483 | 483 |
} |
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
|
| 487 | 487 |
//i = 0 means the constant term (shift) |
| 488 | 488 |
glp_set_obj_coef(lp, i, obj_coef); |
| 489 | 489 |
} |
| 490 | 490 |
|
| 491 | 491 |
GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
|
| 492 | 492 |
//i = 0 means the constant term (shift) |
| 493 | 493 |
return glp_get_obj_coef(lp, i); |
| 494 | 494 |
} |
| 495 | 495 |
|
| 496 | 496 |
void GlpkBase::_setSense(GlpkBase::Sense sense) {
|
| 497 | 497 |
switch (sense) {
|
| 498 | 498 |
case MIN: |
| 499 | 499 |
glp_set_obj_dir(lp, GLP_MIN); |
| 500 | 500 |
break; |
| 501 | 501 |
case MAX: |
| 502 | 502 |
glp_set_obj_dir(lp, GLP_MAX); |
| 503 | 503 |
break; |
| 504 | 504 |
} |
| 505 | 505 |
} |
| 506 | 506 |
|
| 507 | 507 |
GlpkBase::Sense GlpkBase::_getSense() const {
|
| 508 | 508 |
switch(glp_get_obj_dir(lp)) {
|
| 509 | 509 |
case GLP_MIN: |
| 510 | 510 |
return MIN; |
| 511 | 511 |
case GLP_MAX: |
| 512 | 512 |
return MAX; |
| 513 | 513 |
default: |
| 514 | 514 |
LEMON_ASSERT(false, "Wrong sense"); |
| 515 | 515 |
return GlpkBase::Sense(); |
| 516 | 516 |
} |
| 517 | 517 |
} |
| 518 | 518 |
|
| 519 | 519 |
void GlpkBase::_clear() {
|
| 520 | 520 |
glp_erase_prob(lp); |
| 521 | 521 |
rows.clear(); |
| 522 | 522 |
cols.clear(); |
| 523 | 523 |
} |
| 524 | 524 |
|
| 525 |
// |
|
| 525 |
// GlpkLp members |
|
| 526 | 526 |
|
| 527 |
|
|
| 527 |
GlpkLp::GlpkLp() |
|
| 528 | 528 |
: LpBase(), GlpkBase(), LpSolver() {
|
| 529 | 529 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 530 | 530 |
} |
| 531 | 531 |
|
| 532 |
|
|
| 532 |
GlpkLp::GlpkLp(const GlpkLp& other) |
|
| 533 | 533 |
: LpBase(other), GlpkBase(other), LpSolver(other) {
|
| 534 | 534 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 535 | 535 |
} |
| 536 | 536 |
|
| 537 |
LpGlpk* LpGlpk::_newSolver() const { return new LpGlpk; }
|
|
| 538 |
LpGlpk* LpGlpk::_cloneSolver() const { return new LpGlpk(*this); }
|
|
| 537 |
GlpkLp* GlpkLp::_newSolver() const { return new GlpkLp; }
|
|
| 538 |
GlpkLp* GlpkLp::_cloneSolver() const { return new GlpkLp(*this); }
|
|
| 539 | 539 |
|
| 540 |
const char* |
|
| 540 |
const char* GlpkLp::_solverName() const { return "GlpkLp"; }
|
|
| 541 | 541 |
|
| 542 |
void |
|
| 542 |
void GlpkLp::_clear_temporals() {
|
|
| 543 | 543 |
_primal_ray.clear(); |
| 544 | 544 |
_dual_ray.clear(); |
| 545 | 545 |
} |
| 546 | 546 |
|
| 547 |
|
|
| 547 |
GlpkLp::SolveExitStatus GlpkLp::_solve() {
|
|
| 548 | 548 |
return solvePrimal(); |
| 549 | 549 |
} |
| 550 | 550 |
|
| 551 |
|
|
| 551 |
GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
|
|
| 552 | 552 |
_clear_temporals(); |
| 553 | 553 |
|
| 554 | 554 |
glp_smcp smcp; |
| 555 | 555 |
glp_init_smcp(&smcp); |
| 556 | 556 |
|
| 557 | 557 |
switch (_message_level) {
|
| 558 | 558 |
case MESSAGE_NO_OUTPUT: |
| 559 | 559 |
smcp.msg_lev = GLP_MSG_OFF; |
| 560 | 560 |
break; |
| 561 | 561 |
case MESSAGE_ERROR_MESSAGE: |
| 562 | 562 |
smcp.msg_lev = GLP_MSG_ERR; |
| 563 | 563 |
break; |
| 564 | 564 |
case MESSAGE_NORMAL_OUTPUT: |
| 565 | 565 |
smcp.msg_lev = GLP_MSG_ON; |
| 566 | 566 |
break; |
| 567 | 567 |
case MESSAGE_FULL_OUTPUT: |
| 568 | 568 |
smcp.msg_lev = GLP_MSG_ALL; |
| 569 | 569 |
break; |
| 570 | 570 |
} |
| 571 | 571 |
|
| 572 | 572 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
| 573 | 573 |
return SOLVED; |
| 574 | 574 |
} |
| 575 | 575 |
|
| 576 |
|
|
| 576 |
GlpkLp::SolveExitStatus GlpkLp::solveDual() {
|
|
| 577 | 577 |
_clear_temporals(); |
| 578 | 578 |
|
| 579 | 579 |
glp_smcp smcp; |
| 580 | 580 |
glp_init_smcp(&smcp); |
| 581 | 581 |
|
| 582 | 582 |
switch (_message_level) {
|
| 583 | 583 |
case MESSAGE_NO_OUTPUT: |
| 584 | 584 |
smcp.msg_lev = GLP_MSG_OFF; |
| 585 | 585 |
break; |
| 586 | 586 |
case MESSAGE_ERROR_MESSAGE: |
| 587 | 587 |
smcp.msg_lev = GLP_MSG_ERR; |
| 588 | 588 |
break; |
| 589 | 589 |
case MESSAGE_NORMAL_OUTPUT: |
| 590 | 590 |
smcp.msg_lev = GLP_MSG_ON; |
| 591 | 591 |
break; |
| 592 | 592 |
case MESSAGE_FULL_OUTPUT: |
| 593 | 593 |
smcp.msg_lev = GLP_MSG_ALL; |
| 594 | 594 |
break; |
| 595 | 595 |
} |
| 596 | 596 |
smcp.meth = GLP_DUAL; |
| 597 | 597 |
|
| 598 | 598 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
| 599 | 599 |
return SOLVED; |
| 600 | 600 |
} |
| 601 | 601 |
|
| 602 |
|
|
| 602 |
GlpkLp::Value GlpkLp::_getPrimal(int i) const {
|
|
| 603 | 603 |
return glp_get_col_prim(lp, i); |
| 604 | 604 |
} |
| 605 | 605 |
|
| 606 |
|
|
| 606 |
GlpkLp::Value GlpkLp::_getDual(int i) const {
|
|
| 607 | 607 |
return glp_get_row_dual(lp, i); |
| 608 | 608 |
} |
| 609 | 609 |
|
| 610 |
|
|
| 610 |
GlpkLp::Value GlpkLp::_getPrimalValue() const {
|
|
| 611 | 611 |
return glp_get_obj_val(lp); |
| 612 | 612 |
} |
| 613 | 613 |
|
| 614 |
|
|
| 614 |
GlpkLp::VarStatus GlpkLp::_getColStatus(int i) const {
|
|
| 615 | 615 |
switch (glp_get_col_stat(lp, i)) {
|
| 616 | 616 |
case GLP_BS: |
| 617 | 617 |
return BASIC; |
| 618 | 618 |
case GLP_UP: |
| 619 | 619 |
return UPPER; |
| 620 | 620 |
case GLP_LO: |
| 621 | 621 |
return LOWER; |
| 622 | 622 |
case GLP_NF: |
| 623 | 623 |
return FREE; |
| 624 | 624 |
case GLP_NS: |
| 625 | 625 |
return FIXED; |
| 626 | 626 |
default: |
| 627 | 627 |
LEMON_ASSERT(false, "Wrong column status"); |
| 628 |
return |
|
| 628 |
return GlpkLp::VarStatus(); |
|
| 629 | 629 |
} |
| 630 | 630 |
} |
| 631 | 631 |
|
| 632 |
|
|
| 632 |
GlpkLp::VarStatus GlpkLp::_getRowStatus(int i) const {
|
|
| 633 | 633 |
switch (glp_get_row_stat(lp, i)) {
|
| 634 | 634 |
case GLP_BS: |
| 635 | 635 |
return BASIC; |
| 636 | 636 |
case GLP_UP: |
| 637 | 637 |
return UPPER; |
| 638 | 638 |
case GLP_LO: |
| 639 | 639 |
return LOWER; |
| 640 | 640 |
case GLP_NF: |
| 641 | 641 |
return FREE; |
| 642 | 642 |
case GLP_NS: |
| 643 | 643 |
return FIXED; |
| 644 | 644 |
default: |
| 645 | 645 |
LEMON_ASSERT(false, "Wrong row status"); |
| 646 |
return |
|
| 646 |
return GlpkLp::VarStatus(); |
|
| 647 | 647 |
} |
| 648 | 648 |
} |
| 649 | 649 |
|
| 650 |
|
|
| 650 |
GlpkLp::Value GlpkLp::_getPrimalRay(int i) const {
|
|
| 651 | 651 |
if (_primal_ray.empty()) {
|
| 652 | 652 |
int row_num = glp_get_num_rows(lp); |
| 653 | 653 |
int col_num = glp_get_num_cols(lp); |
| 654 | 654 |
|
| 655 | 655 |
_primal_ray.resize(col_num + 1, 0.0); |
| 656 | 656 |
|
| 657 | 657 |
int index = glp_get_unbnd_ray(lp); |
| 658 | 658 |
if (index != 0) {
|
| 659 | 659 |
// The primal ray is found in primal simplex second phase |
| 660 | 660 |
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) : |
| 661 | 661 |
glp_get_col_stat(lp, index - row_num)) != GLP_BS, |
| 662 | 662 |
"Wrong primal ray"); |
| 663 | 663 |
|
| 664 | 664 |
bool negate = glp_get_obj_dir(lp) == GLP_MAX; |
| 665 | 665 |
|
| 666 | 666 |
if (index > row_num) {
|
| 667 | 667 |
_primal_ray[index - row_num] = 1.0; |
| 668 | 668 |
if (glp_get_col_dual(lp, index - row_num) > 0) {
|
| 669 | 669 |
negate = !negate; |
| 670 | 670 |
} |
| 671 | 671 |
} else {
|
| 672 | 672 |
if (glp_get_row_dual(lp, index) > 0) {
|
| 673 | 673 |
negate = !negate; |
| 674 | 674 |
} |
| 675 | 675 |
} |
| 676 | 676 |
|
| 677 | 677 |
std::vector<int> ray_indexes(row_num + 1); |
| 678 | 678 |
std::vector<Value> ray_values(row_num + 1); |
| 679 | 679 |
int ray_length = glp_eval_tab_col(lp, index, &ray_indexes.front(), |
| 680 | 680 |
&ray_values.front()); |
| 681 | 681 |
|
| 682 | 682 |
for (int i = 1; i <= ray_length; ++i) {
|
| 683 | 683 |
if (ray_indexes[i] > row_num) {
|
| 684 | 684 |
_primal_ray[ray_indexes[i] - row_num] = ray_values[i]; |
| 685 | 685 |
} |
| 686 | 686 |
} |
| 687 | 687 |
|
| 688 | 688 |
if (negate) {
|
| 689 | 689 |
for (int i = 1; i <= col_num; ++i) {
|
| 690 | 690 |
_primal_ray[i] = - _primal_ray[i]; |
| 691 | 691 |
} |
| 692 | 692 |
} |
| 693 | 693 |
} else {
|
| 694 | 694 |
for (int i = 1; i <= col_num; ++i) {
|
| 695 | 695 |
_primal_ray[i] = glp_get_col_prim(lp, i); |
| 696 | 696 |
} |
| 697 | 697 |
} |
| 698 | 698 |
} |
| 699 | 699 |
return _primal_ray[i]; |
| 700 | 700 |
} |
| 701 | 701 |
|
| 702 |
|
|
| 702 |
GlpkLp::Value GlpkLp::_getDualRay(int i) const {
|
|
| 703 | 703 |
if (_dual_ray.empty()) {
|
| 704 | 704 |
int row_num = glp_get_num_rows(lp); |
| 705 | 705 |
|
| 706 | 706 |
_dual_ray.resize(row_num + 1, 0.0); |
| 707 | 707 |
|
| 708 | 708 |
int index = glp_get_unbnd_ray(lp); |
| 709 | 709 |
if (index != 0) {
|
| 710 | 710 |
// The dual ray is found in dual simplex second phase |
| 711 | 711 |
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) : |
| 712 | 712 |
glp_get_col_stat(lp, index - row_num)) == GLP_BS, |
| 713 | 713 |
|
| 714 | 714 |
"Wrong dual ray"); |
| 715 | 715 |
|
| 716 | 716 |
int idx; |
| 717 | 717 |
bool negate = false; |
| 718 | 718 |
|
| 719 | 719 |
if (index > row_num) {
|
| 720 | 720 |
idx = glp_get_col_bind(lp, index - row_num); |
| 721 | 721 |
if (glp_get_col_prim(lp, index - row_num) > |
| 722 | 722 |
glp_get_col_ub(lp, index - row_num)) {
|
| 723 | 723 |
negate = true; |
| 724 | 724 |
} |
| 725 | 725 |
} else {
|
| 726 | 726 |
idx = glp_get_row_bind(lp, index); |
| 727 | 727 |
if (glp_get_row_prim(lp, index) > glp_get_row_ub(lp, index)) {
|
| 728 | 728 |
negate = true; |
| 729 | 729 |
} |
| 730 | 730 |
} |
| 731 | 731 |
|
| 732 | 732 |
_dual_ray[idx] = negate ? - 1.0 : 1.0; |
| 733 | 733 |
|
| 734 | 734 |
glp_btran(lp, &_dual_ray.front()); |
| 735 | 735 |
} else {
|
| 736 | 736 |
double eps = 1e-7; |
| 737 | 737 |
// The dual ray is found in primal simplex first phase |
| 738 | 738 |
// We assume that the glpk minimizes the slack to get feasible solution |
| 739 | 739 |
for (int i = 1; i <= row_num; ++i) {
|
| 740 | 740 |
int index = glp_get_bhead(lp, i); |
| 741 | 741 |
if (index <= row_num) {
|
| 742 | 742 |
double res = glp_get_row_prim(lp, index); |
| 743 | 743 |
if (res > glp_get_row_ub(lp, index) + eps) {
|
| 744 | 744 |
_dual_ray[i] = -1; |
| 745 | 745 |
} else if (res < glp_get_row_lb(lp, index) - eps) {
|
| 746 | 746 |
_dual_ray[i] = 1; |
| 747 | 747 |
} else {
|
| 748 | 748 |
_dual_ray[i] = 0; |
| 749 | 749 |
} |
| 750 | 750 |
_dual_ray[i] *= glp_get_rii(lp, index); |
| 751 | 751 |
} else {
|
| 752 | 752 |
double res = glp_get_col_prim(lp, index - row_num); |
| 753 | 753 |
if (res > glp_get_col_ub(lp, index - row_num) + eps) {
|
| 754 | 754 |
_dual_ray[i] = -1; |
| 755 | 755 |
} else if (res < glp_get_col_lb(lp, index - row_num) - eps) {
|
| 756 | 756 |
_dual_ray[i] = 1; |
| 757 | 757 |
} else {
|
| 758 | 758 |
_dual_ray[i] = 0; |
| 759 | 759 |
} |
| 760 | 760 |
_dual_ray[i] /= glp_get_sjj(lp, index - row_num); |
| 761 | 761 |
} |
| 762 | 762 |
} |
| 763 | 763 |
|
| 764 | 764 |
glp_btran(lp, &_dual_ray.front()); |
| 765 | 765 |
|
| 766 | 766 |
for (int i = 1; i <= row_num; ++i) {
|
| 767 | 767 |
_dual_ray[i] /= glp_get_rii(lp, i); |
| 768 | 768 |
} |
| 769 | 769 |
} |
| 770 | 770 |
} |
| 771 | 771 |
return _dual_ray[i]; |
| 772 | 772 |
} |
| 773 | 773 |
|
| 774 |
|
|
| 774 |
GlpkLp::ProblemType GlpkLp::_getPrimalType() const {
|
|
| 775 | 775 |
if (glp_get_status(lp) == GLP_OPT) |
| 776 | 776 |
return OPTIMAL; |
| 777 | 777 |
switch (glp_get_prim_stat(lp)) {
|
| 778 | 778 |
case GLP_UNDEF: |
| 779 | 779 |
return UNDEFINED; |
| 780 | 780 |
case GLP_FEAS: |
| 781 | 781 |
case GLP_INFEAS: |
| 782 | 782 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
|
| 783 | 783 |
return UNBOUNDED; |
| 784 | 784 |
} else {
|
| 785 | 785 |
return UNDEFINED; |
| 786 | 786 |
} |
| 787 | 787 |
case GLP_NOFEAS: |
| 788 | 788 |
return INFEASIBLE; |
| 789 | 789 |
default: |
| 790 | 790 |
LEMON_ASSERT(false, "Wrong primal type"); |
| 791 |
return |
|
| 791 |
return GlpkLp::ProblemType(); |
|
| 792 | 792 |
} |
| 793 | 793 |
} |
| 794 | 794 |
|
| 795 |
|
|
| 795 |
GlpkLp::ProblemType GlpkLp::_getDualType() const {
|
|
| 796 | 796 |
if (glp_get_status(lp) == GLP_OPT) |
| 797 | 797 |
return OPTIMAL; |
| 798 | 798 |
switch (glp_get_dual_stat(lp)) {
|
| 799 | 799 |
case GLP_UNDEF: |
| 800 | 800 |
return UNDEFINED; |
| 801 | 801 |
case GLP_FEAS: |
| 802 | 802 |
case GLP_INFEAS: |
| 803 | 803 |
if (glp_get_prim_stat(lp) == GLP_NOFEAS) {
|
| 804 | 804 |
return UNBOUNDED; |
| 805 | 805 |
} else {
|
| 806 | 806 |
return UNDEFINED; |
| 807 | 807 |
} |
| 808 | 808 |
case GLP_NOFEAS: |
| 809 | 809 |
return INFEASIBLE; |
| 810 | 810 |
default: |
| 811 | 811 |
LEMON_ASSERT(false, "Wrong primal type"); |
| 812 |
return |
|
| 812 |
return GlpkLp::ProblemType(); |
|
| 813 | 813 |
} |
| 814 | 814 |
} |
| 815 | 815 |
|
| 816 |
void |
|
| 816 |
void GlpkLp::presolver(bool b) {
|
|
| 817 | 817 |
lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0); |
| 818 | 818 |
} |
| 819 | 819 |
|
| 820 |
void |
|
| 820 |
void GlpkLp::messageLevel(MessageLevel m) {
|
|
| 821 | 821 |
_message_level = m; |
| 822 | 822 |
} |
| 823 | 823 |
|
| 824 |
// |
|
| 824 |
// GlpkMip members |
|
| 825 | 825 |
|
| 826 |
|
|
| 826 |
GlpkMip::GlpkMip() |
|
| 827 | 827 |
: LpBase(), GlpkBase(), MipSolver() {
|
| 828 | 828 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 829 | 829 |
} |
| 830 | 830 |
|
| 831 |
|
|
| 831 |
GlpkMip::GlpkMip(const GlpkMip& other) |
|
| 832 | 832 |
: LpBase(), GlpkBase(other), MipSolver() {
|
| 833 | 833 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 834 | 834 |
} |
| 835 | 835 |
|
| 836 |
void |
|
| 836 |
void GlpkMip::_setColType(int i, GlpkMip::ColTypes col_type) {
|
|
| 837 | 837 |
switch (col_type) {
|
| 838 | 838 |
case INTEGER: |
| 839 | 839 |
glp_set_col_kind(lp, i, GLP_IV); |
| 840 | 840 |
break; |
| 841 | 841 |
case REAL: |
| 842 | 842 |
glp_set_col_kind(lp, i, GLP_CV); |
| 843 | 843 |
break; |
| 844 | 844 |
} |
| 845 | 845 |
} |
| 846 | 846 |
|
| 847 |
|
|
| 847 |
GlpkMip::ColTypes GlpkMip::_getColType(int i) const {
|
|
| 848 | 848 |
switch (glp_get_col_kind(lp, i)) {
|
| 849 | 849 |
case GLP_IV: |
| 850 | 850 |
case GLP_BV: |
| 851 | 851 |
return INTEGER; |
| 852 | 852 |
default: |
| 853 | 853 |
return REAL; |
| 854 | 854 |
} |
| 855 | 855 |
|
| 856 | 856 |
} |
| 857 | 857 |
|
| 858 |
|
|
| 858 |
GlpkMip::SolveExitStatus GlpkMip::_solve() {
|
|
| 859 | 859 |
glp_smcp smcp; |
| 860 | 860 |
glp_init_smcp(&smcp); |
| 861 | 861 |
|
| 862 | 862 |
switch (_message_level) {
|
| 863 | 863 |
case MESSAGE_NO_OUTPUT: |
| 864 | 864 |
smcp.msg_lev = GLP_MSG_OFF; |
| 865 | 865 |
break; |
| 866 | 866 |
case MESSAGE_ERROR_MESSAGE: |
| 867 | 867 |
smcp.msg_lev = GLP_MSG_ERR; |
| 868 | 868 |
break; |
| 869 | 869 |
case MESSAGE_NORMAL_OUTPUT: |
| 870 | 870 |
smcp.msg_lev = GLP_MSG_ON; |
| 871 | 871 |
break; |
| 872 | 872 |
case MESSAGE_FULL_OUTPUT: |
| 873 | 873 |
smcp.msg_lev = GLP_MSG_ALL; |
| 874 | 874 |
break; |
| 875 | 875 |
} |
| 876 | 876 |
smcp.meth = GLP_DUAL; |
| 877 | 877 |
|
| 878 | 878 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
| 879 | 879 |
if (glp_get_status(lp) != GLP_OPT) return SOLVED; |
| 880 | 880 |
|
| 881 | 881 |
glp_iocp iocp; |
| 882 | 882 |
glp_init_iocp(&iocp); |
| 883 | 883 |
|
| 884 | 884 |
switch (_message_level) {
|
| 885 | 885 |
case MESSAGE_NO_OUTPUT: |
| 886 | 886 |
iocp.msg_lev = GLP_MSG_OFF; |
| 887 | 887 |
break; |
| 888 | 888 |
case MESSAGE_ERROR_MESSAGE: |
| 889 | 889 |
iocp.msg_lev = GLP_MSG_ERR; |
| 890 | 890 |
break; |
| 891 | 891 |
case MESSAGE_NORMAL_OUTPUT: |
| 892 | 892 |
iocp.msg_lev = GLP_MSG_ON; |
| 893 | 893 |
break; |
| 894 | 894 |
case MESSAGE_FULL_OUTPUT: |
| 895 | 895 |
iocp.msg_lev = GLP_MSG_ALL; |
| 896 | 896 |
break; |
| 897 | 897 |
} |
| 898 | 898 |
|
| 899 | 899 |
if (glp_intopt(lp, &iocp) != 0) return UNSOLVED; |
| 900 | 900 |
return SOLVED; |
| 901 | 901 |
} |
| 902 | 902 |
|
| 903 | 903 |
|
| 904 |
|
|
| 904 |
GlpkMip::ProblemType GlpkMip::_getType() const {
|
|
| 905 | 905 |
switch (glp_get_status(lp)) {
|
| 906 | 906 |
case GLP_OPT: |
| 907 | 907 |
switch (glp_mip_status(lp)) {
|
| 908 | 908 |
case GLP_UNDEF: |
| 909 | 909 |
return UNDEFINED; |
| 910 | 910 |
case GLP_NOFEAS: |
| 911 | 911 |
return INFEASIBLE; |
| 912 | 912 |
case GLP_FEAS: |
| 913 | 913 |
return FEASIBLE; |
| 914 | 914 |
case GLP_OPT: |
| 915 | 915 |
return OPTIMAL; |
| 916 | 916 |
default: |
| 917 | 917 |
LEMON_ASSERT(false, "Wrong problem type."); |
| 918 |
return |
|
| 918 |
return GlpkMip::ProblemType(); |
|
| 919 | 919 |
} |
| 920 | 920 |
case GLP_NOFEAS: |
| 921 | 921 |
return INFEASIBLE; |
| 922 | 922 |
case GLP_INFEAS: |
| 923 | 923 |
case GLP_FEAS: |
| 924 | 924 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
|
| 925 | 925 |
return UNBOUNDED; |
| 926 | 926 |
} else {
|
| 927 | 927 |
return UNDEFINED; |
| 928 | 928 |
} |
| 929 | 929 |
default: |
| 930 | 930 |
LEMON_ASSERT(false, "Wrong problem type."); |
| 931 |
return |
|
| 931 |
return GlpkMip::ProblemType(); |
|
| 932 | 932 |
} |
| 933 | 933 |
} |
| 934 | 934 |
|
| 935 |
|
|
| 935 |
GlpkMip::Value GlpkMip::_getSol(int i) const {
|
|
| 936 | 936 |
return glp_mip_col_val(lp, i); |
| 937 | 937 |
} |
| 938 | 938 |
|
| 939 |
|
|
| 939 |
GlpkMip::Value GlpkMip::_getSolValue() const {
|
|
| 940 | 940 |
return glp_mip_obj_val(lp); |
| 941 | 941 |
} |
| 942 | 942 |
|
| 943 |
MipGlpk* MipGlpk::_newSolver() const { return new MipGlpk; }
|
|
| 944 |
MipGlpk* MipGlpk::_cloneSolver() const {return new MipGlpk(*this); }
|
|
| 943 |
GlpkMip* GlpkMip::_newSolver() const { return new GlpkMip; }
|
|
| 944 |
GlpkMip* GlpkMip::_cloneSolver() const {return new GlpkMip(*this); }
|
|
| 945 | 945 |
|
| 946 |
const char* |
|
| 946 |
const char* GlpkMip::_solverName() const { return "GlpkMip"; }
|
|
| 947 | 947 |
|
| 948 |
void |
|
| 948 |
void GlpkMip::messageLevel(MessageLevel m) {
|
|
| 949 | 949 |
_message_level = m; |
| 950 | 950 |
} |
| 951 | 951 |
|
| 952 | 952 |
} //END OF NAMESPACE LEMON |
| ... | ... |
@@ -58,202 +58,202 @@ |
| 58 | 58 |
virtual void _eraseRow(int i); |
| 59 | 59 |
|
| 60 | 60 |
virtual void _eraseColId(int i); |
| 61 | 61 |
virtual void _eraseRowId(int i); |
| 62 | 62 |
|
| 63 | 63 |
virtual void _getColName(int col, std::string& name) const; |
| 64 | 64 |
virtual void _setColName(int col, const std::string& name); |
| 65 | 65 |
virtual int _colByName(const std::string& name) const; |
| 66 | 66 |
|
| 67 | 67 |
virtual void _getRowName(int row, std::string& name) const; |
| 68 | 68 |
virtual void _setRowName(int row, const std::string& name); |
| 69 | 69 |
virtual int _rowByName(const std::string& name) const; |
| 70 | 70 |
|
| 71 | 71 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
| 72 | 72 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
| 73 | 73 |
|
| 74 | 74 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
| 75 | 75 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
| 76 | 76 |
|
| 77 | 77 |
virtual void _setCoeff(int row, int col, Value value); |
| 78 | 78 |
virtual Value _getCoeff(int row, int col) const; |
| 79 | 79 |
|
| 80 | 80 |
virtual void _setColLowerBound(int i, Value value); |
| 81 | 81 |
virtual Value _getColLowerBound(int i) const; |
| 82 | 82 |
|
| 83 | 83 |
virtual void _setColUpperBound(int i, Value value); |
| 84 | 84 |
virtual Value _getColUpperBound(int i) const; |
| 85 | 85 |
|
| 86 | 86 |
virtual void _setRowLowerBound(int i, Value value); |
| 87 | 87 |
virtual Value _getRowLowerBound(int i) const; |
| 88 | 88 |
|
| 89 | 89 |
virtual void _setRowUpperBound(int i, Value value); |
| 90 | 90 |
virtual Value _getRowUpperBound(int i) const; |
| 91 | 91 |
|
| 92 | 92 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
| 93 | 93 |
virtual void _getObjCoeffs(InsertIterator b) const; |
| 94 | 94 |
|
| 95 | 95 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 96 | 96 |
virtual Value _getObjCoeff(int i) const; |
| 97 | 97 |
|
| 98 | 98 |
virtual void _setSense(Sense); |
| 99 | 99 |
virtual Sense _getSense() const; |
| 100 | 100 |
|
| 101 | 101 |
virtual void _clear(); |
| 102 | 102 |
|
| 103 | 103 |
public: |
| 104 | 104 |
|
| 105 | 105 |
///Pointer to the underlying GLPK data structure. |
| 106 | 106 |
LPX *lpx() {return lp;}
|
| 107 | 107 |
///Const pointer to the underlying GLPK data structure. |
| 108 | 108 |
const LPX *lpx() const {return lp;}
|
| 109 | 109 |
|
| 110 | 110 |
///Returns the constraint identifier understood by GLPK. |
| 111 | 111 |
int lpxRow(Row r) const { return rows(id(r)); }
|
| 112 | 112 |
|
| 113 | 113 |
///Returns the variable identifier understood by GLPK. |
| 114 | 114 |
int lpxCol(Col c) const { return cols(id(c)); }
|
| 115 | 115 |
|
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Interface for the GLPK LP solver |
| 119 | 119 |
/// |
| 120 | 120 |
/// This class implements an interface for the GLPK LP solver. |
| 121 | 121 |
///\ingroup lp_group |
| 122 |
class |
|
| 122 |
class GlpkLp : public GlpkBase, public LpSolver {
|
|
| 123 | 123 |
public: |
| 124 | 124 |
|
| 125 | 125 |
///\e |
| 126 |
|
|
| 126 |
GlpkLp(); |
|
| 127 | 127 |
///\e |
| 128 |
|
|
| 128 |
GlpkLp(const GlpkLp&); |
|
| 129 | 129 |
|
| 130 | 130 |
private: |
| 131 | 131 |
|
| 132 | 132 |
mutable std::vector<double> _primal_ray; |
| 133 | 133 |
mutable std::vector<double> _dual_ray; |
| 134 | 134 |
|
| 135 | 135 |
void _clear_temporals(); |
| 136 | 136 |
|
| 137 | 137 |
protected: |
| 138 | 138 |
|
| 139 |
virtual LpGlpk* _cloneSolver() const; |
|
| 140 |
virtual LpGlpk* _newSolver() const; |
|
| 139 |
virtual GlpkLp* _cloneSolver() const; |
|
| 140 |
virtual GlpkLp* _newSolver() const; |
|
| 141 | 141 |
|
| 142 | 142 |
virtual const char* _solverName() const; |
| 143 | 143 |
|
| 144 | 144 |
virtual SolveExitStatus _solve(); |
| 145 | 145 |
virtual Value _getPrimal(int i) const; |
| 146 | 146 |
virtual Value _getDual(int i) const; |
| 147 | 147 |
|
| 148 | 148 |
virtual Value _getPrimalValue() const; |
| 149 | 149 |
|
| 150 | 150 |
virtual VarStatus _getColStatus(int i) const; |
| 151 | 151 |
virtual VarStatus _getRowStatus(int i) const; |
| 152 | 152 |
|
| 153 | 153 |
virtual Value _getPrimalRay(int i) const; |
| 154 | 154 |
virtual Value _getDualRay(int i) const; |
| 155 | 155 |
|
| 156 | 156 |
///\todo It should be clarified |
| 157 | 157 |
/// |
| 158 | 158 |
virtual ProblemType _getPrimalType() const; |
| 159 | 159 |
virtual ProblemType _getDualType() const; |
| 160 | 160 |
|
| 161 | 161 |
public: |
| 162 | 162 |
|
| 163 | 163 |
///Solve with primal simplex |
| 164 | 164 |
SolveExitStatus solvePrimal(); |
| 165 | 165 |
|
| 166 | 166 |
///Solve with dual simplex |
| 167 | 167 |
SolveExitStatus solveDual(); |
| 168 | 168 |
|
| 169 | 169 |
///Turns on or off the presolver |
| 170 | 170 |
|
| 171 | 171 |
///Turns on (\c b is \c true) or off (\c b is \c false) the presolver |
| 172 | 172 |
/// |
| 173 | 173 |
///The presolver is off by default. |
| 174 | 174 |
void presolver(bool b); |
| 175 | 175 |
|
| 176 | 176 |
///Enum for \c messageLevel() parameter |
| 177 | 177 |
enum MessageLevel {
|
| 178 | 178 |
/// no output (default value) |
| 179 | 179 |
MESSAGE_NO_OUTPUT = 0, |
| 180 | 180 |
/// error messages only |
| 181 | 181 |
MESSAGE_ERROR_MESSAGE = 1, |
| 182 | 182 |
/// normal output |
| 183 | 183 |
MESSAGE_NORMAL_OUTPUT = 2, |
| 184 | 184 |
/// full output (includes informational messages) |
| 185 | 185 |
MESSAGE_FULL_OUTPUT = 3 |
| 186 | 186 |
}; |
| 187 | 187 |
|
| 188 | 188 |
private: |
| 189 | 189 |
|
| 190 | 190 |
MessageLevel _message_level; |
| 191 | 191 |
|
| 192 | 192 |
public: |
| 193 | 193 |
|
| 194 | 194 |
///Set the verbosity of the messages |
| 195 | 195 |
|
| 196 | 196 |
///Set the verbosity of the messages |
| 197 | 197 |
/// |
| 198 | 198 |
///\param m is the level of the messages output by the solver routines. |
| 199 | 199 |
void messageLevel(MessageLevel m); |
| 200 | 200 |
}; |
| 201 | 201 |
|
| 202 | 202 |
/// \brief Interface for the GLPK MIP solver |
| 203 | 203 |
/// |
| 204 | 204 |
/// This class implements an interface for the GLPK MIP solver. |
| 205 | 205 |
///\ingroup lp_group |
| 206 |
class |
|
| 206 |
class GlpkMip : public GlpkBase, public MipSolver {
|
|
| 207 | 207 |
public: |
| 208 | 208 |
|
| 209 | 209 |
///\e |
| 210 |
|
|
| 210 |
GlpkMip(); |
|
| 211 | 211 |
///\e |
| 212 |
|
|
| 212 |
GlpkMip(const GlpkMip&); |
|
| 213 | 213 |
|
| 214 | 214 |
protected: |
| 215 | 215 |
|
| 216 |
virtual MipGlpk* _cloneSolver() const; |
|
| 217 |
virtual MipGlpk* _newSolver() const; |
|
| 216 |
virtual GlpkMip* _cloneSolver() const; |
|
| 217 |
virtual GlpkMip* _newSolver() const; |
|
| 218 | 218 |
|
| 219 | 219 |
virtual const char* _solverName() const; |
| 220 | 220 |
|
| 221 | 221 |
virtual ColTypes _getColType(int col) const; |
| 222 | 222 |
virtual void _setColType(int col, ColTypes col_type); |
| 223 | 223 |
|
| 224 | 224 |
virtual SolveExitStatus _solve(); |
| 225 | 225 |
virtual ProblemType _getType() const; |
| 226 | 226 |
virtual Value _getSol(int i) const; |
| 227 | 227 |
virtual Value _getSolValue() const; |
| 228 | 228 |
|
| 229 | 229 |
///Enum for \c messageLevel() parameter |
| 230 | 230 |
enum MessageLevel {
|
| 231 | 231 |
/// no output (default value) |
| 232 | 232 |
MESSAGE_NO_OUTPUT = 0, |
| 233 | 233 |
/// error messages only |
| 234 | 234 |
MESSAGE_ERROR_MESSAGE = 1, |
| 235 | 235 |
/// normal output |
| 236 | 236 |
MESSAGE_NORMAL_OUTPUT = 2, |
| 237 | 237 |
/// full output (includes informational messages) |
| 238 | 238 |
MESSAGE_FULL_OUTPUT = 3 |
| 239 | 239 |
}; |
| 240 | 240 |
|
| 241 | 241 |
private: |
| 242 | 242 |
|
| 243 | 243 |
MessageLevel _message_level; |
| 244 | 244 |
|
| 245 | 245 |
public: |
| 246 | 246 |
|
| 247 | 247 |
///Set the verbosity of the messages |
| 248 | 248 |
|
| 249 | 249 |
///Set the verbosity of the messages |
| 250 | 250 |
/// |
| 251 | 251 |
///\param m is the level of the messages output by the solver routines. |
| 252 | 252 |
void messageLevel(MessageLevel m); |
| 253 | 253 |
}; |
| 254 | 254 |
|
| 255 | 255 |
|
| 256 | 256 |
} //END OF NAMESPACE LEMON |
| 257 | 257 |
|
| 258 | 258 |
#endif //LEMON_GLPK_H |
| 259 | 259 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_LP_H |
| 20 | 20 |
#define LEMON_LP_H |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/config.h> |
| 23 | 23 |
|
| 24 | 24 |
|
| 25 | 25 |
#ifdef HAVE_GLPK |
| 26 | 26 |
#include <lemon/glpk.h> |
| 27 | 27 |
#elif HAVE_CPLEX |
| 28 | 28 |
#include <lemon/cplex.h> |
| 29 | 29 |
#elif HAVE_SOPLEX |
| 30 | 30 |
#include <lemon/soplex.h> |
| 31 | 31 |
#elif HAVE_CLP |
| 32 | 32 |
#include <lemon/clp.h> |
| 33 | 33 |
#endif |
| 34 | 34 |
|
| 35 | 35 |
///\file |
| 36 | 36 |
///\brief Defines a default LP solver |
| 37 | 37 |
///\ingroup lp_group |
| 38 | 38 |
namespace lemon {
|
| 39 | 39 |
|
| 40 | 40 |
#ifdef DOXYGEN |
| 41 | 41 |
///The default LP solver identifier |
| 42 | 42 |
|
| 43 | 43 |
///The default LP solver identifier. |
| 44 | 44 |
///\ingroup lp_group |
| 45 | 45 |
/// |
| 46 | 46 |
///Currently, the possible values are \c GLPK, \c CPLEX, |
| 47 | 47 |
///\c SOPLEX or \c CLP |
| 48 | 48 |
#define LEMON_DEFAULT_LP SOLVER |
| 49 | 49 |
///The default LP solver |
| 50 | 50 |
|
| 51 | 51 |
///The default LP solver. |
| 52 | 52 |
///\ingroup lp_group |
| 53 | 53 |
/// |
| 54 |
///Currently, it is either \c LpGlpk, \c LpCplex, \c LpSoplex or \c LpClp |
|
| 55 |
typedef LpGlpk Lp; |
|
| 54 |
///Currently, it is either \c GlpkLp, \c CplexLp, \c SoplexLp or \c ClpLp |
|
| 55 |
typedef GlpkLp Lp; |
|
| 56 | 56 |
|
| 57 | 57 |
///The default MIP solver identifier |
| 58 | 58 |
|
| 59 | 59 |
///The default MIP solver identifier. |
| 60 | 60 |
///\ingroup lp_group |
| 61 | 61 |
/// |
| 62 | 62 |
///Currently, the possible values are \c GLPK or \c CPLEX |
| 63 | 63 |
#define LEMON_DEFAULT_MIP SOLVER |
| 64 | 64 |
///The default MIP solver. |
| 65 | 65 |
|
| 66 | 66 |
///The default MIP solver. |
| 67 | 67 |
///\ingroup lp_group |
| 68 | 68 |
/// |
| 69 |
///Currently, it is either \c MipGlpk or \c MipCplex |
|
| 70 |
typedef MipGlpk Mip; |
|
| 69 |
///Currently, it is either \c GlpkMip or \c CplexMip |
|
| 70 |
typedef GlpkMip Mip; |
|
| 71 | 71 |
#else |
| 72 | 72 |
#ifdef HAVE_GLPK |
| 73 | 73 |
# define LEMON_DEFAULT_LP GLPK |
| 74 |
typedef |
|
| 74 |
typedef GlpkLp Lp; |
|
| 75 | 75 |
# define LEMON_DEFAULT_MIP GLPK |
| 76 |
typedef |
|
| 76 |
typedef GlpkMip Mip; |
|
| 77 | 77 |
#elif HAVE_CPLEX |
| 78 | 78 |
# define LEMON_DEFAULT_LP CPLEX |
| 79 |
typedef |
|
| 79 |
typedef CplexLp Lp; |
|
| 80 | 80 |
# define LEMON_DEFAULT_MIP CPLEX |
| 81 |
typedef |
|
| 81 |
typedef CplexMip Mip; |
|
| 82 | 82 |
#elif HAVE_SOPLEX |
| 83 | 83 |
# define DEFAULT_LP SOPLEX |
| 84 |
typedef |
|
| 84 |
typedef SoplexLp Lp; |
|
| 85 | 85 |
#elif HAVE_CLP |
| 86 | 86 |
# define DEFAULT_LP CLP |
| 87 |
typedef |
|
| 87 |
typedef ClpLp Lp; |
|
| 88 | 88 |
#endif |
| 89 | 89 |
#endif |
| 90 | 90 |
|
| 91 | 91 |
} //namespace lemon |
| 92 | 92 |
|
| 93 | 93 |
#endif //LEMON_LP_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <lemon/soplex.h> |
| 21 | 21 |
|
| 22 | 22 |
#include <soplex/soplex.h> |
| 23 | 23 |
|
| 24 | 24 |
|
| 25 | 25 |
///\file |
| 26 | 26 |
///\brief Implementation of the LEMON-SOPLEX lp solver interface. |
| 27 | 27 |
namespace lemon {
|
| 28 | 28 |
|
| 29 |
|
|
| 29 |
SoplexLp::SoplexLp() {
|
|
| 30 | 30 |
soplex = new soplex::SoPlex; |
| 31 | 31 |
} |
| 32 | 32 |
|
| 33 |
|
|
| 33 |
SoplexLp::~SoplexLp() {
|
|
| 34 | 34 |
delete soplex; |
| 35 | 35 |
} |
| 36 | 36 |
|
| 37 |
|
|
| 37 |
SoplexLp::SoplexLp(const SoplexLp& lp) {
|
|
| 38 | 38 |
rows = lp.rows; |
| 39 | 39 |
cols = lp.cols; |
| 40 | 40 |
|
| 41 | 41 |
soplex = new soplex::SoPlex; |
| 42 | 42 |
(*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex); |
| 43 | 43 |
|
| 44 | 44 |
_col_names = lp._col_names; |
| 45 | 45 |
_col_names_ref = lp._col_names_ref; |
| 46 | 46 |
|
| 47 | 47 |
_row_names = lp._row_names; |
| 48 | 48 |
_row_names_ref = lp._row_names_ref; |
| 49 | 49 |
|
| 50 | 50 |
} |
| 51 | 51 |
|
| 52 |
void |
|
| 52 |
void SoplexLp::_clear_temporals() {
|
|
| 53 | 53 |
_primal_values.clear(); |
| 54 | 54 |
_dual_values.clear(); |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 |
LpSoplex* LpSoplex::_newSolver() const {
|
|
| 58 |
LpSoplex* newlp = new LpSoplex(); |
|
| 57 |
SoplexLp* SoplexLp::_newSolver() const {
|
|
| 58 |
SoplexLp* newlp = new SoplexLp(); |
|
| 59 | 59 |
return newlp; |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 |
LpSoplex* LpSoplex::_cloneSolver() const {
|
|
| 63 |
LpSoplex* newlp = new LpSoplex(*this); |
|
| 62 |
SoplexLp* SoplexLp::_cloneSolver() const {
|
|
| 63 |
SoplexLp* newlp = new SoplexLp(*this); |
|
| 64 | 64 |
return newlp; |
| 65 | 65 |
} |
| 66 | 66 |
|
| 67 |
const char* |
|
| 67 |
const char* SoplexLp::_solverName() const { return "SoplexLp"; }
|
|
| 68 | 68 |
|
| 69 |
int |
|
| 69 |
int SoplexLp::_addCol() {
|
|
| 70 | 70 |
soplex::LPCol c; |
| 71 | 71 |
c.setLower(-soplex::infinity); |
| 72 | 72 |
c.setUpper(soplex::infinity); |
| 73 | 73 |
soplex->addCol(c); |
| 74 | 74 |
|
| 75 | 75 |
_col_names.push_back(std::string()); |
| 76 | 76 |
|
| 77 | 77 |
return soplex->nCols() - 1; |
| 78 | 78 |
} |
| 79 | 79 |
|
| 80 |
int |
|
| 80 |
int SoplexLp::_addRow() {
|
|
| 81 | 81 |
soplex::LPRow r; |
| 82 | 82 |
r.setLhs(-soplex::infinity); |
| 83 | 83 |
r.setRhs(soplex::infinity); |
| 84 | 84 |
soplex->addRow(r); |
| 85 | 85 |
|
| 86 | 86 |
_row_names.push_back(std::string()); |
| 87 | 87 |
|
| 88 | 88 |
return soplex->nRows() - 1; |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
|
| 92 |
void |
|
| 92 |
void SoplexLp::_eraseCol(int i) {
|
|
| 93 | 93 |
soplex->removeCol(i); |
| 94 | 94 |
_col_names_ref.erase(_col_names[i]); |
| 95 | 95 |
_col_names[i] = _col_names.back(); |
| 96 | 96 |
_col_names_ref[_col_names.back()] = i; |
| 97 | 97 |
_col_names.pop_back(); |
| 98 | 98 |
} |
| 99 | 99 |
|
| 100 |
void |
|
| 100 |
void SoplexLp::_eraseRow(int i) {
|
|
| 101 | 101 |
soplex->removeRow(i); |
| 102 | 102 |
_row_names_ref.erase(_row_names[i]); |
| 103 | 103 |
_row_names[i] = _row_names.back(); |
| 104 | 104 |
_row_names_ref[_row_names.back()] = i; |
| 105 | 105 |
_row_names.pop_back(); |
| 106 | 106 |
} |
| 107 | 107 |
|
| 108 |
void |
|
| 108 |
void SoplexLp::_eraseColId(int i) {
|
|
| 109 | 109 |
cols.eraseIndex(i); |
| 110 | 110 |
cols.relocateIndex(i, cols.maxIndex()); |
| 111 | 111 |
} |
| 112 |
void |
|
| 112 |
void SoplexLp::_eraseRowId(int i) {
|
|
| 113 | 113 |
rows.eraseIndex(i); |
| 114 | 114 |
rows.relocateIndex(i, rows.maxIndex()); |
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 |
void |
|
| 117 |
void SoplexLp::_getColName(int c, std::string &name) const {
|
|
| 118 | 118 |
name = _col_names[c]; |
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 |
void |
|
| 121 |
void SoplexLp::_setColName(int c, const std::string &name) {
|
|
| 122 | 122 |
_col_names_ref.erase(_col_names[c]); |
| 123 | 123 |
_col_names[c] = name; |
| 124 | 124 |
if (!name.empty()) {
|
| 125 | 125 |
_col_names_ref.insert(std::make_pair(name, c)); |
| 126 | 126 |
} |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 |
int |
|
| 129 |
int SoplexLp::_colByName(const std::string& name) const {
|
|
| 130 | 130 |
std::map<std::string, int>::const_iterator it = |
| 131 | 131 |
_col_names_ref.find(name); |
| 132 | 132 |
if (it != _col_names_ref.end()) {
|
| 133 | 133 |
return it->second; |
| 134 | 134 |
} else {
|
| 135 | 135 |
return -1; |
| 136 | 136 |
} |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 |
void |
|
| 139 |
void SoplexLp::_getRowName(int r, std::string &name) const {
|
|
| 140 | 140 |
name = _row_names[r]; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 |
void |
|
| 143 |
void SoplexLp::_setRowName(int r, const std::string &name) {
|
|
| 144 | 144 |
_row_names_ref.erase(_row_names[r]); |
| 145 | 145 |
_row_names[r] = name; |
| 146 | 146 |
if (!name.empty()) {
|
| 147 | 147 |
_row_names_ref.insert(std::make_pair(name, r)); |
| 148 | 148 |
} |
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 |
int |
|
| 151 |
int SoplexLp::_rowByName(const std::string& name) const {
|
|
| 152 | 152 |
std::map<std::string, int>::const_iterator it = |
| 153 | 153 |
_row_names_ref.find(name); |
| 154 | 154 |
if (it != _row_names_ref.end()) {
|
| 155 | 155 |
return it->second; |
| 156 | 156 |
} else {
|
| 157 | 157 |
return -1; |
| 158 | 158 |
} |
| 159 | 159 |
} |
| 160 | 160 |
|
| 161 | 161 |
|
| 162 |
void |
|
| 162 |
void SoplexLp::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
|
|
| 163 | 163 |
for (int j = 0; j < soplex->nCols(); ++j) {
|
| 164 | 164 |
soplex->changeElement(i, j, 0.0); |
| 165 | 165 |
} |
| 166 | 166 |
for(ExprIterator it = b; it != e; ++it) {
|
| 167 | 167 |
soplex->changeElement(i, it->first, it->second); |
| 168 | 168 |
} |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 |
void |
|
| 171 |
void SoplexLp::_getRowCoeffs(int i, InsertIterator b) const {
|
|
| 172 | 172 |
const soplex::SVector& vec = soplex->rowVector(i); |
| 173 | 173 |
for (int k = 0; k < vec.size(); ++k) {
|
| 174 | 174 |
*b = std::make_pair(vec.index(k), vec.value(k)); |
| 175 | 175 |
++b; |
| 176 | 176 |
} |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 |
void |
|
| 179 |
void SoplexLp::_setColCoeffs(int j, ExprIterator b, ExprIterator e) {
|
|
| 180 | 180 |
for (int i = 0; i < soplex->nRows(); ++i) {
|
| 181 | 181 |
soplex->changeElement(i, j, 0.0); |
| 182 | 182 |
} |
| 183 | 183 |
for(ExprIterator it = b; it != e; ++it) {
|
| 184 | 184 |
soplex->changeElement(it->first, j, it->second); |
| 185 | 185 |
} |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 |
void |
|
| 188 |
void SoplexLp::_getColCoeffs(int i, InsertIterator b) const {
|
|
| 189 | 189 |
const soplex::SVector& vec = soplex->colVector(i); |
| 190 | 190 |
for (int k = 0; k < vec.size(); ++k) {
|
| 191 | 191 |
*b = std::make_pair(vec.index(k), vec.value(k)); |
| 192 | 192 |
++b; |
| 193 | 193 |
} |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 |
void |
|
| 196 |
void SoplexLp::_setCoeff(int i, int j, Value value) {
|
|
| 197 | 197 |
soplex->changeElement(i, j, value); |
| 198 | 198 |
} |
| 199 | 199 |
|
| 200 |
|
|
| 200 |
SoplexLp::Value SoplexLp::_getCoeff(int i, int j) const {
|
|
| 201 | 201 |
return soplex->rowVector(i)[j]; |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 |
void |
|
| 204 |
void SoplexLp::_setColLowerBound(int i, Value value) {
|
|
| 205 | 205 |
LEMON_ASSERT(value != INF, "Invalid bound"); |
| 206 | 206 |
soplex->changeLower(i, value != -INF ? value : -soplex::infinity); |
| 207 | 207 |
} |
| 208 | 208 |
|
| 209 |
|
|
| 209 |
SoplexLp::Value SoplexLp::_getColLowerBound(int i) const {
|
|
| 210 | 210 |
double value = soplex->lower(i); |
| 211 | 211 |
return value != -soplex::infinity ? value : -INF; |
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 |
void |
|
| 214 |
void SoplexLp::_setColUpperBound(int i, Value value) {
|
|
| 215 | 215 |
LEMON_ASSERT(value != -INF, "Invalid bound"); |
| 216 | 216 |
soplex->changeUpper(i, value != INF ? value : soplex::infinity); |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 |
|
|
| 219 |
SoplexLp::Value SoplexLp::_getColUpperBound(int i) const {
|
|
| 220 | 220 |
double value = soplex->upper(i); |
| 221 | 221 |
return value != soplex::infinity ? value : INF; |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 |
void |
|
| 224 |
void SoplexLp::_setRowLowerBound(int i, Value lb) {
|
|
| 225 | 225 |
LEMON_ASSERT(lb != INF, "Invalid bound"); |
| 226 | 226 |
soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, soplex->rhs(i)); |
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 |
|
|
| 229 |
SoplexLp::Value SoplexLp::_getRowLowerBound(int i) const {
|
|
| 230 | 230 |
double res = soplex->lhs(i); |
| 231 | 231 |
return res == -soplex::infinity ? -INF : res; |
| 232 | 232 |
} |
| 233 | 233 |
|
| 234 |
void |
|
| 234 |
void SoplexLp::_setRowUpperBound(int i, Value ub) {
|
|
| 235 | 235 |
LEMON_ASSERT(ub != -INF, "Invalid bound"); |
| 236 | 236 |
soplex->changeRange(i, soplex->lhs(i), ub != INF ? ub : soplex::infinity); |
| 237 | 237 |
} |
| 238 | 238 |
|
| 239 |
|
|
| 239 |
SoplexLp::Value SoplexLp::_getRowUpperBound(int i) const {
|
|
| 240 | 240 |
double res = soplex->rhs(i); |
| 241 | 241 |
return res == soplex::infinity ? INF : res; |
| 242 | 242 |
} |
| 243 | 243 |
|
| 244 |
void |
|
| 244 |
void SoplexLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
|
|
| 245 | 245 |
for (int j = 0; j < soplex->nCols(); ++j) {
|
| 246 | 246 |
soplex->changeObj(j, 0.0); |
| 247 | 247 |
} |
| 248 | 248 |
for (ExprIterator it = b; it != e; ++it) {
|
| 249 | 249 |
soplex->changeObj(it->first, it->second); |
| 250 | 250 |
} |
| 251 | 251 |
} |
| 252 | 252 |
|
| 253 |
void |
|
| 253 |
void SoplexLp::_getObjCoeffs(InsertIterator b) const {
|
|
| 254 | 254 |
for (int j = 0; j < soplex->nCols(); ++j) {
|
| 255 | 255 |
Value coef = soplex->obj(j); |
| 256 | 256 |
if (coef != 0.0) {
|
| 257 | 257 |
*b = std::make_pair(j, coef); |
| 258 | 258 |
++b; |
| 259 | 259 |
} |
| 260 | 260 |
} |
| 261 | 261 |
} |
| 262 | 262 |
|
| 263 |
void |
|
| 263 |
void SoplexLp::_setObjCoeff(int i, Value obj_coef) {
|
|
| 264 | 264 |
soplex->changeObj(i, obj_coef); |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 |
|
|
| 267 |
SoplexLp::Value SoplexLp::_getObjCoeff(int i) const {
|
|
| 268 | 268 |
return soplex->obj(i); |
| 269 | 269 |
} |
| 270 | 270 |
|
| 271 |
|
|
| 271 |
SoplexLp::SolveExitStatus SoplexLp::_solve() {
|
|
| 272 | 272 |
|
| 273 | 273 |
_clear_temporals(); |
| 274 | 274 |
|
| 275 | 275 |
soplex::SPxSolver::Status status = soplex->solve(); |
| 276 | 276 |
|
| 277 | 277 |
switch (status) {
|
| 278 | 278 |
case soplex::SPxSolver::OPTIMAL: |
| 279 | 279 |
case soplex::SPxSolver::INFEASIBLE: |
| 280 | 280 |
case soplex::SPxSolver::UNBOUNDED: |
| 281 | 281 |
return SOLVED; |
| 282 | 282 |
default: |
| 283 | 283 |
return UNSOLVED; |
| 284 | 284 |
} |
| 285 | 285 |
} |
| 286 | 286 |
|
| 287 |
|
|
| 287 |
SoplexLp::Value SoplexLp::_getPrimal(int i) const {
|
|
| 288 | 288 |
if (_primal_values.empty()) {
|
| 289 | 289 |
_primal_values.resize(soplex->nCols()); |
| 290 | 290 |
soplex::Vector pv(_primal_values.size(), &_primal_values.front()); |
| 291 | 291 |
soplex->getPrimal(pv); |
| 292 | 292 |
} |
| 293 | 293 |
return _primal_values[i]; |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 |
|
|
| 296 |
SoplexLp::Value SoplexLp::_getDual(int i) const {
|
|
| 297 | 297 |
if (_dual_values.empty()) {
|
| 298 | 298 |
_dual_values.resize(soplex->nRows()); |
| 299 | 299 |
soplex::Vector dv(_dual_values.size(), &_dual_values.front()); |
| 300 | 300 |
soplex->getDual(dv); |
| 301 | 301 |
} |
| 302 | 302 |
return _dual_values[i]; |
| 303 | 303 |
} |
| 304 | 304 |
|
| 305 |
|
|
| 305 |
SoplexLp::Value SoplexLp::_getPrimalValue() const {
|
|
| 306 | 306 |
return soplex->objValue(); |
| 307 | 307 |
} |
| 308 | 308 |
|
| 309 |
|
|
| 309 |
SoplexLp::VarStatus SoplexLp::_getColStatus(int i) const {
|
|
| 310 | 310 |
switch (soplex->getBasisColStatus(i)) {
|
| 311 | 311 |
case soplex::SPxSolver::BASIC: |
| 312 | 312 |
return BASIC; |
| 313 | 313 |
case soplex::SPxSolver::ON_UPPER: |
| 314 | 314 |
return UPPER; |
| 315 | 315 |
case soplex::SPxSolver::ON_LOWER: |
| 316 | 316 |
return LOWER; |
| 317 | 317 |
case soplex::SPxSolver::FIXED: |
| 318 | 318 |
return FIXED; |
| 319 | 319 |
case soplex::SPxSolver::ZERO: |
| 320 | 320 |
return FREE; |
| 321 | 321 |
default: |
| 322 | 322 |
LEMON_ASSERT(false, "Wrong column status"); |
| 323 | 323 |
return VarStatus(); |
| 324 | 324 |
} |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 |
|
|
| 327 |
SoplexLp::VarStatus SoplexLp::_getRowStatus(int i) const {
|
|
| 328 | 328 |
switch (soplex->getBasisRowStatus(i)) {
|
| 329 | 329 |
case soplex::SPxSolver::BASIC: |
| 330 | 330 |
return BASIC; |
| 331 | 331 |
case soplex::SPxSolver::ON_UPPER: |
| 332 | 332 |
return UPPER; |
| 333 | 333 |
case soplex::SPxSolver::ON_LOWER: |
| 334 | 334 |
return LOWER; |
| 335 | 335 |
case soplex::SPxSolver::FIXED: |
| 336 | 336 |
return FIXED; |
| 337 | 337 |
case soplex::SPxSolver::ZERO: |
| 338 | 338 |
return FREE; |
| 339 | 339 |
default: |
| 340 | 340 |
LEMON_ASSERT(false, "Wrong row status"); |
| 341 | 341 |
return VarStatus(); |
| 342 | 342 |
} |
| 343 | 343 |
} |
| 344 | 344 |
|
| 345 |
|
|
| 345 |
SoplexLp::Value SoplexLp::_getPrimalRay(int i) const {
|
|
| 346 | 346 |
if (_primal_ray.empty()) {
|
| 347 | 347 |
_primal_ray.resize(soplex->nCols()); |
| 348 | 348 |
soplex::Vector pv(_primal_ray.size(), &_primal_ray.front()); |
| 349 | 349 |
soplex->getDualfarkas(pv); |
| 350 | 350 |
} |
| 351 | 351 |
return _primal_ray[i]; |
| 352 | 352 |
} |
| 353 | 353 |
|
| 354 |
|
|
| 354 |
SoplexLp::Value SoplexLp::_getDualRay(int i) const {
|
|
| 355 | 355 |
if (_dual_ray.empty()) {
|
| 356 | 356 |
_dual_ray.resize(soplex->nRows()); |
| 357 | 357 |
soplex::Vector dv(_dual_ray.size(), &_dual_ray.front()); |
| 358 | 358 |
soplex->getDualfarkas(dv); |
| 359 | 359 |
} |
| 360 | 360 |
return _dual_ray[i]; |
| 361 | 361 |
} |
| 362 | 362 |
|
| 363 |
|
|
| 363 |
SoplexLp::ProblemType SoplexLp::_getPrimalType() const {
|
|
| 364 | 364 |
switch (soplex->status()) {
|
| 365 | 365 |
case soplex::SPxSolver::OPTIMAL: |
| 366 | 366 |
return OPTIMAL; |
| 367 | 367 |
case soplex::SPxSolver::UNBOUNDED: |
| 368 | 368 |
return UNBOUNDED; |
| 369 | 369 |
case soplex::SPxSolver::INFEASIBLE: |
| 370 | 370 |
return INFEASIBLE; |
| 371 | 371 |
default: |
| 372 | 372 |
return UNDEFINED; |
| 373 | 373 |
} |
| 374 | 374 |
} |
| 375 | 375 |
|
| 376 |
|
|
| 376 |
SoplexLp::ProblemType SoplexLp::_getDualType() const {
|
|
| 377 | 377 |
switch (soplex->status()) {
|
| 378 | 378 |
case soplex::SPxSolver::OPTIMAL: |
| 379 | 379 |
return OPTIMAL; |
| 380 | 380 |
case soplex::SPxSolver::UNBOUNDED: |
| 381 | 381 |
return UNBOUNDED; |
| 382 | 382 |
case soplex::SPxSolver::INFEASIBLE: |
| 383 | 383 |
return INFEASIBLE; |
| 384 | 384 |
default: |
| 385 | 385 |
return UNDEFINED; |
| 386 | 386 |
} |
| 387 | 387 |
} |
| 388 | 388 |
|
| 389 |
void |
|
| 389 |
void SoplexLp::_setSense(Sense sense) {
|
|
| 390 | 390 |
switch (sense) {
|
| 391 | 391 |
case MIN: |
| 392 | 392 |
soplex->changeSense(soplex::SPxSolver::MINIMIZE); |
| 393 | 393 |
break; |
| 394 | 394 |
case MAX: |
| 395 | 395 |
soplex->changeSense(soplex::SPxSolver::MAXIMIZE); |
| 396 | 396 |
} |
| 397 | 397 |
} |
| 398 | 398 |
|
| 399 |
|
|
| 399 |
SoplexLp::Sense SoplexLp::_getSense() const {
|
|
| 400 | 400 |
switch (soplex->spxSense()) {
|
| 401 | 401 |
case soplex::SPxSolver::MAXIMIZE: |
| 402 | 402 |
return MAX; |
| 403 | 403 |
case soplex::SPxSolver::MINIMIZE: |
| 404 | 404 |
return MIN; |
| 405 | 405 |
default: |
| 406 | 406 |
LEMON_ASSERT(false, "Wrong sense."); |
| 407 |
return |
|
| 407 |
return SoplexLp::Sense(); |
|
| 408 | 408 |
} |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 |
void |
|
| 411 |
void SoplexLp::_clear() {
|
|
| 412 | 412 |
soplex->clear(); |
| 413 | 413 |
_col_names.clear(); |
| 414 | 414 |
_col_names_ref.clear(); |
| 415 | 415 |
_row_names.clear(); |
| 416 | 416 |
_row_names_ref.clear(); |
| 417 | 417 |
cols.clear(); |
| 418 | 418 |
rows.clear(); |
| 419 | 419 |
_clear_temporals(); |
| 420 | 420 |
} |
| 421 | 421 |
|
| 422 | 422 |
} //namespace lemon |
| 423 | 423 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_SOPLEX_H |
| 20 | 20 |
#define LEMON_SOPLEX_H |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Header of the LEMON-SOPLEX lp solver interface. |
| 24 | 24 |
|
| 25 | 25 |
#include <vector> |
| 26 | 26 |
#include <string> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/lp_base.h> |
| 29 | 29 |
|
| 30 | 30 |
// Forward declaration |
| 31 | 31 |
namespace soplex {
|
| 32 | 32 |
class SoPlex; |
| 33 | 33 |
} |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \ingroup lp_group |
| 38 | 38 |
/// |
| 39 | 39 |
/// \brief Interface for the SOPLEX solver |
| 40 | 40 |
/// |
| 41 | 41 |
/// This class implements an interface for the SoPlex LP solver. |
| 42 | 42 |
/// The SoPlex library is an object oriented lp solver library |
| 43 | 43 |
/// developed at the Konrad-Zuse-Zentrum f�r Informationstechnik |
| 44 | 44 |
/// Berlin (ZIB). You can find detailed information about it at the |
| 45 | 45 |
/// <tt>http://soplex.zib.de</tt> address. |
| 46 |
class |
|
| 46 |
class SoplexLp : public LpSolver {
|
|
| 47 | 47 |
private: |
| 48 | 48 |
|
| 49 | 49 |
soplex::SoPlex* soplex; |
| 50 | 50 |
|
| 51 | 51 |
std::vector<std::string> _col_names; |
| 52 | 52 |
std::map<std::string, int> _col_names_ref; |
| 53 | 53 |
|
| 54 | 54 |
std::vector<std::string> _row_names; |
| 55 | 55 |
std::map<std::string, int> _row_names_ref; |
| 56 | 56 |
|
| 57 | 57 |
private: |
| 58 | 58 |
|
| 59 | 59 |
// these values cannot be retrieved element by element |
| 60 | 60 |
mutable std::vector<Value> _primal_values; |
| 61 | 61 |
mutable std::vector<Value> _dual_values; |
| 62 | 62 |
|
| 63 | 63 |
mutable std::vector<Value> _primal_ray; |
| 64 | 64 |
mutable std::vector<Value> _dual_ray; |
| 65 | 65 |
|
| 66 | 66 |
void _clear_temporals(); |
| 67 | 67 |
|
| 68 | 68 |
public: |
| 69 | 69 |
|
| 70 | 70 |
/// \e |
| 71 |
|
|
| 71 |
SoplexLp(); |
|
| 72 | 72 |
/// \e |
| 73 |
|
|
| 73 |
SoplexLp(const SoplexLp&); |
|
| 74 | 74 |
/// \e |
| 75 |
~ |
|
| 75 |
~SoplexLp(); |
|
| 76 | 76 |
|
| 77 | 77 |
protected: |
| 78 | 78 |
|
| 79 |
virtual LpSoplex* _newSolver() const; |
|
| 80 |
virtual LpSoplex* _cloneSolver() const; |
|
| 79 |
virtual SoplexLp* _newSolver() const; |
|
| 80 |
virtual SoplexLp* _cloneSolver() const; |
|
| 81 | 81 |
|
| 82 | 82 |
virtual const char* _solverName() const; |
| 83 | 83 |
|
| 84 | 84 |
virtual int _addCol(); |
| 85 | 85 |
virtual int _addRow(); |
| 86 | 86 |
|
| 87 | 87 |
virtual void _eraseCol(int i); |
| 88 | 88 |
virtual void _eraseRow(int i); |
| 89 | 89 |
|
| 90 | 90 |
virtual void _eraseColId(int i); |
| 91 | 91 |
virtual void _eraseRowId(int i); |
| 92 | 92 |
|
| 93 | 93 |
virtual void _getColName(int col, std::string& name) const; |
| 94 | 94 |
virtual void _setColName(int col, const std::string& name); |
| 95 | 95 |
virtual int _colByName(const std::string& name) const; |
| 96 | 96 |
|
| 97 | 97 |
virtual void _getRowName(int row, std::string& name) const; |
| 98 | 98 |
virtual void _setRowName(int row, const std::string& name); |
| 99 | 99 |
virtual int _rowByName(const std::string& name) const; |
| 100 | 100 |
|
| 101 | 101 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
| 102 | 102 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
| 103 | 103 |
|
| 104 | 104 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
| 105 | 105 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
| 106 | 106 |
|
| 107 | 107 |
virtual void _setCoeff(int row, int col, Value value); |
| 108 | 108 |
virtual Value _getCoeff(int row, int col) const; |
| 109 | 109 |
|
| 110 | 110 |
virtual void _setColLowerBound(int i, Value value); |
| 111 | 111 |
virtual Value _getColLowerBound(int i) const; |
| 112 | 112 |
virtual void _setColUpperBound(int i, Value value); |
| 113 | 113 |
virtual Value _getColUpperBound(int i) const; |
| 114 | 114 |
|
| 115 | 115 |
virtual void _setRowLowerBound(int i, Value value); |
| 116 | 116 |
virtual Value _getRowLowerBound(int i) const; |
| 117 | 117 |
virtual void _setRowUpperBound(int i, Value value); |
| 118 | 118 |
virtual Value _getRowUpperBound(int i) const; |
| 119 | 119 |
|
| 120 | 120 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
| 121 | 121 |
virtual void _getObjCoeffs(InsertIterator b) const; |
| 122 | 122 |
|
| 123 | 123 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 124 | 124 |
virtual Value _getObjCoeff(int i) const; |
| 125 | 125 |
|
| 126 | 126 |
virtual void _setSense(Sense sense); |
| 127 | 127 |
virtual Sense _getSense() const; |
| 128 | 128 |
|
| 129 | 129 |
virtual SolveExitStatus _solve(); |
| 130 | 130 |
virtual Value _getPrimal(int i) const; |
| 131 | 131 |
virtual Value _getDual(int i) const; |
| 132 | 132 |
|
| 133 | 133 |
virtual Value _getPrimalValue() const; |
| 134 | 134 |
|
| 135 | 135 |
virtual Value _getPrimalRay(int i) const; |
| 136 | 136 |
virtual Value _getDualRay(int i) const; |
| 137 | 137 |
|
| 138 | 138 |
virtual VarStatus _getColStatus(int i) const; |
| 139 | 139 |
virtual VarStatus _getRowStatus(int i) const; |
| 140 | 140 |
|
| 141 | 141 |
virtual ProblemType _getPrimalType() const; |
| 142 | 142 |
virtual ProblemType _getDualType() const; |
| 143 | 143 |
|
| 144 | 144 |
virtual void _clear(); |
| ... | ... |
@@ -301,103 +301,103 @@ |
| 301 | 301 |
check(e[x2] == 1, "The second coefficient should 1."); |
| 302 | 302 |
|
| 303 | 303 |
LpSolver::DualExpr de = lp.col(x1); |
| 304 | 304 |
check( de[upright] == 1, "The first coefficient should 1."); |
| 305 | 305 |
|
| 306 | 306 |
LpSolver* clp = lp.cloneSolver(); |
| 307 | 307 |
|
| 308 | 308 |
//Testing the problem retrieving routines |
| 309 | 309 |
check(clp->objCoeff(x1)==1,"First term should be 1 in the obj function!"); |
| 310 | 310 |
check(clp->sense() == clp->MAX,"This is a maximization!"); |
| 311 | 311 |
check(clp->coeff(upright,x1)==1,"The coefficient in question is 1!"); |
| 312 | 312 |
// std::cout<<lp.colLowerBound(x1)<<std::endl; |
| 313 | 313 |
check(clp->colLowerBound(x1)==0, |
| 314 | 314 |
"The lower bound for variable x1 should be 0."); |
| 315 | 315 |
check(clp->colUpperBound(x1)==LpSolver::INF, |
| 316 | 316 |
"The upper bound for variable x1 should be infty."); |
| 317 | 317 |
|
| 318 | 318 |
check(lp.rowLowerBound(upright)==-LpSolver::INF, |
| 319 | 319 |
"The lower bound for the first row should be -infty."); |
| 320 | 320 |
check(lp.rowUpperBound(upright)==1, |
| 321 | 321 |
"The upper bound for the first row should be 1."); |
| 322 | 322 |
e = clp->row(upright); |
| 323 | 323 |
check(e[x1] == 1, "The first coefficient should 1."); |
| 324 | 324 |
check(e[x2] == 1, "The second coefficient should 1."); |
| 325 | 325 |
|
| 326 | 326 |
de = clp->col(x1); |
| 327 | 327 |
check(de[upright] == 1, "The first coefficient should 1."); |
| 328 | 328 |
|
| 329 | 329 |
delete clp; |
| 330 | 330 |
|
| 331 | 331 |
//Maximization of x1+x2 |
| 332 | 332 |
//over the triangle with vertices (0,0) (0,1) (1,0) |
| 333 | 333 |
double expected_opt=1; |
| 334 | 334 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 335 | 335 |
|
| 336 | 336 |
//Minimization |
| 337 | 337 |
lp.sense(lp.MIN); |
| 338 | 338 |
expected_opt=0; |
| 339 | 339 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 340 | 340 |
|
| 341 | 341 |
//Vertex (-1,0) instead of (0,0) |
| 342 | 342 |
lp.colLowerBound(x1, -LpSolver::INF); |
| 343 | 343 |
expected_opt=-1; |
| 344 | 344 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 345 | 345 |
|
| 346 | 346 |
//Erase one constraint and return to maximization |
| 347 | 347 |
lp.erase(upright); |
| 348 | 348 |
lp.sense(lp.MAX); |
| 349 | 349 |
expected_opt=LpSolver::INF; |
| 350 | 350 |
solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt); |
| 351 | 351 |
|
| 352 | 352 |
//Infeasibilty |
| 353 | 353 |
lp.addRow(x1+x2 <=-2); |
| 354 | 354 |
solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt); |
| 355 | 355 |
|
| 356 | 356 |
} |
| 357 | 357 |
|
| 358 | 358 |
int main() |
| 359 | 359 |
{
|
| 360 | 360 |
LpSkeleton lp_skel; |
| 361 | 361 |
lpTest(lp_skel); |
| 362 | 362 |
|
| 363 | 363 |
#ifdef HAVE_GLPK |
| 364 | 364 |
{
|
| 365 |
|
|
| 365 |
GlpkLp lp_glpk1,lp_glpk2; |
|
| 366 | 366 |
lpTest(lp_glpk1); |
| 367 | 367 |
aTest(lp_glpk2); |
| 368 | 368 |
} |
| 369 | 369 |
#endif |
| 370 | 370 |
|
| 371 | 371 |
#ifdef HAVE_CPLEX |
| 372 | 372 |
try {
|
| 373 |
|
|
| 373 |
CplexLp lp_cplex1,lp_cplex2; |
|
| 374 | 374 |
lpTest(lp_cplex1); |
| 375 | 375 |
aTest(lp_cplex2); |
| 376 | 376 |
} catch (CplexEnv::LicenseError& error) {
|
| 377 | 377 |
#ifdef LEMON_FORCE_CPLEX_CHECK |
| 378 | 378 |
check(false, error.what()); |
| 379 | 379 |
#else |
| 380 | 380 |
std::cerr << error.what() << std::endl; |
| 381 | 381 |
std::cerr << "Cplex license check failed, lp check skipped" << std::endl; |
| 382 | 382 |
#endif |
| 383 | 383 |
} |
| 384 | 384 |
#endif |
| 385 | 385 |
|
| 386 | 386 |
#ifdef HAVE_SOPLEX |
| 387 | 387 |
{
|
| 388 |
|
|
| 388 |
SoplexLp lp_soplex1,lp_soplex2; |
|
| 389 | 389 |
lpTest(lp_soplex1); |
| 390 | 390 |
aTest(lp_soplex2); |
| 391 | 391 |
} |
| 392 | 392 |
#endif |
| 393 | 393 |
|
| 394 | 394 |
#ifdef HAVE_CLP |
| 395 | 395 |
{
|
| 396 |
|
|
| 396 |
ClpLp lp_clp1,lp_clp2; |
|
| 397 | 397 |
lpTest(lp_clp1); |
| 398 | 398 |
aTest(lp_clp2); |
| 399 | 399 |
} |
| 400 | 400 |
#endif |
| 401 | 401 |
|
| 402 | 402 |
return 0; |
| 403 | 403 |
} |
| ... | ... |
@@ -51,86 +51,86 @@ |
| 51 | 51 |
std::ostringstream sbuf; |
| 52 | 52 |
buf << "Wrong optimal value: the right optimum is " << exp_opt; |
| 53 | 53 |
check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); |
| 54 | 54 |
//+ecvt(exp_opt,2) |
| 55 | 55 |
} |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
void aTest(MipSolver& mip) |
| 59 | 59 |
{
|
| 60 | 60 |
//The following example is very simple |
| 61 | 61 |
|
| 62 | 62 |
|
| 63 | 63 |
typedef MipSolver::Row Row; |
| 64 | 64 |
typedef MipSolver::Col Col; |
| 65 | 65 |
|
| 66 | 66 |
|
| 67 | 67 |
|
| 68 | 68 |
Col x1 = mip.addCol(); |
| 69 | 69 |
Col x2 = mip.addCol(); |
| 70 | 70 |
|
| 71 | 71 |
|
| 72 | 72 |
//Objective function |
| 73 | 73 |
mip.obj(x1); |
| 74 | 74 |
|
| 75 | 75 |
mip.max(); |
| 76 | 76 |
|
| 77 | 77 |
|
| 78 | 78 |
//Unconstrained optimization |
| 79 | 79 |
mip.solve(); |
| 80 | 80 |
//Check it out! |
| 81 | 81 |
|
| 82 | 82 |
//Constraints |
| 83 | 83 |
mip.addRow(2*x1+x2 <=2); |
| 84 | 84 |
mip.addRow(x1-2*x2 <=0); |
| 85 | 85 |
|
| 86 | 86 |
//Nonnegativity of the variable x1 |
| 87 | 87 |
mip.colLowerBound(x1, 0); |
| 88 | 88 |
|
| 89 | 89 |
//Maximization of x1 |
| 90 | 90 |
//over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
| 91 | 91 |
double expected_opt=4.0/5.0; |
| 92 | 92 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 93 | 93 |
|
| 94 | 94 |
//Restrict x2 to integer |
| 95 | 95 |
mip.colType(x2,MipSolver::INTEGER); |
| 96 | 96 |
expected_opt=1.0/2.0; |
| 97 | 97 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
//Restrict both to integer |
| 101 | 101 |
mip.colType(x1,MipSolver::INTEGER); |
| 102 | 102 |
expected_opt=0; |
| 103 | 103 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 104 | 104 |
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
|
| 110 | 110 |
int main() |
| 111 | 111 |
{
|
| 112 | 112 |
|
| 113 | 113 |
#ifdef HAVE_GLPK |
| 114 | 114 |
{
|
| 115 |
|
|
| 115 |
GlpkMip mip1; |
|
| 116 | 116 |
aTest(mip1); |
| 117 | 117 |
} |
| 118 | 118 |
#endif |
| 119 | 119 |
|
| 120 | 120 |
#ifdef HAVE_CPLEX |
| 121 | 121 |
try {
|
| 122 |
|
|
| 122 |
CplexMip mip2; |
|
| 123 | 123 |
aTest(mip2); |
| 124 | 124 |
} catch (CplexEnv::LicenseError& error) {
|
| 125 | 125 |
#ifdef LEMON_FORCE_CPLEX_CHECK |
| 126 | 126 |
check(false, error.what()); |
| 127 | 127 |
#else |
| 128 | 128 |
std::cerr << error.what() << std::endl; |
| 129 | 129 |
std::cerr << "Cplex license check failed, lp check skipped" << std::endl; |
| 130 | 130 |
#endif |
| 131 | 131 |
} |
| 132 | 132 |
#endif |
| 133 | 133 |
|
| 134 | 134 |
return 0; |
| 135 | 135 |
|
| 136 | 136 |
} |
0 comments (0 inline)