lemon/lp_soplex.cc
author deba
Thu, 15 Feb 2007 13:06:23 +0000
changeset 2362 eb37b9774ef6
child 2363 2aabce558574
permissions -rw-r--r--
Small changes
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
 *
deba@2313
     5
 * Copyright (C) 2003-2006
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@2313
    30
    soplex = new soplex::SoPlex;
deba@2313
    31
  }
deba@2313
    32
  
deba@2313
    33
  LpSoplex::~LpSoplex() {
deba@2313
    34
    delete soplex;
deba@2313
    35
  }
deba@2313
    36
  
deba@2313
    37
  LpSolverBase &LpSoplex::_newLp() {
deba@2313
    38
    LpSoplex* newlp = new LpSoplex();
deba@2313
    39
    return *newlp;
deba@2313
    40
  }
deba@2313
    41
deba@2313
    42
  LpSolverBase &LpSoplex::_copyLp() {
deba@2313
    43
    LpSoplex* newlp = new LpSoplex();
deba@2313
    44
    ((soplex::SPxLP&)*(newlp->soplex)) = *soplex;
deba@2313
    45
    return *newlp;
deba@2313
    46
  }
deba@2313
    47
deba@2313
    48
  int LpSoplex::_addCol() {
deba@2313
    49
    soplex::LPCol col;
deba@2313
    50
    soplex->addCol(col);
deba@2313
    51
deba@2313
    52
    colNames.push_back(std::string());
deba@2313
    53
    primal.push_back(0.0);
deba@2313
    54
deba@2313
    55
    return soplex->nCols() - 1;
deba@2313
    56
  }
deba@2313
    57
deba@2313
    58
  int LpSoplex::_addRow() {
deba@2313
    59
    soplex::LPRow row;
deba@2313
    60
    soplex->addRow(row);
deba@2313
    61
deba@2313
    62
    dual.push_back(0.0);
deba@2313
    63
deba@2313
    64
    return soplex->nRows() - 1;
deba@2313
    65
  }
deba@2313
    66
deba@2313
    67
deba@2313
    68
  void LpSoplex::_eraseCol(int i) {
deba@2313
    69
    soplex->removeCol(i);
deba@2313
    70
    primal[i] = primal.back();
deba@2313
    71
    primal.pop_back();
deba@2313
    72
  }
deba@2313
    73
  
deba@2313
    74
  void LpSoplex::_eraseRow(int i) {
deba@2313
    75
    soplex->removeRow(i);
deba@2313
    76
    dual[i] = dual.back();
deba@2313
    77
    dual.pop_back();
deba@2313
    78
  }
deba@2313
    79
  
deba@2313
    80
  void LpSoplex::_getColName(int col, std::string &name) {
deba@2313
    81
    name = colNames[col]; 
deba@2313
    82
  }
deba@2313
    83
  
deba@2313
    84
  void LpSoplex::_setColName(int col, const std::string &name) {
deba@2313
    85
    colNames[col] = name; 
deba@2313
    86
  }
deba@2313
    87
  
deba@2313
    88
deba@2313
    89
  void LpSoplex::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) {
deba@2313
    90
    for (int j = 0; j < soplex->nCols(); ++j) {
deba@2313
    91
      soplex->changeElement(i, j, 0.0);
deba@2313
    92
    }
deba@2313
    93
    for(LpRowIterator it = b; it != e; ++it) {
deba@2313
    94
      soplex->changeElement(i, it->first, it->second);
deba@2313
    95
    }
deba@2313
    96
  }
deba@2313
    97
  
deba@2313
    98
  void LpSoplex::_setColCoeffs(int j, LpColIterator b, LpColIterator e) {
deba@2313
    99
    for (int i = 0; i < soplex->nRows(); ++i) {
deba@2313
   100
      soplex->changeElement(i, j, 0.0);
deba@2313
   101
    }
deba@2313
   102
    for(LpColIterator it = b; it != e; ++it) {
deba@2313
   103
      soplex->changeElement(it->first, j, it->second);
deba@2313
   104
    }
deba@2313
   105
  }
deba@2313
   106
  
deba@2313
   107
  void LpSoplex::_setCoeff(int row, int col, Value value) {
deba@2313
   108
    soplex->changeElement(row, col, value);
deba@2313
   109
  }
deba@2313
   110
deba@2313
   111
  void LpSoplex::_setColLowerBound(int i, Value value) {
deba@2313
   112
    soplex->changeLower(i, value); 
deba@2313
   113
  }
deba@2313
   114
  
