0
2
0
| ... | ... |
@@ -492,385 +492,385 @@ |
| 492 | 492 |
return *this; |
| 493 | 493 |
} |
| 494 | 494 |
|
| 495 | 495 |
/// @} |
| 496 | 496 |
|
| 497 | 497 |
/// \name Execution control |
| 498 | 498 |
/// The algorithm can be executed using \ref run(). |
| 499 | 499 |
|
| 500 | 500 |
/// @{
|
| 501 | 501 |
|
| 502 | 502 |
/// \brief Run the algorithm. |
| 503 | 503 |
/// |
| 504 | 504 |
/// This function runs the algorithm. |
| 505 | 505 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 506 | 506 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 507 | 507 |
/// For example, |
| 508 | 508 |
/// \code |
| 509 | 509 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 510 | 510 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 511 | 511 |
/// .supplyMap(sup).run(); |
| 512 | 512 |
/// \endcode |
| 513 | 513 |
/// |
| 514 | 514 |
/// This function can be called more than once. All the parameters |
| 515 | 515 |
/// that have been given are kept for the next call, unless |
| 516 | 516 |
/// \ref reset() is called, thus only the modified parameters |
| 517 | 517 |
/// have to be set again. See \ref reset() for examples. |
| 518 | 518 |
/// However, the underlying digraph must not be modified after this |
| 519 | 519 |
/// class have been constructed, since it copies and extends the graph. |
| 520 | 520 |
/// |
| 521 | 521 |
/// \param factor The capacity scaling factor. It must be larger than |
| 522 | 522 |
/// one to use scaling. If it is less or equal to one, then scaling |
| 523 | 523 |
/// will be disabled. |
| 524 | 524 |
/// |
| 525 | 525 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 526 | 526 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 527 | 527 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 528 | 528 |
/// optimal flow and node potentials (primal and dual solutions), |
| 529 | 529 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 530 | 530 |
/// and infinite upper bound. It means that the objective function |
| 531 | 531 |
/// is unbounded on that arc, however, note that it could actually be |
| 532 | 532 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 533 | 533 |
/// these cases. |
| 534 | 534 |
/// |
| 535 | 535 |
/// \see ProblemType |
| 536 | 536 |
ProblemType run(int factor = 4) {
|
| 537 | 537 |
_factor = factor; |
| 538 | 538 |
ProblemType pt = init(); |
| 539 | 539 |
if (pt != OPTIMAL) return pt; |
| 540 | 540 |
return start(); |
| 541 | 541 |
} |
| 542 | 542 |
|
| 543 | 543 |
/// \brief Reset all the parameters that have been given before. |
| 544 | 544 |
/// |
| 545 | 545 |
/// This function resets all the paramaters that have been given |
| 546 | 546 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 547 | 547 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 548 | 548 |
/// |
| 549 | 549 |
/// It is useful for multiple run() calls. If this function is not |
| 550 | 550 |
/// used, all the parameters given before are kept for the next |
| 551 | 551 |
/// \ref run() call. |
| 552 | 552 |
/// However, the underlying digraph must not be modified after this |
| 553 | 553 |
/// class have been constructed, since it copies and extends the graph. |
| 554 | 554 |
/// |
| 555 | 555 |
/// For example, |
| 556 | 556 |
/// \code |
| 557 | 557 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 558 | 558 |
/// |
| 559 | 559 |
/// // First run |
| 560 | 560 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 561 | 561 |
/// .supplyMap(sup).run(); |
| 562 | 562 |
/// |
| 563 | 563 |
/// // Run again with modified cost map (reset() is not called, |
| 564 | 564 |
/// // so only the cost map have to be set again) |
| 565 | 565 |
/// cost[e] += 100; |
| 566 | 566 |
/// cs.costMap(cost).run(); |
| 567 | 567 |
/// |
| 568 | 568 |
/// // Run again from scratch using reset() |
| 569 | 569 |
/// // (the lower bounds will be set to zero on all arcs) |
| 570 | 570 |
/// cs.reset(); |
| 571 | 571 |
/// cs.upperMap(capacity).costMap(cost) |
| 572 | 572 |
/// .supplyMap(sup).run(); |
| 573 | 573 |
/// \endcode |
| 574 | 574 |
/// |
| 575 | 575 |
/// \return <tt>(*this)</tt> |
| 576 | 576 |
CapacityScaling& reset() {
|
| 577 | 577 |
for (int i = 0; i != _node_num; ++i) {
|
| 578 | 578 |
_supply[i] = 0; |
| 579 | 579 |
} |
| 580 | 580 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 581 | 581 |
_lower[j] = 0; |
| 582 | 582 |
_upper[j] = INF; |
| 583 | 583 |
_cost[j] = _forward[j] ? 1 : -1; |
| 584 | 584 |
} |
| 585 | 585 |
_have_lower = false; |
| 586 | 586 |
return *this; |
| 587 | 587 |
} |
| 588 | 588 |
|
| 589 | 589 |
/// @} |
| 590 | 590 |
|
| 591 | 591 |
/// \name Query Functions |
| 592 | 592 |
/// The results of the algorithm can be obtained using these |
| 593 | 593 |
/// functions.\n |
| 594 | 594 |
/// The \ref run() function must be called before using them. |
| 595 | 595 |
|
| 596 | 596 |
/// @{
|
| 597 | 597 |
|
| 598 | 598 |
/// \brief Return the total cost of the found flow. |
| 599 | 599 |
/// |
| 600 | 600 |
/// This function returns the total cost of the found flow. |
| 601 | 601 |
/// Its complexity is O(e). |
| 602 | 602 |
/// |
| 603 | 603 |
/// \note The return type of the function can be specified as a |
| 604 | 604 |
/// template parameter. For example, |
| 605 | 605 |
/// \code |
| 606 | 606 |
/// cs.totalCost<double>(); |
| 607 | 607 |
/// \endcode |
| 608 | 608 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 609 | 609 |
/// type of the algorithm, which is the default return type of the |
| 610 | 610 |
/// function. |
| 611 | 611 |
/// |
| 612 | 612 |
/// \pre \ref run() must be called before using this function. |
| 613 | 613 |
template <typename Number> |
| 614 | 614 |
Number totalCost() const {
|
| 615 | 615 |
Number c = 0; |
| 616 | 616 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 617 | 617 |
int i = _arc_idb[a]; |
| 618 | 618 |
c += static_cast<Number>(_res_cap[i]) * |
| 619 | 619 |
(-static_cast<Number>(_cost[i])); |
| 620 | 620 |
} |
| 621 | 621 |
return c; |
| 622 | 622 |
} |
| 623 | 623 |
|
| 624 | 624 |
#ifndef DOXYGEN |
| 625 | 625 |
Cost totalCost() const {
|
| 626 | 626 |
return totalCost<Cost>(); |
| 627 | 627 |
} |
| 628 | 628 |
#endif |
| 629 | 629 |
|
| 630 | 630 |
/// \brief Return the flow on the given arc. |
| 631 | 631 |
/// |
| 632 | 632 |
/// This function returns the flow on the given arc. |
| 633 | 633 |
/// |
| 634 | 634 |
/// \pre \ref run() must be called before using this function. |
| 635 | 635 |
Value flow(const Arc& a) const {
|
| 636 | 636 |
return _res_cap[_arc_idb[a]]; |
| 637 | 637 |
} |
| 638 | 638 |
|
| 639 | 639 |
/// \brief Return the flow map (the primal solution). |
| 640 | 640 |
/// |
| 641 | 641 |
/// This function copies the flow value on each arc into the given |
| 642 | 642 |
/// map. The \c Value type of the algorithm must be convertible to |
| 643 | 643 |
/// the \c Value type of the map. |
| 644 | 644 |
/// |
| 645 | 645 |
/// \pre \ref run() must be called before using this function. |
| 646 | 646 |
template <typename FlowMap> |
| 647 | 647 |
void flowMap(FlowMap &map) const {
|
| 648 | 648 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 649 | 649 |
map.set(a, _res_cap[_arc_idb[a]]); |
| 650 | 650 |
} |
| 651 | 651 |
} |
| 652 | 652 |
|
| 653 | 653 |
/// \brief Return the potential (dual value) of the given node. |
| 654 | 654 |
/// |
| 655 | 655 |
/// This function returns the potential (dual value) of the |
| 656 | 656 |
/// given node. |
| 657 | 657 |
/// |
| 658 | 658 |
/// \pre \ref run() must be called before using this function. |
| 659 | 659 |
Cost potential(const Node& n) const {
|
| 660 | 660 |
return _pi[_node_id[n]]; |
| 661 | 661 |
} |
| 662 | 662 |
|
| 663 | 663 |
/// \brief Return the potential map (the dual solution). |
| 664 | 664 |
/// |
| 665 | 665 |
/// This function copies the potential (dual value) of each node |
| 666 | 666 |
/// into the given map. |
| 667 | 667 |
/// The \c Cost type of the algorithm must be convertible to the |
| 668 | 668 |
/// \c Value type of the map. |
| 669 | 669 |
/// |
| 670 | 670 |
/// \pre \ref run() must be called before using this function. |
| 671 | 671 |
template <typename PotentialMap> |
| 672 | 672 |
void potentialMap(PotentialMap &map) const {
|
| 673 | 673 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 674 | 674 |
map.set(n, _pi[_node_id[n]]); |
| 675 | 675 |
} |
| 676 | 676 |
} |
| 677 | 677 |
|
| 678 | 678 |
/// @} |
| 679 | 679 |
|
| 680 | 680 |
private: |
| 681 | 681 |
|
| 682 | 682 |
// Initialize the algorithm |
| 683 | 683 |
ProblemType init() {
|
| 684 |
if (_node_num |
|
| 684 |
if (_node_num <= 1) return INFEASIBLE; |
|
| 685 | 685 |
|
| 686 | 686 |
// Check the sum of supply values |
| 687 | 687 |
_sum_supply = 0; |
| 688 | 688 |
for (int i = 0; i != _root; ++i) {
|
| 689 | 689 |
_sum_supply += _supply[i]; |
| 690 | 690 |
} |
| 691 | 691 |
if (_sum_supply > 0) return INFEASIBLE; |
| 692 | 692 |
|
| 693 | 693 |
// Initialize vectors |
| 694 | 694 |
for (int i = 0; i != _root; ++i) {
|
| 695 | 695 |
_pi[i] = 0; |
| 696 | 696 |
_excess[i] = _supply[i]; |
| 697 | 697 |
} |
| 698 | 698 |
|
| 699 | 699 |
// Remove non-zero lower bounds |
| 700 | 700 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 701 | 701 |
int last_out; |
| 702 | 702 |
if (_have_lower) {
|
| 703 | 703 |
for (int i = 0; i != _root; ++i) {
|
| 704 | 704 |
last_out = _first_out[i+1]; |
| 705 | 705 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 706 | 706 |
if (_forward[j]) {
|
| 707 | 707 |
Value c = _lower[j]; |
| 708 | 708 |
if (c >= 0) {
|
| 709 | 709 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
| 710 | 710 |
} else {
|
| 711 | 711 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
| 712 | 712 |
} |
| 713 | 713 |
_excess[i] -= c; |
| 714 | 714 |
_excess[_target[j]] += c; |
| 715 | 715 |
} else {
|
| 716 | 716 |
_res_cap[j] = 0; |
| 717 | 717 |
} |
| 718 | 718 |
} |
| 719 | 719 |
} |
| 720 | 720 |
} else {
|
| 721 | 721 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 722 | 722 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
| 723 | 723 |
} |
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 | 726 |
// Handle negative costs |
| 727 | 727 |
for (int i = 0; i != _root; ++i) {
|
| 728 | 728 |
last_out = _first_out[i+1] - 1; |
| 729 | 729 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 730 | 730 |
Value rc = _res_cap[j]; |
| 731 | 731 |
if (_cost[j] < 0 && rc > 0) {
|
| 732 | 732 |
if (rc >= MAX) return UNBOUNDED; |
| 733 | 733 |
_excess[i] -= rc; |
| 734 | 734 |
_excess[_target[j]] += rc; |
| 735 | 735 |
_res_cap[j] = 0; |
| 736 | 736 |
_res_cap[_reverse[j]] += rc; |
| 737 | 737 |
} |
| 738 | 738 |
} |
| 739 | 739 |
} |
| 740 | 740 |
|
| 741 | 741 |
// Handle GEQ supply type |
| 742 | 742 |
if (_sum_supply < 0) {
|
| 743 | 743 |
_pi[_root] = 0; |
| 744 | 744 |
_excess[_root] = -_sum_supply; |
| 745 | 745 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 746 | 746 |
int ra = _reverse[a]; |
| 747 | 747 |
_res_cap[a] = -_sum_supply + 1; |
| 748 | 748 |
_res_cap[ra] = 0; |
| 749 | 749 |
_cost[a] = 0; |
| 750 | 750 |
_cost[ra] = 0; |
| 751 | 751 |
} |
| 752 | 752 |
} else {
|
| 753 | 753 |
_pi[_root] = 0; |
| 754 | 754 |
_excess[_root] = 0; |
| 755 | 755 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 756 | 756 |
int ra = _reverse[a]; |
| 757 | 757 |
_res_cap[a] = 1; |
| 758 | 758 |
_res_cap[ra] = 0; |
| 759 | 759 |
_cost[a] = 0; |
| 760 | 760 |
_cost[ra] = 0; |
| 761 | 761 |
} |
| 762 | 762 |
} |
| 763 | 763 |
|
| 764 | 764 |
// Initialize delta value |
| 765 | 765 |
if (_factor > 1) {
|
| 766 | 766 |
// With scaling |
| 767 | 767 |
Value max_sup = 0, max_dem = 0; |
| 768 | 768 |
for (int i = 0; i != _node_num; ++i) {
|
| 769 | 769 |
Value ex = _excess[i]; |
| 770 | 770 |
if ( ex > max_sup) max_sup = ex; |
| 771 | 771 |
if (-ex > max_dem) max_dem = -ex; |
| 772 | 772 |
} |
| 773 | 773 |
Value max_cap = 0; |
| 774 | 774 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 775 | 775 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
| 776 | 776 |
} |
| 777 | 777 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
| 778 | 778 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
| 779 | 779 |
} else {
|
| 780 | 780 |
// Without scaling |
| 781 | 781 |
_delta = 1; |
| 782 | 782 |
} |
| 783 | 783 |
|
| 784 | 784 |
return OPTIMAL; |
| 785 | 785 |
} |
| 786 | 786 |
|
| 787 | 787 |
ProblemType start() {
|
| 788 | 788 |
// Execute the algorithm |
| 789 | 789 |
ProblemType pt; |
| 790 | 790 |
if (_delta > 1) |
| 791 | 791 |
pt = startWithScaling(); |
| 792 | 792 |
else |
| 793 | 793 |
pt = startWithoutScaling(); |
| 794 | 794 |
|
| 795 | 795 |
// Handle non-zero lower bounds |
| 796 | 796 |
if (_have_lower) {
|
| 797 | 797 |
int limit = _first_out[_root]; |
| 798 | 798 |
for (int j = 0; j != limit; ++j) {
|
| 799 | 799 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 800 | 800 |
} |
| 801 | 801 |
} |
| 802 | 802 |
|
| 803 | 803 |
// Shift potentials if necessary |
| 804 | 804 |
Cost pr = _pi[_root]; |
| 805 | 805 |
if (_sum_supply < 0 || pr > 0) {
|
| 806 | 806 |
for (int i = 0; i != _node_num; ++i) {
|
| 807 | 807 |
_pi[i] -= pr; |
| 808 | 808 |
} |
| 809 | 809 |
} |
| 810 | 810 |
|
| 811 | 811 |
return pt; |
| 812 | 812 |
} |
| 813 | 813 |
|
| 814 | 814 |
// Execute the capacity scaling algorithm |
| 815 | 815 |
ProblemType startWithScaling() {
|
| 816 | 816 |
// Perform capacity scaling phases |
| 817 | 817 |
int s, t; |
| 818 | 818 |
ResidualDijkstra _dijkstra(*this); |
| 819 | 819 |
while (true) {
|
| 820 | 820 |
// Saturate all arcs not satisfying the optimality condition |
| 821 | 821 |
int last_out; |
| 822 | 822 |
for (int u = 0; u != _node_num; ++u) {
|
| 823 | 823 |
last_out = _sum_supply < 0 ? |
| 824 | 824 |
_first_out[u+1] : _first_out[u+1] - 1; |
| 825 | 825 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 826 | 826 |
int v = _target[a]; |
| 827 | 827 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
| 828 | 828 |
Value rc = _res_cap[a]; |
| 829 | 829 |
if (c < 0 && rc >= _delta) {
|
| 830 | 830 |
_excess[u] -= rc; |
| 831 | 831 |
_excess[v] += rc; |
| 832 | 832 |
_res_cap[a] = 0; |
| 833 | 833 |
_res_cap[_reverse[a]] += rc; |
| 834 | 834 |
} |
| 835 | 835 |
} |
| 836 | 836 |
} |
| 837 | 837 |
|
| 838 | 838 |
// Find excess nodes and deficit nodes |
| 839 | 839 |
_excess_nodes.clear(); |
| 840 | 840 |
_deficit_nodes.clear(); |
| 841 | 841 |
for (int u = 0; u != _node_num; ++u) {
|
| 842 | 842 |
Value ex = _excess[u]; |
| 843 | 843 |
if (ex >= _delta) _excess_nodes.push_back(u); |
| 844 | 844 |
if (ex <= -_delta) _deficit_nodes.push_back(u); |
| 845 | 845 |
} |
| 846 | 846 |
int next_node = 0, next_def_node = 0; |
| 847 | 847 |
|
| 848 | 848 |
// Find augmenting shortest paths |
| 849 | 849 |
while (next_node < int(_excess_nodes.size())) {
|
| 850 | 850 |
// Check deficit nodes |
| 851 | 851 |
if (_delta > 1) {
|
| 852 | 852 |
bool delta_deficit = false; |
| 853 | 853 |
for ( ; next_def_node < int(_deficit_nodes.size()); |
| 854 | 854 |
++next_def_node ) {
|
| 855 | 855 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
|
| 856 | 856 |
delta_deficit = true; |
| 857 | 857 |
break; |
| 858 | 858 |
} |
| 859 | 859 |
} |
| 860 | 860 |
if (!delta_deficit) break; |
| 861 | 861 |
} |
| 862 | 862 |
|
| 863 | 863 |
// Run Dijkstra in the residual network |
| 864 | 864 |
s = _excess_nodes[next_node]; |
| 865 | 865 |
if ((t = _dijkstra.run(s, _delta)) == -1) {
|
| 866 | 866 |
if (_delta > 1) {
|
| 867 | 867 |
++next_node; |
| 868 | 868 |
continue; |
| 869 | 869 |
} |
| 870 | 870 |
return INFEASIBLE; |
| 871 | 871 |
} |
| 872 | 872 |
|
| 873 | 873 |
// Augment along a shortest path from s to t |
| 874 | 874 |
Value d = std::min(_excess[s], -_excess[t]); |
| 875 | 875 |
int u = t; |
| 876 | 876 |
int a; |
| ... | ... |
@@ -523,385 +523,385 @@ |
| 523 | 523 |
/// @{
|
| 524 | 524 |
|
| 525 | 525 |
/// \brief Run the algorithm. |
| 526 | 526 |
/// |
| 527 | 527 |
/// This function runs the algorithm. |
| 528 | 528 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 529 | 529 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 530 | 530 |
/// For example, |
| 531 | 531 |
/// \code |
| 532 | 532 |
/// CostScaling<ListDigraph> cs(graph); |
| 533 | 533 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 534 | 534 |
/// .supplyMap(sup).run(); |
| 535 | 535 |
/// \endcode |
| 536 | 536 |
/// |
| 537 | 537 |
/// This function can be called more than once. All the parameters |
| 538 | 538 |
/// that have been given are kept for the next call, unless |
| 539 | 539 |
/// \ref reset() is called, thus only the modified parameters |
| 540 | 540 |
/// have to be set again. See \ref reset() for examples. |
| 541 | 541 |
/// However, the underlying digraph must not be modified after this |
| 542 | 542 |
/// class have been constructed, since it copies and extends the graph. |
| 543 | 543 |
/// |
| 544 | 544 |
/// \param method The internal method that will be used in the |
| 545 | 545 |
/// algorithm. For more information, see \ref Method. |
| 546 | 546 |
/// \param factor The cost scaling factor. It must be larger than one. |
| 547 | 547 |
/// |
| 548 | 548 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 549 | 549 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 550 | 550 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 551 | 551 |
/// optimal flow and node potentials (primal and dual solutions), |
| 552 | 552 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 553 | 553 |
/// and infinite upper bound. It means that the objective function |
| 554 | 554 |
/// is unbounded on that arc, however, note that it could actually be |
| 555 | 555 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 556 | 556 |
/// these cases. |
| 557 | 557 |
/// |
| 558 | 558 |
/// \see ProblemType, Method |
| 559 | 559 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) {
|
| 560 | 560 |
_alpha = factor; |
| 561 | 561 |
ProblemType pt = init(); |
| 562 | 562 |
if (pt != OPTIMAL) return pt; |
| 563 | 563 |
start(method); |
| 564 | 564 |
return OPTIMAL; |
| 565 | 565 |
} |
| 566 | 566 |
|
| 567 | 567 |
/// \brief Reset all the parameters that have been given before. |
| 568 | 568 |
/// |
| 569 | 569 |
/// This function resets all the paramaters that have been given |
| 570 | 570 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 571 | 571 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 572 | 572 |
/// |
| 573 | 573 |
/// It is useful for multiple run() calls. If this function is not |
| 574 | 574 |
/// used, all the parameters given before are kept for the next |
| 575 | 575 |
/// \ref run() call. |
| 576 | 576 |
/// However, the underlying digraph must not be modified after this |
| 577 | 577 |
/// class have been constructed, since it copies and extends the graph. |
| 578 | 578 |
/// |
| 579 | 579 |
/// For example, |
| 580 | 580 |
/// \code |
| 581 | 581 |
/// CostScaling<ListDigraph> cs(graph); |
| 582 | 582 |
/// |
| 583 | 583 |
/// // First run |
| 584 | 584 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 585 | 585 |
/// .supplyMap(sup).run(); |
| 586 | 586 |
/// |
| 587 | 587 |
/// // Run again with modified cost map (reset() is not called, |
| 588 | 588 |
/// // so only the cost map have to be set again) |
| 589 | 589 |
/// cost[e] += 100; |
| 590 | 590 |
/// cs.costMap(cost).run(); |
| 591 | 591 |
/// |
| 592 | 592 |
/// // Run again from scratch using reset() |
| 593 | 593 |
/// // (the lower bounds will be set to zero on all arcs) |
| 594 | 594 |
/// cs.reset(); |
| 595 | 595 |
/// cs.upperMap(capacity).costMap(cost) |
| 596 | 596 |
/// .supplyMap(sup).run(); |
| 597 | 597 |
/// \endcode |
| 598 | 598 |
/// |
| 599 | 599 |
/// \return <tt>(*this)</tt> |
| 600 | 600 |
CostScaling& reset() {
|
| 601 | 601 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 602 | 602 |
_supply[i] = 0; |
| 603 | 603 |
} |
| 604 | 604 |
int limit = _first_out[_root]; |
| 605 | 605 |
for (int j = 0; j != limit; ++j) {
|
| 606 | 606 |
_lower[j] = 0; |
| 607 | 607 |
_upper[j] = INF; |
| 608 | 608 |
_scost[j] = _forward[j] ? 1 : -1; |
| 609 | 609 |
} |
| 610 | 610 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 611 | 611 |
_lower[j] = 0; |
| 612 | 612 |
_upper[j] = INF; |
| 613 | 613 |
_scost[j] = 0; |
| 614 | 614 |
_scost[_reverse[j]] = 0; |
| 615 | 615 |
} |
| 616 | 616 |
_have_lower = false; |
| 617 | 617 |
return *this; |
| 618 | 618 |
} |
| 619 | 619 |
|
| 620 | 620 |
/// @} |
| 621 | 621 |
|
| 622 | 622 |
/// \name Query Functions |
| 623 | 623 |
/// The results of the algorithm can be obtained using these |
| 624 | 624 |
/// functions.\n |
| 625 | 625 |
/// The \ref run() function must be called before using them. |
| 626 | 626 |
|
| 627 | 627 |
/// @{
|
| 628 | 628 |
|
| 629 | 629 |
/// \brief Return the total cost of the found flow. |
| 630 | 630 |
/// |
| 631 | 631 |
/// This function returns the total cost of the found flow. |
| 632 | 632 |
/// Its complexity is O(e). |
| 633 | 633 |
/// |
| 634 | 634 |
/// \note The return type of the function can be specified as a |
| 635 | 635 |
/// template parameter. For example, |
| 636 | 636 |
/// \code |
| 637 | 637 |
/// cs.totalCost<double>(); |
| 638 | 638 |
/// \endcode |
| 639 | 639 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 640 | 640 |
/// type of the algorithm, which is the default return type of the |
| 641 | 641 |
/// function. |
| 642 | 642 |
/// |
| 643 | 643 |
/// \pre \ref run() must be called before using this function. |
| 644 | 644 |
template <typename Number> |
| 645 | 645 |
Number totalCost() const {
|
| 646 | 646 |
Number c = 0; |
| 647 | 647 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 648 | 648 |
int i = _arc_idb[a]; |
| 649 | 649 |
c += static_cast<Number>(_res_cap[i]) * |
| 650 | 650 |
(-static_cast<Number>(_scost[i])); |
| 651 | 651 |
} |
| 652 | 652 |
return c; |
| 653 | 653 |
} |
| 654 | 654 |
|
| 655 | 655 |
#ifndef DOXYGEN |
| 656 | 656 |
Cost totalCost() const {
|
| 657 | 657 |
return totalCost<Cost>(); |
| 658 | 658 |
} |
| 659 | 659 |
#endif |
| 660 | 660 |
|
| 661 | 661 |
/// \brief Return the flow on the given arc. |
| 662 | 662 |
/// |
| 663 | 663 |
/// This function returns the flow on the given arc. |
| 664 | 664 |
/// |
| 665 | 665 |
/// \pre \ref run() must be called before using this function. |
| 666 | 666 |
Value flow(const Arc& a) const {
|
| 667 | 667 |
return _res_cap[_arc_idb[a]]; |
| 668 | 668 |
} |
| 669 | 669 |
|
| 670 | 670 |
/// \brief Return the flow map (the primal solution). |
| 671 | 671 |
/// |
| 672 | 672 |
/// This function copies the flow value on each arc into the given |
| 673 | 673 |
/// map. The \c Value type of the algorithm must be convertible to |
| 674 | 674 |
/// the \c Value type of the map. |
| 675 | 675 |
/// |
| 676 | 676 |
/// \pre \ref run() must be called before using this function. |
| 677 | 677 |
template <typename FlowMap> |
| 678 | 678 |
void flowMap(FlowMap &map) const {
|
| 679 | 679 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 680 | 680 |
map.set(a, _res_cap[_arc_idb[a]]); |
| 681 | 681 |
} |
| 682 | 682 |
} |
| 683 | 683 |
|
| 684 | 684 |
/// \brief Return the potential (dual value) of the given node. |
| 685 | 685 |
/// |
| 686 | 686 |
/// This function returns the potential (dual value) of the |
| 687 | 687 |
/// given node. |
| 688 | 688 |
/// |
| 689 | 689 |
/// \pre \ref run() must be called before using this function. |
| 690 | 690 |
Cost potential(const Node& n) const {
|
| 691 | 691 |
return static_cast<Cost>(_pi[_node_id[n]]); |
| 692 | 692 |
} |
| 693 | 693 |
|
| 694 | 694 |
/// \brief Return the potential map (the dual solution). |
| 695 | 695 |
/// |
| 696 | 696 |
/// This function copies the potential (dual value) of each node |
| 697 | 697 |
/// into the given map. |
| 698 | 698 |
/// The \c Cost type of the algorithm must be convertible to the |
| 699 | 699 |
/// \c Value type of the map. |
| 700 | 700 |
/// |
| 701 | 701 |
/// \pre \ref run() must be called before using this function. |
| 702 | 702 |
template <typename PotentialMap> |
| 703 | 703 |
void potentialMap(PotentialMap &map) const {
|
| 704 | 704 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 705 | 705 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
| 706 | 706 |
} |
| 707 | 707 |
} |
| 708 | 708 |
|
| 709 | 709 |
/// @} |
| 710 | 710 |
|
| 711 | 711 |
private: |
| 712 | 712 |
|
| 713 | 713 |
// Initialize the algorithm |
| 714 | 714 |
ProblemType init() {
|
| 715 |
if (_res_node_num |
|
| 715 |
if (_res_node_num <= 1) return INFEASIBLE; |
|
| 716 | 716 |
|
| 717 | 717 |
// Check the sum of supply values |
| 718 | 718 |
_sum_supply = 0; |
| 719 | 719 |
for (int i = 0; i != _root; ++i) {
|
| 720 | 720 |
_sum_supply += _supply[i]; |
| 721 | 721 |
} |
| 722 | 722 |
if (_sum_supply > 0) return INFEASIBLE; |
| 723 | 723 |
|
| 724 | 724 |
|
| 725 | 725 |
// Initialize vectors |
| 726 | 726 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 727 | 727 |
_pi[i] = 0; |
| 728 | 728 |
_excess[i] = _supply[i]; |
| 729 | 729 |
} |
| 730 | 730 |
|
| 731 | 731 |
// Remove infinite upper bounds and check negative arcs |
| 732 | 732 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 733 | 733 |
int last_out; |
| 734 | 734 |
if (_have_lower) {
|
| 735 | 735 |
for (int i = 0; i != _root; ++i) {
|
| 736 | 736 |
last_out = _first_out[i+1]; |
| 737 | 737 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 738 | 738 |
if (_forward[j]) {
|
| 739 | 739 |
Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; |
| 740 | 740 |
if (c >= MAX) return UNBOUNDED; |
| 741 | 741 |
_excess[i] -= c; |
| 742 | 742 |
_excess[_target[j]] += c; |
| 743 | 743 |
} |
| 744 | 744 |
} |
| 745 | 745 |
} |
| 746 | 746 |
} else {
|
| 747 | 747 |
for (int i = 0; i != _root; ++i) {
|
| 748 | 748 |
last_out = _first_out[i+1]; |
| 749 | 749 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 750 | 750 |
if (_forward[j] && _scost[j] < 0) {
|
| 751 | 751 |
Value c = _upper[j]; |
| 752 | 752 |
if (c >= MAX) return UNBOUNDED; |
| 753 | 753 |
_excess[i] -= c; |
| 754 | 754 |
_excess[_target[j]] += c; |
| 755 | 755 |
} |
| 756 | 756 |
} |
| 757 | 757 |
} |
| 758 | 758 |
} |
| 759 | 759 |
Value ex, max_cap = 0; |
| 760 | 760 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 761 | 761 |
ex = _excess[i]; |
| 762 | 762 |
_excess[i] = 0; |
| 763 | 763 |
if (ex < 0) max_cap -= ex; |
| 764 | 764 |
} |
| 765 | 765 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 766 | 766 |
if (_upper[j] >= MAX) _upper[j] = max_cap; |
| 767 | 767 |
} |
| 768 | 768 |
|
| 769 | 769 |
// Initialize the large cost vector and the epsilon parameter |
| 770 | 770 |
_epsilon = 0; |
| 771 | 771 |
LargeCost lc; |
| 772 | 772 |
for (int i = 0; i != _root; ++i) {
|
| 773 | 773 |
last_out = _first_out[i+1]; |
| 774 | 774 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 775 | 775 |
lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha; |
| 776 | 776 |
_cost[j] = lc; |
| 777 | 777 |
if (lc > _epsilon) _epsilon = lc; |
| 778 | 778 |
} |
| 779 | 779 |
} |
| 780 | 780 |
_epsilon /= _alpha; |
| 781 | 781 |
|
| 782 | 782 |
// Initialize maps for Circulation and remove non-zero lower bounds |
| 783 | 783 |
ConstMap<Arc, Value> low(0); |
| 784 | 784 |
typedef typename Digraph::template ArcMap<Value> ValueArcMap; |
| 785 | 785 |
typedef typename Digraph::template NodeMap<Value> ValueNodeMap; |
| 786 | 786 |
ValueArcMap cap(_graph), flow(_graph); |
| 787 | 787 |
ValueNodeMap sup(_graph); |
| 788 | 788 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 789 | 789 |
sup[n] = _supply[_node_id[n]]; |
| 790 | 790 |
} |
| 791 | 791 |
if (_have_lower) {
|
| 792 | 792 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 793 | 793 |
int j = _arc_idf[a]; |
| 794 | 794 |
Value c = _lower[j]; |
| 795 | 795 |
cap[a] = _upper[j] - c; |
| 796 | 796 |
sup[_graph.source(a)] -= c; |
| 797 | 797 |
sup[_graph.target(a)] += c; |
| 798 | 798 |
} |
| 799 | 799 |
} else {
|
| 800 | 800 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 801 | 801 |
cap[a] = _upper[_arc_idf[a]]; |
| 802 | 802 |
} |
| 803 | 803 |
} |
| 804 | 804 |
|
| 805 | 805 |
// Find a feasible flow using Circulation |
| 806 | 806 |
Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap> |
| 807 | 807 |
circ(_graph, low, cap, sup); |
| 808 | 808 |
if (!circ.flowMap(flow).run()) return INFEASIBLE; |
| 809 | 809 |
|
| 810 | 810 |
// Set residual capacities and handle GEQ supply type |
| 811 | 811 |
if (_sum_supply < 0) {
|
| 812 | 812 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 813 | 813 |
Value fa = flow[a]; |
| 814 | 814 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 815 | 815 |
_res_cap[_arc_idb[a]] = fa; |
| 816 | 816 |
sup[_graph.source(a)] -= fa; |
| 817 | 817 |
sup[_graph.target(a)] += fa; |
| 818 | 818 |
} |
| 819 | 819 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 820 | 820 |
_excess[_node_id[n]] = sup[n]; |
| 821 | 821 |
} |
| 822 | 822 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 823 | 823 |
int u = _target[a]; |
| 824 | 824 |
int ra = _reverse[a]; |
| 825 | 825 |
_res_cap[a] = -_sum_supply + 1; |
| 826 | 826 |
_res_cap[ra] = -_excess[u]; |
| 827 | 827 |
_cost[a] = 0; |
| 828 | 828 |
_cost[ra] = 0; |
| 829 | 829 |
_excess[u] = 0; |
| 830 | 830 |
} |
| 831 | 831 |
} else {
|
| 832 | 832 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 833 | 833 |
Value fa = flow[a]; |
| 834 | 834 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 835 | 835 |
_res_cap[_arc_idb[a]] = fa; |
| 836 | 836 |
} |
| 837 | 837 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 838 | 838 |
int ra = _reverse[a]; |
| 839 | 839 |
_res_cap[a] = 1; |
| 840 | 840 |
_res_cap[ra] = 0; |
| 841 | 841 |
_cost[a] = 0; |
| 842 | 842 |
_cost[ra] = 0; |
| 843 | 843 |
} |
| 844 | 844 |
} |
| 845 | 845 |
|
| 846 | 846 |
return OPTIMAL; |
| 847 | 847 |
} |
| 848 | 848 |
|
| 849 | 849 |
// Execute the algorithm and transform the results |
| 850 | 850 |
void start(Method method) {
|
| 851 | 851 |
// Maximum path length for partial augment |
| 852 | 852 |
const int MAX_PATH_LENGTH = 4; |
| 853 | 853 |
|
| 854 | 854 |
// Execute the algorithm |
| 855 | 855 |
switch (method) {
|
| 856 | 856 |
case PUSH: |
| 857 | 857 |
startPush(); |
| 858 | 858 |
break; |
| 859 | 859 |
case AUGMENT: |
| 860 | 860 |
startAugment(); |
| 861 | 861 |
break; |
| 862 | 862 |
case PARTIAL_AUGMENT: |
| 863 | 863 |
startAugment(MAX_PATH_LENGTH); |
| 864 | 864 |
break; |
| 865 | 865 |
} |
| 866 | 866 |
|
| 867 | 867 |
// Compute node potentials for the original costs |
| 868 | 868 |
_arc_vec.clear(); |
| 869 | 869 |
_cost_vec.clear(); |
| 870 | 870 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 871 | 871 |
if (_res_cap[j] > 0) {
|
| 872 | 872 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 873 | 873 |
_cost_vec.push_back(_scost[j]); |
| 874 | 874 |
} |
| 875 | 875 |
} |
| 876 | 876 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 877 | 877 |
|
| 878 | 878 |
typename BellmanFord<StaticDigraph, LargeCostArcMap> |
| 879 | 879 |
::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map); |
| 880 | 880 |
bf.distMap(_pi_map); |
| 881 | 881 |
bf.init(0); |
| 882 | 882 |
bf.start(); |
| 883 | 883 |
|
| 884 | 884 |
// Handle non-zero lower bounds |
| 885 | 885 |
if (_have_lower) {
|
| 886 | 886 |
int limit = _first_out[_root]; |
| 887 | 887 |
for (int j = 0; j != limit; ++j) {
|
| 888 | 888 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 889 | 889 |
} |
| 890 | 890 |
} |
| 891 | 891 |
} |
| 892 | 892 |
|
| 893 | 893 |
/// Execute the algorithm performing augment and relabel operations |
| 894 | 894 |
void startAugment(int max_length = std::numeric_limits<int>::max()) {
|
| 895 | 895 |
// Paramters for heuristics |
| 896 | 896 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000; |
| 897 | 897 |
const int BF_HEURISTIC_BOUND_FACTOR = 3; |
| 898 | 898 |
|
| 899 | 899 |
// Perform cost scaling phases |
| 900 | 900 |
IntVector pred_arc(_res_node_num); |
| 901 | 901 |
std::vector<int> path_nodes; |
| 902 | 902 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
| 903 | 903 |
1 : _epsilon / _alpha ) |
| 904 | 904 |
{
|
| 905 | 905 |
// "Early Termination" heuristic: use Bellman-Ford algorithm |
| 906 | 906 |
// to check if the current flow is optimal |
| 907 | 907 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
|
0 comments (0 inline)