2 * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_LP_BASE_H
18 #define LEMON_LP_BASE_H
25 #include<lemon/utility.h>
26 #include<lemon/error.h>
27 #include<lemon/invalid.h>
29 //#include"lin_expr.h"
32 ///\brief The interface of the LP solver interface.
35 ///Internal data structure to convert floating id's to fix one's
37 ///\todo This might by implemented to be usable in other places.
40 std::vector<int> index;
41 std::vector<int> cross;
44 _FixId() : first_free(-1) {};
45 ///Convert a floating id to a fix one
47 ///\param n is a floating id
48 ///\return the corresponding fix id
49 int fixId(int n) {return cross[n];}
50 ///Convert a fix id to a floating one
52 ///\param n is a fix id
53 ///\return the corresponding floating id
54 int floatingId(int n) { return index[n];}
55 ///Add a new floating id.
57 ///\param n is a floating id
58 ///\return the fix id of the new value
59 ///\todo Multiple additions should also be handled.
62 if(n>=int(cross.size())) {
65 cross[n]=index.size();
70 int next=index[first_free];
76 ///\todo Create an own exception type.
77 else throw LogicError(); //floatingId-s must form a continuous range;
81 ///\param n is a fix id
88 for(int i=fl+1;i<int(cross.size());++i) {
94 ///An upper bound on the largest fix id.
96 ///\todo Do we need this?
98 std::size_t maxFixId() { return cross.size()-1; }
102 ///Common base class for LP solvers
119 ///The floating point type used by the solver
120 typedef double Value;
121 ///The infinity constant
122 static const Value INF;
123 ///The not a number constant
124 static const Value NaN;
126 ///Refer to a column of the LP.
128 ///This type is used to refer to a column of the LP.
130 ///Its value remains valid and correct even after the addition or erase of
133 ///\todo Document what can one do with a Col (INVALID, comparing,
134 ///it is similar to Node/Edge)
138 friend class LpSolverBase;
140 typedef Value ExprValue;
141 typedef True LpSolverCol;
143 Col(const Invalid&) : id(-1) {}
144 bool operator<(Col c) const {return id<c.id;}
145 bool operator==(Col c) const {return id==c.id;}
146 bool operator!=(Col c) const {return id==c.id;}
149 ///Refer to a row of the LP.
151 ///This type is used to refer to a row of the LP.
153 ///Its value remains valid and correct even after the addition or erase of
156 ///\todo Document what can one do with a Row (INVALID, comparing,
157 ///it is similar to Node/Edge)
161 friend class LpSolverBase;
163 typedef Value ExprValue;
164 typedef True LpSolverRow;
166 Row(const Invalid&) : id(-1) {}
167 typedef True LpSolverRow;
168 bool operator<(Row c) const {return id<c.id;}
169 bool operator==(Row c) const {return id==c.id;}
170 bool operator!=(Row c) const {return id==c.id;}
174 // typedef SparseLinExpr<Col> Expr;
175 class Expr : public std::map<Col,Value>
178 typedef LpSolverBase::Col Key;
179 typedef LpSolverBase::Value Value;
182 typedef std::map<Col,Value> Base;
186 typedef True IsLinExpression;
188 Expr() : Base(), const_comp(0) { }
190 Expr(const Key &v) : const_comp(0) {
191 Base::insert(std::make_pair(v, 1));
194 Expr(const Value &v) : const_comp(v) {}
196 void set(const Key &v,const Value &c) {
197 Base::insert(std::make_pair(v, c));
200 Value &constComp() { return const_comp; }
202 const Value &constComp() const { return const_comp; }
204 ///Removes the components with zero coefficient.
206 for (Base::iterator i=Base::begin(); i!=Base::end();) {
209 if ((*i).second==0) Base::erase(i);
214 ///Sets all coefficients and the constant component to 0.
221 Expr &operator+=(const Expr &e) {
222 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
223 (*this)[j->first]+=j->second;
224 ///\todo it might be speeded up using "hints"
225 const_comp+=e.const_comp;
229 Expr &operator-=(const Expr &e) {
230 for (Base::const_iterator j=e.begin(); j!=e.end(); ++j)
231 (*this)[j->first]-=j->second;
232 const_comp-=e.const_comp;
236 Expr &operator*=(const Value &c) {
237 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
243 Expr &operator/=(const Value &c) {
244 for (Base::iterator j=Base::begin(); j!=Base::end(); ++j)
252 //typedef LinConstr<Expr> Constr;
256 typedef LpSolverBase::Expr Expr;
257 typedef Expr::Key Key;
258 typedef Expr::Value Value;
260 static const Value INF;
261 static const Value NaN;
262 // static const Value INF=0;
263 // static const Value NaN=1;
270 Constr() : _expr(), _lb(NaN), _ub(NaN) {}
272 Constr(Value lb,const Expr &e,Value ub) :
273 _expr(e), _lb(lb), _ub(ub) {}
275 Constr(const Expr &e,Value ub) :
276 _expr(e), _lb(NaN), _ub(ub) {}
278 Constr(Value lb,const Expr &e) :
279 _expr(e), _lb(lb), _ub(NaN) {}
281 Constr(const Expr &e) :
282 _expr(e), _lb(NaN), _ub(NaN) {}
290 Expr &expr() { return _expr; }
292 const Expr &expr() const { return _expr; }
294 Value &lowerBound() { return _lb; }
296 const Value &lowerBound() const { return _lb; }
298 Value &upperBound() { return _ub; }
300 const Value &upperBound() const { return _ub; }
309 virtual int _addCol() = 0;
311 virtual int _addRow() = 0;
314 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
316 virtual void _setRowCoeffs(int i,
319 Value const * values ) = 0;
322 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
324 virtual void _setColCoeffs(int i,
327 Value const * values ) = 0;
331 /// The lower bound of a variable (column) have to be given by an
332 /// extended number of type Value, i.e. a finite number of type
333 /// Value or -\ref INF.
334 virtual void _setColLowerBound(int i, Value value) = 0;
337 /// The upper bound of a variable (column) have to be given by an
338 /// extended number of type Value, i.e. a finite number of type
339 /// Value or \ref INF.
340 virtual void _setColUpperBound(int i, Value value) = 0;
343 /// The lower bound of a linear expression (row) have to be given by an
344 /// extended number of type Value, i.e. a finite number of type
345 /// Value or -\ref INF.
346 virtual void _setRowLowerBound(int i, Value value) = 0;
349 /// The upper bound of a linear expression (row) have to be given by an
350 /// extended number of type Value, i.e. a finite number of type
351 /// Value or \ref INF.
352 virtual void _setRowUpperBound(int i, Value value) = 0;
355 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
359 ///\bug Wrong interface
361 virtual SolutionType _solve() = 0;
365 ///\bug Wrong interface
367 virtual Value _getSolution(int i) = 0;
370 ///\bug unimplemented!!!!
376 virtual ~LpSolverBase() {}
378 ///\name Building up and modification of the LP
382 ///Add a new empty column (i.e a new variable) to the LP
383 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
385 ///\brief Fill the elements of a container with newly created columns
386 ///(i.e a new variables)
388 ///This magic function takes a container as its argument
389 ///and fills its elements
390 ///with new columns (i.e. variables)
392 ///- a standard STL compatible iterable container with
393 ///\ref Col as its \c values_type
396 ///std::vector<LpSolverBase::Col>
397 ///std::list<LpSolverBase::Col>
399 ///- a standard STL compatible iterable container with
400 ///\ref Col as its \c mapped_type
403 ///std::map<AnyType,LpSolverBase::Col>
405 ///- an iterable lemon \ref concept::WriteMap "write map" like
407 ///ListGraph::NodeMap<LpSolverBase::Col>
408 ///ListGraph::EdgeMap<LpSolverBase::Col>
410 ///\return The number of the created column.
411 ///\bug Iterable nodemap hasn't been implemented yet.
414 int addColSet(T &t) { return 0;}
417 typename enable_if<typename T::value_type::LpSolverCol,int>::type
418 addColSet(T &t,dummy<0> = 0) {
420 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
424 typename enable_if<typename T::value_type::second_type::LpSolverCol,
426 addColSet(T &t,dummy<1> = 1) {
428 for(typename T::iterator i=t.begin();i!=t.end();++i) {
435 typename enable_if<typename T::ValueSet::value_type::LpSolverCol,
437 addColSet(T &t,dummy<2> = 2) {
438 ///\bug <tt>return addColSet(t.valueSet());</tt> should also work.
440 for(typename T::ValueSet::iterator i=t.valueSet().begin();
441 i!=t.valueSet().end();
451 ///Add a new empty row (i.e a new constaint) to the LP
453 ///This function adds a new empty row (i.e a new constaint) to the LP.
454 ///\return The created row
455 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
457 ///Set a row (i.e a constaint) of the LP
459 ///\param r is the row to be modified
460 ///\param l is lower bound (-\ref INF means no bound)
461 ///\param e is a linear expression (see \ref Expr)
462 ///\param u is the upper bound (\ref INF means no bound)
463 ///\bug This is a temportary function. The interface will change to
465 void setRow(Row r, Value l,const Expr &e, Value u) {
466 std::vector<int> indices;
467 std::vector<Value> values;
468 indices.push_back(0);
470 for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
471 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
472 indices.push_back(cols.floatingId((*i).first.id));
473 values.push_back((*i).second);
475 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
476 &indices[0],&values[0]);
477 _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
478 _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
481 ///Set a row (i.e a constaint) of the LP
483 ///\param r is the row to be modified
484 ///\param c is a linear expression (see \ref Constr)
485 void setRow(Row r, const Constr &c) {
487 isnan(c.lowerBound())?-INF:c.lowerBound(),
489 isnan(c.upperBound())?INF:c.upperBound());
492 ///Add a new row (i.e a new constaint) to the LP
494 ///\param l is the lower bound (-\ref INF means no bound)
495 ///\param e is a linear expression (see \ref Expr)
496 ///\param u is the upper bound (\ref INF means no bound)
497 ///\return The created row.
498 ///\bug This is a temportary function. The interface will change to
500 Row addRow(Value l,const Expr &e, Value u) {
506 ///Add a new row (i.e a new constaint) to the LP
508 ///\param c is a linear expression (see \ref Constr)
509 ///\return The created row.
510 Row addRow(const Constr &c) {
516 /// Set the lower bound of a column (i.e a variable)
518 /// The upper bound of a variable (column) have to be given by an
519 /// extended number of type Value, i.e. a finite number of type
520 /// Value or -\ref INF.
521 virtual void setColLowerBound(Col c, Value value) {
522 _setColLowerBound(cols.floatingId(c.id),value);
524 /// Set the upper bound of a column (i.e a variable)
526 /// The upper bound of a variable (column) have to be given by an
527 /// extended number of type Value, i.e. a finite number of type
528 /// Value or \ref INF.
529 virtual void setColUpperBound(Col c, Value value) {
530 _setColUpperBound(cols.floatingId(c.id),value);
532 /// Set the lower bound of a row (i.e a constraint)
534 /// The lower bound of a linear expression (row) have to be given by an
535 /// extended number of type Value, i.e. a finite number of type
536 /// Value or -\ref INF.
537 virtual void setRowLowerBound(Row r, Value value) {
538 _setRowLowerBound(rows.floatingId(r.id),value);
540 /// Set the upper bound of a row (i.e a constraint)
542 /// The upper bound of a linear expression (row) have to be given by an
543 /// extended number of type Value, i.e. a finite number of type
544 /// Value or \ref INF.
545 virtual void setRowUpperBound(Row r, Value value) {
546 _setRowUpperBound(rows.floatingId(r.id),value);
548 ///Set an element of the objective function
549 void setObjCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
550 ///Set the objective function
552 ///\param e is a linear expression of type \ref Expr.
553 ///\todo What to do with the constant component?
554 void setObj(Expr e) {
556 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
557 setObjCoeff((*i).first,(*i).second);
563 ///\name Solving the LP
568 SolutionType solve() { return _solve(); }
572 ///\name Obtaining the solution LP
577 Value solution(Col c) { return _getSolution(cols.floatingId(c.id)); }
585 ///\relates LpSolverBase::Expr
587 inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a,
588 const LpSolverBase::Expr &b)
590 LpSolverBase::Expr tmp(a);
591 tmp+=b; ///\todo Don't STL have some special 'merge' algorithm?
596 ///\relates LpSolverBase::Expr
598 inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a,
599 const LpSolverBase::Expr &b)
601 LpSolverBase::Expr tmp(a);
602 tmp-=b; ///\todo Don't STL have some special 'merge' algorithm?
607 ///\relates LpSolverBase::Expr
609 inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a,
610 const LpSolverBase::Value &b)
612 LpSolverBase::Expr tmp(a);
613 tmp*=b; ///\todo Don't STL have some special 'merge' algorithm?
619 ///\relates LpSolverBase::Expr
621 inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a,
622 const LpSolverBase::Expr &b)
624 LpSolverBase::Expr tmp(b);
625 tmp*=a; ///\todo Don't STL have some special 'merge' algorithm?
630 ///\relates LpSolverBase::Expr
632 inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a,
633 const LpSolverBase::Value &b)
635 LpSolverBase::Expr tmp(a);
636 tmp/=b; ///\todo Don't STL have some special 'merge' algorithm?
642 ///\relates LpSolverBase::Constr
644 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
645 const LpSolverBase::Expr &f)
647 return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0);
652 ///\relates LpSolverBase::Constr
654 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e,
655 const LpSolverBase::Expr &f)
657 return LpSolverBase::Constr(e,f);
662 ///\relates LpSolverBase::Constr
664 inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e,
665 const LpSolverBase::Value &f)
667 return LpSolverBase::Constr(e,f);
672 ///\relates LpSolverBase::Constr
674 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
675 const LpSolverBase::Expr &f)
677 return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0);
683 ///\relates LpSolverBase::Constr
685 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e,
686 const LpSolverBase::Expr &f)
688 return LpSolverBase::Constr(f,e);
694 ///\relates LpSolverBase::Constr
696 inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e,
697 const LpSolverBase::Value &f)
699 return LpSolverBase::Constr(f,e);
704 ///\relates LpSolverBase::Constr
706 inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e,
707 const LpSolverBase::Expr &f)
709 return LpSolverBase::Constr(0,e-f,0);
714 ///\relates LpSolverBase::Constr
716 inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n,
717 const LpSolverBase::Constr&c)
719 LpSolverBase::Constr tmp(c);
720 ///\todo Create an own exception type.
721 if(!isnan(tmp.lowerBound())) throw LogicError();
722 else tmp.lowerBound()=n;
727 ///\relates LpSolverBase::Constr
729 inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c,
730 const LpSolverBase::Value &n)
732 LpSolverBase::Constr tmp(c);
733 ///\todo Create an own exception type.
734 if(!isnan(tmp.upperBound())) throw LogicError();
735 else tmp.upperBound()=n;
741 ///\relates LpSolverBase::Constr
743 inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n,
744 const LpSolverBase::Constr&c)
746 LpSolverBase::Constr tmp(c);
747 ///\todo Create an own exception type.
748 if(!isnan(tmp.upperBound())) throw LogicError();
749 else tmp.upperBound()=n;
754 ///\relates LpSolverBase::Constr
756 inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c,
757 const LpSolverBase::Value &n)
759 LpSolverBase::Constr tmp(c);
760 ///\todo Create an own exception type.
761 if(!isnan(tmp.lowerBound())) throw LogicError();
762 else tmp.lowerBound()=n;
769 #endif //LEMON_LP_BASE_H