lemon/lp_soplex.cc
author deba
Thu, 11 Jan 2007 21:35:14 +0000
changeset 2342 4dd3eb348641
child 2363 2aabce558574
permissions -rw-r--r--
Bug fix
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include<iostream>
    20 #include<lemon/lp_soplex.h>
    21 
    22 #include <soplex/soplex.h>
    23 
    24 
    25 ///\file
    26 ///\brief Implementation of the LEMON-SOPLEX lp solver interface.
    27 namespace lemon {
    28   
    29   LpSoplex::LpSoplex() : LpSolverBase() {
    30     soplex = new soplex::SoPlex;
    31   }
    32   
    33   LpSoplex::~LpSoplex() {
    34     delete soplex;
    35   }
    36   
    37   LpSolverBase &LpSoplex::_newLp() {
    38     LpSoplex* newlp = new LpSoplex();
    39     return *newlp;
    40   }
    41 
    42   LpSolverBase &LpSoplex::_copyLp() {
    43     LpSoplex* newlp = new LpSoplex();
    44     ((soplex::SPxLP&)*(newlp->soplex)) = *soplex;
    45     return *newlp;
    46   }
    47 
    48   int LpSoplex::_addCol() {
    49     soplex::LPCol col;
    50     soplex->addCol(col);
    51 
    52     colNames.push_back(std::string());
    53     primal.push_back(0.0);
    54 
    55     return soplex->nCols() - 1;
    56   }
    57 
    58   int LpSoplex::_addRow() {
    59     soplex::LPRow row;
    60     soplex->addRow(row);
    61 
    62     dual.push_back(0.0);
    63 
    64     return soplex->nRows() - 1;
    65   }
    66 
    67 
    68   void LpSoplex::_eraseCol(int i) {
    69     soplex->removeCol(i);
    70     primal[i] = primal.back();
    71     primal.pop_back();
    72   }
    73   
    74   void LpSoplex::_eraseRow(int i) {
    75     soplex->removeRow(i);
    76     dual[i] = dual.back();
    77     dual.pop_back();
    78   }
    79   
    80   void LpSoplex::_getColName(int col, std::string &name) {
    81     name = colNames[col]; 
    82   }
    83   
    84   void LpSoplex::_setColName(int col, const std::string &name) {
    85     colNames[col] = name; 
    86   }
    87   
    88 
    89   void LpSoplex::_setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) {
    90     for (int j = 0; j < soplex->nCols(); ++j) {
    91       soplex->changeElement(i, j, 0.0);
    92     }
    93     for(LpRowIterator it = b; it != e; ++it) {
    94       soplex->changeElement(i, it->first, it->second);
    95     }
    96   }
    97   
    98   void LpSoplex::_setColCoeffs(int j, LpColIterator b, LpColIterator e) {
    99     for (int i = 0; i < soplex->nRows(); ++i) {
   100       soplex->changeElement(i, j, 0.0);
   101     }
   102     for(LpColIterator it = b; it != e; ++it) {
   103       soplex->changeElement(it->first, j, it->second);
   104     }
   105   }
   106   
   107   void LpSoplex::_setCoeff(int row, int col, Value value) {
   108     soplex->changeElement(row, col, value);
   109   }
   110 
   111   void LpSoplex::_setColLowerBound(int i, Value value) {
   112     soplex->changeLower(i, value); 
   113   }
   114   
   115   void LpSoplex::_setColUpperBound(int i, Value value) {
   116     soplex->changeUpper(i, value); 
   117   }
   118 
   119   void LpSoplex::_setRowBounds(int i, Value lb, Value ub) {
   120     soplex->changeRange(i, lb, ub);
   121   }
   122 
   123   void LpSoplex::_setObjCoeff(int i, Value obj_coef) {
   124     soplex->changeObj(i, obj_coef);
   125   }
   126 
   127   void LpSoplex::_clearObj() {
   128     for (int i = 0; i < soplex->nCols(); ++i) {
   129       soplex->changeObj(i, 0.0);
   130     }
   131   }
   132 
   133   LpSoplex::SolveExitStatus LpSoplex::_solve() {
   134     soplex::SPxSolver::Status status = soplex->solve();
   135 
   136     soplex::Vector pv(primal.size(), &primal[0]);
   137     soplex->getPrimal(pv);
   138 
   139     soplex::Vector dv(dual.size(), &dual[0]);
   140     soplex->getDual(dv);
   141 
   142     switch (status) {
   143     case soplex::SPxSolver::OPTIMAL:
   144     case soplex::SPxSolver::INFEASIBLE:
   145     case soplex::SPxSolver::UNBOUNDED:
   146       return SOLVED;
   147     default:
   148       return UNSOLVED;
   149     }
   150   }
   151 
   152   LpSoplex::Value LpSoplex::_getPrimal(int i) {
   153     return primal[i];
   154   }
   155 
   156   LpSoplex::Value LpSoplex::_getDual(int i) {
   157     return dual[i];
   158   }
   159   
   160   LpSoplex::Value LpSoplex::_getPrimalValue() {
   161     return soplex->objValue();
   162   }
   163 
   164   bool LpSoplex::_isBasicCol(int i) {
   165     return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC;
   166   }  
   167 
   168   LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() {
   169     switch (soplex->status()) {
   170     case soplex::SPxSolver::OPTIMAL:
   171       return OPTIMAL;
   172     case soplex::SPxSolver::UNBOUNDED:
   173       return INFINITE;
   174     case soplex::SPxSolver::INFEASIBLE:
   175       return INFEASIBLE;
   176     default:
   177       return UNDEFINED;
   178     }
   179   }
   180 
   181   LpSoplex::SolutionStatus LpSoplex::_getDualStatus() {
   182     switch (0) {
   183     case 0:
   184       return UNDEFINED;
   185       return OPTIMAL;
   186       return INFEASIBLE;
   187       return UNDEFINED;
   188     }
   189   }
   190 
   191   LpSoplex::ProblemTypes LpSoplex::_getProblemType() {
   192     switch (0) {
   193     case 0:
   194       return PRIMAL_DUAL_FEASIBLE;
   195       return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
   196       return UNKNOWN;
   197     }
   198   }
   199 
   200   void LpSoplex::_setMax() {
   201     soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
   202   }
   203   void LpSoplex::_setMin() {
   204     soplex->changeSense(soplex::SPxSolver::MINIMIZE);
   205   }
   206   
   207 } //namespace lemon
   208