lemon/soplex.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 23 Feb 2009 12:33:40 +0000
changeset 577 1c5d6e47921f
parent 484 08d495d48089
child 586 547e966b3b29
permissions -rw-r--r--
Merge
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@484
     5
 * Copyright (C) 2003-2008
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@484
    22
#include <soplex/soplex.h>
alpar@484
    23
alpar@484
    24
alpar@484
    25
///\file
alpar@484
    26
///\brief Implementation of the LEMON-SOPLEX lp solver interface.
alpar@484
    27
namespace lemon {
alpar@484
    28
alpar@485
    29
  SoplexLp::SoplexLp() {
alpar@484
    30
    soplex = new soplex::SoPlex;
alpar@484
    31
  }
alpar@484
    32
alpar@485
    33
  SoplexLp::~SoplexLp() {
alpar@484
    34
    delete soplex;
alpar@484
    35
  }
alpar@484
    36
alpar@485
    37
  SoplexLp::SoplexLp(const SoplexLp& lp) {
alpar@484
    38
    rows = lp.rows;
alpar@484
    39
    cols = lp.cols;
alpar@484
    40
alpar@484
    41
    soplex = new soplex::SoPlex;
alpar@484
    42
    (*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex);
alpar@484
    43
alpar@484
    44
    _col_names = lp._col_names;
alpar@484
    45
    _col_names_ref = lp._col_names_ref;
alpar@484
    46
alpar@484
    47
    _row_names = lp._row_names;
alpar@484
    48
    _row_names_ref = lp._row_names_ref;
alpar@484
    49
alpar@484
    50
  }
alpar@484
    51
alpar@485
    52
  void SoplexLp::_clear_temporals() {
alpar@484
    53
    _primal_values.clear();
alpar@484
    54
    _dual_values.clear();
alpar@484
    55
  }
alpar@484
    56
alpar@485
    57
  SoplexLp* SoplexLp::_newSolver() const {
alpar@485
    58
    SoplexLp* newlp = new SoplexLp();
alpar@484
    59
    return newlp;
alpar@484
    60
  }
alpar@484
    61
alpar@485
    62
  SoplexLp* SoplexLp::_cloneSolver() const {
alpar@485
    63
    SoplexLp* newlp = new SoplexLp(*this);
alpar@484
    64
    return newlp;
alpar@484
    65
  }
alpar@484
    66
alpar@485
    67
  const char* SoplexLp::_solverName() const { return "SoplexLp"; }
alpar@484
    68
alpar@485
    69
  int SoplexLp::_addCol() {
alpar@484
    70
    soplex::LPCol c;
alpar@484
    71
    c.setLower(-soplex::infinity);
alpar@484
    72
    c.setUpper(soplex::infinity);
alpar@484
    73
    soplex->addCol(c);
alpar@484
    74
alpar@484
    75
    _col_names.push_back(std::string());
alpar@484
    76
alpar@484
    77
    return soplex->nCols() - 1;
alpar@484
    78
  }
alpar@484
    79
alpar@485
    80
  int SoplexLp::_addRow() {
alpar@484
    81
    soplex::LPRow r;
alpar@484
    82
    r.setLhs(-soplex::infinity);
alpar@484
    83
    r.setRhs(soplex::infinity);
alpar@484
    84
    soplex->addRow(r);
alpar@484
    85
alpar@484
    86
    _row_names.push_back(std::string());
alpar@484
    87
alpar@484
    88
    return soplex->nRows() - 1;
alpar@484
    89
  }
alpar@484
    90
alpar@484
    91
alpar@485
    92
  void SoplexLp::_eraseCol(int i) {
alpar@484
    93
    soplex->removeCol(i);
alpar@484
    94
    _col_names_ref.erase(_col_names[i]);
alpar@484
    95
    _col_names[i] = _col_names.back();
alpar@484
    96
    _col_names_ref[_col_names.back()] = i;
alpar@484
    97
    _col_names.pop_back();
alpar@484
    98
  }
alpar@484
    99
alpar@485
   100
  void SoplexLp::_eraseRow(int i) {
alpar@484
   101
    soplex->removeRow(i);
alpar@484
   102
    _row_names_ref.erase(_row_names[i]);
alpar@484
   103
    _row_names[i] = _row_names.back();
alpar@484
   104
    _row_names_ref[_row_names.back()] = i;
alpar@484
   105
    _row_names.pop_back();
alpar@484
   106
  }
alpar@484
   107
alpar@485
   108
  void SoplexLp::_eraseColId(int i) {
alpar@484
   109
    cols.eraseIndex(i);
alpar@484
   110
    cols.relocateIndex(i, cols.maxIndex());
alpar@484
   111
  }
alpar@485
   112
  void SoplexLp::_eraseRowId(int i) {
alpar@484
   113
    rows.eraseIndex(i);
alpar@484
   114
    rows.relocateIndex(i, rows.maxIndex());
alpar@484
   115
  }
alpar@484
   116
alpar@485
   117
  void SoplexLp::_getColName(int c, std::string &name) const {
alpar@484
   118
    name = _col_names[c];
alpar@484
   119
  }
alpar@484
   120
alpar@485
   121
  void SoplexLp::_setColName(int c, const std::string &name) {
alpar@484
   122
    _col_names_ref.erase(_col_names[c]);
alpar@484
   123
    _col_names[c] = name;
alpar@484
   124
    if (!name.empty()) {
alpar@484
   125
      _col_names_ref.insert(std::make_pair(name, c));
alpar@484
   126
    }
alpar@484
   127
  }
alpar@484
   128
alpar@485
   129
  int SoplexLp::_colByName(const std::string& name) const {
alpar@484
   130
    std::map<std::string, int>::const_iterator it =
alpar@484
   131
      _col_names_ref.find(name);
alpar@484
   132
    if (it != _col_names_ref.end()) {
alpar@484
   133
      return it->second;
alpar@484
   134
    } else {
alpar@484
   135
      return -1;
alpar@484
   136
    }
alpar@484
   137
  }
alpar@484
   138
alpar@485
   139
  void SoplexLp::_getRowName(int r, std::string &name) const {
alpar@484
   140
    name = _row_names[r];
alpar@484
   141
  }
alpar@484
   142
alpar@485
   143
  void SoplexLp::_setRowName(int r, const std::string &name) {
alpar@484
   144
    _row_names_ref.erase(_row_names[r]);
alpar@484
   145
    _row_names[r] = name;
alpar@484
   146
    if (!name.empty()) {
alpar@484
   147
      _row_names_ref.insert(std::make_pair(name, r));
alpar@484
   148
    }
alpar@484
   149
  }
alpar@484
   150
alpar@485
   151
  int SoplexLp::_rowByName(const std::string& name) const {
alpar@484
   152
    std::map<std::string, int>::const_iterator it =
alpar@484
   153
      _row_names_ref.find(name);
alpar@484
   154
    if (it != _row_names_ref.end()) {
alpar@484
   155
      return it->second;
alpar@484
   156
    } else {
alpar@484
   157
      return -1;
alpar@484
   158
    }
alpar@484
   159
  }
alpar@484
   160
alpar@484
   161
alpar@485
   162
  void SoplexLp::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
alpar@484
   163
    for (int j = 0; j < soplex->nCols(); ++j) {
alpar@484
   164
      soplex->changeElement(i, j, 0.0);
alpar@484
   165
    }
alpar@484
   166
    for(ExprIterator it = b; it != e; ++it) {
alpar@484
   167
      soplex->changeElement(i, it->first, it->second);
alpar@484
   168
    }
alpar@484
   169
  }
