lemon/soplex.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 03 May 2010 10:24:52 +0200
branch1.2
changeset 978 cbf32bf95954
parent 793 e4554cd6b2bf
child 1270 dceba191c00d
permissions -rw-r--r--
Merge bugfix #368 to branch 1.2
alpar@484
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@484
     2
 *
alpar@484
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@484
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
alpar@484
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@484
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@484
     8
 *
alpar@484
     9
 * Permission to use, modify and distribute this software is granted
alpar@484
    10
 * provided that this copyright notice appears in all copies. For
alpar@484
    11
 * precise terms see the accompanying LICENSE file.
alpar@484
    12
 *
alpar@484
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@484
    14
 * express or implied, and with no claim as to its suitability for any
alpar@484
    15
 * purpose.
alpar@484
    16
 *
alpar@484
    17
 */
alpar@484
    18
alpar@484
    19
#include <iostream>
alpar@484
    20
#include <lemon/soplex.h>
alpar@484
    21
alpar@586
    22
#include <soplex.h>
deba@623
    23
#include <spxout.h>
alpar@484
    24
alpar@484
    25
alpar@484
    26
///\file
alpar@484
    27
///\brief Implementation of the LEMON-SOPLEX lp solver interface.
alpar@484
    28
namespace lemon {
alpar@484
    29
alpar@485
    30
  SoplexLp::SoplexLp() {
alpar@484
    31
    soplex = new soplex::SoPlex;
deba@623
    32
    messageLevel(MESSAGE_NOTHING);
alpar@484
    33
  }
alpar@484
    34
alpar@485
    35
  SoplexLp::~SoplexLp() {
alpar@484
    36
    delete soplex;
alpar@484
    37
  }
alpar@484
    38
alpar@485
    39
  SoplexLp::SoplexLp(const SoplexLp& lp) {
alpar@484
    40
    rows = lp.rows;
alpar@484
    41
    cols = lp.cols;
alpar@484
    42
alpar@484
    43
    soplex = new soplex::SoPlex;
alpar@484
    44
    (*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex);
alpar@484
    45
alpar@484
    46
    _col_names = lp._col_names;
alpar@484
    47
    _col_names_ref = lp._col_names_ref;
alpar@484
    48
alpar@484
    49
    _row_names = lp._row_names;
alpar@484
    50
    _row_names_ref = lp._row_names_ref;
alpar@484
    51
deba@623
    52
    messageLevel(MESSAGE_NOTHING);
alpar@484
    53
  }
alpar@484
    54
alpar@485
    55
  void SoplexLp::_clear_temporals() {
alpar@484
    56
    _primal_values.clear();
alpar@484
    57
    _dual_values.clear();
alpar@484
    58
  }
alpar@484
    59
alpar@587
    60
  SoplexLp* SoplexLp::newSolver() const {
alpar@485
    61
    SoplexLp* newlp = new SoplexLp();
alpar@484
    62
    return newlp;
alpar@484
    63
  }
alpar@484
    64
alpar@587
    65
  SoplexLp* SoplexLp::cloneSolver() const {
alpar@485
    66
    SoplexLp* newlp = new SoplexLp(*this);
alpar@484
    67
    return newlp;
alpar@484
    68
  }
alpar@484
    69
alpar@485
    70
  const char* SoplexLp::_solverName() const { return "SoplexLp"; }
alpar@484
    71
alpar@485
    72
  int SoplexLp::_addCol() {
alpar@484
    73
    soplex::LPCol c;
alpar@484
    74
    c.setLower(-soplex::infinity);
alpar@484
    75
    c.setUpper(soplex::infinity);
alpar@484
    76
    soplex->addCol(c);
alpar@484
    77
alpar@484
    78
    _col_names.push_back(std::string());
alpar@484
    79
alpar@484
    80
    return soplex->nCols() - 1;
alpar@484
    81
  }
alpar@484
    82
alpar@485
    83
  int SoplexLp::_addRow() {
alpar@484
    84
    soplex::LPRow r;
alpar@484
    85
    r.setLhs(-soplex::infinity);
alpar@484
    86
    r.setRhs(soplex::infinity);
alpar@484
    87
    soplex->addRow(r);
alpar@484
    88
alpar@484
    89
    _row_names.push_back(std::string());
alpar@484
    90
alpar@484
    91
    return soplex->nRows() - 1;
alpar@484
    92
  }
alpar@484
    93
deba@793
    94
  int SoplexLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
deba@793
    95
    soplex::DSVector v;
deba@793
    96
    for (ExprIterator it = b; it != e; ++it) {
deba@793
    97
      v.add(it->first, it->second);
deba@793
    98
    }
deba@793
    99
    soplex::LPRow r(l, v, u);
deba@793
   100
    soplex->addRow(r);
deba@793
   101
deba@793
   102
    _row_names.push_back(std::string());
deba@793
   103
deba@793
   104
    return soplex->nRows() - 1;
deba@793
   105
  }
