| ... | ... |
@@ -116,98 +116,96 @@ |
| 116 | 116 |
|
| 117 | 117 |
/// \brief Constants for selecting the pivot rule. |
| 118 | 118 |
/// |
| 119 | 119 |
/// Enum type containing constants for selecting the pivot rule for |
| 120 | 120 |
/// the \ref run() function. |
| 121 | 121 |
/// |
| 122 | 122 |
/// \ref NetworkSimplex provides five different pivot rule |
| 123 | 123 |
/// implementations that significantly affect the running time |
| 124 | 124 |
/// of the algorithm. |
| 125 | 125 |
/// By default \ref BLOCK_SEARCH "Block Search" is used, which |
| 126 | 126 |
/// proved to be the most efficient and the most robust on various |
| 127 | 127 |
/// test inputs according to our benchmark tests. |
| 128 | 128 |
/// However another pivot rule can be selected using the \ref run() |
| 129 | 129 |
/// function with the proper parameter. |
| 130 | 130 |
enum PivotRule {
|
| 131 | 131 |
|
| 132 | 132 |
/// The First Eligible pivot rule. |
| 133 | 133 |
/// The next eligible arc is selected in a wraparound fashion |
| 134 | 134 |
/// in every iteration. |
| 135 | 135 |
FIRST_ELIGIBLE, |
| 136 | 136 |
|
| 137 | 137 |
/// The Best Eligible pivot rule. |
| 138 | 138 |
/// The best eligible arc is selected in every iteration. |
| 139 | 139 |
BEST_ELIGIBLE, |
| 140 | 140 |
|
| 141 | 141 |
/// The Block Search pivot rule. |
| 142 | 142 |
/// A specified number of arcs are examined in every iteration |
| 143 | 143 |
/// in a wraparound fashion and the best eligible arc is selected |
| 144 | 144 |
/// from this block. |
| 145 | 145 |
BLOCK_SEARCH, |
| 146 | 146 |
|
| 147 | 147 |
/// The Candidate List pivot rule. |
| 148 | 148 |
/// In a major iteration a candidate list is built from eligible arcs |
| 149 | 149 |
/// in a wraparound fashion and in the following minor iterations |
| 150 | 150 |
/// the best eligible arc is selected from this list. |
| 151 | 151 |
CANDIDATE_LIST, |
| 152 | 152 |
|
| 153 | 153 |
/// The Altering Candidate List pivot rule. |
| 154 | 154 |
/// It is a modified version of the Candidate List method. |
| 155 | 155 |
/// It keeps only the several best eligible arcs from the former |
| 156 | 156 |
/// candidate list and extends this list in every iteration. |
| 157 | 157 |
ALTERING_LIST |
| 158 | 158 |
}; |
| 159 | 159 |
|
| 160 | 160 |
private: |
| 161 | 161 |
|
| 162 | 162 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 163 | 163 |
|
| 164 |
typedef std::vector<Arc> ArcVector; |
|
| 165 |
typedef std::vector<Node> NodeVector; |
|
| 166 | 164 |
typedef std::vector<int> IntVector; |
| 167 | 165 |
typedef std::vector<bool> BoolVector; |
| 168 | 166 |
typedef std::vector<Value> ValueVector; |
| 169 | 167 |
typedef std::vector<Cost> CostVector; |
| 170 | 168 |
|
| 171 | 169 |
// State constants for arcs |
| 172 | 170 |
enum ArcStateEnum {
|
| 173 | 171 |
STATE_UPPER = -1, |
| 174 | 172 |
STATE_TREE = 0, |
| 175 | 173 |
STATE_LOWER = 1 |
| 176 | 174 |
}; |
| 177 | 175 |
|
| 178 | 176 |
private: |
| 179 | 177 |
|
| 180 | 178 |
// Data related to the underlying digraph |
| 181 | 179 |
const GR &_graph; |
| 182 | 180 |
int _node_num; |
| 183 | 181 |
int _arc_num; |
| 184 | 182 |
int _all_arc_num; |
| 185 | 183 |
int _search_arc_num; |
| 186 | 184 |
|
| 187 | 185 |
// Parameters of the problem |
| 188 | 186 |
bool _have_lower; |
| 189 | 187 |
SupplyType _stype; |
| 190 | 188 |
Value _sum_supply; |
| 191 | 189 |
|
| 192 | 190 |
// Data structures for storing the digraph |
| 193 | 191 |
IntNodeMap _node_id; |
| 194 | 192 |
IntArcMap _arc_id; |
| 195 | 193 |
IntVector _source; |
| 196 | 194 |
IntVector _target; |
| 197 | 195 |
|
| 198 | 196 |
// Node and arc data |
| 199 | 197 |
ValueVector _lower; |
| 200 | 198 |
ValueVector _upper; |
| 201 | 199 |
ValueVector _cap; |
| 202 | 200 |
CostVector _cost; |
| 203 | 201 |
ValueVector _supply; |
| 204 | 202 |
ValueVector _flow; |
| 205 | 203 |
CostVector _pi; |
| 206 | 204 |
|
| 207 | 205 |
// Data for storing the spanning tree structure |
| 208 | 206 |
IntVector _parent; |
| 209 | 207 |
IntVector _pred; |
| 210 | 208 |
IntVector _thread; |
| 211 | 209 |
IntVector _rev_thread; |
| 212 | 210 |
IntVector _succ_num; |
| 213 | 211 |
IntVector _last_succ; |
| ... | ... |
@@ -640,201 +638,190 @@ |
| 640 | 638 |
std::numeric_limits<Value>::max()) |
| 641 | 639 |
{
|
| 642 | 640 |
// Check the value types |
| 643 | 641 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 644 | 642 |
"The flow type of NetworkSimplex must be signed"); |
| 645 | 643 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 646 | 644 |
"The cost type of NetworkSimplex must be signed"); |
| 647 | 645 |
|
| 648 | 646 |
// Resize vectors |
| 649 | 647 |
_node_num = countNodes(_graph); |
| 650 | 648 |
_arc_num = countArcs(_graph); |
| 651 | 649 |
int all_node_num = _node_num + 1; |
| 652 | 650 |
int max_arc_num = _arc_num + 2 * _node_num; |
| 653 | 651 |
|
| 654 | 652 |
_source.resize(max_arc_num); |
| 655 | 653 |
_target.resize(max_arc_num); |
| 656 | 654 |
|
| 657 | 655 |
_lower.resize(_arc_num); |
| 658 | 656 |
_upper.resize(_arc_num); |
| 659 | 657 |
_cap.resize(max_arc_num); |
| 660 | 658 |
_cost.resize(max_arc_num); |
| 661 | 659 |
_supply.resize(all_node_num); |
| 662 | 660 |
_flow.resize(max_arc_num); |
| 663 | 661 |
_pi.resize(all_node_num); |
| 664 | 662 |
|
| 665 | 663 |
_parent.resize(all_node_num); |
| 666 | 664 |
_pred.resize(all_node_num); |
| 667 | 665 |
_forward.resize(all_node_num); |
| 668 | 666 |
_thread.resize(all_node_num); |
| 669 | 667 |
_rev_thread.resize(all_node_num); |
| 670 | 668 |
_succ_num.resize(all_node_num); |
| 671 | 669 |
_last_succ.resize(all_node_num); |
| 672 | 670 |
_state.resize(max_arc_num); |
| 673 | 671 |
|
| 674 | 672 |
// Copy the graph (store the arcs in a mixed order) |
| 675 | 673 |
int i = 0; |
| 676 | 674 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 677 | 675 |
_node_id[n] = i; |
| 678 | 676 |
} |
| 679 | 677 |
int k = std::max(int(std::sqrt(double(_arc_num))), 10); |
| 680 | 678 |
i = 0; |
| 681 | 679 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 682 | 680 |
_arc_id[a] = i; |
| 683 | 681 |
_source[i] = _node_id[_graph.source(a)]; |
| 684 | 682 |
_target[i] = _node_id[_graph.target(a)]; |
| 685 | 683 |
if ((i += k) >= _arc_num) i = (i % k) + 1; |
| 686 | 684 |
} |
| 687 | 685 |
|
| 688 |
// Initialize maps |
|
| 689 |
for (int i = 0; i != _node_num; ++i) {
|
|
| 690 |
_supply[i] = 0; |
|
| 691 |
} |
|
| 692 |
for (int i = 0; i != _arc_num; ++i) {
|
|
| 693 |
_lower[i] = 0; |
|
| 694 |
_upper[i] = INF; |
|
| 695 |
_cost[i] = 1; |
|
| 696 |
} |
|
| 697 |
_have_lower = false; |
|
| 698 |
|
|
| 686 |
// Reset parameters |
|
| 687 |
reset(); |
|
| 699 | 688 |
} |
| 700 | 689 |
|
| 701 | 690 |
/// \name Parameters |
| 702 | 691 |
/// The parameters of the algorithm can be specified using these |
| 703 | 692 |
/// functions. |
| 704 | 693 |
|
| 705 | 694 |
/// @{
|
| 706 | 695 |
|
| 707 | 696 |
/// \brief Set the lower bounds on the arcs. |
| 708 | 697 |
/// |
| 709 | 698 |
/// This function sets the lower bounds on the arcs. |
| 710 | 699 |
/// If it is not used before calling \ref run(), the lower bounds |
| 711 | 700 |
/// will be set to zero on all arcs. |
| 712 | 701 |
/// |
| 713 | 702 |
/// \param map An arc map storing the lower bounds. |
| 714 | 703 |
/// Its \c Value type must be convertible to the \c Value type |
| 715 | 704 |
/// of the algorithm. |
| 716 | 705 |
/// |
| 717 | 706 |
/// \return <tt>(*this)</tt> |
| 718 | 707 |
template <typename LowerMap> |
| 719 | 708 |
NetworkSimplex& lowerMap(const LowerMap& map) {
|
| 720 | 709 |
_have_lower = true; |
| 721 | 710 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 722 | 711 |
_lower[_arc_id[a]] = map[a]; |
| 723 | 712 |
} |
| 724 | 713 |
return *this; |
| 725 | 714 |
} |
| 726 | 715 |
|
| 727 | 716 |
/// \brief Set the upper bounds (capacities) on the arcs. |
| 728 | 717 |
/// |
| 729 | 718 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 730 | 719 |
/// If it is not used before calling \ref run(), the upper bounds |
| 731 | 720 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 732 | 721 |
/// unbounded from above on each arc). |
| 733 | 722 |
/// |
| 734 | 723 |
/// \param map An arc map storing the upper bounds. |
| 735 | 724 |
/// Its \c Value type must be convertible to the \c Value type |
| 736 | 725 |
/// of the algorithm. |
| 737 | 726 |
/// |
| 738 | 727 |
/// \return <tt>(*this)</tt> |
| 739 | 728 |
template<typename UpperMap> |
| 740 | 729 |
NetworkSimplex& upperMap(const UpperMap& map) {
|
| 741 | 730 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 742 | 731 |
_upper[_arc_id[a]] = map[a]; |
| 743 | 732 |
} |
| 744 | 733 |
return *this; |
| 745 | 734 |
} |
| 746 | 735 |
|
| 747 | 736 |
/// \brief Set the costs of the arcs. |
| 748 | 737 |
/// |
| 749 | 738 |
/// This function sets the costs of the arcs. |
| 750 | 739 |
/// If it is not used before calling \ref run(), the costs |
| 751 | 740 |
/// will be set to \c 1 on all arcs. |
| 752 | 741 |
/// |
| 753 | 742 |
/// \param map An arc map storing the costs. |
| 754 | 743 |
/// Its \c Value type must be convertible to the \c Cost type |
| 755 | 744 |
/// of the algorithm. |
| 756 | 745 |
/// |
| 757 | 746 |
/// \return <tt>(*this)</tt> |
| 758 | 747 |
template<typename CostMap> |
| 759 | 748 |
NetworkSimplex& costMap(const CostMap& map) {
|
| 760 | 749 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 761 | 750 |
_cost[_arc_id[a]] = map[a]; |
| 762 | 751 |
} |
| 763 | 752 |
return *this; |
| 764 | 753 |
} |
| 765 | 754 |
|
| 766 | 755 |
/// \brief Set the supply values of the nodes. |
| 767 | 756 |
/// |
| 768 | 757 |
/// This function sets the supply values of the nodes. |
| 769 | 758 |
/// If neither this function nor \ref stSupply() is used before |
| 770 | 759 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 771 |
/// (It makes sense only if non-zero lower bounds are given.) |
|
| 772 | 760 |
/// |
| 773 | 761 |
/// \param map A node map storing the supply values. |
| 774 | 762 |
/// Its \c Value type must be convertible to the \c Value type |
| 775 | 763 |
/// of the algorithm. |
| 776 | 764 |
/// |
| 777 | 765 |
/// \return <tt>(*this)</tt> |
| 778 | 766 |
template<typename SupplyMap> |
| 779 | 767 |
NetworkSimplex& supplyMap(const SupplyMap& map) {
|
| 780 | 768 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 781 | 769 |
_supply[_node_id[n]] = map[n]; |
| 782 | 770 |
} |
| 783 | 771 |
return *this; |
| 784 | 772 |
} |
| 785 | 773 |
|
| 786 | 774 |
/// \brief Set single source and target nodes and a supply value. |
| 787 | 775 |
/// |
| 788 | 776 |
/// This function sets a single source node and a single target node |
| 789 | 777 |
/// and the required flow value. |
| 790 | 778 |
/// If neither this function nor \ref supplyMap() is used before |
| 791 | 779 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 792 |
/// (It makes sense only if non-zero lower bounds are given.) |
|
| 793 | 780 |
/// |
| 794 | 781 |
/// Using this function has the same effect as using \ref supplyMap() |
| 795 | 782 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 796 | 783 |
/// assigned to \c t and all other nodes have zero supply value. |
| 797 | 784 |
/// |
| 798 | 785 |
/// \param s The source node. |
| 799 | 786 |
/// \param t The target node. |
| 800 | 787 |
/// \param k The required amount of flow from node \c s to node \c t |
| 801 | 788 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 802 | 789 |
/// |
| 803 | 790 |
/// \return <tt>(*this)</tt> |
| 804 | 791 |
NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) {
|
| 805 | 792 |
for (int i = 0; i != _node_num; ++i) {
|
| 806 | 793 |
_supply[i] = 0; |
| 807 | 794 |
} |
| 808 | 795 |
_supply[_node_id[s]] = k; |
| 809 | 796 |
_supply[_node_id[t]] = -k; |
| 810 | 797 |
return *this; |
| 811 | 798 |
} |
| 812 | 799 |
|
| 813 | 800 |
/// \brief Set the type of the supply constraints. |
| 814 | 801 |
/// |
| 815 | 802 |
/// This function sets the type of the supply/demand constraints. |
| 816 | 803 |
/// If it is not used before calling \ref run(), the \ref GEQ supply |
| 817 | 804 |
/// type will be used. |
| 818 | 805 |
/// |
| 819 | 806 |
/// For more information see \ref SupplyType. |
| 820 | 807 |
/// |
| 821 | 808 |
/// \return <tt>(*this)</tt> |
| 822 | 809 |
NetworkSimplex& supplyType(SupplyType supply_type) {
|
| 823 | 810 |
_stype = supply_type; |
| 824 | 811 |
return *this; |
| 825 | 812 |
} |
| 826 | 813 |
|
| 827 | 814 |
/// @} |
| 828 | 815 |
|
| 829 | 816 |
/// \name Execution Control |
| 830 | 817 |
/// The algorithm can be executed using \ref run(). |
| 831 | 818 |
|
| 832 | 819 |
/// @{
|
| 833 | 820 |
|
| 834 | 821 |
/// \brief Run the algorithm. |
| 835 | 822 |
/// |
| 836 | 823 |
/// This function runs the algorithm. |
| 837 | 824 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 838 | 825 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
| 839 | 826 |
/// \ref supplyType(). |
| 840 | 827 |
/// For example, |
0 comments (0 inline)