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.
45 std::vector<int> index;
46 std::vector<int> cross;
47 _FixId() : _first_index(-1), first_free(-1) {};
48 ///Convert a floating id to a fix one
50 ///\param n is a floating id
51 ///\return the corresponding fix id
52 int fixId(int n) const {return cross[n];}
53 ///Convert a fix id to a floating one
55 ///\param n is a fix id
56 ///\return the corresponding floating id
57 int floatingId(int n) const { return index[n];}
58 ///Add a new floating id.
60 ///\param n is a floating id
61 ///\return the fix id of the new value
62 ///\todo Multiple additions should also be handled.
65 if(cross.empty()) _first_index=n;
66 if(n>=int(cross.size())) {
69 cross[n]=index.size();
74 int next=index[first_free];
81 ///\todo Create an own exception type.
82 throw LogicError(); //floatingId-s must form a continuous range;
87 ///\param n is a fix id
94 for(int i=fl+1;i<int(cross.size());++i) {
100 ///An upper bound on the largest fix id.
102 ///\todo Do we need this?
104 std::size_t maxFixId() { return cross.size()-1; }
106 ///Returns the first (smallest) inserted index
108 ///Returns the first (smallest) inserted index
109 ///or -1 if no index has been inserted before.
110 int firstIndex() {return _first_index;}
113 ///Common base class for LP solvers
115 ///\todo Much more docs
116 ///\ingroup gen_opt_group
125 ///Possible outcomes of an LP solving procedure
126 enum SolveExitStatus {
127 ///This means that the problem has been successfully solved: either
128 ///an optimal solution has been found or infeasibility/unboundedness
131 ///Any other case (including the case when some user specified limit has been exceeded)
136 enum SolutionStatus {
137 ///Feasible solution hasn't been found (but may exist).
139 ///\todo NOTFOUND might be a better name.
142 ///The problem has no feasible solution
144 ///Feasible solution found
146 ///Optimal solution exists and found
148 ///The cost function is unbounded
150 ///\todo Give a feasible solution and an infinite ray (and the
151 ///corresponding bases)
155 ///\e The type of the investigated LP problem
157 ///Primal-dual feasible
158 PRIMAL_DUAL_FEASIBLE = 0,
159 ///Primal feasible dual infeasible
160 PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1,
161 ///Primal infeasible dual feasible
162 PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2,
163 ///Primal-dual infeasible
164 PRIMAL_DUAL_INFEASIBLE = 3,
165 ///Could not determine so far
169 ///The floating point type used by the solver
170 typedef double Value;
171 ///The infinity constant
172 static const Value INF;
173 ///The not a number constant
174 static const Value NaN;
176 static inline bool isNaN(const Value& v) { return v!=v; }
182 ///Refer to a column of the LP.
184 ///This type is used to refer to a column of the LP.
186 ///Its value remains valid and correct even after the addition or erase of
189 ///\todo Document what can one do with a Col (INVALID, comparing,
190 ///it is similar to Node/Edge)
194 friend class LpSolverBase;
195 friend class MipSolverBase;
197 typedef Value ExprValue;
198 typedef True LpSolverCol;
200 Col(const Invalid&) : id(-1) {}
201 bool operator< (Col c) const {return id< c.id;}
202 bool operator> (Col c) const {return id> c.id;}
203 bool operator==(Col c) const {return id==c.id;}
204 bool operator!=(Col c) const {return id!=c.id;}
207 class ColIt : public Col {
210 ColIt(LpSolverBase &lp) : _lp(&lp)
212 id = _lp->cols.cross.empty()?-1:
213 _lp->cols.fixId(_lp->cols.firstIndex());
215 ColIt(const Invalid&) : Col(INVALID) {}
218 int fid = _lp->cols.floatingId(id)+1;
219 id = unsigned(fid)<_lp->cols.cross.size() ? _lp->cols.fixId(fid) : -1;
224 ///Refer to a row of the LP.
226 ///This type is used to refer to a row of the LP.
228 ///Its value remains valid and correct even after the addition or erase of
231 ///\todo Document what can one do with a Row (INVALID, comparing,
232 ///it is similar to Node/Edge)
236 friend class LpSolverBase;
238 typedef Value ExprValue;
239 typedef True LpSolverRow;
241 Row(const Invalid&) : id(-1) {}
243 bool operator< (Row c) const {return id< c.id;}
244 bool operator> (Row c) const {return id> c.id;}
245 bool operator==(Row c) const {return id==c.id;}
246 bool operator!=(Row c) const {return id!=c.id;}
249 ///Linear expression of variables and a constant component
251 ///This data structure strores a linear expression of the variables
252 ///(\ref Col "Col"s) and also has a constant component.
254 ///There are several ways to access and modify the contents of this
256 ///- Its it fully compatible with \c std::map<Col,double>, so for expamle
257 ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can
258 ///read and modify the coefficients like
265 ///or you can also iterate through its elements.
268 ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i)
271 ///(This code computes the sum of all coefficients).
272 ///- Numbers (<tt>double</tt>'s)
273 ///and variables (\ref Col "Col"s) directly convert to an
274 ///\ref Expr and the usual linear operations are defined, so
277 ///2*v-3.12*(v-w/2)+2
278 ///v*2.1+(3*v+(v*12+w+6)*3)/2
280 ///are valid \ref Expr "Expr"essions.
281 ///The usual assignment operations are also defined.
284 ///e+=2*v-3.12*(v-w/2)+2;
288 ///- The constant member can be set and read by \ref constComp()
291 ///double c=e.constComp();
294 ///\note \ref clear() not only sets all coefficients to 0 but also
295 ///clears the constant components.
299 class Expr : public std::map<Col,Value>
302 typedef LpSolverBase::Col Key;
303 typedef LpSolverBase::Value Value;
306 typedef std::map<Col,Value> Base;
310 typedef True IsLinExpression;
312 Expr() : Base(), const_comp(0) { }
314 Expr(const Key &v) : const_comp(0) {
315 Base::insert(std::make_pair(v, 1));
318 Expr(const Value &v) : const_comp(v) {}
320 void set(const Key &v,const Value &c) {
321 Base::insert(std::make_pair(v, c));
324 Value &constComp() { return const_comp; }
326 const Value &constComp() const { return const_comp; }
328 ///Removes the components with zero coefficient.
330 for (Base::iterator i=Base::begin(); i!=Base::end();) {
333 if ((*i).second==0) Base::erase(i);
338 ///Removes the coefficients closer to zero than \c tolerance.
339 void simplify(double &tolerance) {
340 for (Base::iterator i=Base::begin(); i!=Base::end();) {
343 if (std::fabs((*i).second)<tolerance) Base::erase(i);
348 ///Sets all coefficients and the constant component to 0.
355 Expr &operator+=(const Expr &e) {
356 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
357 (*this)[j->first]+=j->second;
358 const_comp+=e.const_comp;
362 Expr &operator-=(const Expr &e) {
363 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
364 (*this)[j->first]-=j->second;
365 const_comp-=e.const_comp;
369 Expr &operator*=(const Value &c) {
370 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
376 Expr &operator/=(const Value &c) {
377 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
386 ///This data stucture represents a linear constraint in the LP.
387 ///Basically it is a linear expression with a lower or an upper bound
388 ///(or both). These parts of the constraint can be obtained by the member
389 ///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
391 ///There are two ways to construct a constraint.
392 ///- You can set the linear expression and the bounds directly
393 /// by the functions above.
394 ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
395 /// are defined between expressions, or even between constraints whenever
396 /// it makes sense. Therefore if \c e and \c f are linear expressions and
397 /// \c s and \c t are numbers, then the followings are valid expressions
398 /// and thus they can be used directly e.g. in \ref addRow() whenever
407 ///\warning The validity of a constraint is checked only at run time, so
408 ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a
409 ///\ref LogicError exception.
413 typedef LpSolverBase::Expr Expr;
414 typedef Expr::Key Key;
415 typedef Expr::Value Value;
417 // static const Value INF;
418 // static const Value NaN;
425 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
427 Constr(Value lb,const Expr &e,Value ub) :
428 _expr(e), _lb(lb), _ub(ub) {}
430 Constr(const Expr &e,Value ub) :
431 _expr(e), _lb(NaN), _ub(ub) {}
433 Constr(Value lb,const Expr &e) :
434 _expr(e), _lb(lb), _ub(NaN) {}
436 Constr(const Expr &e) :
437 _expr(e), _lb(NaN), _ub(NaN) {}
445 ///Reference to the linear expression
446 Expr &expr() { return _expr; }
447 ///Cont reference to the linear expression
448 const Expr &expr() const { return _expr; }
449 ///Reference to the lower bound.
452 ///- \ref INF "INF": the constraint is lower unbounded.
453 ///- \ref NaN "NaN": lower bound has not been set.
454 ///- finite number: the lower bound
455 Value &lowerBound() { return _lb; }
456 ///The const version of \ref lowerBound()
457 const Value &lowerBound() const { return _lb; }
458 ///Reference to the upper bound.
461 ///- \ref INF "INF": the constraint is upper unbounded.
462 ///- \ref NaN "NaN": upper bound has not been set.
463 ///- finite number: the upper bound
464 Value &upperBound() { return _ub; }
465 ///The const version of \ref upperBound()
466 const Value &upperBound() const { return _ub; }
467 ///Is the constraint lower bounded?
468 bool lowerBounded() const {
472 ///Is the constraint upper bounded?
473 bool upperBounded() const {
479 ///Linear expression of rows
481 ///This data structure represents a column of the matrix,
482 ///thas is it strores a linear expression of the dual variables
483 ///(\ref Row "Row"s).
485 ///There are several ways to access and modify the contents of this
487 ///- Its it fully compatible with \c std::map<Row,double>, so for expamle
488 ///if \c e is an DualExpr and \c v
489 ///and \c w are of type \ref Row, then you can
490 ///read and modify the coefficients like
497 ///or you can also iterate through its elements.
500 ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i)
503 ///(This code computes the sum of all coefficients).
504 ///- Numbers (<tt>double</tt>'s)
505 ///and variables (\ref Row "Row"s) directly convert to an
506 ///\ref DualExpr and the usual linear operations are defined, so
510 ///v*2.1+(3*v+(v*12+w)*3)/2
512 ///are valid \ref DualExpr "DualExpr"essions.
513 ///The usual assignment operations are also defined.
516 ///e+=2*v-3.12*(v-w/2);
523 class DualExpr : public std::map<Row,Value>
526 typedef LpSolverBase::Row Key;
527 typedef LpSolverBase::Value Value;
530 typedef std::map<Row,Value> Base;
533 typedef True IsLinExpression;
535 DualExpr() : Base() { }
537 DualExpr(const Key &v) {
538 Base::insert(std::make_pair(v, 1));
541 void set(const Key &v,const Value &c) {
542 Base::insert(std::make_pair(v, c));
545 ///Removes the components with zero coefficient.
547 for (Base::iterator i=Base::begin(); i!=Base::end();) {
550 if ((*i).second==0) Base::erase(i);
555 ///Removes the coefficients closer to zero than \c tolerance.
556 void simplify(double &tolerance) {
557 for (Base::iterator i=Base::begin(); i!=Base::end();) {
560 if (std::fabs((*i).second)<tolerance) Base::erase(i);
566 ///Sets all coefficients to 0.
572 DualExpr &operator+=(const DualExpr &e) {
573 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
574 (*this)[j->first]+=j->second;
578 DualExpr &operator-=(const DualExpr &e) {
579 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
580 (*this)[j->first]-=j->second;
584 DualExpr &operator*=(const Value &c) {
585 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
590 DualExpr &operator/=(const Value &c) {
591 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
600 //Abstract virtual functions
601 virtual LpSolverBase &_newLp() = 0;
602 virtual LpSolverBase &_copyLp(){
603 ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden.
606 LpSolverBase & newlp(_newLp());
608 //return *(LpSolverBase*)0;
611 virtual int _addCol() = 0;
612 virtual int _addRow() = 0;
613 virtual void _eraseCol(int col) = 0;
614 virtual void _eraseRow(int row) = 0;
615 virtual void _getColName(int col, std::string & name) = 0;
616 virtual void _setColName(int col, const std::string & name) = 0;
617 virtual void _setRowCoeffs(int i,
620 Value const * values ) = 0;
621 virtual void _setColCoeffs(int i,
624 Value const * values ) = 0;
625 virtual void _setCoeff(int row, int col, Value value) = 0;
626 virtual void _setColLowerBound(int i, Value value) = 0;
627 virtual void _setColUpperBound(int i, Value value) = 0;
628 // virtual void _setRowLowerBound(int i, Value value) = 0;
629 // virtual void _setRowUpperBound(int i, Value value) = 0;
630 virtual void _setRowBounds(int i, Value lower, Value upper) = 0;
631 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
632 virtual void _clearObj()=0;
633 // virtual void _setObj(int length,
634 // int const * indices,
635 // Value const * values ) = 0;
636 virtual SolveExitStatus _solve() = 0;
637 virtual Value _getPrimal(int i) = 0;
638 virtual Value _getDual(int i) = 0;
639 virtual Value _getPrimalValue() = 0;
640 virtual bool _isBasicCol(int i) = 0;
641 virtual SolutionStatus _getPrimalStatus() = 0;
642 virtual SolutionStatus _getDualStatus() = 0;
643 ///\todo This could be implemented here, too, using _getPrimalStatus() and
645 virtual ProblemTypes _getProblemType() = 0;
647 virtual void _setMax() = 0;
648 virtual void _setMin() = 0;
650 //Own protected stuff
652 //Constant component of the objective function
653 Value obj_const_comp;
661 LpSolverBase() : obj_const_comp(0) {}
664 virtual ~LpSolverBase() {}
666 ///Creates a new LP problem
667 LpSolverBase &newLp() {return _newLp();}
668 ///Makes a copy of the LP problem
669 LpSolverBase ©Lp() {return _copyLp();}
671 ///\name Build up and modify the LP
675 ///Add a new empty column (i.e a new variable) to the LP
676 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
678 ///\brief Adds several new columns
679 ///(i.e a variables) at once
681 ///This magic function takes a container as its argument
682 ///and fills its elements
683 ///with new columns (i.e. variables)
685 ///- a standard STL compatible iterable container with
686 ///\ref Col as its \c values_type
689 ///std::vector<LpSolverBase::Col>
690 ///std::list<LpSolverBase::Col>
692 ///- a standard STL compatible iterable container with
693 ///\ref Col as its \c mapped_type
696 ///std::map<AnyType,LpSolverBase::Col>
698 ///- an iterable lemon \ref concepts::WriteMap "write map" like
700 ///ListGraph::NodeMap<LpSolverBase::Col>
701 ///ListGraph::EdgeMap<LpSolverBase::Col>
703 ///\return The number of the created column.
706 int addColSet(T &t) { return 0;}
709 typename enable_if<typename T::value_type::LpSolverCol,int>::type
710 addColSet(T &t,dummy<0> = 0) {
712 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
716 typename enable_if<typename T::value_type::second_type::LpSolverCol,
718 addColSet(T &t,dummy<1> = 1) {
720 for(typename T::iterator i=t.begin();i!=t.end();++i) {
727 typename enable_if<typename T::MapIt::Value::LpSolverCol,
729 addColSet(T &t,dummy<2> = 2) {
731 for(typename T::MapIt i(t); i!=INVALID; ++i)
740 ///Set a column (i.e a dual constraint) of the LP
742 ///\param c is the column to be modified
743 ///\param e is a dual linear expression (see \ref DualExpr)
745 void col(Col c,const DualExpr &e) {
746 std::vector<int> indices;
747 std::vector<Value> values;
748 indices.push_back(0);
750 for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i)
752 indices.push_back(rows.floatingId((*i).first.id));
753 values.push_back((*i).second);
755 _setColCoeffs(cols.floatingId(c.id),indices.size()-1,
756 &indices[0],&values[0]);
759 ///Add a new column to the LP
761 ///\param e is a dual linear expression (see \ref DualExpr)
762 ///\param obj is the corresponding component of the objective
763 ///function. It is 0 by default.
764 ///\return The created column.
765 Col addCol(const DualExpr &e, Value obj=0) {
772 ///Add a new empty row (i.e a new constraint) to the LP
774 ///This function adds a new empty row (i.e a new constraint) to the LP.
775 ///\return The created row
776 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
778 ///\brief Add several new rows
779 ///(i.e a constraints) at once
781 ///This magic function takes a container as its argument
782 ///and fills its elements
783 ///with new row (i.e. variables)
785 ///- a standard STL compatible iterable container with
786 ///\ref Row as its \c values_type
789 ///std::vector<LpSolverBase::Row>
790 ///std::list<LpSolverBase::Row>
792 ///- a standard STL compatible iterable container with
793 ///\ref Row as its \c mapped_type
796 ///std::map<AnyType,LpSolverBase::Row>
798 ///- an iterable lemon \ref concepts::WriteMap "write map" like
800 ///ListGraph::NodeMap<LpSolverBase::Row>
801 ///ListGraph::EdgeMap<LpSolverBase::Row>
803 ///\return The number of rows created.
806 int addRowSet(T &t) { return 0;}
809 typename enable_if<typename T::value_type::LpSolverRow,int>::type
810 addRowSet(T &t,dummy<0> = 0) {
812 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
816 typename enable_if<typename T::value_type::second_type::LpSolverRow,
818 addRowSet(T &t,dummy<1> = 1) {
820 for(typename T::iterator i=t.begin();i!=t.end();++i) {
827 typename enable_if<typename T::MapIt::Value::LpSolverRow,
829 addRowSet(T &t,dummy<2> = 2) {
831 for(typename T::MapIt i(t); i!=INVALID; ++i)
840 ///Set a row (i.e a constraint) of the LP
842 ///\param r is the row to be modified
843 ///\param l is lower bound (-\ref INF means no bound)
844 ///\param e is a linear expression (see \ref Expr)
845 ///\param u is the upper bound (\ref INF means no bound)
846 ///\bug This is a temportary function. The interface will change to
848 ///\todo Option to control whether a constraint with a single variable is
850 void row(Row r, Value l,const Expr &e, Value u) {
851 std::vector<int> indices;
852 std::vector<Value> values;
853 indices.push_back(0);
855 for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
856 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
857 indices.push_back(cols.floatingId((*i).first.id));
858 values.push_back((*i).second);
860 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
861 &indices[0],&values[0]);
862 // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
863 // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
864 _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp());
867 ///Set a row (i.e a constraint) of the LP
869 ///\param r is the row to be modified
870 ///\param c is a linear expression (see \ref Constr)
871 void row(Row r, const Constr &c) {
873 c.lowerBounded()?c.lowerBound():-INF,
875 c.upperBounded()?c.upperBound():INF);
878 ///Add a new row (i.e a new constraint) to the LP
880 ///\param l is the lower bound (-\ref INF means no bound)
881 ///\param e is a linear expression (see \ref Expr)
882 ///\param u is the upper bound (\ref INF means no bound)
883 ///\return The created row.
884 ///\bug This is a temportary function. The interface will change to
886 Row addRow(Value l,const Expr &e, Value u) {
892 ///Add a new row (i.e a new constraint) to the LP
894 ///\param c is a linear expression (see \ref Constr)
895 ///\return The created row.
896 Row addRow(const Constr &c) {
901 ///Erase a coloumn (i.e a variable) from the LP
903 ///\param c is the coloumn to be deleted
904 ///\todo Please check this
905 void eraseCol(Col c) {
906 _eraseCol(cols.floatingId(c.id));
909 ///Erase a row (i.e a constraint) from the LP
911 ///\param r is the row to be deleted
912 ///\todo Please check this
913 void eraseRow(Row r) {
914 _eraseRow(rows.floatingId(r.id));
918 /// Get the name of a column
920 ///\param c is the coresponding coloumn
921 ///\return The name of the colunm
922 std::string colName(Col c){
924 _getColName(cols.floatingId(c.id), name);
928 /// Set the name of a column
930 ///\param c is the coresponding coloumn
931 ///\param name The name to be given
932 void colName(Col c, const std::string & name){
933 _setColName(cols.floatingId(c.id), name);
936 /// Set an element of the coefficient matrix of the LP
938 ///\param r is the row of the element to be modified
939 ///\param c is the coloumn of the element to be modified
940 ///\param val is the new value of the coefficient
942 void coeff(Row r, Col c, Value val){
943 _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val);
946 /// Set the lower bound of a column (i.e a variable)
948 /// The lower bound of a variable (column) has to be given by an
949 /// extended number of type Value, i.e. a finite number of type
950 /// Value or -\ref INF.
951 void colLowerBound(Col c, Value value) {
952 _setColLowerBound(cols.floatingId(c.id),value);
955 ///\brief Set the lower bound of several columns
956 ///(i.e a variables) at once
958 ///This magic function takes a container as its argument
959 ///and applies the function on all of its elements.
960 /// The lower bound of a variable (column) has to be given by an
961 /// extended number of type Value, i.e. a finite number of type
962 /// Value or -\ref INF.
965 void colLowerBound(T &t, Value value) { return 0;}
968 typename enable_if<typename T::value_type::LpSolverCol,void>::type
969 colLowerBound(T &t, Value value,dummy<0> = 0) {
970 for(typename T::iterator i=t.begin();i!=t.end();++i) {
971 colLowerBound(*i, value);
975 typename enable_if<typename T::value_type::second_type::LpSolverCol,
977 colLowerBound(T &t, Value value,dummy<1> = 1) {
978 for(typename T::iterator i=t.begin();i!=t.end();++i) {
979 colLowerBound(i->second, value);
983 typename enable_if<typename T::MapIt::Value::LpSolverCol,
985 colLowerBound(T &t, Value value,dummy<2> = 2) {
986 for(typename T::MapIt i(t); i!=INVALID; ++i){
987 colLowerBound(*i, value);
992 /// Set the upper bound of a column (i.e a variable)
994 /// The upper bound of a variable (column) has to be given by an
995 /// extended number of type Value, i.e. a finite number of type
996 /// Value or \ref INF.
997 void colUpperBound(Col c, Value value) {
998 _setColUpperBound(cols.floatingId(c.id),value);
1001 ///\brief Set the lower bound of several columns
1002 ///(i.e a variables) at once
1004 ///This magic function takes a container as its argument
1005 ///and applies the function on all of its elements.
1006 /// The upper bound of a variable (column) has to be given by an
1007 /// extended number of type Value, i.e. a finite number of type
1008 /// Value or \ref INF.
1011 void colUpperBound(T &t, Value value) { return 0;}
1014 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1015 colUpperBound(T &t, Value value,dummy<0> = 0) {
1016 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1017 colUpperBound(*i, value);
1021 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1023 colUpperBound(T &t, Value value,dummy<1> = 1) {
1024 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1025 colUpperBound(i->second, value);
1029 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1031 colUpperBound(T &t, Value value,dummy<2> = 2) {
1032 for(typename T::MapIt i(t); i!=INVALID; ++i){
1033 colUpperBound(*i, value);
1038 /// Set the lower and the upper bounds of a column (i.e a variable)
1040 /// The lower and the upper bounds of
1041 /// a variable (column) have to be given by an
1042 /// extended number of type Value, i.e. a finite number of type
1043 /// Value, -\ref INF or \ref INF.
1044 void colBounds(Col c, Value lower, Value upper) {
1045 _setColLowerBound(cols.floatingId(c.id),lower);
1046 _setColUpperBound(cols.floatingId(c.id),upper);
1049 ///\brief Set the lower and the upper bound of several columns
1050 ///(i.e a variables) at once
1052 ///This magic function takes a container as its argument
1053 ///and applies the function on all of its elements.
1054 /// The lower and the upper bounds of
1055 /// a variable (column) have to be given by an
1056 /// extended number of type Value, i.e. a finite number of type
1057 /// Value, -\ref INF or \ref INF.
1060 void colBounds(T &t, Value lower, Value upper) { return 0;}
1063 typename enable_if<typename T::value_type::LpSolverCol,void>::type
1064 colBounds(T &t, Value lower, Value upper,dummy<0> = 0) {
1065 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1066 colBounds(*i, lower, upper);
1070 typename enable_if<typename T::value_type::second_type::LpSolverCol,
1072 colBounds(T &t, Value lower, Value upper,dummy<1> = 1) {
1073 for(typename T::iterator i=t.begin();i!=t.end();++i) {
1074 colBounds(i->second, lower, upper);
1078 typename enable_if<typename T::MapIt::Value::LpSolverCol,
1080 colBounds(T &t, Value lower, Value upper,dummy<2> = 2) {
1081 for(typename T::MapIt i(t); i!=INVALID; ++i){
1082 colBounds(*i, lower, upper);
1087 // /// Set the lower bound of a row (i.e a constraint)
1089 // /// The lower bound of a linear expression (row) has to be given by an
1090 // /// extended number of type Value, i.e. a finite number of type
1091 // /// Value or -\ref INF.
1092 // void rowLowerBound(Row r, Value value) {
1093 // _setRowLowerBound(rows.floatingId(r.id),value);
1095 // /// Set the upper bound of a row (i.e a constraint)
1097 // /// The upper bound of a linear expression (row) has to be given by an
1098 // /// extended number of type Value, i.e. a finite number of type
1099 // /// Value or \ref INF.
1100 // void rowUpperBound(Row r, Value value) {
1101 // _setRowUpperBound(rows.floatingId(r.id),value);
1104 /// Set the lower and the upper bounds of a row (i.e a constraint)
1106 /// The lower and the upper bounds of
1107 /// a constraint (row) have to be given by an
1108 /// extended number of type Value, i.e. a finite number of type
1109 /// Value, -\ref INF or \ref INF.
1110 void rowBounds(Row c, Value lower, Value upper) {
1111 _setRowBounds(rows.floatingId(c.id),lower, upper);
1112 // _setRowUpperBound(rows.floatingId(c.id),upper);
1115 ///Set an element of the objective function
1116 void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
1117 ///Set the objective function
1119 ///\param e is a linear expression of type \ref Expr.
1120 ///\bug Is should be called obj()
1121 void setObj(Expr e) {
1123 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
1124 objCoeff((*i).first,(*i).second);
1125 obj_const_comp=e.constComp();
1129 void max() { _setMax(); }
1131 void min() { _setMin(); }
1137 ///\name Solve the LP
1141 ///\e Solve the LP problem at hand
1143 ///\return The result of the optimization procedure. Possible
1144 ///values and their meanings can be found in the documentation of
1145 ///\ref SolveExitStatus.
1147 ///\todo Which method is used to solve the problem
1148 SolveExitStatus solve() { return _solve(); }
1152 ///\name Obtain the solution
1156 /// The status of the primal problem (the original LP problem)
1157 SolutionStatus primalStatus() {
1158 return _getPrimalStatus();
1161 /// The status of the dual (of the original LP) problem
1162 SolutionStatus dualStatus() {
1163 return _getDualStatus();
1166 ///The type of the original LP problem
1167 ProblemTypes problemType() {
1168 return _getProblemType();
1172 Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); }
1175 Value dual(Row r) { return _getDual(rows.floatingId(r.id)); }
1178 bool isBasicCol(Col c) { return _isBasicCol(cols.floatingId(c.id)); }
1183 ///- \ref INF or -\ref INF means either infeasibility or unboundedness
1184 /// of the primal problem, depending on whether we minimize or maximize.
1185 ///- \ref NaN if no primal solution is found.
1186 ///- The (finite) objective value if an optimal solution is found.
1187 Value primalValue() { return _getPrimalValue()+obj_const_comp;}
1193 ///Common base class for MIP solvers
1194 ///\todo Much more docs
1195 ///\ingroup gen_opt_group
1196 class MipSolverBase : virtual public LpSolverBase{
1199 ///Possible variable (coloumn) types (e.g. real, integer, binary etc.)
1201 ///Continuous variable
1205 ///Unfortunately, cplex 7.5 somewhere writes something like
1206 ///#define INTEGER 'I'
1208 ///\todo No support for other types yet.
1211 ///Sets the type of the given coloumn to the given type
1213 ///Sets the type of the given coloumn to the given type.
1214 void colType(Col c, ColTypes col_type) {
1215 _colType(cols.floatingId(c.id),col_type);
1218 ///Gives back the type of the column.
1220 ///Gives back the type of the column.
1221 ColTypes colType(Col c){
1222 return _colType(cols.floatingId(c.id));
1225 ///Sets the type of the given Col to integer or remove that property.
1227 ///Sets the type of the given Col to integer or remove that property.
1228 void integer(Col c, bool enable) {
1235 ///Gives back whether the type of the column is integer or not.
1237 ///Gives back the type of the column.
1238 ///\return true if the column has integer type and false if not.
1239 bool integer(Col c){
1240 return (colType(c)==INT);
1243 /// The status of the MIP problem
1244 SolutionStatus mipStatus() {
1245 return _getMipStatus();
1250 virtual ColTypes _colType(int col) = 0;
1251 virtual void _colType(int col, ColTypes col_type) = 0;
1252 virtual SolutionStatus _getMipStatus()=0;
1256 ///\relates LpSolverBase::Expr
1258 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
1259 const LpSolverBase::Expr &b)
1261 LpSolverBase::Expr tmp(a);
1267 ///\relates LpSolverBase::Expr
1269 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
1270 const LpSolverBase::Expr &b)
1272 LpSolverBase::Expr tmp(a);
1278 ///\relates LpSolverBase::Expr
1280 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
1281 const LpSolverBase::Value &b)
1283 LpSolverBase::Expr tmp(a);
1290 ///\relates LpSolverBase::Expr
1292 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
1293 const LpSolverBase::Expr &b)
1295 LpSolverBase::Expr tmp(b);
1301 ///\relates LpSolverBase::Expr
1303 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
1304 const LpSolverBase::Value &b)
1306 LpSolverBase::Expr tmp(a);
1313 ///\relates LpSolverBase::Constr
1315 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1316 const LpSolverBase::Expr &f)
1318 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
1323 ///\relates LpSolverBase::Constr
1325 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
1326 const LpSolverBase::Expr &f)
1328 return LpSolverBase::Constr(e,f);
1333 ///\relates LpSolverBase::Constr
1335 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
1336 const LpSolverBase::Value &f)
1338 return LpSolverBase::Constr(e,f);
1343 ///\relates LpSolverBase::Constr
1345 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1346 const LpSolverBase::Expr &f)
1348 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
1354 ///\relates LpSolverBase::Constr
1356 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
1357 const LpSolverBase::Expr &f)
1359 return LpSolverBase::Constr(f,e);
1365 ///\relates LpSolverBase::Constr
1367 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
1368 const LpSolverBase::Value &f)
1370 return LpSolverBase::Constr(f,e);
1375 ///\relates LpSolverBase::Constr
1377 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
1378 const LpSolverBase::Expr &f)
1380 return LpSolverBase::Constr(0,e-f,0);
1385 ///\relates LpSolverBase::Constr
1387 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
1388 const LpSolverBase::Constr&c)
1390 LpSolverBase::Constr tmp(c);
1391 ///\todo Create an own exception type.
1392 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1393 else tmp.lowerBound()=n;
1398 ///\relates LpSolverBase::Constr
1400 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
1401 const LpSolverBase::Value &n)
1403 LpSolverBase::Constr tmp(c);
1404 ///\todo Create an own exception type.
1405 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1406 else tmp.upperBound()=n;
1412 ///\relates LpSolverBase::Constr
1414 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
1415 const LpSolverBase::Constr&c)
1417 LpSolverBase::Constr tmp(c);
1418 ///\todo Create an own exception type.
1419 if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError();
1420 else tmp.upperBound()=n;
1425 ///\relates LpSolverBase::Constr
1427 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
1428 const LpSolverBase::Value &n)
1430 LpSolverBase::Constr tmp(c);
1431 ///\todo Create an own exception type.
1432 if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError();
1433 else tmp.lowerBound()=n;
1439 ///\relates LpSolverBase::DualExpr
1441 inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a,
1442 const LpSolverBase::DualExpr &b)
1444 LpSolverBase::DualExpr tmp(a);
1450 ///\relates LpSolverBase::DualExpr
1452 inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a,
1453 const LpSolverBase::DualExpr &b)
1455 LpSolverBase::DualExpr tmp(a);
1461 ///\relates LpSolverBase::DualExpr
1463 inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a,
1464 const LpSolverBase::Value &b)
1466 LpSolverBase::DualExpr tmp(a);
1473 ///\relates LpSolverBase::DualExpr
1475 inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a,
1476 const LpSolverBase::DualExpr &b)
1478 LpSolverBase::DualExpr tmp(b);
1484 ///\relates LpSolverBase::DualExpr
1486 inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a,
1487 const LpSolverBase::Value &b)
1489 LpSolverBase::DualExpr tmp(a);
1497 #endif //LEMON_LP_BASE_H