deba@793
   106
alpar@484
   107
alpar@485
   108
  void SoplexLp::_eraseCol(int i) {
alpar@484
   109
    soplex->removeCol(i);
alpar@484
   110
    _col_names_ref.erase(_col_names[i]);
alpar@484
   111
    _col_names[i] = _col_names.back();
alpar@484
   112
    _col_names_ref[_col_names.back()] = i;
alpar@484
   113
    _col_names.pop_back();
alpar@484
   114
  }
alpar@484
   115
alpar@485
   116
  void SoplexLp::_eraseRow(int i) {
alpar@484
   117
    soplex->removeRow(i);
alpar@484
   118
    _row_names_ref.erase(_row_names[i]);
alpar@484
   119
    _row_names[i] = _row_names.back();
alpar@484
   120
    _row_names_ref[_row_names.back()] = i;
alpar@484
   121
    _row_names.pop_back();
alpar@484
   122
  }
alpar@484
   123
alpar@485
   124
  void SoplexLp::_eraseColId(int i) {
alpar@484
   125
    cols.eraseIndex(i);
alpar@484
   126
    cols.relocateIndex(i, cols.maxIndex());
alpar@484
   127
  }
alpar@485
   128
  void SoplexLp::_eraseRowId(int i) {
alpar@484
   129
    rows.eraseIndex(i);
alpar@484
   130
    rows.relocateIndex(i, rows.maxIndex());
alpar@484
   131
  }
alpar@484
   132
alpar@485
   133
  void SoplexLp::_getColName(int c, std::string &name) const {
alpar@484
   134
    name = _col_names[c];
alpar@484
   135
  }
alpar@484
   136
alpar@485
   137
  void SoplexLp::_setColName(int c, const std::string &name) {
alpar@484
   138
    _col_names_ref.erase(_col_names[c]);
alpar@484
   139
    _col_names[c] = name;
alpar@484
   140
    if (!name.empty()) {
alpar@484
   141
      _col_names_ref.insert(std::make_pair(name, c));
alpar@484
   142
    }
alpar@484
   143
  }
alpar@484
   144
alpar@485
   145
  int SoplexLp::_colByName(const std::string& name) const {
alpar@484
   146
    std::map<std::string, int>::const_iterator it =
alpar@484
   147
      _col_names_ref.find(name);
alpar@484
   148
    if (it != _col_names_ref.end()) {
alpar@484
   149
      return it->second;
alpar@484
   150
    } else {
alpar@484
   151
      return -1;
alpar@484
   152
    }
alpar@484
   153
  }
alpar@484
   154
alpar@485
   155
  void SoplexLp::_getRowName(int r, std::string &name) const {
alpar@484
   156
    name = _row_names[r];
alpar@484
   157
  }
alpar@484
   158
alpar@485
   159
  void SoplexLp::_setRowName(int r, const std::string &name) {
alpar@484
   160
    _row_names_ref.erase(_row_names[r]);
alpar@484
   161
    _row_names[r] = name;
alpar@484
   162
    if (!name.empty()) {
alpar@484
   163
      _row_names_ref.insert(std::make_pair(name, r));
alpar@484
   164
    }
alpar@484
   165
  }
alpar@484
   166
