Minor comment added.
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 has'n 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;
179 typedef Value ExprValue;
180 typedef True LpSolverCol;
182 Col(const Invalid&) : id(-1) {}
183 bool operator< (Col c) const {return id< c.id;}
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;}
189 ///Refer to a row of the LP.
191 ///This type is used to refer to a row of the LP.
193 ///Its value remains valid and correct even after the addition or erase of
196 ///\todo Document what can one do with a Row (INVALID, comparing,
197 ///it is similar to Node/Edge)
201 friend class LpSolverBase;
203 typedef Value ExprValue;
204 typedef True LpSolverRow;
206 Row(const Invalid&) : id(-1) {}
208 bool operator< (Row c) const {return id< c.id;}
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;}
214 ///Linear expression of variables and a constant component
216 ///This data structure strores a linear expression of the variables
217 ///(\ref Col "Col"s) and also has a constant component.
219 ///There are several ways to access and modify the contents of this
221 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
222 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
223 ///read and modify the coefficients like
230 ///or you can also iterate through its elements.
233 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
236 ///(This code computes the sum of all coefficients).
237 ///- Numbers (<tt>double</tt>'s)
238 ///and variables (\ref Col "Col"s) directly convert to an
239 ///\ref Expr and the usual linear operations are defined, so
242 ///2*v-3.12*(v-w/2)+2
243 ///v*2.1+(3*v+(v*12+w+6)*3)/2
245 ///are valid \ref Expr "Expr"essions.
246 ///The usual assignment operations are also defined.
249 ///e+=2*v-3.12*(v-w/2)+2;
253 ///- The constant member can be set and read by \ref constComp()
256 ///double c=e.constComp();
259 ///\note \ref clear() not only sets all coefficients to 0 but also
260 ///clears the constant components.
264 class Expr : public std::map<Col,Value>
267 typedef LpSolverBase::Col Key;
268 typedef LpSolverBase::Value Value;
271 typedef std::map<Col,Value> Base;
275 typedef True IsLinExpression;
277 Expr() : Base(), const_comp(0) { }
279 Expr(const Key &v) : const_comp(0) {
280 Base::insert(std::make_pair(v, 1));
283 Expr(const Value &v) : const_comp(v) {}
285 void set(const Key &v,const Value &c) {
286 Base::insert(std::make_pair(v, c));
289 Value &constComp() { return const_comp; }
291 const Value &constComp() const { return const_comp; }
293 ///Removes the components with zero coefficient.
295 for (Base::iterator i=Base::begin(); i!=Base::end();) {
298 if ((*i).second==0) Base::erase(i);
303 ///Removes the coefficients closer to zero than \c tolerance.
304 void simplify(double &tolerance) {
305 for (Base::iterator i=Base::begin(); i!=Base::end();) {
308 if (std::fabs((*i).second)<tolerance) Base::erase(i);
313 ///Sets all coefficients and the constant component to 0.
320 Expr &operator+=(const Expr &e) {
321 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
322 (*this)[j->first]+=j->second;
323 const_comp+=e.const_comp;
327 Expr &operator-=(const Expr &e) {
328 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
329 (*this)[j->first]-=j->second;
330 const_comp-=e.const_comp;
334 Expr &operator*=(const Value &c) {
335 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
341 Expr &operator/=(const Value &c) {
342 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
351 ///This data stucture represents a linear constraint in the LP.
352 ///Basically it is a linear expression with a lower or an upper bound
353 ///(or both). These parts of the constraint can be obtained by the member
354 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
356 ///There are two ways to construct a constraint.
357 ///- You can set the linear expression and the bounds directly
358 /// by the functions above.
359 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
360 /// are defined between expressions, or even between constraints whenever
361 /// it makes sense. Therefore if \c e and \c f are linear expressions and
362 /// \c s and \c t are numbers, then the followings are valid expressions
363 /// and thus they can be used directly e.g. in \ref addRow() whenever
372 ///\warning The validity of a constraint is checked only at run time, so
373 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
374 ///\ref LogicError exception.
378 typedef LpSolverBase::Expr Expr;
379 typedef Expr::Key Key;
380 typedef Expr::Value Value;
382 // static const Value INF;
383 // static const Value NaN;
390 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
392 Constr(Value lb,const Expr &e,Value ub) :
393 _expr(e), _lb(lb), _ub(ub) {}
395 Constr(const Expr &e,Value ub) :
396 _expr(e), _lb(NaN), _ub(ub) {}
398 Constr(Value lb,const Expr &e) :
399 _expr(e), _lb(lb), _ub(NaN) {}
401 Constr(const Expr &e) :
402 _expr(e), _lb(NaN), _ub(NaN) {}
410 ///Reference to the linear expression
411 Expr &expr() { return _expr; }
412 ///Cont reference to the linear expression
413 const Expr &expr() const { return _expr; }
414 ///Reference to the lower bound.
417 ///- \ref INF "INF": the constraint is lower unbounded.
418 ///- \ref NaN "NaN": lower bound has not been set.
419 ///- finite number: the lower bound
420 Value &lowerBound() { return _lb; }
421 ///The const version of \ref lowerBound()
422 const Value &lowerBound() const { return _lb; }
423 ///Reference to the upper bound.
426 ///- \ref INF "INF": the constraint is upper unbounded.
427 ///- \ref NaN "NaN": upper bound has not been set.
428 ///- finite number: the upper bound
429 Value &upperBound() { return _ub; }
430 ///The const version of \ref upperBound()
431 const Value &upperBound() const { return _ub; }
432 ///Is the constraint lower bounded?
433 bool lowerBounded() const {
437 ///Is the constraint upper bounded?
438 bool upperBounded() const {
444 ///Linear expression of rows
446 ///This data structure represents a column of the matrix,
447 ///thas is it strores a linear expression of the dual variables
448 ///(\ref Row "Row"s).
450 ///There are several ways to access and modify the contents of this
452 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
453 ///if \c e is an DualExpr and \c v
454 ///and \c w are of type \ref Row, then you can
455 ///read and modify the coefficients like
462 ///or you can also iterate through its elements.
465 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
468 ///(This code computes the sum of all coefficients).
469 ///- Numbers (<tt>double</tt>'s)
470 ///and variables (\ref Row "Row"s) directly convert to an
471 ///\ref DualExpr and the usual linear operations are defined, so
475 ///v*2.1+(3*v+(v*12+w)*3)/2
477 ///are valid \ref DualExpr "DualExpr"essions.
478 ///The usual assignment operations are also defined.
481 ///e+=2*v-3.12*(v-w/2);
488 class DualExpr : public std::map<Row,Value>
491 typedef LpSolverBase::Row Key;
492 typedef LpSolverBase::Value Value;
495 typedef std::map<Row,Value> Base;
498 typedef True IsLinExpression;
500 DualExpr() : Base() { }
502 DualExpr(const Key &v) {
503 Base::insert(std::make_pair(v, 1));
506 void set(const Key &v,const Value &c) {
507 Base::insert(std::make_pair(v, c));
510 ///Removes the components with zero coefficient.
512 for (Base::iterator i=Base::begin(); i!=Base::end();) {
515 if ((*i).second==0) Base::erase(i);
520 ///Removes the coefficients closer to zero than \c tolerance.
521 void simplify(double &tolerance) {
522 for (Base::iterator i=Base::begin(); i!=Base::end();) {
525 if (std::fabs((*i).second)<tolerance) Base::erase(i);
531 ///Sets all coefficients to 0.
537 DualExpr &operator+=(const DualExpr &e) {
538 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
539 (*this)[j->first]+=j->second;
543 DualExpr &operator-=(const DualExpr &e) {
544 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
545 (*this)[j->first]-=j->second;
549 DualExpr &operator*=(const Value &c) {
550 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
555 DualExpr &operator/=(const Value &c) {
556 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
567 //Abstract virtual functions
568 virtual LpSolverBase &_newLp() = 0;
569 virtual LpSolverBase &_copyLp(){
570 ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden.
573 LpSolverBase & newlp(_newLp());
575 //return *(LpSolverBase*)0;
578 virtual int _addCol() = 0;
579 virtual int _addRow() = 0;
580 virtual void _eraseCol(int col) = 0;
581 virtual void _eraseRow(int row) = 0;
582 virtual void _getColName(int col, std::string & name) = 0;
583 virtual void _setColName(int col, const std::string & name) = 0;
584 virtual void _setRowCoeffs(int i,
587 Value const * values ) = 0;
588 virtual void _setColCoeffs(int i,
591 Value const * values ) = 0;
592 virtual void _setCoeff(int row, int col, Value value) = 0;
593 virtual void _setColLowerBound(int i, Value value) = 0;
594 virtual void _setColUpperBound(int i, Value value) = 0;
595 // virtual void _setRowLowerBound(int i, Value value) = 0;
596 // virtual void _setRowUpperBound(int i, Value value) = 0;
597 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
598 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
599 virtual void _clearObj()=0;
600 // virtual void _setObj(int length,
601 // int const * indices,
602 // Value const * values ) = 0;
603 virtual SolveExitStatus _solve() = 0;
604 virtual Value _getPrimal(int i) = 0;
605 virtual Value _getDual(int i) = 0;
606 virtual Value _getPrimalValue() = 0;
607 virtual bool _isBasicCol(int i) = 0;
608 virtual SolutionStatus _getPrimalStatus() = 0;
609 virtual SolutionStatus _getDualStatus() = 0;
610 ///\todo This could be implemented here, too, using _getPrimalStatus() and
612 virtual ProblemTypes _getProblemType() = 0;
614 virtual void _setMax() = 0;
615 virtual void _setMin() = 0;
617 //Own protected stuff
619 //Constant component of the objective function
620 Value obj_const_comp;
628 LpSolverBase() : obj_const_comp(0) {}
631 virtual ~LpSolverBase() {}
633 ///Creates a new LP problem
634 LpSolverBase &newLp() {return _newLp();}
635 ///Makes a copy of the LP problem
636 LpSolverBase ©Lp() {return _copyLp();}
638 ///\name Build up and modify the LP
642 ///Add a new empty column (i.e a new variable) to the LP
643 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
645 ///\brief Adds several new columns
646 ///(i.e a variables) at once
648 ///This magic function takes a container as its argument
649 ///and fills its elements
650 ///with new columns (i.e. variables)
652 ///- a standard STL compatible iterable container with
653 ///\ref Col as its \c values_type
656 ///std::vector<LpSolverBase::Col>
657 ///std::list<LpSolverBase::Col>
659 ///- a standard STL compatible iterable container with
660 ///\ref Col as its \c mapped_type
663 ///std::map<AnyType,LpSolverBase::Col>
665 ///- an iterable lemon \ref concept::WriteMap "write map" like
667 ///ListGraph::NodeMap<LpSolverBase::Col>
668 ///ListGraph::EdgeMap<LpSolverBase::Col>
670 ///\return The number of the created column.
673 int addColSet(T &t) { return 0;}
676 typename enable_if<typename T::value_type::LpSolverCol,int>::type
677 addColSet(T &t,dummy<0> = 0) {
679 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
683 typename enable_if<typename T::value_type::second_type::LpSolverCol,
685 addColSet(T &t,dummy<1> = 1) {
687 for(typename T::iterator i=t.begin();i!=t.end();++i) {
694 typename enable_if<typename T::MapIt::Value::LpSolverCol,
696 addColSet(T &t,dummy<2> = 2) {
698 for(typename T::MapIt i(t); i!=INVALID; ++i)
707 ///Set a column (i.e a dual constraint) of the LP
709 ///\param c is the column to be modified
710 ///\param e is a dual linear expression (see \ref DualExpr)
712 void col(Col c,const DualExpr &e) {
713 std::vector<int> indices;
714 std::vector<Value> values;
715 indices.push_back(0);
717 for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
719 indices.push_back(rows.floatingId((*i).first.id));
720 values.push_back((*i).second);
722 _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
723 &indices[0],&values[0]);
726 ///Add a new column to the LP
728 ///\param e is a dual linear expression (see \ref DualExpr)
729 ///\param obj is the corresponding component of the objective
730 ///function. It is 0 by default.
731 ///\return The created column.
732 Col addCol(const DualExpr &e, Value obj=0) {
739 ///Add a new empty row (i.e a new constraint) to the LP
741 ///This function adds a new empty row (i.e a new constraint) to the LP.
742 ///\return The created row
743 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
745 ///\brief Add several new rows
746 ///(i.e a constraints) at once
748 ///This magic function takes a container as its argument
749 ///and fills its elements
750 ///with new row (i.e. variables)
752 ///- a standard STL compatible iterable container with
753 ///\ref Row as its \c values_type
756 ///std::vector<LpSolverBase::Row>
757 ///std::list<LpSolverBase::Row>
759 ///- a standard STL compatible iterable container with
760 ///\ref Row as its \c mapped_type
763 ///std::map<AnyType,LpSolverBase::Row>
765 ///- an iterable lemon \ref concept::WriteMap "write map" like
767 ///ListGraph::NodeMap<LpSolverBase::Row>
768 ///ListGraph::EdgeMap<LpSolverBase::Row>
770 ///\return The number of rows created.
773 int addRowSet(T &t) { return 0;}
776 typename enable_if<typename T::value_type::LpSolverRow,int>::type
777 addRowSet(T &t,dummy<0> = 0) {
779 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
783 typename enable_if<typename T::value_type::second_type::LpSolverRow,
785 addRowSet(T &t,dummy<1> = 1) {
787 for(typename T::iterator i=t.begin();i!=t.end();++i) {
794 typename enable_if<typename T::MapIt::Value::LpSolverRow,
796 addRowSet(T &t,dummy<2> = 2) {
798 for(typename T::MapIt i(t); i!=INVALID; ++i)
807 ///Set a row (i.e a constraint) of the LP
809 ///\param r is the row to be modified
810 ///\param l is lower bound (-\ref INF means no bound)
811 ///\param e is a linear expression (see \ref Expr)
812 ///\param u is the upper bound (\ref INF means no bound)
813 ///\bug This is a temportary function. The interface will change to
815 ///\todo Option to control whether a constraint with a single variable is
817 void row(Row r, Value l,const Expr &e, Value u) {
818 std::vector<int> indices;
819 std::vector<Value> values;
820 indices.push_back(0);
822 for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
823 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
824 indices.push_back(cols.floatingId((*i).first.id));
825 values.push_back((*i).second);
827 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
828 &indices[0],&values[0]);
829 // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
830 // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
831 _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
834 ///Set a row (i.e a constraint) of the LP
836 ///\param r is the row to be modified
837 ///\param c is a linear expression (see \ref Constr)
838 void row(Row r, const Constr &c) {
840 c.lowerBounded()?c.lowerBound():-INF,
842 c.upperBounded()?c.upperBound():INF);
845 ///Add a new row (i.e a new constraint) to the LP
847 ///\param l is the lower bound (-\ref INF means no bound)
848 ///\param e is a linear expression (see \ref Expr)
849 ///\param u is the upper bound (\ref INF means no bound)
850 ///\return The created row.
851 ///\bug This is a temportary function. The interface will change to
853 Row addRow(Value l,const Expr &e, Value u) {
859 ///Add a new row (i.e a new constraint) to the LP
861 ///\param c is a linear expression (see \ref Constr)
862 ///\return The created row.
863 Row addRow(const Constr &c) {
868 ///Erase a coloumn (i.e a variable) from the LP
870 ///\param c is the coloumn to be deleted
871 ///\todo Please check this
872 void eraseCol(Col c) {
873 _eraseCol(cols.floatingId(c.id));
876 ///Erase a row (i.e a constraint) from the LP
878 ///\param r is the row to be deleted
879 ///\todo Please check this
880 void eraseRow(Row r) {
881 _eraseRow(rows.floatingId(r.id));
885 /// Get the name of a column
887 ///\param c is the coresponding coloumn
888 ///\return The name of the colunm
889 std::string ColName(Col c){
891 _getColName(cols.floatingId(c.id), name);
895 /// Set the name of a column
897 ///\param c is the coresponding coloumn
898 ///\param name The name to be given
899 void ColName(Col c, const std::string & name){
900 _setColName(cols.floatingId(c.id), name);
903 /// Set an element of the coefficient matrix of the LP
905 ///\param r is the row of the element to be modified
906 ///\param c is the coloumn of the element to be modified
907 ///\param val is the new value of the coefficient
909 void Coeff(Row r, Col c, Value val){
910 _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
913 /// Set the lower bound of a column (i.e a variable)
915 /// The lower bound of a variable (column) has to be given by an
916 /// extended number of type Value, i.e. a finite number of type
917 /// Value or -\ref INF.
918 void colLowerBound(Col c, Value value) {
919 _setColLowerBound(cols.floatingId(c.id),value);
922 ///\brief Set the lower bound of several columns
923 ///(i.e a variables) at once
925 ///This magic function takes a container as its argument
926 ///and applies the function on all of its elements.
927 /// The lower bound of a variable (column) has to be given by an
928 /// extended number of type Value, i.e. a finite number of type
929 /// Value or -\ref INF.
932 void colLowerBound(T &t, Value value) { return 0;}
935 typename enable_if<typename T::value_type::LpSolverCol,void>::type
936 colLowerBound(T &t, Value value,dummy<0> = 0) {
937 for(typename T::iterator i=t.begin();i!=t.end();++i) {
938 colLowerBound(*i, value);
942 typename enable_if<typename T::value_type::second_type::LpSolverCol,
944 colLowerBound(T &t, Value value,dummy<1> = 1) {
945 for(typename T::iterator i=t.begin();i!=t.end();++i) {
946 colLowerBound(i->second, value);
950 typename enable_if<typename T::MapIt::Value::LpSolverCol,
952 colLowerBound(T &t, Value value,dummy<2> = 2) {
953 for(typename T::MapIt i(t); i!=INVALID; ++i){
954 colLowerBound(*i, value);
959 /// Set the upper bound of a column (i.e a variable)
961 /// The upper bound of a variable (column) has to be given by an
962 /// extended number of type Value, i.e. a finite number of type
963 /// Value or \ref INF.
964 void colUpperBound(Col c, Value value) {
965 _setColUpperBound(cols.floatingId(c.id),value);
968 ///\brief Set the lower bound of several columns
969 ///(i.e a variables) at once
971 ///This magic function takes a container as its argument
972 ///and applies the function on all of its elements.
973 /// The upper bound of a variable (column) has to be given by an
974 /// extended number of type Value, i.e. a finite number of type
975 /// Value or \ref INF.
978 void colUpperBound(T &t, Value value) { return 0;}
981 typename enable_if<typename T::value_type::LpSolverCol,void>::type
982 colUpperBound(T &t, Value value,dummy<0> = 0) {
983 for(typename T::iterator i=t.begin();i!=t.end();++i) {
984 colUpperBound(*i, value);
988 typename enable_if<typename T::value_type::second_type::LpSolverCol,
990 colUpperBound(T &t, Value value,dummy<1> = 1) {
991 for(typename T::iterator i=t.begin();i!=t.end();++i) {
992 colUpperBound(i->second, value);
996 typename enable_if<typename T::MapIt::Value::LpSolverCol,
998 colUpperBound(T &t, Value value,dummy<2> = 2) {
999 for(typename T::MapIt i(t); i!=INVALID; ++i){
1000 colUpperBound(*i, value);
1005 /// Set the lower and the upper bounds of a column (i.e a variable)
1007 /// The lower and the upper bounds of
1008 /// a variable (column) have to be given by an
1009 /// extended number of type Value, i.e. a finite number of type
1010 /// Value, -\ref INF or \ref INF.
1011 void colBounds(Col c, Value lower, Value upper) {
1012 _setColLowerBound(cols.floatingId(c.id),lower);
1013 _setColUpperBound(cols.floatingId(c.id),upper);
1016 ///\brief Set the lower and the upper bound of several columns
1017 ///(i.e a variables) at once
1019 ///This magic function takes a container as its argument
1020 ///and applies the function on all of its elements.
1021 /// The lower and the upper bounds of
1022 /// a variable (column) have to be given by an
1023 /// extended number of type Value, i.e. a finite number of type
1024 /// Value, -\ref INF or \ref INF.
1027 void colBounds(T &t, Value lower, Value upper) { return 0;}
1030 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1031 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1032 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1033 colBounds(*i, lower, upper);
1037 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1039 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1040 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1041 colBounds(i->second, lower, upper);
1045 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1047 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1048 for(typename T::MapIt i(t); i!=INVALID; ++i){
1049 colBounds(*i, lower, upper);
1054 // /// Set the lower bound of a row (i.e a constraint)
1056 // /// The lower bound of a linear expression (row) has to be given by an
1057 // /// extended number of type Value, i.e. a finite number of type
1058 // /// Value or -\ref INF.
1059 // void rowLowerBound(Row r, Value value) {
1060 // _setRowLowerBound(rows.floatingId(r.id),value);
1062 // /// Set the upper bound of a row (i.e a constraint)
1064 // /// The upper bound of a linear expression (row) has to be given by an
1065 // /// extended number of type Value, i.e. a finite number of type
1066 // /// Value or \ref INF.
1067 // void rowUpperBound(Row r, Value value) {
1068 // _setRowUpperBound(rows.floatingId(r.id),value);
1071 /// Set the lower and the upper bounds of a row (i.e a constraint)
1073 /// The lower and the upper bounds of
1074 /// a constraint (row) have to be given by an
1075 /// extended number of type Value, i.e. a finite number of type
1076 /// Value, -\ref INF or \ref INF.
1077 void rowBounds(Row c, Value lower, Value upper) {
1078 _setRowBounds(rows.floatingId(c.id),lower, upper);
1079 // _setRowUpperBound(rows.floatingId(c.id),upper);
1082 ///Set an element of the objective function
1083 void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
1084 ///Set the objective function
1086 ///\param e is a linear expression of type \ref Expr.
1087 ///\bug Is should be called obj()
1088 void setObj(Expr e) {
1090 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1091 objCoeff((*i).first,(*i).second);
1092 obj_const_comp=e.constComp();
1096 void max() { _setMax(); }
1098 void min() { _setMin(); }
1104 ///\name Solve the LP
1108 ///\e Solve the LP problem at hand
1110 ///\return The result of the optimization procedure. Possible
1111 ///values and their meanings can be found in the documentation of
1112 ///\ref SolveExitStatus.
1114 ///\todo Which method is used to solve the problem
1115 SolveExitStatus solve() { return _solve(); }
1119 ///\name Obtain the solution
1123 /// The status of the primal problem (the original LP problem)
1124 SolutionStatus primalStatus() {
1125 return _getPrimalStatus();
1128 /// The status of the dual (of the original LP) problem
1129 SolutionStatus dualStatus() {
1130 return _getDualStatus();
1133 ///The type of the original LP problem
1134 ProblemTypes problemType() {
1135 return _getProblemType();
1139 Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
1142 Value dual(Row r) { return _getDual(rows.floatingId(r.id)); }
1145 bool isBasicCol(Col c) { return _isBasicCol(cols.floatingId(c.id)); }
1150 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1151 /// of the primal problem, depending on whether we minimize or maximize.
1152 ///- \ref NaN if no primal solution is found.
1153 ///- The (finite) objective value if an optimal solution is found.
1154 Value primalValue() { return _getPrimalValue()+obj_const_comp;}
1161 ///\relates LpSolverBase::Expr
1163 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1164 const LpSolverBase::Expr &b)
1166 LpSolverBase::Expr tmp(a);
1172 ///\relates LpSolverBase::Expr
1174 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1175 const LpSolverBase::Expr &b)
1177 LpSolverBase::Expr tmp(a);
1183 ///\relates LpSolverBase::Expr
1185 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1186 const LpSolverBase::Value &b)
1188 LpSolverBase::Expr tmp(a);
1195 ///\relates LpSolverBase::Expr
1197 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1198 const LpSolverBase::Expr &b)
1200 LpSolverBase::Expr tmp(b);
1206 ///\relates LpSolverBase::Expr
1208 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1209 const LpSolverBase::Value &b)
1211 LpSolverBase::Expr tmp(a);
1218 ///\relates LpSolverBase::Constr
1220 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1221 const LpSolverBase::Expr &f)
1223 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1228 ///\relates LpSolverBase::Constr
1230 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1231 const LpSolverBase::Expr &f)
1233 return LpSolverBase::Constr(e,f);
1238 ///\relates LpSolverBase::Constr
1240 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1241 const LpSolverBase::Value &f)
1243 return LpSolverBase::Constr(e,f);
1248 ///\relates LpSolverBase::Constr
1250 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1251 const LpSolverBase::Expr &f)
1253 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1259 ///\relates LpSolverBase::Constr
1261 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1262 const LpSolverBase::Expr &f)
1264 return LpSolverBase::Constr(f,e);
1270 ///\relates LpSolverBase::Constr
1272 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1273 const LpSolverBase::Value &f)
1275 return LpSolverBase::Constr(f,e);
1280 ///\relates LpSolverBase::Constr
1282 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1283 const LpSolverBase::Expr &f)
1285 return LpSolverBase::Constr(0,e-f,0);
1290 ///\relates LpSolverBase::Constr
1292 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1293 const LpSolverBase::Constr&c)
1295 LpSolverBase::Constr tmp(c);
1296 ///\todo Create an own exception type.
1297 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1298 else tmp.lowerBound()=n;
1303 ///\relates LpSolverBase::Constr
1305 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1306 const LpSolverBase::Value &n)
1308 LpSolverBase::Constr tmp(c);
1309 ///\todo Create an own exception type.
1310 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1311 else tmp.upperBound()=n;
1317 ///\relates LpSolverBase::Constr
1319 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1320 const LpSolverBase::Constr&c)
1322 LpSolverBase::Constr tmp(c);
1323 ///\todo Create an own exception type.
1324 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1325 else tmp.upperBound()=n;
1330 ///\relates LpSolverBase::Constr
1332 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1333 const LpSolverBase::Value &n)
1335 LpSolverBase::Constr tmp(c);
1336 ///\todo Create an own exception type.
1337 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1338 else tmp.lowerBound()=n;
1344 ///\relates LpSolverBase::DualExpr
1346 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1347 const LpSolverBase::DualExpr &b)
1349 LpSolverBase::DualExpr tmp(a);
1355 ///\relates LpSolverBase::DualExpr
1357 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1358 const LpSolverBase::DualExpr &b)
1360 LpSolverBase::DualExpr tmp(a);
1366 ///\relates LpSolverBase::DualExpr
1368 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1369 const LpSolverBase::Value &b)
1371 LpSolverBase::DualExpr tmp(a);
1378 ///\relates LpSolverBase::DualExpr
1380 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1381 const LpSolverBase::DualExpr &b)
1383 LpSolverBase::DualExpr tmp(b);
1389 ///\relates LpSolverBase::DualExpr
1391 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1392 const LpSolverBase::Value &b)
1394 LpSolverBase::DualExpr tmp(a);
1402 #endif //LEMON_LP_BASE_H