Fixed the mip error (mipstatus was incorrect).
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];
78 ///\todo Create an own exception type.
79 else throw LogicError(); //floatingId-s must form a continuous range;
83 ///\param n is a fix id
90 for(int i=fl+1;i<int(cross.size());++i) {
96 ///An upper bound on the largest fix id.
98 ///\todo Do we need this?
100 std::size_t maxFixId() { return cross.size()-1; }
104 ///Common base class for LP solvers
106 ///\todo Much more docs
107 ///\ingroup gen_opt_group
112 ///Possible outcomes of an LP solving procedure
113 enum SolveExitStatus {
114 ///This means that the problem has been successfully solved: either
115 ///an optimal solution has been found or infeasibility/unboundedness
118 ///Any other case (including the case when some user specified limit has been exceeded)
123 enum SolutionStatus {
124 ///Feasible solution hasn't been found (but may exist).
126 ///\todo NOTFOUND might be a better name.
129 ///The problem has no feasible solution
131 ///Feasible solution found
133 ///Optimal solution exists and found
135 ///The cost function is unbounded
137 ///\todo Give a feasible solution and an infinite ray (and the
138 ///corresponding bases)
142 ///\e The type of the investigated LP problem
144 ///Primal-dual feasible
145 PRIMAL_DUAL_FEASIBLE = 0,
146 ///Primal feasible dual infeasible
147 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
148 ///Primal infeasible dual feasible
149 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
150 ///Primal-dual infeasible
151 PRIMAL_DUAL_INFEASIBLE = 3,
152 ///Could not determine so far
156 ///The floating point type used by the solver
157 typedef double Value;
158 ///The infinity constant
159 static const Value INF;
160 ///The not a number constant
161 static const Value NaN;
163 static inline bool isNaN(const Value& v) { return v!=v; }
165 ///Refer to a column of the LP.
167 ///This type is used to refer to a column of the LP.
169 ///Its value remains valid and correct even after the addition or erase of
172 ///\todo Document what can one do with a Col (INVALID, comparing,
173 ///it is similar to Node/Edge)
177 friend class LpSolverBase;
178 friend class MipSolverBase;
180 typedef Value ExprValue;
181 typedef True LpSolverCol;
183 Col(const Invalid&) : id(-1) {}
184 bool operator< (Col c) const {return id< c.id;}
185 bool operator> (Col c) const {return id> c.id;}
186 bool operator==(Col c) const {return id==c.id;}
187 bool operator!=(Col c) const {return id!=c.id;}
190 ///Refer to a row of the LP.
192 ///This type is used to refer to a row of the LP.
194 ///Its value remains valid and correct even after the addition or erase of
197 ///\todo Document what can one do with a Row (INVALID, comparing,
198 ///it is similar to Node/Edge)
202 friend class LpSolverBase;
204 typedef Value ExprValue;
205 typedef True LpSolverRow;
207 Row(const Invalid&) : id(-1) {}
209 bool operator< (Row c) const {return id< c.id;}
210 bool operator> (Row c) const {return id> c.id;}
211 bool operator==(Row c) const {return id==c.id;}
212 bool operator!=(Row c) const {return id!=c.id;}
215 ///Linear expression of variables and a constant component
217 ///This data structure strores a linear expression of the variables
218 ///(\ref Col "Col"s) and also has a constant component.
220 ///There are several ways to access and modify the contents of this
222 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
223 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
224 ///read and modify the coefficients like
231 ///or you can also iterate through its elements.
234 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
237 ///(This code computes the sum of all coefficients).
238 ///- Numbers (<tt>double</tt>'s)
239 ///and variables (\ref Col "Col"s) directly convert to an
240 ///\ref Expr and the usual linear operations are defined, so
243 ///2*v-3.12*(v-w/2)+2
244 ///v*2.1+(3*v+(v*12+w+6)*3)/2
246 ///are valid \ref Expr "Expr"essions.
247 ///The usual assignment operations are also defined.
250 ///e+=2*v-3.12*(v-w/2)+2;
254 ///- The constant member can be set and read by \ref constComp()
257 ///double c=e.constComp();
260 ///\note \ref clear() not only sets all coefficients to 0 but also
261 ///clears the constant components.
265 class Expr : public std::map<Col,Value>
268 typedef LpSolverBase::Col Key;
269 typedef LpSolverBase::Value Value;
272 typedef std::map<Col,Value> Base;
276 typedef True IsLinExpression;
278 Expr() : Base(), const_comp(0) { }
280 Expr(const Key &v) : const_comp(0) {
281 Base::insert(std::make_pair(v, 1));
284 Expr(const Value &v) : const_comp(v) {}
286 void set(const Key &v,const Value &c) {
287 Base::insert(std::make_pair(v, c));
290 Value &constComp() { return const_comp; }
292 const Value &constComp() const { return const_comp; }
294 ///Removes the components with zero coefficient.
296 for (Base::iterator i=Base::begin(); i!=Base::end();) {
299 if ((*i).second==0) Base::erase(i);
304 ///Removes the coefficients closer to zero than \c tolerance.
305 void simplify(double &tolerance) {
306 for (Base::iterator i=Base::begin(); i!=Base::end();) {
309 if (std::fabs((*i).second)<tolerance) Base::erase(i);
314 ///Sets all coefficients and the constant component to 0.
321 Expr &operator+=(const Expr &e) {
322 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
323 (*this)[j->first]+=j->second;
324 const_comp+=e.const_comp;
328 Expr &operator-=(const Expr &e) {
329 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
330 (*this)[j->first]-=j->second;
331 const_comp-=e.const_comp;
335 Expr &operator*=(const Value &c) {
336 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
342 Expr &operator/=(const Value &c) {
343 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
352 ///This data stucture represents a linear constraint in the LP.
353 ///Basically it is a linear expression with a lower or an upper bound
354 ///(or both). These parts of the constraint can be obtained by the member
355 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
357 ///There are two ways to construct a constraint.
358 ///- You can set the linear expression and the bounds directly
359 /// by the functions above.
360 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
361 /// are defined between expressions, or even between constraints whenever
362 /// it makes sense. Therefore if \c e and \c f are linear expressions and
363 /// \c s and \c t are numbers, then the followings are valid expressions
364 /// and thus they can be used directly e.g. in \ref addRow() whenever
373 ///\warning The validity of a constraint is checked only at run time, so
374 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
375 ///\ref LogicError exception.
379 typedef LpSolverBase::Expr Expr;
380 typedef Expr::Key Key;
381 typedef Expr::Value Value;
383 // static const Value INF;
384 // static const Value NaN;
391 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
393 Constr(Value lb,const Expr &e,Value ub) :
394 _expr(e), _lb(lb), _ub(ub) {}
396 Constr(const Expr &e,Value ub) :
397 _expr(e), _lb(NaN), _ub(ub) {}
399 Constr(Value lb,const Expr &e) :
400 _expr(e), _lb(lb), _ub(NaN) {}
402 Constr(const Expr &e) :
403 _expr(e), _lb(NaN), _ub(NaN) {}
411 ///Reference to the linear expression
412 Expr &expr() { return _expr; }
413 ///Cont reference to the linear expression
414 const Expr &expr() const { return _expr; }
415 ///Reference to the lower bound.
418 ///- \ref INF "INF": the constraint is lower unbounded.
419 ///- \ref NaN "NaN": lower bound has not been set.
420 ///- finite number: the lower bound
421 Value &lowerBound() { return _lb; }
422 ///The const version of \ref lowerBound()
423 const Value &lowerBound() const { return _lb; }
424 ///Reference to the upper bound.
427 ///- \ref INF "INF": the constraint is upper unbounded.
428 ///- \ref NaN "NaN": upper bound has not been set.
429 ///- finite number: the upper bound
430 Value &upperBound() { return _ub; }
431 ///The const version of \ref upperBound()
432 const Value &upperBound() const { return _ub; }
433 ///Is the constraint lower bounded?
434 bool lowerBounded() const {
438 ///Is the constraint upper bounded?
439 bool upperBounded() const {
445 ///Linear expression of rows
447 ///This data structure represents a column of the matrix,
448 ///thas is it strores a linear expression of the dual variables
449 ///(\ref Row "Row"s).
451 ///There are several ways to access and modify the contents of this
453 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
454 ///if \c e is an DualExpr and \c v
455 ///and \c w are of type \ref Row, then you can
456 ///read and modify the coefficients like
463 ///or you can also iterate through its elements.
466 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
469 ///(This code computes the sum of all coefficients).
470 ///- Numbers (<tt>double</tt>'s)
471 ///and variables (\ref Row "Row"s) directly convert to an
472 ///\ref DualExpr and the usual linear operations are defined, so
476 ///v*2.1+(3*v+(v*12+w)*3)/2
478 ///are valid \ref DualExpr "DualExpr"essions.
479 ///The usual assignment operations are also defined.
482 ///e+=2*v-3.12*(v-w/2);
489 class DualExpr : public std::map<Row,Value>
492 typedef LpSolverBase::Row Key;
493 typedef LpSolverBase::Value Value;
496 typedef std::map<Row,Value> Base;
499 typedef True IsLinExpression;
501 DualExpr() : Base() { }
503 DualExpr(const Key &v) {
504 Base::insert(std::make_pair(v, 1));
507 void set(const Key &v,const Value &c) {
508 Base::insert(std::make_pair(v, c));
511 ///Removes the components with zero coefficient.
513 for (Base::iterator i=Base::begin(); i!=Base::end();) {
516 if ((*i).second==0) Base::erase(i);
521 ///Removes the coefficients closer to zero than \c tolerance.
522 void simplify(double &tolerance) {
523 for (Base::iterator i=Base::begin(); i!=Base::end();) {
526 if (std::fabs((*i).second)<tolerance) Base::erase(i);
532 ///Sets all coefficients to 0.
538 DualExpr &operator+=(const DualExpr &e) {
539 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
540 (*this)[j->first]+=j->second;
544 DualExpr &operator-=(const DualExpr &e) {
545 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
546 (*this)[j->first]-=j->second;
550 DualExpr &operator*=(const Value &c) {
551 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
556 DualExpr &operator/=(const Value &c) {
557 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
568 //Abstract virtual functions
569 virtual LpSolverBase &_newLp() = 0;
570 virtual LpSolverBase &_copyLp(){
571 ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden.
574 LpSolverBase & newlp(_newLp());
576 //return *(LpSolverBase*)0;
579 virtual int _addCol() = 0;
580 virtual int _addRow() = 0;
581 virtual void _eraseCol(int col) = 0;
582 virtual void _eraseRow(int row) = 0;
583 virtual void _getColName(int col, std::string & name) = 0;
584 virtual void _setColName(int col, const std::string & name) = 0;
585 virtual void _setRowCoeffs(int i,
588 Value const * values ) = 0;
589 virtual void _setColCoeffs(int i,
592 Value const * values ) = 0;
593 virtual void _setCoeff(int row, int col, Value value) = 0;
594 virtual void _setColLowerBound(int i, Value value) = 0;
595 virtual void _setColUpperBound(int i, Value value) = 0;
596 // virtual void _setRowLowerBound(int i, Value value) = 0;
597 // virtual void _setRowUpperBound(int i, Value value) = 0;
598 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
599 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
600 virtual void _clearObj()=0;
601 // virtual void _setObj(int length,
602 // int const * indices,
603 // Value const * values ) = 0;
604 virtual SolveExitStatus _solve() = 0;
605 virtual Value _getPrimal(int i) = 0;
606 virtual Value _getDual(int i) = 0;
607 virtual Value _getPrimalValue() = 0;
608 virtual bool _isBasicCol(int i) = 0;
609 virtual SolutionStatus _getPrimalStatus() = 0;
610 virtual SolutionStatus _getDualStatus() = 0;
611 ///\todo This could be implemented here, too, using _getPrimalStatus() and
613 virtual ProblemTypes _getProblemType() = 0;
615 virtual void _setMax() = 0;
616 virtual void _setMin() = 0;
618 //Own protected stuff
620 //Constant component of the objective function
621 Value obj_const_comp;
629 LpSolverBase() : obj_const_comp(0) {}
632 virtual ~LpSolverBase() {}
634 ///Creates a new LP problem
635 LpSolverBase &newLp() {return _newLp();}
636 ///Makes a copy of the LP problem
637 LpSolverBase ©Lp() {return _copyLp();}
639 ///\name Build up and modify the LP
643 ///Add a new empty column (i.e a new variable) to the LP
644 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
646 ///\brief Adds several new columns
647 ///(i.e a variables) at once
649 ///This magic function takes a container as its argument
650 ///and fills its elements
651 ///with new columns (i.e. variables)
653 ///- a standard STL compatible iterable container with
654 ///\ref Col as its \c values_type
657 ///std::vector<LpSolverBase::Col>
658 ///std::list<LpSolverBase::Col>
660 ///- a standard STL compatible iterable container with
661 ///\ref Col as its \c mapped_type
664 ///std::map<AnyType,LpSolverBase::Col>
666 ///- an iterable lemon \ref concept::WriteMap "write map" like
668 ///ListGraph::NodeMap<LpSolverBase::Col>
669 ///ListGraph::EdgeMap<LpSolverBase::Col>
671 ///\return The number of the created column.
674 int addColSet(T &t) { return 0;}
677 typename enable_if<typename T::value_type::LpSolverCol,int>::type
678 addColSet(T &t,dummy<0> = 0) {
680 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
684 typename enable_if<typename T::value_type::second_type::LpSolverCol,
686 addColSet(T &t,dummy<1> = 1) {
688 for(typename T::iterator i=t.begin();i!=t.end();++i) {
695 typename enable_if<typename T::MapIt::Value::LpSolverCol,
697 addColSet(T &t,dummy<2> = 2) {
699 for(typename T::MapIt i(t); i!=INVALID; ++i)
708 ///Set a column (i.e a dual constraint) of the LP
710 ///\param c is the column to be modified
711 ///\param e is a dual linear expression (see \ref DualExpr)
713 void col(Col c,const DualExpr &e) {
714 std::vector<int> indices;
715 std::vector<Value> values;
716 indices.push_back(0);
718 for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
720 indices.push_back(rows.floatingId((*i).first.id));
721 values.push_back((*i).second);
723 _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
724 &indices[0],&values[0]);
727 ///Add a new column to the LP
729 ///\param e is a dual linear expression (see \ref DualExpr)
730 ///\param obj is the corresponding component of the objective
731 ///function. It is 0 by default.
732 ///\return The created column.
733 Col addCol(const DualExpr &e, Value obj=0) {
740 ///Add a new empty row (i.e a new constraint) to the LP
742 ///This function adds a new empty row (i.e a new constraint) to the LP.
743 ///\return The created row
744 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
746 ///\brief Add several new rows
747 ///(i.e a constraints) at once
749 ///This magic function takes a container as its argument
750 ///and fills its elements
751 ///with new row (i.e. variables)
753 ///- a standard STL compatible iterable container with
754 ///\ref Row as its \c values_type
757 ///std::vector<LpSolverBase::Row>
758 ///std::list<LpSolverBase::Row>
760 ///- a standard STL compatible iterable container with
761 ///\ref Row as its \c mapped_type
764 ///std::map<AnyType,LpSolverBase::Row>
766 ///- an iterable lemon \ref concept::WriteMap "write map" like
768 ///ListGraph::NodeMap<LpSolverBase::Row>
769 ///ListGraph::EdgeMap<LpSolverBase::Row>
771 ///\return The number of rows created.
774 int addRowSet(T &t) { return 0;}
777 typename enable_if<typename T::value_type::LpSolverRow,int>::type
778 addRowSet(T &t,dummy<0> = 0) {
780 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
784 typename enable_if<typename T::value_type::second_type::LpSolverRow,
786 addRowSet(T &t,dummy<1> = 1) {
788 for(typename T::iterator i=t.begin();i!=t.end();++i) {
795 typename enable_if<typename T::MapIt::Value::LpSolverRow,
797 addRowSet(T &t,dummy<2> = 2) {
799 for(typename T::MapIt i(t); i!=INVALID; ++i)
808 ///Set a row (i.e a constraint) of the LP
810 ///\param r is the row to be modified
811 ///\param l is lower bound (-\ref INF means no bound)
812 ///\param e is a linear expression (see \ref Expr)
813 ///\param u is the upper bound (\ref INF means no bound)
814 ///\bug This is a temportary function. The interface will change to
816 ///\todo Option to control whether a constraint with a single variable is
818 void row(Row r, Value l,const Expr &e, Value u) {
819 std::vector<int> indices;
820 std::vector<Value> values;
821 indices.push_back(0);
823 for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
824 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
825 indices.push_back(cols.floatingId((*i).first.id));
826 values.push_back((*i).second);
828 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
829 &indices[0],&values[0]);
830 // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
831 // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
832 _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
835 ///Set a row (i.e a constraint) of the LP
837 ///\param r is the row to be modified
838 ///\param c is a linear expression (see \ref Constr)
839 void row(Row r, const Constr &c) {
841 c.lowerBounded()?c.lowerBound():-INF,
843 c.upperBounded()?c.upperBound():INF);
846 ///Add a new row (i.e a new constraint) to the LP
848 ///\param l is the lower bound (-\ref INF means no bound)
849 ///\param e is a linear expression (see \ref Expr)
850 ///\param u is the upper bound (\ref INF means no bound)
851 ///\return The created row.
852 ///\bug This is a temportary function. The interface will change to
854 Row addRow(Value l,const Expr &e, Value u) {
860 ///Add a new row (i.e a new constraint) to the LP
862 ///\param c is a linear expression (see \ref Constr)
863 ///\return The created row.
864 Row addRow(const Constr &c) {
869 ///Erase a coloumn (i.e a variable) from the LP
871 ///\param c is the coloumn to be deleted
872 ///\todo Please check this
873 void eraseCol(Col c) {
874 _eraseCol(cols.floatingId(c.id));
877 ///Erase a row (i.e a constraint) from the LP
879 ///\param r is the row to be deleted
880 ///\todo Please check this
881 void eraseRow(Row r) {
882 _eraseRow(rows.floatingId(r.id));
886 /// Get the name of a column
888 ///\param c is the coresponding coloumn
889 ///\return The name of the colunm
890 std::string ColName(Col c){
892 _getColName(cols.floatingId(c.id), name);
896 /// Set the name of a column
898 ///\param c is the coresponding coloumn
899 ///\param name The name to be given
900 void ColName(Col c, const std::string & name){
901 _setColName(cols.floatingId(c.id), name);
904 /// Set an element of the coefficient matrix of the LP
906 ///\param r is the row of the element to be modified
907 ///\param c is the coloumn of the element to be modified
908 ///\param val is the new value of the coefficient
910 void Coeff(Row r, Col c, Value val){
911 _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
914 /// Set the lower bound of a column (i.e a variable)
916 /// The lower bound of a variable (column) has to be given by an
917 /// extended number of type Value, i.e. a finite number of type
918 /// Value or -\ref INF.
919 void colLowerBound(Col c, Value value) {
920 _setColLowerBound(cols.floatingId(c.id),value);
923 ///\brief Set the lower bound of several columns
924 ///(i.e a variables) at once
926 ///This magic function takes a container as its argument
927 ///and applies the function on all of its elements.
928 /// The lower bound of a variable (column) has to be given by an
929 /// extended number of type Value, i.e. a finite number of type
930 /// Value or -\ref INF.
933 void colLowerBound(T &t, Value value) { return 0;}
936 typename enable_if<typename T::value_type::LpSolverCol,void>::type
937 colLowerBound(T &t, Value value,dummy<0> = 0) {
938 for(typename T::iterator i=t.begin();i!=t.end();++i) {
939 colLowerBound(*i, value);
943 typename enable_if<typename T::value_type::second_type::LpSolverCol,
945 colLowerBound(T &t, Value value,dummy<1> = 1) {
946 for(typename T::iterator i=t.begin();i!=t.end();++i) {
947 colLowerBound(i->second, value);
951 typename enable_if<typename T::MapIt::Value::LpSolverCol,
953 colLowerBound(T &t, Value value,dummy<2> = 2) {
954 for(typename T::MapIt i(t); i!=INVALID; ++i){
955 colLowerBound(*i, value);
960 /// Set the upper bound of a column (i.e a variable)
962 /// The upper bound of a variable (column) has to be given by an
963 /// extended number of type Value, i.e. a finite number of type
964 /// Value or \ref INF.
965 void colUpperBound(Col c, Value value) {
966 _setColUpperBound(cols.floatingId(c.id),value);
969 ///\brief Set the lower bound of several columns
970 ///(i.e a variables) at once
972 ///This magic function takes a container as its argument
973 ///and applies the function on all of its elements.
974 /// The upper bound of a variable (column) has to be given by an
975 /// extended number of type Value, i.e. a finite number of type
976 /// Value or \ref INF.
979 void colUpperBound(T &t, Value value) { return 0;}
982 typename enable_if<typename T::value_type::LpSolverCol,void>::type
983 colUpperBound(T &t, Value value,dummy<0> = 0) {
984 for(typename T::iterator i=t.begin();i!=t.end();++i) {
985 colUpperBound(*i, value);
989 typename enable_if<typename T::value_type::second_type::LpSolverCol,
991 colUpperBound(T &t, Value value,dummy<1> = 1) {
992 for(typename T::iterator i=t.begin();i!=t.end();++i) {
993 colUpperBound(i->second, value);
997 typename enable_if<typename T::MapIt::Value::LpSolverCol,
999 colUpperBound(T &t, Value value,dummy<2> = 2) {
1000 for(typename T::MapIt i(t); i!=INVALID; ++i){
1001 colUpperBound(*i, value);
1006 /// Set the lower and the upper bounds of a column (i.e a variable)
1008 /// The lower and the upper bounds of
1009 /// a variable (column) have to be given by an
1010 /// extended number of type Value, i.e. a finite number of type
1011 /// Value, -\ref INF or \ref INF.
1012 void colBounds(Col c, Value lower, Value upper) {
1013 _setColLowerBound(cols.floatingId(c.id),lower);
1014 _setColUpperBound(cols.floatingId(c.id),upper);
1017 ///\brief Set the lower and the upper bound of several columns
1018 ///(i.e a variables) at once
1020 ///This magic function takes a container as its argument
1021 ///and applies the function on all of its elements.
1022 /// The lower and the upper bounds of
1023 /// a variable (column) have to be given by an
1024 /// extended number of type Value, i.e. a finite number of type
1025 /// Value, -\ref INF or \ref INF.
1028 void colBounds(T &t, Value lower, Value upper) { return 0;}
1031 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1032 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1033 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1034 colBounds(*i, lower, upper);
1038 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1040 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1041 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1042 colBounds(i->second, lower, upper);
1046 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1048 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1049 for(typename T::MapIt i(t); i!=INVALID; ++i){
1050 colBounds(*i, lower, upper);
1055 // /// Set the lower bound of a row (i.e a constraint)
1057 // /// The lower bound of a linear expression (row) has to be given by an
1058 // /// extended number of type Value, i.e. a finite number of type
1059 // /// Value or -\ref INF.
1060 // void rowLowerBound(Row r, Value value) {
1061 // _setRowLowerBound(rows.floatingId(r.id),value);
1063 // /// Set the upper bound of a row (i.e a constraint)
1065 // /// The upper bound of a linear expression (row) has to be given by an
1066 // /// extended number of type Value, i.e. a finite number of type
1067 // /// Value or \ref INF.
1068 // void rowUpperBound(Row r, Value value) {
1069 // _setRowUpperBound(rows.floatingId(r.id),value);
1072 /// Set the lower and the upper bounds of a row (i.e a constraint)
1074 /// The lower and the upper bounds of
1075 /// a constraint (row) have to be given by an
1076 /// extended number of type Value, i.e. a finite number of type
1077 /// Value, -\ref INF or \ref INF.
1078 void rowBounds(Row c, Value lower, Value upper) {
1079 _setRowBounds(rows.floatingId(c.id),lower, upper);
1080 // _setRowUpperBound(rows.floatingId(c.id),upper);
1083 ///Set an element of the objective function
1084 void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
1085 ///Set the objective function
1087 ///\param e is a linear expression of type \ref Expr.
1088 ///\bug Is should be called obj()
1089 void setObj(Expr e) {
1091 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1092 objCoeff((*i).first,(*i).second);
1093 obj_const_comp=e.constComp();
1097 void max() { _setMax(); }
1099 void min() { _setMin(); }
1105 ///\name Solve the LP
1109 ///\e Solve the LP problem at hand
1111 ///\return The result of the optimization procedure. Possible
1112 ///values and their meanings can be found in the documentation of
1113 ///\ref SolveExitStatus.
1115 ///\todo Which method is used to solve the problem
1116 SolveExitStatus solve() { return _solve(); }
1120 ///\name Obtain the solution
1124 /// The status of the primal problem (the original LP problem)
1125 SolutionStatus primalStatus() {
1126 return _getPrimalStatus();
1129 /// The status of the dual (of the original LP) problem
1130 SolutionStatus dualStatus() {
1131 return _getDualStatus();
1134 ///The type of the original LP problem
1135 ProblemTypes problemType() {
1136 return _getProblemType();
1140 Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
1143 Value dual(Row r) { return _getDual(rows.floatingId(r.id)); }
1146 bool isBasicCol(Col c) { return _isBasicCol(cols.floatingId(c.id)); }
1151 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1152 /// of the primal problem, depending on whether we minimize or maximize.
1153 ///- \ref NaN if no primal solution is found.
1154 ///- The (finite) objective value if an optimal solution is found.
1155 Value primalValue() { return _getPrimalValue()+obj_const_comp;}
1161 ///Common base class for MIP solvers
1162 ///\todo Much more docs
1163 ///\ingroup gen_opt_group
1164 class MipSolverBase : virtual public LpSolverBase{
1167 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1169 ///Continuous variable
1173 ///\todo No support for other types yet.
1176 ///Sets the type of the given coloumn to the given type
1178 ///Sets the type of the given coloumn to the given type.
1179 void colType(Col c, ColTypes col_type) {
1180 _colType(cols.floatingId(c.id),col_type);
1183 ///Gives back the type of the column.
1185 ///Gives back the type of the column.
1186 ColTypes colType(Col c){
1187 return _colType(cols.floatingId(c.id));
1190 ///Sets the type of the given Col to integer or remove that property.
1192 ///Sets the type of the given Col to integer or remove that property.
1193 void integer(Col c, bool enable) {
1200 ///Gives back whether the type of the column is integer or not.
1202 ///Gives back the type of the column.
1203 ///\return true if the column has integer type and false if not.
1204 bool integer(Col c){
1205 return (colType(c)==INTEGER);
1208 /// The status of the MIP problem
1209 SolutionStatus mipStatus() {
1210 return _getMipStatus();
1215 virtual ColTypes _colType(int col) = 0;
1216 virtual void _colType(int col, ColTypes col_type) = 0;
1217 virtual SolutionStatus _getMipStatus()=0;
1221 ///\relates LpSolverBase::Expr
1223 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1224 const LpSolverBase::Expr &b)
1226 LpSolverBase::Expr tmp(a);
1232 ///\relates LpSolverBase::Expr
1234 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1235 const LpSolverBase::Expr &b)
1237 LpSolverBase::Expr tmp(a);
1243 ///\relates LpSolverBase::Expr
1245 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1246 const LpSolverBase::Value &b)
1248 LpSolverBase::Expr tmp(a);
1255 ///\relates LpSolverBase::Expr
1257 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1258 const LpSolverBase::Expr &b)
1260 LpSolverBase::Expr tmp(b);
1266 ///\relates LpSolverBase::Expr
1268 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1269 const LpSolverBase::Value &b)
1271 LpSolverBase::Expr tmp(a);
1278 ///\relates LpSolverBase::Constr
1280 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1281 const LpSolverBase::Expr &f)
1283 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1288 ///\relates LpSolverBase::Constr
1290 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1291 const LpSolverBase::Expr &f)
1293 return LpSolverBase::Constr(e,f);
1298 ///\relates LpSolverBase::Constr
1300 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1301 const LpSolverBase::Value &f)
1303 return LpSolverBase::Constr(e,f);
1308 ///\relates LpSolverBase::Constr
1310 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1311 const LpSolverBase::Expr &f)
1313 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1319 ///\relates LpSolverBase::Constr
1321 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1322 const LpSolverBase::Expr &f)
1324 return LpSolverBase::Constr(f,e);
1330 ///\relates LpSolverBase::Constr
1332 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1333 const LpSolverBase::Value &f)
1335 return LpSolverBase::Constr(f,e);
1340 ///\relates LpSolverBase::Constr
1342 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1343 const LpSolverBase::Expr &f)
1345 return LpSolverBase::Constr(0,e-f,0);
1350 ///\relates LpSolverBase::Constr
1352 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1353 const LpSolverBase::Constr&c)
1355 LpSolverBase::Constr tmp(c);
1356 ///\todo Create an own exception type.
1357 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1358 else tmp.lowerBound()=n;
1363 ///\relates LpSolverBase::Constr
1365 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1366 const LpSolverBase::Value &n)
1368 LpSolverBase::Constr tmp(c);
1369 ///\todo Create an own exception type.
1370 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1371 else tmp.upperBound()=n;
1377 ///\relates LpSolverBase::Constr
1379 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1380 const LpSolverBase::Constr&c)
1382 LpSolverBase::Constr tmp(c);
1383 ///\todo Create an own exception type.
1384 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1385 else tmp.upperBound()=n;
1390 ///\relates LpSolverBase::Constr
1392 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1393 const LpSolverBase::Value &n)
1395 LpSolverBase::Constr tmp(c);
1396 ///\todo Create an own exception type.
1397 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1398 else tmp.lowerBound()=n;
1404 ///\relates LpSolverBase::DualExpr
1406 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1407 const LpSolverBase::DualExpr &b)
1409 LpSolverBase::DualExpr tmp(a);
1415 ///\relates LpSolverBase::DualExpr
1417 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1418 const LpSolverBase::DualExpr &b)
1420 LpSolverBase::DualExpr tmp(a);
1426 ///\relates LpSolverBase::DualExpr
1428 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1429 const LpSolverBase::Value &b)
1431 LpSolverBase::DualExpr tmp(a);
1438 ///\relates LpSolverBase::DualExpr
1440 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1441 const LpSolverBase::DualExpr &b)
1443 LpSolverBase::DualExpr tmp(b);
1449 ///\relates LpSolverBase::DualExpr
1451 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1452 const LpSolverBase::Value &b)
1454 LpSolverBase::DualExpr tmp(a);
1462 #endif //LEMON_LP_BASE_H