Some query functions got implemented, but only for GLPK.
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
27 #include<lemon/bits/utility.h>
28 #include<lemon/error.h>
29 #include<lemon/bits/invalid.h>
32 ///\brief The interface of the LP solver interface.
33 ///\ingroup gen_opt_group
37 ///Internal data structure to convert floating id's to fix one's
39 ///\todo This might be implemented to be also usable in other places.
46 std::vector<int> index;
47 std::vector<int> cross;
48 _FixId() : _first_index(-1), first_free(-1) {};
49 ///Convert a floating id to a fix one
51 ///\param n is a floating id
52 ///\return the corresponding fix id
53 int fixId(int n) const {return cross[n];}
54 ///Convert a fix id to a floating one
56 ///\param n is a fix id
57 ///\return the corresponding floating id
58 int floatingId(int n) const { return index[n];}
59 ///Add a new floating id.
61 ///\param n is a floating id
62 ///\return the fix id of the new value
63 ///\todo Multiple additions should also be handled.
66 if(cross.empty()) _first_index=n;
67 if(n>=int(cross.size())) {
70 cross[n]=index.size();
75 int next=index[first_free];
82 ///\todo Create an own exception type.
83 throw LogicError(); //floatingId-s must form a continuous range;
88 ///\param n is a fix id
95 for(int i=fl+1;i<int(cross.size());++i) {
101 ///An upper bound on the largest fix id.
103 ///\todo Do we need this?
105 std::size_t maxFixId() { return cross.size()-1; }
107 ///Returns the first (smallest) inserted index
109 ///Returns the first (smallest) inserted index
110 ///or -1 if no index has been inserted before.
111 int firstIndex() {return _first_index;}
114 ///Common base class for LP solvers
116 ///\todo Much more docs
117 ///\ingroup gen_opt_group
126 ///Possible outcomes of an LP solving procedure
127 enum SolveExitStatus {
128 ///This means that the problem has been successfully solved: either
129 ///an optimal solution has been found or infeasibility/unboundedness
132 ///Any other case (including the case when some user specified
133 ///limit has been exceeded)
138 enum SolutionStatus {
139 ///Feasible solution hasn't been found (but may exist).
141 ///\todo NOTFOUND might be a better name.
144 ///The problem has no feasible solution
146 ///Feasible solution found
148 ///Optimal solution exists and found
150 ///The cost function is unbounded
152 ///\todo Give a feasible solution and an infinite ray (and the
153 ///corresponding bases)
157 ///\e The type of the investigated LP problem
159 ///Primal-dual feasible
160 PRIMAL_DUAL_FEASIBLE = 0,
161 ///Primal feasible dual infeasible
162 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
163 ///Primal infeasible dual feasible
164 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
165 ///Primal-dual infeasible
166 PRIMAL_DUAL_INFEASIBLE = 3,
167 ///Could not determine so far
171 ///The floating point type used by the solver
172 typedef double Value;
173 ///The infinity constant
174 static const Value INF;
175 ///The not a number constant
176 static const Value NaN;
178 static inline bool isNaN(const Value& v) { return v!=v; }
184 ///Refer to a column of the LP.
186 ///This type is used to refer to a column of the LP.
188 ///Its value remains valid and correct even after the addition or erase of
191 ///\todo Document what can one do with a Col (INVALID, comparing,
192 ///it is similar to Node/Edge)
196 friend class LpSolverBase;
197 friend class MipSolverBase;
199 typedef Value ExprValue;
200 typedef True LpSolverCol;
202 Col(const Invalid&) : id(-1) {}
203 bool operator< (Col c) const {return id< c.id;}
204 bool operator> (Col c) const {return id> c.id;}
205 bool operator==(Col c) const {return id==c.id;}
206 bool operator!=(Col c) const {return id!=c.id;}
209 class ColIt : public Col {
213 ColIt(LpSolverBase &lp) : _lp(&lp)
215 id = _lp->cols.cross.empty()?-1:
216 _lp->cols.fixId(_lp->cols.firstIndex());
218 ColIt(const Invalid&) : Col(INVALID) {}
221 int fid = _lp->cols.floatingId(id)+1;
222 id = unsigned(fid)<_lp->cols.cross.size() ? _lp->cols.fixId(fid) : -1;
227 static int id(const Col& col) { return col.id; }
230 ///Refer to a row of the LP.
232 ///This type is used to refer to a row of the LP.
234 ///Its value remains valid and correct even after the addition or erase of
237 ///\todo Document what can one do with a Row (INVALID, comparing,
238 ///it is similar to Node/Edge)
242 friend class LpSolverBase;
244 typedef Value ExprValue;
245 typedef True LpSolverRow;
247 Row(const Invalid&) : id(-1) {}
249 bool operator< (Row c) const {return id< c.id;}
250 bool operator> (Row c) const {return id> c.id;}
251 bool operator==(Row c) const {return id==c.id;}
252 bool operator!=(Row c) const {return id!=c.id;}
255 static int id(const Row& row) { return row.id; }
259 int _lpId(const Col& col) const {
260 return cols.floatingId(id(col));
263 int _lpId(const Row& row) const {
264 return rows.floatingId(id(row));
270 ///Linear expression of variables and a constant component
272 ///This data structure strores a linear expression of the variables
273 ///(\ref Col "Col"s) and also has a constant component.
275 ///There are several ways to access and modify the contents of this
277 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
278 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
279 ///read and modify the coefficients like
286 ///or you can also iterate through its elements.
289 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
292 ///(This code computes the sum of all coefficients).
293 ///- Numbers (<tt>double</tt>'s)
294 ///and variables (\ref Col "Col"s) directly convert to an
295 ///\ref Expr and the usual linear operations are defined, so
298 ///2*v-3.12*(v-w/2)+2
299 ///v*2.1+(3*v+(v*12+w+6)*3)/2
301 ///are valid \ref Expr "Expr"essions.
302 ///The usual assignment operations are also defined.
305 ///e+=2*v-3.12*(v-w/2)+2;
309 ///- The constant member can be set and read by \ref constComp()
312 ///double c=e.constComp();
315 ///\note \ref clear() not only sets all coefficients to 0 but also
316 ///clears the constant components.
320 class Expr : public std::map<Col,Value>
323 typedef LpSolverBase::Col Key;
324 typedef LpSolverBase::Value Value;
327 typedef std::map<Col,Value> Base;
331 typedef True IsLinExpression;
333 Expr() : Base(), const_comp(0) { }
335 Expr(const Key &v) : const_comp(0) {
336 Base::insert(std::make_pair(v, 1));
339 Expr(const Value &v) : const_comp(v) {}
341 void set(const Key &v,const Value &c) {
342 Base::insert(std::make_pair(v, c));
345 Value &constComp() { return const_comp; }
347 const Value &constComp() const { return const_comp; }
349 ///Removes the components with zero coefficient.
351 for (Base::iterator i=Base::begin(); i!=Base::end();) {
354 if ((*i).second==0) Base::erase(i);
359 void simplify() const {
360 const_cast<Expr*>(this)->simplify();
363 ///Removes the coefficients closer to zero than \c tolerance.
364 void simplify(double &tolerance) {
365 for (Base::iterator i=Base::begin(); i!=Base::end();) {
368 if (std::fabs((*i).second)<tolerance) Base::erase(i);
373 ///Sets all coefficients and the constant component to 0.
380 Expr &operator+=(const Expr &e) {
381 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
382 (*this)[j->first]+=j->second;
383 const_comp+=e.const_comp;
387 Expr &operator-=(const Expr &e) {
388 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
389 (*this)[j->first]-=j->second;
390 const_comp-=e.const_comp;
394 Expr &operator*=(const Value &c) {
395 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
401 Expr &operator/=(const Value &c) {
402 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
411 ///This data stucture represents a linear constraint in the LP.
412 ///Basically it is a linear expression with a lower or an upper bound
413 ///(or both). These parts of the constraint can be obtained by the member
414 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
416 ///There are two ways to construct a constraint.
417 ///- You can set the linear expression and the bounds directly
418 /// by the functions above.
419 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
420 /// are defined between expressions, or even between constraints whenever
421 /// it makes sense. Therefore if \c e and \c f are linear expressions and
422 /// \c s and \c t are numbers, then the followings are valid expressions
423 /// and thus they can be used directly e.g. in \ref addRow() whenever
432 ///\warning The validity of a constraint is checked only at run time, so
433 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
434 ///\ref LogicError exception.
438 typedef LpSolverBase::Expr Expr;
439 typedef Expr::Key Key;
440 typedef Expr::Value Value;
447 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
449 Constr(Value lb,const Expr &e,Value ub) :
450 _expr(e), _lb(lb), _ub(ub) {}
452 Constr(const Expr &e,Value ub) :
453 _expr(e), _lb(NaN), _ub(ub) {}
455 Constr(Value lb,const Expr &e) :
456 _expr(e), _lb(lb), _ub(NaN) {}
458 Constr(const Expr &e) :
459 _expr(e), _lb(NaN), _ub(NaN) {}
467 ///Reference to the linear expression
468 Expr &expr() { return _expr; }
469 ///Cont reference to the linear expression
470 const Expr &expr() const { return _expr; }
471 ///Reference to the lower bound.
474 ///- \ref INF "INF": the constraint is lower unbounded.
475 ///- \ref NaN "NaN": lower bound has not been set.
476 ///- finite number: the lower bound
477 Value &lowerBound() { return _lb; }
478 ///The const version of \ref lowerBound()
479 const Value &lowerBound() const { return _lb; }
480 ///Reference to the upper bound.
483 ///- \ref INF "INF": the constraint is upper unbounded.
484 ///- \ref NaN "NaN": upper bound has not been set.
485 ///- finite number: the upper bound
486 Value &upperBound() { return _ub; }
487 ///The const version of \ref upperBound()
488 const Value &upperBound() const { return _ub; }
489 ///Is the constraint lower bounded?
490 bool lowerBounded() const {
494 ///Is the constraint upper bounded?
495 bool upperBounded() const {
501 ///Linear expression of rows
503 ///This data structure represents a column of the matrix,
504 ///thas is it strores a linear expression of the dual variables
505 ///(\ref Row "Row"s).
507 ///There are several ways to access and modify the contents of this
509 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
510 ///if \c e is an DualExpr and \c v
511 ///and \c w are of type \ref Row, then you can
512 ///read and modify the coefficients like
519 ///or you can also iterate through its elements.
522 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
525 ///(This code computes the sum of all coefficients).
526 ///- Numbers (<tt>double</tt>'s)
527 ///and variables (\ref Row "Row"s) directly convert to an
528 ///\ref DualExpr and the usual linear operations are defined, so
532 ///v*2.1+(3*v+(v*12+w)*3)/2
534 ///are valid \ref DualExpr "DualExpr"essions.
535 ///The usual assignment operations are also defined.
538 ///e+=2*v-3.12*(v-w/2);
545 class DualExpr : public std::map<Row,Value>
548 typedef LpSolverBase::Row Key;
549 typedef LpSolverBase::Value Value;
552 typedef std::map<Row,Value> Base;
555 typedef True IsLinExpression;
557 DualExpr() : Base() { }
559 DualExpr(const Key &v) {
560 Base::insert(std::make_pair(v, 1));
563 void set(const Key &v,const Value &c) {
564 Base::insert(std::make_pair(v, c));
567 ///Removes the components with zero coefficient.
569 for (Base::iterator i=Base::begin(); i!=Base::end();) {
572 if ((*i).second==0) Base::erase(i);
577 void simplify() const {
578 const_cast<DualExpr*>(this)->simplify();
581 ///Removes the coefficients closer to zero than \c tolerance.
582 void simplify(double &tolerance) {
583 for (Base::iterator i=Base::begin(); i!=Base::end();) {
586 if (std::fabs((*i).second)<tolerance) Base::erase(i);
591 ///Sets all coefficients to 0.
597 DualExpr &operator+=(const DualExpr &e) {
598 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
599 (*this)[j->first]+=j->second;
603 DualExpr &operator-=(const DualExpr &e) {
604 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
605 (*this)[j->first]-=j->second;
609 DualExpr &operator*=(const Value &c) {
610 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
615 DualExpr &operator/=(const Value &c) {
616 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
625 template <typename _Base>
626 class MappedIterator {
631 typedef typename Base::iterator_category iterator_category;
632 typedef typename Base::difference_type difference_type;
633 typedef const std::pair<int, Value> value_type;
634 typedef value_type reference;
637 pointer(value_type& _value) : value(_value) {}
638 value_type* operator->() { return &value; }
643 MappedIterator(const Base& _base, const LpSolverBase& _lp)
644 : base(_base), lp(_lp) {}
646 reference operator*() {
647 return std::make_pair(lp._lpId(base->first), base->second);
650 pointer operator->() {
651 return pointer(operator*());
654 MappedIterator& operator++() {
659 MappedIterator& operator++(int) {
660 MappedIterator tmp(*this);
665 bool operator==(const MappedIterator& it) const {
666 return base == it.base;
669 bool operator!=(const MappedIterator& it) const {
670 return base != it.base;
675 const LpSolverBase& lp;
680 /// STL compatible iterator for lp col
681 typedef MappedIterator<Expr::const_iterator> LpRowIterator;
682 /// STL compatible iterator for lp row
683 typedef MappedIterator<DualExpr::const_iterator> LpColIterator;
685 //Abstract virtual functions
686 virtual LpSolverBase &_newLp() = 0;
687 virtual LpSolverBase &_copyLp(){
688 ///\todo This should be implemented here, too, when we have
689 ///problem retrieving routines. It can be overriden.
692 LpSolverBase & newlp(_newLp());
694 //return *(LpSolverBase*)0;
697 virtual int _addCol() = 0;
698 virtual int _addRow() = 0;
699 virtual void _eraseCol(int col) = 0;
700 virtual void _eraseRow(int row) = 0;
701 virtual void _getColName(int col, std::string & name) = 0;
702 virtual void _setColName(int col, const std::string & name) = 0;
703 virtual void _setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) = 0;
704 virtual void _setColCoeffs(int i, LpColIterator b, LpColIterator e) = 0;
705 virtual void _setCoeff(int row, int col, Value value) = 0;
706 virtual Value _getCoeff(int row, int col) = 0;
708 virtual void _setColLowerBound(int i, Value value) = 0;
709 virtual void _setColUpperBound(int i, Value value) = 0;
710 // virtual void _setRowLowerBound(int i, Value value) = 0;
711 // virtual void _setRowUpperBound(int i, Value value) = 0;
712 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
713 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
714 virtual Value _getObjCoeff(int i) = 0;
715 virtual void _clearObj()=0;
717 virtual SolveExitStatus _solve() = 0;
718 virtual Value _getPrimal(int i) = 0;
719 virtual Value _getDual(int i) = 0;
720 virtual Value _getPrimalValue() = 0;
721 virtual bool _isBasicCol(int i) = 0;
722 virtual SolutionStatus _getPrimalStatus() = 0;
723 virtual SolutionStatus _getDualStatus() = 0;
724 ///\todo This could be implemented here, too, using _getPrimalStatus() and
726 virtual ProblemTypes _getProblemType() = 0;
728 virtual void _setMax() = 0;
729 virtual void _setMin() = 0;
732 virtual bool _isMax() = 0;
734 //Own protected stuff
736 //Constant component of the objective function
737 Value obj_const_comp;
742 LpSolverBase() : obj_const_comp(0) {}
745 virtual ~LpSolverBase() {}
747 ///Creates a new LP problem
748 LpSolverBase &newLp() {return _newLp();}
749 ///Makes a copy of the LP problem
750 LpSolverBase ©Lp() {return _copyLp();}
752 ///\name Build up and modify the LP
756 ///Add a new empty column (i.e a new variable) to the LP
757 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
759 ///\brief Adds several new columns
760 ///(i.e a variables) at once
762 ///This magic function takes a container as its argument
763 ///and fills its elements
764 ///with new columns (i.e. variables)
766 ///- a standard STL compatible iterable container with
767 ///\ref Col as its \c values_type
770 ///std::vector<LpSolverBase::Col>
771 ///std::list<LpSolverBase::Col>
773 ///- a standard STL compatible iterable container with
774 ///\ref Col as its \c mapped_type
777 ///std::map<AnyType,LpSolverBase::Col>
779 ///- an iterable lemon \ref concepts::WriteMap "write map" like
781 ///ListGraph::NodeMap<LpSolverBase::Col>
782 ///ListGraph::EdgeMap<LpSolverBase::Col>
784 ///\return The number of the created column.
787 int addColSet(T &t) { return 0;}
790 typename enable_if<typename T::value_type::LpSolverCol,int>::type
791 addColSet(T &t,dummy<0> = 0) {
793 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
797 typename enable_if<typename T::value_type::second_type::LpSolverCol,
799 addColSet(T &t,dummy<1> = 1) {
801 for(typename T::iterator i=t.begin();i!=t.end();++i) {
808 typename enable_if<typename T::MapIt::Value::LpSolverCol,
810 addColSet(T &t,dummy<2> = 2) {
812 for(typename T::MapIt i(t); i!=INVALID; ++i)
821 ///Set a column (i.e a dual constraint) of the LP
823 ///\param c is the column to be modified
824 ///\param e is a dual linear expression (see \ref DualExpr)
826 void col(Col c,const DualExpr &e) {
828 _setColCoeffs(_lpId(c), LpColIterator(e.begin(), *this),
829 LpColIterator(e.end(), *this));
832 ///Add a new column to the LP
834 ///\param e is a dual linear expression (see \ref DualExpr)
835 ///\param obj is the corresponding component of the objective
836 ///function. It is 0 by default.
837 ///\return The created column.
838 Col addCol(const DualExpr &e, Value obj=0) {
845 ///Add a new empty row (i.e a new constraint) to the LP
847 ///This function adds a new empty row (i.e a new constraint) to the LP.
848 ///\return The created row
849 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
851 ///\brief Add several new rows
852 ///(i.e a constraints) at once
854 ///This magic function takes a container as its argument
855 ///and fills its elements
856 ///with new row (i.e. variables)
858 ///- a standard STL compatible iterable container with
859 ///\ref Row as its \c values_type
862 ///std::vector<LpSolverBase::Row>
863 ///std::list<LpSolverBase::Row>
865 ///- a standard STL compatible iterable container with
866 ///\ref Row as its \c mapped_type
869 ///std::map<AnyType,LpSolverBase::Row>
871 ///- an iterable lemon \ref concepts::WriteMap "write map" like
873 ///ListGraph::NodeMap<LpSolverBase::Row>
874 ///ListGraph::EdgeMap<LpSolverBase::Row>
876 ///\return The number of rows created.
879 int addRowSet(T &t) { return 0;}
882 typename enable_if<typename T::value_type::LpSolverRow,int>::type
883 addRowSet(T &t,dummy<0> = 0) {
885 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
889 typename enable_if<typename T::value_type::second_type::LpSolverRow,
891 addRowSet(T &t,dummy<1> = 1) {
893 for(typename T::iterator i=t.begin();i!=t.end();++i) {
900 typename enable_if<typename T::MapIt::Value::LpSolverRow,
902 addRowSet(T &t,dummy<2> = 2) {
904 for(typename T::MapIt i(t); i!=INVALID; ++i)
913 ///Set a row (i.e a constraint) of the LP
915 ///\param r is the row to be modified
916 ///\param l is lower bound (-\ref INF means no bound)
917 ///\param e is a linear expression (see \ref Expr)
918 ///\param u is the upper bound (\ref INF means no bound)
919 ///\bug This is a temportary function. The interface will change to
921 ///\todo Option to control whether a constraint with a single variable is
923 void row(Row r, Value l,const Expr &e, Value u) {
925 _setRowCoeffs(_lpId(r), LpRowIterator(e.begin(), *this),
926 LpRowIterator(e.end(), *this));
927 // _setRowLowerBound(_lpId(r),l-e.constComp());
928 // _setRowUpperBound(_lpId(r),u-e.constComp());
929 _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
932 ///Set a row (i.e a constraint) of the LP
934 ///\param r is the row to be modified
935 ///\param c is a linear expression (see \ref Constr)
936 void row(Row r, const Constr &c) {
937 row(r, c.lowerBounded()?c.lowerBound():-INF,
938 c.expr(), c.upperBounded()?c.upperBound():INF);
941 ///Add a new row (i.e a new constraint) to the LP
943 ///\param l is the lower bound (-\ref INF means no bound)
944 ///\param e is a linear expression (see \ref Expr)
945 ///\param u is the upper bound (\ref INF means no bound)
946 ///\return The created row.
947 ///\bug This is a temportary function. The interface will change to
949 Row addRow(Value l,const Expr &e, Value u) {
955 ///Add a new row (i.e a new constraint) to the LP
957 ///\param c is a linear expression (see \ref Constr)
958 ///\return The created row.
959 Row addRow(const Constr &c) {
964 ///Erase a coloumn (i.e a variable) from the LP
966 ///\param c is the coloumn to be deleted
967 ///\todo Please check this
968 void eraseCol(Col c) {
972 ///Erase a row (i.e a constraint) from the LP
974 ///\param r is the row to be deleted
975 ///\todo Please check this
976 void eraseRow(Row r) {
981 /// Get the name of a column
983 ///\param c is the coresponding coloumn
984 ///\return The name of the colunm
985 std::string colName(Col c){
987 _getColName(_lpId(c), name);
991 /// Set the name of a column
993 ///\param c is the coresponding coloumn
994 ///\param name The name to be given
995 void colName(Col c, const std::string& name){
996 _setColName(_lpId(c), name);
999 /// Set an element of the coefficient matrix of the LP
1001 ///\param r is the row of the element to be modified
1002 ///\param c is the coloumn of the element to be modified
1003 ///\param val is the new value of the coefficient
1005 void coeff(Row r, Col c, Value val){
1006 _setCoeff(_lpId(r),_lpId(c), val);
1009 /// Get an element of the coefficient matrix of the LP
1011 ///\param r is the row of the element in question
1012 ///\param c is the coloumn of the element in question
1013 ///\return the corresponding coefficient
1015 Value coeff(Row r, Col c){
1016 return _getCoeff(_lpId(r),_lpId(c));
1019 /// Set the lower bound of a column (i.e a variable)
1021 /// The lower bound of a variable (column) has to be given by an
1022 /// extended number of type Value, i.e. a finite number of type
1023 /// Value or -\ref INF.
1024 void colLowerBound(Col c, Value value) {
1025 _setColLowerBound(_lpId(c),value);
1028 ///\brief Set the lower bound of several columns
1029 ///(i.e a variables) at once
1031 ///This magic function takes a container as its argument
1032 ///and applies the function on all of its elements.
1033 /// The lower bound of a variable (column) has to be given by an
1034 /// extended number of type Value, i.e. a finite number of type
1035 /// Value or -\ref INF.
1038 void colLowerBound(T &t, Value value) { return 0;}
1041 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1042 colLowerBound(T &t, Value value,dummy<0> = 0) {
1043 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1044 colLowerBound(*i, value);
1048 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1050 colLowerBound(T &t, Value value,dummy<1> = 1) {
1051 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1052 colLowerBound(i->second, value);
1056 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1058 colLowerBound(T &t, Value value,dummy<2> = 2) {
1059 for(typename T::MapIt i(t); i!=INVALID; ++i){
1060 colLowerBound(*i, value);
1065 /// Set the upper bound of a column (i.e a variable)
1067 /// The upper bound of a variable (column) has to be given by an
1068 /// extended number of type Value, i.e. a finite number of type
1069 /// Value or \ref INF.
1070 void colUpperBound(Col c, Value value) {
1071 _setColUpperBound(_lpId(c),value);
1074 ///\brief Set the lower bound of several columns
1075 ///(i.e a variables) at once
1077 ///This magic function takes a container as its argument
1078 ///and applies the function on all of its elements.
1079 /// The upper bound of a variable (column) has to be given by an
1080 /// extended number of type Value, i.e. a finite number of type
1081 /// Value or \ref INF.
1084 void colUpperBound(T &t, Value value) { return 0;}
1087 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1088 colUpperBound(T &t, Value value,dummy<0> = 0) {
1089 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1090 colUpperBound(*i, value);
1094 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1096 colUpperBound(T &t, Value value,dummy<1> = 1) {
1097 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1098 colUpperBound(i->second, value);
1102 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1104 colUpperBound(T &t, Value value,dummy<2> = 2) {
1105 for(typename T::MapIt i(t); i!=INVALID; ++i){
1106 colUpperBound(*i, value);
1111 /// Set the lower and the upper bounds of a column (i.e a variable)
1113 /// The lower and the upper bounds of
1114 /// a variable (column) have to be given by an
1115 /// extended number of type Value, i.e. a finite number of type
1116 /// Value, -\ref INF or \ref INF.
1117 void colBounds(Col c, Value lower, Value upper) {
1118 _setColLowerBound(_lpId(c),lower);
1119 _setColUpperBound(_lpId(c),upper);
1122 ///\brief Set the lower and the upper bound of several columns
1123 ///(i.e a variables) at once
1125 ///This magic function takes a container as its argument
1126 ///and applies the function on all of its elements.
1127 /// The lower and the upper bounds of
1128 /// a variable (column) have to be given by an
1129 /// extended number of type Value, i.e. a finite number of type
1130 /// Value, -\ref INF or \ref INF.
1133 void colBounds(T &t, Value lower, Value upper) { return 0;}
1136 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1137 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1138 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1139 colBounds(*i, lower, upper);
1143 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1145 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1146 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1147 colBounds(i->second, lower, upper);
1151 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1153 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1154 for(typename T::MapIt i(t); i!=INVALID; ++i){
1155 colBounds(*i, lower, upper);
1160 // /// Set the lower bound of a row (i.e a constraint)
1162 // /// The lower bound of a linear expression (row) has to be given by an
1163 // /// extended number of type Value, i.e. a finite number of type
1164 // /// Value or -\ref INF.
1165 // void rowLowerBound(Row r, Value value) {
1166 // _setRowLowerBound(_lpId(r),value);
1168 // /// Set the upper bound of a row (i.e a constraint)
1170 // /// The upper bound of a linear expression (row) has to be given by an
1171 // /// extended number of type Value, i.e. a finite number of type
1172 // /// Value or \ref INF.
1173 // void rowUpperBound(Row r, Value value) {
1174 // _setRowUpperBound(_lpId(r),value);
1177 /// Set the lower and the upper bounds of a row (i.e a constraint)
1179 /// The lower and the upper bounds of
1180 /// a constraint (row) have to be given by an
1181 /// extended number of type Value, i.e. a finite number of type
1182 /// Value, -\ref INF or \ref INF.
1183 void rowBounds(Row c, Value lower, Value upper) {
1184 _setRowBounds(_lpId(c),lower, upper);
1185 // _setRowUpperBound(_lpId(c),upper);
1188 ///Set an element of the objective function
1189 void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
1191 ///Get an element of the objective function
1192 Value objCoeff(Col c) {return _getObjCoeff(_lpId(c)); };
1194 ///Set the objective function
1196 ///\param e is a linear expression of type \ref Expr.
1197 ///\bug Is should be called obj()
1198 void setObj(Expr e) {
1200 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1201 objCoeff((*i).first,(*i).second);
1202 obj_const_comp=e.constComp();
1206 void max() { _setMax(); }
1208 void min() { _setMin(); }
1210 ///Query function: is this a maximization problem?
1211 bool is_max() {return _isMax(); }
1213 ///Query function: is this a minimization problem?
1214 bool is_min() {return !is_max(); }
1219 ///\name Solve the LP
1223 ///\e Solve the LP problem at hand
1225 ///\return The result of the optimization procedure. Possible
1226 ///values and their meanings can be found in the documentation of
1227 ///\ref SolveExitStatus.
1229 ///\todo Which method is used to solve the problem
1230 SolveExitStatus solve() { return _solve(); }
1234 ///\name Obtain the solution
1238 /// The status of the primal problem (the original LP problem)
1239 SolutionStatus primalStatus() {
1240 return _getPrimalStatus();
1243 /// The status of the dual (of the original LP) problem
1244 SolutionStatus dualStatus() {
1245 return _getDualStatus();
1248 ///The type of the original LP problem
1249 ProblemTypes problemType() {
1250 return _getProblemType();
1254 Value primal(Col c) { return _getPrimal(_lpId(c)); }
1257 Value dual(Row r) { return _getDual(_lpId(r)); }
1260 bool isBasicCol(Col c) { return _isBasicCol(_lpId(c)); }
1265 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1266 /// of the primal problem, depending on whether we minimize or maximize.
1267 ///- \ref NaN if no primal solution is found.
1268 ///- The (finite) objective value if an optimal solution is found.
1269 Value primalValue() { return _getPrimalValue()+obj_const_comp;}
1275 ///Common base class for MIP solvers
1276 ///\todo Much more docs
1277 ///\ingroup gen_opt_group
1278 class MipSolverBase : virtual public LpSolverBase{
1281 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1283 ///Continuous variable
1287 ///Unfortunately, cplex 7.5 somewhere writes something like
1288 ///#define INTEGER 'I'
1290 ///\todo No support for other types yet.
1293 ///Sets the type of the given coloumn to the given type
1295 ///Sets the type of the given coloumn to the given type.
1296 void colType(Col c, ColTypes col_type) {
1297 _colType(_lpId(c),col_type);
1300 ///Gives back the type of the column.
1302 ///Gives back the type of the column.
1303 ColTypes colType(Col c){
1304 return _colType(_lpId(c));
1307 ///Sets the type of the given Col to integer or remove that property.
1309 ///Sets the type of the given Col to integer or remove that property.
1310 void integer(Col c, bool enable) {
1317 ///Gives back whether the type of the column is integer or not.
1319 ///Gives back the type of the column.
1320 ///\return true if the column has integer type and false if not.
1321 bool integer(Col c){
1322 return (colType(c)==INT);
1325 /// The status of the MIP problem
1326 SolutionStatus mipStatus() {
1327 return _getMipStatus();
1332 virtual ColTypes _colType(int col) = 0;
1333 virtual void _colType(int col, ColTypes col_type) = 0;
1334 virtual SolutionStatus _getMipStatus()=0;
1338 ///\relates LpSolverBase::Expr
1340 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1341 const LpSolverBase::Expr &b)
1343 LpSolverBase::Expr tmp(a);
1349 ///\relates LpSolverBase::Expr
1351 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1352 const LpSolverBase::Expr &b)
1354 LpSolverBase::Expr tmp(a);
1360 ///\relates LpSolverBase::Expr
1362 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1363 const LpSolverBase::Value &b)
1365 LpSolverBase::Expr tmp(a);
1372 ///\relates LpSolverBase::Expr
1374 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1375 const LpSolverBase::Expr &b)
1377 LpSolverBase::Expr tmp(b);
1383 ///\relates LpSolverBase::Expr
1385 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1386 const LpSolverBase::Value &b)
1388 LpSolverBase::Expr tmp(a);
1395 ///\relates LpSolverBase::Constr
1397 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1398 const LpSolverBase::Expr &f)
1400 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1405 ///\relates LpSolverBase::Constr
1407 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1408 const LpSolverBase::Expr &f)
1410 return LpSolverBase::Constr(e,f);
1415 ///\relates LpSolverBase::Constr
1417 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1418 const LpSolverBase::Value &f)
1420 return LpSolverBase::Constr(e,f);
1425 ///\relates LpSolverBase::Constr
1427 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1428 const LpSolverBase::Expr &f)
1430 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1436 ///\relates LpSolverBase::Constr
1438 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1439 const LpSolverBase::Expr &f)
1441 return LpSolverBase::Constr(f,e);
1447 ///\relates LpSolverBase::Constr
1449 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1450 const LpSolverBase::Value &f)
1452 return LpSolverBase::Constr(f,e);
1457 ///\relates LpSolverBase::Constr
1459 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1460 const LpSolverBase::Expr &f)
1462 return LpSolverBase::Constr(0,e-f,0);
1467 ///\relates LpSolverBase::Constr
1469 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1470 const LpSolverBase::Constr&c)
1472 LpSolverBase::Constr tmp(c);
1473 ///\todo Create an own exception type.
1474 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1475 else tmp.lowerBound()=n;
1480 ///\relates LpSolverBase::Constr
1482 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1483 const LpSolverBase::Value &n)
1485 LpSolverBase::Constr tmp(c);
1486 ///\todo Create an own exception type.
1487 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1488 else tmp.upperBound()=n;
1494 ///\relates LpSolverBase::Constr
1496 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1497 const LpSolverBase::Constr&c)
1499 LpSolverBase::Constr tmp(c);
1500 ///\todo Create an own exception type.
1501 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1502 else tmp.upperBound()=n;
1507 ///\relates LpSolverBase::Constr
1509 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1510 const LpSolverBase::Value &n)
1512 LpSolverBase::Constr tmp(c);
1513 ///\todo Create an own exception type.
1514 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1515 else tmp.lowerBound()=n;
1521 ///\relates LpSolverBase::DualExpr
1523 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1524 const LpSolverBase::DualExpr &b)
1526 LpSolverBase::DualExpr tmp(a);
1532 ///\relates LpSolverBase::DualExpr
1534 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1535 const LpSolverBase::DualExpr &b)
1537 LpSolverBase::DualExpr tmp(a);
1543 ///\relates LpSolverBase::DualExpr
1545 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1546 const LpSolverBase::Value &b)
1548 LpSolverBase::DualExpr tmp(a);
1555 ///\relates LpSolverBase::DualExpr
1557 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1558 const LpSolverBase::DualExpr &b)
1560 LpSolverBase::DualExpr tmp(b);
1566 ///\relates LpSolverBase::DualExpr
1568 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1569 const LpSolverBase::Value &b)
1571 LpSolverBase::DualExpr tmp(a);
1579 #endif //LEMON_LP_BASE_H