alpar@461: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@461:  *
alpar@461:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@461:  *
alpar@461:  * Copyright (C) 2003-2008
alpar@461:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@461:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@461:  *
alpar@461:  * Permission to use, modify and distribute this software is granted
alpar@461:  * provided that this copyright notice appears in all copies. For
alpar@461:  * precise terms see the accompanying LICENSE file.
alpar@461:  *
alpar@461:  * This software is provided "AS IS" with no warranty of any kind,
alpar@461:  * express or implied, and with no claim as to its suitability for any
alpar@461:  * purpose.
alpar@461:  *
alpar@461:  */
alpar@461: 
alpar@461: #include <lemon/clp.h>
alpar@461: #include <coin/ClpSimplex.hpp>
alpar@461: 
alpar@461: namespace lemon {
alpar@461: 
alpar@462:   ClpLp::ClpLp() {
alpar@461:     _prob = new ClpSimplex();
alpar@461:     _init_temporals();
alpar@461:     messageLevel(MESSAGE_NO_OUTPUT);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::ClpLp(const ClpLp& other) {
alpar@461:     _prob = new ClpSimplex(*other._prob);
alpar@461:     rows = other.rows;
alpar@461:     cols = other.cols;
alpar@461:     _init_temporals();
alpar@461:     messageLevel(MESSAGE_NO_OUTPUT);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::~ClpLp() {
alpar@461:     delete _prob;
alpar@461:     _clear_temporals();
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_init_temporals() {
alpar@461:     _primal_ray = 0;
alpar@461:     _dual_ray = 0;
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_clear_temporals() {
alpar@461:     if (_primal_ray) {
alpar@461:       delete[] _primal_ray;
alpar@461:       _primal_ray = 0;
alpar@461:     }
alpar@461:     if (_dual_ray) {
alpar@461:       delete[] _dual_ray;
alpar@461:       _dual_ray = 0;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp* ClpLp::_newSolver() const {
alpar@462:     ClpLp* newlp = new ClpLp;
alpar@461:     return newlp;
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp* ClpLp::_cloneSolver() const {
alpar@462:     ClpLp* copylp = new ClpLp(*this);
alpar@461:     return copylp;
alpar@461:   }
alpar@461: 
alpar@462:   const char* ClpLp::_solverName() const { return "ClpLp"; }
alpar@461: 
alpar@462:   int ClpLp::_addCol() {
alpar@461:     _prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0);
alpar@461:     return _prob->numberColumns() - 1;
alpar@461:   }
alpar@461: 
alpar@462:   int ClpLp::_addRow() {
alpar@461:     _prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX);
alpar@461:     return _prob->numberRows() - 1;
alpar@461:   }
alpar@461: 
alpar@461: 
alpar@462:   void ClpLp::_eraseCol(int c) {
alpar@461:     _col_names_ref.erase(_prob->getColumnName(c));
alpar@461:     _prob->deleteColumns(1, &c);
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_eraseRow(int r) {
alpar@461:     _row_names_ref.erase(_prob->getRowName(r));
alpar@461:     _prob->deleteRows(1, &r);
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_eraseColId(int i) {
alpar@461:     cols.eraseIndex(i);
alpar@461:     cols.shiftIndices(i);
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_eraseRowId(int i) {
alpar@461:     rows.eraseIndex(i);
alpar@461:     rows.shiftIndices(i);
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_getColName(int c, std::string& name) const {
alpar@461:     name = _prob->getColumnName(c);
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setColName(int c, const std::string& name) {
alpar@461:     _prob->setColumnName(c, const_cast<std::string&>(name));
alpar@461:     _col_names_ref[name] = c;
alpar@461:   }
alpar@461: 
alpar@462:   int ClpLp::_colByName(const std::string& name) const {
alpar@461:     std::map<std::string, int>::const_iterator it = _col_names_ref.find(name);
alpar@461:     return it != _col_names_ref.end() ? it->second : -1;
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_getRowName(int r, std::string& name) const {
alpar@461:     name = _prob->getRowName(r);
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setRowName(int r, const std::string& name) {
alpar@461:     _prob->setRowName(r, const_cast<std::string&>(name));
alpar@461:     _row_names_ref[name] = r;
alpar@461:   }
alpar@461: 
alpar@462:   int ClpLp::_rowByName(const std::string& name) const {
alpar@461:     std::map<std::string, int>::const_iterator it = _row_names_ref.find(name);
alpar@461:     return it != _row_names_ref.end() ? it->second : -1;
alpar@461:   }
alpar@461: 
alpar@461: 
alpar@462:   void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
alpar@461:     std::map<int, Value> coeffs;
alpar@461: 
alpar@461:     int n = _prob->clpMatrix()->getNumCols();
alpar@461: 
alpar@461:     const int* indices = _prob->clpMatrix()->getIndices();
alpar@461:     const double* elements = _prob->clpMatrix()->getElements();
alpar@461: 
alpar@461:     for (int i = 0; i < n; ++i) {
alpar@461:       CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
alpar@461:       CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
alpar@461: 
alpar@461:       const int* it = std::lower_bound(indices + begin, indices + end, ix);
alpar@461:       if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
alpar@461:         coeffs[i] = 0.0;
alpar@461:       }
alpar@461:     }
alpar@461: 
alpar@461:     for (ExprIterator it = b; it != e; ++it) {
alpar@461:       coeffs[it->first] = it->second;
alpar@461:     }
alpar@461: 
alpar@461:     for (std::map<int, Value>::iterator it = coeffs.begin();
alpar@461:          it != coeffs.end(); ++it) {
alpar@461:       _prob->modifyCoefficient(ix, it->first, it->second);
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const {
alpar@461:     int n = _prob->clpMatrix()->getNumCols();
alpar@461: 
alpar@461:     const int* indices = _prob->clpMatrix()->getIndices();
alpar@461:     const double* elements = _prob->clpMatrix()->getElements();
alpar@461: 
alpar@461:     for (int i = 0; i < n; ++i) {
alpar@461:       CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i];
alpar@461:       CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i];
alpar@461: 
alpar@461:       const int* it = std::lower_bound(indices + begin, indices + end, ix);
alpar@461:       if (it != indices + end && *it == ix) {
alpar@461:         *b = std::make_pair(i, elements[it - indices]);
alpar@461:       }
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
alpar@461:     std::map<int, Value> coeffs;
alpar@461: 
alpar@461:     CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
alpar@461:     CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
alpar@461: 
alpar@461:     const int* indices = _prob->clpMatrix()->getIndices();
alpar@461:     const double* elements = _prob->clpMatrix()->getElements();
alpar@461: 
alpar@461:     for (CoinBigIndex i = begin; i != end; ++i) {
alpar@461:       if (elements[i] != 0.0) {
alpar@461:         coeffs[indices[i]] = 0.0;
alpar@461:       }
alpar@461:     }
alpar@461:     for (ExprIterator it = b; it != e; ++it) {
alpar@461:       coeffs[it->first] = it->second;
alpar@461:     }
alpar@461:     for (std::map<int, Value>::iterator it = coeffs.begin();
alpar@461:          it != coeffs.end(); ++it) {
alpar@461:       _prob->modifyCoefficient(it->first, ix, it->second);
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_getColCoeffs(int ix, InsertIterator b) const {
alpar@461:     CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
alpar@461:     CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
alpar@461: 
alpar@461:     const int* indices = _prob->clpMatrix()->getIndices();
alpar@461:     const double* elements = _prob->clpMatrix()->getElements();
alpar@461: 
alpar@461:     for (CoinBigIndex i = begin; i != end; ++i) {
alpar@461:       *b = std::make_pair(indices[i], elements[i]);
alpar@461:       ++b;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setCoeff(int ix, int jx, Value value) {
alpar@461:     _prob->modifyCoefficient(ix, jx, value);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const {
alpar@461:     CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix];
alpar@461:     CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix];
alpar@461: 
alpar@461:     const int* indices = _prob->clpMatrix()->getIndices();
alpar@461:     const double* elements = _prob->clpMatrix()->getElements();
alpar@461: 
alpar@461:     const int* it = std::lower_bound(indices + begin, indices + end, jx);
alpar@461:     if (it != indices + end && *it == jx) {
alpar@461:       return elements[it - indices];
alpar@461:     } else {
alpar@461:       return 0.0;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setColLowerBound(int i, Value lo) {
alpar@461:     _prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getColLowerBound(int i) const {
alpar@461:     double val = _prob->getColLower()[i];
alpar@461:     return val == - COIN_DBL_MAX ? - INF : val;
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setColUpperBound(int i, Value up) {
alpar@461:     _prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getColUpperBound(int i) const {
alpar@461:     double val = _prob->getColUpper()[i];
alpar@461:     return val == COIN_DBL_MAX ? INF : val;
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setRowLowerBound(int i, Value lo) {
alpar@461:     _prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getRowLowerBound(int i) const {
alpar@461:     double val = _prob->getRowLower()[i];
alpar@461:     return val == - COIN_DBL_MAX ? - INF : val;
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setRowUpperBound(int i, Value up) {
alpar@461:     _prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getRowUpperBound(int i) const {
alpar@461:     double val = _prob->getRowUpper()[i];
alpar@461:     return val == COIN_DBL_MAX ? INF : val;
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setObjCoeffs(ExprIterator b, ExprIterator e) {
alpar@461:     int num = _prob->clpMatrix()->getNumCols();
alpar@461:     for (int i = 0; i < num; ++i) {
alpar@461:       _prob->setObjectiveCoefficient(i, 0.0);
alpar@461:     }
alpar@461:     for (ExprIterator it = b; it != e; ++it) {
alpar@461:       _prob->setObjectiveCoefficient(it->first, it->second);
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_getObjCoeffs(InsertIterator b) const {
alpar@461:     int num = _prob->clpMatrix()->getNumCols();
alpar@461:     for (int i = 0; i < num; ++i) {
alpar@461:       Value coef = _prob->getObjCoefficients()[i];
alpar@461:       if (coef != 0.0) {
alpar@461:         *b = std::make_pair(i, coef);
alpar@461:         ++b;
alpar@461:       }
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setObjCoeff(int i, Value obj_coef) {
alpar@461:     _prob->setObjectiveCoefficient(i, obj_coef);
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getObjCoeff(int i) const {
alpar@461:     return _prob->getObjCoefficients()[i];
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::SolveExitStatus ClpLp::_solve() {
alpar@461:     return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::SolveExitStatus ClpLp::solvePrimal() {
alpar@461:     return _prob->primal() >= 0 ? SOLVED : UNSOLVED;
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::SolveExitStatus ClpLp::solveDual() {
alpar@461:     return _prob->dual() >= 0 ? SOLVED : UNSOLVED;
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::SolveExitStatus ClpLp::solveBarrier() {
alpar@461:     return _prob->barrier() >= 0 ? SOLVED : UNSOLVED;
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getPrimal(int i) const {
alpar@461:     return _prob->primalColumnSolution()[i];
alpar@461:   }
alpar@462:   ClpLp::Value ClpLp::_getPrimalValue() const {
alpar@461:     return _prob->objectiveValue();
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getDual(int i) const {
alpar@461:     return _prob->dualRowSolution()[i];
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getPrimalRay(int i) const {
alpar@461:     if (!_primal_ray) {
alpar@461:       _primal_ray = _prob->unboundedRay();
alpar@461:       LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided");
alpar@461:     }
alpar@461:     return _primal_ray[i];
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Value ClpLp::_getDualRay(int i) const {
alpar@461:     if (!_dual_ray) {
alpar@461:       _dual_ray = _prob->infeasibilityRay();
alpar@461:       LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided");
alpar@461:     }
alpar@461:     return _dual_ray[i];
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::VarStatus ClpLp::_getColStatus(int i) const {
alpar@461:     switch (_prob->getColumnStatus(i)) {
alpar@461:     case ClpSimplex::basic:
alpar@461:       return BASIC;
alpar@461:     case ClpSimplex::isFree:
alpar@461:       return FREE;
alpar@461:     case ClpSimplex::atUpperBound:
alpar@461:       return UPPER;
alpar@461:     case ClpSimplex::atLowerBound:
alpar@461:       return LOWER;
alpar@461:     case ClpSimplex::isFixed:
alpar@461:       return FIXED;
alpar@461:     case ClpSimplex::superBasic:
alpar@461:       return FREE;
alpar@461:     default:
alpar@461:       LEMON_ASSERT(false, "Wrong column status");
alpar@461:       return VarStatus();
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::VarStatus ClpLp::_getRowStatus(int i) const {
alpar@461:     switch (_prob->getColumnStatus(i)) {
alpar@461:     case ClpSimplex::basic:
alpar@461:       return BASIC;
alpar@461:     case ClpSimplex::isFree:
alpar@461:       return FREE;
alpar@461:     case ClpSimplex::atUpperBound:
alpar@461:       return UPPER;
alpar@461:     case ClpSimplex::atLowerBound:
alpar@461:       return LOWER;
alpar@461:     case ClpSimplex::isFixed:
alpar@461:       return FIXED;
alpar@461:     case ClpSimplex::superBasic:
alpar@461:       return FREE;
alpar@461:     default:
alpar@461:       LEMON_ASSERT(false, "Wrong row status");
alpar@461:       return VarStatus();
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@461: 
alpar@462:   ClpLp::ProblemType ClpLp::_getPrimalType() const {
alpar@461:     if (_prob->isProvenOptimal()) {
alpar@461:       return OPTIMAL;
alpar@461:     } else if (_prob->isProvenPrimalInfeasible()) {
alpar@461:       return INFEASIBLE;
alpar@461:     } else if (_prob->isProvenDualInfeasible()) {
alpar@461:       return UNBOUNDED;
alpar@461:     } else {
alpar@461:       return UNDEFINED;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::ProblemType ClpLp::_getDualType() const {
alpar@461:     if (_prob->isProvenOptimal()) {
alpar@461:       return OPTIMAL;
alpar@461:     } else if (_prob->isProvenDualInfeasible()) {
alpar@461:       return INFEASIBLE;
alpar@461:     } else if (_prob->isProvenPrimalInfeasible()) {
alpar@461:       return INFEASIBLE;
alpar@461:     } else {
alpar@461:       return UNDEFINED;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_setSense(ClpLp::Sense sense) {
alpar@461:     switch (sense) {
alpar@461:     case MIN:
alpar@461:       _prob->setOptimizationDirection(1);
alpar@461:       break;
alpar@461:     case MAX:
alpar@461:       _prob->setOptimizationDirection(-1);
alpar@461:       break;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   ClpLp::Sense ClpLp::_getSense() const {
alpar@461:     double dir = _prob->optimizationDirection();
alpar@461:     if (dir > 0.0) {
alpar@461:       return MIN;
alpar@461:     } else {
alpar@461:       return MAX;
alpar@461:     }
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::_clear() {
alpar@461:     delete _prob;
alpar@461:     _prob = new ClpSimplex();
alpar@461:     rows.clear();
alpar@461:     cols.clear();
alpar@461:     _col_names_ref.clear();
alpar@461:     _clear_temporals();
alpar@461:   }
alpar@461: 
alpar@462:   void ClpLp::messageLevel(MessageLevel m) {
alpar@461:     _prob->setLogLevel(static_cast<int>(m));
alpar@461:   }
alpar@461: 
alpar@461: } //END OF NAMESPACE LEMON