alpar@484
   170
alpar@485
   171
  void SoplexLp::_getRowCoeffs(int i, InsertIterator b) const {
alpar@484
   172
    const soplex::SVector& vec = soplex->rowVector(i);
alpar@484
   173
    for (int k = 0; k < vec.size(); ++k) {
alpar@484
   174
      *b = std::make_pair(vec.index(k), vec.value(k));
alpar@484
   175
      ++b;
alpar@484
   176
    }
alpar@484
   177
  }
alpar@484
   178
alpar@485
   179
  void SoplexLp::_setColCoeffs(int j, ExprIterator b, ExprIterator e) {
alpar@484
   180
    for (int i = 0; i < soplex->nRows(); ++i) {
alpar@484
   181
      soplex->changeElement(i, j, 0.0);
alpar@484
   182
    }
alpar@484
   183
    for(ExprIterator it = b; it != e; ++it) {
alpar@484
   184
      soplex->changeElement(it->first, j, it->second);
alpar@484
   185
    }
alpar@484
   186
  }
alpar@484
   187
alpar@485
   188
  void SoplexLp::_getColCoeffs(int i, InsertIterator b) const {
alpar@484
   189
    const soplex::SVector& vec = soplex->colVector(i);
alpar@484
   190
    for (int k = 0; k < vec.size(); ++k) {
alpar@484
   191
      *b = std::make_pair(vec.index(k), vec.value(k));
alpar@484
   192
      ++b;
alpar@484
   193
    }
alpar@484
   194
  }