alpar@485
   167
  int SoplexLp::_rowByName(const std::string& name) const {
alpar@484
   168
    std::map<std::string, int>::const_iterator it =
alpar@484
   169
      _row_names_ref.find(name);
alpar@484
   170
    if (it != _row_names_ref.end()) {
alpar@484
   171
      return it->second;
alpar@484
   172
    } else {
alpar@484
   173
      return -1;
alpar@484
   174
    }
alpar@484
   175
  }
alpar@484
   176
alpar@484
   177
alpar@485
   178
  void SoplexLp::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
alpar@484
   179
    for (int j = 0; j < soplex->nCols(); ++j) {
alpar@484
   180
      soplex->changeElement(i, j, 0.0);
alpar@484
   181
    }
alpar@484
   182
    for(ExprIterator it = b; it != e; ++it) {
alpar@484
   183
      soplex->changeElement(i, it->first, it->second);
alpar@484
   184
    }
alpar@484
   185
  }
alpar@484
   186
alpar@485
   187
  void SoplexLp::_getRowCoeffs(int i, InsertIterator b) const {
alpar@484
   188
    const soplex::SVector& vec = soplex->rowVector(i);
alpar@484
   189
    for (int k = 0; k < vec.size(); ++k) {
alpar@484
   190
      *b = std::make_pair(vec.index(k), vec.value(k));
alpar@484
   191
      ++b;
alpar@484
   192
    }
alpar@484
   193
  }
alpar@484
   194
alpar@485
   195
  void SoplexLp::_setColCoeffs(int j, ExprIterator b, ExprIterator e) {
alpar@484
   196
    for (int i = 0; i < soplex->nRows(); ++i) {
alpar@484
   197
      soplex->changeElement(i, j, 0.0);
alpar@484
   198
    }
alpar@484
   199
    for(ExprIterator it = b; it != e; ++it) {
alpar@484
   200
      soplex->changeElement(it->first, j, it->second);
alpar@484
   201
    }
alpar@484
   202
  }
alpar@484
   203
alpar@485
   204
  void SoplexLp::_getColCoeffs(int i, InsertIterator b) const {
alpar@484
   205
    const soplex::SVector& vec = soplex->colVector(i);
alpar@484
   206
    for (int k = 0; k < vec.size(); ++k) {
alpar@484
   207
      *b = std::make_pair(vec.index(k), vec.value(k));
alpar@484
   208
      ++b;
alpar@484
   209
    }
alpar@484
   210
  }
alpar@484
   211
alpar@485
   212
  void SoplexLp::_setCoeff(int i, int j, Value value) {
alpar@484
   213
    soplex->changeElement(i, j, value);
alpar@484
   214
  }
alpar@484
   215
alpar@485
   216
  SoplexLp::Value SoplexLp::_getCoeff(int i, int j) const {
alpar@484
   217
    return soplex->rowVector(i)[j];
alpar@484
   218
  }
alpar@484
   219
alpar@485
   220
  void SoplexLp::_setColLowerBound(int i, Value value) {
alpar@484
   221
    LEMON_ASSERT(value != INF, "Invalid bound");
alpar@484
   222
    soplex->changeLower(i, value != -INF ? value : -soplex::infinity);
alpar@484
   223
  }
alpar@484
   224
alpar@485
   225
  SoplexLp::Value SoplexLp::_getColLowerBound(int i) const {
alpar@484
   226
    double value = soplex->lower(i);
alpar@484
   227
    return value != -soplex::infinity ? value : -INF;
alpar@484
   228
  }
alpar@484
   229
alpar@485
   230
  void SoplexLp::_setColUpperBound(int i, Value value) {
alpar@484
   231
    LEMON_ASSERT(value != -INF, "Invalid bound");
alpar@484
   232
    soplex->changeUpper(i, value != INF ? value : soplex::infinity);
alpar@484
   233
  }
alpar@484
   234
alpar@485
   235
  SoplexLp::Value SoplexLp::_getColUpperBound(int i) const {
alpar@484
   236
    double value = soplex->upper(i);
alpar@484
   237
    return value != soplex::infinity ? value : INF;
alpar@484
   238
  }
