lemon/lp_soplex.cc
changeset 2363 2aabce558574
parent 2313 d9daf826e28a
child 2364 3a5e67bd42d2
equal deleted inserted replaced
0:751e06bd8d95 1:5a530509dfa5
    25 ///\file
    25 ///\file
    26 ///\brief Implementation of the LEMON-SOPLEX lp solver interface.
    26 ///\brief Implementation of the LEMON-SOPLEX lp solver interface.
    27 namespace lemon {
    27 namespace lemon {
    28   
    28   
    29   LpSoplex::LpSoplex() : LpSolverBase() {
    29   LpSoplex::LpSoplex() : LpSolverBase() {
       
    30     rows.setIdHandler(relocateIdHandler);
       
    31     cols.setIdHandler(relocateIdHandler);
    30     soplex = new soplex::SoPlex;
    32     soplex = new soplex::SoPlex;
       
    33     solved = false;
    31   }
    34   }
    32   
    35   
    33   LpSoplex::~LpSoplex() {
    36   LpSoplex::~LpSoplex() {
    34     delete soplex;
    37     delete soplex;
    35   }
    38   }
    45     return *newlp;
    48     return *newlp;
    46   }
    49   }
    47 
    50 
    48   int LpSoplex::_addCol() {
    51   int LpSoplex::_addCol() {
    49     soplex::LPCol col;
    52     soplex::LPCol col;
       
    53     col.setLower(-soplex::infinity);
       
    54     col.setUpper(soplex::infinity);
    50     soplex->addCol(col);
    55     soplex->addCol(col);
    51 
    56 
    52     colNames.push_back(std::string());
    57     colNames.push_back(std::string());
    53     primal.push_back(0.0);
    58     primal_value.push_back(0.0);
       
    59     solved = false;
    54 
    60 
    55     return soplex->nCols() - 1;
    61     return soplex->nCols() - 1;
    56   }
    62   }
    57 
    63 
    58   int LpSoplex::_addRow() {
    64   int LpSoplex::_addRow() {
    59     soplex::LPRow row;
    65     soplex::LPRow row;
       
    66     row.setLhs(-soplex::infinity);
       
    67     row.setRhs(soplex::infinity);
    60     soplex->addRow(row);
    68     soplex->addRow(row);
    61 
    69 
    62     dual.push_back(0.0);
    70     dual_value.push_back(0.0);
       
    71     solved = false;
    63 
    72 
    64     return soplex->nRows() - 1;
    73     return soplex->nRows() - 1;
    65   }
    74   }
    66 
    75 
    67 
    76 
    68   void LpSoplex::_eraseCol(int i) {
    77   void LpSoplex::_eraseCol(int i) {
    69     soplex->removeCol(i);
    78     soplex->removeCol(i);
    70     primal[i] = primal.back();
    79     colNames[i] = colNames.back();
    71     primal.pop_back();
    80     colNames.pop_back();
       
    81     primal_value[i] = primal_value.back();
       
    82     primal_value.pop_back();
       
    83     solved = false;
    72   }
    84   }
    73   
    85   
    74   void LpSoplex::_eraseRow(int i) {
    86   void LpSoplex::_eraseRow(int i) {
    75     soplex->removeRow(i);
    87     soplex->removeRow(i);
    76     dual[i] = dual.back();
    88     dual_value[i] = dual_value.back();
    77     dual.pop_back();
    89     dual_value.pop_back();
       
    90     solved = false;
    78   }
    91   }
    79   
    92   
    80   void LpSoplex::_getColName(int col, std::string &name) {
    93   void LpSoplex::_getColName(int col, std::string &name) {
    81     name = colNames[col]; 
    94     name = colNames[col]; 
    82   }
    95   }
    91       soplex->changeElement(i, j, 0.0);
   104       soplex->changeElement(i, j, 0.0);
    92     }
   105     }
    93     for(LpRowIterator it = b; it != e; ++it) {
   106     for(LpRowIterator it = b; it != e; ++it) {
    94       soplex->changeElement(i, it->first, it->second);
   107       soplex->changeElement(i, it->first, it->second);
    95     }
   108     }
       
   109     solved = false;
    96   }
   110   }
    97   
   111   
    98   void LpSoplex::_setColCoeffs(int j, LpColIterator b, LpColIterator e) {
   112   void LpSoplex::_setColCoeffs(int j, LpColIterator b, LpColIterator e) {
    99     for (int i = 0; i < soplex->nRows(); ++i) {
   113     for (int i = 0; i < soplex->nRows(); ++i) {
   100       soplex->changeElement(i, j, 0.0);
   114       soplex->changeElement(i, j, 0.0);
   101     }
   115     }
   102     for(LpColIterator it = b; it != e; ++it) {
   116     for(LpColIterator it = b; it != e; ++it) {
   103       soplex->changeElement(it->first, j, it->second);
   117       soplex->changeElement(it->first, j, it->second);
   104     }
   118     }
   105   }
   119     solved = false;
   106   
   120   }
   107   void LpSoplex::_setCoeff(int row, int col, Value value) {
   121   
   108     soplex->changeElement(row, col, value);
   122   void LpSoplex::_setCoeff(int i, int j, Value value) {
       
   123     soplex->changeElement(i, j, value);
       
   124     solved = false;
       
   125   }
       
   126 
       
   127   LpSoplex::Value LpSoplex::_getCoeff(int i, int j) {
       
   128     return soplex->rowVector(i)[j];
   109   }
   129   }
   110 
   130 
   111   void LpSoplex::_setColLowerBound(int i, Value value) {
   131   void LpSoplex::_setColLowerBound(int i, Value value) {
   112     soplex->changeLower(i, value); 
   132     soplex->changeLower(i, value != -INF ? value : -soplex::infinity); 
   113   }
   133     solved = false;
   114   
   134   }
       
   135   
       
   136   LpSoplex::Value LpSoplex::_getColLowerBound(int i) {
       
   137     double value = soplex->lower(i);
       
   138     return value != -soplex::infinity ? value : -INF;
       
   139   }
       
   140 
   115   void LpSoplex::_setColUpperBound(int i, Value value) {
   141   void LpSoplex::_setColUpperBound(int i, Value value) {
   116     soplex->changeUpper(i, value); 
   142     soplex->changeUpper(i, value != INF ? value : soplex::infinity); 
       
   143     solved = false;
       
   144   }
       
   145 
       
   146   LpSoplex::Value LpSoplex::_getColUpperBound(int i) {
       
   147     double value = soplex->upper(i);
       
   148     return value != soplex::infinity ? value : INF;
   117   }
   149   }
   118 
   150 
   119   void LpSoplex::_setRowBounds(int i, Value lb, Value ub) {
   151   void LpSoplex::_setRowBounds(int i, Value lb, Value ub) {
   120     soplex->changeRange(i, lb, ub);
   152     soplex->changeRange(i, lb != -INF ? lb : -soplex::infinity,
       
   153                         ub != INF ? ub : soplex::infinity);
       
   154     solved = false;
       
   155   }
       
   156   void LpSoplex::_getRowBounds(int i, Value &lower, Value &upper) {
       
   157     lower = soplex->lhs(i);
       
   158     if (lower == -soplex::infinity) lower = -INF;
       
   159     upper = soplex->rhs(i);
       
   160     if (upper == -soplex::infinity) upper = INF;
   121   }
   161   }
   122 
   162 
   123   void LpSoplex::_setObjCoeff(int i, Value obj_coef) {
   163   void LpSoplex::_setObjCoeff(int i, Value obj_coef) {
   124     soplex->changeObj(i, obj_coef);
   164     soplex->changeObj(i, obj_coef);
       
   165     solved = false;
       
   166   }
       
   167 
       
   168   LpSoplex::Value LpSoplex::_getObjCoeff(int i) {
       
   169     return soplex->obj(i);
   125   }
   170   }
   126 
   171 
   127   void LpSoplex::_clearObj() {
   172   void LpSoplex::_clearObj() {
   128     for (int i = 0; i < soplex->nCols(); ++i) {
   173     for (int i = 0; i < soplex->nCols(); ++i) {
   129       soplex->changeObj(i, 0.0);
   174       soplex->changeObj(i, 0.0);
   130     }
   175     }
       
   176     solved = false;
   131   }
   177   }
   132 
   178 
   133   LpSoplex::SolveExitStatus LpSoplex::_solve() {
   179   LpSoplex::SolveExitStatus LpSoplex::_solve() {
   134     soplex::SPxSolver::Status status = soplex->solve();
   180     soplex::SPxSolver::Status status = soplex->solve();
   135 
   181 
   136     soplex::Vector pv(primal.size(), &primal[0]);
   182     soplex::Vector pv(primal_value.size(), &primal_value[0]);
   137     soplex->getPrimal(pv);
   183     soplex->getPrimal(pv);
   138 
   184 
   139     soplex::Vector dv(dual.size(), &dual[0]);
   185     soplex::Vector dv(dual_value.size(), &dual_value[0]);
   140     soplex->getDual(dv);
   186     soplex->getDual(dv);
   141 
   187 
   142     switch (status) {
   188     switch (status) {
   143     case soplex::SPxSolver::OPTIMAL:
   189     case soplex::SPxSolver::OPTIMAL:
   144     case soplex::SPxSolver::INFEASIBLE:
   190     case soplex::SPxSolver::INFEASIBLE:
   145     case soplex::SPxSolver::UNBOUNDED:
   191     case soplex::SPxSolver::UNBOUNDED:
       
   192       solved = true;
   146       return SOLVED;
   193       return SOLVED;
   147     default:
   194     default:
   148       return UNSOLVED;
   195       return UNSOLVED;
   149     }
   196     }
   150   }
   197   }
   151 
   198 
   152   LpSoplex::Value LpSoplex::_getPrimal(int i) {
   199   LpSoplex::Value LpSoplex::_getPrimal(int i) {
   153     return primal[i];
   200     return primal_value[i];
   154   }
   201   }
   155 
   202 
   156   LpSoplex::Value LpSoplex::_getDual(int i) {
   203   LpSoplex::Value LpSoplex::_getDual(int i) {
   157     return dual[i];
   204     return dual_value[i];
   158   }
   205   }
   159   
   206   
   160   LpSoplex::Value LpSoplex::_getPrimalValue() {
   207   LpSoplex::Value LpSoplex::_getPrimalValue() {
   161     return soplex->objValue();
   208     return soplex->objValue();
   162   }
   209   }
   164   bool LpSoplex::_isBasicCol(int i) {
   211   bool LpSoplex::_isBasicCol(int i) {
   165     return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC;
   212     return soplex->getBasisColStatus(i) == soplex::SPxSolver::BASIC;
   166   }  
   213   }  
   167 
   214 
   168   LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() {
   215   LpSoplex::SolutionStatus LpSoplex::_getPrimalStatus() {
       
   216     if (!solved) return UNDEFINED;
   169     switch (soplex->status()) {
   217     switch (soplex->status()) {
   170     case soplex::SPxSolver::OPTIMAL:
   218     case soplex::SPxSolver::OPTIMAL:
   171       return OPTIMAL;
   219       return OPTIMAL;
   172     case soplex::SPxSolver::UNBOUNDED:
   220     case soplex::SPxSolver::UNBOUNDED:
   173       return INFINITE;
   221       return INFINITE;
   177       return UNDEFINED;
   225       return UNDEFINED;
   178     }
   226     }
   179   }
   227   }
   180 
   228 
   181   LpSoplex::SolutionStatus LpSoplex::_getDualStatus() {
   229   LpSoplex::SolutionStatus LpSoplex::_getDualStatus() {
   182     switch (0) {
   230     if (!solved) return UNDEFINED;
   183     case 0:
   231     switch (soplex->status()) {
       
   232     case soplex::SPxSolver::OPTIMAL:
       
   233       return OPTIMAL;
       
   234     case soplex::SPxSolver::UNBOUNDED:
       
   235       return INFEASIBLE;
       
   236     default:
   184       return UNDEFINED;
   237       return UNDEFINED;
   185       return OPTIMAL;
       
   186       return INFEASIBLE;
       
   187       return UNDEFINED;
       
   188     }
   238     }
   189   }
   239   }
   190 
   240 
   191   LpSoplex::ProblemTypes LpSoplex::_getProblemType() {
   241   LpSoplex::ProblemTypes LpSoplex::_getProblemType() {
   192     switch (0) {
   242     if (!solved) return UNKNOWN;
   193     case 0:
   243     switch (soplex->status()) {
       
   244     case soplex::SPxSolver::OPTIMAL:
   194       return PRIMAL_DUAL_FEASIBLE;
   245       return PRIMAL_DUAL_FEASIBLE;
       
   246     case soplex::SPxSolver::UNBOUNDED:
   195       return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
   247       return PRIMAL_FEASIBLE_DUAL_INFEASIBLE;
       
   248     default:
   196       return UNKNOWN;
   249       return UNKNOWN;
   197     }
   250     }
   198   }
   251   }
   199 
   252 
   200   void LpSoplex::_setMax() {
   253   void LpSoplex::_setMax() {
   201     soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
   254     soplex->changeSense(soplex::SPxSolver::MAXIMIZE);
       
   255     solved = false;
   202   }
   256   }
   203   void LpSoplex::_setMin() {
   257   void LpSoplex::_setMin() {
   204     soplex->changeSense(soplex::SPxSolver::MINIMIZE);
   258     soplex->changeSense(soplex::SPxSolver::MINIMIZE);
   205   }
   259     solved = false;
       
   260   }
       
   261   bool LpSoplex::_isMax() {
       
   262     return soplex->spxSense() == soplex::SPxSolver::MAXIMIZE;
       
   263   }
       
   264 
   206   
   265   
   207 } //namespace lemon
   266 } //namespace lemon
   208 
   267