lemon/lp_soplex.cc
author alpar
Tue, 05 Jun 2007 14:48:20 +0000
changeset 2448 ab899ae3505f
parent 2386 81b47fc5c444
child 2553 bfced05fa852
permissions -rw-r--r--
Bugfix and improvement in -tsp2 algorithm
deba@2313
     1
/* -*- C++ -*-
deba@2313
     2
 *
deba@2313
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2313
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
deba@2313
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2313
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2313
     8
 *
deba@2313
     9
 * Permission to use, modify and distribute this software is granted
deba@2313
    10
 * provided that this copyright notice appears in all copies. For
deba@2313
    11
 * precise terms see the accompanying LICENSE file.
deba@2313
    12
 *
deba@2313
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2313
    14
 * express or implied, and with no claim as to its suitability for any
deba@2313
    15
 * purpose.
deba@2313
    16
 *
deba@2313
    17
 */
deba@2313
    18
deba@2313
    19
#include<iostream>
deba@2313
    20
#include<lemon/lp_soplex.h>
deba@2313
    21
deba@2313
    22
#include <soplex/soplex.h>
deba@2313
    23
deba@2313
    24
deba@2313
    25
///\file
deba@2313
    26
///\brief Implementation of the LEMON-SOPLEX lp solver interface.
deba@2313
    27