alpar@484
   195
alpar@485
   196
  void SoplexLp::_setCoeff(int i, int j, Value value) {
alpar@484
   197
    soplex->changeElement(i, j, value);
alpar@484
   198
  }
alpar@484
   199
alpar@485
   200
  SoplexLp::Value SoplexLp::_getCoeff(int i, int j) const {
alpar@484
   201
    return soplex->rowVector(i)[j];
alpar@484
   202
  }
alpar@484
   203
alpar@485
   204
  void SoplexLp::_setColLowerBound(int i, Value value) {
alpar@484
   205
    LEMON_ASSERT(value != INF, "Invalid bound");
alpar@484
   206
    soplex->changeLower(i, value != -INF ? value : -soplex::infinity);
alpar@484
   207
  }
alpar@484
   208
alpar@485
   209
  SoplexLp::Value SoplexLp::_getColLowerBound(int i) const {
alpar@484
   210
    double value = soplex->lower(i);
alpar@484
   211
    return value != -soplex::infinity ? value : -INF;
alpar@484
   212
  }
alpar@484
   213
alpar@485
   214
  void SoplexLp::_setColUpperBound(int i, Value value) {
alpar@484
   215
    LEMON_ASSERT(value != -INF, "Invalid bound");
alpar@484
   216
    soplex->changeUpper(i, value != INF ? value : soplex::infinity);
alpar@484
   217
  }
alpar@484
   218
alpar@485
   219
  SoplexLp::Value SoplexLp::_getColUpperBound(int i) const {
alpar@484
   220
    double value = soplex->upper(i);
alpar@484
   221
    return value != soplex::infinity ? value : INF;
alpar@484
   222
  }
alpar@484
   223
alpar@485
   224
  void SoplexLp::_setRowLowerBound(int i, Value lb) {
alpar@484
   225
    LEMON_ASSERT(lb != INF, "Invalid bound");
alpar@484
   226
    soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity, soplex->rhs(i));
alpar@484
   227
  }
alpar@484
   228
alpar@485
   229
  SoplexLp::Value SoplexLp::_getRowLowerBound(int i) const {
alpar@484
   230
    double res = soplex->lhs(i);
alpar@484
   231
    return res == -soplex::infinity ? -INF : res;
alpar@484
   232
  }
alpar@484
   233
alpar@485
   234
  void SoplexLp::_setRowUpperBound(int i, Value ub) {
alpar@484
   235
    LEMON_ASSERT(ub != -INF, "Invalid bound");
alpar@484
   236
    soplex->changeRange(i, soplex->lhs(i), ub != INF ? ub : soplex::infinity);
alpar@484
   237
  }
alpar@484
   238
alpar@485
   239
  SoplexLp::Value SoplexLp::_getRowUpperBound(int i) const {
alpar@484
   240
    double res = soplex->rhs(i);
alpar@484
   241
    return res == soplex::infinity ? INF : res;
alpar@484
   242
  }
alpar@484
   243
alpar@485
   244
  void SoplexLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
alpar@484
   245
    for (int j = 0; j < soplex->nCols(); ++j) {
alpar@484
   246
      soplex->changeObj(j, 0.0);
alpar@484
   247
    }
alpar@484
   248
    for (ExprIterator it = b; it != e; ++it) {
alpar@484
   249
      soplex->changeObj(it->first, it->second);
alpar@484
   250
    }
alpar@484
   251
  }
alpar@484
   252
