# HG changeset patch # User marci # Date 1106847844 0 # Node ID 91a8ee9d088d4219f224a4e24c6898ea9af8abe6 # Parent e3b3667c68571ca8a4aea2580d54250b6401a382 -=, - operators in expressions diff -r e3b3667c6857 -r 91a8ee9d088d src/work/marci/lp/expression.h --- a/src/work/marci/lp/expression.h Thu Jan 27 16:11:54 2005 +0000 +++ b/src/work/marci/lp/expression.h Thu Jan 27 17:44:04 2005 +0000 @@ -19,11 +19,18 @@ template class Expr { - protected: +// protected: + public: typedef typename std::map<_Col, _Value> Data; Data data; public: + void simplify() { + for (typename Data::iterator i=data.begin(); + i!=data.end(); ++i) { + if ((*i).second==0) data.erase(i); + } + } Expr() { } Expr(_Col _col) { data.insert(std::make_pair(_col, 1)); @@ -33,6 +40,7 @@ i!=data.end(); ++i) { (*i).second *= _value; } + simplify(); return *this; } Expr& operator+=(const Expr<_Col, _Value>& expr) { @@ -45,6 +53,20 @@ (*i).second+=(*j).second; } } + simplify(); + return *this; + } + Expr& operator-=(const Expr<_Col, _Value>& expr) { + for (typename Data::const_iterator j=expr.data.begin(); + j!=expr.data.end(); ++j) { + typename Data::iterator i=data.find((*j).first); + if (i==data.end()) { + data.insert(std::make_pair((*j).first, -(*j).second)); + } else { + (*i).second+=-(*j).second; + } + } + simplify(); return *this; } template @@ -56,6 +78,7 @@ Expr<_Col, _Value> operator*(_Value _value, _Col _col) { Expr<_Col, _Value> tmp(_col); tmp*=_value; + tmp.simplify(); return tmp; } @@ -64,6 +87,7 @@ const Expr<_Col, _Value>& expr) { Expr<_Col, _Value> tmp(expr); tmp*=_value; + tmp.simplify(); return tmp; } @@ -72,6 +96,16 @@ const Expr<_Col, _Value>& expr2) { Expr<_Col, _Value> tmp(expr1); tmp+=expr2; + tmp.simplify(); + return tmp; + } + + template + Expr<_Col, _Value> operator-(const Expr<_Col, _Value>& expr1, + const Expr<_Col, _Value>& expr2) { + Expr<_Col, _Value> tmp(expr1); + tmp-=expr2; + tmp.simplify(); return tmp; } diff -r e3b3667c6857 -r 91a8ee9d088d src/work/marci/lp/lp_solver_wrapper_3.h --- a/src/work/marci/lp/lp_solver_wrapper_3.h Thu Jan 27 16:11:54 2005 +0000 +++ b/src/work/marci/lp/lp_solver_wrapper_3.h Thu Jan 27 17:44:04 2005 +0000 @@ -27,6 +27,7 @@ //#include //#include #include +#include //#include //#include //#include @@ -143,7 +144,18 @@ ClassIt(const int& _i) : i(_i) { } /// Invalid constructor. ClassIt(const Invalid&) : i(-1) { } + friend bool operator<(const ClassIt& x, const ClassIt& y); + friend std::ostream& operator<<(std::ostream& os, + const ClassIt& it); }; + friend bool operator<(const ClassIt& x, const ClassIt& y) { + return (x.i < y.i); + } + friend std::ostream& operator<<(std::ostream& os, + const ClassIt& it) { + os << it.i; + return os; + } /// First member of class \c class_id. ClassIt& first(ClassIt& it, int class_id) const { it.i=tips[class_id].first; @@ -158,104 +170,6 @@ bool valid(const ClassIt& it) const { return it.i!=-1; } }; - template - class Expr; - - template - class SmallExpr { - template - friend class Expr; - protected: - _Col col; - _Value value; - public: - SmallExpr(_Col _col) : col(_col), value(1) { - } - SmallExpr& operator *= (_Value _value) { - value*=_value; - return *this; - } - // template - // friend SmallExpr<_C, _V> operator* (_V _value, - // const SmallExpr<_C, _V>& expr); - template - friend std::ostream& operator<<(std::ostream& os, - const SmallExpr<_C, _V>& expr); - }; - - template - SmallExpr<_Col, _Value> - operator* (_Value value, - const SmallExpr<_Col, _Value>& expr) { - SmallExpr<_Col, _Value> tmp; - tmp=expr; - tmp*=value; - return tmp; - } - - template - std::ostream& operator<<(std::ostream& os, - const SmallExpr<_Col, _Value>& expr) { - os << expr.value << "*" << expr.col; - return os; - } - - template - class Expr { - protected: - typedef - typename std::map<_Col, _Value> Data; - Data data; - public: - Expr() { } - Expr(SmallExpr<_Col, _Value> expr) { - data.insert(std::make_pair(expr.col, expr.value)); - } -// Expr(_Col col) { -// data.insert(std::make_pair(col, 1)); -// } - Expr& operator*=(_Value _value) { - for (typename Data::iterator i=data.begin(); - i!=data.end(); ++i) { - (*i).second *= _value; - } - return *this; - } - Expr& operator+=(SmallExpr<_Col, _Value> expr) { - typename Data::iterator i=data.find(expr.col); - if (i==data.end()) { - data.insert(std::make_pair(expr.col, expr.value)); - } else { - (*i).second+=expr.value; - } - return *this; - } - // template - // friend Expr<_C, _V> operator*(_V _value, const Expr<_C, _V>& expr); - template - friend std::ostream& operator<<(std::ostream& os, - const Expr<_C, _V>& expr); - }; - - template - Expr<_Col, _Value> operator*(_Value _value, - const Expr<_Col, _Value>& expr) { - Expr<_Col, _Value> tmp; - tmp=expr; - tmp*=_value; - return tmp; - } - - template - std::ostream& operator<<(std::ostream& os, - const Expr<_Col, _Value>& expr) { - for (typename Expr<_Col, _Value>::Data::const_iterator i= - expr.data.begin(); - i!=expr.data.end(); ++i) { - os << (*i).second << "*" << (*i).first << " "; - } - return os; - } /*! \e */ @@ -424,6 +338,39 @@ return _getObjCoef(col_iter_map[col_it]); } + //MOST HIGH LEVEL, USER FRIEND FUNCTIONS + + /// \e + typedef Expr Expression; + /// \e + typedef Expr DualExpression; + /// \e + void setRowCoeffs(RowIt row_it, const Expression& expr) { + std::vector > row_coeffs; + for(typename Expression::Data::const_iterator i=expr.data.begin(); + i!=expr.data.end(); ++i) { + row_coeffs.push_back(std::make_pair + (col_iter_map[(*i).first], (*i).second)); + } + _setRowCoeffs(row_iter_map[row_it], row_coeffs); + } + /// \e + void setColCoeffs(ColIt col_it, const DualExpression& expr) { + std::vector > col_coeffs; + for(typename DualExpression::Data::const_iterator i=expr.data.begin(); + i!=expr.data.end(); ++i) { + col_coeffs.push_back(std::make_pair + (row_iter_map[(*i).first], (*i).second)); + } + _setColCoeffs(col_iter_map[col_it], col_coeffs); + } + /// \e + void setObjCoeffs(const Expression& expr) { + for(typename Expression::Data::const_iterator i=expr.data.begin(); + i!=expr.data.end(); ++i) { + setObjCoef((*i).first, (*i).second); + } + } //SOLVER FUNCTIONS /// \e