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
36 ///Internal data structure to convert floating id's to fix one's
38 ///\todo This might be implemented to be also usable in other places.
42 std::vector<int> index;
43 std::vector<int> cross;
46 _FixId() : first_free(-1) {};
47 ///Convert a floating id to a fix one
49 ///\param n is a floating id
50 ///\return the corresponding fix id
51 int fixId(int n) const {return cross[n];}
52 ///Convert a fix id to a floating one
54 ///\param n is a fix id
55 ///\return the corresponding floating id
56 int floatingId(int n) const { return index[n];}
57 ///Add a new floating id.
59 ///\param n is a floating id
60 ///\return the fix id of the new value
61 ///\todo Multiple additions should also be handled.
64 if(n>=int(cross.size())) {
67 cross[n]=index.size();
72 int next=index[first_free];
79 ///\todo Create an own exception type.
80 throw LogicError(); //floatingId-s must form a continuous range;
85 ///\param n is a fix id
92 for(int i=fl+1;i<int(cross.size());++i) {
98 ///An upper bound on the largest fix id.
100 ///\todo Do we need this?
102 std::size_t maxFixId() { return cross.size()-1; }
106 ///Common base class for LP solvers
108 ///\todo Much more docs
109 ///\ingroup gen_opt_group
114 ///Possible outcomes of an LP solving procedure
115 enum SolveExitStatus {
116 ///This means that the problem has been successfully solved: either
117 ///an optimal solution has been found or infeasibility/unboundedness
120 ///Any other case (including the case when some user specified limit has been exceeded)
125 enum SolutionStatus {
126 ///Feasible solution hasn't been found (but may exist).
128 ///\todo NOTFOUND might be a better name.
131 ///The problem has no feasible solution
133 ///Feasible solution found
135 ///Optimal solution exists and found
137 ///The cost function is unbounded
139 ///\todo Give a feasible solution and an infinite ray (and the
140 ///corresponding bases)
144 ///\e The type of the investigated LP problem
146 ///Primal-dual feasible
147 PRIMAL_DUAL_FEASIBLE = 0,
148 ///Primal feasible dual infeasible
149 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
150 ///Primal infeasible dual feasible
151 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
152 ///Primal-dual infeasible
153 PRIMAL_DUAL_INFEASIBLE = 3,
154 ///Could not determine so far
158 ///The floating point type used by the solver
159 typedef double Value;
160 ///The infinity constant
161 static const Value INF;
162 ///The not a number constant
163 static const Value NaN;
165 static inline bool isNaN(const Value& v) { return v!=v; }
167 ///Refer to a column of the LP.
169 ///This type is used to refer to a column of the LP.
171 ///Its value remains valid and correct even after the addition or erase of
174 ///\todo Document what can one do with a Col (INVALID, comparing,
175 ///it is similar to Node/Edge)
179 friend class LpSolverBase;
180 friend class MipSolverBase;
182 typedef Value ExprValue;
183 typedef True LpSolverCol;
185 Col(const Invalid&) : id(-1) {}
186 bool operator< (Col c) const {return id< c.id;}
187 bool operator> (Col c) const {return id> c.id;}
188 bool operator==(Col c) const {return id==c.id;}
189 bool operator!=(Col c) const {return id!=c.id;}
192 ///Refer to a row of the LP.
194 ///This type is used to refer to a row of the LP.
196 ///Its value remains valid and correct even after the addition or erase of
199 ///\todo Document what can one do with a Row (INVALID, comparing,
200 ///it is similar to Node/Edge)
204 friend class LpSolverBase;
206 typedef Value ExprValue;
207 typedef True LpSolverRow;
209 Row(const Invalid&) : id(-1) {}
211 bool operator< (Row c) const {return id< c.id;}
212 bool operator> (Row c) const {return id> c.id;}
213 bool operator==(Row c) const {return id==c.id;}
214 bool operator!=(Row c) const {return id!=c.id;}
217 ///Linear expression of variables and a constant component
219 ///This data structure strores a linear expression of the variables
220 ///(\ref Col "Col"s) and also has a constant component.
222 ///There are several ways to access and modify the contents of this
224 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
225 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
226 ///read and modify the coefficients like
233 ///or you can also iterate through its elements.
236 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
239 ///(This code computes the sum of all coefficients).
240 ///- Numbers (<tt>double</tt>'s)
241 ///and variables (\ref Col "Col"s) directly convert to an
242 ///\ref Expr and the usual linear operations are defined, so
245 ///2*v-3.12*(v-w/2)+2
246 ///v*2.1+(3*v+(v*12+w+6)*3)/2
248 ///are valid \ref Expr "Expr"essions.
249 ///The usual assignment operations are also defined.
252 ///e+=2*v-3.12*(v-w/2)+2;
256 ///- The constant member can be set and read by \ref constComp()
259 ///double c=e.constComp();
262 ///\note \ref clear() not only sets all coefficients to 0 but also
263 ///clears the constant components.
267 class Expr : public std::map<Col,Value>
270 typedef LpSolverBase::Col Key;
271 typedef LpSolverBase::Value Value;
274 typedef std::map<Col,Value> Base;
278 typedef True IsLinExpression;
280 Expr() : Base(), const_comp(0) { }
282 Expr(const Key &v) : const_comp(0) {
283 Base::insert(std::make_pair(v, 1));
286 Expr(const Value &v) : const_comp(v) {}
288 void set(const Key &v,const Value &c) {
289 Base::insert(std::make_pair(v, c));
292 Value &constComp() { return const_comp; }
294 const Value &constComp() const { return const_comp; }
296 ///Removes the components with zero coefficient.
298 for (Base::iterator i=Base::begin(); i!=Base::end();) {
301 if ((*i).second==0) Base::erase(i);
306 ///Removes the coefficients closer to zero than \c tolerance.
307 void simplify(double &tolerance) {
308 for (Base::iterator i=Base::begin(); i!=Base::end();) {
311 if (std::fabs((*i).second)<tolerance) Base::erase(i);
316 ///Sets all coefficients and the constant component to 0.
323 Expr &operator+=(const Expr &e) {
324 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
325 (*this)[j->first]+=j->second;
326 const_comp+=e.const_comp;
330 Expr &operator-=(const Expr &e) {
331 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
332 (*this)[j->first]-=j->second;
333 const_comp-=e.const_comp;
337 Expr &operator*=(const Value &c) {
338 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
344 Expr &operator/=(const Value &c) {
345 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
354 ///This data stucture represents a linear constraint in the LP.
355 ///Basically it is a linear expression with a lower or an upper bound
356 ///(or both). These parts of the constraint can be obtained by the member
357 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
359 ///There are two ways to construct a constraint.
360 ///- You can set the linear expression and the bounds directly
361 /// by the functions above.
362 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
363 /// are defined between expressions, or even between constraints whenever
364 /// it makes sense. Therefore if \c e and \c f are linear expressions and
365 /// \c s and \c t are numbers, then the followings are valid expressions
366 /// and thus they can be used directly e.g. in \ref addRow() whenever
375 ///\warning The validity of a constraint is checked only at run time, so
376 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
377 ///\ref LogicError exception.
381 typedef LpSolverBase::Expr Expr;
382 typedef Expr::Key Key;
383 typedef Expr::Value Value;
385 // static const Value INF;
386 // static const Value NaN;
393 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
395 Constr(Value lb,const Expr &e,Value ub) :
396 _expr(e), _lb(lb), _ub(ub) {}
398 Constr(const Expr &e,Value ub) :
399 _expr(e), _lb(NaN), _ub(ub) {}
401 Constr(Value lb,const Expr &e) :
402 _expr(e), _lb(lb), _ub(NaN) {}
404 Constr(const Expr &e) :
405 _expr(e), _lb(NaN), _ub(NaN) {}
413 ///Reference to the linear expression
414 Expr &expr() { return _expr; }
415 ///Cont reference to the linear expression
416 const Expr &expr() const { return _expr; }
417 ///Reference to the lower bound.
420 ///- \ref INF "INF": the constraint is lower unbounded.
421 ///- \ref NaN "NaN": lower bound has not been set.
422 ///- finite number: the lower bound
423 Value &lowerBound() { return _lb; }
424 ///The const version of \ref lowerBound()
425 const Value &lowerBound() const { return _lb; }
426 ///Reference to the upper bound.
429 ///- \ref INF "INF": the constraint is upper unbounded.
430 ///- \ref NaN "NaN": upper bound has not been set.
431 ///- finite number: the upper bound
432 Value &upperBound() { return _ub; }
433 ///The const version of \ref upperBound()
434 const Value &upperBound() const { return _ub; }
435 ///Is the constraint lower bounded?
436 bool lowerBounded() const {
440 ///Is the constraint upper bounded?
441 bool upperBounded() const {
447 ///Linear expression of rows
449 ///This data structure represents a column of the matrix,
450 ///thas is it strores a linear expression of the dual variables
451 ///(\ref Row "Row"s).
453 ///There are several ways to access and modify the contents of this
455 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
456 ///if \c e is an DualExpr and \c v
457 ///and \c w are of type \ref Row, then you can
458 ///read and modify the coefficients like
465 ///or you can also iterate through its elements.
468 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
471 ///(This code computes the sum of all coefficients).
472 ///- Numbers (<tt>double</tt>'s)
473 ///and variables (\ref Row "Row"s) directly convert to an
474 ///\ref DualExpr and the usual linear operations are defined, so
478 ///v*2.1+(3*v+(v*12+w)*3)/2
480 ///are valid \ref DualExpr "DualExpr"essions.
481 ///The usual assignment operations are also defined.
484 ///e+=2*v-3.12*(v-w/2);
491 class DualExpr : public std::map<Row,Value>
494 typedef LpSolverBase::Row Key;
495 typedef LpSolverBase::Value Value;
498 typedef std::map<Row,Value> Base;
501 typedef True IsLinExpression;
503 DualExpr() : Base() { }
505 DualExpr(const Key &v) {
506 Base::insert(std::make_pair(v, 1));
509 void set(const Key &v,const Value &c) {
510 Base::insert(std::make_pair(v, c));
513 ///Removes the components with zero coefficient.
515 for (Base::iterator i=Base::begin(); i!=Base::end();) {
518 if ((*i).second==0) Base::erase(i);
523 ///Removes the coefficients closer to zero than \c tolerance.
524 void simplify(double &tolerance) {
525 for (Base::iterator i=Base::begin(); i!=Base::end();) {
528 if (std::fabs((*i).second)<tolerance) Base::erase(i);
534 ///Sets all coefficients to 0.
540 DualExpr &operator+=(const DualExpr &e) {
541 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
542 (*this)[j->first]+=j->second;
546 DualExpr &operator-=(const DualExpr &e) {
547 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
548 (*this)[j->first]-=j->second;
552 DualExpr &operator*=(const Value &c) {
553 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
558 DualExpr &operator/=(const Value &c) {
559 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
570 //Abstract virtual functions
571 virtual LpSolverBase &_newLp() = 0;
572 virtual LpSolverBase &_copyLp(){
573 ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden.
576 LpSolverBase & newlp(_newLp());
578 //return *(LpSolverBase*)0;
581 virtual int _addCol() = 0;
582 virtual int _addRow() = 0;
583 virtual void _eraseCol(int col) = 0;
584 virtual void _eraseRow(int row) = 0;
585 virtual void _getColName(int col, std::string & name) = 0;
586 virtual void _setColName(int col, const std::string & name) = 0;
587 virtual void _setRowCoeffs(int i,
590 Value const * values ) = 0;
591 virtual void _setColCoeffs(int i,
594 Value const * values ) = 0;
595 virtual void _setCoeff(int row, int col, Value value) = 0;
596 virtual void _setColLowerBound(int i, Value value) = 0;
597 virtual void _setColUpperBound(int i, Value value) = 0;
598 // virtual void _setRowLowerBound(int i, Value value) = 0;
599 // virtual void _setRowUpperBound(int i, Value value) = 0;
600 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
601 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
602 virtual void _clearObj()=0;
603 // virtual void _setObj(int length,
604 // int const * indices,
605 // Value const * values ) = 0;
606 virtual SolveExitStatus _solve() = 0;
607 virtual Value _getPrimal(int i) = 0;
608 virtual Value _getDual(int i) = 0;
609 virtual Value _getPrimalValue() = 0;
610 virtual bool _isBasicCol(int i) = 0;
611 virtual SolutionStatus _getPrimalStatus() = 0;
612 virtual SolutionStatus _getDualStatus() = 0;
613 ///\todo This could be implemented here, too, using _getPrimalStatus() and
615 virtual ProblemTypes _getProblemType() = 0;
617 virtual void _setMax() = 0;
618 virtual void _setMin() = 0;
620 //Own protected stuff
622 //Constant component of the objective function
623 Value obj_const_comp;
631 LpSolverBase() : obj_const_comp(0) {}
634 virtual ~LpSolverBase() {}
636 ///Creates a new LP problem
637 LpSolverBase &newLp() {return _newLp();}
638 ///Makes a copy of the LP problem
639 LpSolverBase ©Lp() {return _copyLp();}
641 ///\name Build up and modify the LP
645 ///Add a new empty column (i.e a new variable) to the LP
646 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
648 ///\brief Adds several new columns
649 ///(i.e a variables) at once
651 ///This magic function takes a container as its argument
652 ///and fills its elements
653 ///with new columns (i.e. variables)
655 ///- a standard STL compatible iterable container with
656 ///\ref Col as its \c values_type
659 ///std::vector<LpSolverBase::Col>
660 ///std::list<LpSolverBase::Col>
662 ///- a standard STL compatible iterable container with
663 ///\ref Col as its \c mapped_type
666 ///std::map<AnyType,LpSolverBase::Col>
668 ///- an iterable lemon \ref concepts::WriteMap "write map" like
670 ///ListGraph::NodeMap<LpSolverBase::Col>
671 ///ListGraph::EdgeMap<LpSolverBase::Col>
673 ///\return The number of the created column.
676 int addColSet(T &t) { return 0;}
679 typename enable_if<typename T::value_type::LpSolverCol,int>::type
680 addColSet(T &t,dummy<0> = 0) {
682 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
686 typename enable_if<typename T::value_type::second_type::LpSolverCol,
688 addColSet(T &t,dummy<1> = 1) {
690 for(typename T::iterator i=t.begin();i!=t.end();++i) {
697 typename enable_if<typename T::MapIt::Value::LpSolverCol,
699 addColSet(T &t,dummy<2> = 2) {
701 for(typename T::MapIt i(t); i!=INVALID; ++i)
710 ///Set a column (i.e a dual constraint) of the LP
712 ///\param c is the column to be modified
713 ///\param e is a dual linear expression (see \ref DualExpr)
715 void col(Col c,const DualExpr &e) {
716 std::vector<int> indices;
717 std::vector<Value> values;
718 indices.push_back(0);
720 for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
722 indices.push_back(rows.floatingId((*i).first.id));
723 values.push_back((*i).second);
725 _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
726 &indices[0],&values[0]);
729 ///Add a new column to the LP
731 ///\param e is a dual linear expression (see \ref DualExpr)
732 ///\param obj is the corresponding component of the objective
733 ///function. It is 0 by default.
734 ///\return The created column.
735 Col addCol(const DualExpr &e, Value obj=0) {
742 ///Add a new empty row (i.e a new constraint) to the LP
744 ///This function adds a new empty row (i.e a new constraint) to the LP.
745 ///\return The created row
746 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
748 ///\brief Add several new rows
749 ///(i.e a constraints) at once
751 ///This magic function takes a container as its argument
752 ///and fills its elements
753 ///with new row (i.e. variables)
755 ///- a standard STL compatible iterable container with
756 ///\ref Row as its \c values_type
759 ///std::vector<LpSolverBase::Row>
760 ///std::list<LpSolverBase::Row>
762 ///- a standard STL compatible iterable container with
763 ///\ref Row as its \c mapped_type
766 ///std::map<AnyType,LpSolverBase::Row>
768 ///- an iterable lemon \ref concepts::WriteMap "write map" like
770 ///ListGraph::NodeMap<LpSolverBase::Row>
771 ///ListGraph::EdgeMap<LpSolverBase::Row>
773 ///\return The number of rows created.
776 int addRowSet(T &t) { return 0;}
779 typename enable_if<typename T::value_type::LpSolverRow,int>::type
780 addRowSet(T &t,dummy<0> = 0) {
782 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
786 typename enable_if<typename T::value_type::second_type::LpSolverRow,
788 addRowSet(T &t,dummy<1> = 1) {
790 for(typename T::iterator i=t.begin();i!=t.end();++i) {
797 typename enable_if<typename T::MapIt::Value::LpSolverRow,
799 addRowSet(T &t,dummy<2> = 2) {
801 for(typename T::MapIt i(t); i!=INVALID; ++i)
810 ///Set a row (i.e a constraint) of the LP
812 ///\param r is the row to be modified
813 ///\param l is lower bound (-\ref INF means no bound)
814 ///\param e is a linear expression (see \ref Expr)
815 ///\param u is the upper bound (\ref INF means no bound)
816 ///\bug This is a temportary function. The interface will change to
818 ///\todo Option to control whether a constraint with a single variable is
820 void row(Row r, Value l,const Expr &e, Value u) {
821 std::vector<int> indices;
822 std::vector<Value> values;
823 indices.push_back(0);
825 for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
826 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
827 indices.push_back(cols.floatingId((*i).first.id));
828 values.push_back((*i).second);
830 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
831 &indices[0],&values[0]);
832 // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
833 // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
834 _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
837 ///Set a row (i.e a constraint) of the LP
839 ///\param r is the row to be modified
840 ///\param c is a linear expression (see \ref Constr)
841 void row(Row r, const Constr &c) {
843 c.lowerBounded()?c.lowerBound():-INF,
845 c.upperBounded()?c.upperBound():INF);
848 ///Add a new row (i.e a new constraint) to the LP
850 ///\param l is the lower bound (-\ref INF means no bound)
851 ///\param e is a linear expression (see \ref Expr)
852 ///\param u is the upper bound (\ref INF means no bound)
853 ///\return The created row.
854 ///\bug This is a temportary function. The interface will change to
856 Row addRow(Value l,const Expr &e, Value u) {
862 ///Add a new row (i.e a new constraint) to the LP
864 ///\param c is a linear expression (see \ref Constr)
865 ///\return The created row.
866 Row addRow(const Constr &c) {
871 ///Erase a coloumn (i.e a variable) from the LP
873 ///\param c is the coloumn to be deleted
874 ///\todo Please check this
875 void eraseCol(Col c) {
876 _eraseCol(cols.floatingId(c.id));
879 ///Erase a row (i.e a constraint) from the LP
881 ///\param r is the row to be deleted
882 ///\todo Please check this
883 void eraseRow(Row r) {
884 _eraseRow(rows.floatingId(r.id));
888 /// Get the name of a column
890 ///\param c is the coresponding coloumn
891 ///\return The name of the colunm
892 std::string colName(Col c){
894 _getColName(cols.floatingId(c.id), name);
898 /// Set the name of a column
900 ///\param c is the coresponding coloumn
901 ///\param name The name to be given
902 void colName(Col c, const std::string & name){
903 _setColName(cols.floatingId(c.id), name);
906 /// Set an element of the coefficient matrix of the LP
908 ///\param r is the row of the element to be modified
909 ///\param c is the coloumn of the element to be modified
910 ///\param val is the new value of the coefficient
912 void coeff(Row r, Col c, Value val){
913 _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
916 /// Set the lower bound of a column (i.e a variable)
918 /// The lower bound of a variable (column) has to be given by an
919 /// extended number of type Value, i.e. a finite number of type
920 /// Value or -\ref INF.
921 void colLowerBound(Col c, Value value) {
922 _setColLowerBound(cols.floatingId(c.id),value);
925 ///\brief Set the lower bound of several columns
926 ///(i.e a variables) at once
928 ///This magic function takes a container as its argument
929 ///and applies the function on all of its elements.
930 /// The lower bound of a variable (column) has to be given by an
931 /// extended number of type Value, i.e. a finite number of type
932 /// Value or -\ref INF.
935 void colLowerBound(T &t, Value value) { return 0;}
938 typename enable_if<typename T::value_type::LpSolverCol,void>::type
939 colLowerBound(T &t, Value value,dummy<0> = 0) {
940 for(typename T::iterator i=t.begin();i!=t.end();++i) {
941 colLowerBound(*i, value);
945 typename enable_if<typename T::value_type::second_type::LpSolverCol,
947 colLowerBound(T &t, Value value,dummy<1> = 1) {
948 for(typename T::iterator i=t.begin();i!=t.end();++i) {
949 colLowerBound(i->second, value);
953 typename enable_if<typename T::MapIt::Value::LpSolverCol,
955 colLowerBound(T &t, Value value,dummy<2> = 2) {
956 for(typename T::MapIt i(t); i!=INVALID; ++i){
957 colLowerBound(*i, value);
962 /// Set the upper bound of a column (i.e a variable)
964 /// The upper bound of a variable (column) has to be given by an
965 /// extended number of type Value, i.e. a finite number of type
966 /// Value or \ref INF.
967 void colUpperBound(Col c, Value value) {
968 _setColUpperBound(cols.floatingId(c.id),value);
971 ///\brief Set the lower bound of several columns
972 ///(i.e a variables) at once
974 ///This magic function takes a container as its argument
975 ///and applies the function on all of its elements.
976 /// The upper bound of a variable (column) has to be given by an
977 /// extended number of type Value, i.e. a finite number of type
978 /// Value or \ref INF.
981 void colUpperBound(T &t, Value value) { return 0;}
984 typename enable_if<typename T::value_type::LpSolverCol,void>::type
985 colUpperBound(T &t, Value value,dummy<0> = 0) {
986 for(typename T::iterator i=t.begin();i!=t.end();++i) {
987 colUpperBound(*i, value);
991 typename enable_if<typename T::value_type::second_type::LpSolverCol,
993 colUpperBound(T &t, Value value,dummy<1> = 1) {
994 for(typename T::iterator i=t.begin();i!=t.end();++i) {
995 colUpperBound(i->second, value);
999 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1001 colUpperBound(T &t, Value value,dummy<2> = 2) {
1002 for(typename T::MapIt i(t); i!=INVALID; ++i){
1003 colUpperBound(*i, value);
1008 /// Set the lower and the upper bounds of a column (i.e a variable)
1010 /// The lower and the upper bounds of
1011 /// a variable (column) have to be given by an
1012 /// extended number of type Value, i.e. a finite number of type
1013 /// Value, -\ref INF or \ref INF.
1014 void colBounds(Col c, Value lower, Value upper) {
1015 _setColLowerBound(cols.floatingId(c.id),lower);
1016 _setColUpperBound(cols.floatingId(c.id),upper);
1019 ///\brief Set the lower and the upper bound of several columns
1020 ///(i.e a variables) at once
1022 ///This magic function takes a container as its argument
1023 ///and applies the function on all of its elements.
1024 /// The lower and the upper bounds of
1025 /// a variable (column) have to be given by an
1026 /// extended number of type Value, i.e. a finite number of type
1027 /// Value, -\ref INF or \ref INF.
1030 void colBounds(T &t, Value lower, Value upper) { return 0;}
1033 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1034 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1035 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1036 colBounds(*i, lower, upper);
1040 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1042 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1043 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1044 colBounds(i->second, lower, upper);
1048 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1050 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1051 for(typename T::MapIt i(t); i!=INVALID; ++i){
1052 colBounds(*i, lower, upper);
1057 // /// Set the lower bound of a row (i.e a constraint)
1059 // /// The lower bound of a linear expression (row) has to be given by an
1060 // /// extended number of type Value, i.e. a finite number of type
1061 // /// Value or -\ref INF.
1062 // void rowLowerBound(Row r, Value value) {
1063 // _setRowLowerBound(rows.floatingId(r.id),value);
1065 // /// Set the upper bound of a row (i.e a constraint)
1067 // /// The upper bound of a linear expression (row) 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 rowUpperBound(Row r, Value value) {
1071 // _setRowUpperBound(rows.floatingId(r.id),value);
1074 /// Set the lower and the upper bounds of a row (i.e a constraint)
1076 /// The lower and the upper bounds of
1077 /// a constraint (row) have to be given by an
1078 /// extended number of type Value, i.e. a finite number of type
1079 /// Value, -\ref INF or \ref INF.
1080 void rowBounds(Row c, Value lower, Value upper) {
1081 _setRowBounds(rows.floatingId(c.id),lower, upper);
1082 // _setRowUpperBound(rows.floatingId(c.id),upper);
1085 ///Set an element of the objective function
1086 void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
1087 ///Set the objective function
1089 ///\param e is a linear expression of type \ref Expr.
1090 ///\bug Is should be called obj()
1091 void setObj(Expr e) {
1093 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1094 objCoeff((*i).first,(*i).second);
1095 obj_const_comp=e.constComp();
1099 void max() { _setMax(); }
1101 void min() { _setMin(); }
1107 ///\name Solve the LP
1111 ///\e Solve the LP problem at hand
1113 ///\return The result of the optimization procedure. Possible
1114 ///values and their meanings can be found in the documentation of
1115 ///\ref SolveExitStatus.
1117 ///\todo Which method is used to solve the problem
1118 SolveExitStatus solve() { return _solve(); }
1122 ///\name Obtain the solution
1126 /// The status of the primal problem (the original LP problem)
1127 SolutionStatus primalStatus() {
1128 return _getPrimalStatus();
1131 /// The status of the dual (of the original LP) problem
1132 SolutionStatus dualStatus() {
1133 return _getDualStatus();
1136 ///The type of the original LP problem
1137 ProblemTypes problemType() {
1138 return _getProblemType();
1142 Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
1145 Value dual(Row r) { return _getDual(rows.floatingId(r.id)); }
1148 bool isBasicCol(Col c) { return _isBasicCol(cols.floatingId(c.id)); }
1153 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1154 /// of the primal problem, depending on whether we minimize or maximize.
1155 ///- \ref NaN if no primal solution is found.
1156 ///- The (finite) objective value if an optimal solution is found.
1157 Value primalValue() { return _getPrimalValue()+obj_const_comp;}
1163 ///Common base class for MIP solvers
1164 ///\todo Much more docs
1165 ///\ingroup gen_opt_group
1166 class MipSolverBase : virtual public LpSolverBase{
1169 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1171 ///Continuous variable
1175 ///Unfortunately, cplex 7.5 somewhere writes something like
1176 ///#define INTEGER 'I'
1178 ///\todo No support for other types yet.
1181 ///Sets the type of the given coloumn to the given type
1183 ///Sets the type of the given coloumn to the given type.
1184 void colType(Col c, ColTypes col_type) {
1185 _colType(cols.floatingId(c.id),col_type);
1188 ///Gives back the type of the column.
1190 ///Gives back the type of the column.
1191 ColTypes colType(Col c){
1192 return _colType(cols.floatingId(c.id));
1195 ///Sets the type of the given Col to integer or remove that property.
1197 ///Sets the type of the given Col to integer or remove that property.
1198 void integer(Col c, bool enable) {
1205 ///Gives back whether the type of the column is integer or not.
1207 ///Gives back the type of the column.
1208 ///\return true if the column has integer type and false if not.
1209 bool integer(Col c){
1210 return (colType(c)==INT);
1213 /// The status of the MIP problem
1214 SolutionStatus mipStatus() {
1215 return _getMipStatus();
1220 virtual ColTypes _colType(int col) = 0;
1221 virtual void _colType(int col, ColTypes col_type) = 0;
1222 virtual SolutionStatus _getMipStatus()=0;
1226 ///\relates LpSolverBase::Expr
1228 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1229 const LpSolverBase::Expr &b)
1231 LpSolverBase::Expr tmp(a);
1237 ///\relates LpSolverBase::Expr
1239 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1240 const LpSolverBase::Expr &b)
1242 LpSolverBase::Expr tmp(a);
1248 ///\relates LpSolverBase::Expr
1250 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1251 const LpSolverBase::Value &b)
1253 LpSolverBase::Expr tmp(a);
1260 ///\relates LpSolverBase::Expr
1262 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1263 const LpSolverBase::Expr &b)
1265 LpSolverBase::Expr tmp(b);
1271 ///\relates LpSolverBase::Expr
1273 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1274 const LpSolverBase::Value &b)
1276 LpSolverBase::Expr tmp(a);
1283 ///\relates LpSolverBase::Constr
1285 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1286 const LpSolverBase::Expr &f)
1288 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1293 ///\relates LpSolverBase::Constr
1295 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1296 const LpSolverBase::Expr &f)
1298 return LpSolverBase::Constr(e,f);
1303 ///\relates LpSolverBase::Constr
1305 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1306 const LpSolverBase::Value &f)
1308 return LpSolverBase::Constr(e,f);
1313 ///\relates LpSolverBase::Constr
1315 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1316 const LpSolverBase::Expr &f)
1318 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1324 ///\relates LpSolverBase::Constr
1326 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1327 const LpSolverBase::Expr &f)
1329 return LpSolverBase::Constr(f,e);
1335 ///\relates LpSolverBase::Constr
1337 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1338 const LpSolverBase::Value &f)
1340 return LpSolverBase::Constr(f,e);
1345 ///\relates LpSolverBase::Constr
1347 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1348 const LpSolverBase::Expr &f)
1350 return LpSolverBase::Constr(0,e-f,0);
1355 ///\relates LpSolverBase::Constr
1357 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1358 const LpSolverBase::Constr&c)
1360 LpSolverBase::Constr tmp(c);
1361 ///\todo Create an own exception type.
1362 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1363 else tmp.lowerBound()=n;
1368 ///\relates LpSolverBase::Constr
1370 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1371 const LpSolverBase::Value &n)
1373 LpSolverBase::Constr tmp(c);
1374 ///\todo Create an own exception type.
1375 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1376 else tmp.upperBound()=n;
1382 ///\relates LpSolverBase::Constr
1384 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1385 const LpSolverBase::Constr&c)
1387 LpSolverBase::Constr tmp(c);
1388 ///\todo Create an own exception type.
1389 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1390 else tmp.upperBound()=n;
1395 ///\relates LpSolverBase::Constr
1397 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1398 const LpSolverBase::Value &n)
1400 LpSolverBase::Constr tmp(c);
1401 ///\todo Create an own exception type.
1402 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1403 else tmp.lowerBound()=n;
1409 ///\relates LpSolverBase::DualExpr
1411 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1412 const LpSolverBase::DualExpr &b)
1414 LpSolverBase::DualExpr tmp(a);
1420 ///\relates LpSolverBase::DualExpr
1422 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1423 const LpSolverBase::DualExpr &b)
1425 LpSolverBase::DualExpr tmp(a);
1431 ///\relates LpSolverBase::DualExpr
1433 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1434 const LpSolverBase::Value &b)
1436 LpSolverBase::DualExpr tmp(a);
1443 ///\relates LpSolverBase::DualExpr
1445 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1446 const LpSolverBase::DualExpr &b)
1448 LpSolverBase::DualExpr tmp(b);
1454 ///\relates LpSolverBase::DualExpr
1456 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1457 const LpSolverBase::Value &b)
1459 LpSolverBase::DualExpr tmp(a);
1467 #endif //LEMON_LP_BASE_H