athos@1247: /* -*- C++ -*- ladanyi@1435: * lemon/lp_base.h - Part of LEMON, a generic C++ optimization library athos@1247: * alpar@1875: * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). athos@1247: * athos@1247: * Permission to use, modify and distribute this software is granted athos@1247: * provided that this copyright notice appears in all copies. For athos@1247: * precise terms see the accompanying LICENSE file. athos@1247: * athos@1247: * This software is provided "AS IS" with no warranty of any kind, athos@1247: * express or implied, and with no claim as to its suitability for any athos@1247: * purpose. athos@1247: * athos@1247: */ athos@1247: athos@1246: #ifndef LEMON_LP_BASE_H athos@1246: #define LEMON_LP_BASE_H athos@1246: alpar@1253: #include alpar@1272: #include alpar@1256: #include alpar@1397: #include alpar@1253: alpar@1256: #include alpar@1253: #include alpar@1256: #include alpar@1253: athos@1246: ///\file athos@1246: ///\brief The interface of the LP solver interface. alpar@1328: ///\ingroup gen_opt_group athos@1246: namespace lemon { alpar@1253: alpar@1253: ///Internal data structure to convert floating id's to fix one's alpar@1253: alpar@1279: ///\todo This might be implemented to be also usable in other places. alpar@1253: class _FixId alpar@1253: { marci@1787: protected: alpar@1253: std::vector index; alpar@1253: std::vector cross; alpar@1253: int first_free; alpar@1253: public: alpar@1253: _FixId() : first_free(-1) {}; alpar@1253: ///Convert a floating id to a fix one alpar@1253: alpar@1253: ///\param n is a floating id alpar@1253: ///\return the corresponding fix id alpar@1484: int fixId(int n) const {return cross[n];} alpar@1253: ///Convert a fix id to a floating one alpar@1253: alpar@1253: ///\param n is a fix id alpar@1253: ///\return the corresponding floating id alpar@1484: int floatingId(int n) const { return index[n];} alpar@1253: ///Add a new floating id. alpar@1253: alpar@1253: ///\param n is a floating id alpar@1253: ///\return the fix id of the new value alpar@1253: ///\todo Multiple additions should also be handled. alpar@1253: int insert(int n) alpar@1253: { alpar@1253: if(n>=int(cross.size())) { alpar@1253: cross.resize(n+1); alpar@1253: if(first_free==-1) { alpar@1253: cross[n]=index.size(); alpar@1253: index.push_back(n); alpar@1253: } alpar@1253: else { alpar@1253: cross[n]=first_free; alpar@1253: int next=index[first_free]; alpar@1253: index[first_free]=n; alpar@1253: first_free=next; alpar@1253: } alpar@1256: return cross[n]; alpar@1253: } alpar@1273: ///\todo Create an own exception type. alpar@1253: else throw LogicError(); //floatingId-s must form a continuous range; alpar@1253: } alpar@1253: ///Remove a fix id. alpar@1253: alpar@1253: ///\param n is a fix id alpar@1253: /// alpar@1253: void erase(int n) alpar@1253: { alpar@1253: int fl=index[n]; alpar@1253: index[n]=first_free; alpar@1253: first_free=n; alpar@1253: for(int i=fl+1;i, so for expamle alpar@1364: ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can alpar@1279: ///read and modify the coefficients like alpar@1279: ///these. alpar@1279: ///\code alpar@1279: ///e[v]=5; alpar@1279: ///e[v]+=12; alpar@1279: ///e.erase(v); alpar@1279: ///\endcode alpar@1279: ///or you can also iterate through its elements. alpar@1279: ///\code alpar@1279: ///double s=0; alpar@1279: ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) alpar@1279: /// s+=i->second; alpar@1279: ///\endcode alpar@1279: ///(This code computes the sum of all coefficients). alpar@1279: ///- Numbers (double's) alpar@1279: ///and variables (\ref Col "Col"s) directly convert to an alpar@1279: ///\ref Expr and the usual linear operations are defined so alpar@1279: ///\code alpar@1279: ///v+w alpar@1279: ///2*v-3.12*(v-w/2)+2 alpar@1279: ///v*2.1+(3*v+(v*12+w+6)*3)/2 alpar@1279: ///\endcode alpar@1328: ///are valid \ref Expr "Expr"essions. alpar@1328: ///The usual assignment operations are also defined. alpar@1279: ///\code alpar@1279: ///e=v+w; alpar@1279: ///e+=2*v-3.12*(v-w/2)+2; alpar@1279: ///e*=3.4; alpar@1279: ///e/=5; alpar@1279: ///\endcode alpar@1279: ///- The constant member can be set and read by \ref constComp() alpar@1279: ///\code alpar@1279: ///e.constComp()=12; alpar@1279: ///double c=e.constComp(); alpar@1279: ///\endcode alpar@1279: /// alpar@1328: ///\note \ref clear() not only sets all coefficients to 0 but also alpar@1279: ///clears the constant components. alpar@1328: /// alpar@1328: ///\sa Constr alpar@1328: /// alpar@1273: class Expr : public std::map alpar@1272: { alpar@1272: public: alpar@1273: typedef LpSolverBase::Col Key; alpar@1273: typedef LpSolverBase::Value Value; alpar@1272: alpar@1272: protected: alpar@1273: typedef std::map Base; alpar@1272: alpar@1273: Value const_comp; alpar@1272: public: alpar@1272: typedef True IsLinExpression; alpar@1272: ///\e alpar@1272: Expr() : Base(), const_comp(0) { } alpar@1272: ///\e alpar@1273: Expr(const Key &v) : const_comp(0) { alpar@1272: Base::insert(std::make_pair(v, 1)); alpar@1272: } alpar@1272: ///\e alpar@1273: Expr(const Value &v) : const_comp(v) {} alpar@1272: ///\e alpar@1273: void set(const Key &v,const Value &c) { alpar@1272: Base::insert(std::make_pair(v, c)); alpar@1272: } alpar@1272: ///\e alpar@1273: Value &constComp() { return const_comp; } alpar@1272: ///\e alpar@1273: const Value &constComp() const { return const_comp; } alpar@1272: alpar@1272: ///Removes the components with zero coefficient. alpar@1272: void simplify() { alpar@1272: for (Base::iterator i=Base::begin(); i!=Base::end();) { alpar@1272: Base::iterator j=i; alpar@1272: ++j; alpar@1272: if ((*i).second==0) Base::erase(i); alpar@1272: j=i; alpar@1272: } alpar@1272: } alpar@1273: alpar@1771: ///Removes the coefficients closer to zero than \c tolerance. alpar@1771: void simplify(double &tolerance) { alpar@1771: for (Base::iterator i=Base::begin(); i!=Base::end();) { alpar@1771: Base::iterator j=i; alpar@1771: ++j; alpar@1771: if (std::fabs((*i).second)first]+=j->second; alpar@1272: const_comp+=e.const_comp; alpar@1272: return *this; alpar@1272: } alpar@1272: ///\e alpar@1272: Expr &operator-=(const Expr &e) { alpar@1272: for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) alpar@1272: (*this)[j->first]-=j->second; alpar@1272: const_comp-=e.const_comp; alpar@1272: return *this; alpar@1272: } alpar@1272: ///\e alpar@1273: Expr &operator*=(const Value &c) { alpar@1272: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) alpar@1272: j->second*=c; alpar@1272: const_comp*=c; alpar@1272: return *this; alpar@1272: } alpar@1272: ///\e alpar@1273: Expr &operator/=(const Value &c) { alpar@1272: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) alpar@1272: j->second/=c; alpar@1272: const_comp/=c; alpar@1272: return *this; alpar@1272: } alpar@1272: }; alpar@1272: alpar@1264: ///Linear constraint alpar@1328: alpar@1364: ///This data stucture represents a linear constraint in the LP. alpar@1364: ///Basically it is a linear expression with a lower or an upper bound alpar@1364: ///(or both). These parts of the constraint can be obtained by the member alpar@1364: ///functions \ref expr(), \ref lowerBound() and \ref upperBound(), alpar@1364: ///respectively. alpar@1364: ///There are two ways to construct a constraint. alpar@1364: ///- You can set the linear expression and the bounds directly alpar@1364: /// by the functions above. alpar@1364: ///- The operators \<=, == and \>= alpar@1364: /// are defined between expressions, or even between constraints whenever alpar@1364: /// it makes sense. Therefore if \c e and \c f are linear expressions and alpar@1364: /// \c s and \c t are numbers, then the followings are valid expressions alpar@1364: /// and thus they can be used directly e.g. in \ref addRow() whenever alpar@1364: /// it makes sense. alpar@1364: /// \code alpar@1364: /// e<=s alpar@1364: /// e<=f alpar@1364: /// s<=e<=t alpar@1364: /// e>=t alpar@1364: /// \endcode alpar@1364: ///\warning The validity of a constraint is checked only at run time, so alpar@1364: ///e.g. \ref addRow(x[1]\<=x[2]<=5) will compile, but will throw a alpar@1364: ///\ref LogicError exception. alpar@1272: class Constr alpar@1272: { alpar@1272: public: alpar@1272: typedef LpSolverBase::Expr Expr; alpar@1273: typedef Expr::Key Key; alpar@1273: typedef Expr::Value Value; alpar@1272: alpar@1364: // static const Value INF; alpar@1364: // static const Value NaN; alpar@1364: alpar@1273: protected: alpar@1273: Expr _expr; alpar@1273: Value _lb,_ub; alpar@1273: public: alpar@1273: ///\e alpar@1273: Constr() : _expr(), _lb(NaN), _ub(NaN) {} alpar@1273: ///\e alpar@1273: Constr(Value lb,const Expr &e,Value ub) : alpar@1273: _expr(e), _lb(lb), _ub(ub) {} alpar@1273: ///\e alpar@1273: Constr(const Expr &e,Value ub) : alpar@1273: _expr(e), _lb(NaN), _ub(ub) {} alpar@1273: ///\e alpar@1273: Constr(Value lb,const Expr &e) : alpar@1273: _expr(e), _lb(lb), _ub(NaN) {} alpar@1273: ///\e alpar@1272: Constr(const Expr &e) : alpar@1273: _expr(e), _lb(NaN), _ub(NaN) {} alpar@1273: ///\e alpar@1273: void clear() alpar@1273: { alpar@1273: _expr.clear(); alpar@1273: _lb=_ub=NaN; alpar@1273: } alpar@1364: alpar@1364: ///Reference to the linear expression alpar@1273: Expr &expr() { return _expr; } alpar@1364: ///Cont reference to the linear expression alpar@1273: const Expr &expr() const { return _expr; } alpar@1364: ///Reference to the lower bound. alpar@1364: alpar@1364: ///\return alpar@1536: ///- \ref INF "INF": the constraint is lower unbounded. alpar@1536: ///- \ref NaN "NaN": lower bound has not been set. alpar@1364: ///- finite number: the lower bound alpar@1273: Value &lowerBound() { return _lb; } alpar@1364: ///The const version of \ref lowerBound() alpar@1273: const Value &lowerBound() const { return _lb; } alpar@1364: ///Reference to the upper bound. alpar@1364: alpar@1364: ///\return alpar@1536: ///- \ref INF "INF": the constraint is upper unbounded. alpar@1536: ///- \ref NaN "NaN": upper bound has not been set. alpar@1364: ///- finite number: the upper bound alpar@1273: Value &upperBound() { return _ub; } alpar@1364: ///The const version of \ref upperBound() alpar@1273: const Value &upperBound() const { return _ub; } alpar@1364: ///Is the constraint lower bounded? alpar@1295: bool lowerBounded() const { alpar@1295: using namespace std; alpar@1397: return finite(_lb); alpar@1295: } alpar@1364: ///Is the constraint upper bounded? alpar@1295: bool upperBounded() const { alpar@1295: using namespace std; alpar@1397: return finite(_ub); alpar@1295: } alpar@1272: }; alpar@1272: alpar@1445: ///Linear expression of rows alpar@1445: alpar@1445: ///This data structure represents a column of the matrix, alpar@1445: ///thas is it strores a linear expression of the dual variables alpar@1445: ///(\ref Row "Row"s). alpar@1445: /// alpar@1445: ///There are several ways to access and modify the contents of this alpar@1445: ///container. alpar@1445: ///- Its it fully compatible with \c std::map, so for expamle alpar@1445: ///if \c e is an DualExpr and \c v alpar@1445: ///and \c w are of type \ref Row, then you can alpar@1445: ///read and modify the coefficients like alpar@1445: ///these. alpar@1445: ///\code alpar@1445: ///e[v]=5; alpar@1445: ///e[v]+=12; alpar@1445: ///e.erase(v); alpar@1445: ///\endcode alpar@1445: ///or you can also iterate through its elements. alpar@1445: ///\code alpar@1445: ///double s=0; alpar@1445: ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i) alpar@1445: /// s+=i->second; alpar@1445: ///\endcode alpar@1445: ///(This code computes the sum of all coefficients). alpar@1445: ///- Numbers (double's) alpar@1445: ///and variables (\ref Row "Row"s) directly convert to an alpar@1445: ///\ref DualExpr and the usual linear operations are defined so alpar@1445: ///\code alpar@1445: ///v+w alpar@1445: ///2*v-3.12*(v-w/2) alpar@1445: ///v*2.1+(3*v+(v*12+w)*3)/2 alpar@1445: ///\endcode alpar@1445: ///are valid \ref DualExpr "DualExpr"essions. alpar@1445: ///The usual assignment operations are also defined. alpar@1445: ///\code alpar@1445: ///e=v+w; alpar@1445: ///e+=2*v-3.12*(v-w/2); alpar@1445: ///e*=3.4; alpar@1445: ///e/=5; alpar@1445: ///\endcode alpar@1445: /// alpar@1445: ///\sa Expr alpar@1445: /// alpar@1445: class DualExpr : public std::map alpar@1445: { alpar@1445: public: alpar@1445: typedef LpSolverBase::Row Key; alpar@1445: typedef LpSolverBase::Value Value; alpar@1445: alpar@1445: protected: alpar@1445: typedef std::map Base; alpar@1445: alpar@1445: public: alpar@1445: typedef True IsLinExpression; alpar@1445: ///\e alpar@1445: DualExpr() : Base() { } alpar@1445: ///\e alpar@1445: DualExpr(const Key &v) { alpar@1445: Base::insert(std::make_pair(v, 1)); alpar@1445: } alpar@1445: ///\e alpar@1445: void set(const Key &v,const Value &c) { alpar@1445: Base::insert(std::make_pair(v, c)); alpar@1445: } alpar@1445: alpar@1445: ///Removes the components with zero coefficient. alpar@1445: void simplify() { alpar@1445: for (Base::iterator i=Base::begin(); i!=Base::end();) { alpar@1445: Base::iterator j=i; alpar@1445: ++j; alpar@1445: if ((*i).second==0) Base::erase(i); alpar@1445: j=i; alpar@1445: } alpar@1445: } alpar@1445: alpar@1771: ///Removes the coefficients closer to zero than \c tolerance. alpar@1771: void simplify(double &tolerance) { alpar@1771: for (Base::iterator i=Base::begin(); i!=Base::end();) { alpar@1771: Base::iterator j=i; alpar@1771: ++j; alpar@1771: if (std::fabs((*i).second)first]+=j->second; alpar@1445: return *this; alpar@1445: } alpar@1445: ///\e alpar@1445: DualExpr &operator-=(const DualExpr &e) { alpar@1445: for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) alpar@1445: (*this)[j->first]-=j->second; alpar@1445: return *this; alpar@1445: } alpar@1445: ///\e alpar@1445: DualExpr &operator*=(const Value &c) { alpar@1445: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) alpar@1445: j->second*=c; alpar@1445: return *this; alpar@1445: } alpar@1445: ///\e alpar@1445: DualExpr &operator/=(const Value &c) { alpar@1445: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) alpar@1445: j->second/=c; alpar@1445: return *this; alpar@1445: } alpar@1445: }; alpar@1445: alpar@1253: alpar@1253: protected: alpar@1253: _FixId rows; alpar@1253: _FixId cols; athos@1246: alpar@1323: //Abstract virtual functions alpar@1364: virtual LpSolverBase &_newLp() = 0; athos@1436: virtual LpSolverBase &_copyLp(){ athos@1436: ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden. athos@1436: athos@1436: //Starting: athos@1436: LpSolverBase & newlp(_newLp()); athos@1436: return newlp; athos@1436: //return *(LpSolverBase*)0; athos@1436: }; alpar@1364: athos@1246: virtual int _addCol() = 0; athos@1246: virtual int _addRow() = 0; athos@1542: virtual void _eraseCol(int col) = 0; athos@1542: virtual void _eraseRow(int row) = 0; athos@1246: virtual void _setRowCoeffs(int i, athos@1251: int length, athos@1247: int const * indices, athos@1247: Value const * values ) = 0; athos@1246: virtual void _setColCoeffs(int i, athos@1251: int length, athos@1247: int const * indices, athos@1247: Value const * values ) = 0; athos@1431: virtual void _setCoeff(int row, int col, Value value) = 0; alpar@1294: virtual void _setColLowerBound(int i, Value value) = 0; alpar@1294: virtual void _setColUpperBound(int i, Value value) = 0; athos@1405: // virtual void _setRowLowerBound(int i, Value value) = 0; athos@1405: // virtual void _setRowUpperBound(int i, Value value) = 0; athos@1379: virtual void _setRowBounds(int i, Value lower, Value upper) = 0; alpar@1294: virtual void _setObjCoeff(int i, Value obj_coef) = 0; athos@1377: virtual void _clearObj()=0; athos@1377: // virtual void _setObj(int length, athos@1377: // int const * indices, athos@1377: // Value const * values ) = 0; alpar@1303: virtual SolveExitStatus _solve() = 0; alpar@1294: virtual Value _getPrimal(int i) = 0; marci@1787: virtual Value _getDual(int i) = 0; alpar@1312: virtual Value _getPrimalValue() = 0; marci@1840: virtual bool _isBasicCol(int i) = 0; alpar@1312: virtual SolutionStatus _getPrimalStatus() = 0; athos@1460: virtual SolutionStatus _getDualStatus() = 0; athos@1460: ///\todo This could be implemented here, too, using _getPrimalStatus() and athos@1460: ///_getDualStatus() athos@1460: virtual ProblemTypes _getProblemType() = 0; athos@1460: alpar@1312: virtual void _setMax() = 0; alpar@1312: virtual void _setMin() = 0; alpar@1312: alpar@1323: //Own protected stuff alpar@1323: alpar@1323: //Constant component of the objective function alpar@1323: Value obj_const_comp; alpar@1323: athos@1377: athos@1377: alpar@1323: alpar@1253: public: alpar@1253: alpar@1323: ///\e alpar@1323: LpSolverBase() : obj_const_comp(0) {} alpar@1253: alpar@1253: ///\e alpar@1253: virtual ~LpSolverBase() {} alpar@1253: alpar@1364: ///Creates a new LP problem alpar@1364: LpSolverBase &newLp() {return _newLp();} alpar@1381: ///Makes a copy of the LP problem alpar@1364: LpSolverBase ©Lp() {return _copyLp();} alpar@1364: alpar@1612: ///\name Build up and modify the LP alpar@1263: alpar@1263: ///@{ alpar@1263: alpar@1253: ///Add a new empty column (i.e a new variable) to the LP alpar@1253: Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} alpar@1263: alpar@1294: ///\brief Adds several new columns alpar@1294: ///(i.e a variables) at once alpar@1256: /// alpar@1273: ///This magic function takes a container as its argument alpar@1256: ///and fills its elements alpar@1256: ///with new columns (i.e. variables) alpar@1273: ///\param t can be alpar@1273: ///- a standard STL compatible iterable container with alpar@1273: ///\ref Col as its \c values_type alpar@1273: ///like alpar@1273: ///\code alpar@1273: ///std::vector alpar@1273: ///std::list alpar@1273: ///\endcode alpar@1273: ///- a standard STL compatible iterable container with alpar@1273: ///\ref Col as its \c mapped_type alpar@1273: ///like alpar@1273: ///\code alpar@1364: ///std::map alpar@1273: ///\endcode alpar@1273: ///- an iterable lemon \ref concept::WriteMap "write map" like alpar@1273: ///\code alpar@1273: ///ListGraph::NodeMap alpar@1273: ///ListGraph::EdgeMap alpar@1273: ///\endcode alpar@1256: ///\return The number of the created column. alpar@1256: #ifdef DOXYGEN alpar@1256: template alpar@1256: int addColSet(T &t) { return 0;} alpar@1256: #else alpar@1256: template alpar@1256: typename enable_if::type alpar@1256: addColSet(T &t,dummy<0> = 0) { alpar@1256: int s=0; alpar@1256: for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} alpar@1256: return s; alpar@1256: } alpar@1256: template alpar@1256: typename enable_if::type alpar@1256: addColSet(T &t,dummy<1> = 1) { alpar@1256: int s=0; alpar@1256: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1256: i->second=addCol(); alpar@1256: s++; alpar@1256: } alpar@1256: return s; alpar@1256: } alpar@1272: template deba@1810: typename enable_if::type alpar@1272: addColSet(T &t,dummy<2> = 2) { alpar@1272: int s=0; deba@1810: for(typename T::MapIt i(t); i!=INVALID; ++i) alpar@1272: { deba@1810: i.set(addCol()); alpar@1272: s++; alpar@1272: } alpar@1272: return s; alpar@1272: } alpar@1256: #endif alpar@1263: alpar@1445: ///Set a column (i.e a dual constraint) of the LP alpar@1258: alpar@1445: ///\param c is the column to be modified alpar@1445: ///\param e is a dual linear expression (see \ref DualExpr) athos@1542: ///\bug This is a temporary function. The interface will change to alpar@1445: ///a better one. alpar@1445: void setCol(Col c,const DualExpr &e) { alpar@1445: std::vector indices; alpar@1445: std::vector values; alpar@1445: indices.push_back(0); alpar@1445: values.push_back(0); alpar@1445: for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i) alpar@1445: if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! marci@1787: indices.push_back(rows.floatingId((*i).first.id)); alpar@1445: values.push_back((*i).second); alpar@1445: } alpar@1445: _setColCoeffs(cols.floatingId(c.id),indices.size()-1, alpar@1445: &indices[0],&values[0]); alpar@1445: } alpar@1445: alpar@1445: ///Add a new column to the LP alpar@1445: alpar@1445: ///\param e is a dual linear expression (see \ref DualExpr) alpar@1445: ///\param obj is the corresponding component of the objective alpar@1445: ///function. It is 0 by default. alpar@1445: ///\return The created column. alpar@1445: ///\bug This is a temportary function. The interface will change to alpar@1445: ///a better one. alpar@1493: Col addCol(const DualExpr &e, Value obj=0) { alpar@1445: Col c=addCol(); alpar@1445: setCol(c,e); alpar@1493: objCoeff(c,obj); alpar@1445: return c; alpar@1445: } alpar@1445: alpar@1445: ///Add a new empty row (i.e a new constraint) to the LP alpar@1445: alpar@1445: ///This function adds a new empty row (i.e a new constraint) to the LP. alpar@1258: ///\return The created row alpar@1253: Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} alpar@1253: athos@1542: ///\brief Add several new rows athos@1542: ///(i.e a constraints) at once alpar@1445: /// alpar@1445: ///This magic function takes a container as its argument alpar@1445: ///and fills its elements alpar@1445: ///with new row (i.e. variables) alpar@1445: ///\param t can be alpar@1445: ///- a standard STL compatible iterable container with alpar@1445: ///\ref Row as its \c values_type alpar@1445: ///like alpar@1445: ///\code alpar@1445: ///std::vector alpar@1445: ///std::list alpar@1445: ///\endcode alpar@1445: ///- a standard STL compatible iterable container with alpar@1445: ///\ref Row as its \c mapped_type alpar@1445: ///like alpar@1445: ///\code alpar@1445: ///std::map alpar@1445: ///\endcode alpar@1445: ///- an iterable lemon \ref concept::WriteMap "write map" like alpar@1445: ///\code alpar@1445: ///ListGraph::NodeMap alpar@1445: ///ListGraph::EdgeMap alpar@1445: ///\endcode alpar@1445: ///\return The number of rows created. alpar@1445: #ifdef DOXYGEN alpar@1445: template alpar@1445: int addRowSet(T &t) { return 0;} alpar@1445: #else alpar@1445: template alpar@1445: typename enable_if::type alpar@1445: addRowSet(T &t,dummy<0> = 0) { alpar@1445: int s=0; alpar@1445: for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;} alpar@1445: return s; alpar@1445: } alpar@1445: template alpar@1445: typename enable_if::type alpar@1445: addRowSet(T &t,dummy<1> = 1) { alpar@1445: int s=0; alpar@1445: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1445: i->second=addRow(); alpar@1445: s++; alpar@1445: } alpar@1445: return s; alpar@1445: } alpar@1445: template deba@1810: typename enable_if::type alpar@1445: addRowSet(T &t,dummy<2> = 2) { alpar@1445: int s=0; deba@1810: for(typename T::MapIt i(t); i!=INVALID; ++i) alpar@1445: { deba@1810: i.set(addRow()); alpar@1445: s++; alpar@1445: } alpar@1445: return s; alpar@1445: } alpar@1445: #endif alpar@1445: alpar@1445: ///Set a row (i.e a constraint) of the LP alpar@1253: alpar@1258: ///\param r is the row to be modified alpar@1259: ///\param l is lower bound (-\ref INF means no bound) alpar@1258: ///\param e is a linear expression (see \ref Expr) alpar@1259: ///\param u is the upper bound (\ref INF means no bound) alpar@1253: ///\bug This is a temportary function. The interface will change to alpar@1253: ///a better one. alpar@1328: ///\todo Option to control whether a constraint with a single variable is alpar@1328: ///added or not. alpar@1258: void setRow(Row r, Value l,const Expr &e, Value u) { alpar@1253: std::vector indices; alpar@1253: std::vector values; alpar@1253: indices.push_back(0); alpar@1253: values.push_back(0); alpar@1258: for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) alpar@1256: if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! alpar@1256: indices.push_back(cols.floatingId((*i).first.id)); alpar@1256: values.push_back((*i).second); alpar@1256: } alpar@1253: _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, alpar@1253: &indices[0],&values[0]); athos@1405: // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); athos@1405: // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); athos@1405: _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp()); alpar@1258: } alpar@1258: alpar@1445: ///Set a row (i.e a constraint) of the LP alpar@1264: alpar@1264: ///\param r is the row to be modified alpar@1264: ///\param c is a linear expression (see \ref Constr) alpar@1264: void setRow(Row r, const Constr &c) { alpar@1273: setRow(r, alpar@1275: c.lowerBounded()?c.lowerBound():-INF, alpar@1273: c.expr(), alpar@1275: c.upperBounded()?c.upperBound():INF); alpar@1264: } alpar@1264: alpar@1445: ///Add a new row (i.e a new constraint) to the LP alpar@1258: alpar@1259: ///\param l is the lower bound (-\ref INF means no bound) alpar@1258: ///\param e is a linear expression (see \ref Expr) alpar@1259: ///\param u is the upper bound (\ref INF means no bound) alpar@1258: ///\return The created row. alpar@1258: ///\bug This is a temportary function. The interface will change to alpar@1258: ///a better one. alpar@1258: Row addRow(Value l,const Expr &e, Value u) { alpar@1258: Row r=addRow(); alpar@1258: setRow(r,l,e,u); alpar@1253: return r; alpar@1253: } alpar@1253: alpar@1445: ///Add a new row (i.e a new constraint) to the LP alpar@1264: alpar@1264: ///\param c is a linear expression (see \ref Constr) alpar@1264: ///\return The created row. alpar@1264: Row addRow(const Constr &c) { alpar@1264: Row r=addRow(); alpar@1264: setRow(r,c); alpar@1264: return r; alpar@1264: } athos@1542: ///Erase a coloumn (i.e a variable) from the LP athos@1542: athos@1542: ///\param c is the coloumn to be deleted athos@1542: ///\todo Please check this athos@1542: void eraseCol(Col c) { athos@1542: _eraseCol(cols.floatingId(c.id)); athos@1542: cols.erase(c.id); athos@1542: } athos@1542: ///Erase a row (i.e a constraint) from the LP athos@1542: athos@1542: ///\param r is the row to be deleted athos@1542: ///\todo Please check this athos@1542: void eraseRow(Row r) { athos@1542: _eraseRow(rows.floatingId(r.id)); athos@1542: rows.erase(r.id); athos@1542: } alpar@1264: athos@1436: ///Set an element of the coefficient matrix of the LP athos@1436: athos@1436: ///\param r is the row of the element to be modified athos@1436: ///\param c is the coloumn of the element to be modified athos@1436: ///\param val is the new value of the coefficient athos@1436: void setCoeff(Row r, Col c, Value val){ athos@1436: _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val); athos@1436: } athos@1436: alpar@1253: /// Set the lower bound of a column (i.e a variable) alpar@1253: alpar@1293: /// The upper bound of a variable (column) has to be given by an alpar@1253: /// extended number of type Value, i.e. a finite number of type alpar@1259: /// Value or -\ref INF. alpar@1293: void colLowerBound(Col c, Value value) { alpar@1253: _setColLowerBound(cols.floatingId(c.id),value); alpar@1253: } alpar@1253: /// Set the upper bound of a column (i.e a variable) alpar@1253: alpar@1293: /// The upper bound of a variable (column) has to be given by an alpar@1253: /// extended number of type Value, i.e. a finite number of type alpar@1259: /// Value or \ref INF. alpar@1293: void colUpperBound(Col c, Value value) { alpar@1253: _setColUpperBound(cols.floatingId(c.id),value); alpar@1253: }; alpar@1293: /// Set the lower and the upper bounds of a column (i.e a variable) alpar@1293: alpar@1293: /// The lower and the upper bounds of alpar@1293: /// a variable (column) have to be given by an alpar@1293: /// extended number of type Value, i.e. a finite number of type alpar@1293: /// Value, -\ref INF or \ref INF. alpar@1293: void colBounds(Col c, Value lower, Value upper) { alpar@1293: _setColLowerBound(cols.floatingId(c.id),lower); alpar@1293: _setColUpperBound(cols.floatingId(c.id),upper); alpar@1293: } alpar@1293: athos@1405: // /// Set the lower bound of a row (i.e a constraint) alpar@1253: athos@1405: // /// The lower bound of a linear expression (row) has to be given by an athos@1405: // /// extended number of type Value, i.e. a finite number of type athos@1405: // /// Value or -\ref INF. athos@1405: // void rowLowerBound(Row r, Value value) { athos@1405: // _setRowLowerBound(rows.floatingId(r.id),value); athos@1405: // }; athos@1405: // /// Set the upper bound of a row (i.e a constraint) alpar@1253: athos@1405: // /// The upper bound of a linear expression (row) has to be given by an athos@1405: // /// extended number of type Value, i.e. a finite number of type athos@1405: // /// Value or \ref INF. athos@1405: // void rowUpperBound(Row r, Value value) { athos@1405: // _setRowUpperBound(rows.floatingId(r.id),value); athos@1405: // }; athos@1405: athos@1405: /// Set the lower and the upper bounds of a row (i.e a constraint) alpar@1293: alpar@1293: /// The lower and the upper bounds of alpar@1293: /// a constraint (row) have to be given by an alpar@1293: /// extended number of type Value, i.e. a finite number of type alpar@1293: /// Value, -\ref INF or \ref INF. alpar@1293: void rowBounds(Row c, Value lower, Value upper) { athos@1379: _setRowBounds(rows.floatingId(c.id),lower, upper); athos@1379: // _setRowUpperBound(rows.floatingId(c.id),upper); alpar@1293: } alpar@1293: alpar@1253: ///Set an element of the objective function alpar@1293: void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; alpar@1253: ///Set the objective function alpar@1253: alpar@1253: ///\param e is a linear expression of type \ref Expr. alpar@1253: void setObj(Expr e) { athos@1377: _clearObj(); alpar@1253: for (Expr::iterator i=e.begin(); i!=e.end(); ++i) alpar@1293: objCoeff((*i).first,(*i).second); alpar@1323: obj_const_comp=e.constComp(); alpar@1253: } alpar@1263: alpar@1312: ///Maximize alpar@1312: void max() { _setMax(); } alpar@1312: ///Minimize alpar@1312: void min() { _setMin(); } alpar@1312: alpar@1312: alpar@1263: ///@} alpar@1263: alpar@1263: alpar@1294: ///\name Solve the LP alpar@1263: alpar@1263: ///@{ alpar@1263: athos@1458: ///\e Solve the LP problem at hand athos@1458: /// athos@1458: ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus. athos@1458: /// athos@1458: ///\todo Which method is used to solve the problem alpar@1303: SolveExitStatus solve() { return _solve(); } alpar@1263: alpar@1263: ///@} alpar@1263: alpar@1294: ///\name Obtain the solution alpar@1263: alpar@1263: ///@{ alpar@1263: athos@1460: /// The status of the primal problem (the original LP problem) alpar@1312: SolutionStatus primalStatus() { alpar@1312: return _getPrimalStatus(); alpar@1294: } alpar@1294: athos@1460: /// The status of the dual (of the original LP) problem athos@1460: SolutionStatus dualStatus() { athos@1460: return _getDualStatus(); athos@1460: } athos@1460: athos@1460: ///The type of the original LP problem athos@1462: ProblemTypes problemType() { athos@1460: return _getProblemType(); athos@1460: } athos@1460: alpar@1294: ///\e alpar@1293: Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } alpar@1263: alpar@1312: ///\e marci@1787: Value dual(Row r) { return _getDual(rows.floatingId(r.id)); } marci@1787: marci@1787: ///\e marci@1840: bool isBasicCol(Col c) { return _isBasicCol(cols.floatingId(c.id)); } marci@1840: marci@1840: ///\e alpar@1312: alpar@1312: ///\return alpar@1312: ///- \ref INF or -\ref INF means either infeasibility or unboundedness alpar@1312: /// of the primal problem, depending on whether we minimize or maximize. alpar@1364: ///- \ref NaN if no primal solution is found. alpar@1312: ///- The (finite) objective value if an optimal solution is found. alpar@1323: Value primalValue() { return _getPrimalValue()+obj_const_comp;} alpar@1263: ///@} alpar@1253: athos@1248: }; athos@1246: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Expr alpar@1272: /// alpar@1272: inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, alpar@1272: const LpSolverBase::Expr &b) alpar@1272: { alpar@1272: LpSolverBase::Expr tmp(a); alpar@1766: tmp+=b; alpar@1272: return tmp; alpar@1272: } alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Expr alpar@1272: /// alpar@1272: inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, alpar@1272: const LpSolverBase::Expr &b) alpar@1272: { alpar@1272: LpSolverBase::Expr tmp(a); alpar@1766: tmp-=b; alpar@1272: return tmp; alpar@1272: } alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Expr alpar@1272: /// alpar@1272: inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, alpar@1273: const LpSolverBase::Value &b) alpar@1272: { alpar@1272: LpSolverBase::Expr tmp(a); alpar@1766: tmp*=b; alpar@1272: return tmp; alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Expr alpar@1272: /// alpar@1273: inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, alpar@1272: const LpSolverBase::Expr &b) alpar@1272: { alpar@1272: LpSolverBase::Expr tmp(b); alpar@1766: tmp*=a; alpar@1272: return tmp; alpar@1272: } alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Expr alpar@1272: /// alpar@1272: inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, alpar@1273: const LpSolverBase::Value &b) alpar@1272: { alpar@1272: LpSolverBase::Expr tmp(a); alpar@1766: tmp/=b; alpar@1272: return tmp; alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, alpar@1272: const LpSolverBase::Expr &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1273: inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, alpar@1272: const LpSolverBase::Expr &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(e,f); alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, alpar@1273: const LpSolverBase::Value &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(e,f); alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, alpar@1272: const LpSolverBase::Expr &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); alpar@1272: } alpar@1272: alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1273: inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, alpar@1272: const LpSolverBase::Expr &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(f,e); alpar@1272: } alpar@1272: alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, alpar@1273: const LpSolverBase::Value &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(f,e); alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, alpar@1272: const LpSolverBase::Expr &f) alpar@1272: { alpar@1272: return LpSolverBase::Constr(0,e-f,0); alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1273: inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, alpar@1272: const LpSolverBase::Constr&c) alpar@1272: { alpar@1272: LpSolverBase::Constr tmp(c); alpar@1273: ///\todo Create an own exception type. alpar@1273: if(!isnan(tmp.lowerBound())) throw LogicError(); alpar@1273: else tmp.lowerBound()=n; alpar@1272: return tmp; alpar@1272: } alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, alpar@1273: const LpSolverBase::Value &n) alpar@1272: { alpar@1272: LpSolverBase::Constr tmp(c); alpar@1273: ///\todo Create an own exception type. alpar@1273: if(!isnan(tmp.upperBound())) throw LogicError(); alpar@1273: else tmp.upperBound()=n; alpar@1272: return tmp; alpar@1272: } alpar@1272: alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1273: inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, alpar@1272: const LpSolverBase::Constr&c) alpar@1272: { alpar@1272: LpSolverBase::Constr tmp(c); alpar@1273: ///\todo Create an own exception type. alpar@1273: if(!isnan(tmp.upperBound())) throw LogicError(); alpar@1273: else tmp.upperBound()=n; alpar@1272: return tmp; alpar@1272: } alpar@1272: ///\e alpar@1272: alpar@1272: ///\relates LpSolverBase::Constr alpar@1272: /// alpar@1272: inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, alpar@1273: const LpSolverBase::Value &n) alpar@1272: { alpar@1272: LpSolverBase::Constr tmp(c); alpar@1273: ///\todo Create an own exception type. alpar@1273: if(!isnan(tmp.lowerBound())) throw LogicError(); alpar@1273: else tmp.lowerBound()=n; alpar@1272: return tmp; alpar@1272: } alpar@1272: alpar@1445: ///\e alpar@1445: alpar@1445: ///\relates LpSolverBase::DualExpr alpar@1445: /// alpar@1445: inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a, alpar@1445: const LpSolverBase::DualExpr &b) alpar@1445: { alpar@1445: LpSolverBase::DualExpr tmp(a); alpar@1766: tmp+=b; alpar@1445: return tmp; alpar@1445: } alpar@1445: ///\e alpar@1445: alpar@1445: ///\relates LpSolverBase::DualExpr alpar@1445: /// alpar@1445: inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a, alpar@1445: const LpSolverBase::DualExpr &b) alpar@1445: { alpar@1445: LpSolverBase::DualExpr tmp(a); alpar@1766: tmp-=b; alpar@1445: return tmp; alpar@1445: } alpar@1445: ///\e alpar@1445: alpar@1445: ///\relates LpSolverBase::DualExpr alpar@1445: /// alpar@1445: inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a, alpar@1445: const LpSolverBase::Value &b) alpar@1445: { alpar@1445: LpSolverBase::DualExpr tmp(a); alpar@1766: tmp*=b; alpar@1445: return tmp; alpar@1445: } alpar@1445: alpar@1445: ///\e alpar@1445: alpar@1445: ///\relates LpSolverBase::DualExpr alpar@1445: /// alpar@1445: inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a, alpar@1445: const LpSolverBase::DualExpr &b) alpar@1445: { alpar@1445: LpSolverBase::DualExpr tmp(b); alpar@1766: tmp*=a; alpar@1445: return tmp; alpar@1445: } alpar@1445: ///\e alpar@1445: alpar@1445: ///\relates LpSolverBase::DualExpr alpar@1445: /// alpar@1445: inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a, alpar@1445: const LpSolverBase::Value &b) alpar@1445: { alpar@1445: LpSolverBase::DualExpr tmp(a); alpar@1766: tmp/=b; alpar@1445: return tmp; alpar@1445: } alpar@1445: alpar@1272: athos@1246: } //namespace lemon athos@1246: athos@1246: #endif //LEMON_LP_BASE_H