alpar@485
   253
  void SoplexLp::_getObjCoeffs(InsertIterator b) const {
alpar@484
   254
    for (int j = 0; j < soplex->nCols(); ++j) {
alpar@484
   255
      Value coef = soplex->obj(j);
alpar@484
   256
      if (coef != 0.0) {
alpar@484
   257
        *b = std::make_pair(j, coef);
alpar@484
   258
        ++b;
alpar@484
   259
      }
alpar@484
   260
    }
alpar@484
   261
  }
alpar@484
   262
alpar@485
   263
  void SoplexLp::_setObjCoeff(int i, Value obj_coef) {
alpar@484
   264
    soplex->changeObj(i, obj_coef);
alpar@484
   265
  }
alpar@484
   266
alpar@485
   267
  SoplexLp::Value SoplexLp::_getObjCoeff(int i) const {
alpar@484
   268
    return soplex->obj(i);
alpar@484
   269
  }
alpar@484
   270
alpar@485
   271
  SoplexLp::SolveExitStatus SoplexLp::_solve() {
alpar@484
   272
alpar@484
   273
    _clear_temporals();
alpar@484
   274
alpar@484
   275
    soplex::SPxSolver::Status status = soplex->solve();
alpar@484
   276
alpar@484
   277
    switch (status) {
alpar@484
   278
    case soplex::SPxSolver::OPTIMAL:
alpar@484
   279
    case soplex::SPxSolver::INFEASIBLE:
alpar@484
   280
    case soplex::SPxSolver::UNBOUNDED:
alpar@484
   281
      return SOLVED;
alpar@484
   282
    default:
alpar@484
   283
      return UNSOLVED;
alpar@484
   284
    }
alpar@484
   285
  }
alpar@484
   286
alpar@485
   287
  SoplexLp::Value SoplexLp::_getPrimal(int i) const {
alpar@484
   288
    if (_primal_values.empty()) {
alpar@484
   289
      _primal_values.resize(soplex->nCols());
alpar@484
   290
      soplex::Vector pv(_primal_values.size(), &_primal_values.front());
alpar@484
   291
      soplex->getPrimal(pv);
alpar@484
   292
    }
alpar@484
   293
    return _primal_values[i];
alpar@484
   294
  }
alpar@484
   295
alpar@485
   296
  SoplexLp::Value SoplexLp::_getDual(int i) const {
alpar@484
   297
    if (_dual_values.empty()) {
alpar@484
   298
      _dual_values.resize(soplex->nRows());
alpar@484
   299
      soplex::Vector dv(_dual_values.size(), &_dual_values.front());
alpar@484
   300
      soplex->getDual(dv);
alpar@484
   301
    }
alpar@484
   302
    return _dual_values[i];
alpar@484
   303
  }
alpar@484
   304
alpar@485
   305
  SoplexLp::Value SoplexLp::_getPrimalValue() const {
alpar@484
   306
    return soplex->objValue();
alpar@484
   307
  }
alpar@484
   308
alpar@485
   309
  SoplexLp::VarStatus SoplexLp::_getColStatus(int i) const {
alpar@484
   310
    switch (soplex->getBasisColStatus(i)) {
alpar@484
   311
    case soplex::SPxSolver::BASIC:
alpar@484
   312
      return BASIC;
alpar@484
   313
    case soplex::SPxSolver::ON_UPPER:
alpar@484
   314
      return UPPER;
alpar@484
   315
    case soplex::SPxSolver::ON_LOWER:
alpar@484
   316
      return LOWER;
alpar@484
   317
    case soplex::SPxSolver::FIXED:
alpar@484
   318
      return FIXED;
alpar@484
   319
    case soplex::SPxSolver::ZERO:
alpar@484
   320
      return FREE;
alpar@484
   321
    default:
alpar@484
   322
      LEMON_ASSERT(false, "Wrong column status");
alpar@484
   323
      return VarStatus();
alpar@484
   324
    }
alpar@484
   325
  }
alpar@484
   326
