athos@1247: /* -*- C++ -*- athos@1247: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@2391: * Copyright (C) 2003-2007 alpar@1956: * 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: athos@2345: #include alpar@1253: #include alpar@1272: #include alpar@1256: #include alpar@1397: #include alpar@1253: alpar@1253: #include deba@1993: #include deba@2363: #include deba@2363: #include alpar@1253: athos@1246: ///\file athos@1246: ///\brief The interface of the LP solver interface. deba@2370: ///\ingroup lp_group athos@1246: namespace lemon { deba@2312: alpar@1253: ///Common base class for LP solvers alpar@1328: alpar@1328: ///\todo Much more docs deba@2370: ///\ingroup lp_group athos@1246: class LpSolverBase { alpar@1323: alpar@2303: protected: alpar@2303: deba@2363: _lp_bits::LpId rows; deba@2363: _lp_bits::LpId cols; deba@2363: athos@1247: public: deba@2364: athos@1458: ///Possible outcomes of an LP solving procedure alpar@1303: enum SolveExitStatus { athos@1458: ///This means that the problem has been successfully solved: either athos@1458: ///an optimal solution has been found or infeasibility/unboundedness athos@1458: ///has been proved. alpar@1293: SOLVED = 0, deba@2312: ///Any other case (including the case when some user specified deba@2312: ///limit has been exceeded) alpar@1293: UNSOLVED = 1 athos@1291: }; athos@1291: athos@1460: ///\e alpar@1303: enum SolutionStatus { athos@2185: ///Feasible solution hasn't been found (but may exist). alpar@1295: alpar@1295: ///\todo NOTFOUND might be a better name. alpar@1295: /// alpar@1293: UNDEFINED = 0, alpar@1295: ///The problem has no feasible solution alpar@1293: INFEASIBLE = 1, alpar@1295: ///Feasible solution found alpar@1293: FEASIBLE = 2, alpar@1295: ///Optimal solution exists and found alpar@1295: OPTIMAL = 3, alpar@1295: ///The cost function is unbounded alpar@1295: alpar@1295: ///\todo Give a feasible solution and an infinite ray (and the alpar@1295: ///corresponding bases) alpar@1295: INFINITE = 4 alpar@1263: }; athos@1460: athos@1542: ///\e The type of the investigated LP problem athos@1542: enum ProblemTypes { athos@1542: ///Primal-dual feasible athos@1542: PRIMAL_DUAL_FEASIBLE = 0, athos@1542: ///Primal feasible dual infeasible athos@1542: PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1, athos@1542: ///Primal infeasible dual feasible athos@1542: PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2, athos@1542: ///Primal-dual infeasible athos@1542: PRIMAL_DUAL_INFEASIBLE = 3, athos@1542: ///Could not determine so far athos@1542: UNKNOWN = 4 athos@1542: }; athos@1508: alpar@1256: ///The floating point type used by the solver athos@1247: typedef double Value; alpar@1256: ///The infinity constant athos@1247: static const Value INF; alpar@1264: ///The not a number constant alpar@1264: static const Value NaN; deba@2026: deba@2026: static inline bool isNaN(const Value& v) { return v!=v; } alpar@1253: alpar@2303: friend class Col; alpar@2303: friend class ColIt; alpar@2303: friend class Row; alpar@2303: alpar@1256: ///Refer to a column of the LP. alpar@1256: alpar@1256: ///This type is used to refer to a column of the LP. alpar@1256: /// alpar@1256: ///Its value remains valid and correct even after the addition or erase of alpar@1273: ///other columns. alpar@1256: /// alpar@1256: ///\todo Document what can one do with a Col (INVALID, comparing, alpar@1256: ///it is similar to Node/Edge) alpar@1256: class Col { alpar@1256: protected: alpar@1256: int id; alpar@1256: friend class LpSolverBase; athos@2144: friend class MipSolverBase; deba@2364: explicit Col(int _id) : id(_id) {} alpar@1256: public: alpar@1259: typedef Value ExprValue; alpar@1256: typedef True LpSolverCol; alpar@1256: Col() {} alpar@1256: Col(const Invalid&) : id(-1) {} alpar@1900: bool operator< (Col c) const {return id< c.id;} alpar@1900: bool operator> (Col c) const {return id> c.id;} alpar@1256: bool operator==(Col c) const {return id==c.id;} alpar@1900: bool operator!=(Col c) const {return id!=c.id;} alpar@1256: }; alpar@1256: alpar@2303: class ColIt : public Col { deba@2366: const LpSolverBase *_lp; alpar@2309: public: alpar@2303: ColIt() {} deba@2366: ColIt(const LpSolverBase &lp) : _lp(&lp) alpar@2303: { deba@2363: _lp->cols.firstFix(id); alpar@2303: } alpar@2303: ColIt(const Invalid&) : Col(INVALID) {} alpar@2303: ColIt &operator++() alpar@2303: { deba@2363: _lp->cols.nextFix(id); alpar@2303: return *this; alpar@2303: } alpar@2303: }; deba@2312: deba@2312: static int id(const Col& col) { return col.id; } deba@2312: alpar@2303: alpar@1256: ///Refer to a row of the LP. alpar@1256: alpar@1256: ///This type is used to refer to a row of the LP. alpar@1256: /// alpar@1256: ///Its value remains valid and correct even after the addition or erase of alpar@1273: ///other rows. alpar@1256: /// alpar@1256: ///\todo Document what can one do with a Row (INVALID, comparing, alpar@1256: ///it is similar to Node/Edge) alpar@1256: class Row { alpar@1256: protected: alpar@1256: int id; alpar@1256: friend class LpSolverBase; deba@2364: explicit Row(int _id) : id(_id) {} alpar@1256: public: alpar@1259: typedef Value ExprValue; alpar@1256: typedef True LpSolverRow; alpar@1256: Row() {} alpar@1256: Row(const Invalid&) : id(-1) {} alpar@1439: alpar@1900: bool operator< (Row c) const {return id< c.id;} alpar@1900: bool operator> (Row c) const {return id> c.id;} alpar@1256: bool operator==(Row c) const {return id==c.id;} alpar@1900: bool operator!=(Row c) const {return id!=c.id;} deba@2312: }; deba@2312: deba@2364: class RowIt : public Row { deba@2366: const LpSolverBase *_lp; deba@2364: public: deba@2364: RowIt() {} deba@2366: RowIt(const LpSolverBase &lp) : _lp(&lp) deba@2364: { deba@2364: _lp->rows.firstFix(id); deba@2364: } deba@2364: RowIt(const Invalid&) : Row(INVALID) {} deba@2364: RowIt &operator++() deba@2364: { deba@2364: _lp->rows.nextFix(id); deba@2364: return *this; deba@2364: } deba@2364: }; deba@2364: deba@2312: static int id(const Row& row) { return row.id; } deba@2312: deba@2312: protected: deba@2312: deba@2386: int _lpId(const Col& c) const { deba@2386: return cols.floatingId(id(c)); deba@2312: } deba@2312: deba@2386: int _lpId(const Row& r) const { deba@2386: return rows.floatingId(id(r)); deba@2312: } deba@2312: deba@2386: Col _item(int i, Col) const { deba@2386: return Col(cols.fixId(i)); deba@2364: } deba@2364: deba@2386: Row _item(int i, Row) const { deba@2386: return Row(rows.fixId(i)); deba@2364: } deba@2364: deba@2312: deba@2312: public: alpar@1259: alpar@1279: ///Linear expression of variables and a constant component alpar@1279: athos@2345: ///This data structure stores a linear expression of the variables alpar@1279: ///(\ref Col "Col"s) and also has a constant component. alpar@1279: /// alpar@1279: ///There are several ways to access and modify the contents of this alpar@1279: ///container. alpar@1279: ///- Its it fully compatible with \c std::map, 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@1908: ///\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; athos@2345: 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); deba@2085: i=j; alpar@1272: } alpar@1272: } alpar@1273: deba@2312: void simplify() const { deba@2312: const_cast(this)->simplify(); deba@2312: } deba@2312: 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: } athos@2345: athos@2345: //std::ostream & athos@2345: void prettyPrint(std::ostream &os) { athos@2345: //std::fmtflags os.flags(); athos@2345: //os.setf(std::ios::showpos); athos@2345: Base::iterator j=Base::begin(); athos@2345: if (j!=Base::end()) athos@2345: os<second<<"*x["<first)<<"]"; athos@2345: ++j; athos@2345: for (; j!=Base::end(); ++j){ athos@2345: if (j->second>=0) athos@2345: os<<"+"; athos@2345: os<second<<"*x["<first)<<"]"; athos@2345: } athos@2345: //Nem valami korrekt, de nem talaltam meg, hogy kell athos@2345: //os.unsetf(std::ios::showpos); athos@2345: athos@2345: //return os; athos@2345: } athos@2345: 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@1908: ///\code alpar@1364: /// e<=s alpar@1364: /// e<=f alpar@1908: /// e==f alpar@1364: /// s<=e<=t alpar@1364: /// e>=t alpar@1908: ///\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@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: } athos@2345: athos@2345: void prettyPrint(std::ostream &os) { athos@2345: if (_lb==-LpSolverBase::INF||isNaN(_lb)) athos@2345: os<<"-infty<="; athos@2345: else athos@2345: os<<_lb<<"<="; athos@2345: _expr.prettyPrint(os); athos@2345: if (_ub==LpSolverBase::INF) athos@2345: os<<"<=infty"; athos@2345: else athos@2345: os<<"<="<<_ub; athos@2345: //return os; athos@2345: } athos@2345: 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@1908: ///\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); deba@2085: i=j; alpar@1445: } alpar@1445: } alpar@1445: deba@2312: void simplify() const { deba@2312: const_cast(this)->simplify(); deba@2312: } deba@2312: 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: deba@2312: private: deba@2312: deba@2364: template deba@2364: class MappedOutputIterator { deba@2312: public: deba@2312: deba@2364: typedef std::insert_iterator<_Expr> Base; deba@2364: deba@2364: typedef std::output_iterator_tag iterator_category; deba@2364: typedef void difference_type; deba@2364: typedef void value_type; deba@2364: typedef void reference; deba@2364: typedef void pointer; deba@2364: deba@2364: MappedOutputIterator(const Base& _base, const LpSolverBase& _lp) deba@2364: : base(_base), lp(_lp) {} deba@2364: deba@2364: MappedOutputIterator& operator*() { deba@2364: return *this; deba@2364: } deba@2364: deba@2364: MappedOutputIterator& operator=(const std::pair& value) { deba@2364: *base = std::make_pair(lp._item(value.first, typename _Expr::Key()), deba@2364: value.second); deba@2364: return *this; deba@2364: } deba@2364: deba@2364: MappedOutputIterator& operator++() { deba@2364: ++base; deba@2364: return *this; deba@2364: } deba@2364: deba@2364: MappedOutputIterator operator++(int) { deba@2364: MappedOutputIterator tmp(*this); deba@2364: ++base; deba@2364: return tmp; deba@2364: } deba@2364: deba@2364: bool operator==(const MappedOutputIterator& it) const { deba@2364: return base == it.base; deba@2364: } deba@2364: deba@2364: bool operator!=(const MappedOutputIterator& it) const { deba@2364: return base != it.base; deba@2364: } deba@2364: deba@2364: private: deba@2364: Base base; deba@2364: const LpSolverBase& lp; deba@2364: }; deba@2364: deba@2364: template deba@2364: class MappedInputIterator { deba@2364: public: deba@2364: deba@2364: typedef typename Expr::const_iterator Base; deba@2312: deba@2312: typedef typename Base::iterator_category iterator_category; deba@2312: typedef typename Base::difference_type difference_type; deba@2312: typedef const std::pair value_type; deba@2312: typedef value_type reference; deba@2312: class pointer { deba@2312: public: deba@2312: pointer(value_type& _value) : value(_value) {} deba@2312: value_type* operator->() { return &value; } deba@2312: private: deba@2312: value_type value; deba@2312: }; deba@2312: deba@2364: MappedInputIterator(const Base& _base, const LpSolverBase& _lp) deba@2312: : base(_base), lp(_lp) {} deba@2312: deba@2312: reference operator*() { deba@2312: return std::make_pair(lp._lpId(base->first), base->second); deba@2312: } deba@2312: deba@2312: pointer operator->() { deba@2312: return pointer(operator*()); deba@2312: } deba@2312: deba@2364: MappedInputIterator& operator++() { deba@2312: ++base; deba@2312: return *this; deba@2312: } deba@2312: deba@2364: MappedInputIterator operator++(int) { deba@2364: MappedInputIterator tmp(*this); deba@2312: ++base; deba@2312: return tmp; deba@2312: } deba@2312: deba@2364: bool operator==(const MappedInputIterator& it) const { deba@2312: return base == it.base; deba@2312: } deba@2312: deba@2364: bool operator!=(const MappedInputIterator& it) const { deba@2312: return base != it.base; deba@2312: } deba@2312: deba@2312: private: deba@2312: Base base; deba@2312: const LpSolverBase& lp; deba@2312: }; deba@2312: alpar@1253: protected: athos@1246: deba@2312: /// STL compatible iterator for lp col deba@2364: typedef MappedInputIterator ConstRowIterator; deba@2312: /// STL compatible iterator for lp row deba@2364: typedef MappedInputIterator ConstColIterator; deba@2364: deba@2364: /// STL compatible iterator for lp col deba@2364: typedef MappedOutputIterator RowIterator; deba@2364: /// STL compatible iterator for lp row deba@2364: typedef MappedOutputIterator ColIterator; deba@2312: alpar@1323: //Abstract virtual functions alpar@1364: virtual LpSolverBase &_newLp() = 0; athos@1436: virtual LpSolverBase &_copyLp(){ deba@2312: ///\todo This should be implemented here, too, when we have deba@2312: ///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; alpar@2303: virtual int _addRow() = 0; deba@2366: athos@1542: virtual void _eraseCol(int col) = 0; athos@1542: virtual void _eraseRow(int row) = 0; deba@2366: deba@2366: virtual void _getColName(int col, std::string & name) const = 0; alpar@1895: virtual void _setColName(int col, const std::string & name) = 0; deba@2366: virtual int _colByName(const std::string& name) const = 0; deba@2366: deba@2364: virtual void _setRowCoeffs(int i, ConstRowIterator b, deba@2364: ConstRowIterator e) = 0; deba@2366: virtual void _getRowCoeffs(int i, RowIterator b) const = 0; deba@2364: virtual void _setColCoeffs(int i, ConstColIterator b, deba@2364: ConstColIterator e) = 0; deba@2366: virtual void _getColCoeffs(int i, ColIterator b) const = 0; athos@1431: virtual void _setCoeff(int row, int col, Value value) = 0; deba@2366: virtual Value _getCoeff(int row, int col) const = 0; alpar@1294: virtual void _setColLowerBound(int i, Value value) = 0; deba@2366: virtual Value _getColLowerBound(int i) const = 0; alpar@1294: virtual void _setColUpperBound(int i, Value value) = 0; deba@2366: virtual Value _getColUpperBound(int i) const = 0; athos@1379: virtual void _setRowBounds(int i, Value lower, Value upper) = 0; deba@2366: virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0; athos@2328: alpar@1294: virtual void _setObjCoeff(int i, Value obj_coef) = 0; deba@2366: virtual Value _getObjCoeff(int i) const = 0; athos@1377: virtual void _clearObj()=0; deba@2312: alpar@1303: virtual SolveExitStatus _solve() = 0; deba@2366: virtual Value _getPrimal(int i) const = 0; deba@2366: virtual Value _getDual(int i) const = 0; deba@2366: virtual Value _getPrimalValue() const = 0; deba@2366: virtual bool _isBasicCol(int i) const = 0; deba@2366: virtual SolutionStatus _getPrimalStatus() const = 0; deba@2366: virtual SolutionStatus _getDualStatus() const = 0; deba@2366: virtual ProblemTypes _getProblemType() const = 0; athos@1460: alpar@1312: virtual void _setMax() = 0; alpar@1312: virtual void _setMin() = 0; alpar@1312: athos@2324: deba@2366: virtual bool _isMax() const = 0; athos@2324: alpar@1323: //Own protected stuff alpar@1323: alpar@1323: //Constant component of the objective function alpar@1323: Value obj_const_comp; deba@2312: 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 deba@2363: Col addCol() { Col c; _addCol(); c.id = cols.addId(); 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@2260: ///- an iterable lemon \ref concepts::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) alpar@1445: ///a better one. alpar@1899: void col(Col c,const DualExpr &e) { deba@2312: e.simplify(); deba@2364: _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this), deba@2364: ConstColIterator(e.end(), *this)); deba@2364: } deba@2364: deba@2364: ///Get a column (i.e a dual constraint) of the LP deba@2364: deba@2364: ///\param r is the column to get deba@2364: ///\return the dual expression associated to the column deba@2366: DualExpr col(Col c) const { deba@2364: DualExpr e; deba@2364: _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this)); deba@2364: return e; 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. deba@2386: Col addCol(const DualExpr &e, Value o = 0) { alpar@1445: Col c=addCol(); alpar@1899: col(c,e); deba@2386: objCoeff(c,o); 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 deba@2363: Row addRow() { Row r; _addRow(); r.id = rows.addId(); 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@2260: ///- an iterable lemon \ref concepts::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) deba@2369: ///\bug This is a temporary 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. deba@2366: void row(Row r, Value l, const Expr &e, Value u) { deba@2312: e.simplify(); deba@2364: _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this), deba@2364: ConstRowIterator(e.end(), *this)); deba@2364: _setRowBounds(_lpId(r),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@1895: void row(Row r, const Constr &c) { deba@2312: row(r, c.lowerBounded()?c.lowerBound():-INF, deba@2312: c.expr(), c.upperBounded()?c.upperBound():INF); alpar@1264: } alpar@1264: deba@2364: deba@2364: ///Get a row (i.e a constraint) of the LP deba@2364: deba@2364: ///\param r is the row to get deba@2364: ///\return the expression associated to the row deba@2366: Expr row(Row r) const { deba@2364: Expr e; deba@2364: _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this)); deba@2364: return e; deba@2364: } deba@2364: 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. deba@2369: ///\bug This is a temporary 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@1895: row(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@1895: row(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) { deba@2312: _eraseCol(_lpId(c)); deba@2363: cols.eraseId(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) { deba@2312: _eraseRow(_lpId(r)); deba@2363: rows.eraseId(r.id); athos@1542: } alpar@1264: alpar@1895: /// Get the name of a column alpar@1895: alpar@1895: ///\param c is the coresponding coloumn alpar@1895: ///\return The name of the colunm deba@2366: std::string colName(Col c) const { alpar@1895: std::string name; deba@2312: _getColName(_lpId(c), name); alpar@1895: return name; alpar@1895: } alpar@1895: alpar@1895: /// Set the name of a column alpar@1895: alpar@1895: ///\param c is the coresponding coloumn alpar@1895: ///\param name The name to be given deba@2366: void colName(Col c, const std::string& name) { deba@2312: _setColName(_lpId(c), name); alpar@1895: } deba@2368: deba@2368: /// Get the column by its name deba@2368: deba@2368: ///\param name The name of the column deba@2368: ///\return the proper column or \c INVALID deba@2368: Col colByName(const std::string& name) const { deba@2368: int k = _colByName(name); deba@2368: return k != -1 ? Col(cols.fixId(k)) : Col(INVALID); deba@2368: } alpar@1895: alpar@1895: /// 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 alpar@1895: deba@2366: void coeff(Row r, Col c, Value val) { deba@2312: _setCoeff(_lpId(r),_lpId(c), val); athos@1436: } athos@1436: athos@2324: /// Get an element of the coefficient matrix of the LP athos@2324: athos@2324: ///\param r is the row of the element in question athos@2324: ///\param c is the coloumn of the element in question athos@2324: ///\return the corresponding coefficient athos@2324: deba@2366: Value coeff(Row r, Col c) const { athos@2324: return _getCoeff(_lpId(r),_lpId(c)); athos@2324: } athos@2324: alpar@1253: /// Set the lower bound of a column (i.e a variable) alpar@1253: alpar@1895: /// The lower 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) { deba@2312: _setColLowerBound(_lpId(c),value); alpar@1253: } athos@2328: athos@2328: /// Get the lower bound of a column (i.e a variable) athos@2328: athos@2328: /// This function returns the lower bound for column (variable) \t c athos@2328: /// (this might be -\ref INF as well). athos@2328: ///\return The lower bound for coloumn \t c deba@2366: Value colLowerBound(Col c) const { athos@2328: return _getColLowerBound(_lpId(c)); athos@2328: } alpar@1895: alpar@1895: ///\brief Set the lower bound of several columns alpar@1895: ///(i.e a variables) at once alpar@1895: /// alpar@1895: ///This magic function takes a container as its argument alpar@1895: ///and applies the function on all of its elements. alpar@1895: /// The lower bound of a variable (column) has to be given by an alpar@1895: /// extended number of type Value, i.e. a finite number of type alpar@1895: /// Value or -\ref INF. alpar@1895: #ifdef DOXYGEN alpar@1895: template alpar@1895: void colLowerBound(T &t, Value value) { return 0;} alpar@1895: #else alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colLowerBound(T &t, Value value,dummy<0> = 0) { alpar@1895: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1895: colLowerBound(*i, value); alpar@1895: } alpar@1895: } alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colLowerBound(T &t, Value value,dummy<1> = 1) { alpar@1895: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1895: colLowerBound(i->second, value); alpar@1895: } alpar@1895: } alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colLowerBound(T &t, Value value,dummy<2> = 2) { alpar@1895: for(typename T::MapIt i(t); i!=INVALID; ++i){ alpar@1895: colLowerBound(*i, value); alpar@1895: } alpar@1895: } alpar@1895: #endif alpar@1895: 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) { deba@2312: _setColUpperBound(_lpId(c),value); alpar@1253: }; alpar@1895: athos@2328: /// Get the upper bound of a column (i.e a variable) athos@2328: athos@2328: /// This function returns the upper bound for column (variable) \t c athos@2328: /// (this might be \ref INF as well). athos@2328: ///\return The upper bound for coloumn \t c deba@2366: Value colUpperBound(Col c) const { athos@2328: return _getColUpperBound(_lpId(c)); athos@2328: } athos@2328: athos@2328: ///\brief Set the upper bound of several columns alpar@1895: ///(i.e a variables) at once alpar@1895: /// alpar@1895: ///This magic function takes a container as its argument alpar@1895: ///and applies the function on all of its elements. alpar@1895: /// The upper bound of a variable (column) has to be given by an alpar@1895: /// extended number of type Value, i.e. a finite number of type alpar@1895: /// Value or \ref INF. alpar@1895: #ifdef DOXYGEN alpar@1895: template alpar@1895: void colUpperBound(T &t, Value value) { return 0;} alpar@1895: #else alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colUpperBound(T &t, Value value,dummy<0> = 0) { alpar@1895: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1895: colUpperBound(*i, value); alpar@1895: } alpar@1895: } alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colUpperBound(T &t, Value value,dummy<1> = 1) { alpar@1895: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1895: colUpperBound(i->second, value); alpar@1895: } alpar@1895: } alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colUpperBound(T &t, Value value,dummy<2> = 2) { alpar@1895: for(typename T::MapIt i(t); i!=INVALID; ++i){ alpar@1895: colUpperBound(*i, value); alpar@1895: } alpar@1895: } alpar@1895: #endif alpar@1895: 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) { deba@2312: _setColLowerBound(_lpId(c),lower); deba@2312: _setColUpperBound(_lpId(c),upper); alpar@1293: } alpar@1293: alpar@1895: ///\brief Set the lower and the upper bound of several columns alpar@1895: ///(i.e a variables) at once alpar@1895: /// alpar@1895: ///This magic function takes a container as its argument alpar@1895: ///and applies the function on all of its elements. alpar@1895: /// The lower and the upper bounds of alpar@1895: /// a variable (column) have to be given by an alpar@1895: /// extended number of type Value, i.e. a finite number of type alpar@1895: /// Value, -\ref INF or \ref INF. alpar@1895: #ifdef DOXYGEN alpar@1895: template alpar@1895: void colBounds(T &t, Value lower, Value upper) { return 0;} alpar@1895: #else alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colBounds(T &t, Value lower, Value upper,dummy<0> = 0) { alpar@1895: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1895: colBounds(*i, lower, upper); alpar@1895: } alpar@1895: } alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colBounds(T &t, Value lower, Value upper,dummy<1> = 1) { alpar@1895: for(typename T::iterator i=t.begin();i!=t.end();++i) { alpar@1895: colBounds(i->second, lower, upper); alpar@1895: } alpar@1895: } alpar@1895: template alpar@1895: typename enable_if::type alpar@1895: colBounds(T &t, Value lower, Value upper,dummy<2> = 2) { alpar@1895: for(typename T::MapIt i(t); i!=INVALID; ++i){ alpar@1895: colBounds(*i, lower, upper); alpar@1895: } alpar@1895: } alpar@1895: #endif alpar@1895: athos@1405: athos@1405: /// Set the lower and the upper bounds of a row (i.e a constraint) alpar@1293: deba@2363: /// The lower and the upper bound of a constraint (row) have to be deba@2363: /// given by an extended number of type Value, i.e. a finite deba@2363: /// number of type Value, -\ref INF or \ref INF. There is no deba@2363: /// separate function for the lower and the upper bound because deba@2363: /// that would have been hard to implement for CPLEX. alpar@1293: void rowBounds(Row c, Value lower, Value upper) { deba@2312: _setRowBounds(_lpId(c),lower, upper); alpar@1293: } alpar@1293: athos@2328: /// Get the lower and the upper bounds of a row (i.e a constraint) athos@2328: athos@2328: /// The lower and the upper bound of athos@2328: /// a constraint (row) are athos@2328: /// extended numbers of type Value, i.e. finite numbers of type athos@2328: /// Value, -\ref INF or \ref INF. athos@2328: /// \todo There is no separate function for the athos@2328: /// lower and the upper bound because we had problems with the athos@2328: /// implementation of the setting functions for CPLEX: athos@2328: /// check out whether this can be done for these functions. deba@2366: void getRowBounds(Row c, Value &lower, Value &upper) const { athos@2328: _getRowBounds(_lpId(c),lower, upper); athos@2328: } athos@2328: alpar@1253: ///Set an element of the objective function deba@2312: void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); }; athos@2324: athos@2324: ///Get an element of the objective function deba@2366: Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); }; athos@2324: alpar@1253: ///Set the objective function athos@2324: alpar@1253: ///\param e is a linear expression of type \ref Expr. deba@2369: void obj(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: deba@2364: ///Get the objective function deba@2364: deba@2364: ///\return the objective function as a linear expression of type \ref Expr. deba@2366: Expr obj() const { deba@2364: Expr e; deba@2364: for (ColIt it(*this); it != INVALID; ++it) { deba@2364: double c = objCoeff(it); deba@2364: if (c != 0.0) { deba@2364: e.insert(std::make_pair(it, c)); deba@2364: } deba@2364: } deba@2364: return e; deba@2364: } deba@2364: deba@2364: alpar@1312: ///Maximize alpar@1312: void max() { _setMax(); } alpar@1312: ///Minimize alpar@1312: void min() { _setMin(); } alpar@1312: athos@2324: ///Query function: is this a maximization problem? deba@2369: bool isMax() const {return _isMax(); } athos@2324: athos@2324: ///Query function: is this a minimization problem? deba@2369: bool isMin() const {return !isMax(); } 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: /// deba@2026: ///\return The result of the optimization procedure. Possible deba@2026: ///values and their meanings can be found in the documentation of deba@2026: ///\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) deba@2366: SolutionStatus primalStatus() const { alpar@1312: return _getPrimalStatus(); alpar@1294: } alpar@1294: athos@1460: /// The status of the dual (of the original LP) problem deba@2366: SolutionStatus dualStatus() const { athos@1460: return _getDualStatus(); athos@1460: } athos@1460: athos@1460: ///The type of the original LP problem deba@2366: ProblemTypes problemType() const { athos@1460: return _getProblemType(); athos@1460: } athos@1460: alpar@1294: ///\e deba@2366: Value primal(Col c) const { return _getPrimal(_lpId(c)); } alpar@1263: alpar@1312: ///\e deba@2366: Value dual(Row r) const { return _getDual(_lpId(r)); } marci@1787: marci@1787: ///\e deba@2366: bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); } 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. deba@2366: Value primalValue() const { return _getPrimalValue()+obj_const_comp;} alpar@1263: ///@} alpar@1253: athos@1248: }; athos@1246: athos@2144: deba@2370: /// \ingroup lp_group deba@2370: /// deba@2370: /// \brief Common base class for MIP solvers deba@2370: /// \todo Much more docs athos@2144: class MipSolverBase : virtual public LpSolverBase{ athos@2144: public: athos@2144: athos@2148: ///Possible variable (coloumn) types (e.g. real, integer, binary etc.) athos@2148: enum ColTypes { athos@2148: ///Continuous variable athos@2148: REAL = 0, athos@2148: ///Integer variable athos@2218: athos@2218: ///Unfortunately, cplex 7.5 somewhere writes something like athos@2218: ///#define INTEGER 'I' athos@2267: INT = 1 athos@2148: ///\todo No support for other types yet. athos@2148: }; athos@2148: athos@2148: ///Sets the type of the given coloumn to the given type athos@2144: /// athos@2148: ///Sets the type of the given coloumn to the given type. athos@2148: void colType(Col c, ColTypes col_type) { deba@2312: _colType(_lpId(c),col_type); athos@2144: } athos@2144: athos@2144: ///Gives back the type of the column. athos@2144: /// athos@2144: ///Gives back the type of the column. deba@2366: ColTypes colType(Col c) const { deba@2312: return _colType(_lpId(c)); athos@2148: } athos@2148: athos@2148: ///Sets the type of the given Col to integer or remove that property. athos@2148: /// athos@2148: ///Sets the type of the given Col to integer or remove that property. athos@2148: void integer(Col c, bool enable) { athos@2148: if (enable) athos@2267: colType(c,INT); athos@2148: else athos@2148: colType(c,REAL); athos@2148: } athos@2148: athos@2148: ///Gives back whether the type of the column is integer or not. athos@2148: /// athos@2148: ///Gives back the type of the column. athos@2144: ///\return true if the column has integer type and false if not. deba@2366: bool integer(Col c) const { athos@2267: return (colType(c)==INT); athos@2144: } athos@2144: athos@2185: /// The status of the MIP problem deba@2366: SolutionStatus mipStatus() const { athos@2185: return _getMipStatus(); athos@2185: } athos@2185: athos@2144: protected: athos@2144: deba@2366: virtual ColTypes _colType(int col) const = 0; athos@2148: virtual void _colType(int col, ColTypes col_type) = 0; deba@2366: virtual SolutionStatus _getMipStatus() const = 0; athos@2148: athos@2144: }; 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 athos@2345: athos@2345: ///\relates LpSolverBase::Constr athos@2345: /// athos@2345: inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, athos@2345: const LpSolverBase::Value &f) athos@2345: { athos@2345: return LpSolverBase::Constr(f,e,f); athos@2345: } athos@2345: athos@2345: ///\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. deba@2026: if(!LpSolverBase::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. deba@2026: if(!LpSolverBase::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. deba@2026: if(!LpSolverBase::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. deba@2026: if(!LpSolverBase::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, deba@2312: 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, deba@2312: 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, deba@2312: 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, deba@2312: 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, deba@2312: 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