diff -r 2aabce558574 -r 3a5e67bd42d2 lemon/lp_base.h --- a/lemon/lp_base.h Thu Feb 15 14:22:08 2007 +0000 +++ b/lemon/lp_base.h Thu Feb 15 19:15:14 2007 +0000 @@ -49,7 +49,7 @@ _lp_bits::LpId cols; public: - + ///Possible outcomes of an LP solving procedure enum SolveExitStatus { ///This means that the problem has been successfully solved: either @@ -122,6 +122,7 @@ int id; friend class LpSolverBase; friend class MipSolverBase; + explicit Col(int _id) : id(_id) {} public: typedef Value ExprValue; typedef True LpSolverCol; @@ -165,6 +166,7 @@ protected: int id; friend class LpSolverBase; + explicit Row(int _id) : id(_id) {} public: typedef Value ExprValue; typedef True LpSolverRow; @@ -177,6 +179,22 @@ bool operator!=(Row c) const {return id!=c.id;} }; + class RowIt : public Row { + LpSolverBase *_lp; + public: + RowIt() {} + RowIt(LpSolverBase &lp) : _lp(&lp) + { + _lp->rows.firstFix(id); + } + RowIt(const Invalid&) : Row(INVALID) {} + RowIt &operator++() + { + _lp->rows.nextFix(id); + return *this; + } + }; + static int id(const Row& row) { return row.id; } protected: @@ -189,6 +207,14 @@ return rows.floatingId(id(row)); } + Col _item(int id, Col) const { + return Col(cols.fixId(id)); + } + + Row _item(int id, Row) const { + return Row(rows.fixId(id)); + } + public: @@ -581,11 +607,60 @@ private: - template - class MappedIterator { + template + class MappedOutputIterator { public: - typedef _Base Base; + typedef std::insert_iterator<_Expr> Base; + + typedef std::output_iterator_tag iterator_category; + typedef void difference_type; + typedef void value_type; + typedef void reference; + typedef void pointer; + + MappedOutputIterator(const Base& _base, const LpSolverBase& _lp) + : base(_base), lp(_lp) {} + + MappedOutputIterator& operator*() { + return *this; + } + + MappedOutputIterator& operator=(const std::pair& value) { + *base = std::make_pair(lp._item(value.first, typename _Expr::Key()), + value.second); + return *this; + } + + MappedOutputIterator& operator++() { + ++base; + return *this; + } + + MappedOutputIterator operator++(int) { + MappedOutputIterator tmp(*this); + ++base; + return tmp; + } + + bool operator==(const MappedOutputIterator& it) const { + return base == it.base; + } + + bool operator!=(const MappedOutputIterator& it) const { + return base != it.base; + } + + private: + Base base; + const LpSolverBase& lp; + }; + + template + class MappedInputIterator { + public: + + typedef typename Expr::const_iterator Base; typedef typename Base::iterator_category iterator_category; typedef typename Base::difference_type difference_type; @@ -599,7 +674,7 @@ value_type value; }; - MappedIterator(const Base& _base, const LpSolverBase& _lp) + MappedInputIterator(const Base& _base, const LpSolverBase& _lp) : base(_base), lp(_lp) {} reference operator*() { @@ -610,22 +685,22 @@ return pointer(operator*()); } - MappedIterator& operator++() { + MappedInputIterator& operator++() { ++base; return *this; } - MappedIterator& operator++(int) { - MappedIterator tmp(*this); + MappedInputIterator operator++(int) { + MappedInputIterator tmp(*this); ++base; return tmp; } - bool operator==(const MappedIterator& it) const { + bool operator==(const MappedInputIterator& it) const { return base == it.base; } - bool operator!=(const MappedIterator& it) const { + bool operator!=(const MappedInputIterator& it) const { return base != it.base; } @@ -637,9 +712,14 @@ protected: /// STL compatible iterator for lp col - typedef MappedIterator LpRowIterator; + typedef MappedInputIterator ConstRowIterator; /// STL compatible iterator for lp row - typedef MappedIterator LpColIterator; + typedef MappedInputIterator ConstColIterator; + + /// STL compatible iterator for lp col + typedef MappedOutputIterator RowIterator; + /// STL compatible iterator for lp row + typedef MappedOutputIterator ColIterator; //Abstract virtual functions virtual LpSolverBase &_newLp() = 0; @@ -659,11 +739,14 @@ virtual void _eraseRow(int row) = 0; virtual void _getColName(int col, std::string & name) = 0; virtual void _setColName(int col, const std::string & name) = 0; - virtual void _setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) = 0; - virtual void _setColCoeffs(int i, LpColIterator b, LpColIterator e) = 0; + virtual void _setRowCoeffs(int i, ConstRowIterator b, + ConstRowIterator e) = 0; + virtual void _getRowCoeffs(int i, RowIterator b) = 0; + virtual void _setColCoeffs(int i, ConstColIterator b, + ConstColIterator e) = 0; + virtual void _getColCoeffs(int i, ColIterator b) = 0; virtual void _setCoeff(int row, int col, Value value) = 0; virtual Value _getCoeff(int row, int col) = 0; - virtual void _setColLowerBound(int i, Value value) = 0; virtual Value _getColLowerBound(int i) = 0; virtual void _setColUpperBound(int i, Value value) = 0; @@ -786,8 +869,18 @@ ///a better one. void col(Col c,const DualExpr &e) { e.simplify(); - _setColCoeffs(_lpId(c), LpColIterator(e.begin(), *this), - LpColIterator(e.end(), *this)); + _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this), + ConstColIterator(e.end(), *this)); + } + + ///Get a column (i.e a dual constraint) of the LP + + ///\param r is the column to get + ///\return the dual expression associated to the column + DualExpr col(Col c) { + DualExpr e; + _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this)); + return e; } ///Add a new column to the LP @@ -883,9 +976,9 @@ ///added or not. void row(Row r, Value l,const Expr &e, Value u) { e.simplify(); - _setRowCoeffs(_lpId(r), LpRowIterator(e.begin(), *this), - LpRowIterator(e.end(), *this)); - _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp()); + _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this), + ConstRowIterator(e.end(), *this)); + _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp()); } ///Set a row (i.e a constraint) of the LP @@ -897,6 +990,17 @@ c.expr(), c.upperBounded()?c.upperBound():INF); } + + ///Get a row (i.e a constraint) of the LP + + ///\param r is the row to get + ///\return the expression associated to the row + Expr row(Row r) { + Expr e; + _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this)); + return e; + } + ///Add a new row (i.e a new constraint) to the LP ///\param l is the lower bound (-\ref INF means no bound) @@ -1177,6 +1281,21 @@ obj_const_comp=e.constComp(); } + ///Get the objective function + + ///\return the objective function as a linear expression of type \ref Expr. + Expr obj() { + Expr e; + for (ColIt it(*this); it != INVALID; ++it) { + double c = objCoeff(it); + if (c != 0.0) { + e.insert(std::make_pair(it, c)); + } + } + return e; + } + + ///Maximize void max() { _setMax(); } ///Minimize