namespace lemon {
deba@2313
    28
  
deba@2313
    29
  LpSoplex::LpSoplex() : LpSolverBase() {
deba@2363
    30
    rows.setIdHandler(relocateIdHandler);
deba@2363
    31
    cols.setIdHandler(relocateIdHandler);
deba@2313
    32
    soplex = new soplex::SoPlex;
deba@2363
    33
    solved = false;
deba@2313
    34
  }
deba@2313
    35
  
deba@2313
    36
  LpSoplex::~LpSoplex() {
deba@2313
    37
    delete soplex;
deba@2313
    38
  }
deba@2313
    39
  
deba@2313
    40
  LpSolverBase &LpSoplex::_newLp() {
deba@2313
    41
    LpSoplex* newlp = new LpSoplex();
deba@2313
    42
    return *newlp;
deba@2313
    43
  }
deba@2313
    44
deba@2313
    45
  LpSolverBase &LpSoplex::_copyLp() {
deba@2313
    46
    LpSoplex* newlp = new LpSoplex();
deba@2386
    47
    (*static_cast<soplex::SPxLP*>(newlp->soplex)) = *soplex;
deba@2313
    48
    return *newlp;
deba@2313
    49
  }
deba@2313
    50
deba@2313
    51
  int LpSoplex::_addCol() {
deba@2386
    52
    soplex::LPCol c;
deba@2386
    53
    c.setLower(-soplex::infinity);
deba@2386
    54
    c.setUpper(soplex::infinity);
deba@2386
    55
    soplex->addCol(c);
deba@2313
    56
deba@2313
    57
    colNames.push_back(std::string());
deba@2363
    58
    primal_value.push_back(0.0);
deba@2363
    59
    solved = false;
deba@2313
    60
deba@2313
    61
    return soplex->nCols() - 1;
deba@2313
    62
  }
deba@2313
    63
deba@2313
    64
  int LpSoplex::_addRow() {
deba@2386
    65
    soplex::LPRow r;
deba@2386
    66
    r.setLhs(-soplex::infinity);
deba@2386
    67
    r.setRhs(soplex::infinity);
deba@2386
    68
    soplex->addRow(r);
deba@2313
    69
deba@2363
    70
    dual_value.push_back(0.0);
deba@2363
    71
    solved = false;
deba@2313
    72
deba@2313
    73
    return soplex->nRows() - 1;
deba@2313
    74
  }
deba@2313
    75
deba@2313
    76
deba@2313
    77
  void LpSoplex::_eraseCol(int i) {
deba@2313
    78
    soplex->removeCol(i);
deba@2368
    79
    invColNames.erase(colNames[i]);
deba@2363
    80
    colNames[i] = colNames.back();
deba@2368
    81
    invColNames[colNames.back()] = i;
deba@2363
    82
    colNames.pop_back();
deba@2363
    83
    primal_value[i] = primal_value.back();
deba@2363
    84
    primal_value.pop_back();
deba@2363
    85
    solved = false;
deba@2313
    86
  }
deba@2313
    87
  
deba@2313
    88
  void LpSoplex::_eraseRow(int i) {
deba@2313
    89
    soplex->removeRow(i);
deba@2363
    90
    dual_value[i] = dual_value.back();
deba@2363
    91
    dual_value.pop_back();
deba@2363
    92
    solved = false;
deba@2313
    93
  }
deba@2313
    94
  
deba@2386
    95
  void LpSoplex::_getColName(int c, std::string &name) const {
deba@2386
    96
    name = colNames[c]; 
deba@2313
    97
  }
deba@2313
    98
  
deba@2386
    99
  void LpSoplex::_setColName(int c, const std::string &name) {
deba@2386
   100
    invColNames.erase(colNames[c]);
deba@2386
   101
    colNames[c] = name; 
deba@2366
   102
    if (!name.empty()) {
deba@2386
   103
      invColNames.insert(std::make_pair(name, c));
deba@2366
   104
    }
deba@2366
   105
  }
deba@2366
   106
deba@2366
   107
  int LpSoplex::_colByName(const std::string& name) const {
deba@2366
   108
    std::map<std::string, int>::const_iterator it =
deba@2366
   109
      invColNames.find(name);
deba@2366
   110
    if (it != invColNames.end()) {
deba@2366
   111
      return it->second;
deba@2366
   112
    } else {
deba@2366
   113
      return -1;
deba@2366
   114
    }
deba@2313
   115
  }
deba@2313
   116
  
deba@2313
   117
deba@2364
   118
  void LpSoplex::_setRowCoeffs(int i, ConstRowIterator b, ConstRowIterator e) {
deba@2313
   119
    for (int j = 0; j < soplex->nCols(); ++j) {
deba@2313
   120
      soplex->changeElement(i, j, 0.0);
deba@2313
   121
    }
deba@2364
   122
    for(ConstRowIterator it = b; it != e; ++it) {
deba@2313
   123
      soplex->changeElement(i, it->first, it->second);
deba@2313
   124
    }
deba@2363
   125
    solved = false;
deba@2313
   126
  }
deba@2364
   127
deba@2366
   128
  void LpSoplex::_getRowCoeffs(int i, RowIterator b) const {
deba@2364
   129
    const soplex::SVector& vec = soplex->rowVector(i);
deba@2364
   130
    for (int k = 0; k < vec.size(); ++k) {
deba@2364
   131
      *b = std::make_pair(vec.index(k), vec.value(k));
deba@2364
   132
      ++b;
deba@2364
   133
    }
deba@2364
   134
  }
deba@2313
   135
  
deba@2364
   136
  void LpSoplex::_setColCoeffs(int j, ConstColIterator b, ConstColIterator e) {
deba@2313
   137
    for (int i = 0; i < soplex->nRows(); ++i) {
deba@2313
   138
      soplex->changeElement(i, j, 0.0);
deba@2313
   139
    }
deba@2364
   140
    for(ConstColIterator it = b; it != e; ++it) {
deba@2313
   141
      soplex->changeElement(it->first, j, it->second);
deba@2313
   142
    }
deba@2363
   143
    solved = false;
deba@2313
   144
  }
deba@2364
   145
deba@2366
   146
  void LpSoplex::_getColCoeffs(int i, ColIterator b) const {
deba@2364
   147
    const soplex::SVector& vec = soplex->colVector(i);
deba@2364
   148
    for (int k = 0; k < vec.size(); ++k) {
deba@2364
   149
      *b = std::make_pair(vec.index(k), vec.value(k));
deba@2364
   150
      ++b;
deba@2364
   151
    }
deba@2364
   152
  }
deba@2313
   153
  
deba@2363
   154
  void LpSoplex::_setCoeff(int i, int j, Value value) {
deba@2363
   155
    soplex->changeElement(i, j, value);
deba@2363
   156
    solved = false;
deba@2363
   157
  }
deba@2363
   158
deba@2366
   159
  LpSoplex::Value LpSoplex::_getCoeff(int i, int j) const {
deba@2363
   160
    return soplex->rowVector(i)[j];
deba@2313
   161
  }
deba@2313
   162
deba@2313
   163
  void LpSoplex::_setColLowerBound(int i, Value value) {
deba@2363
   164
    soplex->changeLower(i, value != -INF ? value : -soplex::infinity); 
deba@2363
   165
    solved = false;
deba@2313
   166
  }
deba@2313
   167
  
deba@2366
   168
  LpSoplex::Value LpSoplex::_getColLowerBound(int i) const {
deba@2363
   169
    double value = soplex->lower(i);
deba@2363
   170
    return value != -soplex::infinity ? value : -INF;
deba@2363
   171
  }
deba@2363
   172
deba@2313
   173
  void LpSoplex::_setColUpperBound(int i, Value value) {
deba@2363
   174
    soplex->changeUpper(i, value != INF ? value : soplex::infinity); 
deba@2363
   175
    solved = false;
deba@2363
   176
  }
deba@2363
   177
deba@2366
   178
  LpSoplex::Value LpSoplex::_getColUpperBound(int i) const {
deba@2363
   179
    double value = soplex->upper(i);
deba@2363
   180
    return value != soplex::infinity ? value : INF;
deba@2313
   181
  }
deba@2313
   182
deba@2313
   183
  void LpSoplex::_setRowBounds(int i, Value lb, Value ub) {
deba@2363
   184
    soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity,
deba@2363
   185
                        ub != INF ? ub : soplex::infinity);
deba@2363
   186
    solved = false;
deba@2363
   187
  }
