3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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
30 #include<lemon/error.h>
31 #include<lemon/bits/invalid.h>
32 #include<lemon/bits/utility.h>
33 #include<lemon/bits/lp_id.h>
36 ///\brief The interface of the LP solver interface.
37 ///\ingroup gen_opt_group
40 ///Common base class for LP solvers
42 ///\todo Much more docs
43 ///\ingroup gen_opt_group
53 ///Possible outcomes of an LP solving procedure
54 enum SolveExitStatus {
55 ///This means that the problem has been successfully solved: either
56 ///an optimal solution has been found or infeasibility/unboundedness
59 ///Any other case (including the case when some user specified
60 ///limit has been exceeded)
66 ///Feasible solution hasn't been found (but may exist).
68 ///\todo NOTFOUND might be a better name.
71 ///The problem has no feasible solution
73 ///Feasible solution found
75 ///Optimal solution exists and found
77 ///The cost function is unbounded
79 ///\todo Give a feasible solution and an infinite ray (and the
80 ///corresponding bases)
84 ///\e The type of the investigated LP problem
86 ///Primal-dual feasible
87 PRIMAL_DUAL_FEASIBLE = 0,
88 ///Primal feasible dual infeasible
89 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
90 ///Primal infeasible dual feasible
91 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
92 ///Primal-dual infeasible
93 PRIMAL_DUAL_INFEASIBLE = 3,
94 ///Could not determine so far
98 ///The floating point type used by the solver
100 ///The infinity constant
101 static const Value INF;
102 ///The not a number constant
103 static const Value NaN;
105 static inline bool isNaN(const Value& v) { return v!=v; }
111 ///Refer to a column of the LP.
113 ///This type is used to refer to a column of the LP.
115 ///Its value remains valid and correct even after the addition or erase of
118 ///\todo Document what can one do with a Col (INVALID, comparing,
119 ///it is similar to Node/Edge)
123 friend class LpSolverBase;
124 friend class MipSolverBase;
125 explicit Col(int _id) : id(_id) {}
127 typedef Value ExprValue;
128 typedef True LpSolverCol;
130 Col(const Invalid&) : id(-1) {}
131 bool operator< (Col c) const {return id< c.id;}
132 bool operator> (Col c) const {return id> c.id;}
133 bool operator==(Col c) const {return id==c.id;}
134 bool operator!=(Col c) const {return id!=c.id;}
137 class ColIt : public Col {
138 const LpSolverBase *_lp;
141 ColIt(const LpSolverBase &lp) : _lp(&lp)
143 _lp->cols.firstFix(id);
145 ColIt(const Invalid&) : Col(INVALID) {}
148 _lp->cols.nextFix(id);
153 static int id(const Col& col) { return col.id; }
156 ///Refer to a row of the LP.
158 ///This type is used to refer to a row of the LP.
160 ///Its value remains valid and correct even after the addition or erase of
163 ///\todo Document what can one do with a Row (INVALID, comparing,
164 ///it is similar to Node/Edge)
168 friend class LpSolverBase;
169 explicit Row(int _id) : id(_id) {}
171 typedef Value ExprValue;
172 typedef True LpSolverRow;
174 Row(const Invalid&) : id(-1) {}
176 bool operator< (Row c) const {return id< c.id;}
177 bool operator> (Row c) const {return id> c.id;}
178 bool operator==(Row c) const {return id==c.id;}
179 bool operator!=(Row c) const {return id!=c.id;}
182 class RowIt : public Row {
183 const LpSolverBase *_lp;
186 RowIt(const LpSolverBase &lp) : _lp(&lp)
188 _lp->rows.firstFix(id);
190 RowIt(const Invalid&) : Row(INVALID) {}
193 _lp->rows.nextFix(id);
198 static int id(const Row& row) { return row.id; }
202 int _lpId(const Col& col) const {
203 return cols.floatingId(id(col));
206 int _lpId(const Row& row) const {
207 return rows.floatingId(id(row));
210 Col _item(int id, Col) const {
211 return Col(cols.fixId(id));
214 Row _item(int id, Row) const {
215 return Row(rows.fixId(id));
221 ///Linear expression of variables and a constant component
223 ///This data structure stores a linear expression of the variables
224 ///(\ref Col "Col"s) and also has a constant component.
226 ///There are several ways to access and modify the contents of this
228 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
229 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
230 ///read and modify the coefficients like
237 ///or you can also iterate through its elements.
240 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
243 ///(This code computes the sum of all coefficients).
244 ///- Numbers (<tt>double</tt>'s)
245 ///and variables (\ref Col "Col"s) directly convert to an
246 ///\ref Expr and the usual linear operations are defined, so
249 ///2*v-3.12*(v-w/2)+2
250 ///v*2.1+(3*v+(v*12+w+6)*3)/2
252 ///are valid \ref Expr "Expr"essions.
253 ///The usual assignment operations are also defined.
256 ///e+=2*v-3.12*(v-w/2)+2;
260 ///- The constant member can be set and read by \ref constComp()
263 ///double c=e.constComp();
266 ///\note \ref clear() not only sets all coefficients to 0 but also
267 ///clears the constant components.
271 class Expr : public std::map<Col,Value>
274 typedef LpSolverBase::Col Key;
275 typedef LpSolverBase::Value Value;
278 typedef std::map<Col,Value> Base;
282 typedef True IsLinExpression;
284 Expr() : Base(), const_comp(0) { }
286 Expr(const Key &v) : const_comp(0) {
287 Base::insert(std::make_pair(v, 1));
290 Expr(const Value &v) : const_comp(v) {}
292 void set(const Key &v,const Value &c) {
293 Base::insert(std::make_pair(v, c));
296 Value &constComp() { return const_comp; }
298 const Value &constComp() const { return const_comp; }
300 ///Removes the components with zero coefficient.
302 for (Base::iterator i=Base::begin(); i!=Base::end();) {
305 if ((*i).second==0) Base::erase(i);
310 void simplify() const {
311 const_cast<Expr*>(this)->simplify();
314 ///Removes the coefficients closer to zero than \c tolerance.
315 void simplify(double &tolerance) {
316 for (Base::iterator i=Base::begin(); i!=Base::end();) {
319 if (std::fabs((*i).second)<tolerance) Base::erase(i);
324 ///Sets all coefficients and the constant component to 0.
331 Expr &operator+=(const Expr &e) {
332 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
333 (*this)[j->first]+=j->second;
334 const_comp+=e.const_comp;
338 Expr &operator-=(const Expr &e) {
339 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
340 (*this)[j->first]-=j->second;
341 const_comp-=e.const_comp;
345 Expr &operator*=(const Value &c) {
346 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
352 Expr &operator/=(const Value &c) {
353 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
360 void prettyPrint(std::ostream &os) {
361 //std::fmtflags os.flags();
362 //os.setf(std::ios::showpos);
363 Base::iterator j=Base::begin();
365 os<<j->second<<"*x["<<id(j->first)<<"]";
367 for (; j!=Base::end(); ++j){
370 os<<j->second<<"*x["<<id(j->first)<<"]";
372 //Nem valami korrekt, de nem talaltam meg, hogy kell
373 //os.unsetf(std::ios::showpos);
382 ///This data stucture represents a linear constraint in the LP.
383 ///Basically it is a linear expression with a lower or an upper bound
384 ///(or both). These parts of the constraint can be obtained by the member
385 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
387 ///There are two ways to construct a constraint.
388 ///- You can set the linear expression and the bounds directly
389 /// by the functions above.
390 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
391 /// are defined between expressions, or even between constraints whenever
392 /// it makes sense. Therefore if \c e and \c f are linear expressions and
393 /// \c s and \c t are numbers, then the followings are valid expressions
394 /// and thus they can be used directly e.g. in \ref addRow() whenever
403 ///\warning The validity of a constraint is checked only at run time, so
404 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
405 ///\ref LogicError exception.
409 typedef LpSolverBase::Expr Expr;
410 typedef Expr::Key Key;
411 typedef Expr::Value Value;
418 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
420 Constr(Value lb,const Expr &e,Value ub) :
421 _expr(e), _lb(lb), _ub(ub) {}
423 Constr(const Expr &e,Value ub) :
424 _expr(e), _lb(NaN), _ub(ub) {}
426 Constr(Value lb,const Expr &e) :
427 _expr(e), _lb(lb), _ub(NaN) {}
429 Constr(const Expr &e) :
430 _expr(e), _lb(NaN), _ub(NaN) {}
438 ///Reference to the linear expression
439 Expr &expr() { return _expr; }
440 ///Cont reference to the linear expression
441 const Expr &expr() const { return _expr; }
442 ///Reference to the lower bound.
445 ///- \ref INF "INF": the constraint is lower unbounded.
446 ///- \ref NaN "NaN": lower bound has not been set.
447 ///- finite number: the lower bound
448 Value &lowerBound() { return _lb; }
449 ///The const version of \ref lowerBound()
450 const Value &lowerBound() const { return _lb; }
451 ///Reference to the upper bound.
454 ///- \ref INF "INF": the constraint is upper unbounded.
455 ///- \ref NaN "NaN": upper bound has not been set.
456 ///- finite number: the upper bound
457 Value &upperBound() { return _ub; }
458 ///The const version of \ref upperBound()
459 const Value &upperBound() const { return _ub; }
460 ///Is the constraint lower bounded?
461 bool lowerBounded() const {
465 ///Is the constraint upper bounded?
466 bool upperBounded() const {
471 void prettyPrint(std::ostream &os) {
472 if (_lb==-LpSolverBase::INF||isNaN(_lb))
476 _expr.prettyPrint(os);
477 if (_ub==LpSolverBase::INF)
486 ///Linear expression of rows
488 ///This data structure represents a column of the matrix,
489 ///thas is it strores a linear expression of the dual variables
490 ///(\ref Row "Row"s).
492 ///There are several ways to access and modify the contents of this
494 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
495 ///if \c e is an DualExpr and \c v
496 ///and \c w are of type \ref Row, then you can
497 ///read and modify the coefficients like
504 ///or you can also iterate through its elements.
507 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
510 ///(This code computes the sum of all coefficients).
511 ///- Numbers (<tt>double</tt>'s)
512 ///and variables (\ref Row "Row"s) directly convert to an
513 ///\ref DualExpr and the usual linear operations are defined, so
517 ///v*2.1+(3*v+(v*12+w)*3)/2
519 ///are valid \ref DualExpr "DualExpr"essions.
520 ///The usual assignment operations are also defined.
523 ///e+=2*v-3.12*(v-w/2);
530 class DualExpr : public std::map<Row,Value>
533 typedef LpSolverBase::Row Key;
534 typedef LpSolverBase::Value Value;
537 typedef std::map<Row,Value> Base;
540 typedef True IsLinExpression;
542 DualExpr() : Base() { }
544 DualExpr(const Key &v) {
545 Base::insert(std::make_pair(v, 1));
548 void set(const Key &v,const Value &c) {
549 Base::insert(std::make_pair(v, c));
552 ///Removes the components with zero coefficient.
554 for (Base::iterator i=Base::begin(); i!=Base::end();) {
557 if ((*i).second==0) Base::erase(i);
562 void simplify() const {
563 const_cast<DualExpr*>(this)->simplify();
566 ///Removes the coefficients closer to zero than \c tolerance.
567 void simplify(double &tolerance) {
568 for (Base::iterator i=Base::begin(); i!=Base::end();) {
571 if (std::fabs((*i).second)<tolerance) Base::erase(i);
576 ///Sets all coefficients to 0.
582 DualExpr &operator+=(const DualExpr &e) {
583 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
584 (*this)[j->first]+=j->second;
588 DualExpr &operator-=(const DualExpr &e) {
589 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
590 (*this)[j->first]-=j->second;
594 DualExpr &operator*=(const Value &c) {
595 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
600 DualExpr &operator/=(const Value &c) {
601 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
610 template <typename _Expr>
611 class MappedOutputIterator {
614 typedef std::insert_iterator<_Expr> Base;
616 typedef std::output_iterator_tag iterator_category;
617 typedef void difference_type;
618 typedef void value_type;
619 typedef void reference;
620 typedef void pointer;
622 MappedOutputIterator(const Base& _base, const LpSolverBase& _lp)
623 : base(_base), lp(_lp) {}
625 MappedOutputIterator& operator*() {
629 MappedOutputIterator& operator=(const std::pair<int, Value>& value) {
630 *base = std::make_pair(lp._item(value.first, typename _Expr::Key()),
635 MappedOutputIterator& operator++() {
640 MappedOutputIterator operator++(int) {
641 MappedOutputIterator tmp(*this);
646 bool operator==(const MappedOutputIterator& it) const {
647 return base == it.base;
650 bool operator!=(const MappedOutputIterator& it) const {
651 return base != it.base;
656 const LpSolverBase& lp;
659 template <typename Expr>
660 class MappedInputIterator {
663 typedef typename Expr::const_iterator Base;
665 typedef typename Base::iterator_category iterator_category;
666 typedef typename Base::difference_type difference_type;
667 typedef const std::pair<int, Value> value_type;
668 typedef value_type reference;
671 pointer(value_type& _value) : value(_value) {}
672 value_type* operator->() { return &value; }
677 MappedInputIterator(const Base& _base, const LpSolverBase& _lp)
678 : base(_base), lp(_lp) {}
680 reference operator*() {
681 return std::make_pair(lp._lpId(base->first), base->second);
684 pointer operator->() {
685 return pointer(operator*());
688 MappedInputIterator& operator++() {
693 MappedInputIterator operator++(int) {
694 MappedInputIterator tmp(*this);
699 bool operator==(const MappedInputIterator& it) const {
700 return base == it.base;
703 bool operator!=(const MappedInputIterator& it) const {
704 return base != it.base;
709 const LpSolverBase& lp;
714 /// STL compatible iterator for lp col
715 typedef MappedInputIterator<Expr> ConstRowIterator;
716 /// STL compatible iterator for lp row
717 typedef MappedInputIterator<DualExpr> ConstColIterator;
719 /// STL compatible iterator for lp col
720 typedef MappedOutputIterator<Expr> RowIterator;
721 /// STL compatible iterator for lp row
722 typedef MappedOutputIterator<DualExpr> ColIterator;
724 //Abstract virtual functions
725 virtual LpSolverBase &_newLp() = 0;
726 virtual LpSolverBase &_copyLp(){
727 ///\todo This should be implemented here, too, when we have
728 ///problem retrieving routines. It can be overriden.
731 LpSolverBase & newlp(_newLp());
733 //return *(LpSolverBase*)0;
736 virtual int _addCol() = 0;
737 virtual int _addRow() = 0;
739 virtual void _eraseCol(int col) = 0;
740 virtual void _eraseRow(int row) = 0;
742 virtual void _getColName(int col, std::string & name) const = 0;
743 virtual void _setColName(int col, const std::string & name) = 0;
744 virtual int _colByName(const std::string& name) const = 0;
746 virtual void _setRowCoeffs(int i, ConstRowIterator b,
747 ConstRowIterator e) = 0;
748 virtual void _getRowCoeffs(int i, RowIterator b) const = 0;
749 virtual void _setColCoeffs(int i, ConstColIterator b,
750 ConstColIterator e) = 0;
751 virtual void _getColCoeffs(int i, ColIterator b) const = 0;
752 virtual void _setCoeff(int row, int col, Value value) = 0;
753 virtual Value _getCoeff(int row, int col) const = 0;
754 virtual void _setColLowerBound(int i, Value value) = 0;
755 virtual Value _getColLowerBound(int i) const = 0;
756 virtual void _setColUpperBound(int i, Value value) = 0;
757 virtual Value _getColUpperBound(int i) const = 0;
758 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
759 virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0;
761 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
762 virtual Value _getObjCoeff(int i) const = 0;
763 virtual void _clearObj()=0;
765 virtual SolveExitStatus _solve() = 0;
766 virtual Value _getPrimal(int i) const = 0;
767 virtual Value _getDual(int i) const = 0;
768 virtual Value _getPrimalValue() const = 0;
769 virtual bool _isBasicCol(int i) const = 0;
770 virtual SolutionStatus _getPrimalStatus() const = 0;
771 virtual SolutionStatus _getDualStatus() const = 0;
772 virtual ProblemTypes _getProblemType() const = 0;
774 virtual void _setMax() = 0;
775 virtual void _setMin() = 0;
778 virtual bool _isMax() const = 0;
780 //Own protected stuff
782 //Constant component of the objective function
783 Value obj_const_comp;
788 LpSolverBase() : obj_const_comp(0) {}
791 virtual ~LpSolverBase() {}
793 ///Creates a new LP problem
794 LpSolverBase &newLp() {return _newLp();}
795 ///Makes a copy of the LP problem
796 LpSolverBase ©Lp() {return _copyLp();}
798 ///\name Build up and modify the LP
802 ///Add a new empty column (i.e a new variable) to the LP
803 Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;}
805 ///\brief Adds several new columns
806 ///(i.e a variables) at once
808 ///This magic function takes a container as its argument
809 ///and fills its elements
810 ///with new columns (i.e. variables)
812 ///- a standard STL compatible iterable container with
813 ///\ref Col as its \c values_type
816 ///std::vector<LpSolverBase::Col>
817 ///std::list<LpSolverBase::Col>
819 ///- a standard STL compatible iterable container with
820 ///\ref Col as its \c mapped_type
823 ///std::map<AnyType,LpSolverBase::Col>
825 ///- an iterable lemon \ref concepts::WriteMap "write map" like
827 ///ListGraph::NodeMap<LpSolverBase::Col>
828 ///ListGraph::EdgeMap<LpSolverBase::Col>
830 ///\return The number of the created column.
833 int addColSet(T &t) { return 0;}
836 typename enable_if<typename T::value_type::LpSolverCol,int>::type
837 addColSet(T &t,dummy<0> = 0) {
839 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
843 typename enable_if<typename T::value_type::second_type::LpSolverCol,
845 addColSet(T &t,dummy<1> = 1) {
847 for(typename T::iterator i=t.begin();i!=t.end();++i) {
854 typename enable_if<typename T::MapIt::Value::LpSolverCol,
856 addColSet(T &t,dummy<2> = 2) {
858 for(typename T::MapIt i(t); i!=INVALID; ++i)
867 ///Set a column (i.e a dual constraint) of the LP
869 ///\param c is the column to be modified
870 ///\param e is a dual linear expression (see \ref DualExpr)
872 void col(Col c,const DualExpr &e) {
874 _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this),
875 ConstColIterator(e.end(), *this));
878 ///Get a column (i.e a dual constraint) of the LP
880 ///\param r is the column to get
881 ///\return the dual expression associated to the column
882 DualExpr col(Col c) const {
884 _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this));
888 ///Add a new column to the LP
890 ///\param e is a dual linear expression (see \ref DualExpr)
891 ///\param obj is the corresponding component of the objective
892 ///function. It is 0 by default.
893 ///\return The created column.
894 Col addCol(const DualExpr &e, Value obj=0) {
901 ///Add a new empty row (i.e a new constraint) to the LP
903 ///This function adds a new empty row (i.e a new constraint) to the LP.
904 ///\return The created row
905 Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;}
907 ///\brief Add several new rows
908 ///(i.e a constraints) at once
910 ///This magic function takes a container as its argument
911 ///and fills its elements
912 ///with new row (i.e. variables)
914 ///- a standard STL compatible iterable container with
915 ///\ref Row as its \c values_type
918 ///std::vector<LpSolverBase::Row>
919 ///std::list<LpSolverBase::Row>
921 ///- a standard STL compatible iterable container with
922 ///\ref Row as its \c mapped_type
925 ///std::map<AnyType,LpSolverBase::Row>
927 ///- an iterable lemon \ref concepts::WriteMap "write map" like
929 ///ListGraph::NodeMap<LpSolverBase::Row>
930 ///ListGraph::EdgeMap<LpSolverBase::Row>
932 ///\return The number of rows created.
935 int addRowSet(T &t) { return 0;}
938 typename enable_if<typename T::value_type::LpSolverRow,int>::type
939 addRowSet(T &t,dummy<0> = 0) {
941 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
945 typename enable_if<typename T::value_type::second_type::LpSolverRow,
947 addRowSet(T &t,dummy<1> = 1) {
949 for(typename T::iterator i=t.begin();i!=t.end();++i) {
956 typename enable_if<typename T::MapIt::Value::LpSolverRow,
958 addRowSet(T &t,dummy<2> = 2) {
960 for(typename T::MapIt i(t); i!=INVALID; ++i)
969 ///Set a row (i.e a constraint) of the LP
971 ///\param r is the row to be modified
972 ///\param l is lower bound (-\ref INF means no bound)
973 ///\param e is a linear expression (see \ref Expr)
974 ///\param u is the upper bound (\ref INF means no bound)
975 ///\bug This is a temportary function. The interface will change to
977 ///\todo Option to control whether a constraint with a single variable is
979 void row(Row r, Value l, const Expr &e, Value u) {
981 _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this),
982 ConstRowIterator(e.end(), *this));
983 _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
986 ///Set a row (i.e a constraint) of the LP
988 ///\param r is the row to be modified
989 ///\param c is a linear expression (see \ref Constr)
990 void row(Row r, const Constr &c) {
991 row(r, c.lowerBounded()?c.lowerBound():-INF,
992 c.expr(), c.upperBounded()?c.upperBound():INF);
996 ///Get a row (i.e a constraint) of the LP
998 ///\param r is the row to get
999 ///\return the expression associated to the row
1000 Expr row(Row r) const {
1002 _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this));
1006 ///Add a new row (i.e a new constraint) to the LP
1008 ///\param l is the lower bound (-\ref INF means no bound)
1009 ///\param e is a linear expression (see \ref Expr)
1010 ///\param u is the upper bound (\ref INF means no bound)
1011 ///\return The created row.
1012 ///\bug This is a temportary function. The interface will change to
1014 Row addRow(Value l,const Expr &e, Value u) {
1020 ///Add a new row (i.e a new constraint) to the LP
1022 ///\param c is a linear expression (see \ref Constr)
1023 ///\return The created row.
1024 Row addRow(const Constr &c) {
1029 ///Erase a coloumn (i.e a variable) from the LP
1031 ///\param c is the coloumn to be deleted
1032 ///\todo Please check this
1033 void eraseCol(Col c) {
1034 _eraseCol(_lpId(c));
1037 ///Erase a row (i.e a constraint) from the LP
1039 ///\param r is the row to be deleted
1040 ///\todo Please check this
1041 void eraseRow(Row r) {
1042 _eraseRow(_lpId(r));
1046 /// Get the name of a column
1048 ///\param c is the coresponding coloumn
1049 ///\return The name of the colunm
1050 std::string colName(Col c) const {
1052 _getColName(_lpId(c), name);
1056 /// Set the name of a column
1058 ///\param c is the coresponding coloumn
1059 ///\param name The name to be given
1060 void colName(Col c, const std::string& name) {
1061 _setColName(_lpId(c), name);
1064 /// Set an element of the coefficient matrix of the LP
1066 ///\param r is the row of the element to be modified
1067 ///\param c is the coloumn of the element to be modified
1068 ///\param val is the new value of the coefficient
1070 void coeff(Row r, Col c, Value val) {
1071 _setCoeff(_lpId(r),_lpId(c), val);
1074 /// Get an element of the coefficient matrix of the LP
1076 ///\param r is the row of the element in question
1077 ///\param c is the coloumn of the element in question
1078 ///\return the corresponding coefficient
1080 Value coeff(Row r, Col c) const {
1081 return _getCoeff(_lpId(r),_lpId(c));
1084 /// Set the lower bound of a column (i.e a variable)
1086 /// The lower bound of a variable (column) has to be given by an
1087 /// extended number of type Value, i.e. a finite number of type
1088 /// Value or -\ref INF.
1089 void colLowerBound(Col c, Value value) {
1090 _setColLowerBound(_lpId(c),value);
1093 /// Get the lower bound of a column (i.e a variable)
1095 /// This function returns the lower bound for column (variable) \t c
1096 /// (this might be -\ref INF as well).
1097 ///\return The lower bound for coloumn \t c
1098 Value colLowerBound(Col c) const {
1099 return _getColLowerBound(_lpId(c));
1102 ///\brief Set the lower bound of several columns
1103 ///(i.e a variables) at once
1105 ///This magic function takes a container as its argument
1106 ///and applies the function on all of its elements.
1107 /// The lower bound of a variable (column) has to be given by an
1108 /// extended number of type Value, i.e. a finite number of type
1109 /// Value or -\ref INF.
1112 void colLowerBound(T &t, Value value) { return 0;}
1115 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1116 colLowerBound(T &t, Value value,dummy<0> = 0) {
1117 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1118 colLowerBound(*i, value);
1122 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1124 colLowerBound(T &t, Value value,dummy<1> = 1) {
1125 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1126 colLowerBound(i->second, value);
1130 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1132 colLowerBound(T &t, Value value,dummy<2> = 2) {
1133 for(typename T::MapIt i(t); i!=INVALID; ++i){
1134 colLowerBound(*i, value);
1139 /// Set the upper bound of a column (i.e a variable)
1141 /// The upper bound of a variable (column) has to be given by an
1142 /// extended number of type Value, i.e. a finite number of type
1143 /// Value or \ref INF.
1144 void colUpperBound(Col c, Value value) {
1145 _setColUpperBound(_lpId(c),value);
1148 /// Get the upper bound of a column (i.e a variable)
1150 /// This function returns the upper bound for column (variable) \t c
1151 /// (this might be \ref INF as well).
1152 ///\return The upper bound for coloumn \t c
1153 Value colUpperBound(Col c) const {
1154 return _getColUpperBound(_lpId(c));
1157 ///\brief Set the upper bound of several columns
1158 ///(i.e a variables) at once
1160 ///This magic function takes a container as its argument
1161 ///and applies the function on all of its elements.
1162 /// The upper bound of a variable (column) has to be given by an
1163 /// extended number of type Value, i.e. a finite number of type
1164 /// Value or \ref INF.
1167 void colUpperBound(T &t, Value value) { return 0;}
1170 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1171 colUpperBound(T &t, Value value,dummy<0> = 0) {
1172 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1173 colUpperBound(*i, value);
1177 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1179 colUpperBound(T &t, Value value,dummy<1> = 1) {
1180 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1181 colUpperBound(i->second, value);
1185 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1187 colUpperBound(T &t, Value value,dummy<2> = 2) {
1188 for(typename T::MapIt i(t); i!=INVALID; ++i){
1189 colUpperBound(*i, value);
1194 /// Set the lower and the upper bounds of a column (i.e a variable)
1196 /// The lower and the upper bounds of
1197 /// a variable (column) have to be given by an
1198 /// extended number of type Value, i.e. a finite number of type
1199 /// Value, -\ref INF or \ref INF.
1200 void colBounds(Col c, Value lower, Value upper) {
1201 _setColLowerBound(_lpId(c),lower);
1202 _setColUpperBound(_lpId(c),upper);
1205 ///\brief Set the lower and the upper bound of several columns
1206 ///(i.e a variables) at once
1208 ///This magic function takes a container as its argument
1209 ///and applies the function on all of its elements.
1210 /// The lower and the upper bounds of
1211 /// a variable (column) have to be given by an
1212 /// extended number of type Value, i.e. a finite number of type
1213 /// Value, -\ref INF or \ref INF.
1216 void colBounds(T &t, Value lower, Value upper) { return 0;}
1219 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1220 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1221 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1222 colBounds(*i, lower, upper);
1226 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1228 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1229 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1230 colBounds(i->second, lower, upper);
1234 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1236 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1237 for(typename T::MapIt i(t); i!=INVALID; ++i){
1238 colBounds(*i, lower, upper);
1244 /// Set the lower and the upper bounds of a row (i.e a constraint)
1246 /// The lower and the upper bound of a constraint (row) have to be
1247 /// given by an extended number of type Value, i.e. a finite
1248 /// number of type Value, -\ref INF or \ref INF. There is no
1249 /// separate function for the lower and the upper bound because
1250 /// that would have been hard to implement for CPLEX.
1251 void rowBounds(Row c, Value lower, Value upper) {
1252 _setRowBounds(_lpId(c),lower, upper);
1255 /// Get the lower and the upper bounds of a row (i.e a constraint)
1257 /// The lower and the upper bound of
1258 /// a constraint (row) are
1259 /// extended numbers of type Value, i.e. finite numbers of type
1260 /// Value, -\ref INF or \ref INF.
1261 /// \todo There is no separate function for the
1262 /// lower and the upper bound because we had problems with the
1263 /// implementation of the setting functions for CPLEX:
1264 /// check out whether this can be done for these functions.
1265 void getRowBounds(Row c, Value &lower, Value &upper) const {
1266 _getRowBounds(_lpId(c),lower, upper);
1269 ///Set an element of the objective function
1270 void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
1272 ///Get an element of the objective function
1273 Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); };
1275 ///Set the objective function
1277 ///\param e is a linear expression of type \ref Expr.
1278 ///\bug Is should be called obj()
1279 void setObj(Expr e) {
1281 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1282 objCoeff((*i).first,(*i).second);
1283 obj_const_comp=e.constComp();
1286 ///Get the objective function
1288 ///\return the objective function as a linear expression of type \ref Expr.
1291 for (ColIt it(*this); it != INVALID; ++it) {
1292 double c = objCoeff(it);
1294 e.insert(std::make_pair(it, c));
1302 void max() { _setMax(); }
1304 void min() { _setMin(); }
1306 ///Query function: is this a maximization problem?
1307 bool is_max() const {return _isMax(); }
1309 ///Query function: is this a minimization problem?
1310 bool is_min() const {return !is_max(); }
1315 ///\name Solve the LP
1319 ///\e Solve the LP problem at hand
1321 ///\return The result of the optimization procedure. Possible
1322 ///values and their meanings can be found in the documentation of
1323 ///\ref SolveExitStatus.
1325 ///\todo Which method is used to solve the problem
1326 SolveExitStatus solve() { return _solve(); }
1330 ///\name Obtain the solution
1334 /// The status of the primal problem (the original LP problem)
1335 SolutionStatus primalStatus() const {
1336 return _getPrimalStatus();
1339 /// The status of the dual (of the original LP) problem
1340 SolutionStatus dualStatus() const {
1341 return _getDualStatus();
1344 ///The type of the original LP problem
1345 ProblemTypes problemType() const {
1346 return _getProblemType();
1350 Value primal(Col c) const { return _getPrimal(_lpId(c)); }
1353 Value dual(Row r) const { return _getDual(_lpId(r)); }
1356 bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); }
1361 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1362 /// of the primal problem, depending on whether we minimize or maximize.
1363 ///- \ref NaN if no primal solution is found.
1364 ///- The (finite) objective value if an optimal solution is found.
1365 Value primalValue() const { return _getPrimalValue()+obj_const_comp;}
1371 ///Common base class for MIP solvers
1372 ///\todo Much more docs
1373 ///\ingroup gen_opt_group
1374 class MipSolverBase : virtual public LpSolverBase{
1377 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1379 ///Continuous variable
1383 ///Unfortunately, cplex 7.5 somewhere writes something like
1384 ///#define INTEGER 'I'
1386 ///\todo No support for other types yet.
1389 ///Sets the type of the given coloumn to the given type
1391 ///Sets the type of the given coloumn to the given type.
1392 void colType(Col c, ColTypes col_type) {
1393 _colType(_lpId(c),col_type);
1396 ///Gives back the type of the column.
1398 ///Gives back the type of the column.
1399 ColTypes colType(Col c) const {
1400 return _colType(_lpId(c));
1403 ///Sets the type of the given Col to integer or remove that property.
1405 ///Sets the type of the given Col to integer or remove that property.
1406 void integer(Col c, bool enable) {
1413 ///Gives back whether the type of the column is integer or not.
1415 ///Gives back the type of the column.
1416 ///\return true if the column has integer type and false if not.
1417 bool integer(Col c) const {
1418 return (colType(c)==INT);
1421 /// The status of the MIP problem
1422 SolutionStatus mipStatus() const {
1423 return _getMipStatus();
1428 virtual ColTypes _colType(int col) const = 0;
1429 virtual void _colType(int col, ColTypes col_type) = 0;
1430 virtual SolutionStatus _getMipStatus() const = 0;
1434 ///\relates LpSolverBase::Expr
1436 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1437 const LpSolverBase::Expr &b)
1439 LpSolverBase::Expr tmp(a);
1445 ///\relates LpSolverBase::Expr
1447 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1448 const LpSolverBase::Expr &b)
1450 LpSolverBase::Expr tmp(a);
1456 ///\relates LpSolverBase::Expr
1458 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1459 const LpSolverBase::Value &b)
1461 LpSolverBase::Expr tmp(a);
1468 ///\relates LpSolverBase::Expr
1470 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1471 const LpSolverBase::Expr &b)
1473 LpSolverBase::Expr tmp(b);
1479 ///\relates LpSolverBase::Expr
1481 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1482 const LpSolverBase::Value &b)
1484 LpSolverBase::Expr tmp(a);
1491 ///\relates LpSolverBase::Constr
1493 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1494 const LpSolverBase::Expr &f)
1496 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1501 ///\relates LpSolverBase::Constr
1503 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1504 const LpSolverBase::Expr &f)
1506 return LpSolverBase::Constr(e,f);
1511 ///\relates LpSolverBase::Constr
1513 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1514 const LpSolverBase::Value &f)
1516 return LpSolverBase::Constr(e,f);
1521 ///\relates LpSolverBase::Constr
1523 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1524 const LpSolverBase::Expr &f)
1526 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1532 ///\relates LpSolverBase::Constr
1534 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1535 const LpSolverBase::Expr &f)
1537 return LpSolverBase::Constr(f,e);
1543 ///\relates LpSolverBase::Constr
1545 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1546 const LpSolverBase::Value &f)
1548 return LpSolverBase::Constr(f,e);
1553 ///\relates LpSolverBase::Constr
1555 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1556 const LpSolverBase::Value &f)
1558 return LpSolverBase::Constr(f,e,f);
1563 ///\relates LpSolverBase::Constr
1565 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1566 const LpSolverBase::Expr &f)
1568 return LpSolverBase::Constr(0,e-f,0);
1573 ///\relates LpSolverBase::Constr
1575 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1576 const LpSolverBase::Constr&c)
1578 LpSolverBase::Constr tmp(c);
1579 ///\todo Create an own exception type.
1580 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1581 else tmp.lowerBound()=n;
1586 ///\relates LpSolverBase::Constr
1588 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1589 const LpSolverBase::Value &n)
1591 LpSolverBase::Constr tmp(c);
1592 ///\todo Create an own exception type.
1593 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1594 else tmp.upperBound()=n;
1600 ///\relates LpSolverBase::Constr
1602 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1603 const LpSolverBase::Constr&c)
1605 LpSolverBase::Constr tmp(c);
1606 ///\todo Create an own exception type.
1607 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1608 else tmp.upperBound()=n;
1613 ///\relates LpSolverBase::Constr
1615 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1616 const LpSolverBase::Value &n)
1618 LpSolverBase::Constr tmp(c);
1619 ///\todo Create an own exception type.
1620 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1621 else tmp.lowerBound()=n;
1627 ///\relates LpSolverBase::DualExpr
1629 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1630 const LpSolverBase::DualExpr &b)
1632 LpSolverBase::DualExpr tmp(a);
1638 ///\relates LpSolverBase::DualExpr
1640 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1641 const LpSolverBase::DualExpr &b)
1643 LpSolverBase::DualExpr tmp(a);
1649 ///\relates LpSolverBase::DualExpr
1651 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1652 const LpSolverBase::Value &b)
1654 LpSolverBase::DualExpr tmp(a);
1661 ///\relates LpSolverBase::DualExpr
1663 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1664 const LpSolverBase::DualExpr &b)
1666 LpSolverBase::DualExpr tmp(b);
1672 ///\relates LpSolverBase::DualExpr
1674 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1675 const LpSolverBase::Value &b)
1677 LpSolverBase::DualExpr tmp(a);
1685 #endif //LEMON_LP_BASE_H