diff -r 17be4c5bc6c6 -r 2b2ffa625775 src/work/athos/lp/lp_base.h --- a/src/work/athos/lp/lp_base.h Wed Mar 30 08:28:44 2005 +0000 +++ b/src/work/athos/lp/lp_base.h Wed Mar 30 10:38:22 2005 +0000 @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -72,6 +73,7 @@ } return cross[n]; } + ///\todo Create an own exception type. else throw LogicError(); //floatingId-s must form a continuous range; } ///Remove a fix id. @@ -126,8 +128,7 @@ ///This type is used to refer to a column of the LP. /// ///Its value remains valid and correct even after the addition or erase of - ///new column (unless the referred column itself was also deleted, - ///of course). + ///other columns. /// ///\todo Document what can one do with a Col (INVALID, comparing, ///it is similar to Node/Edge) @@ -150,7 +151,7 @@ ///This type is used to refer to a row of the LP. /// ///Its value remains valid and correct even after the addition or erase of - ///new rows (unless the referred row itself was also deleted, of course). + ///other rows. /// ///\todo Document what can one do with a Row (INVALID, comparing, ///it is similar to Node/Edge) @@ -171,34 +172,34 @@ ///Linear expression // typedef SparseLinExpr Expr; - class Expr : public std::map + class Expr : public std::map { public: - typedef Col Var; - typedef Col::ExprValue Coeff; + typedef LpSolverBase::Col Key; + typedef LpSolverBase::Value Value; protected: - typedef std::map Base; + typedef std::map Base; - Coeff const_comp; + Value const_comp; public: typedef True IsLinExpression; ///\e Expr() : Base(), const_comp(0) { } ///\e - Expr(const Var &v) : const_comp(0) { + Expr(const Key &v) : const_comp(0) { Base::insert(std::make_pair(v, 1)); } ///\e - Expr(const Coeff &v) : const_comp(v) {} + Expr(const Value &v) : const_comp(v) {} ///\e - void set(const Var &v,const Coeff &c) { + void set(const Key &v,const Value &c) { Base::insert(std::make_pair(v, c)); } ///\e - Coeff &constComp() { return const_comp; } + Value &constComp() { return const_comp; } ///\e - const Coeff &constComp() const { return const_comp; } + const Value &constComp() const { return const_comp; } ///Removes the components with zero coefficient. void simplify() { @@ -209,7 +210,13 @@ 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) @@ -226,14 +233,14 @@ return *this; } ///\e - Expr &operator*=(const Coeff &c) { + 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 Coeff &c) { + Expr &operator/=(const Value &c) { for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) j->second/=c; const_comp/=c; @@ -247,26 +254,50 @@ { public: typedef LpSolverBase::Expr Expr; - typedef Expr::Var Var; - typedef Expr::Coeff Coeff; + typedef Expr::Key Key; + typedef Expr::Value Value; - static const Coeff INF; - static const Coeff NaN; - // static const Coeff INF=0; - // static const Coeff NaN=1; + static const Value INF; + static const Value NaN; + // static const Value INF=0; + // static const Value NaN=1; - Expr expr; - Coeff lb,ub; - - Constr() : expr(), lb(NaN), ub(NaN) {} - Constr(Coeff _lb,const Expr &e,Coeff _ub) : - expr(e), lb(_lb), ub(_ub) {} - Constr(const Expr &e,Coeff _ub) : - expr(e), lb(NaN), ub(_ub) {} - Constr(Coeff _lb,const Expr &e) : - expr(e), lb(_lb), ub(NaN) {} + 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) {} + _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; } }; @@ -354,16 +385,28 @@ ///\brief Fill the elements of a container with newly created columns ///(i.e a new variables) /// - ///This magic function takes container as its argument + ///This magic function takes a container as its argument ///and fills its elements ///with new columns (i.e. variables) - ///\param t can be either any standard STL iterable container with - ///\ref Col \c values_type or \c mapped_type - ///like std::vector, - /// std::list or - /// std::map or - ///it can be an iterable lemon map like - /// ListGraph::NodeMap. + ///\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 @@ -440,9 +483,10 @@ ///\param r is the row to be modified ///\param c is a linear expression (see \ref Constr) void setRow(Row r, const Constr &c) { - Value lb= c.lb==NaN?-INF:lb; - Value ub= c.ub==NaN?INF:lb; - setRow(r,lb,c.expr,ub); + setRow(r, + isnan(c.lowerBound())?-INF:c.lowerBound(), + c.expr(), + isnan(c.upperBound())?INF:c.upperBound()); } ///Add a new row (i.e a new constaint) to the LP @@ -563,7 +607,7 @@ ///\relates LpSolverBase::Expr /// inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, - const LpSolverBase::Expr::Coeff &b) + const LpSolverBase::Value &b) { LpSolverBase::Expr tmp(a); tmp*=b; ///\todo Don't STL have some special 'merge' algorithm? @@ -574,7 +618,7 @@ ///\relates LpSolverBase::Expr /// - inline LpSolverBase::Expr operator*(const LpSolverBase::Expr::Coeff &a, + inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, const LpSolverBase::Expr &b) { LpSolverBase::Expr tmp(b); @@ -586,7 +630,7 @@ ///\relates LpSolverBase::Expr /// inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, - const LpSolverBase::Expr::Coeff &b) + const LpSolverBase::Value &b) { LpSolverBase::Expr tmp(a); tmp/=b; ///\todo Don't STL have some special 'merge' algorithm? @@ -607,7 +651,7 @@ ///\relates LpSolverBase::Constr /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr::Coeff &e, + inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, const LpSolverBase::Expr &f) { return LpSolverBase::Constr(e,f); @@ -618,7 +662,7 @@ ///\relates LpSolverBase::Constr /// inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, - const LpSolverBase::Expr::Coeff &f) + const LpSolverBase::Value &f) { return LpSolverBase::Constr(e,f); } @@ -638,7 +682,7 @@ ///\relates LpSolverBase::Constr /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr::Coeff &e, + inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, const LpSolverBase::Expr &f) { return LpSolverBase::Constr(f,e); @@ -650,7 +694,7 @@ ///\relates LpSolverBase::Constr /// inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, - const LpSolverBase::Expr::Coeff &f) + const LpSolverBase::Value &f) { return LpSolverBase::Constr(f,e); } @@ -669,12 +713,13 @@ ///\relates LpSolverBase::Constr /// - inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr::Coeff &n, + inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, const LpSolverBase::Constr&c) { LpSolverBase::Constr tmp(c); - if(tmp.lb!=tmp.NaN) throw LogicError(); - else tmp.lb=n; + ///\todo Create an own exception type. + if(!isnan(tmp.lowerBound())) throw LogicError(); + else tmp.lowerBound()=n; return tmp; } ///\e @@ -682,11 +727,12 @@ ///\relates LpSolverBase::Constr /// inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, - const LpSolverBase::Constr::Coeff &n) + const LpSolverBase::Value &n) { LpSolverBase::Constr tmp(c); - if(tmp.ub!=tmp.NaN) throw LogicError(); - else tmp.ub=n; + ///\todo Create an own exception type. + if(!isnan(tmp.upperBound())) throw LogicError(); + else tmp.upperBound()=n; return tmp; } @@ -694,12 +740,13 @@ ///\relates LpSolverBase::Constr /// - inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr::Coeff &n, + inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, const LpSolverBase::Constr&c) { LpSolverBase::Constr tmp(c); - if(tmp.ub!=tmp.NaN) throw LogicError(); - else tmp.ub=n; + ///\todo Create an own exception type. + if(!isnan(tmp.upperBound())) throw LogicError(); + else tmp.upperBound()=n; return tmp; } ///\e @@ -707,11 +754,12 @@ ///\relates LpSolverBase::Constr /// inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, - const LpSolverBase::Constr::Coeff &n) + const LpSolverBase::Value &n) { LpSolverBase::Constr tmp(c); - if(tmp.lb!=tmp.NaN) throw LogicError(); - else tmp.lb=n; + ///\todo Create an own exception type. + if(!isnan(tmp.lowerBound())) throw LogicError(); + else tmp.lowerBound()=n; return tmp; }