deba@2366
   188
  void LpSoplex::_getRowBounds(int i, Value &lower, Value &upper) const {
deba@2363
   189
    lower = soplex->lhs(i);
deba@2363
   190
    if (lower == -soplex::infinity) lower = -INF;
deba@2363
   191
    upper = soplex->rhs(i);
deba@2363
   192
    if (upper == -soplex::infinity) upper = INF;
deba@2313
   193
  }
deba@2313
   194
deba@2313
   195
  void LpSoplex::_setObjCoeff(int i, Value obj_coef) {
deba@2313
   196
    soplex->changeObj(i, obj_coef);
deba@2363
   197
    solved = false;
deba@2363
   198
  }
deba@2363
   199
deba@2366
   200
  LpSoplex::Value LpSoplex::_getObjCoeff(int i) const {
deba@2363
   201
    return soplex->obj(i);
deba@2313
   202
  }
deba@2313
   203
deba@2313
   204
  void LpSoplex::_clearObj() {
deba@2313
   205
    for (int i = 0; i < soplex->nCols(); ++i) {
deba@2313
   206
      soplex->changeObj(i, 0.0);
deba@2313
   207
    }
deba@2363
   208
    solved = false;
deba@2313
   209
  }
deba@2313
   210
deba@2313
   211
  LpSoplex::SolveExitStatus LpSoplex::_solve() {
deba@2313
   212
    soplex::SPxSolver::Status status = soplex->solve();
deba@2313
   213
deba@2363
   214
    soplex::Vector pv(primal_value.size(), &primal_value[0]);
deba@2313
   215
    soplex->getPrimal(pv);
deba@2313
   216
deba@2363
   217
    soplex::Vector dv(dual_value.size(), &dual_value[0]);
deba@2313
   218
    soplex->getDual(dv);
deba@2313
   219
deba@2313
   220
    switch (status) {
deba@2313
   221
    case soplex::SPxSolver::OPTIMAL:
deba@2313
   222
    case soplex::SPxSolver::INFEASIBLE:
deba@2313
   223
    case soplex::SPxSolver::UNBOUNDED:
deba@2363
   224
      solved = true;
deba@2313
   225
      return SOLVED;
deba@2313
   226
    default:
deba@2313
   227
      return UNSOLVED;
deba@2313
   228
    }
deba@2313
   229
  }