alpar@485
   327
  SoplexLp::VarStatus SoplexLp::_getRowStatus(int i) const {
alpar@484
   328
    switch (soplex->getBasisRowStatus(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 row status");
alpar@484
   341
      return VarStatus();
alpar@484
   342
    }
alpar@484
   343
  }
alpar@484
   344
alpar@485
   345
  SoplexLp::Value SoplexLp::_getPrimalRay(int i) const {
alpar@484
   346
    if (_primal_ray.empty()) {
alpar@484
   347
      _primal_ray.resize(soplex->nCols());
alpar@484
   348
      soplex::Vector pv(_primal_ray.size(), &_primal_ray.front());
alpar@484
   349
      soplex->getDualfarkas(pv);
alpar@484
   350
    }
alpar@484
   351
    return _primal_ray[i];
alpar@484
   352
  }
alpar@484
   353
alpar@485
   354
  SoplexLp::Value SoplexLp::_getDualRay(int i) const {
alpar@484
   355
    if (_dual_ray.empty()) {
alpar@484
   356
      _dual_ray.resize(soplex->nRows());
alpar@484
   357
      soplex::Vector dv(_dual_ray.size(), &_dual_ray.front());
alpar@484
   358
      soplex->getDualfarkas(dv);
alpar@484
   359
    }
alpar@484
   360
    return _dual_ray[i];
alpar@484
   361
  }
alpar@484
   362
alpar@485
   363
  SoplexLp::ProblemType SoplexLp::_getPrimalType() const {
alpar@484
   364
    switch (soplex->status()) {
alpar@484
   365
    case soplex::SPxSolver::OPTIMAL:
alpar@484
   366
      return OPTIMAL;
alpar@484
   367
    case soplex::SPxSolver::UNBOUNDED:
alpar@484
   368
      return UNBOUNDED;
alpar@484
   369
    case soplex::SPxSolver::INFEASIBLE:
alpar@484
   370
      return INFEASIBLE;
alpar@484
   371
    default:
alpar@484
   372
      return UNDEFINED;
alpar@484
   373
    }
alpar@484
   374
  }
alpar@484
   375
alpar@485
   376
  SoplexLp::ProblemType SoplexLp::_getDualType() const {
alpar@484
   377
    switch (soplex->status()) {
alpar@484
   378
    case soplex::SPxSolver::OPTIMAL:
alpar@484
   379
      return OPTIMAL;
alpar@484
   380
    case soplex::SPxSolver::UNBOUNDED:
alpar@484
   381
      return UNBOUNDED;
alpar@484
   382
    case soplex::SPxSolver::INFEASIBLE:
alpar@484
   383
      return INFEASIBLE;
alpar@484
   384
    default:
alpar@484
   385
      return UNDEFINED;
alpar@484
   386
    }
alpar@484
   387
  }
alpar@484
   388
alpar@485
   389
  void SoplexLp::_setSense(Sense sense) {
alpar@484
   390
    switch (sense) {
alpar@484
   391
    case MIN:
alpar@484
   392
      soplex->changeSense(soplex::SPxSolver::MINIMIZE);
alpar@484
   393
      break;
alpar@484
   394
    case MAX:
alpar@484
   395
      soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
alpar@484
   396
    }
alpar@484
   397
  }
alpar@484
   398
alpar@485
   399
  SoplexLp::Sense SoplexLp::_getSense() const {
alpar@484
   400
    switch (soplex->spxSense()) {
alpar@484
   401
    case soplex::SPxSolver::MAXIMIZE:
alpar@484
   402
      return MAX;
alpar@484
   403
    case soplex::SPxSolver::MINIMIZE:
alpar@484
   404
      return MIN;
alpar@484
   405
    default:
alpar@484
   406
      LEMON_ASSERT(false, "Wrong sense.");
alpar@485
   407
      return SoplexLp::Sense();
alpar@484
   408
    }
alpar@484
   409
  }
alpar@484
   410
alpar@485
   411
  void SoplexLp::_clear() {
alpar@484
   412
    soplex->clear();
alpar@484
   413
    _col_names.clear();
alpar@484
   414
    _col_names_ref.clear();
alpar@484
   415
    _row_names.clear();
alpar@484
   416
    _row_names_ref.clear();
alpar@484
   417
    cols.clear();
alpar@484
   418
    rows.clear();
alpar@484
   419
    _clear_temporals();
alpar@484
   420
  }
alpar@484
   421
alpar@484
   422
} //namespace lemon
alpar@484
   423