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/bits/utility.h>
31 #include<lemon/error.h>
32 #include<lemon/bits/invalid.h>
35 ///\brief The interface of the LP solver interface.
36 ///\ingroup gen_opt_group
40 ///Internal data structure to convert floating id's to fix one's
42 ///\todo This might be implemented to be also usable in other places.
49 std::vector<int> index;
50 std::vector<int> cross;
51 _FixId() : _first_index(-1), first_free(-1) {};
52 ///Convert a floating id to a fix one
54 ///\param n is a floating id
55 ///\return the corresponding fix id
56 int fixId(int n) const {return cross[n];}
57 ///Convert a fix id to a floating one
59 ///\param n is a fix id
60 ///\return the corresponding floating id
61 int floatingId(int n) const { return index[n];}
62 ///Add a new floating id.
64 ///\param n is a floating id
65 ///\return the fix id of the new value
66 ///\todo Multiple additions should also be handled.
69 if(cross.empty()) _first_index=n;
70 if(n>=int(cross.size())) {
73 cross[n]=index.size();
78 int next=index[first_free];
85 ///\todo Create an own exception type.
86 throw LogicError(); //floatingId-s must form a continuous range;
91 ///\param n is a fix id
98 for(int i=fl+1;i<int(cross.size());++i) {
104 ///An upper bound on the largest fix id.
106 ///\todo Do we need this?
108 std::size_t maxFixId() { return cross.size()-1; }
110 ///Returns the first (smallest) inserted index
112 ///Returns the first (smallest) inserted index
113 ///or -1 if no index has been inserted before.
114 int firstIndex() {return _first_index;}
117 ///Common base class for LP solvers
119 ///\todo Much more docs
120 ///\ingroup gen_opt_group
129 ///Possible outcomes of an LP solving procedure
130 enum SolveExitStatus {
131 ///This means that the problem has been successfully solved: either
132 ///an optimal solution has been found or infeasibility/unboundedness
135 ///Any other case (including the case when some user specified
136 ///limit has been exceeded)
141 enum SolutionStatus {
142 ///Feasible solution hasn't been found (but may exist).
144 ///\todo NOTFOUND might be a better name.
147 ///The problem has no feasible solution
149 ///Feasible solution found
151 ///Optimal solution exists and found
153 ///The cost function is unbounded
155 ///\todo Give a feasible solution and an infinite ray (and the
156 ///corresponding bases)
160 ///\e The type of the investigated LP problem
162 ///Primal-dual feasible
163 PRIMAL_DUAL_FEASIBLE = 0,
164 ///Primal feasible dual infeasible
165 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
166 ///Primal infeasible dual feasible
167 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
168 ///Primal-dual infeasible
169 PRIMAL_DUAL_INFEASIBLE = 3,
170 ///Could not determine so far
174 ///The floating point type used by the solver
175 typedef double Value;
176 ///The infinity constant
177 static const Value INF;
178 ///The not a number constant
179 static const Value NaN;
181 static inline bool isNaN(const Value& v) { return v!=v; }
187 ///Refer to a column of the LP.
189 ///This type is used to refer to a column of the LP.
191 ///Its value remains valid and correct even after the addition or erase of
194 ///\todo Document what can one do with a Col (INVALID, comparing,
195 ///it is similar to Node/Edge)
199 friend class LpSolverBase;
200 friend class MipSolverBase;
202 typedef Value ExprValue;
203 typedef True LpSolverCol;
205 Col(const Invalid&) : id(-1) {}
206 bool operator< (Col c) const {return id< c.id;}
207 bool operator> (Col c) const {return id> c.id;}
208 bool operator==(Col c) const {return id==c.id;}
209 bool operator!=(Col c) const {return id!=c.id;}
212 class ColIt : public Col {
216 ColIt(LpSolverBase &lp) : _lp(&lp)
218 id = _lp->cols.cross.empty()?-1:
219 _lp->cols.fixId(_lp->cols.firstIndex());
221 ColIt(const Invalid&) : Col(INVALID) {}
224 int fid = _lp->cols.floatingId(id)+1;
225 id = unsigned(fid)<_lp->cols.cross.size() ? _lp->cols.fixId(fid) : -1;
230 static int id(const Col& col) { return col.id; }
233 ///Refer to a row of the LP.
235 ///This type is used to refer to a row of the LP.
237 ///Its value remains valid and correct even after the addition or erase of
240 ///\todo Document what can one do with a Row (INVALID, comparing,
241 ///it is similar to Node/Edge)
245 friend class LpSolverBase;
247 typedef Value ExprValue;
248 typedef True LpSolverRow;
250 Row(const Invalid&) : id(-1) {}
252 bool operator< (Row c) const {return id< c.id;}
253 bool operator> (Row c) const {return id> c.id;}
254 bool operator==(Row c) const {return id==c.id;}
255 bool operator!=(Row c) const {return id!=c.id;}
258 static int id(const Row& row) { return row.id; }
262 int _lpId(const Col& col) const {
263 return cols.floatingId(id(col));
266 int _lpId(const Row& row) const {
267 return rows.floatingId(id(row));
273 ///Linear expression of variables and a constant component
275 ///This data structure stores a linear expression of the variables
276 ///(\ref Col "Col"s) and also has a constant component.
278 ///There are several ways to access and modify the contents of this
280 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
281 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
282 ///read and modify the coefficients like
289 ///or you can also iterate through its elements.
292 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
295 ///(This code computes the sum of all coefficients).
296 ///- Numbers (<tt>double</tt>'s)
297 ///and variables (\ref Col "Col"s) directly convert to an
298 ///\ref Expr and the usual linear operations are defined, so
301 ///2*v-3.12*(v-w/2)+2
302 ///v*2.1+(3*v+(v*12+w+6)*3)/2
304 ///are valid \ref Expr "Expr"essions.
305 ///The usual assignment operations are also defined.
308 ///e+=2*v-3.12*(v-w/2)+2;
312 ///- The constant member can be set and read by \ref constComp()
315 ///double c=e.constComp();
318 ///\note \ref clear() not only sets all coefficients to 0 but also
319 ///clears the constant components.
323 class Expr : public std::map<Col,Value>
326 typedef LpSolverBase::Col Key;
327 typedef LpSolverBase::Value Value;
330 typedef std::map<Col,Value> Base;
334 typedef True IsLinExpression;
336 Expr() : Base(), const_comp(0) { }
338 Expr(const Key &v) : const_comp(0) {
339 Base::insert(std::make_pair(v, 1));
342 Expr(const Value &v) : const_comp(v) {}
344 void set(const Key &v,const Value &c) {
345 Base::insert(std::make_pair(v, c));
348 Value &constComp() { return const_comp; }
350 const Value &constComp() const { return const_comp; }
352 ///Removes the components with zero coefficient.
354 for (Base::iterator i=Base::begin(); i!=Base::end();) {
357 if ((*i).second==0) Base::erase(i);
362 void simplify() const {
363 const_cast<Expr*>(this)->simplify();
366 ///Removes the coefficients closer to zero than \c tolerance.
367 void simplify(double &tolerance) {
368 for (Base::iterator i=Base::begin(); i!=Base::end();) {
371 if (std::fabs((*i).second)<tolerance) Base::erase(i);
376 ///Sets all coefficients and the constant component to 0.
383 Expr &operator+=(const Expr &e) {
384 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
385 (*this)[j->first]+=j->second;
386 const_comp+=e.const_comp;
390 Expr &operator-=(const Expr &e) {
391 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
392 (*this)[j->first]-=j->second;
393 const_comp-=e.const_comp;
397 Expr &operator*=(const Value &c) {
398 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
404 Expr &operator/=(const Value &c) {
405 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
412 void prettyPrint(std::ostream &os) {
413 //std::fmtflags os.flags();
414 //os.setf(std::ios::showpos);
415 Base::iterator j=Base::begin();
417 os<<j->second<<"*x["<<id(j->first)<<"]";
419 for (; j!=Base::end(); ++j){
422 os<<j->second<<"*x["<<id(j->first)<<"]";
424 //Nem valami korrekt, de nem talaltam meg, hogy kell
425 //os.unsetf(std::ios::showpos);
434 ///This data stucture represents a linear constraint in the LP.
435 ///Basically it is a linear expression with a lower or an upper bound
436 ///(or both). These parts of the constraint can be obtained by the member
437 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
439 ///There are two ways to construct a constraint.
440 ///- You can set the linear expression and the bounds directly
441 /// by the functions above.
442 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
443 /// are defined between expressions, or even between constraints whenever
444 /// it makes sense. Therefore if \c e and \c f are linear expressions and
445 /// \c s and \c t are numbers, then the followings are valid expressions
446 /// and thus they can be used directly e.g. in \ref addRow() whenever
455 ///\warning The validity of a constraint is checked only at run time, so
456 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
457 ///\ref LogicError exception.
461 typedef LpSolverBase::Expr Expr;
462 typedef Expr::Key Key;
463 typedef Expr::Value Value;
470 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
472 Constr(Value lb,const Expr &e,Value ub) :
473 _expr(e), _lb(lb), _ub(ub) {}
475 Constr(const Expr &e,Value ub) :
476 _expr(e), _lb(NaN), _ub(ub) {}
478 Constr(Value lb,const Expr &e) :
479 _expr(e), _lb(lb), _ub(NaN) {}
481 Constr(const Expr &e) :
482 _expr(e), _lb(NaN), _ub(NaN) {}
490 ///Reference to the linear expression
491 Expr &expr() { return _expr; }
492 ///Cont reference to the linear expression
493 const Expr &expr() const { return _expr; }
494 ///Reference to the lower bound.
497 ///- \ref INF "INF": the constraint is lower unbounded.
498 ///- \ref NaN "NaN": lower bound has not been set.
499 ///- finite number: the lower bound
500 Value &lowerBound() { return _lb; }
501 ///The const version of \ref lowerBound()
502 const Value &lowerBound() const { return _lb; }
503 ///Reference to the upper bound.
506 ///- \ref INF "INF": the constraint is upper unbounded.
507 ///- \ref NaN "NaN": upper bound has not been set.
508 ///- finite number: the upper bound
509 Value &upperBound() { return _ub; }
510 ///The const version of \ref upperBound()
511 const Value &upperBound() const { return _ub; }
512 ///Is the constraint lower bounded?
513 bool lowerBounded() const {
517 ///Is the constraint upper bounded?
518 bool upperBounded() const {
523 void prettyPrint(std::ostream &os) {
524 if (_lb==-LpSolverBase::INF||isNaN(_lb))
528 _expr.prettyPrint(os);
529 if (_ub==LpSolverBase::INF)
538 ///Linear expression of rows
540 ///This data structure represents a column of the matrix,
541 ///thas is it strores a linear expression of the dual variables
542 ///(\ref Row "Row"s).
544 ///There are several ways to access and modify the contents of this
546 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
547 ///if \c e is an DualExpr and \c v
548 ///and \c w are of type \ref Row, then you can
549 ///read and modify the coefficients like
556 ///or you can also iterate through its elements.
559 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
562 ///(This code computes the sum of all coefficients).
563 ///- Numbers (<tt>double</tt>'s)
564 ///and variables (\ref Row "Row"s) directly convert to an
565 ///\ref DualExpr and the usual linear operations are defined, so
569 ///v*2.1+(3*v+(v*12+w)*3)/2
571 ///are valid \ref DualExpr "DualExpr"essions.
572 ///The usual assignment operations are also defined.
575 ///e+=2*v-3.12*(v-w/2);
582 class DualExpr : public std::map<Row,Value>
585 typedef LpSolverBase::Row Key;
586 typedef LpSolverBase::Value Value;
589 typedef std::map<Row,Value> Base;
592 typedef True IsLinExpression;
594 DualExpr() : Base() { }
596 DualExpr(const Key &v) {
597 Base::insert(std::make_pair(v, 1));
600 void set(const Key &v,const Value &c) {
601 Base::insert(std::make_pair(v, c));
604 ///Removes the components with zero coefficient.
606 for (Base::iterator i=Base::begin(); i!=Base::end();) {
609 if ((*i).second==0) Base::erase(i);
614 void simplify() const {
615 const_cast<DualExpr*>(this)->simplify();
618 ///Removes the coefficients closer to zero than \c tolerance.
619 void simplify(double &tolerance) {
620 for (Base::iterator i=Base::begin(); i!=Base::end();) {
623 if (std::fabs((*i).second)<tolerance) Base::erase(i);
628 ///Sets all coefficients to 0.
634 DualExpr &operator+=(const DualExpr &e) {
635 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
636 (*this)[j->first]+=j->second;
640 DualExpr &operator-=(const DualExpr &e) {
641 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
642 (*this)[j->first]-=j->second;
646 DualExpr &operator*=(const Value &c) {
647 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
652 DualExpr &operator/=(const Value &c) {
653 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
662 template <typename _Base>
663 class MappedIterator {
668 typedef typename Base::iterator_category iterator_category;
669 typedef typename Base::difference_type difference_type;
670 typedef const std::pair<int, Value> value_type;
671 typedef value_type reference;
674 pointer(value_type& _value) : value(_value) {}
675 value_type* operator->() { return &value; }
680 MappedIterator(const Base& _base, const LpSolverBase& _lp)
681 : base(_base), lp(_lp) {}
683 reference operator*() {
684 return std::make_pair(lp._lpId(base->first), base->second);
687 pointer operator->() {
688 return pointer(operator*());
691 MappedIterator& operator++() {
696 MappedIterator& operator++(int) {
697 MappedIterator tmp(*this);
702 bool operator==(const MappedIterator& it) const {
703 return base == it.base;
706 bool operator!=(const MappedIterator& it) const {
707 return base != it.base;
712 const LpSolverBase& lp;
717 /// STL compatible iterator for lp col
718 typedef MappedIterator<Expr::const_iterator> LpRowIterator;
719 /// STL compatible iterator for lp row
720 typedef MappedIterator<DualExpr::const_iterator> LpColIterator;
722 //Abstract virtual functions
723 virtual LpSolverBase &_newLp() = 0;
724 virtual LpSolverBase &_copyLp(){
725 ///\todo This should be implemented here, too, when we have
726 ///problem retrieving routines. It can be overriden.
729 LpSolverBase & newlp(_newLp());
731 //return *(LpSolverBase*)0;
734 virtual int _addCol() = 0;
735 virtual int _addRow() = 0;
736 virtual void _eraseCol(int col) = 0;
737 virtual void _eraseRow(int row) = 0;
738 virtual void _getColName(int col, std::string & name) = 0;
739 virtual void _setColName(int col, const std::string & name) = 0;
740 virtual void _setRowCoeffs(int i, LpRowIterator b, LpRowIterator e) = 0;
741 virtual void _setColCoeffs(int i, LpColIterator b, LpColIterator e) = 0;
742 virtual void _setCoeff(int row, int col, Value value) = 0;
743 virtual Value _getCoeff(int row, int col) = 0;
745 virtual void _setColLowerBound(int i, Value value) = 0;
746 virtual Value _getColLowerBound(int i) = 0;
747 virtual void _setColUpperBound(int i, Value value) = 0;
748 virtual Value _getColUpperBound(int i) = 0;
749 // virtual void _setRowLowerBound(int i, Value value) = 0;
750 // virtual void _setRowUpperBound(int i, Value value) = 0;
751 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
752 virtual void _getRowBounds(int i, Value &lower, Value &upper)=0;
754 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
755 virtual Value _getObjCoeff(int i) = 0;
756 virtual void _clearObj()=0;
758 virtual SolveExitStatus _solve() = 0;
759 virtual Value _getPrimal(int i) = 0;
760 virtual Value _getDual(int i) = 0;
761 virtual Value _getPrimalValue() = 0;
762 virtual bool _isBasicCol(int i) = 0;
763 virtual SolutionStatus _getPrimalStatus() = 0;
764 virtual SolutionStatus _getDualStatus() = 0;
765 ///\todo This could be implemented here, too, using _getPrimalStatus() and
767 virtual ProblemTypes _getProblemType() = 0;
769 virtual void _setMax() = 0;
770 virtual void _setMin() = 0;
773 virtual bool _isMax() = 0;
775 //Own protected stuff
777 //Constant component of the objective function
778 Value obj_const_comp;
783 LpSolverBase() : obj_const_comp(0) {}
786 virtual ~LpSolverBase() {}
788 ///Creates a new LP problem
789 LpSolverBase &newLp() {return _newLp();}
790 ///Makes a copy of the LP problem
791 LpSolverBase ©Lp() {return _copyLp();}
793 ///\name Build up and modify the LP
797 ///Add a new empty column (i.e a new variable) to the LP
798 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
800 ///\brief Adds several new columns
801 ///(i.e a variables) at once
803 ///This magic function takes a container as its argument
804 ///and fills its elements
805 ///with new columns (i.e. variables)
807 ///- a standard STL compatible iterable container with
808 ///\ref Col as its \c values_type
811 ///std::vector<LpSolverBase::Col>
812 ///std::list<LpSolverBase::Col>
814 ///- a standard STL compatible iterable container with
815 ///\ref Col as its \c mapped_type
818 ///std::map<AnyType,LpSolverBase::Col>
820 ///- an iterable lemon \ref concepts::WriteMap "write map" like
822 ///ListGraph::NodeMap<LpSolverBase::Col>
823 ///ListGraph::EdgeMap<LpSolverBase::Col>
825 ///\return The number of the created column.
828 int addColSet(T &t) { return 0;}
831 typename enable_if<typename T::value_type::LpSolverCol,int>::type
832 addColSet(T &t,dummy<0> = 0) {
834 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
838 typename enable_if<typename T::value_type::second_type::LpSolverCol,
840 addColSet(T &t,dummy<1> = 1) {
842 for(typename T::iterator i=t.begin();i!=t.end();++i) {
849 typename enable_if<typename T::MapIt::Value::LpSolverCol,
851 addColSet(T &t,dummy<2> = 2) {
853 for(typename T::MapIt i(t); i!=INVALID; ++i)
862 ///Set a column (i.e a dual constraint) of the LP
864 ///\param c is the column to be modified
865 ///\param e is a dual linear expression (see \ref DualExpr)
867 void col(Col c,const DualExpr &e) {
869 _setColCoeffs(_lpId(c), LpColIterator(e.begin(), *this),
870 LpColIterator(e.end(), *this));
873 ///Add a new column to the LP
875 ///\param e is a dual linear expression (see \ref DualExpr)
876 ///\param obj is the corresponding component of the objective
877 ///function. It is 0 by default.
878 ///\return The created column.
879 Col addCol(const DualExpr &e, Value obj=0) {
886 ///Add a new empty row (i.e a new constraint) to the LP
888 ///This function adds a new empty row (i.e a new constraint) to the LP.
889 ///\return The created row
890 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
892 ///\brief Add several new rows
893 ///(i.e a constraints) at once
895 ///This magic function takes a container as its argument
896 ///and fills its elements
897 ///with new row (i.e. variables)
899 ///- a standard STL compatible iterable container with
900 ///\ref Row as its \c values_type
903 ///std::vector<LpSolverBase::Row>
904 ///std::list<LpSolverBase::Row>
906 ///- a standard STL compatible iterable container with
907 ///\ref Row as its \c mapped_type
910 ///std::map<AnyType,LpSolverBase::Row>
912 ///- an iterable lemon \ref concepts::WriteMap "write map" like
914 ///ListGraph::NodeMap<LpSolverBase::Row>
915 ///ListGraph::EdgeMap<LpSolverBase::Row>
917 ///\return The number of rows created.
920 int addRowSet(T &t) { return 0;}
923 typename enable_if<typename T::value_type::LpSolverRow,int>::type
924 addRowSet(T &t,dummy<0> = 0) {
926 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
930 typename enable_if<typename T::value_type::second_type::LpSolverRow,
932 addRowSet(T &t,dummy<1> = 1) {
934 for(typename T::iterator i=t.begin();i!=t.end();++i) {
941 typename enable_if<typename T::MapIt::Value::LpSolverRow,
943 addRowSet(T &t,dummy<2> = 2) {
945 for(typename T::MapIt i(t); i!=INVALID; ++i)
954 ///Set a row (i.e a constraint) of the LP
956 ///\param r is the row to be modified
957 ///\param l is lower bound (-\ref INF means no bound)
958 ///\param e is a linear expression (see \ref Expr)
959 ///\param u is the upper bound (\ref INF means no bound)
960 ///\bug This is a temportary function. The interface will change to
962 ///\todo Option to control whether a constraint with a single variable is
964 void row(Row r, Value l,const Expr &e, Value u) {
966 _setRowCoeffs(_lpId(r), LpRowIterator(e.begin(), *this),
967 LpRowIterator(e.end(), *this));
968 // _setRowLowerBound(_lpId(r),l-e.constComp());
969 // _setRowUpperBound(_lpId(r),u-e.constComp());
970 _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp());
973 ///Set a row (i.e a constraint) of the LP
975 ///\param r is the row to be modified
976 ///\param c is a linear expression (see \ref Constr)
977 void row(Row r, const Constr &c) {
978 row(r, c.lowerBounded()?c.lowerBound():-INF,
979 c.expr(), c.upperBounded()?c.upperBound():INF);
982 ///Add a new row (i.e a new constraint) to the LP
984 ///\param l is the lower bound (-\ref INF means no bound)
985 ///\param e is a linear expression (see \ref Expr)
986 ///\param u is the upper bound (\ref INF means no bound)
987 ///\return The created row.
988 ///\bug This is a temportary function. The interface will change to
990 Row addRow(Value l,const Expr &e, Value u) {
996 ///Add a new row (i.e a new constraint) to the LP
998 ///\param c is a linear expression (see \ref Constr)
999 ///\return The created row.
1000 Row addRow(const Constr &c) {
1005 ///Erase a coloumn (i.e a variable) from the LP
1007 ///\param c is the coloumn to be deleted
1008 ///\todo Please check this
1009 void eraseCol(Col c) {
1010 _eraseCol(_lpId(c));
1013 ///Erase a row (i.e a constraint) from the LP
1015 ///\param r is the row to be deleted
1016 ///\todo Please check this
1017 void eraseRow(Row r) {
1018 _eraseRow(_lpId(r));
1022 /// Get the name of a column
1024 ///\param c is the coresponding coloumn
1025 ///\return The name of the colunm
1026 std::string colName(Col c){
1028 _getColName(_lpId(c), name);
1032 /// Set the name of a column
1034 ///\param c is the coresponding coloumn
1035 ///\param name The name to be given
1036 void colName(Col c, const std::string& name){
1037 _setColName(_lpId(c), name);
1040 /// Set an element of the coefficient matrix of the LP
1042 ///\param r is the row of the element to be modified
1043 ///\param c is the coloumn of the element to be modified
1044 ///\param val is the new value of the coefficient
1046 void coeff(Row r, Col c, Value val){
1047 _setCoeff(_lpId(r),_lpId(c), val);
1050 /// Get an element of the coefficient matrix of the LP
1052 ///\param r is the row of the element in question
1053 ///\param c is the coloumn of the element in question
1054 ///\return the corresponding coefficient
1056 Value coeff(Row r, Col c){
1057 return _getCoeff(_lpId(r),_lpId(c));
1060 /// Set the lower bound of a column (i.e a variable)
1062 /// The lower bound of a variable (column) has to be given by an
1063 /// extended number of type Value, i.e. a finite number of type
1064 /// Value or -\ref INF.
1065 void colLowerBound(Col c, Value value) {
1066 _setColLowerBound(_lpId(c),value);
1069 /// Get the lower bound of a column (i.e a variable)
1071 /// This function returns the lower bound for column (variable) \t c
1072 /// (this might be -\ref INF as well).
1073 ///\return The lower bound for coloumn \t c
1074 Value colLowerBound(Col c) {
1075 return _getColLowerBound(_lpId(c));
1078 ///\brief Set the lower bound of several columns
1079 ///(i.e a variables) at once
1081 ///This magic function takes a container as its argument
1082 ///and applies the function on all of its elements.
1083 /// The lower bound of a variable (column) has to be given by an
1084 /// extended number of type Value, i.e. a finite number of type
1085 /// Value or -\ref INF.
1088 void colLowerBound(T &t, Value value) { return 0;}
1091 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1092 colLowerBound(T &t, Value value,dummy<0> = 0) {
1093 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1094 colLowerBound(*i, value);
1098 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1100 colLowerBound(T &t, Value value,dummy<1> = 1) {
1101 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1102 colLowerBound(i->second, value);
1106 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1108 colLowerBound(T &t, Value value,dummy<2> = 2) {
1109 for(typename T::MapIt i(t); i!=INVALID; ++i){
1110 colLowerBound(*i, value);
1115 /// Set the upper bound of a column (i.e a variable)
1117 /// The upper bound of a variable (column) has to be given by an
1118 /// extended number of type Value, i.e. a finite number of type
1119 /// Value or \ref INF.
1120 void colUpperBound(Col c, Value value) {
1121 _setColUpperBound(_lpId(c),value);
1124 /// Get the upper bound of a column (i.e a variable)
1126 /// This function returns the upper bound for column (variable) \t c
1127 /// (this might be \ref INF as well).
1128 ///\return The upper bound for coloumn \t c
1129 Value colUpperBound(Col c) {
1130 return _getColUpperBound(_lpId(c));
1133 ///\brief Set the upper bound of several columns
1134 ///(i.e a variables) at once
1136 ///This magic function takes a container as its argument
1137 ///and applies the function on all of its elements.
1138 /// The upper bound of a variable (column) has to be given by an
1139 /// extended number of type Value, i.e. a finite number of type
1140 /// Value or \ref INF.
1143 void colUpperBound(T &t, Value value) { return 0;}
1146 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1147 colUpperBound(T &t, Value value,dummy<0> = 0) {
1148 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1149 colUpperBound(*i, value);
1153 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1155 colUpperBound(T &t, Value value,dummy<1> = 1) {
1156 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1157 colUpperBound(i->second, value);
1161 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1163 colUpperBound(T &t, Value value,dummy<2> = 2) {
1164 for(typename T::MapIt i(t); i!=INVALID; ++i){
1165 colUpperBound(*i, value);
1170 /// Set the lower and the upper bounds of a column (i.e a variable)
1172 /// The lower and the upper bounds of
1173 /// a variable (column) have to be given by an
1174 /// extended number of type Value, i.e. a finite number of type
1175 /// Value, -\ref INF or \ref INF.
1176 void colBounds(Col c, Value lower, Value upper) {
1177 _setColLowerBound(_lpId(c),lower);
1178 _setColUpperBound(_lpId(c),upper);
1181 ///\brief Set the lower and the upper bound of several columns
1182 ///(i.e a variables) at once
1184 ///This magic function takes a container as its argument
1185 ///and applies the function on all of its elements.
1186 /// The lower and the upper bounds of
1187 /// a variable (column) have to be given by an
1188 /// extended number of type Value, i.e. a finite number of type
1189 /// Value, -\ref INF or \ref INF.
1192 void colBounds(T &t, Value lower, Value upper) { return 0;}
1195 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1196 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1197 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1198 colBounds(*i, lower, upper);
1202 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1204 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1205 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1206 colBounds(i->second, lower, upper);
1210 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1212 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1213 for(typename T::MapIt i(t); i!=INVALID; ++i){
1214 colBounds(*i, lower, upper);
1219 // /// Set the lower bound of a row (i.e a constraint)
1221 // /// The lower bound of a linear expression (row) has to be given by an
1222 // /// extended number of type Value, i.e. a finite number of type
1223 // /// Value or -\ref INF.
1224 // void rowLowerBound(Row r, Value value) {
1225 // _setRowLowerBound(_lpId(r),value);
1227 // /// Set the upper bound of a row (i.e a constraint)
1229 // /// The upper bound of a linear expression (row) has to be given by an
1230 // /// extended number of type Value, i.e. a finite number of type
1231 // /// Value or \ref INF.
1232 // void rowUpperBound(Row r, Value value) {
1233 // _setRowUpperBound(_lpId(r),value);
1236 /// Set the lower and the upper bounds of a row (i.e a constraint)
1238 /// The lower and the upper bound of
1239 /// a constraint (row) have to be given by an
1240 /// extended number of type Value, i.e. a finite number of type
1241 /// Value, -\ref INF or \ref INF. There is no separate function for the
1242 /// lower and the upper bound because that would have been hard to implement
1244 void rowBounds(Row c, Value lower, Value upper) {
1245 _setRowBounds(_lpId(c),lower, upper);
1248 /// Get the lower and the upper bounds of a row (i.e a constraint)
1250 /// The lower and the upper bound of
1251 /// a constraint (row) are
1252 /// extended numbers of type Value, i.e. finite numbers of type
1253 /// Value, -\ref INF or \ref INF.
1254 /// \todo There is no separate function for the
1255 /// lower and the upper bound because we had problems with the
1256 /// implementation of the setting functions for CPLEX:
1257 /// check out whether this can be done for these functions.
1258 void getRowBounds(Row c, Value &lower, Value &upper) {
1259 _getRowBounds(_lpId(c),lower, upper);
1262 ///Set an element of the objective function
1263 void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); };
1265 ///Get an element of the objective function
1266 Value objCoeff(Col c) {return _getObjCoeff(_lpId(c)); };
1268 ///Set the objective function
1270 ///\param e is a linear expression of type \ref Expr.
1271 ///\bug Is should be called obj()
1272 void setObj(Expr e) {
1274 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1275 objCoeff((*i).first,(*i).second);
1276 obj_const_comp=e.constComp();
1280 void max() { _setMax(); }
1282 void min() { _setMin(); }
1284 ///Query function: is this a maximization problem?
1285 bool is_max() {return _isMax(); }
1287 ///Query function: is this a minimization problem?
1288 bool is_min() {return !is_max(); }
1293 ///\name Solve the LP
1297 ///\e Solve the LP problem at hand
1299 ///\return The result of the optimization procedure. Possible
1300 ///values and their meanings can be found in the documentation of
1301 ///\ref SolveExitStatus.
1303 ///\todo Which method is used to solve the problem
1304 SolveExitStatus solve() { return _solve(); }
1308 ///\name Obtain the solution
1312 /// The status of the primal problem (the original LP problem)
1313 SolutionStatus primalStatus() {
1314 return _getPrimalStatus();
1317 /// The status of the dual (of the original LP) problem
1318 SolutionStatus dualStatus() {
1319 return _getDualStatus();
1322 ///The type of the original LP problem
1323 ProblemTypes problemType() {
1324 return _getProblemType();
1328 Value primal(Col c) { return _getPrimal(_lpId(c)); }
1331 Value dual(Row r) { return _getDual(_lpId(r)); }
1334 bool isBasicCol(Col c) { return _isBasicCol(_lpId(c)); }
1339 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1340 /// of the primal problem, depending on whether we minimize or maximize.
1341 ///- \ref NaN if no primal solution is found.
1342 ///- The (finite) objective value if an optimal solution is found.
1343 Value primalValue() { return _getPrimalValue()+obj_const_comp;}
1349 ///Common base class for MIP solvers
1350 ///\todo Much more docs
1351 ///\ingroup gen_opt_group
1352 class MipSolverBase : virtual public LpSolverBase{
1355 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1357 ///Continuous variable
1361 ///Unfortunately, cplex 7.5 somewhere writes something like
1362 ///#define INTEGER 'I'
1364 ///\todo No support for other types yet.
1367 ///Sets the type of the given coloumn to the given type
1369 ///Sets the type of the given coloumn to the given type.
1370 void colType(Col c, ColTypes col_type) {
1371 _colType(_lpId(c),col_type);
1374 ///Gives back the type of the column.
1376 ///Gives back the type of the column.
1377 ColTypes colType(Col c){
1378 return _colType(_lpId(c));
1381 ///Sets the type of the given Col to integer or remove that property.
1383 ///Sets the type of the given Col to integer or remove that property.
1384 void integer(Col c, bool enable) {
1391 ///Gives back whether the type of the column is integer or not.
1393 ///Gives back the type of the column.
1394 ///\return true if the column has integer type and false if not.
1395 bool integer(Col c){
1396 return (colType(c)==INT);
1399 /// The status of the MIP problem
1400 SolutionStatus mipStatus() {
1401 return _getMipStatus();
1406 virtual ColTypes _colType(int col) = 0;
1407 virtual void _colType(int col, ColTypes col_type) = 0;
1408 virtual SolutionStatus _getMipStatus()=0;
1412 ///\relates LpSolverBase::Expr
1414 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1415 const LpSolverBase::Expr &b)
1417 LpSolverBase::Expr tmp(a);
1423 ///\relates LpSolverBase::Expr
1425 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1426 const LpSolverBase::Expr &b)
1428 LpSolverBase::Expr tmp(a);
1434 ///\relates LpSolverBase::Expr
1436 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1437 const LpSolverBase::Value &b)
1439 LpSolverBase::Expr tmp(a);
1446 ///\relates LpSolverBase::Expr
1448 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1449 const LpSolverBase::Expr &b)
1451 LpSolverBase::Expr tmp(b);
1457 ///\relates LpSolverBase::Expr
1459 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1460 const LpSolverBase::Value &b)
1462 LpSolverBase::Expr tmp(a);
1469 ///\relates LpSolverBase::Constr
1471 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1472 const LpSolverBase::Expr &f)
1474 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1479 ///\relates LpSolverBase::Constr
1481 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1482 const LpSolverBase::Expr &f)
1484 return LpSolverBase::Constr(e,f);
1489 ///\relates LpSolverBase::Constr
1491 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1492 const LpSolverBase::Value &f)
1494 return LpSolverBase::Constr(e,f);
1499 ///\relates LpSolverBase::Constr
1501 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1502 const LpSolverBase::Expr &f)
1504 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1510 ///\relates LpSolverBase::Constr
1512 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1513 const LpSolverBase::Expr &f)
1515 return LpSolverBase::Constr(f,e);
1521 ///\relates LpSolverBase::Constr
1523 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1524 const LpSolverBase::Value &f)
1526 return LpSolverBase::Constr(f,e);
1531 ///\relates LpSolverBase::Constr
1533 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1534 const LpSolverBase::Value &f)
1536 return LpSolverBase::Constr(f,e,f);
1541 ///\relates LpSolverBase::Constr
1543 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1544 const LpSolverBase::Expr &f)
1546 return LpSolverBase::Constr(0,e-f,0);
1551 ///\relates LpSolverBase::Constr
1553 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1554 const LpSolverBase::Constr&c)
1556 LpSolverBase::Constr tmp(c);
1557 ///\todo Create an own exception type.
1558 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1559 else tmp.lowerBound()=n;
1564 ///\relates LpSolverBase::Constr
1566 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1567 const LpSolverBase::Value &n)
1569 LpSolverBase::Constr tmp(c);
1570 ///\todo Create an own exception type.
1571 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1572 else tmp.upperBound()=n;
1578 ///\relates LpSolverBase::Constr
1580 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1581 const LpSolverBase::Constr&c)
1583 LpSolverBase::Constr tmp(c);
1584 ///\todo Create an own exception type.
1585 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1586 else tmp.upperBound()=n;
1591 ///\relates LpSolverBase::Constr
1593 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1594 const LpSolverBase::Value &n)
1596 LpSolverBase::Constr tmp(c);
1597 ///\todo Create an own exception type.
1598 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1599 else tmp.lowerBound()=n;
1605 ///\relates LpSolverBase::DualExpr
1607 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1608 const LpSolverBase::DualExpr &b)
1610 LpSolverBase::DualExpr tmp(a);
1616 ///\relates LpSolverBase::DualExpr
1618 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1619 const LpSolverBase::DualExpr &b)
1621 LpSolverBase::DualExpr tmp(a);
1627 ///\relates LpSolverBase::DualExpr
1629 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1630 const LpSolverBase::Value &b)
1632 LpSolverBase::DualExpr tmp(a);
1639 ///\relates LpSolverBase::DualExpr
1641 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1642 const LpSolverBase::DualExpr &b)
1644 LpSolverBase::DualExpr tmp(b);
1650 ///\relates LpSolverBase::DualExpr
1652 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1653 const LpSolverBase::Value &b)
1655 LpSolverBase::DualExpr tmp(a);
1663 #endif //LEMON_LP_BASE_H