alpar@484
   239
alpar@485
   240
  void SoplexLp::_setRowLowerBound(int i, Value lb) {
alpar@484
   241
    LEMON_ASSERT(lb != INF, "Invalid bound");
alpar@484
   242
    soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, soplex->rhs(i));
alpar@484
   243
  }
alpar@484
   244
alpar@485
   245
  SoplexLp::Value SoplexLp::_getRowLowerBound(int i) const {
alpar@484
   246
    double res = soplex->lhs(i);
alpar@484
   247
    return res == -soplex::infinity ? -INF : res;
alpar@484
   248
  }
alpar@484
   249
alpar@485
   250
  void SoplexLp::_setRowUpperBound(int i, Value ub) {
alpar@484
   251
    LEMON_ASSERT(ub != -INF, "Invalid bound");
alpar@484
   252
    soplex->changeRange(i, soplex->lhs(i), ub != INF ? ub : soplex::infinity);
alpar@484
   253
  }
alpar@484
   254
alpar@485
   255
  SoplexLp::Value SoplexLp::_getRowUpperBound(int i) const {
alpar@484
   256
    double res = soplex->rhs(i);
alpar@484
   257
    return res == soplex::infinity ? INF : res;
alpar@484
   258
  }
alpar@484
   259
alpar@485
   260
  void SoplexLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
alpar@484
   261
    for (int j = 0; j < soplex->nCols(); ++j) {
alpar@484
   262
      soplex->changeObj(j, 0.0);
alpar@484
   263
    }
alpar@484
   264
    for (ExprIterator it = b; it != e; ++it) {
alpar@484
   265
      soplex->changeObj(it->first, it->second);
alpar@484
   266
    }
alpar@484
   267
  }
alpar@484
   268
alpar@485
   269
  void SoplexLp::_getObjCoeffs(InsertIterator b) const {
alpar@484
   270
    for (int j = 0; j < soplex->nCols(); ++j) {
alpar@484
   271
      Value coef = soplex->obj(j);
alpar@484
   272
      if (coef != 0.0) {
alpar@484
   273
        *b = std::make_pair(j, coef);
alpar@484
   274
        ++b;
alpar@484
   275
      }
alpar@484
   276
    }
alpar@484
   277
  }
alpar@484
   278
alpar@485
   279
  void SoplexLp::_setObjCoeff(int i, Value obj_coef) {
alpar@484
   280
    soplex->changeObj(i, obj_coef);
alpar@484
   281
  }
alpar@484
   282
alpar@485
   283
  SoplexLp::Value SoplexLp::_getObjCoeff(int i) const {
alpar@484
   284
    return soplex->obj(i);
alpar@484
   285
  }
alpar@484
   286
alpar@485
   287
  SoplexLp::SolveExitStatus SoplexLp::_solve() {
alpar@484
   288
alpar@484
   289
    _clear_temporals();
alpar@956
   290
deba@623
   291
    _applyMessageLevel();
alpar@484
   292
alpar@484
   293
    soplex::SPxSolver::Status status = soplex->solve();
alpar@484
   294
alpar@484
   295
    switch (status) {
alpar@484
   296
    case soplex::SPxSolver::OPTIMAL:
alpar@484
   297
    case soplex::SPxSolver::INFEASIBLE:
alpar@484
   298
    case soplex::SPxSolver::UNBOUNDED:
alpar@484
   299
      return SOLVED;
alpar@484
   300
    default:
alpar@484
   301
      return UNSOLVED;
alpar@484
   302
    }
alpar@484
   303
  }
alpar@484
   304
alpar@485
   305
  SoplexLp::Value SoplexLp::_getPrimal(int i) const {
alpar@484
   306
    if (_primal_values.empty()) {
alpar@484
   307
      _primal_values.resize(soplex->nCols());
alpar@484
   308
      soplex::Vector pv(_primal_values.size(), &_primal_values.front());
alpar@484
   309
      soplex->getPrimal(pv);
alpar@484
   310
    }
alpar@484
   311
    return _primal_values[i];
alpar@484
   312
  }