deba@2313
   115
  void LpSoplex::_setColUpperBound(int i, Value value) {
deba@2313
   116
    soplex->changeUpper(i, value); 
deba@2313
   117
  }
deba@2313
   118
deba@2313
   119
  void LpSoplex::_setRowBounds(int i, Value lb, Value ub) {
deba@2313
   120
    soplex->changeRange(i, lb, ub);
deba@2313
   121
  }
deba@2313
   122
deba@2313
   123
  void LpSoplex::_setObjCoeff(int i, Value obj_coef) {
deba@2313
   124
    soplex->changeObj(i, obj_coef);
deba@2313
   125
  }
deba@2313
   126
deba@2313
   127
  void LpSoplex::_clearObj() {
deba@2313
   128
    for (int i = 0; i < soplex->nCols(); ++i) {
deba@2313
   129
      soplex->changeObj(i, 0.0);
deba@2313
   130
    }
deba@2313
   131
  }
deba@2313
   132
deba@2313
   133
  LpSoplex::SolveExitStatus LpSoplex::_solve() {
deba@2313
   134
    soplex::SPxSolver::Status status = soplex->solve();
deba@2313
   135
deba@2313
   136
    soplex::Vector pv(primal.size(), &primal[0]);
deba@2313
   137
    soplex->getPrimal(pv);
deba@2313
   138
deba@2313
   139
    soplex::Vector dv(dual.size(), &dual[0]);
deba@2313
   140
    soplex->getDual(dv);
deba@2313
   141
deba@2313
   142
    switch (status) {
deba@2313
   143
    case soplex::SPxSolver::OPTIMAL:
deba@2313
   144
    case soplex::SPxSolver::INFEASIBLE:
deba@2313
   145
    case soplex::SPxSolver::UNBOUNDED:
deba@2313
   146
      return SOLVED;
deba@2313
   147
    default:
deba@2313
   148
      return UNSOLVED;
deba@2313
   149
    }
deba@2313
   150
  }
deba@2313
   151
deba@2313
   152
  LpSoplex::Value LpSoplex::_getPrimal(int i) {
deba@2313
   153
    return primal[i];
deba@2313
   154
  }
deba@2313
   155
deba@2313
   156
  LpSoplex::Value LpSoplex::_getDual(int i) {
deba@2313
   157
    return dual[i];
deba@2313
   158
  }
deba@2313
   159
  
deba@2313
   160
  LpSoplex::Value LpSoplex::_getPrimalValue() {
deba@2313
   161
    return soplex->objValue();
deba@2313
   162
  }
deba@2313
   163
deba@2313
   164
  bool LpSoplex::_isBasicCol(int i) {
deba@2313
   165
    return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC;
deba@2313
   166
  }  
deba@2313
   167
deba@2313
   168
  LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() {
deba@2313
   169
    switch (soplex->status()) {
deba@2313
   170
    case soplex::SPxSolver::OPTIMAL:
deba@2313
   171
      return OPTIMAL;
deba@2313
   172
    case soplex::SPxSolver::UNBOUNDED:
deba@2313
   173
      return INFINITE;
deba@2313
   174
    case soplex::SPxSolver::INFEASIBLE:
deba@2313
   175
      return INFEASIBLE;
deba@2313
   176
    default:
deba@2313
   177
      return UNDEFINED;
deba@2313
   178
    }
deba@2313
   179
  }
deba@2313
   180
deba@2313
   181
  LpSoplex::SolutionStatus LpSoplex::_getDualStatus() {
deba@2313
   182
    switch (0) {
deba@2313
   183
    case 0:
deba@2313
   184
      return UNDEFINED;
deba@2313
   185
      return OPTIMAL;
deba@2313
   186
      return INFEASIBLE;
deba@2313
   187
      return UNDEFINED;
deba@2313
   188
    }
deba@2313
   189
  }
deba@2313
   190
deba@2313
   191
  LpSoplex::ProblemTypes LpSoplex::_getProblemType() {
deba@2313
   192
    switch (0) {
deba@2313
   193
    case 0:
deba@2313
   194
      return PRIMAL_DUAL_FEASIBLE;
deba@2313
   195
      return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
deba@2313
   196
      return UNKNOWN;
deba@2313
   197
    }
deba@2313
   198
  }
deba@2313
   199
deba@2313
   200
  void LpSoplex::_setMax() {
deba@2313
   201
    soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
deba@2313
   202
  }
deba@2313
   203
  void LpSoplex::_setMin() {
deba@2313
   204
    soplex->changeSense(soplex::SPxSolver::MINIMIZE);
deba@2313
   205
  }
deba@2313
   206
  
deba@2313
   207
} //namespace lemon
deba@2313
   208