# HG changeset patch # User ladanyi # Date 1112692103 0 # Node ID c3dc75d4af24ce7c67f48da9bcc0b054559af2de # Parent c9c2e90b23427438bf0a7dd4d6fa6587e75b9de7 - moved lp_base.h, lp_base.cc, lp_glpk.h, lp_glpk.cc, lp_solver_skeleton.h and lp_solver_skeleton.cc to src/lemon - modified the includes diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/Makefile.am --- a/src/lemon/Makefile.am Tue Apr 05 08:43:51 2005 +0000 +++ b/src/lemon/Makefile.am Tue Apr 05 09:08:23 2005 +0000 @@ -4,7 +4,10 @@ pkgconfig_DATA = lemon.pc lib_LTLIBRARIES = libemon.la -libemon_la_SOURCES = +libemon_la_SOURCES = \ + lp_base.cc \ + lp_glpk.cc \ + lp_solver_skeleton.cc pkginclude_HEADERS = \ array_map.h \ diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/lp_base.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/lp_base.cc Tue Apr 05 09:08:23 2005 +0000 @@ -0,0 +1,33 @@ +/* -*- C++ -*- + * src/lib/lp_base.cc - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +///\file +///\brief The implementation of the LP solver interface. + +#include +namespace lemon { + + const LpSolverBase::Value + LpSolverBase::INF = std::numeric_limits::infinity(); + const LpSolverBase::Value + LpSolverBase::NaN = std::numeric_limits::quiet_NaN(); + + const LpSolverBase::Constr::Value + LpSolverBase::Constr::INF = std::numeric_limits::infinity(); + const LpSolverBase::Constr::Value + LpSolverBase::Constr::NaN = std::numeric_limits::quiet_NaN(); + +} //namespace lemon diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/lp_base.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/lp_base.h Tue Apr 05 09:08:23 2005 +0000 @@ -0,0 +1,823 @@ +/* -*- C++ -*- + * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_LP_BASE_H +#define LEMON_LP_BASE_H + +#include +#include +#include +#include + +#include +#include +#include + +//#include"lin_expr.h" + +///\file +///\brief The interface of the LP solver interface. +namespace lemon { + + ///Internal data structure to convert floating id's to fix one's + + ///\todo This might be implemented to be also usable in other places. + class _FixId + { + std::vector index; + std::vector cross; + int first_free; + public: + _FixId() : first_free(-1) {}; + ///Convert a floating id to a fix one + + ///\param n is a floating id + ///\return the corresponding fix id + int fixId(int n) {return cross[n];} + ///Convert a fix id to a floating one + + ///\param n is a fix id + ///\return the corresponding floating id + int floatingId(int n) { return index[n];} + ///Add a new floating id. + + ///\param n is a floating id + ///\return the fix id of the new value + ///\todo Multiple additions should also be handled. + int insert(int n) + { + if(n>=int(cross.size())) { + cross.resize(n+1); + if(first_free==-1) { + cross[n]=index.size(); + index.push_back(n); + } + else { + cross[n]=first_free; + int next=index[first_free]; + index[first_free]=n; + first_free=next; + } + return cross[n]; + } + ///\todo Create an own exception type. + else throw LogicError(); //floatingId-s must form a continuous range; + } + ///Remove a fix id. + + ///\param n is a fix id + /// + void erase(int n) + { + int fl=index[n]; + index[n]=first_free; + first_free=n; + for(int i=fl+1;i, so for expamle + ///if \c e is an Expr and \c v and \c w are of type \ref Col then you can + ///read and modify the coefficients like + ///these. + ///\code + ///e[v]=5; + ///e[v]+=12; + ///e.erase(v); + ///\endcode + ///or you can also iterate through its elements. + ///\code + ///double s=0; + ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) + /// s+=i->second; + ///\endcode + ///(This code computes the sum of all coefficients). + ///- Numbers (double's) + ///and variables (\ref Col "Col"s) directly convert to an + ///\ref Expr and the usual linear operations are defined so + ///\code + ///v+w + ///2*v-3.12*(v-w/2)+2 + ///v*2.1+(3*v+(v*12+w+6)*3)/2 + ///\endcode + ///are valid expressions. The usual assignment operations are also defined. + ///\code + ///e=v+w; + ///e+=2*v-3.12*(v-w/2)+2; + ///e*=3.4; + ///e/=5; + ///\endcode + ///- The constant member can be set and read by \ref constComp() + ///\code + ///e.constComp()=12; + ///double c=e.constComp(); + ///\endcode + /// + ///\note that \ref clear() not only sets all coefficients to 0 but also + ///clears the constant components. + class Expr : public std::map + { + public: + typedef LpSolverBase::Col Key; + typedef LpSolverBase::Value Value; + + protected: + typedef std::map Base; + + Value const_comp; + public: + typedef True IsLinExpression; + ///\e + Expr() : Base(), const_comp(0) { } + ///\e + Expr(const Key &v) : const_comp(0) { + Base::insert(std::make_pair(v, 1)); + } + ///\e + Expr(const Value &v) : const_comp(v) {} + ///\e + void set(const Key &v,const Value &c) { + Base::insert(std::make_pair(v, c)); + } + ///\e + Value &constComp() { return const_comp; } + ///\e + const Value &constComp() const { return const_comp; } + + ///Removes the components with zero coefficient. + void simplify() { + for (Base::iterator i=Base::begin(); i!=Base::end();) { + Base::iterator j=i; + ++j; + if ((*i).second==0) Base::erase(i); + j=i; + } + } + + ///Sets all coefficients and the constant component to 0. + void clear() { + Base::clear(); + const_comp=0; + } + + ///\e + Expr &operator+=(const Expr &e) { + for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) + (*this)[j->first]+=j->second; + ///\todo it might be speeded up using "hints" + const_comp+=e.const_comp; + return *this; + } + ///\e + Expr &operator-=(const Expr &e) { + for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) + (*this)[j->first]-=j->second; + const_comp-=e.const_comp; + return *this; + } + ///\e + Expr &operator*=(const Value &c) { + for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) + j->second*=c; + const_comp*=c; + return *this; + } + ///\e + Expr &operator/=(const Value &c) { + for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) + j->second/=c; + const_comp/=c; + return *this; + } + }; + + ///Linear constraint + //typedef LinConstr Constr; + class Constr + { + public: + typedef LpSolverBase::Expr Expr; + typedef Expr::Key Key; + typedef Expr::Value Value; + + static const Value INF; + static const Value NaN; + // static const Value INF=0; + // static const Value NaN=1; + + protected: + Expr _expr; + Value _lb,_ub; + public: + ///\e + Constr() : _expr(), _lb(NaN), _ub(NaN) {} + ///\e + Constr(Value lb,const Expr &e,Value ub) : + _expr(e), _lb(lb), _ub(ub) {} + ///\e + Constr(const Expr &e,Value ub) : + _expr(e), _lb(NaN), _ub(ub) {} + ///\e + Constr(Value lb,const Expr &e) : + _expr(e), _lb(lb), _ub(NaN) {} + ///\e + Constr(const Expr &e) : + _expr(e), _lb(NaN), _ub(NaN) {} + ///\e + void clear() + { + _expr.clear(); + _lb=_ub=NaN; + } + ///\e + Expr &expr() { return _expr; } + ///\e + const Expr &expr() const { return _expr; } + ///\e + Value &lowerBound() { return _lb; } + ///\e + const Value &lowerBound() const { return _lb; } + ///\e + Value &upperBound() { return _ub; } + ///\e + const Value &upperBound() const { return _ub; } + ///\e + bool lowerBounded() const { + using namespace std; + return isfinite(_lb); + } + ///\e + bool upperBounded() const { + using namespace std; + return isfinite(_ub); + } + }; + + + protected: + _FixId rows; + _FixId cols; + + virtual int _addCol() = 0; + virtual int _addRow() = 0; + virtual void _setRowCoeffs(int i, + int length, + int const * indices, + Value const * values ) = 0; + virtual void _setColCoeffs(int i, + int length, + int const * indices, + Value const * values ) = 0; + virtual void _setColLowerBound(int i, Value value) = 0; + virtual void _setColUpperBound(int i, Value value) = 0; + virtual void _setRowLowerBound(int i, Value value) = 0; + virtual void _setRowUpperBound(int i, Value value) = 0; + virtual void _setObjCoeff(int i, Value obj_coef) = 0; + virtual SolveExitStatus _solve() = 0; + virtual Value _getPrimal(int i) = 0; + virtual SolutionStatus _getPrimalType() = 0; + + + void clearObj() {} + public: + + + ///\e + virtual ~LpSolverBase() {} + + ///\name Build up and modify of the LP + + ///@{ + + ///Add a new empty column (i.e a new variable) to the LP + Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} + + ///\brief Adds several new columns + ///(i.e a variables) at once + /// + ///This magic function takes a container as its argument + ///and fills its elements + ///with new columns (i.e. variables) + ///\param t can be + ///- a standard STL compatible iterable container with + ///\ref Col as its \c values_type + ///like + ///\code + ///std::vector + ///std::list + ///\endcode + ///- a standard STL compatible iterable container with + ///\ref Col as its \c mapped_type + ///like + ///\code + ///std::map + ///\endcode + ///- an iterable lemon \ref concept::WriteMap "write map" like + ///\code + ///ListGraph::NodeMap + ///ListGraph::EdgeMap + ///\endcode + ///\return The number of the created column. + ///\bug Iterable nodemap hasn't been implemented yet. +#ifdef DOXYGEN + template + int addColSet(T &t) { return 0;} +#else + template + typename enable_if::type + addColSet(T &t,dummy<0> = 0) { + int s=0; + for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} + return s; + } + template + typename enable_if::type + addColSet(T &t,dummy<1> = 1) { + int s=0; + for(typename T::iterator i=t.begin();i!=t.end();++i) { + i->second=addCol(); + s++; + } + return s; + } + template + typename enable_if::type + addColSet(T &t,dummy<2> = 2) { + ///\bug return addColSet(t.valueSet()); should also work. + int s=0; + for(typename T::ValueSet::iterator i=t.valueSet().begin(); + i!=t.valueSet().end(); + ++i) + { + *i=addCol(); + s++; + } + return s; + } +#endif + + ///Add a new empty row (i.e a new constaint) to the LP + + ///This function adds a new empty row (i.e a new constaint) to the LP. + ///\return The created row + Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} + + ///Set a row (i.e a constaint) of the LP + + ///\param r is the row to be modified + ///\param l is lower bound (-\ref INF means no bound) + ///\param e is a linear expression (see \ref Expr) + ///\param u is the upper bound (\ref INF means no bound) + ///\bug This is a temportary function. The interface will change to + ///a better one. + void setRow(Row r, Value l,const Expr &e, Value u) { + std::vector indices; + std::vector values; + indices.push_back(0); + values.push_back(0); + for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) + if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! + indices.push_back(cols.floatingId((*i).first.id)); + values.push_back((*i).second); + } + _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, + &indices[0],&values[0]); + _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); + _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); + } + + ///Set a row (i.e a constaint) of the LP + + ///\param r is the row to be modified + ///\param c is a linear expression (see \ref Constr) + void setRow(Row r, const Constr &c) { + setRow(r, + c.lowerBounded()?c.lowerBound():-INF, + c.expr(), + c.upperBounded()?c.upperBound():INF); + } + + ///Add a new row (i.e a new constaint) to the LP + + ///\param l is the lower bound (-\ref INF means no bound) + ///\param e is a linear expression (see \ref Expr) + ///\param u is the upper bound (\ref INF means no bound) + ///\return The created row. + ///\bug This is a temportary function. The interface will change to + ///a better one. + Row addRow(Value l,const Expr &e, Value u) { + Row r=addRow(); + setRow(r,l,e,u); + return r; + } + + ///Add a new row (i.e a new constaint) to the LP + + ///\param c is a linear expression (see \ref Constr) + ///\return The created row. + Row addRow(const Constr &c) { + Row r=addRow(); + setRow(r,c); + return r; + } + + /// Set the lower bound of a column (i.e a variable) + + /// The upper bound of a variable (column) has to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or -\ref INF. + void colLowerBound(Col c, Value value) { + _setColLowerBound(cols.floatingId(c.id),value); + } + /// Set the upper bound of a column (i.e a variable) + + /// The upper bound of a variable (column) has to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or \ref INF. + void colUpperBound(Col c, Value value) { + _setColUpperBound(cols.floatingId(c.id),value); + }; + /// Set the lower and the upper bounds of a column (i.e a variable) + + /// The lower and the upper bounds of + /// a variable (column) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value, -\ref INF or \ref INF. + void colBounds(Col c, Value lower, Value upper) { + _setColLowerBound(cols.floatingId(c.id),lower); + _setColUpperBound(cols.floatingId(c.id),upper); + } + + /// Set the lower bound of a row (i.e a constraint) + + /// The lower bound of a linear expression (row) has to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or -\ref INF. + void rowLowerBound(Row r, Value value) { + _setRowLowerBound(rows.floatingId(r.id),value); + }; + /// Set the upper bound of a row (i.e a constraint) + + /// The upper bound of a linear expression (row) has to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or \ref INF. + void rowUpperBound(Row r, Value value) { + _setRowUpperBound(rows.floatingId(r.id),value); + }; + /// Set the lower and the upper bounds of a row (i.e a variable) + + /// The lower and the upper bounds of + /// a constraint (row) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value, -\ref INF or \ref INF. + void rowBounds(Row c, Value lower, Value upper) { + _setRowLowerBound(rows.floatingId(c.id),lower); + _setRowUpperBound(rows.floatingId(c.id),upper); + } + + ///Set an element of the objective function + void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; + ///Set the objective function + + ///\param e is a linear expression of type \ref Expr. + ///\todo What to do with the constant component? + void setObj(Expr e) { + clearObj(); + for (Expr::iterator i=e.begin(); i!=e.end(); ++i) + objCoeff((*i).first,(*i).second); + } + + ///@} + + + ///\name Solve the LP + + ///@{ + + ///\e + SolveExitStatus solve() { return _solve(); } + + ///@} + + ///\name Obtain the solution + + ///@{ + + ///\e + SolutionStatus primalType() { + return _getPrimalType(); + } + + ///\e + Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } + + ///@} + + }; + + ///\e + + ///\relates LpSolverBase::Expr + /// + inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, + const LpSolverBase::Expr &b) + { + LpSolverBase::Expr tmp(a); + tmp+=b; ///\todo Don't STL have some special 'merge' algorithm? + return tmp; + } + ///\e + + ///\relates LpSolverBase::Expr + /// + inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, + const LpSolverBase::Expr &b) + { + LpSolverBase::Expr tmp(a); + tmp-=b; ///\todo Don't STL have some special 'merge' algorithm? + return tmp; + } + ///\e + + ///\relates LpSolverBase::Expr + /// + inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, + const LpSolverBase::Value &b) + { + LpSolverBase::Expr tmp(a); + tmp*=b; ///\todo Don't STL have some special 'merge' algorithm? + return tmp; + } + + ///\e + + ///\relates LpSolverBase::Expr + /// + inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, + const LpSolverBase::Expr &b) + { + LpSolverBase::Expr tmp(b); + tmp*=a; ///\todo Don't STL have some special 'merge' algorithm? + return tmp; + } + ///\e + + ///\relates LpSolverBase::Expr + /// + inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, + const LpSolverBase::Value &b) + { + LpSolverBase::Expr tmp(a); + tmp/=b; ///\todo Don't STL have some special 'merge' algorithm? + return tmp; + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, + const LpSolverBase::Expr &f) + { + return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, + const LpSolverBase::Expr &f) + { + return LpSolverBase::Constr(e,f); + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, + const LpSolverBase::Value &f) + { + return LpSolverBase::Constr(e,f); + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, + const LpSolverBase::Expr &f) + { + return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); + } + + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, + const LpSolverBase::Expr &f) + { + return LpSolverBase::Constr(f,e); + } + + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, + const LpSolverBase::Value &f) + { + return LpSolverBase::Constr(f,e); + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, + const LpSolverBase::Expr &f) + { + return LpSolverBase::Constr(0,e-f,0); + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, + const LpSolverBase::Constr&c) + { + LpSolverBase::Constr tmp(c); + ///\todo Create an own exception type. + if(!isnan(tmp.lowerBound())) throw LogicError(); + else tmp.lowerBound()=n; + return tmp; + } + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, + const LpSolverBase::Value &n) + { + LpSolverBase::Constr tmp(c); + ///\todo Create an own exception type. + if(!isnan(tmp.upperBound())) throw LogicError(); + else tmp.upperBound()=n; + return tmp; + } + + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, + const LpSolverBase::Constr&c) + { + LpSolverBase::Constr tmp(c); + ///\todo Create an own exception type. + if(!isnan(tmp.upperBound())) throw LogicError(); + else tmp.upperBound()=n; + return tmp; + } + ///\e + + ///\relates LpSolverBase::Constr + /// + inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, + const LpSolverBase::Value &n) + { + LpSolverBase::Constr tmp(c); + ///\todo Create an own exception type. + if(!isnan(tmp.lowerBound())) throw LogicError(); + else tmp.lowerBound()=n; + return tmp; + } + + +} //namespace lemon + +#endif //LEMON_LP_BASE_H diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/lp_glpk.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/lp_glpk.cc Tue Apr 05 09:08:23 2005 +0000 @@ -0,0 +1,288 @@ +/* -*- C++ -*- + * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_LP_GLPK_CC +#define LEMON_LP_GLPK_CC + +///\file +///\brief Implementation of the LEMON-GLPK lp solver interface. + +#include + +namespace lemon { + + /// \e + int LpGlpk::_addCol() { + int i=lpx_add_cols(lp, 1); + _setColLowerBound(i, -INF); + _setColUpperBound(i, INF); + return i; + } + + /// \e + int LpGlpk::_addRow() { + int i=lpx_add_rows(lp, 1); + return i; + } + + + void LpGlpk::_setRowCoeffs(int i, + int length, + const int * indices, + const Value * values ) + { + lpx_set_mat_row(lp, i, length, + const_cast(indices) , + const_cast(values)); + } + + void LpGlpk::_setColCoeffs(int i, + int length, + const int * indices, + const Value * values) + { + lpx_set_mat_col(lp, i, length, + const_cast(indices), + const_cast(values)); + } + + void LpGlpk::_setColLowerBound(int i, Value lo) + { + if (lo==INF) { + //FIXME error + } + int b=lpx_get_col_type(lp, i); + double up=lpx_get_col_ub(lp, i); + if (lo==-INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_col_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_UP: + break; + case LPX_DB: + case LPX_FX: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_col_bnds(lp, i, LPX_LO, lo, up); + break; + case LPX_UP: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_col_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_col_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + + } + + void LpGlpk::_setColUpperBound(int i, Value up) + { + if (up==-INF) { + //FIXME error + } + int b=lpx_get_col_type(lp, i); + double lo=lpx_get_col_lb(lp, i); + if (up==INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + break; + case LPX_UP: + lpx_set_col_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_DB: + case LPX_FX: + lpx_set_col_bnds(lp, i, LPX_LO, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_UP: + lpx_set_col_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_LO: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_col_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_col_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + } + + void LpGlpk::_setRowLowerBound(int i, Value lo) + { + if (lo==INF) { + //FIXME error + } + int b=lpx_get_row_type(lp, i); + double up=lpx_get_row_ub(lp, i); + if (lo==-INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_row_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_UP: + break; + case LPX_DB: + case LPX_FX: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + case LPX_LO: + lpx_set_row_bnds(lp, i, LPX_LO, lo, up); + break; + case LPX_UP: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_row_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_row_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + } + + void LpGlpk::_setRowUpperBound(int i, Value up) + { + if (up==-INF) { + //FIXME error + } + int b=lpx_get_row_type(lp, i); + double lo=lpx_get_row_lb(lp, i); + if (up==INF) { + switch (b) { + case LPX_FR: + case LPX_LO: + break; + case LPX_UP: + lpx_set_row_bnds(lp, i, LPX_FR, lo, up); + break; + case LPX_DB: + case LPX_FX: + lpx_set_row_bnds(lp, i, LPX_LO, lo, up); + break; + default: ; + //FIXME error + } + } else { + switch (b) { + case LPX_FR: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_UP: + lpx_set_row_bnds(lp, i, LPX_UP, lo, up); + break; + case LPX_LO: + case LPX_DB: + case LPX_FX: + if (lo==up) + lpx_set_row_bnds(lp, i, LPX_FX, lo, up); + else + lpx_set_row_bnds(lp, i, LPX_DB, lo, up); + break; + default: ; + //FIXME error + } + } + } + + void LpGlpk::_setObjCoeff(int i, Value obj_coef) + { + lpx_set_obj_coef(lp, i, obj_coef); + } + + + LpGlpk::SolveExitStatus LpGlpk::_solve() + { + int i= lpx_simplex(lp); + switch (i) { + case LPX_E_OK: + return SOLVED; + break; + default: + return UNSOLVED; + } + } + + LpGlpk::Value LpGlpk::_getPrimal(int i) + { + return lpx_get_col_prim(lp,i); + } + + + LpGlpk::SolutionStatus LpGlpk::_getPrimalType() + { + int stat= lpx_get_status(lp); + switch (stat) { + case LPX_UNDEF://Undefined (no solve has been run yet) + return UNDEFINED; + break; + case LPX_NOFEAS://There is no feasible solution (primal, I guess) + case LPX_INFEAS://Infeasible + return INFEASIBLE; + break; + case LPX_UNBND://Unbounded + return INFINITE; + break; + case LPX_FEAS://Feasible + return FEASIBLE; + break; + case LPX_OPT://Feasible + return OPTIMAL; + break; + default: + return UNDEFINED; //to avoid gcc warning + //FIXME error + } + } + + +} //END OF NAMESPACE LEMON + +#endif //LEMON_LP_GLPK_CC diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/lp_glpk.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/lp_glpk.h Tue Apr 05 09:08:23 2005 +0000 @@ -0,0 +1,89 @@ +/* -*- C++ -*- + * src/lemon/lp_glpk.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_LP_GLPK_H +#define LEMON_LP_GLPK_H + +///\file +///\brief Header of the LEMON-GLPK lp solver interface. + +#include "lp_base.h" +extern "C" { +#include "glpk.h" +} + +namespace lemon { + + + /// \brief Wrapper for GLPK solver + /// + /// This class implements a lemon wrapper for GLPK. + class LpGlpk : public LpSolverBase { + + public: + + typedef LpSolverBase Parent; + + /// \e + LPX* lp; + + /// \e + LpGlpk() : Parent(), + lp(lpx_create_prob()) { + lpx_set_int_parm(lp, LPX_K_DUAL, 1); + } + /// \e + ~LpGlpk() { + lpx_delete_prob(lp); + } + + protected: + virtual int _addCol(); + virtual int _addRow(); + virtual void _setRowCoeffs(int i, + int length, + const int * indices, + const Value * values ); + virtual void _setColCoeffs(int i, + int length, + const int * indices, + const Value * values); + virtual void _setColLowerBound(int i, Value value); + virtual void _setColUpperBound(int i, Value value); + virtual void _setRowLowerBound(int i, Value value); + virtual void _setRowUpperBound(int i, Value value); + virtual void _setObjCoeff(int i, Value obj_coef); + ///\e + + ///\bug Unimplemented + /// + virtual SolveExitStatus _solve(); + ///\e + + ///\bug Unimplemented + /// + virtual Value _getPrimal(int i); + ///\e + + ///\bug Unimplemented + /// + virtual SolutionStatus _getPrimalType(); + + }; +} //END OF NAMESPACE LEMON + +#endif //LEMON_LP_GLPK_H + diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/lp_solver_skeleton.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/lp_solver_skeleton.cc Tue Apr 05 09:08:23 2005 +0000 @@ -0,0 +1,84 @@ +/* -*- C++ -*- + * src/lemon/lp_solver_skeleton.cc + * - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#include + +///\file +///\brief A skeleton file to implement LP solver interfaces +namespace lemon { + + int LpSolverSkeleton::_addCol() + { + return ++col_num; + } + + int LpSolverSkeleton::_addRow() + { + return ++row_num; + } + + void LpSolverSkeleton::_setRowCoeffs(int i, + int length, + int const * indices, + Value const * values ) + { + } + + void LpSolverSkeleton::_setColCoeffs(int i, + int length, + int const * indices, + Value const * values) + { + } + + void LpSolverSkeleton::_setColLowerBound(int i, Value value) + { + } + + void LpSolverSkeleton::_setColUpperBound(int i, Value value) + { + } + + void LpSolverSkeleton::_setRowLowerBound(int i, Value value) + { + } + + void LpSolverSkeleton::_setRowUpperBound(int i, Value value) + { + } + + void LpSolverSkeleton::_setObjCoeff(int i, Value obj_coef) + { + } + + LpSolverSkeleton::SolveExitStatus LpSolverSkeleton::_solve() + { + return SOLVED; + } + + LpSolverSkeleton::Value LpSolverSkeleton::_getPrimal(int i) + { + return 0; + } + + LpSolverSkeleton::SolutionStatus LpSolverSkeleton::_getPrimalType() + { + return OPTIMAL; + } + +} //namespace lemon + diff -r c9c2e90b2342 -r c3dc75d4af24 src/lemon/lp_solver_skeleton.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/lp_solver_skeleton.h Tue Apr 05 09:08:23 2005 +0000 @@ -0,0 +1,104 @@ +/* -*- C++ -*- + * src/lemon/lp_solver_skeleton.h + * - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Combinatorial Optimization Research Group, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_LP_SOLVER_SKELETON +#define LEMON_LP_SOLVER_SKELETON + +#include"lp_base.h" + +///\file +///\brief A skeleton file to implement LP solver interfaces +namespace lemon { + + ///A skeleton class to implement LP solver interfaces + class LpSolverSkeleton :public LpSolverBase { + int col_num,row_num; + + protected: + /// \e + virtual int _addCol(); + /// \e + virtual int _addRow(); + /// \e + + /// \warning Arrays are indexed from 1 (datum at index 0 is ignored) + /// + virtual void _setRowCoeffs(int i, + int length, + int const * indices, + Value const * values ); + /// \e + + /// \warning Arrays are indexed from 1 (datum at index 0 is ignored) + /// + virtual void _setColCoeffs(int i, + int length, + int const * indices, + Value const * values ); + + /// \e + + /// The lower bound of a variable (column) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or -\ref INF. + virtual void _setColLowerBound(int i, Value value); + /// \e + + /// The upper bound of a variable (column) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or \ref INF. + virtual void _setColUpperBound(int i, Value value); + /// \e + + /// The lower bound of a linear expression (row) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or -\ref INF. + virtual void _setRowLowerBound(int i, Value value); + /// \e + + /// The upper bound of a linear expression (row) have to be given by an + /// extended number of type Value, i.e. a finite number of type + /// Value or \ref INF. + virtual void _setRowUpperBound(int i, Value value); + + /// \e + virtual void _setObjCoeff(int i, Value obj_coef); + + ///\e + + ///\bug Wrong interface + /// + virtual SolveExitStatus _solve(); + + ///\e + + ///\bug Wrong interface + /// + virtual Value _getPrimal(int i); + ///\e + + ///\bug Wrong interface + /// + virtual SolutionStatus _getPrimalType(); + + public: + LpSolverSkeleton() : LpSolverBase(), col_num(0), row_num(0) {} + }; + +} //namespace lemon + +#endif // LEMON_LP_SOLVER_SKELETON diff -r c9c2e90b2342 -r c3dc75d4af24 src/work/athos/lp/lp_base.cc --- a/src/work/athos/lp/lp_base.cc Tue Apr 05 08:43:51 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* -*- C++ -*- - * src/lib/lp_base.cc - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -///\file -///\brief The implementation of the LP solver interface. - -#include "lp_base.h" -namespace lemon { - - const LpSolverBase::Value - LpSolverBase::INF = std::numeric_limits::infinity(); - const LpSolverBase::Value - LpSolverBase::NaN = std::numeric_limits::quiet_NaN(); - - const LpSolverBase::Constr::Value - LpSolverBase::Constr::INF = std::numeric_limits::infinity(); - const LpSolverBase::Constr::Value - LpSolverBase::Constr::NaN = std::numeric_limits::quiet_NaN(); - -} //namespace lemon diff -r c9c2e90b2342 -r c3dc75d4af24 src/work/athos/lp/lp_base.h --- a/src/work/athos/lp/lp_base.h Tue Apr 05 08:43:51 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,823 +0,0 @@ -/* -*- C++ -*- - * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#ifndef LEMON_LP_BASE_H -#define LEMON_LP_BASE_H - -#include -#include -#include -#include - -#include -#include -#include - -//#include"lin_expr.h" - -///\file -///\brief The interface of the LP solver interface. -namespace lemon { - - ///Internal data structure to convert floating id's to fix one's - - ///\todo This might be implemented to be also usable in other places. - class _FixId - { - std::vector index; - std::vector cross; - int first_free; - public: - _FixId() : first_free(-1) {}; - ///Convert a floating id to a fix one - - ///\param n is a floating id - ///\return the corresponding fix id - int fixId(int n) {return cross[n];} - ///Convert a fix id to a floating one - - ///\param n is a fix id - ///\return the corresponding floating id - int floatingId(int n) { return index[n];} - ///Add a new floating id. - - ///\param n is a floating id - ///\return the fix id of the new value - ///\todo Multiple additions should also be handled. - int insert(int n) - { - if(n>=int(cross.size())) { - cross.resize(n+1); - if(first_free==-1) { - cross[n]=index.size(); - index.push_back(n); - } - else { - cross[n]=first_free; - int next=index[first_free]; - index[first_free]=n; - first_free=next; - } - return cross[n]; - } - ///\todo Create an own exception type. - else throw LogicError(); //floatingId-s must form a continuous range; - } - ///Remove a fix id. - - ///\param n is a fix id - /// - void erase(int n) - { - int fl=index[n]; - index[n]=first_free; - first_free=n; - for(int i=fl+1;i, so for expamle - ///if \c e is an Expr and \c v and \c w are of type \ref Col then you can - ///read and modify the coefficients like - ///these. - ///\code - ///e[v]=5; - ///e[v]+=12; - ///e.erase(v); - ///\endcode - ///or you can also iterate through its elements. - ///\code - ///double s=0; - ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) - /// s+=i->second; - ///\endcode - ///(This code computes the sum of all coefficients). - ///- Numbers (double's) - ///and variables (\ref Col "Col"s) directly convert to an - ///\ref Expr and the usual linear operations are defined so - ///\code - ///v+w - ///2*v-3.12*(v-w/2)+2 - ///v*2.1+(3*v+(v*12+w+6)*3)/2 - ///\endcode - ///are valid expressions. The usual assignment operations are also defined. - ///\code - ///e=v+w; - ///e+=2*v-3.12*(v-w/2)+2; - ///e*=3.4; - ///e/=5; - ///\endcode - ///- The constant member can be set and read by \ref constComp() - ///\code - ///e.constComp()=12; - ///double c=e.constComp(); - ///\endcode - /// - ///\note that \ref clear() not only sets all coefficients to 0 but also - ///clears the constant components. - class Expr : public std::map - { - public: - typedef LpSolverBase::Col Key; - typedef LpSolverBase::Value Value; - - protected: - typedef std::map Base; - - Value const_comp; - public: - typedef True IsLinExpression; - ///\e - Expr() : Base(), const_comp(0) { } - ///\e - Expr(const Key &v) : const_comp(0) { - Base::insert(std::make_pair(v, 1)); - } - ///\e - Expr(const Value &v) : const_comp(v) {} - ///\e - void set(const Key &v,const Value &c) { - Base::insert(std::make_pair(v, c)); - } - ///\e - Value &constComp() { return const_comp; } - ///\e - const Value &constComp() const { return const_comp; } - - ///Removes the components with zero coefficient. - void simplify() { - for (Base::iterator i=Base::begin(); i!=Base::end();) { - Base::iterator j=i; - ++j; - if ((*i).second==0) Base::erase(i); - j=i; - } - } - - ///Sets all coefficients and the constant component to 0. - void clear() { - Base::clear(); - const_comp=0; - } - - ///\e - Expr &operator+=(const Expr &e) { - for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) - (*this)[j->first]+=j->second; - ///\todo it might be speeded up using "hints" - const_comp+=e.const_comp; - return *this; - } - ///\e - Expr &operator-=(const Expr &e) { - for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) - (*this)[j->first]-=j->second; - const_comp-=e.const_comp; - return *this; - } - ///\e - Expr &operator*=(const Value &c) { - for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) - j->second*=c; - const_comp*=c; - return *this; - } - ///\e - Expr &operator/=(const Value &c) { - for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) - j->second/=c; - const_comp/=c; - return *this; - } - }; - - ///Linear constraint - //typedef LinConstr Constr; - class Constr - { - public: - typedef LpSolverBase::Expr Expr; - typedef Expr::Key Key; - typedef Expr::Value Value; - - static const Value INF; - static const Value NaN; - // static const Value INF=0; - // static const Value NaN=1; - - protected: - Expr _expr; - Value _lb,_ub; - public: - ///\e - Constr() : _expr(), _lb(NaN), _ub(NaN) {} - ///\e - Constr(Value lb,const Expr &e,Value ub) : - _expr(e), _lb(lb), _ub(ub) {} - ///\e - Constr(const Expr &e,Value ub) : - _expr(e), _lb(NaN), _ub(ub) {} - ///\e - Constr(Value lb,const Expr &e) : - _expr(e), _lb(lb), _ub(NaN) {} - ///\e - Constr(const Expr &e) : - _expr(e), _lb(NaN), _ub(NaN) {} - ///\e - void clear() - { - _expr.clear(); - _lb=_ub=NaN; - } - ///\e - Expr &expr() { return _expr; } - ///\e - const Expr &expr() const { return _expr; } - ///\e - Value &lowerBound() { return _lb; } - ///\e - const Value &lowerBound() const { return _lb; } - ///\e - Value &upperBound() { return _ub; } - ///\e - const Value &upperBound() const { return _ub; } - ///\e - bool lowerBounded() const { - using namespace std; - return isfinite(_lb); - } - ///\e - bool upperBounded() const { - using namespace std; - return isfinite(_ub); - } - }; - - - protected: - _FixId rows; - _FixId cols; - - virtual int _addCol() = 0; - virtual int _addRow() = 0; - virtual void _setRowCoeffs(int i, - int length, - int const * indices, - Value const * values ) = 0; - virtual void _setColCoeffs(int i, - int length, - int const * indices, - Value const * values ) = 0; - virtual void _setColLowerBound(int i, Value value) = 0; - virtual void _setColUpperBound(int i, Value value) = 0; - virtual void _setRowLowerBound(int i, Value value) = 0; - virtual void _setRowUpperBound(int i, Value value) = 0; - virtual void _setObjCoeff(int i, Value obj_coef) = 0; - virtual SolveExitStatus _solve() = 0; - virtual Value _getPrimal(int i) = 0; - virtual SolutionStatus _getPrimalType() = 0; - - - void clearObj() {} - public: - - - ///\e - virtual ~LpSolverBase() {} - - ///\name Build up and modify of the LP - - ///@{ - - ///Add a new empty column (i.e a new variable) to the LP - Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} - - ///\brief Adds several new columns - ///(i.e a variables) at once - /// - ///This magic function takes a container as its argument - ///and fills its elements - ///with new columns (i.e. variables) - ///\param t can be - ///- a standard STL compatible iterable container with - ///\ref Col as its \c values_type - ///like - ///\code - ///std::vector - ///std::list - ///\endcode - ///- a standard STL compatible iterable container with - ///\ref Col as its \c mapped_type - ///like - ///\code - ///std::map - ///\endcode - ///- an iterable lemon \ref concept::WriteMap "write map" like - ///\code - ///ListGraph::NodeMap - ///ListGraph::EdgeMap - ///\endcode - ///\return The number of the created column. - ///\bug Iterable nodemap hasn't been implemented yet. -#ifdef DOXYGEN - template - int addColSet(T &t) { return 0;} -#else - template - typename enable_if::type - addColSet(T &t,dummy<0> = 0) { - int s=0; - for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} - return s; - } - template - typename enable_if::type - addColSet(T &t,dummy<1> = 1) { - int s=0; - for(typename T::iterator i=t.begin();i!=t.end();++i) { - i->second=addCol(); - s++; - } - return s; - } - template - typename enable_if::type - addColSet(T &t,dummy<2> = 2) { - ///\bug return addColSet(t.valueSet()); should also work. - int s=0; - for(typename T::ValueSet::iterator i=t.valueSet().begin(); - i!=t.valueSet().end(); - ++i) - { - *i=addCol(); - s++; - } - return s; - } -#endif - - ///Add a new empty row (i.e a new constaint) to the LP - - ///This function adds a new empty row (i.e a new constaint) to the LP. - ///\return The created row - Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} - - ///Set a row (i.e a constaint) of the LP - - ///\param r is the row to be modified - ///\param l is lower bound (-\ref INF means no bound) - ///\param e is a linear expression (see \ref Expr) - ///\param u is the upper bound (\ref INF means no bound) - ///\bug This is a temportary function. The interface will change to - ///a better one. - void setRow(Row r, Value l,const Expr &e, Value u) { - std::vector indices; - std::vector values; - indices.push_back(0); - values.push_back(0); - for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) - if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! - indices.push_back(cols.floatingId((*i).first.id)); - values.push_back((*i).second); - } - _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, - &indices[0],&values[0]); - _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); - _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); - } - - ///Set a row (i.e a constaint) of the LP - - ///\param r is the row to be modified - ///\param c is a linear expression (see \ref Constr) - void setRow(Row r, const Constr &c) { - setRow(r, - c.lowerBounded()?c.lowerBound():-INF, - c.expr(), - c.upperBounded()?c.upperBound():INF); - } - - ///Add a new row (i.e a new constaint) to the LP - - ///\param l is the lower bound (-\ref INF means no bound) - ///\param e is a linear expression (see \ref Expr) - ///\param u is the upper bound (\ref INF means no bound) - ///\return The created row. - ///\bug This is a temportary function. The interface will change to - ///a better one. - Row addRow(Value l,const Expr &e, Value u) { - Row r=addRow(); - setRow(r,l,e,u); - return r; - } - - ///Add a new row (i.e a new constaint) to the LP - - ///\param c is a linear expression (see \ref Constr) - ///\return The created row. - Row addRow(const Constr &c) { - Row r=addRow(); - setRow(r,c); - return r; - } - - /// Set the lower bound of a column (i.e a variable) - - /// The upper bound of a variable (column) has to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or -\ref INF. - void colLowerBound(Col c, Value value) { - _setColLowerBound(cols.floatingId(c.id),value); - } - /// Set the upper bound of a column (i.e a variable) - - /// The upper bound of a variable (column) has to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or \ref INF. - void colUpperBound(Col c, Value value) { - _setColUpperBound(cols.floatingId(c.id),value); - }; - /// Set the lower and the upper bounds of a column (i.e a variable) - - /// The lower and the upper bounds of - /// a variable (column) have to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value, -\ref INF or \ref INF. - void colBounds(Col c, Value lower, Value upper) { - _setColLowerBound(cols.floatingId(c.id),lower); - _setColUpperBound(cols.floatingId(c.id),upper); - } - - /// Set the lower bound of a row (i.e a constraint) - - /// The lower bound of a linear expression (row) has to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or -\ref INF. - void rowLowerBound(Row r, Value value) { - _setRowLowerBound(rows.floatingId(r.id),value); - }; - /// Set the upper bound of a row (i.e a constraint) - - /// The upper bound of a linear expression (row) has to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or \ref INF. - void rowUpperBound(Row r, Value value) { - _setRowUpperBound(rows.floatingId(r.id),value); - }; - /// Set the lower and the upper bounds of a row (i.e a variable) - - /// The lower and the upper bounds of - /// a constraint (row) have to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value, -\ref INF or \ref INF. - void rowBounds(Row c, Value lower, Value upper) { - _setRowLowerBound(rows.floatingId(c.id),lower); - _setRowUpperBound(rows.floatingId(c.id),upper); - } - - ///Set an element of the objective function - void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; - ///Set the objective function - - ///\param e is a linear expression of type \ref Expr. - ///\todo What to do with the constant component? - void setObj(Expr e) { - clearObj(); - for (Expr::iterator i=e.begin(); i!=e.end(); ++i) - objCoeff((*i).first,(*i).second); - } - - ///@} - - - ///\name Solve the LP - - ///@{ - - ///\e - SolveExitStatus solve() { return _solve(); } - - ///@} - - ///\name Obtain the solution - - ///@{ - - ///\e - SolutionStatus primalType() { - return _getPrimalType(); - } - - ///\e - Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } - - ///@} - - }; - - ///\e - - ///\relates LpSolverBase::Expr - /// - inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, - const LpSolverBase::Expr &b) - { - LpSolverBase::Expr tmp(a); - tmp+=b; ///\todo Don't STL have some special 'merge' algorithm? - return tmp; - } - ///\e - - ///\relates LpSolverBase::Expr - /// - inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, - const LpSolverBase::Expr &b) - { - LpSolverBase::Expr tmp(a); - tmp-=b; ///\todo Don't STL have some special 'merge' algorithm? - return tmp; - } - ///\e - - ///\relates LpSolverBase::Expr - /// - inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, - const LpSolverBase::Value &b) - { - LpSolverBase::Expr tmp(a); - tmp*=b; ///\todo Don't STL have some special 'merge' algorithm? - return tmp; - } - - ///\e - - ///\relates LpSolverBase::Expr - /// - inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, - const LpSolverBase::Expr &b) - { - LpSolverBase::Expr tmp(b); - tmp*=a; ///\todo Don't STL have some special 'merge' algorithm? - return tmp; - } - ///\e - - ///\relates LpSolverBase::Expr - /// - inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, - const LpSolverBase::Value &b) - { - LpSolverBase::Expr tmp(a); - tmp/=b; ///\todo Don't STL have some special 'merge' algorithm? - return tmp; - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, - const LpSolverBase::Expr &f) - { - return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, - const LpSolverBase::Expr &f) - { - return LpSolverBase::Constr(e,f); - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, - const LpSolverBase::Value &f) - { - return LpSolverBase::Constr(e,f); - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, - const LpSolverBase::Expr &f) - { - return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); - } - - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, - const LpSolverBase::Expr &f) - { - return LpSolverBase::Constr(f,e); - } - - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, - const LpSolverBase::Value &f) - { - return LpSolverBase::Constr(f,e); - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, - const LpSolverBase::Expr &f) - { - return LpSolverBase::Constr(0,e-f,0); - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, - const LpSolverBase::Constr&c) - { - LpSolverBase::Constr tmp(c); - ///\todo Create an own exception type. - if(!isnan(tmp.lowerBound())) throw LogicError(); - else tmp.lowerBound()=n; - return tmp; - } - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, - const LpSolverBase::Value &n) - { - LpSolverBase::Constr tmp(c); - ///\todo Create an own exception type. - if(!isnan(tmp.upperBound())) throw LogicError(); - else tmp.upperBound()=n; - return tmp; - } - - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, - const LpSolverBase::Constr&c) - { - LpSolverBase::Constr tmp(c); - ///\todo Create an own exception type. - if(!isnan(tmp.upperBound())) throw LogicError(); - else tmp.upperBound()=n; - return tmp; - } - ///\e - - ///\relates LpSolverBase::Constr - /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, - const LpSolverBase::Value &n) - { - LpSolverBase::Constr tmp(c); - ///\todo Create an own exception type. - if(!isnan(tmp.lowerBound())) throw LogicError(); - else tmp.lowerBound()=n; - return tmp; - } - - -} //namespace lemon - -#endif //LEMON_LP_BASE_H diff -r c9c2e90b2342 -r c3dc75d4af24 src/work/athos/lp/lp_glpk.cc --- a/src/work/athos/lp/lp_glpk.cc Tue Apr 05 08:43:51 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,288 +0,0 @@ -/* -*- C++ -*- - * src/lemon/lp_glpk.cc - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#ifndef LEMON_LP_GLPK_CC -#define LEMON_LP_GLPK_CC - -///\file -///\brief Implementation of the LEMON-GLPK lp solver interface. - -#include "lp_glpk.h" - -namespace lemon { - - /// \e - int LpGlpk::_addCol() { - int i=lpx_add_cols(lp, 1); - _setColLowerBound(i, -INF); - _setColUpperBound(i, INF); - return i; - } - - /// \e - int LpGlpk::_addRow() { - int i=lpx_add_rows(lp, 1); - return i; - } - - - void LpGlpk::_setRowCoeffs(int i, - int length, - const int * indices, - const Value * values ) - { - lpx_set_mat_row(lp, i, length, - const_cast(indices) , - const_cast(values)); - } - - void LpGlpk::_setColCoeffs(int i, - int length, - const int * indices, - const Value * values) - { - lpx_set_mat_col(lp, i, length, - const_cast(indices), - const_cast(values)); - } - - void LpGlpk::_setColLowerBound(int i, Value lo) - { - if (lo==INF) { - //FIXME error - } - int b=lpx_get_col_type(lp, i); - double up=lpx_get_col_ub(lp, i); - if (lo==-INF) { - switch (b) { - case LPX_FR: - case LPX_LO: - lpx_set_col_bnds(lp, i, LPX_FR, lo, up); - break; - case LPX_UP: - break; - case LPX_DB: - case LPX_FX: - lpx_set_col_bnds(lp, i, LPX_UP, lo, up); - break; - default: ; - //FIXME error - } - } else { - switch (b) { - case LPX_FR: - case LPX_LO: - lpx_set_col_bnds(lp, i, LPX_LO, lo, up); - break; - case LPX_UP: - case LPX_DB: - case LPX_FX: - if (lo==up) - lpx_set_col_bnds(lp, i, LPX_FX, lo, up); - else - lpx_set_col_bnds(lp, i, LPX_DB, lo, up); - break; - default: ; - //FIXME error - } - } - - } - - void LpGlpk::_setColUpperBound(int i, Value up) - { - if (up==-INF) { - //FIXME error - } - int b=lpx_get_col_type(lp, i); - double lo=lpx_get_col_lb(lp, i); - if (up==INF) { - switch (b) { - case LPX_FR: - case LPX_LO: - break; - case LPX_UP: - lpx_set_col_bnds(lp, i, LPX_FR, lo, up); - break; - case LPX_DB: - case LPX_FX: - lpx_set_col_bnds(lp, i, LPX_LO, lo, up); - break; - default: ; - //FIXME error - } - } else { - switch (b) { - case LPX_FR: - lpx_set_col_bnds(lp, i, LPX_UP, lo, up); - break; - case LPX_UP: - lpx_set_col_bnds(lp, i, LPX_UP, lo, up); - break; - case LPX_LO: - case LPX_DB: - case LPX_FX: - if (lo==up) - lpx_set_col_bnds(lp, i, LPX_FX, lo, up); - else - lpx_set_col_bnds(lp, i, LPX_DB, lo, up); - break; - default: ; - //FIXME error - } - } - } - - void LpGlpk::_setRowLowerBound(int i, Value lo) - { - if (lo==INF) { - //FIXME error - } - int b=lpx_get_row_type(lp, i); - double up=lpx_get_row_ub(lp, i); - if (lo==-INF) { - switch (b) { - case LPX_FR: - case LPX_LO: - lpx_set_row_bnds(lp, i, LPX_FR, lo, up); - break; - case LPX_UP: - break; - case LPX_DB: - case LPX_FX: - lpx_set_row_bnds(lp, i, LPX_UP, lo, up); - break; - default: ; - //FIXME error - } - } else { - switch (b) { - case LPX_FR: - case LPX_LO: - lpx_set_row_bnds(lp, i, LPX_LO, lo, up); - break; - case LPX_UP: - case LPX_DB: - case LPX_FX: - if (lo==up) - lpx_set_row_bnds(lp, i, LPX_FX, lo, up); - else - lpx_set_row_bnds(lp, i, LPX_DB, lo, up); - break; - default: ; - //FIXME error - } - } - } - - void LpGlpk::_setRowUpperBound(int i, Value up) - { - if (up==-INF) { - //FIXME error - } - int b=lpx_get_row_type(lp, i); - double lo=lpx_get_row_lb(lp, i); - if (up==INF) { - switch (b) { - case LPX_FR: - case LPX_LO: - break; - case LPX_UP: - lpx_set_row_bnds(lp, i, LPX_FR, lo, up); - break; - case LPX_DB: - case LPX_FX: - lpx_set_row_bnds(lp, i, LPX_LO, lo, up); - break; - default: ; - //FIXME error - } - } else { - switch (b) { - case LPX_FR: - lpx_set_row_bnds(lp, i, LPX_UP, lo, up); - break; - case LPX_UP: - lpx_set_row_bnds(lp, i, LPX_UP, lo, up); - break; - case LPX_LO: - case LPX_DB: - case LPX_FX: - if (lo==up) - lpx_set_row_bnds(lp, i, LPX_FX, lo, up); - else - lpx_set_row_bnds(lp, i, LPX_DB, lo, up); - break; - default: ; - //FIXME error - } - } - } - - void LpGlpk::_setObjCoeff(int i, Value obj_coef) - { - lpx_set_obj_coef(lp, i, obj_coef); - } - - - LpGlpk::SolveExitStatus LpGlpk::_solve() - { - int i= lpx_simplex(lp); - switch (i) { - case LPX_E_OK: - return SOLVED; - break; - default: - return UNSOLVED; - } - } - - LpGlpk::Value LpGlpk::_getPrimal(int i) - { - return lpx_get_col_prim(lp,i); - } - - - LpGlpk::SolutionStatus LpGlpk::_getPrimalType() - { - int stat= lpx_get_status(lp); - switch (stat) { - case LPX_UNDEF://Undefined (no solve has been run yet) - return UNDEFINED; - break; - case LPX_NOFEAS://There is no feasible solution (primal, I guess) - case LPX_INFEAS://Infeasible - return INFEASIBLE; - break; - case LPX_UNBND://Unbounded - return INFINITE; - break; - case LPX_FEAS://Feasible - return FEASIBLE; - break; - case LPX_OPT://Feasible - return OPTIMAL; - break; - default: - return UNDEFINED; //to avoid gcc warning - //FIXME error - } - } - - -} //END OF NAMESPACE LEMON - -#endif //LEMON_LP_GLPK_CC diff -r c9c2e90b2342 -r c3dc75d4af24 src/work/athos/lp/lp_glpk.h --- a/src/work/athos/lp/lp_glpk.h Tue Apr 05 08:43:51 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -/* -*- C++ -*- - * src/lemon/lp_glpk.h - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#ifndef LEMON_LP_GLPK_H -#define LEMON_LP_GLPK_H - -///\file -///\brief Header of the LEMON-GLPK lp solver interface. - -#include "lp_base.h" -extern "C" { -#include "glpk.h" -} - -namespace lemon { - - - /// \brief Wrapper for GLPK solver - /// - /// This class implements a lemon wrapper for GLPK. - class LpGlpk : public LpSolverBase { - - public: - - typedef LpSolverBase Parent; - - /// \e - LPX* lp; - - /// \e - LpGlpk() : Parent(), - lp(lpx_create_prob()) { - lpx_set_int_parm(lp, LPX_K_DUAL, 1); - } - /// \e - ~LpGlpk() { - lpx_delete_prob(lp); - } - - protected: - virtual int _addCol(); - virtual int _addRow(); - virtual void _setRowCoeffs(int i, - int length, - const int * indices, - const Value * values ); - virtual void _setColCoeffs(int i, - int length, - const int * indices, - const Value * values); - virtual void _setColLowerBound(int i, Value value); - virtual void _setColUpperBound(int i, Value value); - virtual void _setRowLowerBound(int i, Value value); - virtual void _setRowUpperBound(int i, Value value); - virtual void _setObjCoeff(int i, Value obj_coef); - ///\e - - ///\bug Unimplemented - /// - virtual SolveExitStatus _solve(); - ///\e - - ///\bug Unimplemented - /// - virtual Value _getPrimal(int i); - ///\e - - ///\bug Unimplemented - /// - virtual SolutionStatus _getPrimalType(); - - }; -} //END OF NAMESPACE LEMON - -#endif //LEMON_LP_GLPK_H - diff -r c9c2e90b2342 -r c3dc75d4af24 src/work/athos/lp/lp_solver_skeleton.cc --- a/src/work/athos/lp/lp_solver_skeleton.cc Tue Apr 05 08:43:51 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -/* -*- C++ -*- - * src/lemon/lp_solver_skeleton.cc - * - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#include"lp_solver_skeleton.h" - -///\file -///\brief A skeleton file to implement LP solver interfaces -namespace lemon { - - int LpSolverSkeleton::_addCol() - { - return ++col_num; - } - - int LpSolverSkeleton::_addRow() - { - return ++row_num; - } - - void LpSolverSkeleton::_setRowCoeffs(int i, - int length, - int const * indices, - Value const * values ) - { - } - - void LpSolverSkeleton::_setColCoeffs(int i, - int length, - int const * indices, - Value const * values) - { - } - - void LpSolverSkeleton::_setColLowerBound(int i, Value value) - { - } - - void LpSolverSkeleton::_setColUpperBound(int i, Value value) - { - } - - void LpSolverSkeleton::_setRowLowerBound(int i, Value value) - { - } - - void LpSolverSkeleton::_setRowUpperBound(int i, Value value) - { - } - - void LpSolverSkeleton::_setObjCoeff(int i, Value obj_coef) - { - } - - LpSolverSkeleton::SolveExitStatus LpSolverSkeleton::_solve() - { - return SOLVED; - } - - LpSolverSkeleton::Value LpSolverSkeleton::_getPrimal(int i) - { - return 0; - } - - LpSolverSkeleton::SolutionStatus LpSolverSkeleton::_getPrimalType() - { - return OPTIMAL; - } - -} //namespace lemon - diff -r c9c2e90b2342 -r c3dc75d4af24 src/work/athos/lp/lp_solver_skeleton.h --- a/src/work/athos/lp/lp_solver_skeleton.h Tue Apr 05 08:43:51 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -/* -*- C++ -*- - * src/lemon/lp_solver_skeleton.h - * - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#ifndef LEMON_LP_SOLVER_SKELETON -#define LEMON_LP_SOLVER_SKELETON - -#include"lp_base.h" - -///\file -///\brief A skeleton file to implement LP solver interfaces -namespace lemon { - - ///A skeleton class to implement LP solver interfaces - class LpSolverSkeleton :public LpSolverBase { - int col_num,row_num; - - protected: - /// \e - virtual int _addCol(); - /// \e - virtual int _addRow(); - /// \e - - /// \warning Arrays are indexed from 1 (datum at index 0 is ignored) - /// - virtual void _setRowCoeffs(int i, - int length, - int const * indices, - Value const * values ); - /// \e - - /// \warning Arrays are indexed from 1 (datum at index 0 is ignored) - /// - virtual void _setColCoeffs(int i, - int length, - int const * indices, - Value const * values ); - - /// \e - - /// The lower bound of a variable (column) have to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or -\ref INF. - virtual void _setColLowerBound(int i, Value value); - /// \e - - /// The upper bound of a variable (column) have to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or \ref INF. - virtual void _setColUpperBound(int i, Value value); - /// \e - - /// The lower bound of a linear expression (row) have to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or -\ref INF. - virtual void _setRowLowerBound(int i, Value value); - /// \e - - /// The upper bound of a linear expression (row) have to be given by an - /// extended number of type Value, i.e. a finite number of type - /// Value or \ref INF. - virtual void _setRowUpperBound(int i, Value value); - - /// \e - virtual void _setObjCoeff(int i, Value obj_coef); - - ///\e - - ///\bug Wrong interface - /// - virtual SolveExitStatus _solve(); - - ///\e - - ///\bug Wrong interface - /// - virtual Value _getPrimal(int i); - ///\e - - ///\bug Wrong interface - /// - virtual SolutionStatus _getPrimalType(); - - public: - LpSolverSkeleton() : LpSolverBase(), col_num(0), row_num(0) {} - }; - -} //namespace lemon - -#endif // LEMON_LP_SOLVER_SKELETON