1 | /* -*- C++ -*- |
---|
2 | * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
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. |
---|
10 | * |
---|
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 |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_LP_BASE_H |
---|
18 | #define LEMON_LP_BASE_H |
---|
19 | |
---|
20 | #include<vector> |
---|
21 | #include<map> |
---|
22 | #include<limits> |
---|
23 | #include<math.h> |
---|
24 | |
---|
25 | #include<lemon/utility.h> |
---|
26 | #include<lemon/error.h> |
---|
27 | #include<lemon/invalid.h> |
---|
28 | |
---|
29 | //#include"lin_expr.h" |
---|
30 | |
---|
31 | ///\file |
---|
32 | ///\brief The interface of the LP solver interface. |
---|
33 | ///\ingroup gen_opt_group |
---|
34 | namespace lemon { |
---|
35 | |
---|
36 | ///Internal data structure to convert floating id's to fix one's |
---|
37 | |
---|
38 | ///\todo This might be implemented to be also usable in other places. |
---|
39 | class _FixId |
---|
40 | { |
---|
41 | std::vector<int> index; |
---|
42 | std::vector<int> cross; |
---|
43 | int first_free; |
---|
44 | public: |
---|
45 | _FixId() : first_free(-1) {}; |
---|
46 | ///Convert a floating id to a fix one |
---|
47 | |
---|
48 | ///\param n is a floating id |
---|
49 | ///\return the corresponding fix id |
---|
50 | int fixId(int n) {return cross[n];} |
---|
51 | ///Convert a fix id to a floating one |
---|
52 | |
---|
53 | ///\param n is a fix id |
---|
54 | ///\return the corresponding floating id |
---|
55 | int floatingId(int n) { return index[n];} |
---|
56 | ///Add a new floating id. |
---|
57 | |
---|
58 | ///\param n is a floating id |
---|
59 | ///\return the fix id of the new value |
---|
60 | ///\todo Multiple additions should also be handled. |
---|
61 | int insert(int n) |
---|
62 | { |
---|
63 | if(n>=int(cross.size())) { |
---|
64 | cross.resize(n+1); |
---|
65 | if(first_free==-1) { |
---|
66 | cross[n]=index.size(); |
---|
67 | index.push_back(n); |
---|
68 | } |
---|
69 | else { |
---|
70 | cross[n]=first_free; |
---|
71 | int next=index[first_free]; |
---|
72 | index[first_free]=n; |
---|
73 | first_free=next; |
---|
74 | } |
---|
75 | return cross[n]; |
---|
76 | } |
---|
77 | ///\todo Create an own exception type. |
---|
78 | else throw LogicError(); //floatingId-s must form a continuous range; |
---|
79 | } |
---|
80 | ///Remove a fix id. |
---|
81 | |
---|
82 | ///\param n is a fix id |
---|
83 | /// |
---|
84 | void erase(int n) |
---|
85 | { |
---|
86 | int fl=index[n]; |
---|
87 | index[n]=first_free; |
---|
88 | first_free=n; |
---|
89 | for(int i=fl+1;i<int(cross.size());++i) { |
---|
90 | cross[i-1]=cross[i]; |
---|
91 | index[cross[i]]--; |
---|
92 | } |
---|
93 | cross.pop_back(); |
---|
94 | } |
---|
95 | ///An upper bound on the largest fix id. |
---|
96 | |
---|
97 | ///\todo Do we need this? |
---|
98 | /// |
---|
99 | std::size_t maxFixId() { return cross.size()-1; } |
---|
100 | |
---|
101 | }; |
---|
102 | |
---|
103 | ///Common base class for LP solvers |
---|
104 | |
---|
105 | ///\todo Much more docs |
---|
106 | ///\ingroup gen_opt_group |
---|
107 | class LpSolverBase { |
---|
108 | |
---|
109 | public: |
---|
110 | |
---|
111 | ///\e |
---|
112 | enum SolveExitStatus { |
---|
113 | ///\e |
---|
114 | SOLVED = 0, |
---|
115 | ///\e |
---|
116 | UNSOLVED = 1 |
---|
117 | }; |
---|
118 | |
---|
119 | ///\e |
---|
120 | enum SolutionStatus { |
---|
121 | ///Feasible solution has'n been found (but may exist). |
---|
122 | |
---|
123 | ///\todo NOTFOUND might be a better name. |
---|
124 | /// |
---|
125 | UNDEFINED = 0, |
---|
126 | ///The problem has no feasible solution |
---|
127 | INFEASIBLE = 1, |
---|
128 | ///Feasible solution found |
---|
129 | FEASIBLE = 2, |
---|
130 | ///Optimal solution exists and found |
---|
131 | OPTIMAL = 3, |
---|
132 | ///The cost function is unbounded |
---|
133 | |
---|
134 | ///\todo Give a feasible solution and an infinite ray (and the |
---|
135 | ///corresponding bases) |
---|
136 | INFINITE = 4 |
---|
137 | }; |
---|
138 | |
---|
139 | ///The floating point type used by the solver |
---|
140 | typedef double Value; |
---|
141 | ///The infinity constant |
---|
142 | static const Value INF; |
---|
143 | ///The not a number constant |
---|
144 | static const Value NaN; |
---|
145 | |
---|
146 | ///Refer to a column of the LP. |
---|
147 | |
---|
148 | ///This type is used to refer to a column of the LP. |
---|
149 | /// |
---|
150 | ///Its value remains valid and correct even after the addition or erase of |
---|
151 | ///other columns. |
---|
152 | /// |
---|
153 | ///\todo Document what can one do with a Col (INVALID, comparing, |
---|
154 | ///it is similar to Node/Edge) |
---|
155 | class Col { |
---|
156 | protected: |
---|
157 | int id; |
---|
158 | friend class LpSolverBase; |
---|
159 | public: |
---|
160 | typedef Value ExprValue; |
---|
161 | typedef True LpSolverCol; |
---|
162 | Col() {} |
---|
163 | Col(const Invalid&) : id(-1) {} |
---|
164 | bool operator<(Col c) const {return id<c.id;} |
---|
165 | bool operator==(Col c) const {return id==c.id;} |
---|
166 | bool operator!=(Col c) const {return id==c.id;} |
---|
167 | }; |
---|
168 | |
---|
169 | ///Refer to a row of the LP. |
---|
170 | |
---|
171 | ///This type is used to refer to a row of the LP. |
---|
172 | /// |
---|
173 | ///Its value remains valid and correct even after the addition or erase of |
---|
174 | ///other rows. |
---|
175 | /// |
---|
176 | ///\todo Document what can one do with a Row (INVALID, comparing, |
---|
177 | ///it is similar to Node/Edge) |
---|
178 | class Row { |
---|
179 | protected: |
---|
180 | int id; |
---|
181 | friend class LpSolverBase; |
---|
182 | public: |
---|
183 | typedef Value ExprValue; |
---|
184 | typedef True LpSolverRow; |
---|
185 | Row() {} |
---|
186 | Row(const Invalid&) : id(-1) {} |
---|
187 | typedef True LpSolverRow; |
---|
188 | bool operator<(Row c) const {return id<c.id;} |
---|
189 | bool operator==(Row c) const {return id==c.id;} |
---|
190 | bool operator!=(Row c) const {return id==c.id;} |
---|
191 | }; |
---|
192 | |
---|
193 | ///Linear expression of variables and a constant component |
---|
194 | |
---|
195 | ///This data structure strores a linear expression of the variables |
---|
196 | ///(\ref Col "Col"s) and also has a constant component. |
---|
197 | /// |
---|
198 | ///There are several ways to access and modify the contents of this |
---|
199 | ///container. |
---|
200 | ///- Its it fully compatible with \c std::map<Col,double>, so for expamle |
---|
201 | ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can |
---|
202 | ///read and modify the coefficients like |
---|
203 | ///these. |
---|
204 | ///\code |
---|
205 | ///e[v]=5; |
---|
206 | ///e[v]+=12; |
---|
207 | ///e.erase(v); |
---|
208 | ///\endcode |
---|
209 | ///or you can also iterate through its elements. |
---|
210 | ///\code |
---|
211 | ///double s=0; |
---|
212 | ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) |
---|
213 | /// s+=i->second; |
---|
214 | ///\endcode |
---|
215 | ///(This code computes the sum of all coefficients). |
---|
216 | ///- Numbers (<tt>double</tt>'s) |
---|
217 | ///and variables (\ref Col "Col"s) directly convert to an |
---|
218 | ///\ref Expr and the usual linear operations are defined so |
---|
219 | ///\code |
---|
220 | ///v+w |
---|
221 | ///2*v-3.12*(v-w/2)+2 |
---|
222 | ///v*2.1+(3*v+(v*12+w+6)*3)/2 |
---|
223 | ///\endcode |
---|
224 | ///are valid \ref Expr "Expr"essions. |
---|
225 | ///The usual assignment operations are also defined. |
---|
226 | ///\code |
---|
227 | ///e=v+w; |
---|
228 | ///e+=2*v-3.12*(v-w/2)+2; |
---|
229 | ///e*=3.4; |
---|
230 | ///e/=5; |
---|
231 | ///\endcode |
---|
232 | ///- The constant member can be set and read by \ref constComp() |
---|
233 | ///\code |
---|
234 | ///e.constComp()=12; |
---|
235 | ///double c=e.constComp(); |
---|
236 | ///\endcode |
---|
237 | /// |
---|
238 | ///\note \ref clear() not only sets all coefficients to 0 but also |
---|
239 | ///clears the constant components. |
---|
240 | /// |
---|
241 | ///\sa Constr |
---|
242 | /// |
---|
243 | class Expr : public std::map<Col,Value> |
---|
244 | { |
---|
245 | public: |
---|
246 | typedef LpSolverBase::Col Key; |
---|
247 | typedef LpSolverBase::Value Value; |
---|
248 | |
---|
249 | protected: |
---|
250 | typedef std::map<Col,Value> Base; |
---|
251 | |
---|
252 | Value const_comp; |
---|
253 | public: |
---|
254 | typedef True IsLinExpression; |
---|
255 | ///\e |
---|
256 | Expr() : Base(), const_comp(0) { } |
---|
257 | ///\e |
---|
258 | Expr(const Key &v) : const_comp(0) { |
---|
259 | Base::insert(std::make_pair(v, 1)); |
---|
260 | } |
---|
261 | ///\e |
---|
262 | Expr(const Value &v) : const_comp(v) {} |
---|
263 | ///\e |
---|
264 | void set(const Key &v,const Value &c) { |
---|
265 | Base::insert(std::make_pair(v, c)); |
---|
266 | } |
---|
267 | ///\e |
---|
268 | Value &constComp() { return const_comp; } |
---|
269 | ///\e |
---|
270 | const Value &constComp() const { return const_comp; } |
---|
271 | |
---|
272 | ///Removes the components with zero coefficient. |
---|
273 | void simplify() { |
---|
274 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
275 | Base::iterator j=i; |
---|
276 | ++j; |
---|
277 | if ((*i).second==0) Base::erase(i); |
---|
278 | j=i; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | ///Sets all coefficients and the constant component to 0. |
---|
283 | void clear() { |
---|
284 | Base::clear(); |
---|
285 | const_comp=0; |
---|
286 | } |
---|
287 | |
---|
288 | ///\e |
---|
289 | Expr &operator+=(const Expr &e) { |
---|
290 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
291 | (*this)[j->first]+=j->second; |
---|
292 | ///\todo it might be speeded up using "hints" |
---|
293 | const_comp+=e.const_comp; |
---|
294 | return *this; |
---|
295 | } |
---|
296 | ///\e |
---|
297 | Expr &operator-=(const Expr &e) { |
---|
298 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
299 | (*this)[j->first]-=j->second; |
---|
300 | const_comp-=e.const_comp; |
---|
301 | return *this; |
---|
302 | } |
---|
303 | ///\e |
---|
304 | Expr &operator*=(const Value &c) { |
---|
305 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
306 | j->second*=c; |
---|
307 | const_comp*=c; |
---|
308 | return *this; |
---|
309 | } |
---|
310 | ///\e |
---|
311 | Expr &operator/=(const Value &c) { |
---|
312 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
313 | j->second/=c; |
---|
314 | const_comp/=c; |
---|
315 | return *this; |
---|
316 | } |
---|
317 | }; |
---|
318 | |
---|
319 | ///Linear constraint |
---|
320 | |
---|
321 | ///This data stucture represents a linear constraint in the LP. |
---|
322 | ///Basically it is a linear expression with a lower or an upper bound |
---|
323 | ///(or both). These parts of the constraint can be obtained by the member |
---|
324 | ///functions \ref expr(), \ref lowerBound() and \ref upperBound(), |
---|
325 | ///respectively. |
---|
326 | ///There are two ways to construct a constraint. |
---|
327 | ///- You can set the linear expression and the bounds directly |
---|
328 | /// by the functions above. |
---|
329 | ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt> |
---|
330 | /// are defined between expressions, or even between constraints whenever |
---|
331 | /// it makes sense. Therefore if \c e and \c f are linear expressions and |
---|
332 | /// \c s and \c t are numbers, then the followings are valid expressions |
---|
333 | /// and thus they can be used directly e.g. in \ref addRow() whenever |
---|
334 | /// it makes sense. |
---|
335 | /// \code |
---|
336 | /// e<=s |
---|
337 | /// e<=f |
---|
338 | /// s<=e<=t |
---|
339 | /// e>=t |
---|
340 | /// \endcode |
---|
341 | ///\warning The validity of a constraint is checked only at run time, so |
---|
342 | ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a |
---|
343 | ///\ref LogicError exception. |
---|
344 | class Constr |
---|
345 | { |
---|
346 | public: |
---|
347 | typedef LpSolverBase::Expr Expr; |
---|
348 | typedef Expr::Key Key; |
---|
349 | typedef Expr::Value Value; |
---|
350 | |
---|
351 | // static const Value INF; |
---|
352 | // static const Value NaN; |
---|
353 | |
---|
354 | protected: |
---|
355 | Expr _expr; |
---|
356 | Value _lb,_ub; |
---|
357 | public: |
---|
358 | ///\e |
---|
359 | Constr() : _expr(), _lb(NaN), _ub(NaN) {} |
---|
360 | ///\e |
---|
361 | Constr(Value lb,const Expr &e,Value ub) : |
---|
362 | _expr(e), _lb(lb), _ub(ub) {} |
---|
363 | ///\e |
---|
364 | Constr(const Expr &e,Value ub) : |
---|
365 | _expr(e), _lb(NaN), _ub(ub) {} |
---|
366 | ///\e |
---|
367 | Constr(Value lb,const Expr &e) : |
---|
368 | _expr(e), _lb(lb), _ub(NaN) {} |
---|
369 | ///\e |
---|
370 | Constr(const Expr &e) : |
---|
371 | _expr(e), _lb(NaN), _ub(NaN) {} |
---|
372 | ///\e |
---|
373 | void clear() |
---|
374 | { |
---|
375 | _expr.clear(); |
---|
376 | _lb=_ub=NaN; |
---|
377 | } |
---|
378 | |
---|
379 | ///Reference to the linear expression |
---|
380 | Expr &expr() { return _expr; } |
---|
381 | ///Cont reference to the linear expression |
---|
382 | const Expr &expr() const { return _expr; } |
---|
383 | ///Reference to the lower bound. |
---|
384 | |
---|
385 | ///\return |
---|
386 | ///- -\ref INF: the constraint is lower unbounded. |
---|
387 | ///- -\ref NaN: lower bound has not been set. |
---|
388 | ///- finite number: the lower bound |
---|
389 | Value &lowerBound() { return _lb; } |
---|
390 | ///The const version of \ref lowerBound() |
---|
391 | const Value &lowerBound() const { return _lb; } |
---|
392 | ///Reference to the upper bound. |
---|
393 | |
---|
394 | ///\return |
---|
395 | ///- -\ref INF: the constraint is upper unbounded. |
---|
396 | ///- -\ref NaN: upper bound has not been set. |
---|
397 | ///- finite number: the upper bound |
---|
398 | Value &upperBound() { return _ub; } |
---|
399 | ///The const version of \ref upperBound() |
---|
400 | const Value &upperBound() const { return _ub; } |
---|
401 | ///Is the constraint lower bounded? |
---|
402 | bool lowerBounded() const { |
---|
403 | using namespace std; |
---|
404 | return isfinite(_lb); |
---|
405 | } |
---|
406 | ///Is the constraint upper bounded? |
---|
407 | bool upperBounded() const { |
---|
408 | using namespace std; |
---|
409 | return isfinite(_ub); |
---|
410 | } |
---|
411 | }; |
---|
412 | |
---|
413 | |
---|
414 | protected: |
---|
415 | _FixId rows; |
---|
416 | _FixId cols; |
---|
417 | |
---|
418 | //Abstract virtual functions |
---|
419 | virtual LpSolverBase &_newLp() = 0; |
---|
420 | virtual LpSolverBase &_copyLp() = 0; |
---|
421 | |
---|
422 | virtual int _addCol() = 0; |
---|
423 | virtual int _addRow() = 0; |
---|
424 | virtual void _setRowCoeffs(int i, |
---|
425 | int length, |
---|
426 | int const * indices, |
---|
427 | Value const * values ) = 0; |
---|
428 | virtual void _setColCoeffs(int i, |
---|
429 | int length, |
---|
430 | int const * indices, |
---|
431 | Value const * values ) = 0; |
---|
432 | virtual void _setColLowerBound(int i, Value value) = 0; |
---|
433 | virtual void _setColUpperBound(int i, Value value) = 0; |
---|
434 | virtual void _setRowLowerBound(int i, Value value) = 0; |
---|
435 | virtual void _setRowUpperBound(int i, Value value) = 0; |
---|
436 | virtual void _setObjCoeff(int i, Value obj_coef) = 0; |
---|
437 | virtual void _clearObj()=0; |
---|
438 | // virtual void _setObj(int length, |
---|
439 | // int const * indices, |
---|
440 | // Value const * values ) = 0; |
---|
441 | virtual SolveExitStatus _solve() = 0; |
---|
442 | virtual Value _getPrimal(int i) = 0; |
---|
443 | virtual Value _getPrimalValue() = 0; |
---|
444 | virtual SolutionStatus _getPrimalStatus() = 0; |
---|
445 | virtual void _setMax() = 0; |
---|
446 | virtual void _setMin() = 0; |
---|
447 | |
---|
448 | //Own protected stuff |
---|
449 | |
---|
450 | //Constant component of the objective function |
---|
451 | Value obj_const_comp; |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | |
---|
456 | public: |
---|
457 | |
---|
458 | ///\e |
---|
459 | LpSolverBase() : obj_const_comp(0) {} |
---|
460 | |
---|
461 | ///\e |
---|
462 | virtual ~LpSolverBase() {} |
---|
463 | |
---|
464 | ///Creates a new LP problem |
---|
465 | LpSolverBase &newLp() {return _newLp();} |
---|
466 | ///Make a copy of the LP problem |
---|
467 | LpSolverBase ©Lp() {return _copyLp();} |
---|
468 | |
---|
469 | ///\name Build up and modify of the LP |
---|
470 | |
---|
471 | ///@{ |
---|
472 | |
---|
473 | ///Add a new empty column (i.e a new variable) to the LP |
---|
474 | Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} |
---|
475 | |
---|
476 | ///\brief Adds several new columns |
---|
477 | ///(i.e a variables) at once |
---|
478 | /// |
---|
479 | ///This magic function takes a container as its argument |
---|
480 | ///and fills its elements |
---|
481 | ///with new columns (i.e. variables) |
---|
482 | ///\param t can be |
---|
483 | ///- a standard STL compatible iterable container with |
---|
484 | ///\ref Col as its \c values_type |
---|
485 | ///like |
---|
486 | ///\code |
---|
487 | ///std::vector<LpSolverBase::Col> |
---|
488 | ///std::list<LpSolverBase::Col> |
---|
489 | ///\endcode |
---|
490 | ///- a standard STL compatible iterable container with |
---|
491 | ///\ref Col as its \c mapped_type |
---|
492 | ///like |
---|
493 | ///\code |
---|
494 | ///std::map<AnyType,LpSolverBase::Col> |
---|
495 | ///\endcode |
---|
496 | ///- an iterable lemon \ref concept::WriteMap "write map" like |
---|
497 | ///\code |
---|
498 | ///ListGraph::NodeMap<LpSolverBase::Col> |
---|
499 | ///ListGraph::EdgeMap<LpSolverBase::Col> |
---|
500 | ///\endcode |
---|
501 | ///\return The number of the created column. |
---|
502 | #ifdef DOXYGEN |
---|
503 | template<class T> |
---|
504 | int addColSet(T &t) { return 0;} |
---|
505 | #else |
---|
506 | template<class T> |
---|
507 | typename enable_if<typename T::value_type::LpSolverCol,int>::type |
---|
508 | addColSet(T &t,dummy<0> = 0) { |
---|
509 | int s=0; |
---|
510 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} |
---|
511 | return s; |
---|
512 | } |
---|
513 | template<class T> |
---|
514 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
515 | int>::type |
---|
516 | addColSet(T &t,dummy<1> = 1) { |
---|
517 | int s=0; |
---|
518 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
519 | i->second=addCol(); |
---|
520 | s++; |
---|
521 | } |
---|
522 | return s; |
---|
523 | } |
---|
524 | template<class T> |
---|
525 | typename enable_if<typename T::ValueSet::value_type::LpSolverCol, |
---|
526 | int>::type |
---|
527 | addColSet(T &t,dummy<2> = 2) { |
---|
528 | ///\bug <tt>return addColSet(t.valueSet());</tt> should also work. |
---|
529 | int s=0; |
---|
530 | for(typename T::ValueSet::iterator i=t.valueSet().begin(); |
---|
531 | i!=t.valueSet().end(); |
---|
532 | ++i) |
---|
533 | { |
---|
534 | *i=addCol(); |
---|
535 | s++; |
---|
536 | } |
---|
537 | return s; |
---|
538 | } |
---|
539 | #endif |
---|
540 | |
---|
541 | ///Add a new empty row (i.e a new constaint) to the LP |
---|
542 | |
---|
543 | ///This function adds a new empty row (i.e a new constaint) to the LP. |
---|
544 | ///\return The created row |
---|
545 | Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} |
---|
546 | |
---|
547 | ///Set a row (i.e a constaint) of the LP |
---|
548 | |
---|
549 | ///\param r is the row to be modified |
---|
550 | ///\param l is lower bound (-\ref INF means no bound) |
---|
551 | ///\param e is a linear expression (see \ref Expr) |
---|
552 | ///\param u is the upper bound (\ref INF means no bound) |
---|
553 | ///\bug This is a temportary function. The interface will change to |
---|
554 | ///a better one. |
---|
555 | ///\todo Option to control whether a constraint with a single variable is |
---|
556 | ///added or not. |
---|
557 | void setRow(Row r, Value l,const Expr &e, Value u) { |
---|
558 | std::vector<int> indices; |
---|
559 | std::vector<Value> values; |
---|
560 | indices.push_back(0); |
---|
561 | values.push_back(0); |
---|
562 | for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) |
---|
563 | if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! |
---|
564 | indices.push_back(cols.floatingId((*i).first.id)); |
---|
565 | values.push_back((*i).second); |
---|
566 | } |
---|
567 | _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, |
---|
568 | &indices[0],&values[0]); |
---|
569 | _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); |
---|
570 | _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); |
---|
571 | } |
---|
572 | |
---|
573 | ///Set a row (i.e a constaint) of the LP |
---|
574 | |
---|
575 | ///\param r is the row to be modified |
---|
576 | ///\param c is a linear expression (see \ref Constr) |
---|
577 | void setRow(Row r, const Constr &c) { |
---|
578 | setRow(r, |
---|
579 | c.lowerBounded()?c.lowerBound():-INF, |
---|
580 | c.expr(), |
---|
581 | c.upperBounded()?c.upperBound():INF); |
---|
582 | } |
---|
583 | |
---|
584 | ///Add a new row (i.e a new constaint) to the LP |
---|
585 | |
---|
586 | ///\param l is the lower bound (-\ref INF means no bound) |
---|
587 | ///\param e is a linear expression (see \ref Expr) |
---|
588 | ///\param u is the upper bound (\ref INF means no bound) |
---|
589 | ///\return The created row. |
---|
590 | ///\bug This is a temportary function. The interface will change to |
---|
591 | ///a better one. |
---|
592 | Row addRow(Value l,const Expr &e, Value u) { |
---|
593 | Row r=addRow(); |
---|
594 | setRow(r,l,e,u); |
---|
595 | return r; |
---|
596 | } |
---|
597 | |
---|
598 | ///Add a new row (i.e a new constaint) to the LP |
---|
599 | |
---|
600 | ///\param c is a linear expression (see \ref Constr) |
---|
601 | ///\return The created row. |
---|
602 | Row addRow(const Constr &c) { |
---|
603 | Row r=addRow(); |
---|
604 | setRow(r,c); |
---|
605 | return r; |
---|
606 | } |
---|
607 | |
---|
608 | /// Set the lower bound of a column (i.e a variable) |
---|
609 | |
---|
610 | /// The upper bound of a variable (column) has to be given by an |
---|
611 | /// extended number of type Value, i.e. a finite number of type |
---|
612 | /// Value or -\ref INF. |
---|
613 | void colLowerBound(Col c, Value value) { |
---|
614 | _setColLowerBound(cols.floatingId(c.id),value); |
---|
615 | } |
---|
616 | /// Set the upper bound of a column (i.e a variable) |
---|
617 | |
---|
618 | /// The upper bound of a variable (column) has to be given by an |
---|
619 | /// extended number of type Value, i.e. a finite number of type |
---|
620 | /// Value or \ref INF. |
---|
621 | void colUpperBound(Col c, Value value) { |
---|
622 | _setColUpperBound(cols.floatingId(c.id),value); |
---|
623 | }; |
---|
624 | /// Set the lower and the upper bounds of a column (i.e a variable) |
---|
625 | |
---|
626 | /// The lower and the upper bounds of |
---|
627 | /// a variable (column) have to be given by an |
---|
628 | /// extended number of type Value, i.e. a finite number of type |
---|
629 | /// Value, -\ref INF or \ref INF. |
---|
630 | void colBounds(Col c, Value lower, Value upper) { |
---|
631 | _setColLowerBound(cols.floatingId(c.id),lower); |
---|
632 | _setColUpperBound(cols.floatingId(c.id),upper); |
---|
633 | } |
---|
634 | |
---|
635 | /// Set the lower bound of a row (i.e a constraint) |
---|
636 | |
---|
637 | /// The lower bound of a linear expression (row) has to be given by an |
---|
638 | /// extended number of type Value, i.e. a finite number of type |
---|
639 | /// Value or -\ref INF. |
---|
640 | void rowLowerBound(Row r, Value value) { |
---|
641 | _setRowLowerBound(rows.floatingId(r.id),value); |
---|
642 | }; |
---|
643 | /// Set the upper bound of a row (i.e a constraint) |
---|
644 | |
---|
645 | /// The upper bound of a linear expression (row) has to be given by an |
---|
646 | /// extended number of type Value, i.e. a finite number of type |
---|
647 | /// Value or \ref INF. |
---|
648 | void rowUpperBound(Row r, Value value) { |
---|
649 | _setRowUpperBound(rows.floatingId(r.id),value); |
---|
650 | }; |
---|
651 | /// Set the lower and the upper bounds of a row (i.e a variable) |
---|
652 | |
---|
653 | /// The lower and the upper bounds of |
---|
654 | /// a constraint (row) have to be given by an |
---|
655 | /// extended number of type Value, i.e. a finite number of type |
---|
656 | /// Value, -\ref INF or \ref INF. |
---|
657 | void rowBounds(Row c, Value lower, Value upper) { |
---|
658 | _setRowLowerBound(rows.floatingId(c.id),lower); |
---|
659 | _setRowUpperBound(rows.floatingId(c.id),upper); |
---|
660 | } |
---|
661 | |
---|
662 | ///Set an element of the objective function |
---|
663 | void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; |
---|
664 | ///Set the objective function |
---|
665 | |
---|
666 | ///\param e is a linear expression of type \ref Expr. |
---|
667 | ///\bug The previous objective function is not cleared! |
---|
668 | void setObj(Expr e) { |
---|
669 | _clearObj(); |
---|
670 | for (Expr::iterator i=e.begin(); i!=e.end(); ++i) |
---|
671 | objCoeff((*i).first,(*i).second); |
---|
672 | obj_const_comp=e.constComp(); |
---|
673 | } |
---|
674 | |
---|
675 | ///Maximize |
---|
676 | void max() { _setMax(); } |
---|
677 | ///Minimize |
---|
678 | void min() { _setMin(); } |
---|
679 | |
---|
680 | |
---|
681 | ///@} |
---|
682 | |
---|
683 | |
---|
684 | ///\name Solve the LP |
---|
685 | |
---|
686 | ///@{ |
---|
687 | |
---|
688 | ///\e |
---|
689 | SolveExitStatus solve() { return _solve(); } |
---|
690 | |
---|
691 | ///@} |
---|
692 | |
---|
693 | ///\name Obtain the solution |
---|
694 | |
---|
695 | ///@{ |
---|
696 | |
---|
697 | ///\e |
---|
698 | SolutionStatus primalStatus() { |
---|
699 | return _getPrimalStatus(); |
---|
700 | } |
---|
701 | |
---|
702 | ///\e |
---|
703 | Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } |
---|
704 | |
---|
705 | ///\e |
---|
706 | |
---|
707 | ///\return |
---|
708 | ///- \ref INF or -\ref INF means either infeasibility or unboundedness |
---|
709 | /// of the primal problem, depending on whether we minimize or maximize. |
---|
710 | ///- \ref NaN if no primal solution is found. |
---|
711 | ///- The (finite) objective value if an optimal solution is found. |
---|
712 | Value primalValue() { return _getPrimalValue()+obj_const_comp;} |
---|
713 | ///@} |
---|
714 | |
---|
715 | }; |
---|
716 | |
---|
717 | ///\e |
---|
718 | |
---|
719 | ///\relates LpSolverBase::Expr |
---|
720 | /// |
---|
721 | inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, |
---|
722 | const LpSolverBase::Expr &b) |
---|
723 | { |
---|
724 | LpSolverBase::Expr tmp(a); |
---|
725 | tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
726 | return tmp; |
---|
727 | } |
---|
728 | ///\e |
---|
729 | |
---|
730 | ///\relates LpSolverBase::Expr |
---|
731 | /// |
---|
732 | inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, |
---|
733 | const LpSolverBase::Expr &b) |
---|
734 | { |
---|
735 | LpSolverBase::Expr tmp(a); |
---|
736 | tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
737 | return tmp; |
---|
738 | } |
---|
739 | ///\e |
---|
740 | |
---|
741 | ///\relates LpSolverBase::Expr |
---|
742 | /// |
---|
743 | inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, |
---|
744 | const LpSolverBase::Value &b) |
---|
745 | { |
---|
746 | LpSolverBase::Expr tmp(a); |
---|
747 | tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
748 | return tmp; |
---|
749 | } |
---|
750 | |
---|
751 | ///\e |
---|
752 | |
---|
753 | ///\relates LpSolverBase::Expr |
---|
754 | /// |
---|
755 | inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, |
---|
756 | const LpSolverBase::Expr &b) |
---|
757 | { |
---|
758 | LpSolverBase::Expr tmp(b); |
---|
759 | tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
760 | return tmp; |
---|
761 | } |
---|
762 | ///\e |
---|
763 | |
---|
764 | ///\relates LpSolverBase::Expr |
---|
765 | /// |
---|
766 | inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, |
---|
767 | const LpSolverBase::Value &b) |
---|
768 | { |
---|
769 | LpSolverBase::Expr tmp(a); |
---|
770 | tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
771 | return tmp; |
---|
772 | } |
---|
773 | |
---|
774 | ///\e |
---|
775 | |
---|
776 | ///\relates LpSolverBase::Constr |
---|
777 | /// |
---|
778 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
779 | const LpSolverBase::Expr &f) |
---|
780 | { |
---|
781 | return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); |
---|
782 | } |
---|
783 | |
---|
784 | ///\e |
---|
785 | |
---|
786 | ///\relates LpSolverBase::Constr |
---|
787 | /// |
---|
788 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, |
---|
789 | const LpSolverBase::Expr &f) |
---|
790 | { |
---|
791 | return LpSolverBase::Constr(e,f); |
---|
792 | } |
---|
793 | |
---|
794 | ///\e |
---|
795 | |
---|
796 | ///\relates LpSolverBase::Constr |
---|
797 | /// |
---|
798 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
799 | const LpSolverBase::Value &f) |
---|
800 | { |
---|
801 | return LpSolverBase::Constr(e,f); |
---|
802 | } |
---|
803 | |
---|
804 | ///\e |
---|
805 | |
---|
806 | ///\relates LpSolverBase::Constr |
---|
807 | /// |
---|
808 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
809 | const LpSolverBase::Expr &f) |
---|
810 | { |
---|
811 | return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); |
---|
812 | } |
---|
813 | |
---|
814 | |
---|
815 | ///\e |
---|
816 | |
---|
817 | ///\relates LpSolverBase::Constr |
---|
818 | /// |
---|
819 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, |
---|
820 | const LpSolverBase::Expr &f) |
---|
821 | { |
---|
822 | return LpSolverBase::Constr(f,e); |
---|
823 | } |
---|
824 | |
---|
825 | |
---|
826 | ///\e |
---|
827 | |
---|
828 | ///\relates LpSolverBase::Constr |
---|
829 | /// |
---|
830 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
831 | const LpSolverBase::Value &f) |
---|
832 | { |
---|
833 | return LpSolverBase::Constr(f,e); |
---|
834 | } |
---|
835 | |
---|
836 | ///\e |
---|
837 | |
---|
838 | ///\relates LpSolverBase::Constr |
---|
839 | /// |
---|
840 | inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, |
---|
841 | const LpSolverBase::Expr &f) |
---|
842 | { |
---|
843 | return LpSolverBase::Constr(0,e-f,0); |
---|
844 | } |
---|
845 | |
---|
846 | ///\e |
---|
847 | |
---|
848 | ///\relates LpSolverBase::Constr |
---|
849 | /// |
---|
850 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, |
---|
851 | const LpSolverBase::Constr&c) |
---|
852 | { |
---|
853 | LpSolverBase::Constr tmp(c); |
---|
854 | ///\todo Create an own exception type. |
---|
855 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
856 | else tmp.lowerBound()=n; |
---|
857 | return tmp; |
---|
858 | } |
---|
859 | ///\e |
---|
860 | |
---|
861 | ///\relates LpSolverBase::Constr |
---|
862 | /// |
---|
863 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, |
---|
864 | const LpSolverBase::Value &n) |
---|
865 | { |
---|
866 | LpSolverBase::Constr tmp(c); |
---|
867 | ///\todo Create an own exception type. |
---|
868 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
869 | else tmp.upperBound()=n; |
---|
870 | return tmp; |
---|
871 | } |
---|
872 | |
---|
873 | ///\e |
---|
874 | |
---|
875 | ///\relates LpSolverBase::Constr |
---|
876 | /// |
---|
877 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, |
---|
878 | const LpSolverBase::Constr&c) |
---|
879 | { |
---|
880 | LpSolverBase::Constr tmp(c); |
---|
881 | ///\todo Create an own exception type. |
---|
882 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
883 | else tmp.upperBound()=n; |
---|
884 | return tmp; |
---|
885 | } |
---|
886 | ///\e |
---|
887 | |
---|
888 | ///\relates LpSolverBase::Constr |
---|
889 | /// |
---|
890 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, |
---|
891 | const LpSolverBase::Value &n) |
---|
892 | { |
---|
893 | LpSolverBase::Constr tmp(c); |
---|
894 | ///\todo Create an own exception type. |
---|
895 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
896 | else tmp.lowerBound()=n; |
---|
897 | return tmp; |
---|
898 | } |
---|
899 | |
---|
900 | |
---|
901 | } //namespace lemon |
---|
902 | |
---|
903 | #endif //LEMON_LP_BASE_H |
---|