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/error.h>
29 #include<lemon/bits/invalid.h>
30 #include<lemon/bits/utility.h>
31 #include<lemon/bits/lp_id.h>
34 ///\brief The interface of the LP solver interface.
38 /// Function to decide whether a floating point value is finite or not.
40 /// Retruns true if the argument is not infinity, minus infinity or NaN.
41 /// It does the same as the isfinite() function defined by C99.
43 bool isFinite(T value)
45 typedef std::numeric_limits<T> Lim;
46 if ((Lim::has_infinity && (value == Lim::infinity() || value ==
48 ((Lim::has_quiet_NaN || Lim::has_signaling_NaN) && value != value))
55 ///Common base class for LP solvers
57 ///\todo Much more docs
68 ///Possible outcomes of an LP solving procedure
69 enum SolveExitStatus {
70 ///This means that the problem has been successfully solved: either
71 ///an optimal solution has been found or infeasibility/unboundedness
74 ///Any other case (including the case when some user specified
75 ///limit has been exceeded)
81 ///Feasible solution hasn't been found (but may exist).
83 ///\todo NOTFOUND might be a better name.
86 ///The problem has no feasible solution
88 ///Feasible solution found
90 ///Optimal solution exists and found
92 ///The cost function is unbounded
94 ///\todo Give a feasible solution and an infinite ray (and the
95 ///corresponding bases)
99 ///\e The type of the investigated LP problem
101 ///Primal-dual feasible
102 PRIMAL_DUAL_FEASIBLE = 0,
103 ///Primal feasible dual infeasible
104 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
105 ///Primal infeasible dual feasible
106 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
107 ///Primal-dual infeasible
108 PRIMAL_DUAL_INFEASIBLE = 3,
109 ///Could not determine so far
113 ///The floating point type used by the solver
114 typedef double Value;
115 ///The infinity constant
116 static const Value INF;
117 ///The not a number constant
118 static const Value NaN;
120 static inline bool isNaN(const Value& v) { return v!=v; }
126 ///Refer to a column of the LP.
128 ///This type is used to refer to a column of the LP.
130 ///Its value remains valid and correct even after the addition or erase of
133 ///\todo Document what can one do with a Col (INVALID, comparing,
134 ///it is similar to Node/Edge)
138 friend class LpSolverBase;
139 friend class MipSolverBase;
140 explicit Col(int _id) : id(_id) {}
142 typedef Value ExprValue;
143 typedef True LpSolverCol;
145 Col(const Invalid&) : id(-1) {}
146 bool operator< (Col c) const {return id< c.id;}
147 bool operator> (Col c) const {return id> c.id;}
148 bool operator==(Col c) const {return id==c.id;}
149 bool operator!=(Col c) const {return id!=c.id;}
152 class ColIt : public Col {
153 const LpSolverBase *_lp;
156 ColIt(const LpSolverBase &lp) : _lp(&lp)
158 _lp->cols.firstFix(id);
160 ColIt(const Invalid&) : Col(INVALID) {}
163 _lp->cols.nextFix(id);
168 static int id(const Col& col) { return col.id; }
171 ///Refer to a row of the LP.
173 ///This type is used to refer to a row of the LP.
175 ///Its value remains valid and correct even after the addition or erase of
178 ///\todo Document what can one do with a Row (INVALID, comparing,
179 ///it is similar to Node/Edge)
183 friend class LpSolverBase;
184 explicit Row(int _id) : id(_id) {}
186 typedef Value ExprValue;
187 typedef True LpSolverRow;
189 Row(const Invalid&) : id(-1) {}
191 bool operator< (Row c) const {return id< c.id;}
192 bool operator> (Row c) const {return id> c.id;}
193 bool operator==(Row c) const {return id==c.id;}
194 bool operator!=(Row c) const {return id!=c.id;}
197 class RowIt : public Row {
198 const LpSolverBase *_lp;
201 RowIt(const LpSolverBase &lp) : _lp(&lp)
203 _lp->rows.firstFix(id);
205 RowIt(const Invalid&) : Row(INVALID) {}
208 _lp->rows.nextFix(id);
213 static int id(const Row& row) { return row.id; }
217 int _lpId(const Col& c) const {
218 return cols.floatingId(id(c));
221 int _lpId(const Row& r) const {
222 return rows.floatingId(id(r));
225 Col _item(int i, Col) const {
226 return Col(cols.fixId(i));
229 Row _item(int i, Row) const {
230 return Row(rows.fixId(i));
236 ///Linear expression of variables and a constant component
238 ///This data structure stores a linear expression of the variables
239 ///(\ref Col "Col"s) and also has a constant component.
241 ///There are several ways to access and modify the contents of this
243 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
244 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
245 ///read and modify the coefficients like
252 ///or you can also iterate through its elements.
255 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
258 ///(This code computes the sum of all coefficients).
259 ///- Numbers (<tt>double</tt>'s)
260 ///and variables (\ref Col "Col"s) directly convert to an
261 ///\ref Expr and the usual linear operations are defined, so
264 ///2*v-3.12*(v-w/2)+2
265 ///v*2.1+(3*v+(v*12+w+6)*3)/2
267 ///are valid \ref Expr "Expr"essions.
268 ///The usual assignment operations are also defined.
271 ///e+=2*v-3.12*(v-w/2)+2;
275 ///- The constant member can be set and read by \ref constComp()
278 ///double c=e.constComp();
281 ///\note \ref clear() not only sets all coefficients to 0 but also
282 ///clears the constant components.
286 class Expr : public std::map<Col,Value>
289 typedef LpSolverBase::Col Key;
290 typedef LpSolverBase::Value Value;
293 typedef std::map<Col,Value> Base;
297 typedef True IsLinExpression;
299 Expr() : Base(), const_comp(0) { }
301 Expr(const Key &v) : const_comp(0) {
302 Base::insert(std::make_pair(v, 1));
305 Expr(const Value &v) : const_comp(v) {}
307 void set(const Key &v,const Value &c) {
308 Base::insert(std::make_pair(v, c));
311 Value &constComp() { return const_comp; }
313 const Value &constComp() const { return const_comp; }
315 ///Removes the components with zero coefficient.
317 for (Base::iterator i=Base::begin(); i!=Base::end();) {
320 if ((*i).second==0) Base::erase(i);
325 void simplify() const {
326 const_cast<Expr*>(this)->simplify();
329 ///Removes the coefficients closer to zero than \c tolerance.
330 void simplify(double &tolerance) {
331 for (Base::iterator i=Base::begin(); i!=Base::end();) {
334 if (std::fabs((*i).second)<tolerance) Base::erase(i);
339 ///Sets all coefficients and the constant component to 0.
346 Expr &operator+=(const Expr &e) {
347 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
348 (*this)[j->first]+=j->second;
349 const_comp+=e.const_comp;
353 Expr &operator-=(const Expr &e) {
354 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
355 (*this)[j->first]-=j->second;
356 const_comp-=e.const_comp;
360 Expr &operator*=(const Value &c) {
361 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
367 Expr &operator/=(const Value &c) {
368 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
378 ///This data stucture represents a linear constraint in the LP.
379 ///Basically it is a linear expression with a lower or an upper bound
380 ///(or both). These parts of the constraint can be obtained by the member
381 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
383 ///There are two ways to construct a constraint.
384 ///- You can set the linear expression and the bounds directly
385 /// by the functions above.
386 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
387 /// are defined between expressions, or even between constraints whenever
388 /// it makes sense. Therefore if \c e and \c f are linear expressions and
389 /// \c s and \c t are numbers, then the followings are valid expressions
390 /// and thus they can be used directly e.g. in \ref addRow() whenever
399 ///\warning The validity of a constraint is checked only at run time, so
400 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
401 ///\ref LogicError exception.
405 typedef LpSolverBase::Expr Expr;
406 typedef Expr::Key Key;
407 typedef Expr::Value Value;
414 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
416 Constr(Value lb,const Expr &e,Value ub) :
417 _expr(e), _lb(lb), _ub(ub) {}
419 Constr(const Expr &e,Value ub) :
420 _expr(e), _lb(NaN), _ub(ub) {}
422 Constr(Value lb,const Expr &e) :
423 _expr(e), _lb(lb), _ub(NaN) {}
425 Constr(const Expr &e) :
426 _expr(e), _lb(NaN), _ub(NaN) {}
434 ///Reference to the linear expression
435 Expr &expr() { return _expr; }
436 ///Cont reference to the linear expression
437 const Expr &expr() const { return _expr; }
438 ///Reference to the lower bound.
441 ///- \ref INF "INF": the constraint is lower unbounded.
442 ///- \ref NaN "NaN": lower bound has not been set.
443 ///- finite number: the lower bound
444 Value &lowerBound() { return _lb; }
445 ///The const version of \ref lowerBound()
446 const Value &lowerBound() const { return _lb; }
447 ///Reference to the upper bound.
450 ///- \ref INF "INF": the constraint is upper unbounded.
451 ///- \ref NaN "NaN": upper bound has not been set.
452 ///- finite number: the upper bound
453 Value &upperBound() { return _ub; }
454 ///The const version of \ref upperBound()
455 const Value &upperBound() const { return _ub; }
456 ///Is the constraint lower bounded?
457 bool lowerBounded() const {
458 return isFinite(_lb);
460 ///Is the constraint upper bounded?
461 bool upperBounded() const {
462 return isFinite(_ub);
467 ///Linear expression of rows
469 ///This data structure represents a column of the matrix,
470 ///thas is it strores a linear expression of the dual variables
471 ///(\ref Row "Row"s).
473 ///There are several ways to access and modify the contents of this
475 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
476 ///if \c e is an DualExpr and \c v
477 ///and \c w are of type \ref Row, then you can
478 ///read and modify the coefficients like
485 ///or you can also iterate through its elements.
488 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
491 ///(This code computes the sum of all coefficients).
492 ///- Numbers (<tt>double</tt>'s)
493 ///and variables (\ref Row "Row"s) directly convert to an
494 ///\ref DualExpr and the usual linear operations are defined, so
498 ///v*2.1+(3*v+(v*12+w)*3)/2
500 ///are valid \ref DualExpr "DualExpr"essions.
501 ///The usual assignment operations are also defined.
504 ///e+=2*v-3.12*(v-w/2);
511 class DualExpr : public std::map<Row,Value>
514 typedef LpSolverBase::Row Key;
515 typedef LpSolverBase::Value Value;
518 typedef std::map<Row,Value> Base;
521 typedef True IsLinExpression;
523 DualExpr() : Base() { }
525 DualExpr(const Key &v) {
526 Base::insert(std::make_pair(v, 1));
529 void set(const Key &v,const Value &c) {
530 Base::insert(std::make_pair(v, c));
533 ///Removes the components with zero coefficient.
535 for (Base::iterator i=Base::begin(); i!=Base::end();) {
538 if ((*i).second==0) Base::erase(i);
543 void simplify() const {
544 const_cast<DualExpr*>(this)->simplify();
547 ///Removes the coefficients closer to zero than \c tolerance.
548 void simplify(double &tolerance) {
549 for (Base::iterator i=Base::begin(); i!=Base::end();) {
552 if (std::fabs((*i).second)<tolerance) Base::erase(i);
557 ///Sets all coefficients to 0.
563 DualExpr &operator+=(const DualExpr &e) {
564 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
565 (*this)[j->first]+=j->second;
569 DualExpr &operator-=(const DualExpr &e) {
570 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
571 (*this)[j->first]-=j->second;
575 DualExpr &operator*=(const Value &c) {
576 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
581 DualExpr &operator/=(const Value &c) {
582 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
591 template <typename _Expr>
592 class MappedOutputIterator {
595 typedef std::insert_iterator<_Expr> Base;
597 typedef std::output_iterator_tag iterator_category;
598 typedef void difference_type;
599 typedef void value_type;
600 typedef void reference;
601 typedef void pointer;
603 MappedOutputIterator(const Base& _base, const LpSolverBase& _lp)
604 : base(_base), lp(_lp) {}
606 MappedOutputIterator& operator*() {
610 MappedOutputIterator& operator=(const std::pair<int, Value>& value) {
611 *base = std::make_pair(lp._item(value.first, typename _Expr::Key()),
616 MappedOutputIterator& operator++() {
621 MappedOutputIterator operator++(int) {
622 MappedOutputIterator tmp(*this);
627 bool operator==(const MappedOutputIterator& it) const {
628 return base == it.base;
631 bool operator!=(const MappedOutputIterator& it) const {
632 return base != it.base;
637 const LpSolverBase& lp;
640 template <typename Expr>
641 class MappedInputIterator {
644 typedef typename Expr::const_iterator Base;
646 typedef typename Base::iterator_category iterator_category;
647 typedef typename Base::difference_type difference_type;
648 typedef const std::pair<int, Value> value_type;
649 typedef value_type reference;
652 pointer(value_type& _value) : value(_value) {}
653 value_type* operator->() { return &value; }
658 MappedInputIterator(const Base& _base, const LpSolverBase& _lp)
659 : base(_base), lp(_lp) {}
661 reference operator*() {
662 return std::make_pair(lp._lpId(base->first), base->second);
665 pointer operator->() {
666 return pointer(operator*());
669 MappedInputIterator& operator++() {
674 MappedInputIterator operator++(int) {
675 MappedInputIterator tmp(*this);
680 bool operator==(const MappedInputIterator& it) const {
681 return base == it.base;
684 bool operator!=(const MappedInputIterator& it) const {
685 return base != it.base;
690 const LpSolverBase& lp;
695 /// STL compatible iterator for lp col
696 typedef MappedInputIterator<Expr> ConstRowIterator;
697 /// STL compatible iterator for lp row
698 typedef MappedInputIterator<DualExpr> ConstColIterator;
700 /// STL compatible iterator for lp col
701 typedef MappedOutputIterator<Expr> RowIterator;
702 /// STL compatible iterator for lp row
703 typedef MappedOutputIterator<DualExpr> ColIterator;
705 //Abstract virtual functions
706 virtual LpSolverBase* _newLp() = 0;
707 virtual LpSolverBase* _copyLp(){
708 LpSolverBase* newlp = _newLp();
710 std::map<Col, Col> ref;
711 for (LpSolverBase::ColIt it(*this); it != INVALID; ++it) {
712 Col ccol = newlp->addCol();
714 newlp->colName(ccol, colName(it));
715 newlp->colLowerBound(ccol, colLowerBound(it));
716 newlp->colUpperBound(ccol, colUpperBound(it));
719 for (LpSolverBase::RowIt it(*this); it != INVALID; ++it) {
720 Expr e = row(it), ce;
721 for (Expr::iterator jt = e.begin(); jt != e.end(); ++jt) {
722 ce[ref[jt->first]] = jt->second;
725 Row r = newlp->addRow(ce);
728 getRowBounds(it, lower, upper);
729 newlp->rowBounds(r, lower, upper);
735 virtual int _addCol() = 0;
736 virtual int _addRow() = 0;
738 virtual void _eraseCol(int col) = 0;
739 virtual void _eraseRow(int row) = 0;
741 virtual void _getColName(int col, std::string & name) const = 0;
742 virtual void _setColName(int col, const std::string & name) = 0;
743 virtual int _colByName(const std::string& name) const = 0;
745 virtual void _setRowCoeffs(int i, ConstRowIterator b,
746 ConstRowIterator e) = 0;
747 virtual void _getRowCoeffs(int i, RowIterator b) const = 0;
748 virtual void _setColCoeffs(int i, ConstColIterator b,
749 ConstColIterator e) = 0;
750 virtual void _getColCoeffs(int i, ColIterator b) const = 0;
751 virtual void _setCoeff(int row, int col, Value value) = 0;
752 virtual Value _getCoeff(int row, int col) const = 0;
753 virtual void _setColLowerBound(int i, Value value) = 0;
754 virtual Value _getColLowerBound(int i) const = 0;
755 virtual void _setColUpperBound(int i, Value value) = 0;
756 virtual Value _getColUpperBound(int i) const = 0;
757 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
758 virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0;
760 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
761 virtual Value _getObjCoeff(int i) const = 0;
762 virtual void _clearObj()=0;
764 virtual SolveExitStatus _solve() = 0;
765 virtual Value _getPrimal(int i) const = 0;
766 virtual Value _getDual(int i) const = 0;
767 virtual Value _getPrimalValue() const = 0;
768 virtual bool _isBasicCol(int i) const = 0;
769 virtual SolutionStatus _getPrimalStatus() const = 0;
770 virtual SolutionStatus _getDualStatus() const = 0;
771 virtual ProblemTypes _getProblemType() const = 0;
773 virtual void _setMax() = 0;
774 virtual void _setMin() = 0;
777 virtual bool _isMax() const = 0;
779 //Own protected stuff
781 //Constant component of the objective function
782 Value obj_const_comp;
787 LpSolverBase() : obj_const_comp(0) {}
790 virtual ~LpSolverBase() {}
792 ///Creates a new LP problem
793 LpSolverBase* newLp() {return _newLp();}
794 ///Makes a copy of the LP problem
795 LpSolverBase* copyLp() {return _copyLp();}
797 ///\name Build up and modify the LP
801 ///Add a new empty column (i.e a new variable) to the LP
802 Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;}
804 ///\brief Adds several new columns
805 ///(i.e a variables) at once
807 ///This magic function takes a container as its argument
808 ///and fills its elements
809 ///with new columns (i.e. variables)
811 ///- a standard STL compatible iterable container with
812 ///\ref Col as its \c values_type
815 ///std::vector<LpSolverBase::Col>
816 ///std::list<LpSolverBase::Col>
818 ///- a standard STL compatible iterable container with
819 ///\ref Col as its \c mapped_type
822 ///std::map<AnyType,LpSolverBase::Col>
824 ///- an iterable lemon \ref concepts::WriteMap "write map" like
826 ///ListGraph::NodeMap<LpSolverBase::Col>
827 ///ListGraph::EdgeMap<LpSolverBase::Col>
829 ///\return The number of the created column.
832 int addColSet(T &t) { return 0;}
835 typename enable_if<typename T::value_type::LpSolverCol,int>::type
836 addColSet(T &t,dummy<0> = 0) {
838 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
842 typename enable_if<typename T::value_type::second_type::LpSolverCol,
844 addColSet(T &t,dummy<1> = 1) {
846 for(typename T::iterator i=t.begin();i!=t.end();++i) {
853 typename enable_if<typename T::MapIt::Value::LpSolverCol,
855 addColSet(T &t,dummy<2> = 2) {
857 for(typename T::MapIt i(t); i!=INVALID; ++i)
866 ///Set a column (i.e a dual constraint) of the LP
868 ///\param c is the column to be modified
869 ///\param e is a dual linear expression (see \ref DualExpr)
871 void col(Col c,const DualExpr &e) {
873 _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this),
874 ConstColIterator(e.end(), *this));
877 ///Get a column (i.e a dual constraint) of the LP
879 ///\param r is the column to get
880 ///\return the dual expression associated to the column
881 DualExpr col(Col c) const {
883 _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this));
887 ///Add a new column to the LP
889 ///\param e is a dual linear expression (see \ref DualExpr)
890 ///\param obj is the corresponding component of the objective
891 ///function. It is 0 by default.
892 ///\return The created column.
893 Col addCol(const DualExpr &e, Value o = 0) {
900 ///Add a new empty row (i.e a new constraint) to the LP
902 ///This function adds a new empty row (i.e a new constraint) to the LP.
903 ///\return The created row
904 Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;}
906 ///\brief Add several new rows
907 ///(i.e a constraints) at once
909 ///This magic function takes a container as its argument
910 ///and fills its elements
911 ///with new row (i.e. variables)
913 ///- a standard STL compatible iterable container with
914 ///\ref Row as its \c values_type
917 ///std::vector<LpSolverBase::Row>
918 ///std::list<LpSolverBase::Row>
920 ///- a standard STL compatible iterable container with
921 ///\ref Row as its \c mapped_type
924 ///std::map<AnyType,LpSolverBase::Row>
926 ///- an iterable lemon \ref concepts::WriteMap "write map" like
928 ///ListGraph::NodeMap<LpSolverBase::Row>
929 ///ListGraph::EdgeMap<LpSolverBase::Row>
931 ///\return The number of rows created.
934 int addRowSet(T &t) { return 0;}
937 typename enable_if<typename T::value_type::LpSolverRow,int>::type
938 addRowSet(T &t,dummy<0> = 0) {
940 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
944 typename enable_if<typename T::value_type::second_type::LpSolverRow,
946 addRowSet(T &t,dummy<1> = 1) {
948 for(typename T::iterator i=t.begin();i!=t.end();++i) {
955 typename enable_if<typename T::MapIt::Value::LpSolverRow,
957 addRowSet(T &t,dummy<2> = 2) {
959 for(typename T::MapIt i(t); i!=INVALID; ++i)
968 ///Set a row (i.e a constraint) of the LP
970 ///\param r is the row to be modified
971 ///\param l is lower bound (-\ref INF means no bound)
972 ///\param e is a linear expression (see \ref Expr)
973 ///\param u is the upper bound (\ref INF means no bound)
974 ///\bug This is a temporary function. The interface will change to
976 ///\todo Option to control whether a constraint with a single variable is
978 void row(Row r, Value l, const Expr &e, Value u) {
980 _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this),
981 ConstRowIterator(e.end(), *this));
982 _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
985 ///Set a row (i.e a constraint) of the LP
987 ///\param r is the row to be modified
988 ///\param c is a linear expression (see \ref Constr)
989 void row(Row r, const Constr &c) {
990 row(r, c.lowerBounded()?c.lowerBound():-INF,
991 c.expr(), c.upperBounded()?c.upperBound():INF);
995 ///Get a row (i.e a constraint) of the LP
997 ///\param r is the row to get
998 ///\return the expression associated to the row
999 Expr row(Row r) const {
1001 _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this));
1005 ///Add a new row (i.e a new constraint) to the LP
1007 ///\param l is the lower bound (-\ref INF means no bound)
1008 ///\param e is a linear expression (see \ref Expr)
1009 ///\param u is the upper bound (\ref INF means no bound)
1010 ///\return The created row.
1011 ///\bug This is a temporary function. The interface will change to
1013 Row addRow(Value l,const Expr &e, Value u) {
1019 ///Add a new row (i.e a new constraint) to the LP
1021 ///\param c is a linear expression (see \ref Constr)
1022 ///\return The created row.
1023 Row addRow(const Constr &c) {
1028 ///Erase a coloumn (i.e a variable) from the LP
1030 ///\param c is the coloumn to be deleted
1031 ///\todo Please check this
1032 void eraseCol(Col c) {
1033 _eraseCol(_lpId(c));
1036 ///Erase a row (i.e a constraint) from the LP
1038 ///\param r is the row to be deleted
1039 ///\todo Please check this
1040 void eraseRow(Row r) {
1041 _eraseRow(_lpId(r));
1045 /// Get the name of a column
1047 ///\param c is the coresponding coloumn
1048 ///\return The name of the colunm
1049 std::string colName(Col c) const {
1051 _getColName(_lpId(c), name);
1055 /// Set the name of a column
1057 ///\param c is the coresponding coloumn
1058 ///\param name The name to be given
1059 void colName(Col c, const std::string& name) {
1060 _setColName(_lpId(c), name);
1063 /// Get the column by its name
1065 ///\param name The name of the column
1066 ///\return the proper column or \c INVALID
1067 Col colByName(const std::string& name) const {
1068 int k = _colByName(name);
1069 return k != -1 ? Col(cols.fixId(k)) : Col(INVALID);
1072 /// Set an element of the coefficient matrix of the LP
1074 ///\param r is the row of the element to be modified
1075 ///\param c is the coloumn of the element to be modified
1076 ///\param val is the new value of the coefficient
1078 void coeff(Row r, Col c, Value val) {
1079 _setCoeff(_lpId(r),_lpId(c), val);
1082 /// Get an element of the coefficient matrix of the LP
1084 ///\param r is the row of the element in question
1085 ///\param c is the coloumn of the element in question
1086 ///\return the corresponding coefficient
1088 Value coeff(Row r, Col c) const {
1089 return _getCoeff(_lpId(r),_lpId(c));
1092 /// Set the lower bound of a column (i.e a variable)
1094 /// The lower bound of a variable (column) has to be given by an
1095 /// extended number of type Value, i.e. a finite number of type
1096 /// Value or -\ref INF.
1097 void colLowerBound(Col c, Value value) {
1098 _setColLowerBound(_lpId(c),value);
1101 /// Get the lower bound of a column (i.e a variable)
1103 /// This function returns the lower bound for column (variable) \t c
1104 /// (this might be -\ref INF as well).
1105 ///\return The lower bound for coloumn \t c
1106 Value colLowerBound(Col c) const {
1107 return _getColLowerBound(_lpId(c));
1110 ///\brief Set the lower bound of several columns
1111 ///(i.e a variables) at once
1113 ///This magic function takes a container as its argument
1114 ///and applies the function on all of its elements.
1115 /// The lower bound of a variable (column) has to be given by an
1116 /// extended number of type Value, i.e. a finite number of type
1117 /// Value or -\ref INF.
1120 void colLowerBound(T &t, Value value) { return 0;}
1123 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1124 colLowerBound(T &t, Value value,dummy<0> = 0) {
1125 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1126 colLowerBound(*i, value);
1130 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1132 colLowerBound(T &t, Value value,dummy<1> = 1) {
1133 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1134 colLowerBound(i->second, value);
1138 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1140 colLowerBound(T &t, Value value,dummy<2> = 2) {
1141 for(typename T::MapIt i(t); i!=INVALID; ++i){
1142 colLowerBound(*i, value);
1147 /// Set the upper bound of a column (i.e a variable)
1149 /// The upper bound of a variable (column) has to be given by an
1150 /// extended number of type Value, i.e. a finite number of type
1151 /// Value or \ref INF.
1152 void colUpperBound(Col c, Value value) {
1153 _setColUpperBound(_lpId(c),value);
1156 /// Get the upper bound of a column (i.e a variable)
1158 /// This function returns the upper bound for column (variable) \t c
1159 /// (this might be \ref INF as well).
1160 ///\return The upper bound for coloumn \t c
1161 Value colUpperBound(Col c) const {
1162 return _getColUpperBound(_lpId(c));
1165 ///\brief Set the upper bound of several columns
1166 ///(i.e a variables) at once
1168 ///This magic function takes a container as its argument
1169 ///and applies the function on all of its elements.
1170 /// The upper bound of a variable (column) has to be given by an
1171 /// extended number of type Value, i.e. a finite number of type
1172 /// Value or \ref INF.
1175 void colUpperBound(T &t, Value value) { return 0;}
1178 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1179 colUpperBound(T &t, Value value,dummy<0> = 0) {
1180 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1181 colUpperBound(*i, value);
1185 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1187 colUpperBound(T &t, Value value,dummy<1> = 1) {
1188 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1189 colUpperBound(i->second, value);
1193 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1195 colUpperBound(T &t, Value value,dummy<2> = 2) {
1196 for(typename T::MapIt i(t); i!=INVALID; ++i){
1197 colUpperBound(*i, value);
1202 /// Set the lower and the upper bounds of a column (i.e a variable)
1204 /// The lower and the upper bounds of
1205 /// a variable (column) have to be given by an
1206 /// extended number of type Value, i.e. a finite number of type
1207 /// Value, -\ref INF or \ref INF.
1208 void colBounds(Col c, Value lower, Value upper) {
1209 _setColLowerBound(_lpId(c),lower);
1210 _setColUpperBound(_lpId(c),upper);
1213 ///\brief Set the lower and the upper bound of several columns
1214 ///(i.e a variables) at once
1216 ///This magic function takes a container as its argument
1217 ///and applies the function on all of its elements.
1218 /// The lower and the upper bounds of
1219 /// a variable (column) have to be given by an
1220 /// extended number of type Value, i.e. a finite number of type
1221 /// Value, -\ref INF or \ref INF.
1224 void colBounds(T &t, Value lower, Value upper) { return 0;}
1227 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1228 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1229 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1230 colBounds(*i, lower, upper);
1234 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1236 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1237 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1238 colBounds(i->second, lower, upper);
1242 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1244 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1245 for(typename T::MapIt i(t); i!=INVALID; ++i){
1246 colBounds(*i, lower, upper);
1252 /// Set the lower and the upper bounds of a row (i.e a constraint)
1254 /// The lower and the upper bound of a constraint (row) have to be
1255 /// given by an extended number of type Value, i.e. a finite
1256 /// number of type Value, -\ref INF or \ref INF. There is no
1257 /// separate function for the lower and the upper bound because
1258 /// that would have been hard to implement for CPLEX.
1259 void rowBounds(Row c, Value lower, Value upper) {
1260 _setRowBounds(_lpId(c),lower, upper);
1263 /// Get the lower and the upper bounds of a row (i.e a constraint)
1265 /// The lower and the upper bound of
1266 /// a constraint (row) are
1267 /// extended numbers of type Value, i.e. finite numbers of type
1268 /// Value, -\ref INF or \ref INF.
1269 /// \todo There is no separate function for the
1270 /// lower and the upper bound because we had problems with the
1271 /// implementation of the setting functions for CPLEX:
1272 /// check out whether this can be done for these functions.
1273 void getRowBounds(Row c, Value &lower, Value &upper) const {
1274 _getRowBounds(_lpId(c),lower, upper);
1277 ///Set an element of the objective function
1278 void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
1280 ///Get an element of the objective function
1281 Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); };
1283 ///Set the objective function
1285 ///\param e is a linear expression of type \ref Expr.
1288 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1289 objCoeff((*i).first,(*i).second);
1290 obj_const_comp=e.constComp();
1293 ///Get the objective function
1295 ///\return the objective function as a linear expression of type \ref Expr.
1298 for (ColIt it(*this); it != INVALID; ++it) {
1299 double c = objCoeff(it);
1301 e.insert(std::make_pair(it, c));
1309 void max() { _setMax(); }
1311 void min() { _setMin(); }
1313 ///Query function: is this a maximization problem?
1314 bool isMax() const {return _isMax(); }
1316 ///Query function: is this a minimization problem?
1317 bool isMin() const {return !isMax(); }
1322 ///\name Solve the LP
1326 ///\e Solve the LP problem at hand
1328 ///\return The result of the optimization procedure. Possible
1329 ///values and their meanings can be found in the documentation of
1330 ///\ref SolveExitStatus.
1332 ///\todo Which method is used to solve the problem
1333 SolveExitStatus solve() { return _solve(); }
1337 ///\name Obtain the solution
1341 /// The status of the primal problem (the original LP problem)
1342 SolutionStatus primalStatus() const {
1343 return _getPrimalStatus();
1346 /// The status of the dual (of the original LP) problem
1347 SolutionStatus dualStatus() const {
1348 return _getDualStatus();
1351 ///The type of the original LP problem
1352 ProblemTypes problemType() const {
1353 return _getProblemType();
1357 Value primal(Col c) const { return _getPrimal(_lpId(c)); }
1359 Value primal(const Expr& e) const {
1360 double res = e.constComp();
1361 for (std::map<Col, double>::const_iterator it = e.begin();
1362 it != e.end(); ++it) {
1363 res += _getPrimal(_lpId(it->first)) * it->second;
1369 Value dual(Row r) const { return _getDual(_lpId(r)); }
1371 Value dual(const DualExpr& e) const {
1373 for (std::map<Row, double>::const_iterator it = e.begin();
1374 it != e.end(); ++it) {
1375 res += _getPrimal(_lpId(it->first)) * it->second;
1381 bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); }
1386 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1387 /// of the primal problem, depending on whether we minimize or maximize.
1388 ///- \ref NaN if no primal solution is found.
1389 ///- The (finite) objective value if an optimal solution is found.
1390 Value primalValue() const { return _getPrimalValue()+obj_const_comp;}
1396 /// \ingroup lp_group
1398 /// \brief Common base class for MIP solvers
1399 /// \todo Much more docs
1400 class MipSolverBase : virtual public LpSolverBase{
1403 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1405 ///Continuous variable
1409 ///Unfortunately, cplex 7.5 somewhere writes something like
1410 ///#define INTEGER 'I'
1412 ///\todo No support for other types yet.
1415 ///Sets the type of the given coloumn to the given type
1417 ///Sets the type of the given coloumn to the given type.
1418 void colType(Col c, ColTypes col_type) {
1419 _colType(_lpId(c),col_type);
1422 ///Gives back the type of the column.
1424 ///Gives back the type of the column.
1425 ColTypes colType(Col c) const {
1426 return _colType(_lpId(c));
1429 ///Sets the type of the given Col to integer or remove that property.
1431 ///Sets the type of the given Col to integer or remove that property.
1432 void integer(Col c, bool enable) {
1439 ///Gives back whether the type of the column is integer or not.
1441 ///Gives back the type of the column.
1442 ///\return true if the column has integer type and false if not.
1443 bool integer(Col c) const {
1444 return (colType(c)==INT);
1447 /// The status of the MIP problem
1448 SolutionStatus mipStatus() const {
1449 return _getMipStatus();
1454 virtual ColTypes _colType(int col) const = 0;
1455 virtual void _colType(int col, ColTypes col_type) = 0;
1456 virtual SolutionStatus _getMipStatus() const = 0;
1460 ///\relates LpSolverBase::Expr
1462 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1463 const LpSolverBase::Expr &b)
1465 LpSolverBase::Expr tmp(a);
1471 ///\relates LpSolverBase::Expr
1473 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1474 const LpSolverBase::Expr &b)
1476 LpSolverBase::Expr tmp(a);
1482 ///\relates LpSolverBase::Expr
1484 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1485 const LpSolverBase::Value &b)
1487 LpSolverBase::Expr tmp(a);
1494 ///\relates LpSolverBase::Expr
1496 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1497 const LpSolverBase::Expr &b)
1499 LpSolverBase::Expr tmp(b);
1505 ///\relates LpSolverBase::Expr
1507 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1508 const LpSolverBase::Value &b)
1510 LpSolverBase::Expr tmp(a);
1517 ///\relates LpSolverBase::Constr
1519 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1520 const LpSolverBase::Expr &f)
1522 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1527 ///\relates LpSolverBase::Constr
1529 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1530 const LpSolverBase::Expr &f)
1532 return LpSolverBase::Constr(e,f);
1537 ///\relates LpSolverBase::Constr
1539 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1540 const LpSolverBase::Value &f)
1542 return LpSolverBase::Constr(-LpSolverBase::INF,e,f);
1547 ///\relates LpSolverBase::Constr
1549 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1550 const LpSolverBase::Expr &f)
1552 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1558 ///\relates LpSolverBase::Constr
1560 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1561 const LpSolverBase::Expr &f)
1563 return LpSolverBase::Constr(f,e);
1569 ///\relates LpSolverBase::Constr
1571 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1572 const LpSolverBase::Value &f)
1574 return LpSolverBase::Constr(f,e,LpSolverBase::INF);
1579 ///\relates LpSolverBase::Constr
1581 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1582 const LpSolverBase::Value &f)
1584 return LpSolverBase::Constr(f,e,f);
1589 ///\relates LpSolverBase::Constr
1591 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1592 const LpSolverBase::Expr &f)
1594 return LpSolverBase::Constr(0,e-f,0);
1599 ///\relates LpSolverBase::Constr
1601 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1602 const LpSolverBase::Constr&c)
1604 LpSolverBase::Constr tmp(c);
1605 ///\todo Create an own exception type.
1606 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1607 else tmp.lowerBound()=n;
1612 ///\relates LpSolverBase::Constr
1614 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1615 const LpSolverBase::Value &n)
1617 LpSolverBase::Constr tmp(c);
1618 ///\todo Create an own exception type.
1619 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1620 else tmp.upperBound()=n;
1626 ///\relates LpSolverBase::Constr
1628 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1629 const LpSolverBase::Constr&c)
1631 LpSolverBase::Constr tmp(c);
1632 ///\todo Create an own exception type.
1633 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1634 else tmp.upperBound()=n;
1639 ///\relates LpSolverBase::Constr
1641 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1642 const LpSolverBase::Value &n)
1644 LpSolverBase::Constr tmp(c);
1645 ///\todo Create an own exception type.
1646 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1647 else tmp.lowerBound()=n;
1653 ///\relates LpSolverBase::DualExpr
1655 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1656 const LpSolverBase::DualExpr &b)
1658 LpSolverBase::DualExpr tmp(a);
1664 ///\relates LpSolverBase::DualExpr
1666 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1667 const LpSolverBase::DualExpr &b)
1669 LpSolverBase::DualExpr tmp(a);
1675 ///\relates LpSolverBase::DualExpr
1677 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1678 const LpSolverBase::Value &b)
1680 LpSolverBase::DualExpr tmp(a);
1687 ///\relates LpSolverBase::DualExpr
1689 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1690 const LpSolverBase::DualExpr &b)
1692 LpSolverBase::DualExpr tmp(b);
1698 ///\relates LpSolverBase::DualExpr
1700 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1701 const LpSolverBase::Value &b)
1703 LpSolverBase::DualExpr tmp(a);
1711 #endif //LEMON_LP_BASE_H