0
2
0
| ... | ... |
@@ -588,193 +588,193 @@ |
| 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 |
| ... | ... |
@@ -619,193 +619,193 @@ |
| 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) {
|
0 comments (0 inline)