deba@458: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@458: * deba@458: * This file is a part of LEMON, a generic C++ optimization library. deba@458: * deba@458: * Copyright (C) 2003-2008 deba@458: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@458: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@458: * deba@458: * Permission to use, modify and distribute this software is granted deba@458: * provided that this copyright notice appears in all copies. For deba@458: * precise terms see the accompanying LICENSE file. deba@458: * deba@458: * This software is provided "AS IS" with no warranty of any kind, deba@458: * express or implied, and with no claim as to its suitability for any deba@458: * purpose. deba@458: * deba@458: */ deba@458: deba@458: #ifndef LEMON_LP_BASE_H deba@458: #define LEMON_LP_BASE_H deba@458: deba@458: #include deba@458: #include deba@458: #include deba@458: #include deba@458: #include deba@458: deba@458: #include deba@458: #include deba@458: deba@458: ///\file deba@458: ///\brief The interface of the LP solver interface. deba@458: ///\ingroup lp_group deba@458: namespace lemon { deba@458: deba@458: /// Function to decide whether a floating point value is finite or not. deba@458: deba@458: /// Retruns true if the argument is not infinity, minus infinity or NaN. deba@458: /// It does the same as the isfinite() function defined by C99. deba@458: template deba@458: bool isFinite(T value) deba@458: { deba@458: typedef std::numeric_limits Lim; deba@458: if ((Lim::has_infinity && (value == Lim::infinity() || value == deba@458: -Lim::infinity())) || deba@458: ((Lim::has_quiet_NaN || Lim::has_signaling_NaN) && value != value)) deba@458: { deba@458: return false; deba@458: } deba@458: return true; deba@458: } deba@458: deba@458: ///Common base class for LP solvers deba@458: deba@458: ///\todo Much more docs deba@458: ///\ingroup lp_group deba@458: class LpSolverBase { deba@458: deba@458: protected: deba@458: deba@458: _lp_bits::LpId rows; deba@458: _lp_bits::LpId cols; deba@458: deba@458: public: deba@458: deba@458: ///Possible outcomes of an LP solving procedure deba@458: enum SolveExitStatus { deba@458: ///This means that the problem has been successfully solved: either deba@458: ///an optimal solution has been found or infeasibility/unboundedness deba@458: ///has been proved. deba@458: SOLVED = 0, deba@458: ///Any other case (including the case when some user specified deba@458: ///limit has been exceeded) deba@458: UNSOLVED = 1 deba@458: }; deba@458: deba@458: ///\e deba@458: enum SolutionStatus { deba@458: ///Feasible solution hasn't been found (but may exist). deba@458: deba@458: ///\todo NOTFOUND might be a better name. deba@458: /// deba@458: UNDEFINED = 0, deba@458: ///The problem has no feasible solution deba@458: INFEASIBLE = 1, deba@458: ///Feasible solution found deba@458: FEASIBLE = 2, deba@458: ///Optimal solution exists and found deba@458: OPTIMAL = 3, deba@458: ///The cost function is unbounded deba@458: deba@458: ///\todo Give a feasible solution and an infinite ray (and the deba@458: ///corresponding bases) deba@458: INFINITE = 4 deba@458: }; deba@458: deba@458: ///\e The type of the investigated LP problem deba@458: enum ProblemTypes { deba@458: ///Primal-dual feasible deba@458: PRIMAL_DUAL_FEASIBLE = 0, deba@458: ///Primal feasible dual infeasible deba@458: PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1, deba@458: ///Primal infeasible dual feasible deba@458: PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2, deba@458: ///Primal-dual infeasible deba@458: PRIMAL_DUAL_INFEASIBLE = 3, deba@458: ///Could not determine so far deba@458: UNKNOWN = 4 deba@458: }; deba@458: deba@458: ///The floating point type used by the solver deba@458: typedef double Value; deba@458: ///The infinity constant deba@458: static const Value INF; deba@458: ///The not a number constant deba@458: static const Value NaN; deba@458: deba@458: static inline bool isNaN(const Value& v) { return v!=v; } deba@458: deba@458: friend class Col; deba@458: friend class ColIt; deba@458: friend class Row; deba@458: deba@458: ///Refer to a column of the LP. deba@458: deba@458: ///This type is used to refer to a column of the LP. deba@458: /// deba@458: ///Its value remains valid and correct even after the addition or erase of deba@458: ///other columns. deba@458: /// deba@458: ///\todo Document what can one do with a Col (INVALID, comparing, deba@458: ///it is similar to Node/Edge) deba@458: class Col { deba@458: protected: deba@458: int id; deba@458: friend class LpSolverBase; deba@458: friend class MipSolverBase; deba@458: explicit Col(int _id) : id(_id) {} deba@458: public: deba@458: typedef Value ExprValue; deba@458: typedef True LpSolverCol; deba@458: Col() {} deba@458: Col(const Invalid&) : id(-1) {} deba@458: bool operator< (Col c) const {return id< c.id;} deba@458: bool operator> (Col c) const {return id> c.id;} deba@458: bool operator==(Col c) const {return id==c.id;} deba@458: bool operator!=(Col c) const {return id!=c.id;} deba@458: }; deba@458: deba@458: class ColIt : public Col { deba@458: const LpSolverBase *_lp; deba@458: public: deba@458: ColIt() {} deba@458: ColIt(const LpSolverBase &lp) : _lp(&lp) deba@458: { deba@458: _lp->cols.firstFix(id); deba@458: } deba@458: ColIt(const Invalid&) : Col(INVALID) {} deba@458: ColIt &operator++() deba@458: { deba@458: _lp->cols.nextFix(id); deba@458: return *this; deba@458: } deba@458: }; deba@458: deba@458: static int id(const Col& col) { return col.id; } deba@458: deba@458: deba@458: ///Refer to a row of the LP. deba@458: deba@458: ///This type is used to refer to a row of the LP. deba@458: /// deba@458: ///Its value remains valid and correct even after the addition or erase of deba@458: ///other rows. deba@458: /// deba@458: ///\todo Document what can one do with a Row (INVALID, comparing, deba@458: ///it is similar to Node/Edge) deba@458: class Row { deba@458: protected: deba@458: int id; deba@458: friend class LpSolverBase; deba@458: explicit Row(int _id) : id(_id) {} deba@458: public: deba@458: typedef Value ExprValue; deba@458: typedef True LpSolverRow; deba@458: Row() {} deba@458: Row(const Invalid&) : id(-1) {} deba@458: deba@458: bool operator< (Row c) const {return id< c.id;} deba@458: bool operator> (Row c) const {return id> c.id;} deba@458: bool operator==(Row c) const {return id==c.id;} deba@458: bool operator!=(Row c) const {return id!=c.id;} deba@458: }; deba@458: deba@458: class RowIt : public Row { deba@458: const LpSolverBase *_lp; deba@458: public: deba@458: RowIt() {} deba@458: RowIt(const LpSolverBase &lp) : _lp(&lp) deba@458: { deba@458: _lp->rows.firstFix(id); deba@458: } deba@458: RowIt(const Invalid&) : Row(INVALID) {} deba@458: RowIt &operator++() deba@458: { deba@458: _lp->rows.nextFix(id); deba@458: return *this; deba@458: } deba@458: }; deba@458: deba@458: static int id(const Row& row) { return row.id; } deba@458: deba@458: protected: deba@458: deba@458: int _lpId(const Col& c) const { deba@458: return cols.floatingId(id(c)); deba@458: } deba@458: deba@458: int _lpId(const Row& r) const { deba@458: return rows.floatingId(id(r)); deba@458: } deba@458: deba@458: Col _item(int i, Col) const { deba@458: return Col(cols.fixId(i)); deba@458: } deba@458: deba@458: Row _item(int i, Row) const { deba@458: return Row(rows.fixId(i)); deba@458: } deba@458: deba@458: deba@458: public: deba@458: deba@458: ///Linear expression of variables and a constant component deba@458: deba@458: ///This data structure stores a linear expression of the variables deba@458: ///(\ref Col "Col"s) and also has a constant component. deba@458: /// deba@458: ///There are several ways to access and modify the contents of this deba@458: ///container. deba@458: ///- Its it fully compatible with \c std::map, so for expamle deba@458: ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can deba@458: ///read and modify the coefficients like deba@458: ///these. deba@458: ///\code deba@458: ///e[v]=5; deba@458: ///e[v]+=12; deba@458: ///e.erase(v); deba@458: ///\endcode deba@458: ///or you can also iterate through its elements. deba@458: ///\code deba@458: ///double s=0; deba@458: ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) deba@458: /// s+=i->second; deba@458: ///\endcode deba@458: ///(This code computes the sum of all coefficients). deba@458: ///- Numbers (double's) deba@458: ///and variables (\ref Col "Col"s) directly convert to an deba@458: ///\ref Expr and the usual linear operations are defined, so deba@458: ///\code deba@458: ///v+w deba@458: ///2*v-3.12*(v-w/2)+2 deba@458: ///v*2.1+(3*v+(v*12+w+6)*3)/2 deba@458: ///\endcode deba@458: ///are valid \ref Expr "Expr"essions. deba@458: ///The usual assignment operations are also defined. deba@458: ///\code deba@458: ///e=v+w; deba@458: ///e+=2*v-3.12*(v-w/2)+2; deba@458: ///e*=3.4; deba@458: ///e/=5; deba@458: ///\endcode deba@458: ///- The constant member can be set and read by \ref constComp() deba@458: ///\code deba@458: ///e.constComp()=12; deba@458: ///double c=e.constComp(); deba@458: ///\endcode deba@458: /// deba@458: ///\note \ref clear() not only sets all coefficients to 0 but also deba@458: ///clears the constant components. deba@458: /// deba@458: ///\sa Constr deba@458: /// deba@458: class Expr : public std::map deba@458: { deba@458: public: deba@458: typedef LpSolverBase::Col Key; deba@458: typedef LpSolverBase::Value Value; deba@458: deba@458: protected: deba@458: typedef std::map Base; deba@458: deba@458: Value const_comp; deba@458: public: deba@458: typedef True IsLinExpression; deba@458: ///\e deba@458: Expr() : Base(), const_comp(0) { } deba@458: ///\e deba@458: Expr(const Key &v) : const_comp(0) { deba@458: Base::insert(std::make_pair(v, 1)); deba@458: } deba@458: ///\e deba@458: Expr(const Value &v) : const_comp(v) {} deba@458: ///\e deba@458: void set(const Key &v,const Value &c) { deba@458: Base::insert(std::make_pair(v, c)); deba@458: } deba@458: ///\e deba@458: Value &constComp() { return const_comp; } deba@458: ///\e deba@458: const Value &constComp() const { return const_comp; } deba@458: deba@458: ///Removes the components with zero coefficient. deba@458: void simplify() { deba@458: for (Base::iterator i=Base::begin(); i!=Base::end();) { deba@458: Base::iterator j=i; deba@458: ++j; deba@458: if ((*i).second==0) Base::erase(i); deba@458: i=j; deba@458: } deba@458: } deba@458: deba@458: void simplify() const { deba@458: const_cast(this)->simplify(); deba@458: } deba@458: deba@458: ///Removes the coefficients closer to zero than \c tolerance. deba@458: void simplify(double &tolerance) { deba@458: for (Base::iterator i=Base::begin(); i!=Base::end();) { deba@458: Base::iterator j=i; deba@458: ++j; deba@458: if (std::fabs((*i).second)first]+=j->second; deba@458: const_comp+=e.const_comp; deba@458: return *this; deba@458: } deba@458: ///\e deba@458: Expr &operator-=(const Expr &e) { deba@458: for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) deba@458: (*this)[j->first]-=j->second; deba@458: const_comp-=e.const_comp; deba@458: return *this; deba@458: } deba@458: ///\e deba@458: Expr &operator*=(const Value &c) { deba@458: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) deba@458: j->second*=c; deba@458: const_comp*=c; deba@458: return *this; deba@458: } deba@458: ///\e deba@458: Expr &operator/=(const Value &c) { deba@458: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) deba@458: j->second/=c; deba@458: const_comp/=c; deba@458: return *this; deba@458: } deba@458: deba@458: }; deba@458: deba@458: ///Linear constraint deba@458: deba@458: ///This data stucture represents a linear constraint in the LP. deba@458: ///Basically it is a linear expression with a lower or an upper bound deba@458: ///(or both). These parts of the constraint can be obtained by the member deba@458: ///functions \ref expr(), \ref lowerBound() and \ref upperBound(), deba@458: ///respectively. deba@458: ///There are two ways to construct a constraint. deba@458: ///- You can set the linear expression and the bounds directly deba@458: /// by the functions above. deba@458: ///- The operators \<=, == and \>= deba@458: /// are defined between expressions, or even between constraints whenever deba@458: /// it makes sense. Therefore if \c e and \c f are linear expressions and deba@458: /// \c s and \c t are numbers, then the followings are valid expressions deba@458: /// and thus they can be used directly e.g. in \ref addRow() whenever deba@458: /// it makes sense. deba@458: ///\code deba@458: /// e<=s deba@458: /// e<=f deba@458: /// e==f deba@458: /// s<=e<=t deba@458: /// e>=t deba@458: ///\endcode deba@458: ///\warning The validity of a constraint is checked only at run time, so deba@458: ///e.g. \ref addRow(x[1]\<=x[2]<=5) will compile, but will throw deba@458: ///an assertion. deba@458: class Constr deba@458: { deba@458: public: deba@458: typedef LpSolverBase::Expr Expr; deba@458: typedef Expr::Key Key; deba@458: typedef Expr::Value Value; deba@458: deba@458: protected: deba@458: Expr _expr; deba@458: Value _lb,_ub; deba@458: public: deba@458: ///\e deba@458: Constr() : _expr(), _lb(NaN), _ub(NaN) {} deba@458: ///\e deba@458: Constr(Value lb,const Expr &e,Value ub) : deba@458: _expr(e), _lb(lb), _ub(ub) {} deba@458: ///\e deba@458: Constr(const Expr &e,Value ub) : deba@458: _expr(e), _lb(NaN), _ub(ub) {} deba@458: ///\e deba@458: Constr(Value lb,const Expr &e) : deba@458: _expr(e), _lb(lb), _ub(NaN) {} deba@458: ///\e deba@458: Constr(const Expr &e) : deba@458: _expr(e), _lb(NaN), _ub(NaN) {} deba@458: ///\e deba@458: void clear() deba@458: { deba@458: _expr.clear(); deba@458: _lb=_ub=NaN; deba@458: } deba@458: deba@458: ///Reference to the linear expression deba@458: Expr &expr() { return _expr; } deba@458: ///Cont reference to the linear expression deba@458: const Expr &expr() const { return _expr; } deba@458: ///Reference to the lower bound. deba@458: deba@458: ///\return deba@458: ///- \ref INF "INF": the constraint is lower unbounded. deba@458: ///- \ref NaN "NaN": lower bound has not been set. deba@458: ///- finite number: the lower bound deba@458: Value &lowerBound() { return _lb; } deba@458: ///The const version of \ref lowerBound() deba@458: const Value &lowerBound() const { return _lb; } deba@458: ///Reference to the upper bound. deba@458: deba@458: ///\return deba@458: ///- \ref INF "INF": the constraint is upper unbounded. deba@458: ///- \ref NaN "NaN": upper bound has not been set. deba@458: ///- finite number: the upper bound deba@458: Value &upperBound() { return _ub; } deba@458: ///The const version of \ref upperBound() deba@458: const Value &upperBound() const { return _ub; } deba@458: ///Is the constraint lower bounded? deba@458: bool lowerBounded() const { deba@458: return isFinite(_lb); deba@458: } deba@458: ///Is the constraint upper bounded? deba@458: bool upperBounded() const { deba@458: return isFinite(_ub); deba@458: } deba@458: deba@458: }; deba@458: deba@458: ///Linear expression of rows deba@458: deba@458: ///This data structure represents a column of the matrix, deba@458: ///thas is it strores a linear expression of the dual variables deba@458: ///(\ref Row "Row"s). deba@458: /// deba@458: ///There are several ways to access and modify the contents of this deba@458: ///container. deba@458: ///- Its it fully compatible with \c std::map, so for expamle deba@458: ///if \c e is an DualExpr and \c v deba@458: ///and \c w are of type \ref Row, then you can deba@458: ///read and modify the coefficients like deba@458: ///these. deba@458: ///\code deba@458: ///e[v]=5; deba@458: ///e[v]+=12; deba@458: ///e.erase(v); deba@458: ///\endcode deba@458: ///or you can also iterate through its elements. deba@458: ///\code deba@458: ///double s=0; deba@458: ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i) deba@458: /// s+=i->second; deba@458: ///\endcode deba@458: ///(This code computes the sum of all coefficients). deba@458: ///- Numbers (double's) deba@458: ///and variables (\ref Row "Row"s) directly convert to an deba@458: ///\ref DualExpr and the usual linear operations are defined, so deba@458: ///\code deba@458: ///v+w deba@458: ///2*v-3.12*(v-w/2) deba@458: ///v*2.1+(3*v+(v*12+w)*3)/2 deba@458: ///\endcode deba@458: ///are valid \ref DualExpr "DualExpr"essions. deba@458: ///The usual assignment operations are also defined. deba@458: ///\code deba@458: ///e=v+w; deba@458: ///e+=2*v-3.12*(v-w/2); deba@458: ///e*=3.4; deba@458: ///e/=5; deba@458: ///\endcode deba@458: /// deba@458: ///\sa Expr deba@458: /// deba@458: class DualExpr : public std::map deba@458: { deba@458: public: deba@458: typedef LpSolverBase::Row Key; deba@458: typedef LpSolverBase::Value Value; deba@458: deba@458: protected: deba@458: typedef std::map Base; deba@458: deba@458: public: deba@458: typedef True IsLinExpression; deba@458: ///\e deba@458: DualExpr() : Base() { } deba@458: ///\e deba@458: DualExpr(const Key &v) { deba@458: Base::insert(std::make_pair(v, 1)); deba@458: } deba@458: ///\e deba@458: void set(const Key &v,const Value &c) { deba@458: Base::insert(std::make_pair(v, c)); deba@458: } deba@458: deba@458: ///Removes the components with zero coefficient. deba@458: void simplify() { deba@458: for (Base::iterator i=Base::begin(); i!=Base::end();) { deba@458: Base::iterator j=i; deba@458: ++j; deba@458: if ((*i).second==0) Base::erase(i); deba@458: i=j; deba@458: } deba@458: } deba@458: deba@458: void simplify() const { deba@458: const_cast(this)->simplify(); deba@458: } deba@458: deba@458: ///Removes the coefficients closer to zero than \c tolerance. deba@458: void simplify(double &tolerance) { deba@458: for (Base::iterator i=Base::begin(); i!=Base::end();) { deba@458: Base::iterator j=i; deba@458: ++j; deba@458: if (std::fabs((*i).second)first]+=j->second; deba@458: return *this; deba@458: } deba@458: ///\e deba@458: DualExpr &operator-=(const DualExpr &e) { deba@458: for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) deba@458: (*this)[j->first]-=j->second; deba@458: return *this; deba@458: } deba@458: ///\e deba@458: DualExpr &operator*=(const Value &c) { deba@458: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) deba@458: j->second*=c; deba@458: return *this; deba@458: } deba@458: ///\e deba@458: DualExpr &operator/=(const Value &c) { deba@458: for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) deba@458: j->second/=c; deba@458: return *this; deba@458: } deba@458: }; deba@458: deba@458: deba@458: private: deba@458: deba@458: template deba@458: class MappedOutputIterator { deba@458: public: deba@458: deba@458: typedef std::insert_iterator<_Expr> Base; deba@458: deba@458: typedef std::output_iterator_tag iterator_category; deba@458: typedef void difference_type; deba@458: typedef void value_type; deba@458: typedef void reference; deba@458: typedef void pointer; deba@458: deba@458: MappedOutputIterator(const Base& _base, const LpSolverBase& _lp) deba@458: : base(_base), lp(_lp) {} deba@458: deba@458: MappedOutputIterator& operator*() { deba@458: return *this; deba@458: } deba@458: deba@458: MappedOutputIterator& operator=(const std::pair& value) { deba@458: *base = std::make_pair(lp._item(value.first, typename _Expr::Key()), deba@458: value.second); deba@458: return *this; deba@458: } deba@458: deba@458: MappedOutputIterator& operator++() { deba@458: ++base; deba@458: return *this; deba@458: } deba@458: deba@458: MappedOutputIterator operator++(int) { deba@458: MappedOutputIterator tmp(*this); deba@458: ++base; deba@458: return tmp; deba@458: } deba@458: deba@458: bool operator==(const MappedOutputIterator& it) const { deba@458: return base == it.base; deba@458: } deba@458: deba@458: bool operator!=(const MappedOutputIterator& it) const { deba@458: return base != it.base; deba@458: } deba@458: deba@458: private: deba@458: Base base; deba@458: const LpSolverBase& lp; deba@458: }; deba@458: deba@458: template deba@458: class MappedInputIterator { deba@458: public: deba@458: deba@458: typedef typename Expr::const_iterator Base; deba@458: deba@458: typedef typename Base::iterator_category iterator_category; deba@458: typedef typename Base::difference_type difference_type; deba@458: typedef const std::pair value_type; deba@458: typedef value_type reference; deba@458: class pointer { deba@458: public: deba@458: pointer(value_type& _value) : value(_value) {} deba@458: value_type* operator->() { return &value; } deba@458: private: deba@458: value_type value; deba@458: }; deba@458: deba@458: MappedInputIterator(const Base& _base, const LpSolverBase& _lp) deba@458: : base(_base), lp(_lp) {} deba@458: deba@458: reference operator*() { deba@458: return std::make_pair(lp._lpId(base->first), base->second); deba@458: } deba@458: deba@458: pointer operator->() { deba@458: return pointer(operator*()); deba@458: } deba@458: deba@458: MappedInputIterator& operator++() { deba@458: ++base; deba@458: return *this; deba@458: } deba@458: deba@458: MappedInputIterator operator++(int) { deba@458: MappedInputIterator tmp(*this); deba@458: ++base; deba@458: return tmp; deba@458: } deba@458: deba@458: bool operator==(const MappedInputIterator& it) const { deba@458: return base == it.base; deba@458: } deba@458: deba@458: bool operator!=(const MappedInputIterator& it) const { deba@458: return base != it.base; deba@458: } deba@458: deba@458: private: deba@458: Base base; deba@458: const LpSolverBase& lp; deba@458: }; deba@458: deba@458: protected: deba@458: deba@458: /// STL compatible iterator for lp col deba@458: typedef MappedInputIterator ConstRowIterator; deba@458: /// STL compatible iterator for lp row deba@458: typedef MappedInputIterator ConstColIterator; deba@458: deba@458: /// STL compatible iterator for lp col deba@458: typedef MappedOutputIterator RowIterator; deba@458: /// STL compatible iterator for lp row deba@458: typedef MappedOutputIterator ColIterator; deba@458: deba@458: //Abstract virtual functions deba@458: virtual LpSolverBase* _newLp() = 0; deba@458: virtual LpSolverBase* _copyLp(){ deba@458: LpSolverBase* newlp = _newLp(); deba@458: deba@458: std::map ref; deba@458: for (LpSolverBase::ColIt it(*this); it != INVALID; ++it) { deba@458: Col ccol = newlp->addCol(); deba@458: ref[it] = ccol; deba@458: newlp->colName(ccol, colName(it)); deba@458: newlp->colLowerBound(ccol, colLowerBound(it)); deba@458: newlp->colUpperBound(ccol, colUpperBound(it)); deba@458: } deba@458: deba@458: for (LpSolverBase::RowIt it(*this); it != INVALID; ++it) { deba@458: Expr e = row(it), ce; deba@458: for (Expr::iterator jt = e.begin(); jt != e.end(); ++jt) { deba@458: ce[ref[jt->first]] = jt->second; deba@458: } deba@458: ce += e.constComp(); deba@458: Row r = newlp->addRow(ce); deba@458: deba@458: double lower, upper; deba@458: getRowBounds(it, lower, upper); deba@458: newlp->rowBounds(r, lower, upper); deba@458: } deba@458: deba@458: return newlp; deba@458: }; deba@458: deba@458: virtual int _addCol() = 0; deba@458: virtual int _addRow() = 0; deba@458: deba@458: virtual void _eraseCol(int col) = 0; deba@458: virtual void _eraseRow(int row) = 0; deba@458: deba@458: virtual void _getColName(int col, std::string & name) const = 0; deba@458: virtual void _setColName(int col, const std::string & name) = 0; deba@458: virtual int _colByName(const std::string& name) const = 0; deba@458: deba@458: virtual void _setRowCoeffs(int i, ConstRowIterator b, deba@458: ConstRowIterator e) = 0; deba@458: virtual void _getRowCoeffs(int i, RowIterator b) const = 0; deba@458: virtual void _setColCoeffs(int i, ConstColIterator b, deba@458: ConstColIterator e) = 0; deba@458: virtual void _getColCoeffs(int i, ColIterator b) const = 0; deba@458: virtual void _setCoeff(int row, int col, Value value) = 0; deba@458: virtual Value _getCoeff(int row, int col) const = 0; deba@458: virtual void _setColLowerBound(int i, Value value) = 0; deba@458: virtual Value _getColLowerBound(int i) const = 0; deba@458: virtual void _setColUpperBound(int i, Value value) = 0; deba@458: virtual Value _getColUpperBound(int i) const = 0; deba@458: virtual void _setRowBounds(int i, Value lower, Value upper) = 0; deba@458: virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0; deba@458: deba@458: virtual void _setObjCoeff(int i, Value obj_coef) = 0; deba@458: virtual Value _getObjCoeff(int i) const = 0; deba@458: virtual void _clearObj()=0; deba@458: deba@458: virtual SolveExitStatus _solve() = 0; deba@458: virtual Value _getPrimal(int i) const = 0; deba@458: virtual Value _getDual(int i) const = 0; deba@458: virtual Value _getPrimalValue() const = 0; deba@458: virtual bool _isBasicCol(int i) const = 0; deba@458: virtual SolutionStatus _getPrimalStatus() const = 0; deba@458: virtual SolutionStatus _getDualStatus() const = 0; deba@458: virtual ProblemTypes _getProblemType() const = 0; deba@458: deba@458: virtual void _setMax() = 0; deba@458: virtual void _setMin() = 0; deba@458: deba@458: deba@458: virtual bool _isMax() const = 0; deba@458: deba@458: //Own protected stuff deba@458: deba@458: //Constant component of the objective function deba@458: Value obj_const_comp; deba@458: deba@458: public: deba@458: deba@458: ///\e deba@458: LpSolverBase() : obj_const_comp(0) {} deba@458: deba@458: ///\e deba@458: virtual ~LpSolverBase() {} deba@458: deba@458: ///Creates a new LP problem deba@458: LpSolverBase* newLp() {return _newLp();} deba@458: ///Makes a copy of the LP problem deba@458: LpSolverBase* copyLp() {return _copyLp();} deba@458: deba@458: ///\name Build up and modify the LP deba@458: deba@458: ///@{ deba@458: deba@458: ///Add a new empty column (i.e a new variable) to the LP deba@458: Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;} deba@458: deba@458: ///\brief Adds several new columns deba@458: ///(i.e a variables) at once deba@458: /// deba@458: ///This magic function takes a container as its argument deba@458: ///and fills its elements deba@458: ///with new columns (i.e. variables) deba@458: ///\param t can be deba@458: ///- a standard STL compatible iterable container with deba@458: ///\ref Col as its \c values_type deba@458: ///like deba@458: ///\code deba@458: ///std::vector deba@458: ///std::list deba@458: ///\endcode deba@458: ///- a standard STL compatible iterable container with deba@458: ///\ref Col as its \c mapped_type deba@458: ///like deba@458: ///\code deba@458: ///std::map deba@458: ///\endcode deba@458: ///- an iterable lemon \ref concepts::WriteMap "write map" like deba@458: ///\code deba@458: ///ListGraph::NodeMap deba@458: ///ListGraph::EdgeMap deba@458: ///\endcode deba@458: ///\return The number of the created column. deba@458: #ifdef DOXYGEN deba@458: template deba@458: int addColSet(T &t) { return 0;} deba@458: #else deba@458: template deba@458: typename enable_if::type deba@458: addColSet(T &t,dummy<0> = 0) { deba@458: int s=0; deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} deba@458: return s; deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: addColSet(T &t,dummy<1> = 1) { deba@458: int s=0; deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: i->second=addCol(); deba@458: s++; deba@458: } deba@458: return s; deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: addColSet(T &t,dummy<2> = 2) { deba@458: int s=0; deba@458: for(typename T::MapIt i(t); i!=INVALID; ++i) deba@458: { deba@458: i.set(addCol()); deba@458: s++; deba@458: } deba@458: return s; deba@458: } deba@458: #endif deba@458: deba@458: ///Set a column (i.e a dual constraint) of the LP deba@458: deba@458: ///\param c is the column to be modified deba@458: ///\param e is a dual linear expression (see \ref DualExpr) deba@458: ///a better one. deba@458: void col(Col c,const DualExpr &e) { deba@458: e.simplify(); deba@458: _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this), deba@458: ConstColIterator(e.end(), *this)); deba@458: } deba@458: deba@458: ///Get a column (i.e a dual constraint) of the LP deba@458: deba@458: ///\param r is the column to get deba@458: ///\return the dual expression associated to the column deba@458: DualExpr col(Col c) const { deba@458: DualExpr e; deba@458: _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this)); deba@458: return e; deba@458: } deba@458: deba@458: ///Add a new column to the LP deba@458: deba@458: ///\param e is a dual linear expression (see \ref DualExpr) deba@458: ///\param obj is the corresponding component of the objective deba@458: ///function. It is 0 by default. deba@458: ///\return The created column. deba@458: Col addCol(const DualExpr &e, Value o = 0) { deba@458: Col c=addCol(); deba@458: col(c,e); deba@458: objCoeff(c,o); deba@458: return c; deba@458: } deba@458: deba@458: ///Add a new empty row (i.e a new constraint) to the LP deba@458: deba@458: ///This function adds a new empty row (i.e a new constraint) to the LP. deba@458: ///\return The created row deba@458: Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;} deba@458: deba@458: ///\brief Add several new rows deba@458: ///(i.e a constraints) at once deba@458: /// deba@458: ///This magic function takes a container as its argument deba@458: ///and fills its elements deba@458: ///with new row (i.e. variables) deba@458: ///\param t can be deba@458: ///- a standard STL compatible iterable container with deba@458: ///\ref Row as its \c values_type deba@458: ///like deba@458: ///\code deba@458: ///std::vector deba@458: ///std::list deba@458: ///\endcode deba@458: ///- a standard STL compatible iterable container with deba@458: ///\ref Row as its \c mapped_type deba@458: ///like deba@458: ///\code deba@458: ///std::map deba@458: ///\endcode deba@458: ///- an iterable lemon \ref concepts::WriteMap "write map" like deba@458: ///\code deba@458: ///ListGraph::NodeMap deba@458: ///ListGraph::EdgeMap deba@458: ///\endcode deba@458: ///\return The number of rows created. deba@458: #ifdef DOXYGEN deba@458: template deba@458: int addRowSet(T &t) { return 0;} deba@458: #else deba@458: template deba@458: typename enable_if::type deba@458: addRowSet(T &t,dummy<0> = 0) { deba@458: int s=0; deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;} deba@458: return s; deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: addRowSet(T &t,dummy<1> = 1) { deba@458: int s=0; deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: i->second=addRow(); deba@458: s++; deba@458: } deba@458: return s; deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: addRowSet(T &t,dummy<2> = 2) { deba@458: int s=0; deba@458: for(typename T::MapIt i(t); i!=INVALID; ++i) deba@458: { deba@458: i.set(addRow()); deba@458: s++; deba@458: } deba@458: return s; deba@458: } deba@458: #endif deba@458: deba@458: ///Set a row (i.e a constraint) of the LP deba@458: deba@458: ///\param r is the row to be modified deba@458: ///\param l is lower bound (-\ref INF means no bound) deba@458: ///\param e is a linear expression (see \ref Expr) deba@458: ///\param u is the upper bound (\ref INF means no bound) deba@458: ///\bug This is a temporary function. The interface will change to deba@458: ///a better one. deba@458: ///\todo Option to control whether a constraint with a single variable is deba@458: ///added or not. deba@458: void row(Row r, Value l, const Expr &e, Value u) { deba@458: e.simplify(); deba@458: _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this), deba@458: ConstRowIterator(e.end(), *this)); deba@458: _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp()); deba@458: } deba@458: deba@458: ///Set a row (i.e a constraint) of the LP deba@458: deba@458: ///\param r is the row to be modified deba@458: ///\param c is a linear expression (see \ref Constr) deba@458: void row(Row r, const Constr &c) { deba@458: row(r, c.lowerBounded()?c.lowerBound():-INF, deba@458: c.expr(), c.upperBounded()?c.upperBound():INF); deba@458: } deba@458: deba@458: deba@458: ///Get a row (i.e a constraint) of the LP deba@458: deba@458: ///\param r is the row to get deba@458: ///\return the expression associated to the row deba@458: Expr row(Row r) const { deba@458: Expr e; deba@458: _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this)); deba@458: return e; deba@458: } deba@458: deba@458: ///Add a new row (i.e a new constraint) to the LP deba@458: deba@458: ///\param l is the lower bound (-\ref INF means no bound) deba@458: ///\param e is a linear expression (see \ref Expr) deba@458: ///\param u is the upper bound (\ref INF means no bound) deba@458: ///\return The created row. deba@458: ///\bug This is a temporary function. The interface will change to deba@458: ///a better one. deba@458: Row addRow(Value l,const Expr &e, Value u) { deba@458: Row r=addRow(); deba@458: row(r,l,e,u); deba@458: return r; deba@458: } deba@458: deba@458: ///Add a new row (i.e a new constraint) to the LP deba@458: deba@458: ///\param c is a linear expression (see \ref Constr) deba@458: ///\return The created row. deba@458: Row addRow(const Constr &c) { deba@458: Row r=addRow(); deba@458: row(r,c); deba@458: return r; deba@458: } deba@458: ///Erase a coloumn (i.e a variable) from the LP deba@458: deba@458: ///\param c is the coloumn to be deleted deba@458: ///\todo Please check this deba@458: void eraseCol(Col c) { deba@458: _eraseCol(_lpId(c)); deba@458: cols.eraseId(c.id); deba@458: } deba@458: ///Erase a row (i.e a constraint) from the LP deba@458: deba@458: ///\param r is the row to be deleted deba@458: ///\todo Please check this deba@458: void eraseRow(Row r) { deba@458: _eraseRow(_lpId(r)); deba@458: rows.eraseId(r.id); deba@458: } deba@458: deba@458: /// Get the name of a column deba@458: deba@458: ///\param c is the coresponding coloumn deba@458: ///\return The name of the colunm deba@458: std::string colName(Col c) const { deba@458: std::string name; deba@458: _getColName(_lpId(c), name); deba@458: return name; deba@458: } deba@458: deba@458: /// Set the name of a column deba@458: deba@458: ///\param c is the coresponding coloumn deba@458: ///\param name The name to be given deba@458: void colName(Col c, const std::string& name) { deba@458: _setColName(_lpId(c), name); deba@458: } deba@458: deba@458: /// Get the column by its name deba@458: deba@458: ///\param name The name of the column deba@458: ///\return the proper column or \c INVALID deba@458: Col colByName(const std::string& name) const { deba@458: int k = _colByName(name); deba@458: return k != -1 ? Col(cols.fixId(k)) : Col(INVALID); deba@458: } deba@458: deba@458: /// Set an element of the coefficient matrix of the LP deba@458: deba@458: ///\param r is the row of the element to be modified deba@458: ///\param c is the coloumn of the element to be modified deba@458: ///\param val is the new value of the coefficient deba@458: deba@458: void coeff(Row r, Col c, Value val) { deba@458: _setCoeff(_lpId(r),_lpId(c), val); deba@458: } deba@458: deba@458: /// Get an element of the coefficient matrix of the LP deba@458: deba@458: ///\param r is the row of the element in question deba@458: ///\param c is the coloumn of the element in question deba@458: ///\return the corresponding coefficient deba@458: deba@458: Value coeff(Row r, Col c) const { deba@458: return _getCoeff(_lpId(r),_lpId(c)); deba@458: } deba@458: deba@458: /// Set the lower bound of a column (i.e a variable) deba@458: deba@458: /// The lower bound of a variable (column) has to be given by an deba@458: /// extended number of type Value, i.e. a finite number of type deba@458: /// Value or -\ref INF. deba@458: void colLowerBound(Col c, Value value) { deba@458: _setColLowerBound(_lpId(c),value); deba@458: } deba@458: deba@458: /// Get the lower bound of a column (i.e a variable) deba@458: deba@458: /// This function returns the lower bound for column (variable) \t c deba@458: /// (this might be -\ref INF as well). deba@458: ///\return The lower bound for coloumn \t c deba@458: Value colLowerBound(Col c) const { deba@458: return _getColLowerBound(_lpId(c)); deba@458: } deba@458: deba@458: ///\brief Set the lower bound of several columns deba@458: ///(i.e a variables) at once deba@458: /// deba@458: ///This magic function takes a container as its argument deba@458: ///and applies the function on all of its elements. deba@458: /// The lower bound of a variable (column) has to be given by an deba@458: /// extended number of type Value, i.e. a finite number of type deba@458: /// Value or -\ref INF. deba@458: #ifdef DOXYGEN deba@458: template deba@458: void colLowerBound(T &t, Value value) { return 0;} deba@458: #else deba@458: template deba@458: typename enable_if::type deba@458: colLowerBound(T &t, Value value,dummy<0> = 0) { deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: colLowerBound(*i, value); deba@458: } deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: colLowerBound(T &t, Value value,dummy<1> = 1) { deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: colLowerBound(i->second, value); deba@458: } deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: colLowerBound(T &t, Value value,dummy<2> = 2) { deba@458: for(typename T::MapIt i(t); i!=INVALID; ++i){ deba@458: colLowerBound(*i, value); deba@458: } deba@458: } deba@458: #endif deba@458: deba@458: /// Set the upper bound of a column (i.e a variable) deba@458: deba@458: /// The upper bound of a variable (column) has to be given by an deba@458: /// extended number of type Value, i.e. a finite number of type deba@458: /// Value or \ref INF. deba@458: void colUpperBound(Col c, Value value) { deba@458: _setColUpperBound(_lpId(c),value); deba@458: }; deba@458: deba@458: /// Get the upper bound of a column (i.e a variable) deba@458: deba@458: /// This function returns the upper bound for column (variable) \t c deba@458: /// (this might be \ref INF as well). deba@458: ///\return The upper bound for coloumn \t c deba@458: Value colUpperBound(Col c) const { deba@458: return _getColUpperBound(_lpId(c)); deba@458: } deba@458: deba@458: ///\brief Set the upper bound of several columns deba@458: ///(i.e a variables) at once deba@458: /// deba@458: ///This magic function takes a container as its argument deba@458: ///and applies the function on all of its elements. deba@458: /// The upper bound of a variable (column) has to be given by an deba@458: /// extended number of type Value, i.e. a finite number of type deba@458: /// Value or \ref INF. deba@458: #ifdef DOXYGEN deba@458: template deba@458: void colUpperBound(T &t, Value value) { return 0;} deba@458: #else deba@458: template deba@458: typename enable_if::type deba@458: colUpperBound(T &t, Value value,dummy<0> = 0) { deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: colUpperBound(*i, value); deba@458: } deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: colUpperBound(T &t, Value value,dummy<1> = 1) { deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: colUpperBound(i->second, value); deba@458: } deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: colUpperBound(T &t, Value value,dummy<2> = 2) { deba@458: for(typename T::MapIt i(t); i!=INVALID; ++i){ deba@458: colUpperBound(*i, value); deba@458: } deba@458: } deba@458: #endif deba@458: deba@458: /// Set the lower and the upper bounds of a column (i.e a variable) deba@458: deba@458: /// The lower and the upper bounds of deba@458: /// a variable (column) have to be given by an deba@458: /// extended number of type Value, i.e. a finite number of type deba@458: /// Value, -\ref INF or \ref INF. deba@458: void colBounds(Col c, Value lower, Value upper) { deba@458: _setColLowerBound(_lpId(c),lower); deba@458: _setColUpperBound(_lpId(c),upper); deba@458: } deba@458: deba@458: ///\brief Set the lower and the upper bound of several columns deba@458: ///(i.e a variables) at once deba@458: /// deba@458: ///This magic function takes a container as its argument deba@458: ///and applies the function on all of its elements. deba@458: /// The lower and the upper bounds of deba@458: /// a variable (column) have to be given by an deba@458: /// extended number of type Value, i.e. a finite number of type deba@458: /// Value, -\ref INF or \ref INF. deba@458: #ifdef DOXYGEN deba@458: template deba@458: void colBounds(T &t, Value lower, Value upper) { return 0;} deba@458: #else deba@458: template deba@458: typename enable_if::type deba@458: colBounds(T &t, Value lower, Value upper,dummy<0> = 0) { deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: colBounds(*i, lower, upper); deba@458: } deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: colBounds(T &t, Value lower, Value upper,dummy<1> = 1) { deba@458: for(typename T::iterator i=t.begin();i!=t.end();++i) { deba@458: colBounds(i->second, lower, upper); deba@458: } deba@458: } deba@458: template deba@458: typename enable_if::type deba@458: colBounds(T &t, Value lower, Value upper,dummy<2> = 2) { deba@458: for(typename T::MapIt i(t); i!=INVALID; ++i){ deba@458: colBounds(*i, lower, upper); deba@458: } deba@458: } deba@458: #endif deba@458: deba@458: deba@458: /// Set the lower and the upper bounds of a row (i.e a constraint) deba@458: deba@458: /// The lower and the upper bound of a constraint (row) have to be deba@458: /// given by an extended number of type Value, i.e. a finite deba@458: /// number of type Value, -\ref INF or \ref INF. There is no deba@458: /// separate function for the lower and the upper bound because deba@458: /// that would have been hard to implement for CPLEX. deba@458: void rowBounds(Row c, Value lower, Value upper) { deba@458: _setRowBounds(_lpId(c),lower, upper); deba@458: } deba@458: deba@458: /// Get the lower and the upper bounds of a row (i.e a constraint) deba@458: deba@458: /// The lower and the upper bound of deba@458: /// a constraint (row) are deba@458: /// extended numbers of type Value, i.e. finite numbers of type deba@458: /// Value, -\ref INF or \ref INF. deba@458: /// \todo There is no separate function for the deba@458: /// lower and the upper bound because we had problems with the deba@458: /// implementation of the setting functions for CPLEX: deba@458: /// check out whether this can be done for these functions. deba@458: void getRowBounds(Row c, Value &lower, Value &upper) const { deba@458: _getRowBounds(_lpId(c),lower, upper); deba@458: } deba@458: deba@458: ///Set an element of the objective function deba@458: void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); }; deba@458: deba@458: ///Get an element of the objective function deba@458: Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); }; deba@458: deba@458: ///Set the objective function deba@458: deba@458: ///\param e is a linear expression of type \ref Expr. deba@458: void obj(Expr e) { deba@458: _clearObj(); deba@458: for (Expr::iterator i=e.begin(); i!=e.end(); ++i) deba@458: objCoeff((*i).first,(*i).second); deba@458: obj_const_comp=e.constComp(); deba@458: } deba@458: deba@458: ///Get the objective function deba@458: deba@458: ///\return the objective function as a linear expression of type \ref Expr. deba@458: Expr obj() const { deba@458: Expr e; deba@458: for (ColIt it(*this); it != INVALID; ++it) { deba@458: double c = objCoeff(it); deba@458: if (c != 0.0) { deba@458: e.insert(std::make_pair(it, c)); deba@458: } deba@458: } deba@458: return e; deba@458: } deba@458: deba@458: deba@458: ///Maximize deba@458: void max() { _setMax(); } deba@458: ///Minimize deba@458: void min() { _setMin(); } deba@458: deba@458: ///Query function: is this a maximization problem? deba@458: bool isMax() const {return _isMax(); } deba@458: deba@458: ///Query function: is this a minimization problem? deba@458: bool isMin() const {return !isMax(); } deba@458: deba@458: ///@} deba@458: deba@458: deba@458: ///\name Solve the LP deba@458: deba@458: ///@{ deba@458: deba@458: ///\e Solve the LP problem at hand deba@458: /// deba@458: ///\return The result of the optimization procedure. Possible deba@458: ///values and their meanings can be found in the documentation of deba@458: ///\ref SolveExitStatus. deba@458: /// deba@458: ///\todo Which method is used to solve the problem deba@458: SolveExitStatus solve() { return _solve(); } deba@458: deba@458: ///@} deba@458: deba@458: ///\name Obtain the solution deba@458: deba@458: ///@{ deba@458: deba@458: /// The status of the primal problem (the original LP problem) deba@458: SolutionStatus primalStatus() const { deba@458: return _getPrimalStatus(); deba@458: } deba@458: deba@458: /// The status of the dual (of the original LP) problem deba@458: SolutionStatus dualStatus() const { deba@458: return _getDualStatus(); deba@458: } deba@458: deba@458: ///The type of the original LP problem deba@458: ProblemTypes problemType() const { deba@458: return _getProblemType(); deba@458: } deba@458: deba@458: ///\e deba@458: Value primal(Col c) const { return _getPrimal(_lpId(c)); } deba@458: ///\e deba@458: Value primal(const Expr& e) const { deba@458: double res = e.constComp(); deba@458: for (std::map::const_iterator it = e.begin(); deba@458: it != e.end(); ++it) { deba@458: res += _getPrimal(_lpId(it->first)) * it->second; deba@458: } deba@458: return res; deba@458: } deba@458: deba@458: ///\e deba@458: Value dual(Row r) const { return _getDual(_lpId(r)); } deba@458: ///\e deba@458: Value dual(const DualExpr& e) const { deba@458: double res = 0.0; deba@458: for (std::map::const_iterator it = e.begin(); deba@458: it != e.end(); ++it) { deba@458: res += _getPrimal(_lpId(it->first)) * it->second; deba@458: } deba@458: return res; deba@458: } deba@458: deba@458: ///\e deba@458: bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); } deba@458: deba@458: ///\e deba@458: deba@458: ///\return deba@458: ///- \ref INF or -\ref INF means either infeasibility or unboundedness deba@458: /// of the primal problem, depending on whether we minimize or maximize. deba@458: ///- \ref NaN if no primal solution is found. deba@458: ///- The (finite) objective value if an optimal solution is found. deba@458: Value primalValue() const { return _getPrimalValue()+obj_const_comp;} deba@458: ///@} deba@458: deba@458: }; deba@458: deba@458: deba@458: /// \ingroup lp_group deba@458: /// deba@458: /// \brief Common base class for MIP solvers deba@458: /// \todo Much more docs deba@458: class MipSolverBase : virtual public LpSolverBase{ deba@458: public: deba@458: deba@458: ///Possible variable (coloumn) types (e.g. real, integer, binary etc.) deba@458: enum ColTypes { deba@458: ///Continuous variable deba@458: REAL = 0, deba@458: ///Integer variable deba@458: deba@458: ///Unfortunately, cplex 7.5 somewhere writes something like deba@458: ///#define INTEGER 'I' deba@458: INT = 1 deba@458: ///\todo No support for other types yet. deba@458: }; deba@458: deba@458: ///Sets the type of the given coloumn to the given type deba@458: /// deba@458: ///Sets the type of the given coloumn to the given type. deba@458: void colType(Col c, ColTypes col_type) { deba@458: _colType(_lpId(c),col_type); deba@458: } deba@458: deba@458: ///Gives back the type of the column. deba@458: /// deba@458: ///Gives back the type of the column. deba@458: ColTypes colType(Col c) const { deba@458: return _colType(_lpId(c)); deba@458: } deba@458: deba@458: ///Sets the type of the given Col to integer or remove that property. deba@458: /// deba@458: ///Sets the type of the given Col to integer or remove that property. deba@458: void integer(Col c, bool enable) { deba@458: if (enable) deba@458: colType(c,INT); deba@458: else deba@458: colType(c,REAL); deba@458: } deba@458: deba@458: ///Gives back whether the type of the column is integer or not. deba@458: /// deba@458: ///Gives back the type of the column. deba@458: ///\return true if the column has integer type and false if not. deba@458: bool integer(Col c) const { deba@458: return (colType(c)==INT); deba@458: } deba@458: deba@458: /// The status of the MIP problem deba@458: SolutionStatus mipStatus() const { deba@458: return _getMipStatus(); deba@458: } deba@458: deba@458: protected: deba@458: deba@458: virtual ColTypes _colType(int col) const = 0; deba@458: virtual void _colType(int col, ColTypes col_type) = 0; deba@458: virtual SolutionStatus _getMipStatus() const = 0; deba@458: deba@458: }; deba@458: deba@458: ///\relates LpSolverBase::Expr deba@458: /// deba@458: inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, deba@458: const LpSolverBase::Expr &b) deba@458: { deba@458: LpSolverBase::Expr tmp(a); deba@458: tmp+=b; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Expr deba@458: /// deba@458: inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, deba@458: const LpSolverBase::Expr &b) deba@458: { deba@458: LpSolverBase::Expr tmp(a); deba@458: tmp-=b; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Expr deba@458: /// deba@458: inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, deba@458: const LpSolverBase::Value &b) deba@458: { deba@458: LpSolverBase::Expr tmp(a); deba@458: tmp*=b; deba@458: return tmp; deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Expr deba@458: /// deba@458: inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, deba@458: const LpSolverBase::Expr &b) deba@458: { deba@458: LpSolverBase::Expr tmp(b); deba@458: tmp*=a; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Expr deba@458: /// deba@458: inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, deba@458: const LpSolverBase::Value &b) deba@458: { deba@458: LpSolverBase::Expr tmp(a); deba@458: tmp/=b; deba@458: return tmp; deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, deba@458: const LpSolverBase::Expr &f) deba@458: { deba@458: return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, deba@458: const LpSolverBase::Expr &f) deba@458: { deba@458: return LpSolverBase::Constr(e,f); deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, deba@458: const LpSolverBase::Value &f) deba@458: { deba@458: return LpSolverBase::Constr(-LpSolverBase::INF,e,f); deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, deba@458: const LpSolverBase::Expr &f) deba@458: { deba@458: return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); deba@458: } deba@458: deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, deba@458: const LpSolverBase::Expr &f) deba@458: { deba@458: return LpSolverBase::Constr(f,e); deba@458: } deba@458: deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, deba@458: const LpSolverBase::Value &f) deba@458: { deba@458: return LpSolverBase::Constr(f,e,LpSolverBase::INF); deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, deba@458: const LpSolverBase::Value &f) deba@458: { deba@458: return LpSolverBase::Constr(f,e,f); deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, deba@458: const LpSolverBase::Expr &f) deba@458: { deba@458: return LpSolverBase::Constr(0,e-f,0); deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, deba@458: const LpSolverBase::Constr&c) deba@458: { deba@458: LpSolverBase::Constr tmp(c); deba@458: LEMON_ASSERT(LpSolverBase::isNaN(tmp.lowerBound()), "Wrong LP constraint"); deba@458: tmp.lowerBound()=n; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, deba@458: const LpSolverBase::Value &n) deba@458: { deba@458: LpSolverBase::Constr tmp(c); deba@458: LEMON_ASSERT(LpSolverBase::isNaN(tmp.upperBound()), "Wrong LP constraint"); deba@458: tmp.upperBound()=n; deba@458: return tmp; deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, deba@458: const LpSolverBase::Constr&c) deba@458: { deba@458: LpSolverBase::Constr tmp(c); deba@458: LEMON_ASSERT(LpSolverBase::isNaN(tmp.upperBound()), "Wrong LP constraint"); deba@458: tmp.upperBound()=n; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::Constr deba@458: /// deba@458: inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, deba@458: const LpSolverBase::Value &n) deba@458: { deba@458: LpSolverBase::Constr tmp(c); deba@458: LEMON_ASSERT(LpSolverBase::isNaN(tmp.lowerBound()), "Wrong LP constraint"); deba@458: tmp.lowerBound()=n; deba@458: return tmp; deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::DualExpr deba@458: /// deba@458: inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a, deba@458: const LpSolverBase::DualExpr &b) deba@458: { deba@458: LpSolverBase::DualExpr tmp(a); deba@458: tmp+=b; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::DualExpr deba@458: /// deba@458: inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a, deba@458: const LpSolverBase::DualExpr &b) deba@458: { deba@458: LpSolverBase::DualExpr tmp(a); deba@458: tmp-=b; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::DualExpr deba@458: /// deba@458: inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a, deba@458: const LpSolverBase::Value &b) deba@458: { deba@458: LpSolverBase::DualExpr tmp(a); deba@458: tmp*=b; deba@458: return tmp; deba@458: } deba@458: deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::DualExpr deba@458: /// deba@458: inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a, deba@458: const LpSolverBase::DualExpr &b) deba@458: { deba@458: LpSolverBase::DualExpr tmp(b); deba@458: tmp*=a; deba@458: return tmp; deba@458: } deba@458: ///\e deba@458: deba@458: ///\relates LpSolverBase::DualExpr deba@458: /// deba@458: inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a, deba@458: const LpSolverBase::Value &b) deba@458: { deba@458: LpSolverBase::DualExpr tmp(a); deba@458: tmp/=b; deba@458: return tmp; deba@458: } deba@458: deba@458: deba@458: } //namespace lemon deba@458: deba@458: #endif //LEMON_LP_BASE_H