alpar@484
   313
alpar@485
   314
  SoplexLp::Value SoplexLp::_getDual(int i) const {
alpar@484
   315
    if (_dual_values.empty()) {
alpar@484
   316
      _dual_values.resize(soplex->nRows());
alpar@484
   317
      soplex::Vector dv(_dual_values.size(), &_dual_values.front());
alpar@484
   318
      soplex->getDual(dv);
alpar@484
   319
    }
alpar@484
   320
    return _dual_values[i];
alpar@484
   321
  }
alpar@484
   322
alpar@485
   323
  SoplexLp::Value SoplexLp::_getPrimalValue() const {
alpar@484
   324
    return soplex->objValue();
alpar@484
   325
  }
alpar@484
   326
alpar@485
   327
  SoplexLp::VarStatus SoplexLp::_getColStatus(int i) const {
alpar@484
   328
    switch (soplex->getBasisColStatus(i)) {
alpar@484
   329
    case soplex::SPxSolver::BASIC:
alpar@484
   330
      return BASIC;
alpar@484
   331
    case soplex::SPxSolver::ON_UPPER:
alpar@484
   332
      return UPPER;
alpar@484
   333
    case soplex::SPxSolver::ON_LOWER:
alpar@484
   334
      return LOWER;
alpar@484
   335
    case soplex::SPxSolver::FIXED:
alpar@484
   336
      return FIXED;
alpar@484
   337
    case soplex::SPxSolver::ZERO:
alpar@484
   338
      return FREE;
alpar@484
   339
    default:
alpar@484
   340
      LEMON_ASSERT(false, "Wrong column status");
alpar@484
   341
      return VarStatus();
alpar@484
   342
    }
alpar@484
   343
  }
alpar@484
   344
alpar@485
   345
  SoplexLp::VarStatus SoplexLp::_getRowStatus(int i) const {
alpar@484
   346
    switch (soplex->getBasisRowStatus(i)) {
alpar@484
   347
    case soplex::SPxSolver::BASIC:
alpar@484
   348
      return BASIC;
alpar@484
   349
    case soplex::SPxSolver::ON_UPPER:
alpar@484
   350
      return UPPER;
alpar@484
   351
    case soplex::SPxSolver::ON_LOWER:
alpar@484
   352
      return LOWER;
alpar@484
   353
    case soplex::SPxSolver::FIXED:
alpar@484
   354
      return FIXED;
alpar@484
   355
    case soplex::SPxSolver::ZERO:
alpar@484
   356
      return FREE;
alpar@484
   357
    default:
alpar@484
   358
      LEMON_ASSERT(false, "Wrong row status");
alpar@484
   359
      return VarStatus();
alpar@484
   360
    }
alpar@484
   361
  }
alpar@484
   362
alpar@485
   363
  SoplexLp::Value SoplexLp::_getPrimalRay(int i) const {
alpar@484
   364
    if (_primal_ray.empty()) {
alpar@484
   365
      _primal_ray.resize(soplex->nCols());
alpar@484
   366
      soplex::Vector pv(_primal_ray.size(), &_primal_ray.front());
alpar@484
   367
      soplex->getDualfarkas(pv);
alpar@484
   368
    }
alpar@484
   369
    return _primal_ray[i];
alpar@484
   370
  }
alpar@484
   371
alpar@485
   372
  SoplexLp::Value SoplexLp::_getDualRay(int i) const {
alpar@484
   373
    if (_dual_ray.empty()) {
alpar@484
   374
      _dual_ray.resize(soplex->nRows());
alpar@484
   375
      soplex::Vector dv(_dual_ray.size(), &_dual_ray.front());
alpar@484
   376
      soplex->getDualfarkas(dv);
alpar@484
   377
    }
alpar@484
   378
    return _dual_ray[i];
alpar@484
   379
  }
alpar@484
   380