deba@2313
   230
deba@2366
   231
  LpSoplex::Value LpSoplex::_getPrimal(int i) const {
deba@2363
   232
    return primal_value[i];
deba@2313
   233
  }
deba@2313
   234
deba@2366
   235
  LpSoplex::Value LpSoplex::_getDual(int i) const {
deba@2363
   236
    return dual_value[i];
deba@2313
   237
  }
deba@2313
   238
  
deba@2366
   239
  LpSoplex::Value LpSoplex::_getPrimalValue() const {
deba@2313
   240
    return soplex->objValue();
deba@2313
   241
  }
deba@2313
   242
deba@2366
   243
  bool LpSoplex::_isBasicCol(int i) const {
deba@2313
   244
    return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC;
deba@2313
   245
  }  
deba@2313
   246
deba@2366
   247
  LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() const {
deba@2363
   248
    if (!solved) return UNDEFINED;
deba@2313
   249
    switch (soplex->status()) {
deba@2313
   250
    case soplex::SPxSolver::OPTIMAL:
deba@2313
   251
      return OPTIMAL;
deba@2313
   252
    case soplex::SPxSolver::UNBOUNDED:
deba@2313
   253
      return INFINITE;
deba@2313
   254
    case soplex::SPxSolver::INFEASIBLE:
deba@2313
   255
      return INFEASIBLE;
deba@2313
   256
    default:
deba@2313
   257
      return UNDEFINED;
deba@2313
   258
    }
deba@2313
   259
  }
deba@2313
   260
deba@2366
   261
  LpSoplex::SolutionStatus LpSoplex::_getDualStatus() const {
deba@2363
   262
    if (!solved) return UNDEFINED;
deba@2363
   263
    switch (soplex->status()) {
deba@2363
   264
    case soplex::SPxSolver::OPTIMAL:
deba@2313
   265
      return OPTIMAL;
deba@2363
   266
    case soplex::SPxSolver::UNBOUNDED:
deba@2313
   267
      return INFEASIBLE;
deba@2363
   268
    default:
deba@2313
   269
      return UNDEFINED;
deba@2313
   270
    }
deba@2313
   271
  }
deba@2313
   272
deba@2366
   273
  LpSoplex::ProblemTypes LpSoplex::_getProblemType() const {
deba@2363
   274
    if (!solved) return UNKNOWN;
deba@2363
   275
    switch (soplex->status()) {
deba@2363
   276
    case soplex::SPxSolver::OPTIMAL:
deba@2313
   277
      return PRIMAL_DUAL_FEASIBLE;
deba@2363
   278
    case soplex::SPxSolver::UNBOUNDED:
deba@2313
   279
      return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
deba@2363
   280
    default:
deba@2313
   281
      return UNKNOWN;
deba@2313
   282
    }
deba@2313
   283
  }
deba@2313
   284
deba@2313
   285
  void LpSoplex::_setMax() {
deba@2313
   286
    soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
deba@2363
   287
    solved = false;
deba@2313
   288
  }
deba@2313
   289
  void LpSoplex::_setMin() {
deba@2313
   290
    soplex->changeSense(soplex::SPxSolver::MINIMIZE);
deba@2363
   291
    solved = false;
deba@2313
   292
  }
deba@2366
   293
  bool LpSoplex::_isMax() const {
deba@2363
   294
    return soplex->spxSense() == soplex::SPxSolver::MAXIMIZE;
deba@2363
   295
  }
deba@2363
   296
deba@2313
   297
  
deba@2313
   298
} //namespace lemon
deba@2313
   299