... |
... |
@@ -85,2018 +85,2018 @@
|
85 |
85 |
|
86 |
86 |
|
87 |
87 |
///The floating point type used by the solver
|
88 |
88 |
typedef double Value;
|
89 |
89 |
///The infinity constant
|
90 |
90 |
static const Value INF;
|
91 |
91 |
///The not a number constant
|
92 |
92 |
static const Value NaN;
|
93 |
93 |
|
94 |
94 |
friend class Col;
|
95 |
95 |
friend class ColIt;
|
96 |
96 |
friend class Row;
|
97 |
97 |
friend class RowIt;
|
98 |
98 |
|
99 |
99 |
///Refer to a column of the LP.
|
100 |
100 |
|
101 |
101 |
///This type is used to refer to a column of the LP.
|
102 |
102 |
///
|
103 |
103 |
///Its value remains valid and correct even after the addition or erase of
|
104 |
104 |
///other columns.
|
105 |
105 |
///
|
106 |
106 |
///\note This class is similar to other Item types in LEMON, like
|
107 |
107 |
///Node and Arc types in digraph.
|
108 |
108 |
class Col {
|
109 |
109 |
friend class LpBase;
|
110 |
110 |
protected:
|
111 |
111 |
int _id;
|
112 |
112 |
explicit Col(int id) : _id(id) {}
|
113 |
113 |
public:
|
114 |
114 |
typedef Value ExprValue;
|
115 |
115 |
typedef True LpCol;
|
116 |
116 |
/// Default constructor
|
117 |
117 |
|
118 |
118 |
/// \warning The default constructor sets the Col to an
|
119 |
119 |
/// undefined value.
|
120 |
120 |
Col() {}
|
121 |
121 |
/// Invalid constructor \& conversion.
|
122 |
122 |
|
123 |
123 |
/// This constructor initializes the Col to be invalid.
|
124 |
124 |
/// \sa Invalid for more details.
|
125 |
125 |
Col(const Invalid&) : _id(-1) {}
|
126 |
126 |
/// Equality operator
|
127 |
127 |
|
128 |
128 |
/// Two \ref Col "Col"s are equal if and only if they point to
|
129 |
129 |
/// the same LP column or both are invalid.
|
130 |
130 |
bool operator==(Col c) const {return _id == c._id;}
|
131 |
131 |
/// Inequality operator
|
132 |
132 |
|
133 |
133 |
/// \sa operator==(Col c)
|
134 |
134 |
///
|
135 |
135 |
bool operator!=(Col c) const {return _id != c._id;}
|
136 |
136 |
/// Artificial ordering operator.
|
137 |
137 |
|
138 |
138 |
/// To allow the use of this object in std::map or similar
|
139 |
139 |
/// associative container we require this.
|
140 |
140 |
///
|
141 |
141 |
/// \note This operator only have to define some strict ordering of
|
142 |
142 |
/// the items; this order has nothing to do with the iteration
|
143 |
143 |
/// ordering of the items.
|
144 |
144 |
bool operator<(Col c) const {return _id < c._id;}
|
145 |
145 |
};
|
146 |
146 |
|
147 |
147 |
///Iterator for iterate over the columns of an LP problem
|
148 |
148 |
|
149 |
149 |
/// Its usage is quite simple, for example, you can count the number
|
150 |
150 |
/// of columns in an LP \c lp:
|
151 |
151 |
///\code
|
152 |
152 |
/// int count=0;
|
153 |
153 |
/// for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count;
|
154 |
154 |
///\endcode
|
155 |
155 |
class ColIt : public Col {
|
156 |
156 |
const LpBase *_solver;
|
157 |
157 |
public:
|
158 |
158 |
/// Default constructor
|
159 |
159 |
|
160 |
160 |
/// \warning The default constructor sets the iterator
|
161 |
161 |
/// to an undefined value.
|
162 |
162 |
ColIt() {}
|
163 |
163 |
/// Sets the iterator to the first Col
|
164 |
164 |
|
165 |
165 |
/// Sets the iterator to the first Col.
|
166 |
166 |
///
|
167 |
167 |
ColIt(const LpBase &solver) : _solver(&solver)
|
168 |
168 |
{
|
169 |
169 |
_solver->cols.firstItem(_id);
|
170 |
170 |
}
|
171 |
171 |
/// Invalid constructor \& conversion
|
172 |
172 |
|
173 |
173 |
/// Initialize the iterator to be invalid.
|
174 |
174 |
/// \sa Invalid for more details.
|
175 |
175 |
ColIt(const Invalid&) : Col(INVALID) {}
|
176 |
176 |
/// Next column
|
177 |
177 |
|
178 |
178 |
/// Assign the iterator to the next column.
|
179 |
179 |
///
|
180 |
180 |
ColIt &operator++()
|
181 |
181 |
{
|
182 |
182 |
_solver->cols.nextItem(_id);
|
183 |
183 |
return *this;
|
184 |
184 |
}
|
185 |
185 |
};
|
186 |
186 |
|
187 |
187 |
/// \brief Returns the ID of the column.
|
188 |
188 |
static int id(const Col& col) { return col._id; }
|
189 |
189 |
/// \brief Returns the column with the given ID.
|
190 |
190 |
///
|
191 |
191 |
/// \pre The argument should be a valid column ID in the LP problem.
|
192 |
192 |
static Col colFromId(int id) { return Col(id); }
|
193 |
193 |
|
194 |
194 |
///Refer to a row of the LP.
|
195 |
195 |
|
196 |
196 |
///This type is used to refer to a row of the LP.
|
197 |
197 |
///
|
198 |
198 |
///Its value remains valid and correct even after the addition or erase of
|
199 |
199 |
///other rows.
|
200 |
200 |
///
|
201 |
201 |
///\note This class is similar to other Item types in LEMON, like
|
202 |
202 |
///Node and Arc types in digraph.
|
203 |
203 |
class Row {
|
204 |
204 |
friend class LpBase;
|
205 |
205 |
protected:
|
206 |
206 |
int _id;
|
207 |
207 |
explicit Row(int id) : _id(id) {}
|
208 |
208 |
public:
|
209 |
209 |
typedef Value ExprValue;
|
210 |
210 |
typedef True LpRow;
|
211 |
211 |
/// Default constructor
|
212 |
212 |
|
213 |
213 |
/// \warning The default constructor sets the Row to an
|
214 |
214 |
/// undefined value.
|
215 |
215 |
Row() {}
|
216 |
216 |
/// Invalid constructor \& conversion.
|
217 |
217 |
|
218 |
218 |
/// This constructor initializes the Row to be invalid.
|
219 |
219 |
/// \sa Invalid for more details.
|
220 |
220 |
Row(const Invalid&) : _id(-1) {}
|
221 |
221 |
/// Equality operator
|
222 |
222 |
|
223 |
223 |
/// Two \ref Row "Row"s are equal if and only if they point to
|
224 |
224 |
/// the same LP row or both are invalid.
|
225 |
225 |
bool operator==(Row r) const {return _id == r._id;}
|
226 |
226 |
/// Inequality operator
|
227 |
227 |
|
228 |
228 |
/// \sa operator==(Row r)
|
229 |
229 |
///
|
230 |
230 |
bool operator!=(Row r) const {return _id != r._id;}
|
231 |
231 |
/// Artificial ordering operator.
|
232 |
232 |
|
233 |
233 |
/// To allow the use of this object in std::map or similar
|
234 |
234 |
/// associative container we require this.
|
235 |
235 |
///
|
236 |
236 |
/// \note This operator only have to define some strict ordering of
|
237 |
237 |
/// the items; this order has nothing to do with the iteration
|
238 |
238 |
/// ordering of the items.
|
239 |
239 |
bool operator<(Row r) const {return _id < r._id;}
|
240 |
240 |
};
|
241 |
241 |
|
242 |
242 |
///Iterator for iterate over the rows of an LP problem
|
243 |
243 |
|
244 |
244 |
/// Its usage is quite simple, for example, you can count the number
|
245 |
245 |
/// of rows in an LP \c lp:
|
246 |
246 |
///\code
|
247 |
247 |
/// int count=0;
|
248 |
248 |
/// for (LpBase::RowIt c(lp); c!=INVALID; ++c) ++count;
|
249 |
249 |
///\endcode
|
250 |
250 |
class RowIt : public Row {
|
251 |
251 |
const LpBase *_solver;
|
252 |
252 |
public:
|
253 |
253 |
/// Default constructor
|
254 |
254 |
|
255 |
255 |
/// \warning The default constructor sets the iterator
|
256 |
256 |
/// to an undefined value.
|
257 |
257 |
RowIt() {}
|
258 |
258 |
/// Sets the iterator to the first Row
|
259 |
259 |
|
260 |
260 |
/// Sets the iterator to the first Row.
|
261 |
261 |
///
|
262 |
262 |
RowIt(const LpBase &solver) : _solver(&solver)
|
263 |
263 |
{
|
264 |
264 |
_solver->rows.firstItem(_id);
|
265 |
265 |
}
|
266 |
266 |
/// Invalid constructor \& conversion
|
267 |
267 |
|
268 |
268 |
/// Initialize the iterator to be invalid.
|
269 |
269 |
/// \sa Invalid for more details.
|
270 |
270 |
RowIt(const Invalid&) : Row(INVALID) {}
|
271 |
271 |
/// Next row
|
272 |
272 |
|
273 |
273 |
/// Assign the iterator to the next row.
|
274 |
274 |
///
|
275 |
275 |
RowIt &operator++()
|
276 |
276 |
{
|
277 |
277 |
_solver->rows.nextItem(_id);
|
278 |
278 |
return *this;
|
279 |
279 |
}
|
280 |
280 |
};
|
281 |
281 |
|
282 |
282 |
/// \brief Returns the ID of the row.
|
283 |
283 |
static int id(const Row& row) { return row._id; }
|
284 |
284 |
/// \brief Returns the row with the given ID.
|
285 |
285 |
///
|
286 |
286 |
/// \pre The argument should be a valid row ID in the LP problem.
|
287 |
287 |
static Row rowFromId(int id) { return Row(id); }
|
288 |
288 |
|
289 |
289 |
public:
|
290 |
290 |
|
291 |
291 |
///Linear expression of variables and a constant component
|
292 |
292 |
|
293 |
293 |
///This data structure stores a linear expression of the variables
|
294 |
294 |
///(\ref Col "Col"s) and also has a constant component.
|
295 |
295 |
///
|
296 |
296 |
///There are several ways to access and modify the contents of this
|
297 |
297 |
///container.
|
298 |
298 |
///\code
|
299 |
299 |
///e[v]=5;
|
300 |
300 |
///e[v]+=12;
|
301 |
301 |
///e.erase(v);
|
302 |
302 |
///\endcode
|
303 |
303 |
///or you can also iterate through its elements.
|
304 |
304 |
///\code
|
305 |
305 |
///double s=0;
|
306 |
306 |
///for(LpBase::Expr::ConstCoeffIt i(e);i!=INVALID;++i)
|
307 |
307 |
/// s+=*i * primal(i);
|
308 |
308 |
///\endcode
|
309 |
309 |
///(This code computes the primal value of the expression).
|
310 |
310 |
///- Numbers (<tt>double</tt>'s)
|
311 |
311 |
///and variables (\ref Col "Col"s) directly convert to an
|
312 |
312 |
///\ref Expr and the usual linear operations are defined, so
|
313 |
313 |
///\code
|
314 |
314 |
///v+w
|
315 |
315 |
///2*v-3.12*(v-w/2)+2
|
316 |
316 |
///v*2.1+(3*v+(v*12+w+6)*3)/2
|
317 |
317 |
///\endcode
|
318 |
318 |
///are valid expressions.
|
319 |
319 |
///The usual assignment operations are also defined.
|
320 |
320 |
///\code
|
321 |
321 |
///e=v+w;
|
322 |
322 |
///e+=2*v-3.12*(v-w/2)+2;
|
323 |
323 |
///e*=3.4;
|
324 |
324 |
///e/=5;
|
325 |
325 |
///\endcode
|
326 |
326 |
///- The constant member can be set and read by dereference
|
327 |
327 |
/// operator (unary *)
|
328 |
328 |
///
|
329 |
329 |
///\code
|
330 |
330 |
///*e=12;
|
331 |
331 |
///double c=*e;
|
332 |
332 |
///\endcode
|
333 |
333 |
///
|
334 |
334 |
///\sa Constr
|
335 |
335 |
class Expr {
|
336 |
336 |
friend class LpBase;
|
337 |
337 |
public:
|
338 |
338 |
/// The key type of the expression
|
339 |
339 |
typedef LpBase::Col Key;
|
340 |
340 |
/// The value type of the expression
|
341 |
341 |
typedef LpBase::Value Value;
|
342 |
342 |
|
343 |
343 |
protected:
|
344 |
344 |
Value const_comp;
|
345 |
345 |
std::map<int, Value> comps;
|
346 |
346 |
|
347 |
347 |
public:
|
348 |
348 |
typedef True SolverExpr;
|
349 |
349 |
/// Default constructor
|
350 |
350 |
|
351 |
351 |
/// Construct an empty expression, the coefficients and
|
352 |
352 |
/// the constant component are initialized to zero.
|
353 |
353 |
Expr() : const_comp(0) {}
|
354 |
354 |
/// Construct an expression from a column
|
355 |
355 |
|
356 |
356 |
/// Construct an expression, which has a term with \c c variable
|
357 |
357 |
/// and 1.0 coefficient.
|
358 |
358 |
Expr(const Col &c) : const_comp(0) {
|
359 |
359 |
typedef std::map<int, Value>::value_type pair_type;
|
360 |
360 |
comps.insert(pair_type(id(c), 1));
|
361 |
361 |
}
|
362 |
362 |
/// Construct an expression from a constant
|
363 |
363 |
|
364 |
364 |
/// Construct an expression, which's constant component is \c v.
|
365 |
365 |
///
|
366 |
366 |
Expr(const Value &v) : const_comp(v) {}
|
367 |
367 |
/// Returns the coefficient of the column
|
368 |
368 |
Value operator[](const Col& c) const {
|
369 |
369 |
std::map<int, Value>::const_iterator it=comps.find(id(c));
|
370 |
370 |
if (it != comps.end()) {
|
371 |
371 |
return it->second;
|
372 |
372 |
} else {
|
373 |
373 |
return 0;
|
374 |
374 |
}
|
375 |
375 |
}
|
376 |
376 |
/// Returns the coefficient of the column
|
377 |
377 |
Value& operator[](const Col& c) {
|
378 |
378 |
return comps[id(c)];
|
379 |
379 |
}
|
380 |
380 |
/// Sets the coefficient of the column
|
381 |
381 |
void set(const Col &c, const Value &v) {
|
382 |
382 |
if (v != 0.0) {
|
383 |
383 |
typedef std::map<int, Value>::value_type pair_type;
|
384 |
384 |
comps.insert(pair_type(id(c), v));
|
385 |
385 |
} else {
|
386 |
386 |
comps.erase(id(c));
|
387 |
387 |
}
|
388 |
388 |
}
|
389 |
389 |
/// Returns the constant component of the expression
|
390 |
390 |
Value& operator*() { return const_comp; }
|
391 |
391 |
/// Returns the constant component of the expression
|
392 |
392 |
const Value& operator*() const { return const_comp; }
|
393 |
393 |
/// \brief Removes the coefficients which's absolute value does
|
394 |
394 |
/// not exceed \c epsilon. It also sets to zero the constant
|
395 |
395 |
/// component, if it does not exceed epsilon in absolute value.
|
396 |
396 |
void simplify(Value epsilon = 0.0) {
|
397 |
397 |
std::map<int, Value>::iterator it=comps.begin();
|
398 |
398 |
while (it != comps.end()) {
|
399 |
399 |
std::map<int, Value>::iterator jt=it;
|
400 |
400 |
++jt;
|
401 |
401 |
if (std::fabs((*it).second) <= epsilon) comps.erase(it);
|
402 |
402 |
it=jt;
|
403 |
403 |
}
|
404 |
404 |
if (std::fabs(const_comp) <= epsilon) const_comp = 0;
|
405 |
405 |
}
|
406 |
406 |
|
407 |
407 |
void simplify(Value epsilon = 0.0) const {
|
408 |
408 |
const_cast<Expr*>(this)->simplify(epsilon);
|
409 |
409 |
}
|
410 |
410 |
|
411 |
411 |
///Sets all coefficients and the constant component to 0.
|
412 |
412 |
void clear() {
|
413 |
413 |
comps.clear();
|
414 |
414 |
const_comp=0;
|
415 |
415 |
}
|
416 |
416 |
|
417 |
417 |
///Compound assignment
|
418 |
418 |
Expr &operator+=(const Expr &e) {
|
419 |
419 |
for (std::map<int, Value>::const_iterator it=e.comps.begin();
|
420 |
420 |
it!=e.comps.end(); ++it)
|
421 |
421 |
comps[it->first]+=it->second;
|
422 |
422 |
const_comp+=e.const_comp;
|
423 |
423 |
return *this;
|
424 |
424 |
}
|
425 |
425 |
///Compound assignment
|
426 |
426 |
Expr &operator-=(const Expr &e) {
|
427 |
427 |
for (std::map<int, Value>::const_iterator it=e.comps.begin();
|
428 |
428 |
it!=e.comps.end(); ++it)
|
429 |
429 |
comps[it->first]-=it->second;
|
430 |
430 |
const_comp-=e.const_comp;
|
431 |
431 |
return *this;
|
432 |
432 |
}
|
433 |
433 |
///Multiply with a constant
|
434 |
434 |
Expr &operator*=(const Value &v) {
|
435 |
435 |
for (std::map<int, Value>::iterator it=comps.begin();
|
436 |
436 |
it!=comps.end(); ++it)
|
437 |
437 |
it->second*=v;
|
438 |
438 |
const_comp*=v;
|
439 |
439 |
return *this;
|
440 |
440 |
}
|
441 |
441 |
///Division with a constant
|
442 |
442 |
Expr &operator/=(const Value &c) {
|
443 |
443 |
for (std::map<int, Value>::iterator it=comps.begin();
|
444 |
444 |
it!=comps.end(); ++it)
|
445 |
445 |
it->second/=c;
|
446 |
446 |
const_comp/=c;
|
447 |
447 |
return *this;
|
448 |
448 |
}
|
449 |
449 |
|
450 |
450 |
///Iterator over the expression
|
451 |
451 |
|
452 |
452 |
///The iterator iterates over the terms of the expression.
|
453 |
453 |
///
|
454 |
454 |
///\code
|
455 |
455 |
///double s=0;
|
456 |
456 |
///for(LpBase::Expr::CoeffIt i(e);i!=INVALID;++i)
|
457 |
457 |
/// s+= *i * primal(i);
|
458 |
458 |
///\endcode
|
459 |
459 |
class CoeffIt {
|
460 |
460 |
private:
|
461 |
461 |
|
462 |
462 |
std::map<int, Value>::iterator _it, _end;
|
463 |
463 |
|
464 |
464 |
public:
|
465 |
465 |
|
466 |
466 |
/// Sets the iterator to the first term
|
467 |
467 |
|
468 |
468 |
/// Sets the iterator to the first term of the expression.
|
469 |
469 |
///
|
470 |
470 |
CoeffIt(Expr& e)
|
471 |
471 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
472 |
472 |
|
473 |
473 |
/// Convert the iterator to the column of the term
|
474 |
474 |
operator Col() const {
|
475 |
475 |
return colFromId(_it->first);
|
476 |
476 |
}
|
477 |
477 |
|
478 |
478 |
/// Returns the coefficient of the term
|
479 |
479 |
Value& operator*() { return _it->second; }
|
480 |
480 |
|
481 |
481 |
/// Returns the coefficient of the term
|
482 |
482 |
const Value& operator*() const { return _it->second; }
|
483 |
483 |
/// Next term
|
484 |
484 |
|
485 |
485 |
/// Assign the iterator to the next term.
|
486 |
486 |
///
|
487 |
487 |
CoeffIt& operator++() { ++_it; return *this; }
|
488 |
488 |
|
489 |
489 |
/// Equality operator
|
490 |
490 |
bool operator==(Invalid) const { return _it == _end; }
|
491 |
491 |
/// Inequality operator
|
492 |
492 |
bool operator!=(Invalid) const { return _it != _end; }
|
493 |
493 |
};
|
494 |
494 |
|
495 |
495 |
/// Const iterator over the expression
|
496 |
496 |
|
497 |
497 |
///The iterator iterates over the terms of the expression.
|
498 |
498 |
///
|
499 |
499 |
///\code
|
500 |
500 |
///double s=0;
|
501 |
501 |
///for(LpBase::Expr::ConstCoeffIt i(e);i!=INVALID;++i)
|
502 |
502 |
/// s+=*i * primal(i);
|
503 |
503 |
///\endcode
|
504 |
504 |
class ConstCoeffIt {
|
505 |
505 |
private:
|
506 |
506 |
|
507 |
507 |
std::map<int, Value>::const_iterator _it, _end;
|
508 |
508 |
|
509 |
509 |
public:
|
510 |
510 |
|
511 |
511 |
/// Sets the iterator to the first term
|
512 |
512 |
|
513 |
513 |
/// Sets the iterator to the first term of the expression.
|
514 |
514 |
///
|
515 |
515 |
ConstCoeffIt(const Expr& e)
|
516 |
516 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
517 |
517 |
|
518 |
518 |
/// Convert the iterator to the column of the term
|
519 |
519 |
operator Col() const {
|
520 |
520 |
return colFromId(_it->first);
|
521 |
521 |
}
|
522 |
522 |
|
523 |
523 |
/// Returns the coefficient of the term
|
524 |
524 |
const Value& operator*() const { return _it->second; }
|
525 |
525 |
|
526 |
526 |
/// Next term
|
527 |
527 |
|
528 |
528 |
/// Assign the iterator to the next term.
|
529 |
529 |
///
|
530 |
530 |
ConstCoeffIt& operator++() { ++_it; return *this; }
|
531 |
531 |
|
532 |
532 |
/// Equality operator
|
533 |
533 |
bool operator==(Invalid) const { return _it == _end; }
|
534 |
534 |
/// Inequality operator
|
535 |
535 |
bool operator!=(Invalid) const { return _it != _end; }
|
536 |
536 |
};
|
537 |
537 |
|
538 |
538 |
};
|
539 |
539 |
|
540 |
540 |
///Linear constraint
|
541 |
541 |
|
542 |
542 |
///This data stucture represents a linear constraint in the LP.
|
543 |
543 |
///Basically it is a linear expression with a lower or an upper bound
|
544 |
544 |
///(or both). These parts of the constraint can be obtained by the member
|
545 |
545 |
///functions \ref expr(), \ref lowerBound() and \ref upperBound(),
|
546 |
546 |
///respectively.
|
547 |
547 |
///There are two ways to construct a constraint.
|
548 |
548 |
///- You can set the linear expression and the bounds directly
|
549 |
549 |
/// by the functions above.
|
550 |
550 |
///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt>
|
551 |
551 |
/// are defined between expressions, or even between constraints whenever
|
552 |
552 |
/// it makes sense. Therefore if \c e and \c f are linear expressions and
|
553 |
553 |
/// \c s and \c t are numbers, then the followings are valid expressions
|
554 |
554 |
/// and thus they can be used directly e.g. in \ref addRow() whenever
|
555 |
555 |
/// it makes sense.
|
556 |
556 |
///\code
|
557 |
557 |
/// e<=s
|
558 |
558 |
/// e<=f
|
559 |
559 |
/// e==f
|
560 |
560 |
/// s<=e<=t
|
561 |
561 |
/// e>=t
|
562 |
562 |
///\endcode
|
563 |
563 |
///\warning The validity of a constraint is checked only at run
|
564 |
564 |
///time, so e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will
|
565 |
565 |
///compile, but will fail an assertion.
|
566 |
566 |
class Constr
|
567 |
567 |
{
|
568 |
568 |
public:
|
569 |
569 |
typedef LpBase::Expr Expr;
|
570 |
570 |
typedef Expr::Key Key;
|
571 |
571 |
typedef Expr::Value Value;
|
572 |
572 |
|
573 |
573 |
protected:
|
574 |
574 |
Expr _expr;
|
575 |
575 |
Value _lb,_ub;
|
576 |
576 |
public:
|
577 |
577 |
///\e
|
578 |
578 |
Constr() : _expr(), _lb(NaN), _ub(NaN) {}
|
579 |
579 |
///\e
|
580 |
580 |
Constr(Value lb, const Expr &e, Value ub) :
|
581 |
581 |
_expr(e), _lb(lb), _ub(ub) {}
|
582 |
582 |
Constr(const Expr &e) :
|
583 |
583 |
_expr(e), _lb(NaN), _ub(NaN) {}
|
584 |
584 |
///\e
|
585 |
585 |
void clear()
|
586 |
586 |
{
|
587 |
587 |
_expr.clear();
|
588 |
588 |
_lb=_ub=NaN;
|
589 |
589 |
}
|
590 |
590 |
|
591 |
591 |
///Reference to the linear expression
|
592 |
592 |
Expr &expr() { return _expr; }
|
593 |
593 |
///Cont reference to the linear expression
|
594 |
594 |
const Expr &expr() const { return _expr; }
|
595 |
595 |
///Reference to the lower bound.
|
596 |
596 |
|
597 |
597 |
///\return
|
598 |
598 |
///- \ref INF "INF": the constraint is lower unbounded.
|
599 |
599 |
///- \ref NaN "NaN": lower bound has not been set.
|
600 |
600 |
///- finite number: the lower bound
|
601 |
601 |
Value &lowerBound() { return _lb; }
|
602 |
602 |
///The const version of \ref lowerBound()
|
603 |
603 |
const Value &lowerBound() const { return _lb; }
|
604 |
604 |
///Reference to the upper bound.
|
605 |
605 |
|
606 |
606 |
///\return
|
607 |
607 |
///- \ref INF "INF": the constraint is upper unbounded.
|
608 |
608 |
///- \ref NaN "NaN": upper bound has not been set.
|
609 |
609 |
///- finite number: the upper bound
|
610 |
610 |
Value &upperBound() { return _ub; }
|
611 |
611 |
///The const version of \ref upperBound()
|
612 |
612 |
const Value &upperBound() const { return _ub; }
|
613 |
613 |
///Is the constraint lower bounded?
|
614 |
614 |
bool lowerBounded() const {
|
615 |
615 |
return _lb != -INF && !isNaN(_lb);
|
616 |
616 |
}
|
617 |
617 |
///Is the constraint upper bounded?
|
618 |
618 |
bool upperBounded() const {
|
619 |
619 |
return _ub != INF && !isNaN(_ub);
|
620 |
620 |
}
|
621 |
621 |
|
622 |
622 |
};
|
623 |
623 |
|
624 |
624 |
///Linear expression of rows
|
625 |
625 |
|
626 |
626 |
///This data structure represents a column of the matrix,
|
627 |
627 |
///thas is it strores a linear expression of the dual variables
|
628 |
628 |
///(\ref Row "Row"s).
|
629 |
629 |
///
|
630 |
630 |
///There are several ways to access and modify the contents of this
|
631 |
631 |
///container.
|
632 |
632 |
///\code
|
633 |
633 |
///e[v]=5;
|
634 |
634 |
///e[v]+=12;
|
635 |
635 |
///e.erase(v);
|
636 |
636 |
///\endcode
|
637 |
637 |
///or you can also iterate through its elements.
|
638 |
638 |
///\code
|
639 |
639 |
///double s=0;
|
640 |
640 |
///for(LpBase::DualExpr::ConstCoeffIt i(e);i!=INVALID;++i)
|
641 |
641 |
/// s+=*i;
|
642 |
642 |
///\endcode
|
643 |
643 |
///(This code computes the sum of all coefficients).
|
644 |
644 |
///- Numbers (<tt>double</tt>'s)
|
645 |
645 |
///and variables (\ref Row "Row"s) directly convert to an
|
646 |
646 |
///\ref DualExpr and the usual linear operations are defined, so
|
647 |
647 |
///\code
|
648 |
648 |
///v+w
|
649 |
649 |
///2*v-3.12*(v-w/2)
|
650 |
650 |
///v*2.1+(3*v+(v*12+w)*3)/2
|
651 |
651 |
///\endcode
|
652 |
652 |
///are valid \ref DualExpr dual expressions.
|
653 |
653 |
///The usual assignment operations are also defined.
|
654 |
654 |
///\code
|
655 |
655 |
///e=v+w;
|
656 |
656 |
///e+=2*v-3.12*(v-w/2);
|
657 |
657 |
///e*=3.4;
|
658 |
658 |
///e/=5;
|
659 |
659 |
///\endcode
|
660 |
660 |
///
|
661 |
661 |
///\sa Expr
|
662 |
662 |
class DualExpr {
|
663 |
663 |
friend class LpBase;
|
664 |
664 |
public:
|
665 |
665 |
/// The key type of the expression
|
666 |
666 |
typedef LpBase::Row Key;
|
667 |
667 |
/// The value type of the expression
|
668 |
668 |
typedef LpBase::Value Value;
|
669 |
669 |
|
670 |
670 |
protected:
|
671 |
671 |
std::map<int, Value> comps;
|
672 |
672 |
|
673 |
673 |
public:
|
674 |
674 |
typedef True SolverExpr;
|
675 |
675 |
/// Default constructor
|
676 |
676 |
|
677 |
677 |
/// Construct an empty expression, the coefficients are
|
678 |
678 |
/// initialized to zero.
|
679 |
679 |
DualExpr() {}
|
680 |
680 |
/// Construct an expression from a row
|
681 |
681 |
|
682 |
682 |
/// Construct an expression, which has a term with \c r dual
|
683 |
683 |
/// variable and 1.0 coefficient.
|
684 |
684 |
DualExpr(const Row &r) {
|
685 |
685 |
typedef std::map<int, Value>::value_type pair_type;
|
686 |
686 |
comps.insert(pair_type(id(r), 1));
|
687 |
687 |
}
|
688 |
688 |
/// Returns the coefficient of the row
|
689 |
689 |
Value operator[](const Row& r) const {
|
690 |
690 |
std::map<int, Value>::const_iterator it = comps.find(id(r));
|
691 |
691 |
if (it != comps.end()) {
|
692 |
692 |
return it->second;
|
693 |
693 |
} else {
|
694 |
694 |
return 0;
|
695 |
695 |
}
|
696 |
696 |
}
|
697 |
697 |
/// Returns the coefficient of the row
|
698 |
698 |
Value& operator[](const Row& r) {
|
699 |
699 |
return comps[id(r)];
|
700 |
700 |
}
|
701 |
701 |
/// Sets the coefficient of the row
|
702 |
702 |
void set(const Row &r, const Value &v) {
|
703 |
703 |
if (v != 0.0) {
|
704 |
704 |
typedef std::map<int, Value>::value_type pair_type;
|
705 |
705 |
comps.insert(pair_type(id(r), v));
|
706 |
706 |
} else {
|
707 |
707 |
comps.erase(id(r));
|
708 |
708 |
}
|
709 |
709 |
}
|
710 |
710 |
/// \brief Removes the coefficients which's absolute value does
|
711 |
711 |
/// not exceed \c epsilon.
|
712 |
712 |
void simplify(Value epsilon = 0.0) {
|
713 |
713 |
std::map<int, Value>::iterator it=comps.begin();
|
714 |
714 |
while (it != comps.end()) {
|
715 |
715 |
std::map<int, Value>::iterator jt=it;
|
716 |
716 |
++jt;
|
717 |
717 |
if (std::fabs((*it).second) <= epsilon) comps.erase(it);
|
718 |
718 |
it=jt;
|
719 |
719 |
}
|
720 |
720 |
}
|
721 |
721 |
|
722 |
722 |
void simplify(Value epsilon = 0.0) const {
|
723 |
723 |
const_cast<DualExpr*>(this)->simplify(epsilon);
|
724 |
724 |
}
|
725 |
725 |
|
726 |
726 |
///Sets all coefficients to 0.
|
727 |
727 |
void clear() {
|
728 |
728 |
comps.clear();
|
729 |
729 |
}
|
730 |
730 |
///Compound assignment
|
731 |
731 |
DualExpr &operator+=(const DualExpr &e) {
|
732 |
732 |
for (std::map<int, Value>::const_iterator it=e.comps.begin();
|
733 |
733 |
it!=e.comps.end(); ++it)
|
734 |
734 |
comps[it->first]+=it->second;
|
735 |
735 |
return *this;
|
736 |
736 |
}
|
737 |
737 |
///Compound assignment
|
738 |
738 |
DualExpr &operator-=(const DualExpr &e) {
|
739 |
739 |
for (std::map<int, Value>::const_iterator it=e.comps.begin();
|
740 |
740 |
it!=e.comps.end(); ++it)
|
741 |
741 |
comps[it->first]-=it->second;
|
742 |
742 |
return *this;
|
743 |
743 |
}
|
744 |
744 |
///Multiply with a constant
|
745 |
745 |
DualExpr &operator*=(const Value &v) {
|
746 |
746 |
for (std::map<int, Value>::iterator it=comps.begin();
|
747 |
747 |
it!=comps.end(); ++it)
|
748 |
748 |
it->second*=v;
|
749 |
749 |
return *this;
|
750 |
750 |
}
|
751 |
751 |
///Division with a constant
|
752 |
752 |
DualExpr &operator/=(const Value &v) {
|
753 |
753 |
for (std::map<int, Value>::iterator it=comps.begin();
|
754 |
754 |
it!=comps.end(); ++it)
|
755 |
755 |
it->second/=v;
|
756 |
756 |
return *this;
|
757 |
757 |
}
|
758 |
758 |
|
759 |
759 |
///Iterator over the expression
|
760 |
760 |
|
761 |
761 |
///The iterator iterates over the terms of the expression.
|
762 |
762 |
///
|
763 |
763 |
///\code
|
764 |
764 |
///double s=0;
|
765 |
765 |
///for(LpBase::DualExpr::CoeffIt i(e);i!=INVALID;++i)
|
766 |
766 |
/// s+= *i * dual(i);
|
767 |
767 |
///\endcode
|
768 |
768 |
class CoeffIt {
|
769 |
769 |
private:
|
770 |
770 |
|
771 |
771 |
std::map<int, Value>::iterator _it, _end;
|
772 |
772 |
|
773 |
773 |
public:
|
774 |
774 |
|
775 |
775 |
/// Sets the iterator to the first term
|
776 |
776 |
|
777 |
777 |
/// Sets the iterator to the first term of the expression.
|
778 |
778 |
///
|
779 |
779 |
CoeffIt(DualExpr& e)
|
780 |
780 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
781 |
781 |
|
782 |
782 |
/// Convert the iterator to the row of the term
|
783 |
783 |
operator Row() const {
|
784 |
784 |
return rowFromId(_it->first);
|
785 |
785 |
}
|
786 |
786 |
|
787 |
787 |
/// Returns the coefficient of the term
|
788 |
788 |
Value& operator*() { return _it->second; }
|
789 |
789 |
|
790 |
790 |
/// Returns the coefficient of the term
|
791 |
791 |
const Value& operator*() const { return _it->second; }
|
792 |
792 |
|
793 |
793 |
/// Next term
|
794 |
794 |
|
795 |
795 |
/// Assign the iterator to the next term.
|
796 |
796 |
///
|
797 |
797 |
CoeffIt& operator++() { ++_it; return *this; }
|
798 |
798 |
|
799 |
799 |
/// Equality operator
|
800 |
800 |
bool operator==(Invalid) const { return _it == _end; }
|
801 |
801 |
/// Inequality operator
|
802 |
802 |
bool operator!=(Invalid) const { return _it != _end; }
|
803 |
803 |
};
|
804 |
804 |
|
805 |
805 |
///Iterator over the expression
|
806 |
806 |
|
807 |
807 |
///The iterator iterates over the terms of the expression.
|
808 |
808 |
///
|
809 |
809 |
///\code
|
810 |
810 |
///double s=0;
|
811 |
811 |
///for(LpBase::DualExpr::ConstCoeffIt i(e);i!=INVALID;++i)
|
812 |
812 |
/// s+= *i * dual(i);
|
813 |
813 |
///\endcode
|
814 |
814 |
class ConstCoeffIt {
|
815 |
815 |
private:
|
816 |
816 |
|
817 |
817 |
std::map<int, Value>::const_iterator _it, _end;
|
818 |
818 |
|
819 |
819 |
public:
|
820 |
820 |
|
821 |
821 |
/// Sets the iterator to the first term
|
822 |
822 |
|
823 |
823 |
/// Sets the iterator to the first term of the expression.
|
824 |
824 |
///
|
825 |
825 |
ConstCoeffIt(const DualExpr& e)
|
826 |
826 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
827 |
827 |
|
828 |
828 |
/// Convert the iterator to the row of the term
|
829 |
829 |
operator Row() const {
|
830 |
830 |
return rowFromId(_it->first);
|
831 |
831 |
}
|
832 |
832 |
|
833 |
833 |
/// Returns the coefficient of the term
|
834 |
834 |
const Value& operator*() const { return _it->second; }
|
835 |
835 |
|
836 |
836 |
/// Next term
|
837 |
837 |
|
838 |
838 |
/// Assign the iterator to the next term.
|
839 |
839 |
///
|
840 |
840 |
ConstCoeffIt& operator++() { ++_it; return *this; }
|
841 |
841 |
|
842 |
842 |
/// Equality operator
|
843 |
843 |
bool operator==(Invalid) const { return _it == _end; }
|
844 |
844 |
/// Inequality operator
|
845 |
845 |
bool operator!=(Invalid) const { return _it != _end; }
|
846 |
846 |
};
|
847 |
847 |
};
|
848 |
848 |
|
849 |
849 |
|
850 |
850 |
protected:
|
851 |
851 |
|
852 |
852 |
class InsertIterator {
|
853 |
853 |
private:
|
854 |
854 |
|
855 |
855 |
std::map<int, Value>& _host;
|
856 |
856 |
const _solver_bits::VarIndex& _index;
|
857 |
857 |
|
858 |
858 |
public:
|
859 |
859 |
|
860 |
860 |
typedef std::output_iterator_tag iterator_category;
|
861 |
861 |
typedef void difference_type;
|
862 |
862 |
typedef void value_type;
|
863 |
863 |
typedef void reference;
|
864 |
864 |
typedef void pointer;
|
865 |
865 |
|
866 |
866 |
InsertIterator(std::map<int, Value>& host,
|
867 |
867 |
const _solver_bits::VarIndex& index)
|
868 |
868 |
: _host(host), _index(index) {}
|
869 |
869 |
|
870 |
870 |
InsertIterator& operator=(const std::pair<int, Value>& value) {
|
871 |
871 |
typedef std::map<int, Value>::value_type pair_type;
|
872 |
872 |
_host.insert(pair_type(_index[value.first], value.second));
|
873 |
873 |
return *this;
|
874 |
874 |
}
|
875 |
875 |
|
876 |
876 |
InsertIterator& operator*() { return *this; }
|
877 |
877 |
InsertIterator& operator++() { return *this; }
|
878 |
878 |
InsertIterator operator++(int) { return *this; }
|
879 |
879 |
|
880 |
880 |
};
|
881 |
881 |
|
882 |
882 |
class ExprIterator {
|
883 |
883 |
private:
|
884 |
884 |
std::map<int, Value>::const_iterator _host_it;
|
885 |
885 |
const _solver_bits::VarIndex& _index;
|
886 |
886 |
public:
|
887 |
887 |
|
888 |
888 |
typedef std::bidirectional_iterator_tag iterator_category;
|
889 |
889 |
typedef std::ptrdiff_t difference_type;
|
890 |
890 |
typedef const std::pair<int, Value> value_type;
|
891 |
891 |
typedef value_type reference;
|
892 |
892 |
|
893 |
893 |
class pointer {
|
894 |
894 |
public:
|
895 |
895 |
pointer(value_type& _value) : value(_value) {}
|
896 |
896 |
value_type* operator->() { return &value; }
|
897 |
897 |
private:
|
898 |
898 |
value_type value;
|
899 |
899 |
};
|
900 |
900 |
|
901 |
901 |
ExprIterator(const std::map<int, Value>::const_iterator& host_it,
|
902 |
902 |
const _solver_bits::VarIndex& index)
|
903 |
903 |
: _host_it(host_it), _index(index) {}
|
904 |
904 |
|
905 |
905 |
reference operator*() {
|
906 |
906 |
return std::make_pair(_index(_host_it->first), _host_it->second);
|
907 |
907 |
}
|
908 |
908 |
|
909 |
909 |
pointer operator->() {
|
910 |
910 |
return pointer(operator*());
|
911 |
911 |
}
|
912 |
912 |
|
913 |
913 |
ExprIterator& operator++() { ++_host_it; return *this; }
|
914 |
914 |
ExprIterator operator++(int) {
|
915 |
915 |
ExprIterator tmp(*this); ++_host_it; return tmp;
|
916 |
916 |
}
|
917 |
917 |
|
918 |
918 |
ExprIterator& operator--() { --_host_it; return *this; }
|
919 |
919 |
ExprIterator operator--(int) {
|
920 |
920 |
ExprIterator tmp(*this); --_host_it; return tmp;
|
921 |
921 |
}
|
922 |
922 |
|
923 |
923 |
bool operator==(const ExprIterator& it) const {
|
924 |
924 |
return _host_it == it._host_it;
|
925 |
925 |
}
|
926 |
926 |
|
927 |
927 |
bool operator!=(const ExprIterator& it) const {
|
928 |
928 |
return _host_it != it._host_it;
|
929 |
929 |
}
|
930 |
930 |
|
931 |
931 |
};
|
932 |
932 |
|
933 |
933 |
protected:
|
934 |
934 |
|
935 |
935 |
//Abstract virtual functions
|
936 |
936 |
|
937 |
937 |
virtual int _addColId(int col) { return cols.addIndex(col); }
|
938 |
938 |
virtual int _addRowId(int row) { return rows.addIndex(row); }
|
939 |
939 |
|
940 |
940 |
virtual void _eraseColId(int col) { cols.eraseIndex(col); }
|
941 |
941 |
virtual void _eraseRowId(int row) { rows.eraseIndex(row); }
|
942 |
942 |
|
943 |
943 |
virtual int _addCol() = 0;
|
944 |
944 |
virtual int _addRow() = 0;
|
945 |
945 |
|
946 |
946 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u) {
|
947 |
947 |
int row = _addRow();
|
948 |
948 |
_setRowCoeffs(row, b, e);
|
949 |
949 |
_setRowLowerBound(row, l);
|
950 |
950 |
_setRowUpperBound(row, u);
|
951 |
951 |
return row;
|
952 |
952 |
}
|
953 |
953 |
|
954 |
954 |
virtual void _eraseCol(int col) = 0;
|
955 |
955 |
virtual void _eraseRow(int row) = 0;
|
956 |
956 |
|
957 |
957 |
virtual void _getColName(int col, std::string& name) const = 0;
|
958 |
958 |
virtual void _setColName(int col, const std::string& name) = 0;
|
959 |
959 |
virtual int _colByName(const std::string& name) const = 0;
|
960 |
960 |
|
961 |
961 |
virtual void _getRowName(int row, std::string& name) const = 0;
|
962 |
962 |
virtual void _setRowName(int row, const std::string& name) = 0;
|
963 |
963 |
virtual int _rowByName(const std::string& name) const = 0;
|
964 |
964 |
|
965 |
965 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e) = 0;
|
966 |
966 |
virtual void _getRowCoeffs(int i, InsertIterator b) const = 0;
|
967 |
967 |
|
968 |
968 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e) = 0;
|
969 |
969 |
virtual void _getColCoeffs(int i, InsertIterator b) const = 0;
|
970 |
970 |
|
971 |
971 |
virtual void _setCoeff(int row, int col, Value value) = 0;
|
972 |
972 |
virtual Value _getCoeff(int row, int col) const = 0;
|
973 |
973 |
|
974 |
974 |
virtual void _setColLowerBound(int i, Value value) = 0;
|
975 |
975 |
virtual Value _getColLowerBound(int i) const = 0;
|
976 |
976 |
|
977 |
977 |
virtual void _setColUpperBound(int i, Value value) = 0;
|
978 |
978 |
virtual Value _getColUpperBound(int i) const = 0;
|
979 |
979 |
|
980 |
980 |
virtual void _setRowLowerBound(int i, Value value) = 0;
|
981 |
981 |
virtual Value _getRowLowerBound(int i) const = 0;
|
982 |
982 |
|
983 |
983 |
virtual void _setRowUpperBound(int i, Value value) = 0;
|
984 |
984 |
virtual Value _getRowUpperBound(int i) const = 0;
|
985 |
985 |
|
986 |
986 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e) = 0;
|
987 |
987 |
virtual void _getObjCoeffs(InsertIterator b) const = 0;
|
988 |
988 |
|
989 |
989 |
virtual void _setObjCoeff(int i, Value obj_coef) = 0;
|
990 |
990 |
virtual Value _getObjCoeff(int i) const = 0;
|
991 |
991 |
|
992 |
992 |
virtual void _setSense(Sense) = 0;
|
993 |
993 |
virtual Sense _getSense() const = 0;
|
994 |
994 |
|
995 |
995 |
virtual void _clear() = 0;
|
996 |
996 |
|
997 |
997 |
virtual const char* _solverName() const = 0;
|
998 |
998 |
|
999 |
999 |
virtual void _messageLevel(MessageLevel level) = 0;
|
1000 |
1000 |
|
1001 |
1001 |
//Own protected stuff
|
1002 |
1002 |
|
1003 |
1003 |
//Constant component of the objective function
|
1004 |
1004 |
Value obj_const_comp;
|
1005 |
1005 |
|
1006 |
1006 |
LpBase() : rows(), cols(), obj_const_comp(0) {}
|
1007 |
1007 |
|
1008 |
1008 |
public:
|
1009 |
1009 |
|
1010 |
1010 |
/// Virtual destructor
|
1011 |
1011 |
virtual ~LpBase() {}
|
1012 |
1012 |
|
1013 |
1013 |
///Gives back the name of the solver.
|
1014 |
1014 |
const char* solverName() const {return _solverName();}
|
1015 |
1015 |
|
1016 |
1016 |
///\name Build Up and Modify the LP
|
1017 |
1017 |
|
1018 |
1018 |
///@{
|
1019 |
1019 |
|
1020 |
1020 |
///Add a new empty column (i.e a new variable) to the LP
|
1021 |
1021 |
Col addCol() { Col c; c._id = _addColId(_addCol()); return c;}
|
1022 |
1022 |
|
1023 |
1023 |
///\brief Adds several new columns (i.e variables) at once
|
1024 |
1024 |
///
|
1025 |
1025 |
///This magic function takes a container as its argument and fills
|
1026 |
1026 |
///its elements with new columns (i.e. variables)
|
1027 |
1027 |
///\param t can be
|
1028 |
1028 |
///- a standard STL compatible iterable container with
|
1029 |
1029 |
///\ref Col as its \c values_type like
|
1030 |
1030 |
///\code
|
1031 |
1031 |
///std::vector<LpBase::Col>
|
1032 |
1032 |
///std::list<LpBase::Col>
|
1033 |
1033 |
///\endcode
|
1034 |
1034 |
///- a standard STL compatible iterable container with
|
1035 |
1035 |
///\ref Col as its \c mapped_type like
|
1036 |
1036 |
///\code
|
1037 |
1037 |
///std::map<AnyType,LpBase::Col>
|
1038 |
1038 |
///\endcode
|
1039 |
1039 |
///- an iterable lemon \ref concepts::WriteMap "write map" like
|
1040 |
1040 |
///\code
|
1041 |
1041 |
///ListGraph::NodeMap<LpBase::Col>
|
1042 |
1042 |
///ListGraph::ArcMap<LpBase::Col>
|
1043 |
1043 |
///\endcode
|
1044 |
1044 |
///\return The number of the created column.
|
1045 |
1045 |
#ifdef DOXYGEN
|
1046 |
1046 |
template<class T>
|
1047 |
1047 |
int addColSet(T &t) { return 0;}
|
1048 |
1048 |
#else
|
1049 |
1049 |
template<class T>
|
1050 |
1050 |
typename enable_if<typename T::value_type::LpCol,int>::type
|
1051 |
1051 |
addColSet(T &t,dummy<0> = 0) {
|
1052 |
1052 |
int s=0;
|
1053 |
1053 |
for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
|
1054 |
1054 |
return s;
|
1055 |
1055 |
}
|
1056 |
1056 |
template<class T>
|
1057 |
1057 |
typename enable_if<typename T::value_type::second_type::LpCol,
|
1058 |
1058 |
int>::type
|
1059 |
1059 |
addColSet(T &t,dummy<1> = 1) {
|
1060 |
1060 |
int s=0;
|
1061 |
1061 |
for(typename T::iterator i=t.begin();i!=t.end();++i) {
|
1062 |
1062 |
i->second=addCol();
|
1063 |
1063 |
s++;
|
1064 |
1064 |
}
|
1065 |
1065 |
return s;
|
1066 |
1066 |
}
|
1067 |
1067 |
template<class T>
|
1068 |
1068 |
typename enable_if<typename T::MapIt::Value::LpCol,
|
1069 |
1069 |
int>::type
|
1070 |
1070 |
addColSet(T &t,dummy<2> = 2) {
|
1071 |
1071 |
int s=0;
|
1072 |
1072 |
for(typename T::MapIt i(t); i!=INVALID; ++i)
|
1073 |
1073 |
{
|
1074 |
1074 |
i.set(addCol());
|
1075 |
1075 |
s++;
|
1076 |
1076 |
}
|
1077 |
1077 |
return s;
|
1078 |
1078 |
}
|
1079 |
1079 |
#endif
|
1080 |
1080 |
|
1081 |
1081 |
///Set a column (i.e a dual constraint) of the LP
|
1082 |
1082 |
|
1083 |
1083 |
///\param c is the column to be modified
|
1084 |
1084 |
///\param e is a dual linear expression (see \ref DualExpr)
|
1085 |
1085 |
///a better one.
|
1086 |
1086 |
void col(Col c, const DualExpr &e) {
|
1087 |
1087 |
e.simplify();
|
1088 |
1088 |
_setColCoeffs(cols(id(c)), ExprIterator(e.comps.begin(), rows),
|
1089 |
1089 |
ExprIterator(e.comps.end(), rows));
|
1090 |
1090 |
}
|
1091 |
1091 |
|
1092 |
1092 |
///Get a column (i.e a dual constraint) of the LP
|
1093 |
1093 |
|
1094 |
1094 |
///\param c is the column to get
|
1095 |
1095 |
///\return the dual expression associated to the column
|
1096 |
1096 |
DualExpr col(Col c) const {
|
1097 |
1097 |
DualExpr e;
|
1098 |
1098 |
_getColCoeffs(cols(id(c)), InsertIterator(e.comps, rows));
|
1099 |
1099 |
return e;
|
1100 |
1100 |
}
|
1101 |
1101 |
|
1102 |
1102 |
///Add a new column to the LP
|
1103 |
1103 |
|
1104 |
1104 |
///\param e is a dual linear expression (see \ref DualExpr)
|
1105 |
1105 |
///\param o is the corresponding component of the objective
|
1106 |
1106 |
///function. It is 0 by default.
|
1107 |
1107 |
///\return The created column.
|
1108 |
1108 |
Col addCol(const DualExpr &e, Value o = 0) {
|
1109 |
1109 |
Col c=addCol();
|
1110 |
1110 |
col(c,e);
|
1111 |
1111 |
objCoeff(c,o);
|
1112 |
1112 |
return c;
|
1113 |
1113 |
}
|
1114 |
1114 |
|
1115 |
1115 |
///Add a new empty row (i.e a new constraint) to the LP
|
1116 |
1116 |
|
1117 |
1117 |
///This function adds a new empty row (i.e a new constraint) to the LP.
|
1118 |
1118 |
///\return The created row
|
1119 |
1119 |
Row addRow() { Row r; r._id = _addRowId(_addRow()); return r;}
|
1120 |
1120 |
|
1121 |
1121 |
///\brief Add several new rows (i.e constraints) at once
|
1122 |
1122 |
///
|
1123 |
1123 |
///This magic function takes a container as its argument and fills
|
1124 |
1124 |
///its elements with new row (i.e. variables)
|
1125 |
1125 |
///\param t can be
|
1126 |
1126 |
///- a standard STL compatible iterable container with
|
1127 |
1127 |
///\ref Row as its \c values_type like
|
1128 |
1128 |
///\code
|
1129 |
1129 |
///std::vector<LpBase::Row>
|
1130 |
1130 |
///std::list<LpBase::Row>
|
1131 |
1131 |
///\endcode
|
1132 |
1132 |
///- a standard STL compatible iterable container with
|
1133 |
1133 |
///\ref Row as its \c mapped_type like
|
1134 |
1134 |
///\code
|
1135 |
1135 |
///std::map<AnyType,LpBase::Row>
|
1136 |
1136 |
///\endcode
|
1137 |
1137 |
///- an iterable lemon \ref concepts::WriteMap "write map" like
|
1138 |
1138 |
///\code
|
1139 |
1139 |
///ListGraph::NodeMap<LpBase::Row>
|
1140 |
1140 |
///ListGraph::ArcMap<LpBase::Row>
|
1141 |
1141 |
///\endcode
|
1142 |
1142 |
///\return The number of rows created.
|
1143 |
1143 |
#ifdef DOXYGEN
|
1144 |
1144 |
template<class T>
|
1145 |
1145 |
int addRowSet(T &t) { return 0;}
|
1146 |
1146 |
#else
|
1147 |
1147 |
template<class T>
|
1148 |
1148 |
typename enable_if<typename T::value_type::LpRow,int>::type
|
1149 |
1149 |
addRowSet(T &t, dummy<0> = 0) {
|
1150 |
1150 |
int s=0;
|
1151 |
1151 |
for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;}
|
1152 |
1152 |
return s;
|
1153 |
1153 |
}
|
1154 |
1154 |
template<class T>
|
1155 |
1155 |
typename enable_if<typename T::value_type::second_type::LpRow, int>::type
|
1156 |
1156 |
addRowSet(T &t, dummy<1> = 1) {
|
1157 |
1157 |
int s=0;
|
1158 |
1158 |
for(typename T::iterator i=t.begin();i!=t.end();++i) {
|
1159 |
1159 |
i->second=addRow();
|
1160 |
1160 |
s++;
|
1161 |
1161 |
}
|
1162 |
1162 |
return s;
|
1163 |
1163 |
}
|
1164 |
1164 |
template<class T>
|
1165 |
1165 |
typename enable_if<typename T::MapIt::Value::LpRow, int>::type
|
1166 |
1166 |
addRowSet(T &t, dummy<2> = 2) {
|
1167 |
1167 |
int s=0;
|
1168 |
1168 |
for(typename T::MapIt i(t); i!=INVALID; ++i)
|
1169 |
1169 |
{
|
1170 |
1170 |
i.set(addRow());
|
1171 |
1171 |
s++;
|
1172 |
1172 |
}
|
1173 |
1173 |
return s;
|
1174 |
1174 |
}
|
1175 |
1175 |
#endif
|
1176 |
1176 |
|
1177 |
1177 |
///Set a row (i.e a constraint) of the LP
|
1178 |
1178 |
|
1179 |
1179 |
///\param r is the row to be modified
|
1180 |
1180 |
///\param l is lower bound (-\ref INF means no bound)
|
1181 |
1181 |
///\param e is a linear expression (see \ref Expr)
|
1182 |
1182 |
///\param u is the upper bound (\ref INF means no bound)
|
1183 |
1183 |
void row(Row r, Value l, const Expr &e, Value u) {
|
1184 |
1184 |
e.simplify();
|
1185 |
1185 |
_setRowCoeffs(rows(id(r)), ExprIterator(e.comps.begin(), cols),
|
1186 |
1186 |
ExprIterator(e.comps.end(), cols));
|
1187 |
1187 |
_setRowLowerBound(rows(id(r)),l - *e);
|
1188 |
1188 |
_setRowUpperBound(rows(id(r)),u - *e);
|
1189 |
1189 |
}
|
1190 |
1190 |
|
1191 |
1191 |
///Set a row (i.e a constraint) of the LP
|
1192 |
1192 |
|
1193 |
1193 |
///\param r is the row to be modified
|
1194 |
1194 |
///\param c is a linear expression (see \ref Constr)
|
1195 |
1195 |
void row(Row r, const Constr &c) {
|
1196 |
1196 |
row(r, c.lowerBounded()?c.lowerBound():-INF,
|
1197 |
1197 |
c.expr(), c.upperBounded()?c.upperBound():INF);
|
1198 |
1198 |
}
|
1199 |
1199 |
|
1200 |
1200 |
|
1201 |
1201 |
///Get a row (i.e a constraint) of the LP
|
1202 |
1202 |
|
1203 |
1203 |
///\param r is the row to get
|
1204 |
1204 |
///\return the expression associated to the row
|
1205 |
1205 |
Expr row(Row r) const {
|
1206 |
1206 |
Expr e;
|
1207 |
1207 |
_getRowCoeffs(rows(id(r)), InsertIterator(e.comps, cols));
|
1208 |
1208 |
return e;
|
1209 |
1209 |
}
|
1210 |
1210 |
|
1211 |
1211 |
///Add a new row (i.e a new constraint) to the LP
|
1212 |
1212 |
|
1213 |
1213 |
///\param l is the lower bound (-\ref INF means no bound)
|
1214 |
1214 |
///\param e is a linear expression (see \ref Expr)
|
1215 |
1215 |
///\param u is the upper bound (\ref INF means no bound)
|
1216 |
1216 |
///\return The created row.
|
1217 |
1217 |
Row addRow(Value l,const Expr &e, Value u) {
|
1218 |
1218 |
Row r;
|
1219 |
1219 |
e.simplify();
|
1220 |
1220 |
r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols),
|
1221 |
1221 |
ExprIterator(e.comps.end(), cols), u - *e));
|
1222 |
1222 |
return r;
|
1223 |
1223 |
}
|
1224 |
1224 |
|
1225 |
1225 |
///Add a new row (i.e a new constraint) to the LP
|
1226 |
1226 |
|
1227 |
1227 |
///\param c is a linear expression (see \ref Constr)
|
1228 |
1228 |
///\return The created row.
|
1229 |
1229 |
Row addRow(const Constr &c) {
|
1230 |
1230 |
Row r;
|
1231 |
1231 |
c.expr().simplify();
|
1232 |
1232 |
r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound()-*c.expr():-INF,
|
1233 |
1233 |
ExprIterator(c.expr().comps.begin(), cols),
|
1234 |
1234 |
ExprIterator(c.expr().comps.end(), cols),
|
1235 |
1235 |
c.upperBounded()?c.upperBound()-*c.expr():INF));
|
1236 |
1236 |
return r;
|
1237 |
1237 |
}
|
1238 |
1238 |
///Erase a column (i.e a variable) from the LP
|
1239 |
1239 |
|
1240 |
1240 |
///\param c is the column to be deleted
|
1241 |
1241 |
void erase(Col c) {
|
1242 |
1242 |
_eraseCol(cols(id(c)));
|
1243 |
1243 |
_eraseColId(cols(id(c)));
|
1244 |
1244 |
}
|
1245 |
1245 |
///Erase a row (i.e a constraint) from the LP
|
1246 |
1246 |
|
1247 |
1247 |
///\param r is the row to be deleted
|
1248 |
1248 |
void erase(Row r) {
|
1249 |
1249 |
_eraseRow(rows(id(r)));
|
1250 |
1250 |
_eraseRowId(rows(id(r)));
|
1251 |
1251 |
}
|
1252 |
1252 |
|
1253 |
1253 |
/// Get the name of a column
|
1254 |
1254 |
|
1255 |
1255 |
///\param c is the coresponding column
|
1256 |
1256 |
///\return The name of the colunm
|
1257 |
1257 |
std::string colName(Col c) const {
|
1258 |
1258 |
std::string name;
|
1259 |
1259 |
_getColName(cols(id(c)), name);
|
1260 |
1260 |
return name;
|
1261 |
1261 |
}
|
1262 |
1262 |
|
1263 |
1263 |
/// Set the name of a column
|
1264 |
1264 |
|
1265 |
1265 |
///\param c is the coresponding column
|
1266 |
1266 |
///\param name The name to be given
|
1267 |
1267 |
void colName(Col c, const std::string& name) {
|
1268 |
1268 |
_setColName(cols(id(c)), name);
|
1269 |
1269 |
}
|
1270 |
1270 |
|
1271 |
1271 |
/// Get the column by its name
|
1272 |
1272 |
|
1273 |
1273 |
///\param name The name of the column
|
1274 |
1274 |
///\return the proper column or \c INVALID
|
1275 |
1275 |
Col colByName(const std::string& name) const {
|
1276 |
1276 |
int k = _colByName(name);
|
1277 |
1277 |
return k != -1 ? Col(cols[k]) : Col(INVALID);
|
1278 |
1278 |
}
|
1279 |
1279 |
|
1280 |
1280 |
/// Get the name of a row
|
1281 |
1281 |
|
1282 |
1282 |
///\param r is the coresponding row
|
1283 |
1283 |
///\return The name of the row
|
1284 |
1284 |
std::string rowName(Row r) const {
|
1285 |
1285 |
std::string name;
|
1286 |
1286 |
_getRowName(rows(id(r)), name);
|
1287 |
1287 |
return name;
|
1288 |
1288 |
}
|
1289 |
1289 |
|
1290 |
1290 |
/// Set the name of a row
|
1291 |
1291 |
|
1292 |
1292 |
///\param r is the coresponding row
|
1293 |
1293 |
///\param name The name to be given
|
1294 |
1294 |
void rowName(Row r, const std::string& name) {
|
1295 |
1295 |
_setRowName(rows(id(r)), name);
|
1296 |
1296 |
}
|
1297 |
1297 |
|
1298 |
1298 |
/// Get the row by its name
|
1299 |
1299 |
|
1300 |
1300 |
///\param name The name of the row
|
1301 |
1301 |
///\return the proper row or \c INVALID
|
1302 |
1302 |
Row rowByName(const std::string& name) const {
|
1303 |
1303 |
int k = _rowByName(name);
|
1304 |
1304 |
return k != -1 ? Row(rows[k]) : Row(INVALID);
|
1305 |
1305 |
}
|
1306 |
1306 |
|
1307 |
1307 |
/// Set an element of the coefficient matrix of the LP
|
1308 |
1308 |
|
1309 |
1309 |
///\param r is the row of the element to be modified
|
1310 |
1310 |
///\param c is the column of the element to be modified
|
1311 |
1311 |
///\param val is the new value of the coefficient
|
1312 |
1312 |
void coeff(Row r, Col c, Value val) {
|
1313 |
1313 |
_setCoeff(rows(id(r)),cols(id(c)), val);
|
1314 |
1314 |
}
|
1315 |
1315 |
|
1316 |
1316 |
/// Get an element of the coefficient matrix of the LP
|
1317 |
1317 |
|
1318 |
1318 |
///\param r is the row of the element
|
1319 |
1319 |
///\param c is the column of the element
|
1320 |
1320 |
///\return the corresponding coefficient
|
1321 |
1321 |
Value coeff(Row r, Col c) const {
|
1322 |
1322 |
return _getCoeff(rows(id(r)),cols(id(c)));
|
1323 |
1323 |
}
|
1324 |
1324 |
|
1325 |
1325 |
/// Set the lower bound of a column (i.e a variable)
|
1326 |
1326 |
|
1327 |
1327 |
/// The lower bound of a variable (column) has to be given by an
|
1328 |
1328 |
/// extended number of type Value, i.e. a finite number of type
|
1329 |
1329 |
/// Value or -\ref INF.
|
1330 |
1330 |
void colLowerBound(Col c, Value value) {
|
1331 |
1331 |
_setColLowerBound(cols(id(c)),value);
|
1332 |
1332 |
}
|
1333 |
1333 |
|
1334 |
1334 |
/// Get the lower bound of a column (i.e a variable)
|
1335 |
1335 |
|
1336 |
1336 |
/// This function returns the lower bound for column (variable) \c c
|
1337 |
1337 |
/// (this might be -\ref INF as well).
|
1338 |
1338 |
///\return The lower bound for column \c c
|
1339 |
1339 |
Value colLowerBound(Col c) const {
|
1340 |
1340 |
return _getColLowerBound(cols(id(c)));
|
1341 |
1341 |
}
|
1342 |
1342 |
|
1343 |
1343 |
///\brief Set the lower bound of several columns
|
1344 |
1344 |
///(i.e variables) at once
|
1345 |
1345 |
///
|
1346 |
1346 |
///This magic function takes a container as its argument
|
1347 |
1347 |
///and applies the function on all of its elements.
|
1348 |
1348 |
///The lower bound of a variable (column) has to be given by an
|
1349 |
1349 |
///extended number of type Value, i.e. a finite number of type
|
1350 |
1350 |
///Value or -\ref INF.
|
1351 |
1351 |
#ifdef DOXYGEN
|
1352 |
1352 |
template<class T>
|
1353 |
1353 |
void colLowerBound(T &t, Value value) { return 0;}
|
1354 |
1354 |
#else
|
1355 |
1355 |
template<class T>
|
1356 |
1356 |
typename enable_if<typename T::value_type::LpCol,void>::type
|
1357 |
1357 |
colLowerBound(T &t, Value value,dummy<0> = 0) {
|
1358 |
1358 |
for(typename T::iterator i=t.begin();i!=t.end();++i) {
|
1359 |
1359 |
colLowerBound(*i, value);
|
1360 |
1360 |
}
|
1361 |
1361 |
}
|
1362 |
1362 |
template<class T>
|
1363 |
1363 |
typename enable_if<typename T::value_type::second_type::LpCol,
|
1364 |
1364 |
void>::type
|
1365 |
1365 |
colLowerBound(T &t, Value value,dummy<1> = 1) {
|
1366 |
1366 |
for(typename T::iterator i=t.begin();i!=t.end();++i) {
|
1367 |
1367 |
colLowerBound(i->second, value);
|
1368 |
1368 |
}
|
1369 |
1369 |
}
|
1370 |
1370 |
template<class T>
|
1371 |
1371 |
typename enable_if<typename T::MapIt::Value::LpCol,
|
1372 |
1372 |
void>::type
|
1373 |
1373 |
colLowerBound(T &t, Value value,dummy<2> = 2) {
|
1374 |
1374 |
for(typename T::MapIt i(t); i!=INVALID; ++i){
|
1375 |
1375 |
colLowerBound(*i, value);
|
1376 |
1376 |
}
|
1377 |
1377 |
}
|
1378 |
1378 |
#endif
|
1379 |
1379 |
|
1380 |
1380 |
/// Set the upper bound of a column (i.e a variable)
|
1381 |
1381 |
|
1382 |
1382 |
/// The upper bound of a variable (column) has to be given by an
|
1383 |
1383 |
/// extended number of type Value, i.e. a finite number of type
|
1384 |
1384 |
/// Value or \ref INF.
|
1385 |
1385 |
void colUpperBound(Col c, Value value) {
|
1386 |
1386 |
_setColUpperBound(cols(id(c)),value);
|
1387 |
1387 |
};
|
1388 |
1388 |
|
1389 |
1389 |
/// Get the upper bound of a column (i.e a variable)
|
1390 |
1390 |
|
1391 |
1391 |
/// This function returns the upper bound for column (variable) \c c
|
1392 |
1392 |
/// (this might be \ref INF as well).
|
1393 |
1393 |
/// \return The upper bound for column \c c
|
1394 |
1394 |
Value colUpperBound(Col c) const {
|
1395 |
1395 |
return _getColUpperBound(cols(id(c)));
|
1396 |
1396 |
}
|
1397 |
1397 |
|
1398 |
1398 |
///\brief Set the upper bound of several columns
|
1399 |
1399 |
///(i.e variables) at once
|
1400 |
1400 |
///
|
1401 |
1401 |
///This magic function takes a container as its argument
|
1402 |
1402 |
///and applies the function on all of its elements.
|
1403 |
1403 |
///The upper bound of a variable (column) has to be given by an
|
1404 |
1404 |
///extended number of type Value, i.e. a finite number of type
|
1405 |
1405 |
///Value or \ref INF.
|
1406 |
1406 |
#ifdef DOXYGEN
|
1407 |
1407 |
template<class T>
|
1408 |
1408 |
void colUpperBound(T &t, Value value) { return 0;}
|
1409 |
1409 |
#else
|
1410 |
1410 |
template<class T1>
|
1411 |
1411 |
typename enable_if<typename T1::value_type::LpCol,void>::type
|
1412 |
1412 |
colUpperBound(T1 &t, Value value,dummy<0> = 0) {
|
1413 |
1413 |
for(typename T1::iterator i=t.begin();i!=t.end();++i) {
|
1414 |
1414 |
colUpperBound(*i, value);
|
1415 |
1415 |
}
|
1416 |
1416 |
}
|
1417 |
1417 |
template<class T1>
|
1418 |
1418 |
typename enable_if<typename T1::value_type::second_type::LpCol,
|
1419 |
1419 |
void>::type
|
1420 |
1420 |
colUpperBound(T1 &t, Value value,dummy<1> = 1) {
|
1421 |
1421 |
for(typename T1::iterator i=t.begin();i!=t.end();++i) {
|
1422 |
1422 |
colUpperBound(i->second, value);
|
1423 |
1423 |
}
|
1424 |
1424 |
}
|
1425 |
1425 |
template<class T1>
|
1426 |
1426 |
typename enable_if<typename T1::MapIt::Value::LpCol,
|
1427 |
1427 |
void>::type
|
1428 |
1428 |
colUpperBound(T1 &t, Value value,dummy<2> = 2) {
|
1429 |
1429 |
for(typename T1::MapIt i(t); i!=INVALID; ++i){
|
1430 |
1430 |
colUpperBound(*i, value);
|
1431 |
1431 |
}
|
1432 |
1432 |
}
|
1433 |
1433 |
#endif
|
1434 |
1434 |
|
1435 |
1435 |
/// Set the lower and the upper bounds of a column (i.e a variable)
|
1436 |
1436 |
|
1437 |
1437 |
/// The lower and the upper bounds of
|
1438 |
1438 |
/// a variable (column) have to be given by an
|
1439 |
1439 |
/// extended number of type Value, i.e. a finite number of type
|
1440 |
1440 |
/// Value, -\ref INF or \ref INF.
|
1441 |
1441 |
void colBounds(Col c, Value lower, Value upper) {
|
1442 |
1442 |
_setColLowerBound(cols(id(c)),lower);
|
1443 |
1443 |
_setColUpperBound(cols(id(c)),upper);
|
1444 |
1444 |
}
|
1445 |
1445 |
|
1446 |
1446 |
///\brief Set the lower and the upper bound of several columns
|
1447 |
1447 |
///(i.e variables) at once
|
1448 |
1448 |
///
|
1449 |
1449 |
///This magic function takes a container as its argument
|
1450 |
1450 |
///and applies the function on all of its elements.
|
1451 |
1451 |
/// The lower and the upper bounds of
|
1452 |
1452 |
/// a variable (column) have to be given by an
|
1453 |
1453 |
/// extended number of type Value, i.e. a finite number of type
|
1454 |
1454 |
/// Value, -\ref INF or \ref INF.
|
1455 |
1455 |
#ifdef DOXYGEN
|
1456 |
1456 |
template<class T>
|
1457 |
1457 |
void colBounds(T &t, Value lower, Value upper) { return 0;}
|
1458 |
1458 |
#else
|
1459 |
1459 |
template<class T2>
|
1460 |
1460 |
typename enable_if<typename T2::value_type::LpCol,void>::type
|
1461 |
1461 |
colBounds(T2 &t, Value lower, Value upper,dummy<0> = 0) {
|
1462 |
1462 |
for(typename T2::iterator i=t.begin();i!=t.end();++i) {
|
1463 |
1463 |
colBounds(*i, lower, upper);
|
1464 |
1464 |
}
|
1465 |
1465 |
}
|
1466 |
1466 |
template<class T2>
|
1467 |
1467 |
typename enable_if<typename T2::value_type::second_type::LpCol, void>::type
|
1468 |
1468 |
colBounds(T2 &t, Value lower, Value upper,dummy<1> = 1) {
|
1469 |
1469 |
for(typename T2::iterator i=t.begin();i!=t.end();++i) {
|
1470 |
1470 |
colBounds(i->second, lower, upper);
|
1471 |
1471 |
}
|
1472 |
1472 |
}
|
1473 |
1473 |
template<class T2>
|
1474 |
1474 |
typename enable_if<typename T2::MapIt::Value::LpCol, void>::type
|
1475 |
1475 |
colBounds(T2 &t, Value lower, Value upper,dummy<2> = 2) {
|
1476 |
1476 |
for(typename T2::MapIt i(t); i!=INVALID; ++i){
|
1477 |
1477 |
colBounds(*i, lower, upper);
|
1478 |
1478 |
}
|
1479 |
1479 |
}
|
1480 |
1480 |
#endif
|
1481 |
1481 |
|
1482 |
1482 |
/// Set the lower bound of a row (i.e a constraint)
|
1483 |
1483 |
|
1484 |
1484 |
/// The lower bound of a constraint (row) has to be given by an
|
1485 |
1485 |
/// extended number of type Value, i.e. a finite number of type
|
1486 |
1486 |
/// Value or -\ref INF.
|
1487 |
1487 |
void rowLowerBound(Row r, Value value) {
|
1488 |
1488 |
_setRowLowerBound(rows(id(r)),value);
|
1489 |
1489 |
}
|
1490 |
1490 |
|
1491 |
1491 |
/// Get the lower bound of a row (i.e a constraint)
|
1492 |
1492 |
|
1493 |
1493 |
/// This function returns the lower bound for row (constraint) \c c
|
1494 |
1494 |
/// (this might be -\ref INF as well).
|
1495 |
1495 |
///\return The lower bound for row \c r
|
1496 |
1496 |
Value rowLowerBound(Row r) const {
|
1497 |
1497 |
return _getRowLowerBound(rows(id(r)));
|
1498 |
1498 |
}
|
1499 |
1499 |
|
1500 |
1500 |
/// Set the upper bound of a row (i.e a constraint)
|
1501 |
1501 |
|
1502 |
1502 |
/// The upper bound of a constraint (row) has to be given by an
|
1503 |
1503 |
/// extended number of type Value, i.e. a finite number of type
|
1504 |
1504 |
/// Value or -\ref INF.
|
1505 |
1505 |
void rowUpperBound(Row r, Value value) {
|
1506 |
1506 |
_setRowUpperBound(rows(id(r)),value);
|
1507 |
1507 |
}
|
1508 |
1508 |
|
1509 |
1509 |
/// Get the upper bound of a row (i.e a constraint)
|
1510 |
1510 |
|
1511 |
1511 |
/// This function returns the upper bound for row (constraint) \c c
|
1512 |
1512 |
/// (this might be -\ref INF as well).
|
1513 |
1513 |
///\return The upper bound for row \c r
|
1514 |
1514 |
Value rowUpperBound(Row r) const {
|
1515 |
1515 |
return _getRowUpperBound(rows(id(r)));
|
1516 |
1516 |
}
|
1517 |
1517 |
|
1518 |
1518 |
///Set an element of the objective function
|
1519 |
1519 |
void objCoeff(Col c, Value v) {_setObjCoeff(cols(id(c)),v); };
|
1520 |
1520 |
|
1521 |
1521 |
///Get an element of the objective function
|
1522 |
1522 |
Value objCoeff(Col c) const { return _getObjCoeff(cols(id(c))); };
|
1523 |
1523 |
|
1524 |
1524 |
///Set the objective function
|
1525 |
1525 |
|
1526 |
1526 |
///\param e is a linear expression of type \ref Expr.
|
1527 |
1527 |
///
|
1528 |
1528 |
void obj(const Expr& e) {
|
1529 |
1529 |
_setObjCoeffs(ExprIterator(e.comps.begin(), cols),
|
1530 |
1530 |
ExprIterator(e.comps.end(), cols));
|
1531 |
1531 |
obj_const_comp = *e;
|
1532 |
1532 |
}
|
1533 |
1533 |
|
1534 |
1534 |
///Get the objective function
|
1535 |
1535 |
|
1536 |
1536 |
///\return the objective function as a linear expression of type
|
1537 |
1537 |
///Expr.
|
1538 |
1538 |
Expr obj() const {
|
1539 |
1539 |
Expr e;
|
1540 |
1540 |
_getObjCoeffs(InsertIterator(e.comps, cols));
|
1541 |
1541 |
*e = obj_const_comp;
|
1542 |
1542 |
return e;
|
1543 |
1543 |
}
|
1544 |
1544 |
|
1545 |
1545 |
|
1546 |
1546 |
///Set the direction of optimization
|
1547 |
1547 |
void sense(Sense sense) { _setSense(sense); }
|
1548 |
1548 |
|
1549 |
1549 |
///Query the direction of the optimization
|
1550 |
1550 |
Sense sense() const {return _getSense(); }
|
1551 |
1551 |
|
1552 |
1552 |
///Set the sense to maximization
|
1553 |
1553 |
void max() { _setSense(MAX); }
|
1554 |
1554 |
|
1555 |
1555 |
///Set the sense to maximization
|
1556 |
1556 |
void min() { _setSense(MIN); }
|
1557 |
1557 |
|
1558 |
1558 |
///Clears the problem
|
1559 |
1559 |
void clear() { _clear(); }
|
1560 |
1560 |
|
1561 |
1561 |
/// Sets the message level of the solver
|
1562 |
1562 |
void messageLevel(MessageLevel level) { _messageLevel(level); }
|
1563 |
1563 |
|
1564 |
1564 |
///@}
|
1565 |
1565 |
|
1566 |
1566 |
};
|
1567 |
1567 |
|
1568 |
1568 |
/// Addition
|
1569 |
1569 |
|
1570 |
1570 |
///\relates LpBase::Expr
|
1571 |
1571 |
///
|
1572 |
1572 |
inline LpBase::Expr operator+(const LpBase::Expr &a, const LpBase::Expr &b) {
|
1573 |
1573 |
LpBase::Expr tmp(a);
|
1574 |
1574 |
tmp+=b;
|
1575 |
1575 |
return tmp;
|
1576 |
1576 |
}
|
1577 |
1577 |
///Substraction
|
1578 |
1578 |
|
1579 |
1579 |
///\relates LpBase::Expr
|
1580 |
1580 |
///
|
1581 |
1581 |
inline LpBase::Expr operator-(const LpBase::Expr &a, const LpBase::Expr &b) {
|
1582 |
1582 |
LpBase::Expr tmp(a);
|
1583 |
1583 |
tmp-=b;
|
1584 |
1584 |
return tmp;
|
1585 |
1585 |
}
|
1586 |
1586 |
///Multiply with constant
|
1587 |
1587 |
|
1588 |
1588 |
///\relates LpBase::Expr
|
1589 |
1589 |
///
|
1590 |
1590 |
inline LpBase::Expr operator*(const LpBase::Expr &a, const LpBase::Value &b) {
|
1591 |
1591 |
LpBase::Expr tmp(a);
|
1592 |
1592 |
tmp*=b;
|
1593 |
1593 |
return tmp;
|
1594 |
1594 |
}
|
1595 |
1595 |
|
1596 |
1596 |
///Multiply with constant
|
1597 |
1597 |
|
1598 |
1598 |
///\relates LpBase::Expr
|
1599 |
1599 |
///
|
1600 |
1600 |
inline LpBase::Expr operator*(const LpBase::Value &a, const LpBase::Expr &b) {
|
1601 |
1601 |
LpBase::Expr tmp(b);
|
1602 |
1602 |
tmp*=a;
|
1603 |
1603 |
return tmp;
|
1604 |
1604 |
}
|
1605 |
1605 |
///Divide with constant
|
1606 |
1606 |
|
1607 |
1607 |
///\relates LpBase::Expr
|
1608 |
1608 |
///
|
1609 |
1609 |
inline LpBase::Expr operator/(const LpBase::Expr &a, const LpBase::Value &b) {
|
1610 |
1610 |
LpBase::Expr tmp(a);
|
1611 |
1611 |
tmp/=b;
|
1612 |
1612 |
return tmp;
|
1613 |
1613 |
}
|
1614 |
1614 |
|
1615 |
1615 |
///Create constraint
|
1616 |
1616 |
|
1617 |
1617 |
///\relates LpBase::Constr
|
1618 |
1618 |
///
|
1619 |
1619 |
inline LpBase::Constr operator<=(const LpBase::Expr &e,
|
1620 |
1620 |
const LpBase::Expr &f) {
|
1621 |
|
return LpBase::Constr(0, f - e, LpBase::INF);
|
|
1621 |
return LpBase::Constr(0, f - e, LpBase::NaN);
|
1622 |
1622 |
}
|
1623 |
1623 |
|
1624 |
1624 |
///Create constraint
|
1625 |
1625 |
|
1626 |
1626 |
///\relates LpBase::Constr
|
1627 |
1627 |
///
|
1628 |
1628 |
inline LpBase::Constr operator<=(const LpBase::Value &e,
|
1629 |
1629 |
const LpBase::Expr &f) {
|
1630 |
1630 |
return LpBase::Constr(e, f, LpBase::NaN);
|
1631 |
1631 |
}
|
1632 |
1632 |
|
1633 |
1633 |
///Create constraint
|
1634 |
1634 |
|
1635 |
1635 |
///\relates LpBase::Constr
|
1636 |
1636 |
///
|
1637 |
1637 |
inline LpBase::Constr operator<=(const LpBase::Expr &e,
|
1638 |
1638 |
const LpBase::Value &f) {
|
1639 |
|
return LpBase::Constr(- LpBase::INF, e, f);
|
|
1639 |
return LpBase::Constr(LpBase::NaN, e, f);
|
1640 |
1640 |
}
|
1641 |
1641 |
|
1642 |
1642 |
///Create constraint
|
1643 |
1643 |
|
1644 |
1644 |
///\relates LpBase::Constr
|
1645 |
1645 |
///
|
1646 |
1646 |
inline LpBase::Constr operator>=(const LpBase::Expr &e,
|
1647 |
1647 |
const LpBase::Expr &f) {
|
1648 |
|
return LpBase::Constr(0, e - f, LpBase::INF);
|
|
1648 |
return LpBase::Constr(0, e - f, LpBase::NaN);
|
1649 |
1649 |
}
|
1650 |
1650 |
|
1651 |
1651 |
|
1652 |
1652 |
///Create constraint
|
1653 |
1653 |
|
1654 |
1654 |
///\relates LpBase::Constr
|
1655 |
1655 |
///
|
1656 |
1656 |
inline LpBase::Constr operator>=(const LpBase::Value &e,
|
1657 |
1657 |
const LpBase::Expr &f) {
|
1658 |
1658 |
return LpBase::Constr(LpBase::NaN, f, e);
|
1659 |
1659 |
}
|
1660 |
1660 |
|
1661 |
1661 |
|
1662 |
1662 |
///Create constraint
|
1663 |
1663 |
|
1664 |
1664 |
///\relates LpBase::Constr
|
1665 |
1665 |
///
|
1666 |
1666 |
inline LpBase::Constr operator>=(const LpBase::Expr &e,
|
1667 |
1667 |
const LpBase::Value &f) {
|
1668 |
|
return LpBase::Constr(f, e, LpBase::INF);
|
|
1668 |
return LpBase::Constr(f, e, LpBase::NaN);
|
1669 |
1669 |
}
|
1670 |
1670 |
|
1671 |
1671 |
///Create constraint
|
1672 |
1672 |
|
1673 |
1673 |
///\relates LpBase::Constr
|
1674 |
1674 |
///
|
1675 |
1675 |
inline LpBase::Constr operator==(const LpBase::Expr &e,
|
1676 |
1676 |
const LpBase::Value &f) {
|
1677 |
1677 |
return LpBase::Constr(f, e, f);
|
1678 |
1678 |
}
|
1679 |
1679 |
|
1680 |
1680 |
///Create constraint
|
1681 |
1681 |
|
1682 |
1682 |
///\relates LpBase::Constr
|
1683 |
1683 |
///
|
1684 |
1684 |
inline LpBase::Constr operator==(const LpBase::Expr &e,
|
1685 |
1685 |
const LpBase::Expr &f) {
|
1686 |
1686 |
return LpBase::Constr(0, f - e, 0);
|
1687 |
1687 |
}
|
1688 |
1688 |
|
1689 |
1689 |
///Create constraint
|
1690 |
1690 |
|
1691 |
1691 |
///\relates LpBase::Constr
|
1692 |
1692 |
///
|
1693 |
1693 |
inline LpBase::Constr operator<=(const LpBase::Value &n,
|
1694 |
1694 |
const LpBase::Constr &c) {
|
1695 |
1695 |
LpBase::Constr tmp(c);
|
1696 |
1696 |
LEMON_ASSERT(isNaN(tmp.lowerBound()), "Wrong LP constraint");
|
1697 |
1697 |
tmp.lowerBound()=n;
|
1698 |
1698 |
return tmp;
|
1699 |
1699 |
}
|
1700 |
1700 |
///Create constraint
|
1701 |
1701 |
|
1702 |
1702 |
///\relates LpBase::Constr
|
1703 |
1703 |
///
|
1704 |
1704 |
inline LpBase::Constr operator<=(const LpBase::Constr &c,
|
1705 |
1705 |
const LpBase::Value &n)
|
1706 |
1706 |
{
|
1707 |
1707 |
LpBase::Constr tmp(c);
|
1708 |
1708 |
LEMON_ASSERT(isNaN(tmp.upperBound()), "Wrong LP constraint");
|
1709 |
1709 |
tmp.upperBound()=n;
|
1710 |
1710 |
return tmp;
|
1711 |
1711 |
}
|
1712 |
1712 |
|
1713 |
1713 |
///Create constraint
|
1714 |
1714 |
|
1715 |
1715 |
///\relates LpBase::Constr
|
1716 |
1716 |
///
|
1717 |
1717 |
inline LpBase::Constr operator>=(const LpBase::Value &n,
|
1718 |
1718 |
const LpBase::Constr &c) {
|
1719 |
1719 |
LpBase::Constr tmp(c);
|
1720 |
1720 |
LEMON_ASSERT(isNaN(tmp.upperBound()), "Wrong LP constraint");
|
1721 |
1721 |
tmp.upperBound()=n;
|
1722 |
1722 |
return tmp;
|
1723 |
1723 |
}
|
1724 |
1724 |
///Create constraint
|
1725 |
1725 |
|
1726 |
1726 |
///\relates LpBase::Constr
|
1727 |
1727 |
///
|
1728 |
1728 |
inline LpBase::Constr operator>=(const LpBase::Constr &c,
|
1729 |
1729 |
const LpBase::Value &n)
|
1730 |
1730 |
{
|
1731 |
1731 |
LpBase::Constr tmp(c);
|
1732 |
1732 |
LEMON_ASSERT(isNaN(tmp.lowerBound()), "Wrong LP constraint");
|
1733 |
1733 |
tmp.lowerBound()=n;
|
1734 |
1734 |
return tmp;
|
1735 |
1735 |
}
|
1736 |
1736 |
|
1737 |
1737 |
///Addition
|
1738 |
1738 |
|
1739 |
1739 |
///\relates LpBase::DualExpr
|
1740 |
1740 |
///
|
1741 |
1741 |
inline LpBase::DualExpr operator+(const LpBase::DualExpr &a,
|
1742 |
1742 |
const LpBase::DualExpr &b) {
|
1743 |
1743 |
LpBase::DualExpr tmp(a);
|
1744 |
1744 |
tmp+=b;
|
1745 |
1745 |
return tmp;
|
1746 |
1746 |
}
|
1747 |
1747 |
///Substraction
|
1748 |
1748 |
|
1749 |
1749 |
///\relates LpBase::DualExpr
|
1750 |
1750 |
///
|
1751 |
1751 |
inline LpBase::DualExpr operator-(const LpBase::DualExpr &a,
|
1752 |
1752 |
const LpBase::DualExpr &b) {
|
1753 |
1753 |
LpBase::DualExpr tmp(a);
|
1754 |
1754 |
tmp-=b;
|
1755 |
1755 |
return tmp;
|
1756 |
1756 |
}
|
1757 |
1757 |
///Multiply with constant
|
1758 |
1758 |
|
1759 |
1759 |
///\relates LpBase::DualExpr
|
1760 |
1760 |
///
|
1761 |
1761 |
inline LpBase::DualExpr operator*(const LpBase::DualExpr &a,
|
1762 |
1762 |
const LpBase::Value &b) {
|
1763 |
1763 |
LpBase::DualExpr tmp(a);
|
1764 |
1764 |
tmp*=b;
|
1765 |
1765 |
return tmp;
|
1766 |
1766 |
}
|
1767 |
1767 |
|
1768 |
1768 |
///Multiply with constant
|
1769 |
1769 |
|
1770 |
1770 |
///\relates LpBase::DualExpr
|
1771 |
1771 |
///
|
1772 |
1772 |
inline LpBase::DualExpr operator*(const LpBase::Value &a,
|
1773 |
1773 |
const LpBase::DualExpr &b) {
|
1774 |
1774 |
LpBase::DualExpr tmp(b);
|
1775 |
1775 |
tmp*=a;
|
1776 |
1776 |
return tmp;
|
1777 |
1777 |
}
|
1778 |
1778 |
///Divide with constant
|
1779 |
1779 |
|
1780 |
1780 |
///\relates LpBase::DualExpr
|
1781 |
1781 |
///
|
1782 |
1782 |
inline LpBase::DualExpr operator/(const LpBase::DualExpr &a,
|
1783 |
1783 |
const LpBase::Value &b) {
|
1784 |
1784 |
LpBase::DualExpr tmp(a);
|
1785 |
1785 |
tmp/=b;
|
1786 |
1786 |
return tmp;
|
1787 |
1787 |
}
|
1788 |
1788 |
|
1789 |
1789 |
/// \ingroup lp_group
|
1790 |
1790 |
///
|
1791 |
1791 |
/// \brief Common base class for LP solvers
|
1792 |
1792 |
///
|
1793 |
1793 |
/// This class is an abstract base class for LP solvers. This class
|
1794 |
1794 |
/// provides a full interface for set and modify an LP problem,
|
1795 |
1795 |
/// solve it and retrieve the solution. You can use one of the
|
1796 |
1796 |
/// descendants as a concrete implementation, or the \c Lp
|
1797 |
1797 |
/// default LP solver. However, if you would like to handle LP
|
1798 |
1798 |
/// solvers as reference or pointer in a generic way, you can use
|
1799 |
1799 |
/// this class directly.
|
1800 |
1800 |
class LpSolver : virtual public LpBase {
|
1801 |
1801 |
public:
|
1802 |
1802 |
|
1803 |
1803 |
/// The problem types for primal and dual problems
|
1804 |
1804 |
enum ProblemType {
|
1805 |
1805 |
/// = 0. Feasible solution hasn't been found (but may exist).
|
1806 |
1806 |
UNDEFINED = 0,
|
1807 |
1807 |
/// = 1. The problem has no feasible solution.
|
1808 |
1808 |
INFEASIBLE = 1,
|
1809 |
1809 |
/// = 2. Feasible solution found.
|
1810 |
1810 |
FEASIBLE = 2,
|
1811 |
1811 |
/// = 3. Optimal solution exists and found.
|
1812 |
1812 |
OPTIMAL = 3,
|
1813 |
1813 |
/// = 4. The cost function is unbounded.
|
1814 |
1814 |
UNBOUNDED = 4
|
1815 |
1815 |
};
|
1816 |
1816 |
|
1817 |
1817 |
///The basis status of variables
|
1818 |
1818 |
enum VarStatus {
|
1819 |
1819 |
/// The variable is in the basis
|
1820 |
1820 |
BASIC,
|
1821 |
1821 |
/// The variable is free, but not basic
|
1822 |
1822 |
FREE,
|
1823 |
1823 |
/// The variable has active lower bound
|
1824 |
1824 |
LOWER,
|
1825 |
1825 |
/// The variable has active upper bound
|
1826 |
1826 |
UPPER,
|
1827 |
1827 |
/// The variable is non-basic and fixed
|
1828 |
1828 |
FIXED
|
1829 |
1829 |
};
|
1830 |
1830 |
|
1831 |
1831 |
protected:
|
1832 |
1832 |
|
1833 |
1833 |
virtual SolveExitStatus _solve() = 0;
|
1834 |
1834 |
|
1835 |
1835 |
virtual Value _getPrimal(int i) const = 0;
|
1836 |
1836 |
virtual Value _getDual(int i) const = 0;
|
1837 |
1837 |
|
1838 |
1838 |
virtual Value _getPrimalRay(int i) const = 0;
|
1839 |
1839 |
virtual Value _getDualRay(int i) const = 0;
|
1840 |
1840 |
|
1841 |
1841 |
virtual Value _getPrimalValue() const = 0;
|
1842 |
1842 |
|
1843 |
1843 |
virtual VarStatus _getColStatus(int i) const = 0;
|
1844 |
1844 |
virtual VarStatus _getRowStatus(int i) const = 0;
|
1845 |
1845 |
|
1846 |
1846 |
virtual ProblemType _getPrimalType() const = 0;
|
1847 |
1847 |
virtual ProblemType _getDualType() const = 0;
|
1848 |
1848 |
|
1849 |
1849 |
public:
|
1850 |
1850 |
|
1851 |
1851 |
///Allocate a new LP problem instance
|
1852 |
1852 |
virtual LpSolver* newSolver() const = 0;
|
1853 |
1853 |
///Make a copy of the LP problem
|
1854 |
1854 |
virtual LpSolver* cloneSolver() const = 0;
|
1855 |
1855 |
|
1856 |
1856 |
///\name Solve the LP
|
1857 |
1857 |
|
1858 |
1858 |
///@{
|
1859 |
1859 |
|
1860 |
1860 |
///\e Solve the LP problem at hand
|
1861 |
1861 |
///
|
1862 |
1862 |
///\return The result of the optimization procedure. Possible
|
1863 |
1863 |
///values and their meanings can be found in the documentation of
|
1864 |
1864 |
///\ref SolveExitStatus.
|
1865 |
1865 |
SolveExitStatus solve() { return _solve(); }
|
1866 |
1866 |
|
1867 |
1867 |
///@}
|
1868 |
1868 |
|
1869 |
1869 |
///\name Obtain the Solution
|
1870 |
1870 |
|
1871 |
1871 |
///@{
|
1872 |
1872 |
|
1873 |
1873 |
/// The type of the primal problem
|
1874 |
1874 |
ProblemType primalType() const {
|
1875 |
1875 |
return _getPrimalType();
|
1876 |
1876 |
}
|
1877 |
1877 |
|
1878 |
1878 |
/// The type of the dual problem
|
1879 |
1879 |
ProblemType dualType() const {
|
1880 |
1880 |
return _getDualType();
|
1881 |
1881 |
}
|
1882 |
1882 |
|
1883 |
1883 |
/// Return the primal value of the column
|
1884 |
1884 |
|
1885 |
1885 |
/// Return the primal value of the column.
|
1886 |
1886 |
/// \pre The problem is solved.
|
1887 |
1887 |
Value primal(Col c) const { return _getPrimal(cols(id(c))); }
|
1888 |
1888 |
|
1889 |
1889 |
/// Return the primal value of the expression
|
1890 |
1890 |
|
1891 |
1891 |
/// Return the primal value of the expression, i.e. the dot
|
1892 |
1892 |
/// product of the primal solution and the expression.
|
1893 |
1893 |
/// \pre The problem is solved.
|
1894 |
1894 |
Value primal(const Expr& e) const {
|
1895 |
1895 |
double res = *e;
|
1896 |
1896 |
for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
|
1897 |
1897 |
res += *c * primal(c);
|
1898 |
1898 |
}
|
1899 |
1899 |
return res;
|
1900 |
1900 |
}
|
1901 |
1901 |
/// Returns a component of the primal ray
|
1902 |
1902 |
|
1903 |
1903 |
/// The primal ray is solution of the modified primal problem,
|
1904 |
1904 |
/// where we change each finite bound to 0, and we looking for a
|
1905 |
1905 |
/// negative objective value in case of minimization, and positive
|
1906 |
1906 |
/// objective value for maximization. If there is such solution,
|
1907 |
1907 |
/// that proofs the unsolvability of the dual problem, and if a
|
1908 |
1908 |
/// feasible primal solution exists, then the unboundness of
|
1909 |
1909 |
/// primal problem.
|
1910 |
1910 |
///
|
1911 |
1911 |
/// \pre The problem is solved and the dual problem is infeasible.
|
1912 |
1912 |
/// \note Some solvers does not provide primal ray calculation
|
1913 |
1913 |
/// functions.
|
1914 |
1914 |
Value primalRay(Col c) const { return _getPrimalRay(cols(id(c))); }
|
1915 |
1915 |
|
1916 |
1916 |
/// Return the dual value of the row
|
1917 |
1917 |
|
1918 |
1918 |
/// Return the dual value of the row.
|
1919 |
1919 |
/// \pre The problem is solved.
|
1920 |
1920 |
Value dual(Row r) const { return _getDual(rows(id(r))); }
|
1921 |
1921 |
|
1922 |
1922 |
/// Return the dual value of the dual expression
|
1923 |
1923 |
|
1924 |
1924 |
/// Return the dual value of the dual expression, i.e. the dot
|
1925 |
1925 |
/// product of the dual solution and the dual expression.
|
1926 |
1926 |
/// \pre The problem is solved.
|
1927 |
1927 |
Value dual(const DualExpr& e) const {
|
1928 |
1928 |
double res = 0.0;
|
1929 |
1929 |
for (DualExpr::ConstCoeffIt r(e); r != INVALID; ++r) {
|
1930 |
1930 |
res += *r * dual(r);
|
1931 |
1931 |
}
|
1932 |
1932 |
return res;
|
1933 |
1933 |
}
|
1934 |
1934 |
|
1935 |
1935 |
/// Returns a component of the dual ray
|
1936 |
1936 |
|
1937 |
1937 |
/// The dual ray is solution of the modified primal problem, where
|
1938 |
1938 |
/// we change each finite bound to 0 (i.e. the objective function
|
1939 |
1939 |
/// coefficients in the primal problem), and we looking for a
|
1940 |
1940 |
/// ositive objective value. If there is such solution, that
|
1941 |
1941 |
/// proofs the unsolvability of the primal problem, and if a
|
1942 |
1942 |
/// feasible dual solution exists, then the unboundness of
|
1943 |
1943 |
/// dual problem.
|
1944 |
1944 |
///
|
1945 |
1945 |
/// \pre The problem is solved and the primal problem is infeasible.
|
1946 |
1946 |
/// \note Some solvers does not provide dual ray calculation
|
1947 |
1947 |
/// functions.
|
1948 |
1948 |
Value dualRay(Row r) const { return _getDualRay(rows(id(r))); }
|
1949 |
1949 |
|
1950 |
1950 |
/// Return the basis status of the column
|
1951 |
1951 |
|
1952 |
1952 |
/// \see VarStatus
|
1953 |
1953 |
VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
|
1954 |
1954 |
|
1955 |
1955 |
/// Return the basis status of the row
|
1956 |
1956 |
|
1957 |
1957 |
/// \see VarStatus
|
1958 |
1958 |
VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
|
1959 |
1959 |
|
1960 |
1960 |
///The value of the objective function
|
1961 |
1961 |
|
1962 |
1962 |
///\return
|
1963 |
1963 |
///- \ref INF or -\ref INF means either infeasibility or unboundedness
|
1964 |
1964 |
/// of the primal problem, depending on whether we minimize or maximize.
|
1965 |
1965 |
///- \ref NaN if no primal solution is found.
|
1966 |
1966 |
///- The (finite) objective value if an optimal solution is found.
|
1967 |
1967 |
Value primal() const { return _getPrimalValue()+obj_const_comp;}
|
1968 |
1968 |
///@}
|
1969 |
1969 |
|
1970 |
1970 |
protected:
|
1971 |
1971 |
|
1972 |
1972 |
};
|
1973 |
1973 |
|
1974 |
1974 |
|
1975 |
1975 |
/// \ingroup lp_group
|
1976 |
1976 |
///
|
1977 |
1977 |
/// \brief Common base class for MIP solvers
|
1978 |
1978 |
///
|
1979 |
1979 |
/// This class is an abstract base class for MIP solvers. This class
|
1980 |
1980 |
/// provides a full interface for set and modify an MIP problem,
|
1981 |
1981 |
/// solve it and retrieve the solution. You can use one of the
|
1982 |
1982 |
/// descendants as a concrete implementation, or the \c Lp
|
1983 |
1983 |
/// default MIP solver. However, if you would like to handle MIP
|
1984 |
1984 |
/// solvers as reference or pointer in a generic way, you can use
|
1985 |
1985 |
/// this class directly.
|
1986 |
1986 |
class MipSolver : virtual public LpBase {
|
1987 |
1987 |
public:
|
1988 |
1988 |
|
1989 |
1989 |
/// The problem types for MIP problems
|
1990 |
1990 |
enum ProblemType {
|
1991 |
1991 |
/// = 0. Feasible solution hasn't been found (but may exist).
|
1992 |
1992 |
UNDEFINED = 0,
|
1993 |
1993 |
/// = 1. The problem has no feasible solution.
|
1994 |
1994 |
INFEASIBLE = 1,
|
1995 |
1995 |
/// = 2. Feasible solution found.
|
1996 |
1996 |
FEASIBLE = 2,
|
1997 |
1997 |
/// = 3. Optimal solution exists and found.
|
1998 |
1998 |
OPTIMAL = 3,
|
1999 |
1999 |
/// = 4. The cost function is unbounded.
|
2000 |
2000 |
///The Mip or at least the relaxed problem is unbounded.
|
2001 |
2001 |
UNBOUNDED = 4
|
2002 |
2002 |
};
|
2003 |
2003 |
|
2004 |
2004 |
///Allocate a new MIP problem instance
|
2005 |
2005 |
virtual MipSolver* newSolver() const = 0;
|
2006 |
2006 |
///Make a copy of the MIP problem
|
2007 |
2007 |
virtual MipSolver* cloneSolver() const = 0;
|
2008 |
2008 |
|
2009 |
2009 |
///\name Solve the MIP
|
2010 |
2010 |
|
2011 |
2011 |
///@{
|
2012 |
2012 |
|
2013 |
2013 |
/// Solve the MIP problem at hand
|
2014 |
2014 |
///
|
2015 |
2015 |
///\return The result of the optimization procedure. Possible
|
2016 |
2016 |
///values and their meanings can be found in the documentation of
|
2017 |
2017 |
///\ref SolveExitStatus.
|
2018 |
2018 |
SolveExitStatus solve() { return _solve(); }
|
2019 |
2019 |
|
2020 |
2020 |
///@}
|
2021 |
2021 |
|
2022 |
2022 |
///\name Set Column Type
|
2023 |
2023 |
///@{
|
2024 |
2024 |
|
2025 |
2025 |
///Possible variable (column) types (e.g. real, integer, binary etc.)
|
2026 |
2026 |
enum ColTypes {
|
2027 |
2027 |
/// = 0. Continuous variable (default).
|
2028 |
2028 |
REAL = 0,
|
2029 |
2029 |
/// = 1. Integer variable.
|
2030 |
2030 |
INTEGER = 1
|
2031 |
2031 |
};
|
2032 |
2032 |
|
2033 |
2033 |
///Sets the type of the given column to the given type
|
2034 |
2034 |
|
2035 |
2035 |
///Sets the type of the given column to the given type.
|
2036 |
2036 |
///
|
2037 |
2037 |
void colType(Col c, ColTypes col_type) {
|
2038 |
2038 |
_setColType(cols(id(c)),col_type);
|
2039 |
2039 |
}
|
2040 |
2040 |
|
2041 |
2041 |
///Gives back the type of the column.
|
2042 |
2042 |
|
2043 |
2043 |
///Gives back the type of the column.
|
2044 |
2044 |
///
|
2045 |
2045 |
ColTypes colType(Col c) const {
|
2046 |
2046 |
return _getColType(cols(id(c)));
|
2047 |
2047 |
}
|
2048 |
2048 |
///@}
|
2049 |
2049 |
|
2050 |
2050 |
///\name Obtain the Solution
|
2051 |
2051 |
|
2052 |
2052 |
///@{
|
2053 |
2053 |
|
2054 |
2054 |
/// The type of the MIP problem
|
2055 |
2055 |
ProblemType type() const {
|
2056 |
2056 |
return _getType();
|
2057 |
2057 |
}
|
2058 |
2058 |
|
2059 |
2059 |
/// Return the value of the row in the solution
|
2060 |
2060 |
|
2061 |
2061 |
/// Return the value of the row in the solution.
|
2062 |
2062 |
/// \pre The problem is solved.
|
2063 |
2063 |
Value sol(Col c) const { return _getSol(cols(id(c))); }
|
2064 |
2064 |
|
2065 |
2065 |
/// Return the value of the expression in the solution
|
2066 |
2066 |
|
2067 |
2067 |
/// Return the value of the expression in the solution, i.e. the
|
2068 |
2068 |
/// dot product of the solution and the expression.
|
2069 |
2069 |
/// \pre The problem is solved.
|
2070 |
2070 |
Value sol(const Expr& e) const {
|
2071 |
2071 |
double res = *e;
|
2072 |
2072 |
for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
|
2073 |
2073 |
res += *c * sol(c);
|
2074 |
2074 |
}
|
2075 |
2075 |
return res;
|
2076 |
2076 |
}
|
2077 |
2077 |
///The value of the objective function
|
2078 |
2078 |
|
2079 |
2079 |
///\return
|
2080 |
2080 |
///- \ref INF or -\ref INF means either infeasibility or unboundedness
|
2081 |
2081 |
/// of the problem, depending on whether we minimize or maximize.
|
2082 |
2082 |
///- \ref NaN if no primal solution is found.
|
2083 |
2083 |
///- The (finite) objective value if an optimal solution is found.
|
2084 |
2084 |
Value solValue() const { return _getSolValue()+obj_const_comp;}
|
2085 |
2085 |
///@}
|
2086 |
2086 |
|
2087 |
2087 |
protected:
|
2088 |
2088 |
|
2089 |
2089 |
virtual SolveExitStatus _solve() = 0;
|
2090 |
2090 |
virtual ColTypes _getColType(int col) const = 0;
|
2091 |
2091 |
virtual void _setColType(int col, ColTypes col_type) = 0;
|
2092 |
2092 |
virtual ProblemType _getType() const = 0;
|
2093 |
2093 |
virtual Value _getSol(int i) const = 0;
|
2094 |
2094 |
virtual Value _getSolValue() const = 0;
|
2095 |
2095 |
|
2096 |
2096 |
};
|
2097 |
2097 |
|
2098 |
2098 |
|
2099 |
2099 |
|
2100 |
2100 |
} //namespace lemon
|
2101 |
2101 |
|
2102 |
2102 |
#endif //LEMON_LP_BASE_H
|