alpar@485
   381
  SoplexLp::ProblemType SoplexLp::_getPrimalType() const {
alpar@484
   382
    switch (soplex->status()) {
alpar@484
   383
    case soplex::SPxSolver::OPTIMAL:
alpar@484
   384
      return OPTIMAL;
alpar@484
   385
    case soplex::SPxSolver::UNBOUNDED:
alpar@484
   386
      return UNBOUNDED;
alpar@484
   387
    case soplex::SPxSolver::INFEASIBLE:
alpar@484
   388
      return INFEASIBLE;
alpar@484
   389
    default:
alpar@484
   390
      return UNDEFINED;
alpar@484
   391
    }
alpar@484
   392
  }
alpar@484
   393
alpar@485
   394
  SoplexLp::ProblemType SoplexLp::_getDualType() const {
alpar@484
   395
    switch (soplex->status()) {
alpar@484
   396
    case soplex::SPxSolver::OPTIMAL:
alpar@484
   397
      return OPTIMAL;
alpar@484
   398
    case soplex::SPxSolver::UNBOUNDED:
alpar@484
   399
      return UNBOUNDED;
alpar@484
   400
    case soplex::SPxSolver::INFEASIBLE:
alpar@484
   401
      return INFEASIBLE;
alpar@484
   402
    default:
alpar@484
   403
      return UNDEFINED;
alpar@484
   404
    }
alpar@484
   405
  }
alpar@484
   406
alpar@485
   407
  void SoplexLp::_setSense(Sense sense) {
alpar@484
   408
    switch (sense) {
alpar@484
   409
    case MIN:
alpar@484
   410
      soplex->changeSense(soplex::SPxSolver::MINIMIZE);
alpar@484
   411
      break;
alpar@484
   412
    case MAX:
alpar@484
   413
      soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
alpar@484
   414
    }
alpar@484
   415
  }
alpar@484
   416
alpar@485
   417
  SoplexLp::Sense SoplexLp::_getSense() const {
alpar@484
   418
    switch (soplex->spxSense()) {
alpar@484
   419
    case soplex::SPxSolver::MAXIMIZE:
alpar@484
   420
      return MAX;
alpar@484
   421
    case soplex::SPxSolver::MINIMIZE:
alpar@484
   422
      return MIN;
alpar@484
   423
    default:
alpar@484
   424
      LEMON_ASSERT(false, "Wrong sense.");
alpar@485
   425
      return SoplexLp::Sense();
alpar@484
   426
    }
alpar@484
   427
  }
alpar@484
   428
alpar@485
   429
  void SoplexLp::_clear() {
alpar@484
   430
    soplex->clear();
alpar@484
   431
    _col_names.clear();
alpar@484
   432
    _col_names_ref.clear();
alpar@484
   433
    _row_names.clear();
alpar@484
   434
    _row_names_ref.clear();
alpar@484
   435
    cols.clear();
alpar@484
   436
    rows.clear();
alpar@484
   437
    _clear_temporals();
alpar@484
   438
  }
alpar@484
   439
deba@623
   440
  void SoplexLp::_messageLevel(MessageLevel level) {
deba@623
   441
    switch (level) {
deba@623
   442
    case MESSAGE_NOTHING:
deba@623
   443
      _message_level = -1;
deba@623
   444
      break;
deba@623
   445
    case MESSAGE_ERROR:
deba@623
   446
      _message_level = soplex::SPxOut::ERROR;
deba@623
   447
      break;
deba@623
   448
    case MESSAGE_WARNING:
deba@623
   449
      _message_level = soplex::SPxOut::WARNING;
deba@623
   450
      break;
deba@623
   451
    case MESSAGE_NORMAL:
deba@623
   452
      _message_level = soplex::SPxOut::INFO2;
deba@623
   453
      break;
deba@623
   454
    case MESSAGE_VERBOSE:
deba@623
   455
      _message_level = soplex::SPxOut::DEBUG;
deba@623
   456
      break;
deba@623
   457
    }
deba@623
   458
  }
deba@623
   459
deba@623
   460
  void SoplexLp::_applyMessageLevel() {
deba@623
   461
    soplex::Param::setVerbose(_message_level);
deba@623
   462
  }
deba@623
   463
alpar@484
   464
} //namespace lemon
alpar@484
   465