| ... | ... |
@@ -394,207 +394,208 @@ |
| 394 | 394 |
return *this; |
| 395 | 395 |
} |
| 396 | 396 |
|
| 397 | 397 |
/// \brief Set the costs of the arcs. |
| 398 | 398 |
/// |
| 399 | 399 |
/// This function sets the costs of the arcs. |
| 400 | 400 |
/// If it is not used before calling \ref run(), the costs |
| 401 | 401 |
/// will be set to \c 1 on all arcs. |
| 402 | 402 |
/// |
| 403 | 403 |
/// \param map An arc map storing the costs. |
| 404 | 404 |
/// Its \c Value type must be convertible to the \c Cost type |
| 405 | 405 |
/// of the algorithm. |
| 406 | 406 |
/// |
| 407 | 407 |
/// \return <tt>(*this)</tt> |
| 408 | 408 |
template<typename CostMap> |
| 409 | 409 |
CostScaling& costMap(const CostMap& map) {
|
| 410 | 410 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 411 | 411 |
_scost[_arc_idf[a]] = map[a]; |
| 412 | 412 |
_scost[_arc_idb[a]] = -map[a]; |
| 413 | 413 |
} |
| 414 | 414 |
return *this; |
| 415 | 415 |
} |
| 416 | 416 |
|
| 417 | 417 |
/// \brief Set the supply values of the nodes. |
| 418 | 418 |
/// |
| 419 | 419 |
/// This function sets the supply values of the nodes. |
| 420 | 420 |
/// If neither this function nor \ref stSupply() is used before |
| 421 | 421 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 422 | 422 |
/// |
| 423 | 423 |
/// \param map A node map storing the supply values. |
| 424 | 424 |
/// Its \c Value type must be convertible to the \c Value type |
| 425 | 425 |
/// of the algorithm. |
| 426 | 426 |
/// |
| 427 | 427 |
/// \return <tt>(*this)</tt> |
| 428 | 428 |
template<typename SupplyMap> |
| 429 | 429 |
CostScaling& supplyMap(const SupplyMap& map) {
|
| 430 | 430 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 431 | 431 |
_supply[_node_id[n]] = map[n]; |
| 432 | 432 |
} |
| 433 | 433 |
return *this; |
| 434 | 434 |
} |
| 435 | 435 |
|
| 436 | 436 |
/// \brief Set single source and target nodes and a supply value. |
| 437 | 437 |
/// |
| 438 | 438 |
/// This function sets a single source node and a single target node |
| 439 | 439 |
/// and the required flow value. |
| 440 | 440 |
/// If neither this function nor \ref supplyMap() is used before |
| 441 | 441 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 442 | 442 |
/// |
| 443 | 443 |
/// Using this function has the same effect as using \ref supplyMap() |
| 444 | 444 |
/// with a map in which \c k is assigned to \c s, \c -k is |
| 445 | 445 |
/// assigned to \c t and all other nodes have zero supply value. |
| 446 | 446 |
/// |
| 447 | 447 |
/// \param s The source node. |
| 448 | 448 |
/// \param t The target node. |
| 449 | 449 |
/// \param k The required amount of flow from node \c s to node \c t |
| 450 | 450 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 451 | 451 |
/// |
| 452 | 452 |
/// \return <tt>(*this)</tt> |
| 453 | 453 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 454 | 454 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 455 | 455 |
_supply[i] = 0; |
| 456 | 456 |
} |
| 457 | 457 |
_supply[_node_id[s]] = k; |
| 458 | 458 |
_supply[_node_id[t]] = -k; |
| 459 | 459 |
return *this; |
| 460 | 460 |
} |
| 461 | 461 |
|
| 462 | 462 |
/// @} |
| 463 | 463 |
|
| 464 | 464 |
/// \name Execution control |
| 465 | 465 |
/// The algorithm can be executed using \ref run(). |
| 466 | 466 |
|
| 467 | 467 |
/// @{
|
| 468 | 468 |
|
| 469 | 469 |
/// \brief Run the algorithm. |
| 470 | 470 |
/// |
| 471 | 471 |
/// This function runs the algorithm. |
| 472 | 472 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 473 | 473 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 474 | 474 |
/// For example, |
| 475 | 475 |
/// \code |
| 476 | 476 |
/// CostScaling<ListDigraph> cs(graph); |
| 477 | 477 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 478 | 478 |
/// .supplyMap(sup).run(); |
| 479 | 479 |
/// \endcode |
| 480 | 480 |
/// |
| 481 | 481 |
/// This function can be called more than once. All the given parameters |
| 482 | 482 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 483 | 483 |
/// is used, thus only the modified parameters have to be set again. |
| 484 | 484 |
/// If the underlying digraph was also modified after the construction |
| 485 | 485 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 486 | 486 |
/// function must be called. |
| 487 | 487 |
/// |
| 488 | 488 |
/// \param method The internal method that will be used in the |
| 489 | 489 |
/// algorithm. For more information, see \ref Method. |
| 490 |
/// \param factor The cost scaling factor. It must be |
|
| 490 |
/// \param factor The cost scaling factor. It must be at least two. |
|
| 491 | 491 |
/// |
| 492 | 492 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 493 | 493 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 494 | 494 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 495 | 495 |
/// optimal flow and node potentials (primal and dual solutions), |
| 496 | 496 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 497 | 497 |
/// and infinite upper bound. It means that the objective function |
| 498 | 498 |
/// is unbounded on that arc, however, note that it could actually be |
| 499 | 499 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 500 | 500 |
/// these cases. |
| 501 | 501 |
/// |
| 502 | 502 |
/// \see ProblemType, Method |
| 503 | 503 |
/// \see resetParams(), reset() |
| 504 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = |
|
| 504 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 16) {
|
|
| 505 |
LEMON_ASSERT(factor >= 2, "The scaling factor must be at least 2"); |
|
| 505 | 506 |
_alpha = factor; |
| 506 | 507 |
ProblemType pt = init(); |
| 507 | 508 |
if (pt != OPTIMAL) return pt; |
| 508 | 509 |
start(method); |
| 509 | 510 |
return OPTIMAL; |
| 510 | 511 |
} |
| 511 | 512 |
|
| 512 | 513 |
/// \brief Reset all the parameters that have been given before. |
| 513 | 514 |
/// |
| 514 | 515 |
/// This function resets all the paramaters that have been given |
| 515 | 516 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 516 | 517 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 517 | 518 |
/// |
| 518 | 519 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
| 519 | 520 |
/// parameters are kept for the next \ref run() call, unless |
| 520 | 521 |
/// \ref resetParams() or \ref reset() is used. |
| 521 | 522 |
/// If the underlying digraph was also modified after the construction |
| 522 | 523 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 523 | 524 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 524 | 525 |
/// |
| 525 | 526 |
/// For example, |
| 526 | 527 |
/// \code |
| 527 | 528 |
/// CostScaling<ListDigraph> cs(graph); |
| 528 | 529 |
/// |
| 529 | 530 |
/// // First run |
| 530 | 531 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 531 | 532 |
/// .supplyMap(sup).run(); |
| 532 | 533 |
/// |
| 533 | 534 |
/// // Run again with modified cost map (resetParams() is not called, |
| 534 | 535 |
/// // so only the cost map have to be set again) |
| 535 | 536 |
/// cost[e] += 100; |
| 536 | 537 |
/// cs.costMap(cost).run(); |
| 537 | 538 |
/// |
| 538 | 539 |
/// // Run again from scratch using resetParams() |
| 539 | 540 |
/// // (the lower bounds will be set to zero on all arcs) |
| 540 | 541 |
/// cs.resetParams(); |
| 541 | 542 |
/// cs.upperMap(capacity).costMap(cost) |
| 542 | 543 |
/// .supplyMap(sup).run(); |
| 543 | 544 |
/// \endcode |
| 544 | 545 |
/// |
| 545 | 546 |
/// \return <tt>(*this)</tt> |
| 546 | 547 |
/// |
| 547 | 548 |
/// \see reset(), run() |
| 548 | 549 |
CostScaling& resetParams() {
|
| 549 | 550 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 550 | 551 |
_supply[i] = 0; |
| 551 | 552 |
} |
| 552 | 553 |
int limit = _first_out[_root]; |
| 553 | 554 |
for (int j = 0; j != limit; ++j) {
|
| 554 | 555 |
_lower[j] = 0; |
| 555 | 556 |
_upper[j] = INF; |
| 556 | 557 |
_scost[j] = _forward[j] ? 1 : -1; |
| 557 | 558 |
} |
| 558 | 559 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 559 | 560 |
_lower[j] = 0; |
| 560 | 561 |
_upper[j] = INF; |
| 561 | 562 |
_scost[j] = 0; |
| 562 | 563 |
_scost[_reverse[j]] = 0; |
| 563 | 564 |
} |
| 564 | 565 |
_have_lower = false; |
| 565 | 566 |
return *this; |
| 566 | 567 |
} |
| 567 | 568 |
|
| 568 | 569 |
/// \brief Reset the internal data structures and all the parameters |
| 569 | 570 |
/// that have been given before. |
| 570 | 571 |
/// |
| 571 | 572 |
/// This function resets the internal data structures and all the |
| 572 | 573 |
/// paramaters that have been given before using functions \ref lowerMap(), |
| 573 | 574 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 574 | 575 |
/// |
| 575 | 576 |
/// It is useful for multiple \ref run() calls. By default, all the given |
| 576 | 577 |
/// parameters are kept for the next \ref run() call, unless |
| 577 | 578 |
/// \ref resetParams() or \ref reset() is used. |
| 578 | 579 |
/// If the underlying digraph was also modified after the construction |
| 579 | 580 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 580 | 581 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 581 | 582 |
/// |
| 582 | 583 |
/// See \ref resetParams() for examples. |
| 583 | 584 |
/// |
| 584 | 585 |
/// \return <tt>(*this)</tt> |
| 585 | 586 |
/// |
| 586 | 587 |
/// \see resetParams(), run() |
| 587 | 588 |
CostScaling& reset() {
|
| 588 | 589 |
// Resize vectors |
| 589 | 590 |
_node_num = countNodes(_graph); |
| 590 | 591 |
_arc_num = countArcs(_graph); |
| 591 | 592 |
_res_node_num = _node_num + 1; |
| 592 | 593 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 593 | 594 |
_root = _node_num; |
| 594 | 595 |
|
| 595 | 596 |
_first_out.resize(_res_node_num + 1); |
| 596 | 597 |
_forward.resize(_res_arc_num); |
| 597 | 598 |
_source.resize(_res_arc_num); |
| 598 | 599 |
_target.resize(_res_arc_num); |
| 599 | 600 |
_reverse.resize(_res_arc_num); |
| 600 | 601 |
|
0 comments (0 inline)