| 1 | //----------------------------------------------------------------------------- |
| 2 | // name: OSI Interface for SoPlex >= 1.4.2c |
| 3 | // authors: Tobias Pfender |
| 4 | // Ambros Gleixner |
| 5 | // Wei Huang |
| 6 | // Konrad-Zuse-Zentrum Berlin (Germany) |
| 7 | // date: 01/16/2002 |
| 8 | // license: this file may be freely distributed under the terms of EPL |
| 9 | //----------------------------------------------------------------------------- |
| 10 | // Copyright (C) 2002, Tobias Pfender, International Business Machines |
| 11 | // Corporation and others. All Rights Reserved. |
| 12 | // Last edit: $Id: OsiSpxSolverInterface.cpp 2200 2019-01-06 23:02:02Z unxusr $ |
| 13 | |
| 14 | #include "CoinPragma.hpp" |
| 15 | |
| 16 | #include <iostream> |
| 17 | #include <fstream> |
| 18 | #include <cassert> |
| 19 | #include <string> |
| 20 | #include <numeric> |
| 21 | |
| 22 | #include "CoinError.hpp" |
| 23 | |
| 24 | #include "OsiRowCut.hpp" |
| 25 | #include "OsiColCut.hpp" |
| 26 | #include "CoinPackedMatrix.hpp" |
| 27 | #include "CoinWarmStartBasis.hpp" |
| 28 | |
| 29 | #ifndef SOPLEX_LEGACY |
| 30 | #define SOPLEX_LEGACY |
| 31 | #endif |
| 32 | |
| 33 | #include "soplex.h" |
| 34 | |
| 35 | // it's important to include this header after soplex.h |
| 36 | #include "OsiSpxSolverInterface.hpp" |
| 37 | |
| 38 | //############################################################################# |
| 39 | // A couple of helper functions |
| 40 | //############################################################################# |
| 41 | |
| 42 | inline void freeCacheDouble(double *&ptr) |
| 43 | { |
| 44 | if (ptr != NULL) { |
| 45 | delete[] ptr; |
| 46 | ptr = NULL; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | inline void freeCacheChar(char *&ptr) |
| 51 | { |
| 52 | if (ptr != NULL) { |
| 53 | delete[] ptr; |
| 54 | ptr = NULL; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | inline void freeCacheMatrix(CoinPackedMatrix *&ptr) |
| 59 | { |
| 60 | if (ptr != NULL) { |
| 61 | delete ptr; |
| 62 | ptr = NULL; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | inline void throwSPXerror(std::string error, std::string osimethod) |
| 67 | { |
| 68 | std::cout << "ERROR: " << error << " (" << osimethod << " in OsiSpxSolverInterface)" << std::endl; |
| 69 | throw CoinError(error.c_str(), osimethod.c_str(), "OsiSpxSolverInterface"); |
| 70 | } |
| 71 | |
| 72 | //############################################################################# |
| 73 | // Solve methods |
| 74 | //############################################################################# |
| 75 | |
| 76 | void OsiSpxSolverInterface::initialSolve() |
| 77 | { |
| 78 | bool takeHint; |
| 79 | OsiHintStrength strength; |
| 80 | |
| 81 | // by default we use dual simplex |
| 82 | // unless we get the hint to use primal simplex |
| 83 | bool dual = true; |
| 84 | getHintParam(OsiDoDualInInitial, takeHint, strength); |
| 85 | if (strength != OsiHintIgnore) |
| 86 | dual = takeHint; |
| 87 | |
| 88 | // always use column representation |
| 89 | if (soplex_->rep() != soplex::SPxSolver::COLUMN) |
| 90 | soplex_->setRep(soplex::SPxSolver::COLUMN); |
| 91 | |
| 92 | // set algorithm type |
| 93 | if (dual) { |
| 94 | if (soplex_->type() != soplex::SPxSolver::LEAVE) |
| 95 | soplex_->setType(soplex::SPxSolver::LEAVE); |
| 96 | } else { |
| 97 | if (soplex_->type() != soplex::SPxSolver::ENTER) |
| 98 | soplex_->setType(soplex::SPxSolver::ENTER); |
| 99 | } |
| 100 | |
| 101 | // set dual objective limit |
| 102 | double dualobjlimit; |
| 103 | OsiSolverInterface::getDblParam(OsiDualObjectiveLimit, dualobjlimit); |
| 104 | if (fabs(dualobjlimit) < getInfinity()) { |
| 105 | double objoffset; |
| 106 | OsiSolverInterface::getDblParam(OsiDualObjectiveLimit, objoffset); |
| 107 | dualobjlimit += objoffset; |
| 108 | } |
| 109 | soplex_->setTerminationValue(dualobjlimit); |
| 110 | |
| 111 | // solve |
| 112 | try { |
| 113 | soplex_->solve(); |
| 114 | } catch (soplex::SPxException e) { |
| 115 | *messageHandler() << "SoPlex initial solve failed with exception " << e.what() << CoinMessageEol; |
| 116 | #if (SOPLEX_VERSION >= 160) || (SOPLEX_SUBVERSION >= 7) |
| 117 | try { |
| 118 | *messageHandler() << "Retry with cleared basis" << CoinMessageEol; |
| 119 | soplex_->clearBasis(); |
| 120 | soplex_->solve(); |
| 121 | } catch (soplex::SPxException e) { |
| 122 | *messageHandler() << "SoPlex initial solve with cleared basis failed with exception " << e.what() << CoinMessageEol; |
| 123 | } |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | freeCachedResults(); |
| 128 | } |
| 129 | //----------------------------------------------------------------------------- |
| 130 | void OsiSpxSolverInterface::resolve() |
| 131 | { |
| 132 | bool takeHint; |
| 133 | OsiHintStrength strength; |
| 134 | |
| 135 | // by default we use dual simplex |
| 136 | // unless we get the hint to use primal simplex |
| 137 | bool dual = true; |
| 138 | getHintParam(OsiDoDualInResolve, takeHint, strength); |
| 139 | if (strength != OsiHintIgnore) |
| 140 | dual = takeHint; |
| 141 | |
| 142 | // always use column representation |
| 143 | if (soplex_->rep() != soplex::SPxSolver::COLUMN) |
| 144 | soplex_->setRep(soplex::SPxSolver::COLUMN); |
| 145 | |
| 146 | // set algorithm type |
| 147 | if (dual) { |
| 148 | if (soplex_->type() != soplex::SPxSolver::LEAVE) |
| 149 | soplex_->setType(soplex::SPxSolver::LEAVE); |
| 150 | } else { |
| 151 | if (soplex_->type() != soplex::SPxSolver::ENTER) |
| 152 | soplex_->setType(soplex::SPxSolver::ENTER); |
| 153 | } |
| 154 | |
| 155 | // set dual objective limit |
| 156 | double dualobjlimit; |
| 157 | OsiSolverInterface::getDblParam(OsiDualObjectiveLimit, dualobjlimit); |
| 158 | if (fabs(dualobjlimit) < getInfinity()) { |
| 159 | double objoffset; |
| 160 | OsiSolverInterface::getDblParam(OsiDualObjectiveLimit, objoffset); |
| 161 | dualobjlimit += objoffset; |
| 162 | } |
| 163 | soplex_->setTerminationValue(dualobjlimit); |
| 164 | |
| 165 | // solve |
| 166 | try { |
| 167 | soplex_->solve(); |
| 168 | } catch (soplex::SPxException e) { |
| 169 | *messageHandler() << "SoPlex resolve failed with exception " << e.what() << CoinMessageEol; |
| 170 | #if (SOPLEX_VERSION >= 160) || (SOPLEX_SUBVERSION >= 7) |
| 171 | try { |
| 172 | *messageHandler() << "Retry with cleared basis" << CoinMessageEol; |
| 173 | soplex_->clearBasis(); |
| 174 | soplex_->solve(); |
| 175 | } catch (soplex::SPxException e) { |
| 176 | *messageHandler() << "SoPlex resolve with cleared basis failed with exception " << e.what() << CoinMessageEol; |
| 177 | } |
| 178 | #endif |
| 179 | } |
| 180 | |
| 181 | freeCachedResults(); |
| 182 | } |
| 183 | //----------------------------------------------------------------------------- |
| 184 | void OsiSpxSolverInterface::branchAndBound() |
| 185 | { |
| 186 | throwSPXerror("SoPlex does not provide an internal branch and bound procedure", |
| 187 | "branchAndBound"); |
| 188 | } |
| 189 | |
| 190 | //############################################################################# |
| 191 | // Parameter related methods |
| 192 | //############################################################################# |
| 193 | |
| 194 | bool OsiSpxSolverInterface::setIntParam(OsiIntParam key, int value) |
| 195 | { |
| 196 | bool retval = false; |
| 197 | try { |
| 198 | switch (key) { |
| 199 | case OsiMaxNumIteration: |
| 200 | soplex_->setTerminationIter(value); |
| 201 | retval = true; |
| 202 | break; |
| 203 | case OsiMaxNumIterationHotStart: |
| 204 | if (value >= 0) { |
| 205 | hotStartMaxIteration_ = value; |
| 206 | retval = true; |
| 207 | } else |
| 208 | retval = false; |
| 209 | break; |
| 210 | case OsiLastIntParam: |
| 211 | retval = false; |
| 212 | break; |
| 213 | case OsiNameDiscipline: |
| 214 | retval = OsiSolverInterface::setIntParam(key, value); |
| 215 | break; |
| 216 | } |
| 217 | } catch (soplex::SPxException e) { |
| 218 | *messageHandler() << "OsiSpx::setDblParam failed with exception " << e.what() << CoinMessageEol; |
| 219 | retval = false; |
| 220 | } |
| 221 | |
| 222 | return retval; |
| 223 | } |
| 224 | |
| 225 | //----------------------------------------------------------------------------- |
| 226 | |
| 227 | bool OsiSpxSolverInterface::setDblParam(OsiDblParam key, double value) |
| 228 | { |
| 229 | bool retval = false; |
| 230 | try { |
| 231 | switch (key) { |
| 232 | case OsiDualObjectiveLimit: |
| 233 | retval = OsiSolverInterface::setDblParam(key, value); |
| 234 | break; |
| 235 | case OsiPrimalObjectiveLimit: |
| 236 | retval = OsiSolverInterface::setDblParam(key, value); |
| 237 | break; |
| 238 | case OsiDualTolerance: |
| 239 | // SoPlex doesn't support different deltas for primal and dual |
| 240 | soplex_->setDelta(value); |
| 241 | retval = true; |
| 242 | break; |
| 243 | case OsiPrimalTolerance: |
| 244 | // SoPlex doesn't support different deltas for primal and dual |
| 245 | soplex_->setDelta(value); |
| 246 | retval = true; |
| 247 | break; |
| 248 | case OsiObjOffset: |
| 249 | retval = OsiSolverInterface::setDblParam(key, value); |
| 250 | break; |
| 251 | case OsiLastDblParam: |
| 252 | retval = false; |
| 253 | break; |
| 254 | } |
| 255 | } catch (soplex::SPxException e) { |
| 256 | *messageHandler() << "OsiSpx::setDblParam failed with exception " << e.what() << CoinMessageEol; |
| 257 | retval = false; |
| 258 | } |
| 259 | |
| 260 | return retval; |
| 261 | } |
| 262 | |
| 263 | void OsiSpxSolverInterface::setTimeLimit(double value) |
| 264 | { |
| 265 | soplex_->setTerminationTime(value); |
| 266 | } |
| 267 | |
| 268 | //----------------------------------------------------------------------------- |
| 269 | |
| 270 | bool OsiSpxSolverInterface::getIntParam(OsiIntParam key, int &value) const |
| 271 | { |
| 272 | bool retval = false; |
| 273 | switch (key) { |
| 274 | case OsiMaxNumIteration: |
| 275 | value = soplex_->terminationIter(); |
| 276 | retval = true; |
| 277 | break; |
| 278 | case OsiMaxNumIterationHotStart: |
| 279 | value = hotStartMaxIteration_; |
| 280 | retval = true; |
| 281 | break; |
| 282 | case OsiLastIntParam: |
| 283 | retval = false; |
| 284 | break; |
| 285 | case OsiNameDiscipline: |
| 286 | retval = OsiSolverInterface::getIntParam(key, value); |
| 287 | break; |
| 288 | } |
| 289 | return retval; |
| 290 | } |
| 291 | |
| 292 | //----------------------------------------------------------------------------- |
| 293 | |
| 294 | bool OsiSpxSolverInterface::getDblParam(OsiDblParam key, double &value) const |
| 295 | { |
| 296 | bool retval = false; |
| 297 | switch (key) { |
| 298 | case OsiDualObjectiveLimit: |
| 299 | retval = OsiSolverInterface::getDblParam(key, value); |
| 300 | break; |
| 301 | case OsiPrimalObjectiveLimit: |
| 302 | retval = OsiSolverInterface::getDblParam(key, value); |
| 303 | break; |
| 304 | case OsiDualTolerance: |
| 305 | value = soplex_->delta(); |
| 306 | retval = true; |
| 307 | break; |
| 308 | case OsiPrimalTolerance: |
| 309 | value = soplex_->delta(); |
| 310 | retval = true; |
| 311 | break; |
| 312 | case OsiObjOffset: |
| 313 | retval = OsiSolverInterface::getDblParam(key, value); |
| 314 | break; |
| 315 | case OsiLastDblParam: |
| 316 | retval = false; |
| 317 | break; |
| 318 | } |
| 319 | return retval; |
| 320 | } |
| 321 | |
| 322 | bool OsiSpxSolverInterface::getStrParam(OsiStrParam key, std::string &value) const |
| 323 | { |
| 324 | switch (key) { |
| 325 | case OsiSolverName: |
| 326 | value = "SoPlex"; |
| 327 | return true; |
| 328 | default:; |
| 329 | } |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | double OsiSpxSolverInterface::getTimeLimit() const |
| 334 | { |
| 335 | return soplex_->terminationTime(); |
| 336 | } |
| 337 | |
| 338 | //############################################################################# |
| 339 | // Methods returning info on how the solution process terminated |
| 340 | //############################################################################# |
| 341 | |
| 342 | bool OsiSpxSolverInterface::isAbandoned() const |
| 343 | { |
| 344 | int stat = soplex_->status(); |
| 345 | |
| 346 | return (stat == soplex::SPxSolver::SINGULAR || stat == soplex::SPxSolver::ERROR); |
| 347 | } |
| 348 | |
| 349 | bool OsiSpxSolverInterface::isProvenOptimal() const |
| 350 | { |
| 351 | int stat = soplex_->status(); |
| 352 | return (stat == soplex::SPxSolver::OPTIMAL); |
| 353 | } |
| 354 | |
| 355 | bool OsiSpxSolverInterface::isProvenPrimalInfeasible() const |
| 356 | { |
| 357 | int stat = soplex_->status(); |
| 358 | |
| 359 | return (stat == soplex::SPxSolver::INFEASIBLE); |
| 360 | } |
| 361 | |
| 362 | bool OsiSpxSolverInterface::isProvenDualInfeasible() const |
| 363 | { |
| 364 | int stat = soplex_->status(); |
| 365 | |
| 366 | return (stat == soplex::SPxSolver::UNBOUNDED); |
| 367 | } |
| 368 | |
| 369 | bool OsiSpxSolverInterface::isDualObjectiveLimitReached() const |
| 370 | { |
| 371 | if (soplex_->status() == soplex::SPxSolver::ABORT_VALUE) |
| 372 | return true; |
| 373 | |
| 374 | return OsiSolverInterface::isDualObjectiveLimitReached(); |
| 375 | } |
| 376 | |
| 377 | bool OsiSpxSolverInterface::isIterationLimitReached() const |
| 378 | { |
| 379 | return (soplex_->status() == soplex::SPxSolver::ABORT_ITER); |
| 380 | } |
| 381 | |
| 382 | bool OsiSpxSolverInterface::isTimeLimitReached() const |
| 383 | { |
| 384 | return (soplex_->status() == soplex::SPxSolver::ABORT_TIME); |
| 385 | } |
| 386 | |
| 387 | //############################################################################# |
| 388 | // WarmStart related methods |
| 389 | //############################################################################# |
| 390 | |
| 391 | CoinWarmStart *OsiSpxSolverInterface::getWarmStart() const |
| 392 | { |
| 393 | CoinWarmStartBasis *ws = NULL; |
| 394 | int numcols = getNumCols(); |
| 395 | int numrows = getNumRows(); |
| 396 | int i; |
| 397 | |
| 398 | ws = new CoinWarmStartBasis(); |
| 399 | ws->setSize(numcols, numrows); |
| 400 | |
| 401 | if (soplex_->status() <= soplex::SPxSolver::NO_PROBLEM) |
| 402 | return ws; |
| 403 | |
| 404 | // The OSI standard assumes the artificial slack variables to have positive coefficients. SoPlex uses the convention |
| 405 | // Ax - s = 0, lhs <= s <= rhs, so we have to invert the ON_LOWER and ON_UPPER statuses. |
| 406 | for (i = 0; i < numrows; ++i) { |
| 407 | switch (soplex_->getBasisRowStatus(i)) { |
| 408 | case soplex::SPxSolver::BASIC: |
| 409 | ws->setArtifStatus(i, CoinWarmStartBasis::basic); |
| 410 | break; |
| 411 | case soplex::SPxSolver::FIXED: |
| 412 | case soplex::SPxSolver::ON_LOWER: |
| 413 | ws->setArtifStatus(i, CoinWarmStartBasis::atUpperBound); |
| 414 | break; |
| 415 | case soplex::SPxSolver::ON_UPPER: |
| 416 | ws->setArtifStatus(i, CoinWarmStartBasis::atLowerBound); |
| 417 | break; |
| 418 | case soplex::SPxSolver::ZERO: |
| 419 | ws->setArtifStatus(i, CoinWarmStartBasis::isFree); |
| 420 | break; |
| 421 | default: |
| 422 | throwSPXerror("invalid row status", "getWarmStart"); |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | for (i = 0; i < numcols; ++i) { |
| 428 | switch (soplex_->getBasisColStatus(i)) { |
| 429 | case soplex::SPxSolver::BASIC: |
| 430 | ws->setStructStatus(i, CoinWarmStartBasis::basic); |
| 431 | break; |
| 432 | case soplex::SPxSolver::FIXED: |
| 433 | case soplex::SPxSolver::ON_LOWER: |
| 434 | ws->setStructStatus(i, CoinWarmStartBasis::atLowerBound); |
| 435 | break; |
| 436 | case soplex::SPxSolver::ON_UPPER: |
| 437 | ws->setStructStatus(i, CoinWarmStartBasis::atUpperBound); |
| 438 | break; |
| 439 | case soplex::SPxSolver::ZERO: |
| 440 | ws->setStructStatus(i, CoinWarmStartBasis::isFree); |
| 441 | break; |
| 442 | default: |
| 443 | throwSPXerror("invalid column status", "getWarmStart"); |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return ws; |
| 449 | } |
| 450 | |
| 451 | //----------------------------------------------------------------------------- |
| 452 | |
| 453 | bool OsiSpxSolverInterface::setWarmStart(const CoinWarmStart *warmstart) |
| 454 | { |
| 455 | const CoinWarmStartBasis *ws = dynamic_cast< const CoinWarmStartBasis * >(warmstart); |
| 456 | int numcols, numrows, i; |
| 457 | soplex::SPxSolver::VarStatus *cstat, *rstat; |
| 458 | bool retval = false; |
| 459 | |
| 460 | if (!ws) |
| 461 | return false; |
| 462 | |
| 463 | numcols = ws->getNumStructural(); |
| 464 | numrows = ws->getNumArtificial(); |
| 465 | |
| 466 | if (numcols != getNumCols() || numrows != getNumRows()) |
| 467 | return false; |
| 468 | |
| 469 | cstat = new soplex::SPxSolver::VarStatus[numcols]; |
| 470 | rstat = new soplex::SPxSolver::VarStatus[numrows]; |
| 471 | |
| 472 | // The OSI standard assumes the artificial slack variables to have positive coefficients. SoPlex uses the convention |
| 473 | // Ax - s = 0, lhs <= s <= rhs, so we have to invert the atLowerBound and atUpperBound statuses. |
| 474 | for (i = 0; i < numrows; ++i) { |
| 475 | switch (ws->getArtifStatus(i)) { |
| 476 | case CoinWarmStartBasis::basic: |
| 477 | rstat[i] = soplex::SPxSolver::BASIC; |
| 478 | break; |
| 479 | case CoinWarmStartBasis::atLowerBound: |
| 480 | rstat[i] = soplex::SPxSolver::ON_UPPER; |
| 481 | break; |
| 482 | case CoinWarmStartBasis::atUpperBound: |
| 483 | rstat[i] = soplex::SPxSolver::ON_LOWER; |
| 484 | break; |
| 485 | case CoinWarmStartBasis::isFree: |
| 486 | rstat[i] = soplex::SPxSolver::ZERO; |
| 487 | break; |
| 488 | default: // unknown row status |
| 489 | retval = false; |
| 490 | goto TERMINATE; |
| 491 | } |
| 492 | } |
| 493 | for (i = 0; i < numcols; ++i) { |
| 494 | switch (ws->getStructStatus(i)) { |
| 495 | case CoinWarmStartBasis::basic: |
| 496 | cstat[i] = soplex::SPxSolver::BASIC; |
| 497 | break; |
| 498 | case CoinWarmStartBasis::atLowerBound: |
| 499 | cstat[i] = soplex::SPxSolver::ON_LOWER; |
| 500 | break; |
| 501 | case CoinWarmStartBasis::atUpperBound: |
| 502 | cstat[i] = soplex::SPxSolver::ON_UPPER; |
| 503 | break; |
| 504 | case CoinWarmStartBasis::isFree: |
| 505 | cstat[i] = soplex::SPxSolver::ZERO; |
| 506 | break; |
| 507 | default: // unknown column status |
| 508 | retval = false; |
| 509 | goto TERMINATE; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | try { |
| 514 | soplex_->setBasis(rstat, cstat); |
| 515 | } catch (soplex::SPxException e) { |
| 516 | std::cerr << "SoPlex setting starting basis failed with exception " << e.what() << std::endl; |
| 517 | retval = false; |
| 518 | goto TERMINATE; |
| 519 | } |
| 520 | retval = true; |
| 521 | |
| 522 | TERMINATE: |
| 523 | delete[] cstat; |
| 524 | delete[] rstat; |
| 525 | return retval; |
| 526 | } |
| 527 | |
| 528 | //############################################################################# |
| 529 | // Hotstart related methods (primarily used in strong branching) |
| 530 | //############################################################################# |
| 531 | |
| 532 | void OsiSpxSolverInterface::markHotStart() |
| 533 | { |
| 534 | int numcols, numrows; |
| 535 | |
| 536 | numcols = getNumCols(); |
| 537 | numrows = getNumRows(); |
| 538 | if (numcols > hotStartCStatSize_) { |
| 539 | delete[] reinterpret_cast< soplex::SPxSolver::VarStatus * >(hotStartCStat_); |
| 540 | hotStartCStatSize_ = static_cast< int >(1.2 * static_cast< double >(numcols)); // get some extra space for future hot starts |
| 541 | hotStartCStat_ = reinterpret_cast< void * >(new soplex::SPxSolver::VarStatus[hotStartCStatSize_]); |
| 542 | } |
| 543 | if (numrows > hotStartRStatSize_) { |
| 544 | delete[] reinterpret_cast< soplex::SPxSolver::VarStatus * >(hotStartRStat_); |
| 545 | hotStartRStatSize_ = static_cast< int >(1.2 * static_cast< double >(numrows)); // get some extra space for future hot starts |
| 546 | hotStartRStat_ = reinterpret_cast< void * >(new soplex::SPxSolver::VarStatus[hotStartRStatSize_]); |
| 547 | } |
| 548 | soplex_->getBasis(reinterpret_cast< soplex::SPxSolver::VarStatus * >(hotStartRStat_), reinterpret_cast< soplex::SPxSolver::VarStatus * >(hotStartCStat_)); |
| 549 | } |
| 550 | |
| 551 | void OsiSpxSolverInterface::solveFromHotStart() |
| 552 | { |
| 553 | int maxiter; |
| 554 | |
| 555 | assert(getNumCols() <= hotStartCStatSize_); |
| 556 | assert(getNumRows() <= hotStartRStatSize_); |
| 557 | |
| 558 | // @todo why the hotstart basis is not set here ????? |
| 559 | //soplex_->setBasis( reinterpret_cast<soplex::SPxSolver::VarStatus*>(hotStartRStat_), reinterpret_cast<soplex::SPxSolver::VarStatus*>(hotStartCStat_) ); |
| 560 | |
| 561 | maxiter = soplex_->terminationIter(); |
| 562 | soplex_->setTerminationIter(hotStartMaxIteration_); |
| 563 | |
| 564 | resolve(); |
| 565 | |
| 566 | soplex_->setTerminationIter(maxiter); |
| 567 | } |
| 568 | |
| 569 | void OsiSpxSolverInterface::unmarkHotStart() |
| 570 | { |
| 571 | // be lazy with deallocating memory and do nothing here, deallocate memory in the destructor |
| 572 | } |
| 573 | |
| 574 | //############################################################################# |
| 575 | // Problem information methods (original data) |
| 576 | //############################################################################# |
| 577 | |
| 578 | //------------------------------------------------------------------ |
| 579 | // Get number of rows, columns, elements, ... |
| 580 | //------------------------------------------------------------------ |
| 581 | int OsiSpxSolverInterface::getNumCols() const |
| 582 | { |
| 583 | return soplex_->nCols(); |
| 584 | } |
| 585 | int OsiSpxSolverInterface::getNumRows() const |
| 586 | { |
| 587 | return soplex_->nRows(); |
| 588 | } |
| 589 | int OsiSpxSolverInterface::getNumElements() const |
| 590 | { |
| 591 | #if 0 |
| 592 | return soplex_->nNzos(); |
| 593 | #else |
| 594 | int retVal = 0; |
| 595 | int nrows = getNumRows(); |
| 596 | int row; |
| 597 | for (row = 0; row < nrows; ++row) { |
| 598 | const soplex::SVector &rowvec = soplex_->rowVector(row); |
| 599 | retVal += rowvec.size(); |
| 600 | } |
| 601 | return retVal; |
| 602 | #endif |
| 603 | } |
| 604 | |
| 605 | //------------------------------------------------------------------ |
| 606 | // Get pointer to rim vectors |
| 607 | //------------------------------------------------------------------ |
| 608 | |
| 609 | const double *OsiSpxSolverInterface::getColLower() const |
| 610 | { |
| 611 | const double *retVal = NULL; |
| 612 | if (getNumCols() != 0) |
| 613 | retVal = soplex_->lower().get_const_ptr(); |
| 614 | return retVal; |
| 615 | } |
| 616 | //------------------------------------------------------------------ |
| 617 | const double *OsiSpxSolverInterface::getColUpper() const |
| 618 | { |
| 619 | const double *retVal = NULL; |
| 620 | if (getNumCols() != 0) |
| 621 | retVal = soplex_->upper().get_const_ptr(); |
| 622 | return retVal; |
| 623 | } |
| 624 | //------------------------------------------------------------------ |
| 625 | const char *OsiSpxSolverInterface::getRowSense() const |
| 626 | { |
| 627 | if (rowsense_ == NULL) { |
| 628 | // rowsense is determined with rhs, so invoke rhs |
| 629 | getRightHandSide(); |
| 630 | assert(rowsense_ != NULL || getNumRows() == 0); |
| 631 | } |
| 632 | return rowsense_; |
| 633 | } |
| 634 | //------------------------------------------------------------------ |
| 635 | const double *OsiSpxSolverInterface::getRightHandSide() const |
| 636 | { |
| 637 | if (rhs_ == NULL) { |
| 638 | int nrows = getNumRows(); |
| 639 | if (nrows > 0) { |
| 640 | int row; |
| 641 | |
| 642 | assert(rowrange_ == NULL); |
| 643 | assert(rowsense_ == NULL); |
| 644 | |
| 645 | rhs_ = new double[nrows]; |
| 646 | rowrange_ = new double[nrows]; |
| 647 | rowsense_ = new char[nrows]; |
| 648 | |
| 649 | for (row = 0; row < nrows; ++row) |
| 650 | convertBoundToSense(soplex_->lhs(row), soplex_->rhs(row), |
| 651 | rowsense_[row], rhs_[row], rowrange_[row]); |
| 652 | } |
| 653 | } |
| 654 | return rhs_; |
| 655 | } |
| 656 | //------------------------------------------------------------------ |
| 657 | const double *OsiSpxSolverInterface::getRowRange() const |
| 658 | { |
| 659 | if (rowrange_ == NULL) { |
| 660 | // rowrange is determined with rhs, so invoke rhs |
| 661 | getRightHandSide(); |
| 662 | assert(rowrange_ != NULL || getNumRows() == 0); |
| 663 | } |
| 664 | return rowrange_; |
| 665 | } |
| 666 | //------------------------------------------------------------------ |
| 667 | const double *OsiSpxSolverInterface::getRowLower() const |
| 668 | { |
| 669 | const double *retVal = NULL; |
| 670 | if (getNumRows() != 0) |
| 671 | retVal = soplex_->lhs().get_const_ptr(); |
| 672 | return retVal; |
| 673 | } |
| 674 | //------------------------------------------------------------------ |
| 675 | const double *OsiSpxSolverInterface::getRowUpper() const |
| 676 | { |
| 677 | const double *retVal = NULL; |
| 678 | if (getNumRows() != 0) |
| 679 | retVal = soplex_->rhs().get_const_ptr(); |
| 680 | return retVal; |
| 681 | } |
| 682 | //------------------------------------------------------------------ |
| 683 | const double *OsiSpxSolverInterface::getObjCoefficients() const |
| 684 | { |
| 685 | const double *retVal = NULL; |
| 686 | if (obj_ == NULL) { |
| 687 | if (getNumCols() != 0) { |
| 688 | obj_ = new soplex::DVector(getNumCols()); |
| 689 | soplex_->getObj(*obj_); |
| 690 | retVal = obj_->get_const_ptr(); |
| 691 | } |
| 692 | } else { |
| 693 | retVal = obj_->get_const_ptr(); |
| 694 | } |
| 695 | return retVal; |
| 696 | } |
| 697 | //------------------------------------------------------------------ |
| 698 | double OsiSpxSolverInterface::getObjSense() const |
| 699 | { |
| 700 | switch (soplex_->spxSense()) { |
| 701 | case soplex::SPxLP::MINIMIZE: |
| 702 | return +1.0; |
| 703 | case soplex::SPxLP::MAXIMIZE: |
| 704 | return -1.0; |
| 705 | default: |
| 706 | throwSPXerror("invalid optimization sense", "getObjSense"); |
| 707 | return 0.0; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | //------------------------------------------------------------------ |
| 712 | // Return information on integrality |
| 713 | //------------------------------------------------------------------ |
| 714 | |
| 715 | bool OsiSpxSolverInterface::isContinuous(int colNumber) const |
| 716 | { |
| 717 | #if SOPLEX_VERSION >= 300 |
| 718 | return (spxintvars_->pos(colNumber) < 0); |
| 719 | #else |
| 720 | return (spxintvars_->number(colNumber) < 0); |
| 721 | #endif |
| 722 | } |
| 723 | |
| 724 | //------------------------------------------------------------------ |
| 725 | // Row and column copies of the matrix ... |
| 726 | //------------------------------------------------------------------ |
| 727 | |
| 728 | const CoinPackedMatrix *OsiSpxSolverInterface::getMatrixByRow() const |
| 729 | { |
| 730 | if (matrixByRow_ == NULL) { |
| 731 | int nrows = getNumRows(); |
| 732 | int ncols = getNumCols(); |
| 733 | int nelems = getNumElements(); |
| 734 | double *elements = new double[nelems]; |
| 735 | int *indices = new int[nelems]; |
| 736 | int *starts = new int[nrows + 1]; |
| 737 | int *len = new int[nrows]; |
| 738 | int row, i, elem; |
| 739 | |
| 740 | elem = 0; |
| 741 | for (row = 0; row < nrows; ++row) { |
| 742 | const soplex::SVector &rowvec = soplex_->rowVector(row); |
| 743 | starts[row] = elem; |
| 744 | len[row] = rowvec.size(); |
| 745 | for (i = 0; i < len[row]; ++i, ++elem) { |
| 746 | assert(elem < nelems); |
| 747 | elements[elem] = rowvec.value(i); |
| 748 | indices[elem] = rowvec.index(i); |
| 749 | } |
| 750 | } |
| 751 | starts[nrows] = elem; |
| 752 | assert(elem == nelems); |
| 753 | |
| 754 | matrixByRow_ = new CoinPackedMatrix(); |
| 755 | matrixByRow_->assignMatrix(false /* not column ordered */, |
| 756 | ncols, nrows, nelems, |
| 757 | elements, indices, starts, len); |
| 758 | } |
| 759 | return matrixByRow_; |
| 760 | } |
| 761 | |
| 762 | //------------------------------------------------------------------ |
| 763 | |
| 764 | const CoinPackedMatrix *OsiSpxSolverInterface::getMatrixByCol() const |
| 765 | { |
| 766 | if (matrixByCol_ == NULL) { |
| 767 | int nrows = getNumRows(); |
| 768 | int ncols = getNumCols(); |
| 769 | int nelems = getNumElements(); |
| 770 | double *elements = new double[nelems]; |
| 771 | int *indices = new int[nelems]; |
| 772 | int *starts = new int[ncols + 1]; |
| 773 | int *len = new int[ncols]; |
| 774 | int col, i, elem; |
| 775 | |
| 776 | elem = 0; |
| 777 | for (col = 0; col < ncols; ++col) { |
| 778 | const soplex::SVector &colvec = soplex_->colVector(col); |
| 779 | starts[col] = elem; |
| 780 | len[col] = colvec.size(); |
| 781 | for (i = 0; i < len[col]; ++i, ++elem) { |
| 782 | assert(elem < nelems); |
| 783 | elements[elem] = colvec.value(i); |
| 784 | indices[elem] = colvec.index(i); |
| 785 | } |
| 786 | } |
| 787 | starts[ncols] = elem; |
| 788 | assert(elem == nelems); |
| 789 | |
| 790 | matrixByCol_ = new CoinPackedMatrix(); |
| 791 | matrixByCol_->assignMatrix(true /* column ordered */, |
| 792 | nrows, ncols, nelems, |
| 793 | elements, indices, starts, len); |
| 794 | } |
| 795 | return matrixByCol_; |
| 796 | } |
| 797 | |
| 798 | //------------------------------------------------------------------ |
| 799 | // Get solver's value for infinity |
| 800 | //------------------------------------------------------------------ |
| 801 | double OsiSpxSolverInterface::getInfinity() const |
| 802 | { |
| 803 | return soplex::infinity; |
| 804 | } |
| 805 | |
| 806 | //############################################################################# |
| 807 | // Problem information methods (results) |
| 808 | //############################################################################# |
| 809 | |
| 810 | // *FIXME*: what should be done if a certain vector doesn't exist??? |
| 811 | |
| 812 | const double *OsiSpxSolverInterface::getColSolution() const |
| 813 | { |
| 814 | if (colsol_ == NULL) { |
| 815 | int ncols = getNumCols(); |
| 816 | if (ncols > 0) { |
| 817 | colsol_ = new soplex::DVector(ncols); |
| 818 | if (isProvenOptimal()) |
| 819 | soplex_->getPrimal(*colsol_); |
| 820 | else |
| 821 | *colsol_ = soplex_->lower(); |
| 822 | } else |
| 823 | return NULL; |
| 824 | } |
| 825 | return colsol_->get_const_ptr(); |
| 826 | } |
| 827 | //------------------------------------------------------------------ |
| 828 | const double *OsiSpxSolverInterface::getRowPrice() const |
| 829 | { |
| 830 | if (rowsol_ == NULL) { |
| 831 | int nrows = getNumRows(); |
| 832 | if (nrows > 0) { |
| 833 | rowsol_ = new soplex::DVector(nrows); |
| 834 | if (isProvenOptimal()) |
| 835 | soplex_->getDual(*rowsol_); |
| 836 | else |
| 837 | rowsol_->clear(); |
| 838 | } else |
| 839 | return NULL; |
| 840 | } |
| 841 | return rowsol_->get_const_ptr(); |
| 842 | } |
| 843 | //------------------------------------------------------------------ |
| 844 | const double *OsiSpxSolverInterface::getReducedCost() const |
| 845 | { |
| 846 | if (redcost_ == NULL) { |
| 847 | int ncols = getNumCols(); |
| 848 | if (ncols > 0) { |
| 849 | redcost_ = new soplex::DVector(ncols); |
| 850 | if (isProvenOptimal()) |
| 851 | soplex_->getRedCost(*redcost_); |
| 852 | else |
| 853 | redcost_->clear(); |
| 854 | } else |
| 855 | return NULL; |
| 856 | } |
| 857 | return redcost_->get_const_ptr(); |
| 858 | } |
| 859 | //------------------------------------------------------------------ |
| 860 | const double *OsiSpxSolverInterface::getRowActivity() const |
| 861 | { |
| 862 | if (rowact_ == NULL) { |
| 863 | int nrows = getNumRows(); |
| 864 | if (nrows > 0) { |
| 865 | rowact_ = new soplex::DVector(nrows); |
| 866 | if (isProvenOptimal()) |
| 867 | soplex_->getSlacks(*rowact_); |
| 868 | else |
| 869 | rowact_->clear(); |
| 870 | } else |
| 871 | return NULL; |
| 872 | } |
| 873 | return rowact_->get_const_ptr(); |
| 874 | } |
| 875 | //------------------------------------------------------------------ |
| 876 | double OsiSpxSolverInterface::getObjValue() const |
| 877 | { |
| 878 | double objval; |
| 879 | |
| 880 | switch (soplex_->status()) { |
| 881 | case soplex::SPxSolver::OPTIMAL: |
| 882 | case soplex::SPxSolver::UNBOUNDED: |
| 883 | case soplex::SPxSolver::INFEASIBLE: |
| 884 | objval = soplex_->objValue(); |
| 885 | break; |
| 886 | default: { |
| 887 | const double *colsol = getColSolution(); |
| 888 | const double *objcoef = getObjCoefficients(); |
| 889 | int ncols = getNumCols(); |
| 890 | objval = 0.0; |
| 891 | for (int i = 0; i < ncols; ++i) |
| 892 | objval += colsol[i] * objcoef[i]; |
| 893 | break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | // Adjust objective function value by constant term in objective function |
| 898 | double objOffset; |
| 899 | getDblParam(OsiObjOffset, objOffset); |
| 900 | objval = objval - objOffset; |
| 901 | |
| 902 | return objval; |
| 903 | } |
| 904 | //------------------------------------------------------------------ |
| 905 | int OsiSpxSolverInterface::getIterationCount() const |
| 906 | { |
| 907 | return soplex_->iteration(); |
| 908 | } |
| 909 | //------------------------------------------------------------------ |
| 910 | std::vector< double * > OsiSpxSolverInterface::getDualRays(int maxNumRays, |
| 911 | bool fullRay) const |
| 912 | { |
| 913 | if (fullRay == true) { |
| 914 | throw CoinError("Full dual rays not yet implemented.", "getDualRays", |
| 915 | "OsiSpxSolverInterface"); |
| 916 | } |
| 917 | |
| 918 | std::vector< double * > ret = std::vector< double * >(); |
| 919 | |
| 920 | if (soplex_->status() == soplex::SPxSolver::INFEASIBLE && maxNumRays > 0) { |
| 921 | ret.push_back(new double[getNumRows()]); |
| 922 | soplex::Vector proof(getNumRows(), ret[0]); |
| 923 | soplex_->getDualfarkas(proof); |
| 924 | |
| 925 | for (int i = 0; i < getNumRows(); ++i) |
| 926 | ret[0][i] *= -1.0; |
| 927 | } |
| 928 | |
| 929 | return ret; |
| 930 | } |
| 931 | //------------------------------------------------------------------ |
| 932 | std::vector< double * > OsiSpxSolverInterface::getPrimalRays(int maxNumRays) const |
| 933 | { |
| 934 | // *FIXME* : must write the method |
| 935 | throw CoinError("method is not yet written", "getPrimalRays", |
| 936 | "OsiSpxSolverInterface"); |
| 937 | return std::vector< double * >(); |
| 938 | } |
| 939 | |
| 940 | //############################################################################# |
| 941 | // Problem modifying methods (rim vectors) |
| 942 | //############################################################################# |
| 943 | |
| 944 | void OsiSpxSolverInterface::setObjCoeff(int elementIndex, double elementValue) |
| 945 | { |
| 946 | soplex_->changeObj(elementIndex, elementValue); |
| 947 | freeCachedData(OsiSpxSolverInterface::FREECACHED_COLUMN); |
| 948 | } |
| 949 | |
| 950 | void OsiSpxSolverInterface::setColLower(int elementIndex, double elementValue) |
| 951 | { |
| 952 | soplex_->changeLower(elementIndex, elementValue); |
| 953 | freeCachedData(OsiSpxSolverInterface::FREECACHED_COLUMN); |
| 954 | } |
| 955 | //----------------------------------------------------------------------------- |
| 956 | void OsiSpxSolverInterface::setColUpper(int elementIndex, double elementValue) |
| 957 | { |
| 958 | soplex_->changeUpper(elementIndex, elementValue); |
| 959 | freeCachedData(OsiSpxSolverInterface::FREECACHED_COLUMN); |
| 960 | } |
| 961 | //----------------------------------------------------------------------------- |
| 962 | void OsiSpxSolverInterface::setColBounds(int elementIndex, double lower, double upper) |
| 963 | { |
| 964 | soplex_->changeBounds(elementIndex, lower, upper); |
| 965 | freeCachedData(OsiSpxSolverInterface::FREECACHED_COLUMN); |
| 966 | } |
| 967 | //----------------------------------------------------------------------------- |
| 968 | void OsiSpxSolverInterface::setRowLower(int i, double elementValue) |
| 969 | { |
| 970 | soplex_->changeLhs(i, elementValue); |
| 971 | freeCachedData(OsiSpxSolverInterface::FREECACHED_ROW); |
| 972 | } |
| 973 | //----------------------------------------------------------------------------- |
| 974 | void OsiSpxSolverInterface::setRowUpper(int i, double elementValue) |
| 975 | { |
| 976 | soplex_->changeRhs(i, elementValue); |
| 977 | freeCachedData(OsiSpxSolverInterface::FREECACHED_ROW); |
| 978 | } |
| 979 | //----------------------------------------------------------------------------- |
| 980 | void OsiSpxSolverInterface::setRowBounds(int elementIndex, double lower, double upper) |
| 981 | { |
| 982 | soplex_->changeRange(elementIndex, lower, upper); |
| 983 | freeCachedData(OsiSpxSolverInterface::FREECACHED_ROW); |
| 984 | } |
| 985 | //----------------------------------------------------------------------------- |
| 986 | void OsiSpxSolverInterface::setRowType(int i, char sense, double rightHandSide, |
| 987 | double range) |
| 988 | { |
| 989 | double lower = 0.0; |
| 990 | double upper = 0.0; |
| 991 | |
| 992 | convertSenseToBound(sense, rightHandSide, range, lower, upper); |
| 993 | setRowBounds(i, lower, upper); |
| 994 | } |
| 995 | //############################################################################# |
| 996 | void OsiSpxSolverInterface::setContinuous(int index) |
| 997 | { |
| 998 | #if SOPLEX_VERSION >= 300 |
| 999 | int pos = spxintvars_->pos(index); |
| 1000 | #else |
| 1001 | int pos = spxintvars_->number(index); |
| 1002 | #endif |
| 1003 | if (pos >= 0) { |
| 1004 | spxintvars_->remove(pos); |
| 1005 | freeCachedData(OsiSpxSolverInterface::FREECACHED_COLUMN); |
| 1006 | } |
| 1007 | } |
| 1008 | //----------------------------------------------------------------------------- |
| 1009 | void OsiSpxSolverInterface::setInteger(int index) |
| 1010 | { |
| 1011 | #if SOPLEX_VERSION >= 300 |
| 1012 | int pos = spxintvars_->pos(index); |
| 1013 | #else |
| 1014 | int pos = spxintvars_->number(index); |
| 1015 | #endif |
| 1016 | if (pos < 0) { |
| 1017 | spxintvars_->addIdx(index); |
| 1018 | freeCachedData(OsiSpxSolverInterface::FREECACHED_COLUMN); |
| 1019 | } |
| 1020 | } |
| 1021 | //############################################################################# |
| 1022 | |
| 1023 | void OsiSpxSolverInterface::setObjSense(double s) |
| 1024 | { |
| 1025 | if (s != getObjSense()) { |
| 1026 | if (s == +1.0) |
| 1027 | soplex_->changeSense(soplex::SPxLP::MINIMIZE); |
| 1028 | else |
| 1029 | soplex_->changeSense(soplex::SPxLP::MAXIMIZE); |
| 1030 | freeCachedData(OsiSpxSolverInterface::FREECACHED_RESULTS); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | //----------------------------------------------------------------------------- |
| 1035 | |
| 1036 | void OsiSpxSolverInterface::setColSolution(const double *cs) |
| 1037 | { |
| 1038 | int col; |
| 1039 | int ncols = getNumCols(); |
| 1040 | |
| 1041 | if (colsol_ != NULL) |
| 1042 | delete colsol_; |
| 1043 | |
| 1044 | if (ncols > 0 && cs != NULL) { |
| 1045 | colsol_ = new soplex::DVector(ncols); |
| 1046 | for (col = 0; col < ncols; ++col) |
| 1047 | (*colsol_)[col] = cs[col]; |
| 1048 | } else |
| 1049 | colsol_ = NULL; |
| 1050 | } |
| 1051 | |
| 1052 | //----------------------------------------------------------------------------- |
| 1053 | |
| 1054 | void OsiSpxSolverInterface::setRowPrice(const double *rs) |
| 1055 | { |
| 1056 | int row; |
| 1057 | int nrows = getNumRows(); |
| 1058 | |
| 1059 | if (rowsol_ != NULL) |
| 1060 | delete rowsol_; |
| 1061 | |
| 1062 | if (nrows > 0 && rs != NULL) { |
| 1063 | rowsol_ = new soplex::DVector(nrows); |
| 1064 | for (row = 0; row < nrows; ++row) |
| 1065 | (*rowsol_)[row] = rs[row]; |
| 1066 | } else |
| 1067 | rowsol_ = NULL; |
| 1068 | } |
| 1069 | |
| 1070 | //############################################################################# |
| 1071 | // Problem modifying methods (matrix) |
| 1072 | //############################################################################# |
| 1073 | void OsiSpxSolverInterface::addCol(const CoinPackedVectorBase &vec, |
| 1074 | const double collb, const double colub, |
| 1075 | const double obj) |
| 1076 | { |
| 1077 | soplex::DSVector colvec; |
| 1078 | |
| 1079 | colvec.add(vec.getNumElements(), vec.getIndices(), vec.getElements()); |
| 1080 | soplex_->addCol(soplex::LPCol(obj, colvec, colub, collb)); |
| 1081 | freeCachedData(OsiSpxSolverInterface::KEEPCACHED_ROW); |
| 1082 | } |
| 1083 | //----------------------------------------------------------------------------- |
| 1084 | void OsiSpxSolverInterface::deleteCols(const int num, const int *columnIndices) |
| 1085 | { |
| 1086 | soplex_->removeCols(const_cast< int * >(columnIndices), num); |
| 1087 | freeCachedData(OsiSpxSolverInterface::KEEPCACHED_ROW); |
| 1088 | |
| 1089 | // took from OsiClp for updating names |
| 1090 | int nameDiscipline; |
| 1091 | getIntParam(OsiNameDiscipline, nameDiscipline); |
| 1092 | if (num && nameDiscipline) { |
| 1093 | // Very clumsy (and inefficient) - need to sort and then go backwards in ? chunks |
| 1094 | int *indices = CoinCopyOfArray(columnIndices, num); |
| 1095 | std::sort(indices, indices + num); |
| 1096 | int num2 = num; |
| 1097 | while (num2) { |
| 1098 | int next = indices[num2 - 1]; |
| 1099 | int firstDelete = num2 - 1; |
| 1100 | int i; |
| 1101 | for (i = num2 - 2; i >= 0; --i) { |
| 1102 | if (indices[i] + 1 == next) { |
| 1103 | --next; |
| 1104 | firstDelete = i; |
| 1105 | } else { |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | OsiSolverInterface::deleteColNames(indices[firstDelete], num2 - firstDelete); |
| 1110 | num2 = firstDelete; |
| 1111 | assert(num2 >= 0); |
| 1112 | } |
| 1113 | delete[] indices; |
| 1114 | } |
| 1115 | } |
| 1116 | //----------------------------------------------------------------------------- |
| 1117 | void OsiSpxSolverInterface::addRow(const CoinPackedVectorBase &vec, |
| 1118 | const double rowlb, const double rowub) |
| 1119 | { |
| 1120 | soplex::DSVector rowvec; |
| 1121 | |
| 1122 | rowvec.add(vec.getNumElements(), vec.getIndices(), vec.getElements()); |
| 1123 | soplex_->addRow(soplex::LPRow(rowlb, rowvec, rowub)); |
| 1124 | freeCachedData(OsiSpxSolverInterface::KEEPCACHED_COLUMN); |
| 1125 | } |
| 1126 | //----------------------------------------------------------------------------- |
| 1127 | void OsiSpxSolverInterface::addRow(const CoinPackedVectorBase &vec, |
| 1128 | const char rowsen, const double rowrhs, |
| 1129 | const double rowrng) |
| 1130 | { |
| 1131 | double rowlb = 0.0; |
| 1132 | double rowub = 0.0; |
| 1133 | |
| 1134 | convertSenseToBound(rowsen, rowrhs, rowrng, rowlb, rowub); |
| 1135 | addRow(vec, rowlb, rowub); |
| 1136 | } |
| 1137 | //----------------------------------------------------------------------------- |
| 1138 | void OsiSpxSolverInterface::deleteRows(const int num, const int *rowIndices) |
| 1139 | { |
| 1140 | soplex_->removeRows(const_cast< int * >(rowIndices), num); |
| 1141 | freeCachedData(OsiSpxSolverInterface::KEEPCACHED_COLUMN); |
| 1142 | |
| 1143 | // took from OsiClp for updating names |
| 1144 | int nameDiscipline; |
| 1145 | getIntParam(OsiNameDiscipline, nameDiscipline); |
| 1146 | if (num && nameDiscipline) { |
| 1147 | // Very clumsy (and inefficient) - need to sort and then go backwards in ? chunks |
| 1148 | int *indices = CoinCopyOfArray(rowIndices, num); |
| 1149 | std::sort(indices, indices + num); |
| 1150 | int num2 = num; |
| 1151 | while (num2) { |
| 1152 | int next = indices[num2 - 1]; |
| 1153 | int firstDelete = num2 - 1; |
| 1154 | int i; |
| 1155 | for (i = num2 - 2; i >= 0; --i) { |
| 1156 | if (indices[i] + 1 == next) { |
| 1157 | --next; |
| 1158 | firstDelete = i; |
| 1159 | } else { |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | OsiSolverInterface::deleteRowNames(indices[firstDelete], num2 - firstDelete); |
| 1164 | num2 = firstDelete; |
| 1165 | assert(num2 >= 0); |
| 1166 | } |
| 1167 | delete[] indices; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | //############################################################################# |
| 1172 | // Methods to input a problem |
| 1173 | //############################################################################# |
| 1174 | |
| 1175 | void OsiSpxSolverInterface::loadProblem(const CoinPackedMatrix &matrix, |
| 1176 | const double *collb, const double *colub, |
| 1177 | const double *obj, |
| 1178 | const double *rowlb, const double *rowub) |
| 1179 | { |
| 1180 | int ncols = matrix.getNumCols(); |
| 1181 | int nrows = matrix.getNumRows(); |
| 1182 | const int *length = matrix.getVectorLengths(); |
| 1183 | const int *start = matrix.getVectorStarts(); |
| 1184 | const double *elem = matrix.getElements(); |
| 1185 | const int *index = matrix.getIndices(); |
| 1186 | double *thecollb, *thecolub, *theobj, *therowlb, *therowub; |
| 1187 | |
| 1188 | // create defaults if parameter is NULL |
| 1189 | if (collb == NULL) { |
| 1190 | thecollb = new double[ncols]; |
| 1191 | CoinFillN(thecollb, ncols, 0.0); |
| 1192 | } else |
| 1193 | thecollb = const_cast< double * >(collb); |
| 1194 | if (colub == NULL) { |
| 1195 | thecolub = new double[ncols]; |
| 1196 | CoinFillN(thecolub, ncols, getInfinity()); |
| 1197 | } else |
| 1198 | thecolub = const_cast< double * >(colub); |
| 1199 | if (obj == NULL) { |
| 1200 | theobj = new double[ncols]; |
| 1201 | CoinFillN(theobj, ncols, 0.0); |
| 1202 | } else |
| 1203 | theobj = const_cast< double * >(obj); |
| 1204 | if (rowlb == NULL) { |
| 1205 | therowlb = new double[nrows]; |
| 1206 | CoinFillN(therowlb, nrows, -getInfinity()); |
| 1207 | } else |
| 1208 | therowlb = const_cast< double * >(rowlb); |
| 1209 | if (rowub == NULL) { |
| 1210 | therowub = new double[nrows]; |
| 1211 | CoinFillN(therowub, nrows, +getInfinity()); |
| 1212 | } else |
| 1213 | therowub = const_cast< double * >(rowub); |
| 1214 | |
| 1215 | // copy problem into soplex_ |
| 1216 | soplex_->clear(); |
| 1217 | spxintvars_->clear(); |
| 1218 | freeCachedData(OsiSpxSolverInterface::KEEPCACHED_NONE); |
| 1219 | |
| 1220 | if (matrix.isColOrdered()) { |
| 1221 | int row, col, pos; |
| 1222 | soplex::LPRowSet rowset(nrows, 0); |
| 1223 | soplex::DSVector rowvec; |
| 1224 | soplex::LPColSet colset(ncols, matrix.getNumElements()); |
| 1225 | soplex::DSVector colvec; |
| 1226 | |
| 1227 | /* insert empty rows */ |
| 1228 | rowvec.clear(); |
| 1229 | for (row = 0; row < nrows; ++row) |
| 1230 | rowset.add(therowlb[row], rowvec, therowub[row]); |
| 1231 | soplex_->addRows(rowset); |
| 1232 | |
| 1233 | /* create columns */ |
| 1234 | for (col = 0; col < ncols; ++col) { |
| 1235 | pos = start[col]; |
| 1236 | colvec.clear(); |
| 1237 | colvec.add(length[col], &(index[pos]), &(elem[pos])); |
| 1238 | colset.add(theobj[col], thecollb[col], colvec, thecolub[col]); |
| 1239 | } |
| 1240 | |
| 1241 | soplex_->addCols(colset); |
| 1242 | // soplex_->changeRange( soplex::Vector( nrows, therowlb ), soplex::Vector( nrows, therowub ) ); |
| 1243 | } else { |
| 1244 | int row, col, pos; |
| 1245 | soplex::LPRowSet rowset(nrows, matrix.getNumElements()); |
| 1246 | soplex::DSVector rowvec; |
| 1247 | soplex::LPColSet colset(ncols, 0); |
| 1248 | soplex::DSVector colvec; |
| 1249 | |
| 1250 | /* insert empty columns */ |
| 1251 | colvec.clear(); |
| 1252 | for (col = 0; col < ncols; ++col) |
| 1253 | colset.add(theobj[col], thecollb[col], colvec, thecolub[col]); |
| 1254 | soplex_->addCols(colset); |
| 1255 | |
| 1256 | /* create rows */ |
| 1257 | for (row = 0; row < nrows; ++row) { |
| 1258 | pos = start[row]; |
| 1259 | rowvec.clear(); |
| 1260 | rowvec.add(length[row], &(index[pos]), &(elem[pos])); |
| 1261 | rowset.add(therowlb[row], rowvec, therowub[row]); |
| 1262 | } |
| 1263 | |
| 1264 | soplex_->addRows(rowset); |
| 1265 | // soplex_->changeObj( soplex::Vector( ncols, theobj ) ); |
| 1266 | // soplex_->changeBounds( soplex::Vector( ncols, thecollb ), soplex::Vector( ncols, thecolub ) ); |
| 1267 | } |
| 1268 | |
| 1269 | // switch sense to minimization problem |
| 1270 | soplex_->changeSense(soplex::SPxSolver::MINIMIZE); |
| 1271 | |
| 1272 | // delete default arrays if neccessary |
| 1273 | if (collb == NULL) |
| 1274 | delete[] thecollb; |
| 1275 | if (colub == NULL) |
| 1276 | delete[] thecolub; |
| 1277 | if (obj == NULL) |
| 1278 | delete[] theobj; |
| 1279 | if (rowlb == NULL) |
| 1280 | delete[] therowlb; |
| 1281 | if (rowub == NULL) |
| 1282 | delete[] therowub; |
| 1283 | } |
| 1284 | |
| 1285 | //----------------------------------------------------------------------------- |
| 1286 | |
| 1287 | void OsiSpxSolverInterface::assignProblem(CoinPackedMatrix *&matrix, |
| 1288 | double *&collb, double *&colub, |
| 1289 | double *&obj, |
| 1290 | double *&rowlb, double *&rowub) |
| 1291 | { |
| 1292 | loadProblem(*matrix, collb, colub, obj, rowlb, rowub); |
| 1293 | delete matrix; |
| 1294 | matrix = 0; |
| 1295 | delete[] collb; |
| 1296 | collb = 0; |
| 1297 | delete[] colub; |
| 1298 | colub = 0; |
| 1299 | delete[] obj; |
| 1300 | obj = 0; |
| 1301 | delete[] rowlb; |
| 1302 | rowlb = 0; |
| 1303 | delete[] rowub; |
| 1304 | rowub = 0; |
| 1305 | } |
| 1306 | |
| 1307 | //----------------------------------------------------------------------------- |
| 1308 | |
| 1309 | void OsiSpxSolverInterface::loadProblem(const CoinPackedMatrix &matrix, |
| 1310 | const double *collb, const double *colub, |
| 1311 | const double *obj, |
| 1312 | const char *rowsen, const double *rowrhs, |
| 1313 | const double *rowrng) |
| 1314 | { |
| 1315 | int nrows = matrix.getNumRows(); |
| 1316 | double *rowlb = new double[nrows]; |
| 1317 | double *rowub = new double[nrows]; |
| 1318 | int row; |
| 1319 | char *therowsen; |
| 1320 | double *therowrhs, *therowrng; |
| 1321 | |
| 1322 | if (rowsen == NULL) { |
| 1323 | therowsen = new char[nrows]; |
| 1324 | CoinFillN(therowsen, nrows, 'G'); |
| 1325 | } else |
| 1326 | therowsen = const_cast< char * >(rowsen); |
| 1327 | if (rowrhs == NULL) { |
| 1328 | therowrhs = new double[nrows]; |
| 1329 | CoinFillN(therowrhs, nrows, 0.0); |
| 1330 | } else |
| 1331 | therowrhs = const_cast< double * >(rowrhs); |
| 1332 | if (rowrng == NULL) { |
| 1333 | therowrng = new double[nrows]; |
| 1334 | CoinFillN(therowrng, nrows, 0.0); |
| 1335 | } else |
| 1336 | therowrng = const_cast< double * >(rowrng); |
| 1337 | |
| 1338 | for (row = 0; row < nrows; ++row) |
| 1339 | convertSenseToBound(therowsen[row], therowrhs[row], therowrng[row], rowlb[row], rowub[row]); |
| 1340 | |
| 1341 | loadProblem(matrix, collb, colub, obj, rowlb, rowub); |
| 1342 | |
| 1343 | if (rowsen == NULL) |
| 1344 | delete[] therowsen; |
| 1345 | if (rowrhs == NULL) |
| 1346 | delete[] therowrhs; |
| 1347 | if (rowrng == NULL) |
| 1348 | delete[] therowrng; |
| 1349 | |
| 1350 | delete[] rowlb; |
| 1351 | delete[] rowub; |
| 1352 | } |
| 1353 | |
| 1354 | //----------------------------------------------------------------------------- |
| 1355 | |
| 1356 | void OsiSpxSolverInterface::assignProblem(CoinPackedMatrix *&matrix, |
| 1357 | double *&collb, double *&colub, |
| 1358 | double *&obj, |
| 1359 | char *&rowsen, double *&rowrhs, |
| 1360 | double *&rowrng) |
| 1361 | { |
| 1362 | loadProblem(*matrix, collb, colub, obj, rowsen, rowrhs, rowrng); |
| 1363 | delete matrix; |
| 1364 | matrix = 0; |
| 1365 | delete[] collb; |
| 1366 | collb = 0; |
| 1367 | delete[] colub; |
| 1368 | colub = 0; |
| 1369 | delete[] obj; |
| 1370 | obj = 0; |
| 1371 | delete[] rowsen; |
| 1372 | rowsen = 0; |
| 1373 | delete[] rowrhs; |
| 1374 | rowrhs = 0; |
| 1375 | delete[] rowrng; |
| 1376 | rowrng = 0; |
| 1377 | } |
| 1378 | |
| 1379 | //----------------------------------------------------------------------------- |
| 1380 | |
| 1381 | void OsiSpxSolverInterface::loadProblem(const int numcols, const int numrows, |
| 1382 | const int *start, const int *index, |
| 1383 | const double *value, |
| 1384 | const double *collb, const double *colub, |
| 1385 | const double *obj, |
| 1386 | const double *rowlb, const double *rowub) |
| 1387 | { |
| 1388 | int col, pos; |
| 1389 | soplex::LPColSet colset(numcols, start[numcols]); |
| 1390 | soplex::DSVector colvec; |
| 1391 | |
| 1392 | soplex_->clear(); |
| 1393 | spxintvars_->clear(); |
| 1394 | freeCachedData(OsiSpxSolverInterface::KEEPCACHED_NONE); |
| 1395 | |
| 1396 | for (col = 0; col < numcols; ++col) { |
| 1397 | pos = start[col]; |
| 1398 | colvec.clear(); |
| 1399 | colvec.add(start[col + 1] - pos, &(index[pos]), &(value[pos])); |
| 1400 | colset.add(obj[col], collb[col], colvec, colub[col]); |
| 1401 | } |
| 1402 | |
| 1403 | soplex_->addCols(colset); |
| 1404 | soplex_->changeRange(soplex::Vector(numrows, const_cast< double * >(rowlb)), soplex::Vector(numrows, const_cast< double * >(rowub))); |
| 1405 | } |
| 1406 | |
| 1407 | //----------------------------------------------------------------------------- |
| 1408 | |
| 1409 | void OsiSpxSolverInterface::loadProblem(const int numcols, const int numrows, |
| 1410 | const int *start, const int *index, |
| 1411 | const double *value, |
| 1412 | const double *collb, const double *colub, |
| 1413 | const double *obj, |
| 1414 | const char *rowsen, const double *rowrhs, |
| 1415 | const double *rowrng) |
| 1416 | { |
| 1417 | double *rowlb = new double[numrows]; |
| 1418 | double *rowub = new double[numrows]; |
| 1419 | int row; |
| 1420 | |
| 1421 | for (row = 0; row < numrows; ++row) |
| 1422 | convertSenseToBound(rowsen != NULL ? rowsen[row] : 'G', rowrhs != NULL ? rowrhs[row] : 0.0, rowrng != NULL ? rowrng[row] : 0.0, rowlb[row], rowub[row]); |
| 1423 | |
| 1424 | loadProblem(numcols, numrows, start, index, value, collb, colub, obj, rowlb, rowub); |
| 1425 | |
| 1426 | delete[] rowlb; |
| 1427 | delete[] rowub; |
| 1428 | } |
| 1429 | |
| 1430 | //----------------------------------------------------------------------------- |
| 1431 | // Read mps files |
| 1432 | //----------------------------------------------------------------------------- |
| 1433 | int OsiSpxSolverInterface::readMps(const char *filename, |
| 1434 | const char *extension) |
| 1435 | { |
| 1436 | #if 0 |
| 1437 | std::string f(filename); |
| 1438 | std::string e(extension); |
| 1439 | std::string fullname = f + "." + e; |
| 1440 | std::ifstream file(fullname.c_str()); |
| 1441 | if (!file.good()) |
| 1442 | { |
| 1443 | std::cerr << "Error opening file " << fullname << " for reading!" << std::endl; |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
| 1447 | soplex_->clear(); |
| 1448 | if( !soplex_->readMPS(file, NULL, NULL, NULL) ) |
| 1449 | throwSPXerror( "error reading file <" + fullname + ">", "readMps" ); |
| 1450 | #endif |
| 1451 | |
| 1452 | // we preserve the objective sense independent of the problem which is read |
| 1453 | soplex::SPxLP::SPxSense objsen = soplex_->spxSense(); |
| 1454 | int retval = OsiSolverInterface::readMps(filename, extension); |
| 1455 | soplex_->changeSense(objsen); |
| 1456 | return retval; |
| 1457 | } |
| 1458 | |
| 1459 | //----------------------------------------------------------------------------- |
| 1460 | // Write mps files |
| 1461 | //----------------------------------------------------------------------------- |
| 1462 | void OsiSpxSolverInterface::writeMps(const char *filename, |
| 1463 | const char *extension, |
| 1464 | double objSense) const |
| 1465 | { |
| 1466 | std::string f(filename); |
| 1467 | std::string e(extension); |
| 1468 | std::string fullname = f + "." + e; |
| 1469 | std::ofstream file(fullname.c_str()); |
| 1470 | if (!file.good()) { |
| 1471 | std::cerr << "Error opening file " << fullname << " for writing!" << std::endl; |
| 1472 | return; |
| 1473 | } |
| 1474 | soplex::DIdxSet integers(getNumIntegers()); |
| 1475 | for (int i = 0; i < getNumCols(); ++i) |
| 1476 | if (isInteger(i)) |
| 1477 | integers.addIdx(i); |
| 1478 | soplex_->writeMPS(file, NULL, NULL, &integers); |
| 1479 | } |
| 1480 | |
| 1481 | //############################################################################# |
| 1482 | // Constructors, destructors clone and assignment |
| 1483 | //############################################################################# |
| 1484 | |
| 1485 | //------------------------------------------------------------------- |
| 1486 | // Default Constructor |
| 1487 | //------------------------------------------------------------------- |
| 1488 | OsiSpxSolverInterface::OsiSpxSolverInterface() |
| 1489 | : OsiSolverInterface() |
| 1490 | , |
| 1491 | #if SOPLEX_VERSION >= 220 |
| 1492 | spxout_(new soplex::SPxOut) |
| 1493 | , soplex_(new soplex::SoPlex(*spxout_, soplex::SPxSolver::ENTER, soplex::SPxSolver::COLUMN)) |
| 1494 | , // default is primal simplex algorithm |
| 1495 | #else |
| 1496 | spxout_(NULL) |
| 1497 | , soplex_(new soplex::SoPlex(soplex::SPxSolver::ENTER, soplex::SPxSolver::COLUMN)) |
| 1498 | , // default is primal simplex algorithm |
| 1499 | #endif |
| 1500 | spxintvars_(new soplex::DIdxSet()) |
| 1501 | , hotStartCStat_(NULL) |
| 1502 | , hotStartCStatSize_(0) |
| 1503 | , hotStartRStat_(NULL) |
| 1504 | , hotStartRStatSize_(0) |
| 1505 | , hotStartMaxIteration_(1000000) |
| 1506 | , // ??? default iteration limit for strong branching is large |
| 1507 | obj_(NULL) |
| 1508 | , rowsense_(NULL) |
| 1509 | , rhs_(NULL) |
| 1510 | , rowrange_(NULL) |
| 1511 | , colsol_(NULL) |
| 1512 | , rowsol_(NULL) |
| 1513 | , redcost_(NULL) |
| 1514 | , rowact_(NULL) |
| 1515 | , matrixByRow_(NULL) |
| 1516 | , matrixByCol_(NULL) |
| 1517 | { |
| 1518 | #if SOPLEX_VERSION >= 220 |
| 1519 | #ifndef NDEBUG |
| 1520 | spxout_->setVerbosity(soplex::SPxOut::INFO1); |
| 1521 | #else |
| 1522 | spxout_->setVerbosity(soplex::SPxOut::DEBUG); |
| 1523 | #endif |
| 1524 | #else |
| 1525 | #ifndef NDEBUG |
| 1526 | soplex::Param::setVerbose(3); |
| 1527 | #else |
| 1528 | soplex::Param::setVerbose(2); |
| 1529 | #endif |
| 1530 | #endif |
| 1531 | |
| 1532 | // SoPlex default objective sense is maximization, thus we explicitly set it to minimization |
| 1533 | soplex_->changeSense(soplex::SPxLP::MINIMIZE); |
| 1534 | } |
| 1535 | |
| 1536 | //---------------------------------------------------------------- |
| 1537 | // Clone |
| 1538 | //---------------------------------------------------------------- |
| 1539 | OsiSolverInterface *OsiSpxSolverInterface::clone(bool copyData) const |
| 1540 | { |
| 1541 | return (new OsiSpxSolverInterface(*this)); |
| 1542 | } |
| 1543 | |
| 1544 | //------------------------------------------------------------------- |
| 1545 | // Copy constructor |
| 1546 | //------------------------------------------------------------------- |
| 1547 | OsiSpxSolverInterface::OsiSpxSolverInterface(const OsiSpxSolverInterface &source) |
| 1548 | : OsiSolverInterface(source) |
| 1549 | , soplex_(new soplex::SoPlex(*source.soplex_)) |
| 1550 | , spxintvars_(new soplex::DIdxSet(*source.spxintvars_)) |
| 1551 | , hotStartCStat_(NULL) |
| 1552 | , hotStartCStatSize_(0) |
| 1553 | , hotStartRStat_(NULL) |
| 1554 | , hotStartRStatSize_(0) |
| 1555 | , hotStartMaxIteration_(source.hotStartMaxIteration_) |
| 1556 | , obj_(NULL) |
| 1557 | , rowsense_(NULL) |
| 1558 | , rhs_(NULL) |
| 1559 | , rowrange_(NULL) |
| 1560 | , colsol_(NULL) |
| 1561 | , rowsol_(NULL) |
| 1562 | , redcost_(NULL) |
| 1563 | , rowact_(NULL) |
| 1564 | , matrixByRow_(NULL) |
| 1565 | , matrixByCol_(NULL) |
| 1566 | { |
| 1567 | if (source.colsol_ != NULL) |
| 1568 | setColSolution(source.getColSolution()); |
| 1569 | if (source.rowsol_ != NULL) |
| 1570 | setRowPrice(source.getRowPrice()); |
| 1571 | } |
| 1572 | |
| 1573 | //------------------------------------------------------------------- |
| 1574 | // Destructor |
| 1575 | //------------------------------------------------------------------- |
| 1576 | OsiSpxSolverInterface::~OsiSpxSolverInterface() |
| 1577 | { |
| 1578 | freeAllMemory(); |
| 1579 | } |
| 1580 | |
| 1581 | //------------------------------------------------------------------- |
| 1582 | // SPX specific public interfaces |
| 1583 | //------------------------------------------------------------------- |
| 1584 | |
| 1585 | soplex::SoPlex *OsiSpxSolverInterface::getLpPtr(int keepCached) |
| 1586 | { |
| 1587 | freeCachedData(keepCached); |
| 1588 | return soplex_; |
| 1589 | } |
| 1590 | |
| 1591 | //---------------------------------------------------------------- |
| 1592 | // Assignment operator |
| 1593 | //------------------------------------------------------------------- |
| 1594 | OsiSpxSolverInterface &OsiSpxSolverInterface::operator=(const OsiSpxSolverInterface &source) |
| 1595 | { |
| 1596 | if (this != &source) { |
| 1597 | freeAllMemory(); |
| 1598 | |
| 1599 | OsiSolverInterface::operator=(source); |
| 1600 | spxintvars_ = new soplex::DIdxSet(*source.spxintvars_); |
| 1601 | soplex_ = new soplex::SoPlex(*source.soplex_); |
| 1602 | if (source.colsol_ != NULL) |
| 1603 | setColSolution(source.getColSolution()); |
| 1604 | if (source.rowsol_ != NULL) |
| 1605 | setRowPrice(source.getRowPrice()); |
| 1606 | hotStartMaxIteration_ = source.hotStartMaxIteration_; |
| 1607 | } |
| 1608 | return *this; |
| 1609 | } |
| 1610 | |
| 1611 | //############################################################################# |
| 1612 | // Applying cuts |
| 1613 | //############################################################################# |
| 1614 | |
| 1615 | void OsiSpxSolverInterface::applyColCut(const OsiColCut &cc) |
| 1616 | { |
| 1617 | const double *soplexColLB = getColLower(); |
| 1618 | const double *soplexColUB = getColUpper(); |
| 1619 | const CoinPackedVector &lbs = cc.lbs(); |
| 1620 | const CoinPackedVector &ubs = cc.ubs(); |
| 1621 | int i; |
| 1622 | |
| 1623 | for (i = 0; i < lbs.getNumElements(); ++i) |
| 1624 | if (lbs.getElements()[i] > soplexColLB[lbs.getIndices()[i]]) |
| 1625 | setColLower(lbs.getIndices()[i], lbs.getElements()[i]); |
| 1626 | for (i = 0; i < ubs.getNumElements(); ++i) |
| 1627 | if (ubs.getElements()[i] < soplexColUB[ubs.getIndices()[i]]) |
| 1628 | setColUpper(ubs.getIndices()[i], ubs.getElements()[i]); |
| 1629 | } |
| 1630 | |
| 1631 | //----------------------------------------------------------------------------- |
| 1632 | |
| 1633 | void OsiSpxSolverInterface::applyRowCut(const OsiRowCut &rowCut) |
| 1634 | { |
| 1635 | addRow(rowCut.row(), rowCut.lb(), rowCut.ub()); |
| 1636 | } |
| 1637 | |
| 1638 | //############################################################################# |
| 1639 | // Private methods (non-static and static) |
| 1640 | //############################################################################# |
| 1641 | |
| 1642 | //------------------------------------------------------------------- |
| 1643 | /// free cached vectors |
| 1644 | |
| 1645 | void OsiSpxSolverInterface::freeCachedColRim() |
| 1646 | { |
| 1647 | delete obj_; |
| 1648 | obj_ = NULL; |
| 1649 | } |
| 1650 | |
| 1651 | void OsiSpxSolverInterface::freeCachedRowRim() |
| 1652 | { |
| 1653 | freeCacheChar(rowsense_); |
| 1654 | freeCacheDouble(rhs_); |
| 1655 | freeCacheDouble(rowrange_); |
| 1656 | assert(rowsense_ == NULL); |
| 1657 | assert(rhs_ == NULL); |
| 1658 | assert(rowrange_ == NULL); |
| 1659 | } |
| 1660 | |
| 1661 | void OsiSpxSolverInterface::freeCachedMatrix() |
| 1662 | { |
| 1663 | freeCacheMatrix(matrixByRow_); |
| 1664 | freeCacheMatrix(matrixByCol_); |
| 1665 | assert(matrixByRow_ == NULL); |
| 1666 | assert(matrixByCol_ == NULL); |
| 1667 | } |
| 1668 | |
| 1669 | void OsiSpxSolverInterface::freeCachedResults() |
| 1670 | { |
| 1671 | delete colsol_; |
| 1672 | delete rowsol_; |
| 1673 | delete redcost_; |
| 1674 | delete rowact_; |
| 1675 | colsol_ = NULL; |
| 1676 | rowsol_ = NULL; |
| 1677 | redcost_ = NULL; |
| 1678 | rowact_ = NULL; |
| 1679 | } |
| 1680 | |
| 1681 | void OsiSpxSolverInterface::freeCachedData(int keepCached) |
| 1682 | { |
| 1683 | if (!(keepCached & OsiSpxSolverInterface::KEEPCACHED_COLUMN)) |
| 1684 | freeCachedColRim(); |
| 1685 | if (!(keepCached & OsiSpxSolverInterface::KEEPCACHED_ROW)) |
| 1686 | freeCachedRowRim(); |
| 1687 | if (!(keepCached & OsiSpxSolverInterface::KEEPCACHED_MATRIX)) |
| 1688 | freeCachedMatrix(); |
| 1689 | if (!(keepCached & OsiSpxSolverInterface::KEEPCACHED_RESULTS)) |
| 1690 | freeCachedResults(); |
| 1691 | } |
| 1692 | |
| 1693 | void OsiSpxSolverInterface::freeAllMemory() |
| 1694 | { |
| 1695 | freeCachedData(); |
| 1696 | delete[] reinterpret_cast< soplex::SPxSolver::VarStatus * >(hotStartCStat_); |
| 1697 | delete[] reinterpret_cast< soplex::SPxSolver::VarStatus * >(hotStartRStat_); |
| 1698 | hotStartCStat_ = NULL; |
| 1699 | hotStartCStatSize_ = 0; |
| 1700 | hotStartRStat_ = NULL; |
| 1701 | hotStartRStatSize_ = 0; |
| 1702 | delete soplex_; |
| 1703 | #if SOPLEX_VERSION >= 220 |
| 1704 | delete spxout_; |
| 1705 | #endif |
| 1706 | delete spxintvars_; |
| 1707 | } |
| 1708 | |
| 1709 | /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 |
| 1710 | */ |