1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_LP_BASE_H
20 #define LEMON_LP_BASE_H
26 #include<lemon/math.h>
28 #include<lemon/core.h>
29 #include<lemon/bits/lp_id.h>
32 ///\brief The interface of the LP solver interface.
36 /// Function to decide whether a floating point value is finite or not.
38 /// Retruns true if the argument is not infinity, minus infinity or NaN.
39 /// It does the same as the isfinite() function defined by C99.
41 bool isFinite(T value)
43 typedef std::numeric_limits<T> Lim;
44 if ((Lim::has_infinity && (value == Lim::infinity() || value ==
46 ((Lim::has_quiet_NaN || Lim::has_signaling_NaN) && value != value))
53 ///Common base class for LP solvers
55 ///\todo Much more docs
66 ///Possible outcomes of an LP solving procedure
67 enum SolveExitStatus {
68 ///This means that the problem has been successfully solved: either
69 ///an optimal solution has been found or infeasibility/unboundedness
72 ///Any other case (including the case when some user specified
73 ///limit has been exceeded)
79 ///Feasible solution hasn't been found (but may exist).
81 ///\todo NOTFOUND might be a better name.
84 ///The problem has no feasible solution
86 ///Feasible solution found
88 ///Optimal solution exists and found
90 ///The cost function is unbounded
92 ///\todo Give a feasible solution and an infinite ray (and the
93 ///corresponding bases)
97 ///\e The type of the investigated LP problem
99 ///Primal-dual feasible
100 PRIMAL_DUAL_FEASIBLE = 0,
101 ///Primal feasible dual infeasible
102 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
103 ///Primal infeasible dual feasible
104 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
105 ///Primal-dual infeasible
106 PRIMAL_DUAL_INFEASIBLE = 3,
107 ///Could not determine so far
111 ///The floating point type used by the solver
112 typedef double Value;
113 ///The infinity constant
114 static const Value INF;
115 ///The not a number constant
116 static const Value NaN;
118 static inline bool isNaN(const Value& v) { return v!=v; }
124 ///Refer to a column of the LP.
126 ///This type is used to refer to a column of the LP.
128 ///Its value remains valid and correct even after the addition or erase of
131 ///\todo Document what can one do with a Col (INVALID, comparing,
132 ///it is similar to Node/Edge)
136 friend class LpSolverBase;
137 friend class MipSolverBase;
138 explicit Col(int _id) : id(_id) {}
140 typedef Value ExprValue;
141 typedef True LpSolverCol;
143 Col(const Invalid&) : id(-1) {}
144 bool operator< (Col c) const {return id< c.id;}
145 bool operator> (Col c) const {return id> c.id;}
146 bool operator==(Col c) const {return id==c.id;}
147 bool operator!=(Col c) const {return id!=c.id;}
150 class ColIt : public Col {
151 const LpSolverBase *_lp;
154 ColIt(const LpSolverBase &lp) : _lp(&lp)
156 _lp->cols.firstFix(id);
158 ColIt(const Invalid&) : Col(INVALID) {}
161 _lp->cols.nextFix(id);
166 static int id(const Col& col) { return col.id; }
169 ///Refer to a row of the LP.
171 ///This type is used to refer to a row of the LP.
173 ///Its value remains valid and correct even after the addition or erase of
176 ///\todo Document what can one do with a Row (INVALID, comparing,
177 ///it is similar to Node/Edge)
181 friend class LpSolverBase;
182 explicit Row(int _id) : id(_id) {}
184 typedef Value ExprValue;
185 typedef True LpSolverRow;
187 Row(const Invalid&) : id(-1) {}
189 bool operator< (Row c) const {return id< c.id;}
190 bool operator> (Row c) const {return id> c.id;}
191 bool operator==(Row c) const {return id==c.id;}
192 bool operator!=(Row c) const {return id!=c.id;}
195 class RowIt : public Row {
196 const LpSolverBase *_lp;
199 RowIt(const LpSolverBase &lp) : _lp(&lp)
201 _lp->rows.firstFix(id);
203 RowIt(const Invalid&) : Row(INVALID) {}
206 _lp->rows.nextFix(id);
211 static int id(const Row& row) { return row.id; }
215 int _lpId(const Col& c) const {
216 return cols.floatingId(id(c));
219 int _lpId(const Row& r) const {
220 return rows.floatingId(id(r));
223 Col _item(int i, Col) const {
224 return Col(cols.fixId(i));
227 Row _item(int i, Row) const {
228 return Row(rows.fixId(i));
234 ///Linear expression of variables and a constant component
236 ///This data structure stores a linear expression of the variables
237 ///(\ref Col "Col"s) and also has a constant component.
239 ///There are several ways to access and modify the contents of this
241 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
242 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
243 ///read and modify the coefficients like
250 ///or you can also iterate through its elements.
253 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
256 ///(This code computes the sum of all coefficients).
257 ///- Numbers (<tt>double</tt>'s)
258 ///and variables (\ref Col "Col"s) directly convert to an
259 ///\ref Expr and the usual linear operations are defined, so
262 ///2*v-3.12*(v-w/2)+2
263 ///v*2.1+(3*v+(v*12+w+6)*3)/2
265 ///are valid \ref Expr "Expr"essions.
266 ///The usual assignment operations are also defined.
269 ///e+=2*v-3.12*(v-w/2)+2;
273 ///- The constant member can be set and read by \ref constComp()
276 ///double c=e.constComp();
279 ///\note \ref clear() not only sets all coefficients to 0 but also
280 ///clears the constant components.
284 class Expr : public std::map<Col,Value>
287 typedef LpSolverBase::Col Key;
288 typedef LpSolverBase::Value Value;
291 typedef std::map<Col,Value> Base;
295 typedef True IsLinExpression;
297 Expr() : Base(), const_comp(0) { }
299 Expr(const Key &v) : const_comp(0) {
300 Base::insert(std::make_pair(v, 1));
303 Expr(const Value &v) : const_comp(v) {}
305 void set(const Key &v,const Value &c) {
306 Base::insert(std::make_pair(v, c));
309 Value &constComp() { return const_comp; }
311 const Value &constComp() const { return const_comp; }
313 ///Removes the components with zero coefficient.
315 for (Base::iterator i=Base::begin(); i!=Base::end();) {
318 if ((*i).second==0) Base::erase(i);
323 void simplify() const {
324 const_cast<Expr*>(this)->simplify();
327 ///Removes the coefficients closer to zero than \c tolerance.
328 void simplify(double &tolerance) {
329 for (Base::iterator i=Base::begin(); i!=Base::end();) {
332 if (std::fabs((*i).second)<tolerance) Base::erase(i);
337 ///Sets all coefficients and the constant component to 0.
344 Expr &operator+=(const Expr &e) {
345 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
346 (*this)[j->first]+=j->second;
347 const_comp+=e.const_comp;
351 Expr &operator-=(const Expr &e) {
352 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
353 (*this)[j->first]-=j->second;
354 const_comp-=e.const_comp;
358 Expr &operator*=(const Value &c) {
359 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
365 Expr &operator/=(const Value &c) {
366 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
376 ///This data stucture represents a linear constraint in the LP.
377 ///Basically it is a linear expression with a lower or an upper bound
378 ///(or both). These parts of the constraint can be obtained by the member
379 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
381 ///There are two ways to construct a constraint.
382 ///- You can set the linear expression and the bounds directly
383 /// by the functions above.
384 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
385 /// are defined between expressions, or even between constraints whenever
386 /// it makes sense. Therefore if \c e and \c f are linear expressions and
387 /// \c s and \c t are numbers, then the followings are valid expressions
388 /// and thus they can be used directly e.g. in \ref addRow() whenever
397 ///\warning The validity of a constraint is checked only at run time, so
398 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw
403 typedef LpSolverBase::Expr Expr;
404 typedef Expr::Key Key;
405 typedef Expr::Value Value;
412 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
414 Constr(Value lb,const Expr &e,Value ub) :
415 _expr(e), _lb(lb), _ub(ub) {}
417 Constr(const Expr &e,Value ub) :
418 _expr(e), _lb(NaN), _ub(ub) {}
420 Constr(Value lb,const Expr &e) :
421 _expr(e), _lb(lb), _ub(NaN) {}
423 Constr(const Expr &e) :
424 _expr(e), _lb(NaN), _ub(NaN) {}
432 ///Reference to the linear expression
433 Expr &expr() { return _expr; }
434 ///Cont reference to the linear expression
435 const Expr &expr() const { return _expr; }
436 ///Reference to the lower bound.
439 ///- \ref INF "INF": the constraint is lower unbounded.
440 ///- \ref NaN "NaN": lower bound has not been set.
441 ///- finite number: the lower bound
442 Value &lowerBound() { return _lb; }
443 ///The const version of \ref lowerBound()
444 const Value &lowerBound() const { return _lb; }
445 ///Reference to the upper bound.
448 ///- \ref INF "INF": the constraint is upper unbounded.
449 ///- \ref NaN "NaN": upper bound has not been set.
450 ///- finite number: the upper bound
451 Value &upperBound() { return _ub; }
452 ///The const version of \ref upperBound()
453 const Value &upperBound() const { return _ub; }
454 ///Is the constraint lower bounded?
455 bool lowerBounded() const {
456 return isFinite(_lb);
458 ///Is the constraint upper bounded?
459 bool upperBounded() const {
460 return isFinite(_ub);
465 ///Linear expression of rows
467 ///This data structure represents a column of the matrix,
468 ///thas is it strores a linear expression of the dual variables
469 ///(\ref Row "Row"s).
471 ///There are several ways to access and modify the contents of this
473 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
474 ///if \c e is an DualExpr and \c v
475 ///and \c w are of type \ref Row, then you can
476 ///read and modify the coefficients like
483 ///or you can also iterate through its elements.
486 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
489 ///(This code computes the sum of all coefficients).
490 ///- Numbers (<tt>double</tt>'s)
491 ///and variables (\ref Row "Row"s) directly convert to an
492 ///\ref DualExpr and the usual linear operations are defined, so
496 ///v*2.1+(3*v+(v*12+w)*3)/2
498 ///are valid \ref DualExpr "DualExpr"essions.
499 ///The usual assignment operations are also defined.
502 ///e+=2*v-3.12*(v-w/2);
509 class DualExpr : public std::map<Row,Value>
512 typedef LpSolverBase::Row Key;
513 typedef LpSolverBase::Value Value;
516 typedef std::map<Row,Value> Base;
519 typedef True IsLinExpression;
521 DualExpr() : Base() { }
523 DualExpr(const Key &v) {
524 Base::insert(std::make_pair(v, 1));
527 void set(const Key &v,const Value &c) {
528 Base::insert(std::make_pair(v, c));
531 ///Removes the components with zero coefficient.
533 for (Base::iterator i=Base::begin(); i!=Base::end();) {
536 if ((*i).second==0) Base::erase(i);
541 void simplify() const {
542 const_cast<DualExpr*>(this)->simplify();
545 ///Removes the coefficients closer to zero than \c tolerance.
546 void simplify(double &tolerance) {
547 for (Base::iterator i=Base::begin(); i!=Base::end();) {
550 if (std::fabs((*i).second)<tolerance) Base::erase(i);
555 ///Sets all coefficients to 0.
561 DualExpr &operator+=(const DualExpr &e) {
562 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
563 (*this)[j->first]+=j->second;
567 DualExpr &operator-=(const DualExpr &e) {
568 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
569 (*this)[j->first]-=j->second;
573 DualExpr &operator*=(const Value &c) {
574 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
579 DualExpr &operator/=(const Value &c) {
580 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
589 template <typename _Expr>
590 class MappedOutputIterator {
593 typedef std::insert_iterator<_Expr> Base;
595 typedef std::output_iterator_tag iterator_category;
596 typedef void difference_type;
597 typedef void value_type;
598 typedef void reference;
599 typedef void pointer;
601 MappedOutputIterator(const Base& _base, const LpSolverBase& _lp)
602 : base(_base), lp(_lp) {}
604 MappedOutputIterator& operator*() {
608 MappedOutputIterator& operator=(const std::pair<int, Value>& value) {
609 *base = std::make_pair(lp._item(value.first, typename _Expr::Key()),
614 MappedOutputIterator& operator++() {
619 MappedOutputIterator operator++(int) {
620 MappedOutputIterator tmp(*this);
625 bool operator==(const MappedOutputIterator& it) const {
626 return base == it.base;
629 bool operator!=(const MappedOutputIterator& it) const {
630 return base != it.base;
635 const LpSolverBase& lp;
638 template <typename Expr>
639 class MappedInputIterator {
642 typedef typename Expr::const_iterator Base;
644 typedef typename Base::iterator_category iterator_category;
645 typedef typename Base::difference_type difference_type;
646 typedef const std::pair<int, Value> value_type;
647 typedef value_type reference;
650 pointer(value_type& _value) : value(_value) {}
651 value_type* operator->() { return &value; }
656 MappedInputIterator(const Base& _base, const LpSolverBase& _lp)
657 : base(_base), lp(_lp) {}
659 reference operator*() {
660 return std::make_pair(lp._lpId(base->first), base->second);
663 pointer operator->() {
664 return pointer(operator*());
667 MappedInputIterator& operator++() {
672 MappedInputIterator operator++(int) {
673 MappedInputIterator tmp(*this);
678 bool operator==(const MappedInputIterator& it) const {
679 return base == it.base;
682 bool operator!=(const MappedInputIterator& it) const {
683 return base != it.base;
688 const LpSolverBase& lp;
693 /// STL compatible iterator for lp col
694 typedef MappedInputIterator<Expr> ConstRowIterator;
695 /// STL compatible iterator for lp row
696 typedef MappedInputIterator<DualExpr> ConstColIterator;
698 /// STL compatible iterator for lp col
699 typedef MappedOutputIterator<Expr> RowIterator;
700 /// STL compatible iterator for lp row
701 typedef MappedOutputIterator<DualExpr> ColIterator;
703 //Abstract virtual functions
704 virtual LpSolverBase* _newLp() = 0;
705 virtual LpSolverBase* _copyLp(){
706 LpSolverBase* newlp = _newLp();
708 std::map<Col, Col> ref;
709 for (LpSolverBase::ColIt it(*this); it != INVALID; ++it) {
710 Col ccol = newlp->addCol();
712 newlp->colName(ccol, colName(it));
713 newlp->colLowerBound(ccol, colLowerBound(it));
714 newlp->colUpperBound(ccol, colUpperBound(it));
717 for (LpSolverBase::RowIt it(*this); it != INVALID; ++it) {
718 Expr e = row(it), ce;
719 for (Expr::iterator jt = e.begin(); jt != e.end(); ++jt) {
720 ce[ref[jt->first]] = jt->second;
723 Row r = newlp->addRow(ce);
726 getRowBounds(it, lower, upper);
727 newlp->rowBounds(r, lower, upper);
733 virtual int _addCol() = 0;
734 virtual int _addRow() = 0;
736 virtual void _eraseCol(int col) = 0;
737 virtual void _eraseRow(int row) = 0;
739 virtual void _getColName(int col, std::string & name) const = 0;
740 virtual void _setColName(int col, const std::string & name) = 0;
741 virtual int _colByName(const std::string& name) const = 0;
743 virtual void _setRowCoeffs(int i, ConstRowIterator b,
744 ConstRowIterator e) = 0;
745 virtual void _getRowCoeffs(int i, RowIterator b) const = 0;
746 virtual void _setColCoeffs(int i, ConstColIterator b,
747 ConstColIterator e) = 0;
748 virtual void _getColCoeffs(int i, ColIterator b) const = 0;
749 virtual void _setCoeff(int row, int col, Value value) = 0;
750 virtual Value _getCoeff(int row, int col) const = 0;
751 virtual void _setColLowerBound(int i, Value value) = 0;
752 virtual Value _getColLowerBound(int i) const = 0;
753 virtual void _setColUpperBound(int i, Value value) = 0;
754 virtual Value _getColUpperBound(int i) const = 0;
755 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
756 virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0;
758 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
759 virtual Value _getObjCoeff(int i) const = 0;
760 virtual void _clearObj()=0;
762 virtual SolveExitStatus _solve() = 0;
763 virtual Value _getPrimal(int i) const = 0;
764 virtual Value _getDual(int i) const = 0;
765 virtual Value _getPrimalValue() const = 0;
766 virtual bool _isBasicCol(int i) const = 0;
767 virtual SolutionStatus _getPrimalStatus() const = 0;
768 virtual SolutionStatus _getDualStatus() const = 0;
769 virtual ProblemTypes _getProblemType() const = 0;
771 virtual void _setMax() = 0;
772 virtual void _setMin() = 0;
775 virtual bool _isMax() const = 0;
777 //Own protected stuff
779 //Constant component of the objective function
780 Value obj_const_comp;
785 LpSolverBase() : obj_const_comp(0) {}
788 virtual ~LpSolverBase() {}
790 ///Creates a new LP problem
791 LpSolverBase* newLp() {return _newLp();}
792 ///Makes a copy of the LP problem
793 LpSolverBase* copyLp() {return _copyLp();}
795 ///\name Build up and modify the LP
799 ///Add a new empty column (i.e a new variable) to the LP
800 Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;}
802 ///\brief Adds several new columns
803 ///(i.e a variables) at once
805 ///This magic function takes a container as its argument
806 ///and fills its elements
807 ///with new columns (i.e. variables)
809 ///- a standard STL compatible iterable container with
810 ///\ref Col as its \c values_type
813 ///std::vector<LpSolverBase::Col>
814 ///std::list<LpSolverBase::Col>
816 ///- a standard STL compatible iterable container with
817 ///\ref Col as its \c mapped_type
820 ///std::map<AnyType,LpSolverBase::Col>
822 ///- an iterable lemon \ref concepts::WriteMap "write map" like
824 ///ListGraph::NodeMap<LpSolverBase::Col>
825 ///ListGraph::EdgeMap<LpSolverBase::Col>
827 ///\return The number of the created column.
830 int addColSet(T &t) { return 0;}
833 typename enable_if<typename T::value_type::LpSolverCol,int>::type
834 addColSet(T &t,dummy<0> = 0) {
836 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
840 typename enable_if<typename T::value_type::second_type::LpSolverCol,
842 addColSet(T &t,dummy<1> = 1) {
844 for(typename T::iterator i=t.begin();i!=t.end();++i) {
851 typename enable_if<typename T::MapIt::Value::LpSolverCol,
853 addColSet(T &t,dummy<2> = 2) {
855 for(typename T::MapIt i(t); i!=INVALID; ++i)
864 ///Set a column (i.e a dual constraint) of the LP
866 ///\param c is the column to be modified
867 ///\param e is a dual linear expression (see \ref DualExpr)
869 void col(Col c,const DualExpr &e) {
871 _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this),
872 ConstColIterator(e.end(), *this));
875 ///Get a column (i.e a dual constraint) of the LP
877 ///\param r is the column to get
878 ///\return the dual expression associated to the column
879 DualExpr col(Col c) const {
881 _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this));
885 ///Add a new column to the LP
887 ///\param e is a dual linear expression (see \ref DualExpr)
888 ///\param obj is the corresponding component of the objective
889 ///function. It is 0 by default.
890 ///\return The created column.
891 Col addCol(const DualExpr &e, Value o = 0) {
898 ///Add a new empty row (i.e a new constraint) to the LP
900 ///This function adds a new empty row (i.e a new constraint) to the LP.
901 ///\return The created row
902 Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;}
904 ///\brief Add several new rows
905 ///(i.e a constraints) at once
907 ///This magic function takes a container as its argument
908 ///and fills its elements
909 ///with new row (i.e. variables)
911 ///- a standard STL compatible iterable container with
912 ///\ref Row as its \c values_type
915 ///std::vector<LpSolverBase::Row>
916 ///std::list<LpSolverBase::Row>
918 ///- a standard STL compatible iterable container with
919 ///\ref Row as its \c mapped_type
922 ///std::map<AnyType,LpSolverBase::Row>
924 ///- an iterable lemon \ref concepts::WriteMap "write map" like
926 ///ListGraph::NodeMap<LpSolverBase::Row>
927 ///ListGraph::EdgeMap<LpSolverBase::Row>
929 ///\return The number of rows created.
932 int addRowSet(T &t) { return 0;}
935 typename enable_if<typename T::value_type::LpSolverRow,int>::type
936 addRowSet(T &t,dummy<0> = 0) {
938 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
942 typename enable_if<typename T::value_type::second_type::LpSolverRow,
944 addRowSet(T &t,dummy<1> = 1) {
946 for(typename T::iterator i=t.begin();i!=t.end();++i) {
953 typename enable_if<typename T::MapIt::Value::LpSolverRow,
955 addRowSet(T &t,dummy<2> = 2) {
957 for(typename T::MapIt i(t); i!=INVALID; ++i)
966 ///Set a row (i.e a constraint) of the LP
968 ///\param r is the row to be modified
969 ///\param l is lower bound (-\ref INF means no bound)
970 ///\param e is a linear expression (see \ref Expr)
971 ///\param u is the upper bound (\ref INF means no bound)
972 ///\bug This is a temporary function. The interface will change to
974 ///\todo Option to control whether a constraint with a single variable is
976 void row(Row r, Value l, const Expr &e, Value u) {
978 _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this),
979 ConstRowIterator(e.end(), *this));
980 _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
983 ///Set a row (i.e a constraint) of the LP
985 ///\param r is the row to be modified
986 ///\param c is a linear expression (see \ref Constr)
987 void row(Row r, const Constr &c) {
988 row(r, c.lowerBounded()?c.lowerBound():-INF,
989 c.expr(), c.upperBounded()?c.upperBound():INF);
993 ///Get a row (i.e a constraint) of the LP
995 ///\param r is the row to get
996 ///\return the expression associated to the row
997 Expr row(Row r) const {
999 _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this));
1003 ///Add a new row (i.e a new constraint) to the LP
1005 ///\param l is the lower bound (-\ref INF means no bound)
1006 ///\param e is a linear expression (see \ref Expr)
1007 ///\param u is the upper bound (\ref INF means no bound)
1008 ///\return The created row.
1009 ///\bug This is a temporary function. The interface will change to
1011 Row addRow(Value l,const Expr &e, Value u) {
1017 ///Add a new row (i.e a new constraint) to the LP
1019 ///\param c is a linear expression (see \ref Constr)
1020 ///\return The created row.
1021 Row addRow(const Constr &c) {
1026 ///Erase a coloumn (i.e a variable) from the LP
1028 ///\param c is the coloumn to be deleted
1029 ///\todo Please check this
1030 void eraseCol(Col c) {
1031 _eraseCol(_lpId(c));
1034 ///Erase a row (i.e a constraint) from the LP
1036 ///\param r is the row to be deleted
1037 ///\todo Please check this
1038 void eraseRow(Row r) {
1039 _eraseRow(_lpId(r));
1043 /// Get the name of a column
1045 ///\param c is the coresponding coloumn
1046 ///\return The name of the colunm
1047 std::string colName(Col c) const {
1049 _getColName(_lpId(c), name);
1053 /// Set the name of a column
1055 ///\param c is the coresponding coloumn
1056 ///\param name The name to be given
1057 void colName(Col c, const std::string& name) {
1058 _setColName(_lpId(c), name);
1061 /// Get the column by its name
1063 ///\param name The name of the column
1064 ///\return the proper column or \c INVALID
1065 Col colByName(const std::string& name) const {
1066 int k = _colByName(name);
1067 return k != -1 ? Col(cols.fixId(k)) : Col(INVALID);
1070 /// Set an element of the coefficient matrix of the LP
1072 ///\param r is the row of the element to be modified
1073 ///\param c is the coloumn of the element to be modified
1074 ///\param val is the new value of the coefficient
1076 void coeff(Row r, Col c, Value val) {
1077 _setCoeff(_lpId(r),_lpId(c), val);
1080 /// Get an element of the coefficient matrix of the LP
1082 ///\param r is the row of the element in question
1083 ///\param c is the coloumn of the element in question
1084 ///\return the corresponding coefficient
1086 Value coeff(Row r, Col c) const {
1087 return _getCoeff(_lpId(r),_lpId(c));
1090 /// Set the lower bound of a column (i.e a variable)
1092 /// The lower bound of a variable (column) has to be given by an
1093 /// extended number of type Value, i.e. a finite number of type
1094 /// Value or -\ref INF.
1095 void colLowerBound(Col c, Value value) {
1096 _setColLowerBound(_lpId(c),value);
1099 /// Get the lower bound of a column (i.e a variable)
1101 /// This function returns the lower bound for column (variable) \t c
1102 /// (this might be -\ref INF as well).
1103 ///\return The lower bound for coloumn \t c
1104 Value colLowerBound(Col c) const {
1105 return _getColLowerBound(_lpId(c));
1108 ///\brief Set the lower bound of several columns
1109 ///(i.e a variables) at once
1111 ///This magic function takes a container as its argument
1112 ///and applies the function on all of its elements.
1113 /// The lower bound of a variable (column) has to be given by an
1114 /// extended number of type Value, i.e. a finite number of type
1115 /// Value or -\ref INF.
1118 void colLowerBound(T &t, Value value) { return 0;}
1121 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1122 colLowerBound(T &t, Value value,dummy<0> = 0) {
1123 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1124 colLowerBound(*i, value);
1128 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1130 colLowerBound(T &t, Value value,dummy<1> = 1) {
1131 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1132 colLowerBound(i->second, value);
1136 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1138 colLowerBound(T &t, Value value,dummy<2> = 2) {
1139 for(typename T::MapIt i(t); i!=INVALID; ++i){
1140 colLowerBound(*i, value);
1145 /// Set the upper bound of a column (i.e a variable)
1147 /// The upper bound of a variable (column) has to be given by an
1148 /// extended number of type Value, i.e. a finite number of type
1149 /// Value or \ref INF.
1150 void colUpperBound(Col c, Value value) {
1151 _setColUpperBound(_lpId(c),value);
1154 /// Get the upper bound of a column (i.e a variable)
1156 /// This function returns the upper bound for column (variable) \t c
1157 /// (this might be \ref INF as well).
1158 ///\return The upper bound for coloumn \t c
1159 Value colUpperBound(Col c) const {
1160 return _getColUpperBound(_lpId(c));
1163 ///\brief Set the upper bound of several columns
1164 ///(i.e a variables) at once
1166 ///This magic function takes a container as its argument
1167 ///and applies the function on all of its elements.
1168 /// The upper bound of a variable (column) has to be given by an
1169 /// extended number of type Value, i.e. a finite number of type
1170 /// Value or \ref INF.
1173 void colUpperBound(T &t, Value value) { return 0;}
1176 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1177 colUpperBound(T &t, Value value,dummy<0> = 0) {
1178 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1179 colUpperBound(*i, value);
1183 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1185 colUpperBound(T &t, Value value,dummy<1> = 1) {
1186 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1187 colUpperBound(i->second, value);
1191 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1193 colUpperBound(T &t, Value value,dummy<2> = 2) {
1194 for(typename T::MapIt i(t); i!=INVALID; ++i){
1195 colUpperBound(*i, value);
1200 /// Set the lower and the upper bounds of a column (i.e a variable)
1202 /// The lower and the upper bounds of
1203 /// a variable (column) have to be given by an
1204 /// extended number of type Value, i.e. a finite number of type
1205 /// Value, -\ref INF or \ref INF.
1206 void colBounds(Col c, Value lower, Value upper) {
1207 _setColLowerBound(_lpId(c),lower);
1208 _setColUpperBound(_lpId(c),upper);
1211 ///\brief Set the lower and the upper bound of several columns
1212 ///(i.e a variables) at once
1214 ///This magic function takes a container as its argument
1215 ///and applies the function on all of its elements.
1216 /// The lower and the upper bounds of
1217 /// a variable (column) have to be given by an
1218 /// extended number of type Value, i.e. a finite number of type
1219 /// Value, -\ref INF or \ref INF.
1222 void colBounds(T &t, Value lower, Value upper) { return 0;}
1225 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1226 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1227 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1228 colBounds(*i, lower, upper);
1232 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1234 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1235 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1236 colBounds(i->second, lower, upper);
1240 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1242 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1243 for(typename T::MapIt i(t); i!=INVALID; ++i){
1244 colBounds(*i, lower, upper);
1250 /// Set the lower and the upper bounds of a row (i.e a constraint)
1252 /// The lower and the upper bound of a constraint (row) have to be
1253 /// given by an extended number of type Value, i.e. a finite
1254 /// number of type Value, -\ref INF or \ref INF. There is no
1255 /// separate function for the lower and the upper bound because
1256 /// that would have been hard to implement for CPLEX.
1257 void rowBounds(Row c, Value lower, Value upper) {
1258 _setRowBounds(_lpId(c),lower, upper);
1261 /// Get the lower and the upper bounds of a row (i.e a constraint)
1263 /// The lower and the upper bound of
1264 /// a constraint (row) are
1265 /// extended numbers of type Value, i.e. finite numbers of type
1266 /// Value, -\ref INF or \ref INF.
1267 /// \todo There is no separate function for the
1268 /// lower and the upper bound because we had problems with the
1269 /// implementation of the setting functions for CPLEX:
1270 /// check out whether this can be done for these functions.
1271 void getRowBounds(Row c, Value &lower, Value &upper) const {
1272 _getRowBounds(_lpId(c),lower, upper);
1275 ///Set an element of the objective function
1276 void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
1278 ///Get an element of the objective function
1279 Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); };
1281 ///Set the objective function
1283 ///\param e is a linear expression of type \ref Expr.
1286 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1287 objCoeff((*i).first,(*i).second);
1288 obj_const_comp=e.constComp();
1291 ///Get the objective function
1293 ///\return the objective function as a linear expression of type \ref Expr.
1296 for (ColIt it(*this); it != INVALID; ++it) {
1297 double c = objCoeff(it);
1299 e.insert(std::make_pair(it, c));
1307 void max() { _setMax(); }
1309 void min() { _setMin(); }
1311 ///Query function: is this a maximization problem?
1312 bool isMax() const {return _isMax(); }
1314 ///Query function: is this a minimization problem?
1315 bool isMin() const {return !isMax(); }
1320 ///\name Solve the LP
1324 ///\e Solve the LP problem at hand
1326 ///\return The result of the optimization procedure. Possible
1327 ///values and their meanings can be found in the documentation of
1328 ///\ref SolveExitStatus.
1330 ///\todo Which method is used to solve the problem
1331 SolveExitStatus solve() { return _solve(); }
1335 ///\name Obtain the solution
1339 /// The status of the primal problem (the original LP problem)
1340 SolutionStatus primalStatus() const {
1341 return _getPrimalStatus();
1344 /// The status of the dual (of the original LP) problem
1345 SolutionStatus dualStatus() const {
1346 return _getDualStatus();
1349 ///The type of the original LP problem
1350 ProblemTypes problemType() const {
1351 return _getProblemType();
1355 Value primal(Col c) const { return _getPrimal(_lpId(c)); }
1357 Value primal(const Expr& e) const {
1358 double res = e.constComp();
1359 for (std::map<Col, double>::const_iterator it = e.begin();
1360 it != e.end(); ++it) {
1361 res += _getPrimal(_lpId(it->first)) * it->second;
1367 Value dual(Row r) const { return _getDual(_lpId(r)); }
1369 Value dual(const DualExpr& e) const {
1371 for (std::map<Row, double>::const_iterator it = e.begin();
1372 it != e.end(); ++it) {
1373 res += _getPrimal(_lpId(it->first)) * it->second;
1379 bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); }
1384 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1385 /// of the primal problem, depending on whether we minimize or maximize.
1386 ///- \ref NaN if no primal solution is found.
1387 ///- The (finite) objective value if an optimal solution is found.
1388 Value primalValue() const { return _getPrimalValue()+obj_const_comp;}
1394 /// \ingroup lp_group
1396 /// \brief Common base class for MIP solvers
1397 /// \todo Much more docs
1398 class MipSolverBase : virtual public LpSolverBase{
1401 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1403 ///Continuous variable
1407 ///Unfortunately, cplex 7.5 somewhere writes something like
1408 ///#define INTEGER 'I'
1410 ///\todo No support for other types yet.
1413 ///Sets the type of the given coloumn to the given type
1415 ///Sets the type of the given coloumn to the given type.
1416 void colType(Col c, ColTypes col_type) {
1417 _colType(_lpId(c),col_type);
1420 ///Gives back the type of the column.
1422 ///Gives back the type of the column.
1423 ColTypes colType(Col c) const {
1424 return _colType(_lpId(c));
1427 ///Sets the type of the given Col to integer or remove that property.
1429 ///Sets the type of the given Col to integer or remove that property.
1430 void integer(Col c, bool enable) {
1437 ///Gives back whether the type of the column is integer or not.
1439 ///Gives back the type of the column.
1440 ///\return true if the column has integer type and false if not.
1441 bool integer(Col c) const {
1442 return (colType(c)==INT);
1445 /// The status of the MIP problem
1446 SolutionStatus mipStatus() const {
1447 return _getMipStatus();
1452 virtual ColTypes _colType(int col) const = 0;
1453 virtual void _colType(int col, ColTypes col_type) = 0;
1454 virtual SolutionStatus _getMipStatus() const = 0;
1458 ///\relates LpSolverBase::Expr
1460 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1461 const LpSolverBase::Expr &b)
1463 LpSolverBase::Expr tmp(a);
1469 ///\relates LpSolverBase::Expr
1471 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1472 const LpSolverBase::Expr &b)
1474 LpSolverBase::Expr tmp(a);
1480 ///\relates LpSolverBase::Expr
1482 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1483 const LpSolverBase::Value &b)
1485 LpSolverBase::Expr tmp(a);
1492 ///\relates LpSolverBase::Expr
1494 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1495 const LpSolverBase::Expr &b)
1497 LpSolverBase::Expr tmp(b);
1503 ///\relates LpSolverBase::Expr
1505 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1506 const LpSolverBase::Value &b)
1508 LpSolverBase::Expr tmp(a);
1515 ///\relates LpSolverBase::Constr
1517 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1518 const LpSolverBase::Expr &f)
1520 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1525 ///\relates LpSolverBase::Constr
1527 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1528 const LpSolverBase::Expr &f)
1530 return LpSolverBase::Constr(e,f);
1535 ///\relates LpSolverBase::Constr
1537 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1538 const LpSolverBase::Value &f)
1540 return LpSolverBase::Constr(-LpSolverBase::INF,e,f);
1545 ///\relates LpSolverBase::Constr
1547 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1548 const LpSolverBase::Expr &f)
1550 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1556 ///\relates LpSolverBase::Constr
1558 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1559 const LpSolverBase::Expr &f)
1561 return LpSolverBase::Constr(f,e);
1567 ///\relates LpSolverBase::Constr
1569 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1570 const LpSolverBase::Value &f)
1572 return LpSolverBase::Constr(f,e,LpSolverBase::INF);
1577 ///\relates LpSolverBase::Constr
1579 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1580 const LpSolverBase::Value &f)
1582 return LpSolverBase::Constr(f,e,f);
1587 ///\relates LpSolverBase::Constr
1589 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1590 const LpSolverBase::Expr &f)
1592 return LpSolverBase::Constr(0,e-f,0);
1597 ///\relates LpSolverBase::Constr
1599 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1600 const LpSolverBase::Constr&c)
1602 LpSolverBase::Constr tmp(c);
1603 LEMON_ASSERT(LpSolverBase::isNaN(tmp.lowerBound()), "Wrong LP constraint");
1609 ///\relates LpSolverBase::Constr
1611 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1612 const LpSolverBase::Value &n)
1614 LpSolverBase::Constr tmp(c);
1615 LEMON_ASSERT(LpSolverBase::isNaN(tmp.upperBound()), "Wrong LP constraint");
1622 ///\relates LpSolverBase::Constr
1624 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1625 const LpSolverBase::Constr&c)
1627 LpSolverBase::Constr tmp(c);
1628 LEMON_ASSERT(LpSolverBase::isNaN(tmp.upperBound()), "Wrong LP constraint");
1634 ///\relates LpSolverBase::Constr
1636 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1637 const LpSolverBase::Value &n)
1639 LpSolverBase::Constr tmp(c);
1640 LEMON_ASSERT(LpSolverBase::isNaN(tmp.lowerBound()), "Wrong LP constraint");
1647 ///\relates LpSolverBase::DualExpr
1649 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1650 const LpSolverBase::DualExpr &b)
1652 LpSolverBase::DualExpr tmp(a);
1658 ///\relates LpSolverBase::DualExpr
1660 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1661 const LpSolverBase::DualExpr &b)
1663 LpSolverBase::DualExpr tmp(a);
1669 ///\relates LpSolverBase::DualExpr
1671 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1672 const LpSolverBase::Value &b)
1674 LpSolverBase::DualExpr tmp(a);
1681 ///\relates LpSolverBase::DualExpr
1683 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1684 const LpSolverBase::DualExpr &b)
1686 LpSolverBase::DualExpr tmp(b);
1692 ///\relates LpSolverBase::DualExpr
1694 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1695 const LpSolverBase::Value &b)
1697 LpSolverBase::DualExpr tmp(a);
1705 #endif //LEMON_LP_BASE_H