0
2
0
... | ... |
@@ -112,49 +112,49 @@ |
112 | 112 |
/// \brief Problem type constants for the \c run() function. |
113 | 113 |
/// |
114 | 114 |
/// Enum type containing the problem type constants that can be |
115 | 115 |
/// returned by the \ref run() function of the algorithm. |
116 | 116 |
enum ProblemType { |
117 | 117 |
/// The problem has no feasible solution (flow). |
118 | 118 |
INFEASIBLE, |
119 | 119 |
/// The problem has optimal solution (i.e. it is feasible and |
120 | 120 |
/// bounded), and the algorithm has found optimal flow and node |
121 | 121 |
/// potentials (primal and dual solutions). |
122 | 122 |
OPTIMAL, |
123 | 123 |
/// The digraph contains an arc of negative cost and infinite |
124 | 124 |
/// upper bound. It means that the objective function is unbounded |
125 | 125 |
/// on that arc, however note that it could actually be bounded |
126 | 126 |
/// over the feasible flows, but this algroithm cannot handle |
127 | 127 |
/// these cases. |
128 | 128 |
UNBOUNDED |
129 | 129 |
}; |
130 | 130 |
|
131 | 131 |
private: |
132 | 132 |
|
133 | 133 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
134 | 134 |
|
135 | 135 |
typedef std::vector<int> IntVector; |
136 |
typedef std::vector< |
|
136 |
typedef std::vector<char> BoolVector; |
|
137 | 137 |
typedef std::vector<Value> ValueVector; |
138 | 138 |
typedef std::vector<Cost> CostVector; |
139 | 139 |
|
140 | 140 |
private: |
141 | 141 |
|
142 | 142 |
// Data related to the underlying digraph |
143 | 143 |
const GR &_graph; |
144 | 144 |
int _node_num; |
145 | 145 |
int _arc_num; |
146 | 146 |
int _res_arc_num; |
147 | 147 |
int _root; |
148 | 148 |
|
149 | 149 |
// Parameters of the problem |
150 | 150 |
bool _have_lower; |
151 | 151 |
Value _sum_supply; |
152 | 152 |
|
153 | 153 |
// Data structures for storing the digraph |
154 | 154 |
IntNodeMap _node_id; |
155 | 155 |
IntArcMap _arc_idf; |
156 | 156 |
IntArcMap _arc_idb; |
157 | 157 |
IntVector _first_out; |
158 | 158 |
BoolVector _forward; |
159 | 159 |
IntVector _source; |
160 | 160 |
IntVector _target; |
... | ... |
@@ -175,85 +175,87 @@ |
175 | 175 |
Value _delta; |
176 | 176 |
int _factor; |
177 | 177 |
IntVector _pred; |
178 | 178 |
|
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
/// \brief Constant for infinite upper bounds (capacities). |
182 | 182 |
/// |
183 | 183 |
/// Constant for infinite upper bounds (capacities). |
184 | 184 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
185 | 185 |
/// \c std::numeric_limits<Value>::max() otherwise. |
186 | 186 |
const Value INF; |
187 | 187 |
|
188 | 188 |
private: |
189 | 189 |
|
190 | 190 |
// Special implementation of the Dijkstra algorithm for finding |
191 | 191 |
// shortest paths in the residual network of the digraph with |
192 | 192 |
// respect to the reduced arc costs and modifying the node |
193 | 193 |
// potentials according to the found distance labels. |
194 | 194 |
class ResidualDijkstra |
195 | 195 |
{ |
196 | 196 |
private: |
197 | 197 |
|
198 | 198 |
int _node_num; |
199 |
bool _geq; |
|
199 | 200 |
const IntVector &_first_out; |
200 | 201 |
const IntVector &_target; |
201 | 202 |
const CostVector &_cost; |
202 | 203 |
const ValueVector &_res_cap; |
203 | 204 |
const ValueVector &_excess; |
204 | 205 |
CostVector &_pi; |
205 | 206 |
IntVector &_pred; |
206 | 207 |
|
207 | 208 |
IntVector _proc_nodes; |
208 | 209 |
CostVector _dist; |
209 | 210 |
|
210 | 211 |
public: |
211 | 212 |
|
212 | 213 |
ResidualDijkstra(CapacityScaling& cs) : |
213 |
_node_num(cs._node_num), _first_out(cs._first_out), |
|
214 |
_target(cs._target), _cost(cs._cost), _res_cap(cs._res_cap), |
|
215 |
_excess(cs._excess), _pi(cs._pi), _pred(cs._pred), |
|
216 |
_dist(cs._node_num) |
|
214 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
|
215 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
|
216 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
|
217 |
_pred(cs._pred), _dist(cs._node_num) |
|
217 | 218 |
{} |
218 | 219 |
|
219 | 220 |
int run(int s, Value delta = 1) { |
220 | 221 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
221 | 222 |
Heap heap(heap_cross_ref); |
222 | 223 |
heap.push(s, 0); |
223 | 224 |
_pred[s] = -1; |
224 | 225 |
_proc_nodes.clear(); |
225 | 226 |
|
226 | 227 |
// Process nodes |
227 | 228 |
while (!heap.empty() && _excess[heap.top()] > -delta) { |
228 | 229 |
int u = heap.top(), v; |
229 | 230 |
Cost d = heap.prio() + _pi[u], dn; |
230 | 231 |
_dist[u] = heap.prio(); |
231 | 232 |
_proc_nodes.push_back(u); |
232 | 233 |
heap.pop(); |
233 | 234 |
|
234 | 235 |
// Traverse outgoing residual arcs |
235 |
|
|
236 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
|
237 |
for (int a = _first_out[u]; a != last_out; ++a) { |
|
236 | 238 |
if (_res_cap[a] < delta) continue; |
237 | 239 |
v = _target[a]; |
238 | 240 |
switch (heap.state(v)) { |
239 | 241 |
case Heap::PRE_HEAP: |
240 | 242 |
heap.push(v, d + _cost[a] - _pi[v]); |
241 | 243 |
_pred[v] = a; |
242 | 244 |
break; |
243 | 245 |
case Heap::IN_HEAP: |
244 | 246 |
dn = d + _cost[a] - _pi[v]; |
245 | 247 |
if (dn < heap[v]) { |
246 | 248 |
heap.decrease(v, dn); |
247 | 249 |
_pred[v] = a; |
248 | 250 |
} |
249 | 251 |
break; |
250 | 252 |
case Heap::POST_HEAP: |
251 | 253 |
break; |
252 | 254 |
} |
253 | 255 |
} |
254 | 256 |
} |
255 | 257 |
if (heap.empty()) return -1; |
256 | 258 |
|
257 | 259 |
// Update potentials of processed nodes |
258 | 260 |
int t = heap.top(); |
259 | 261 |
Cost dt = heap.prio(); |
... | ... |
@@ -666,193 +668,200 @@ |
666 | 668 |
/// |
667 | 669 |
/// \pre \ref run() must be called before using this function. |
668 | 670 |
template <typename PotentialMap> |
669 | 671 |
void potentialMap(PotentialMap &map) const { |
670 | 672 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
671 | 673 |
map.set(n, _pi[_node_id[n]]); |
672 | 674 |
} |
673 | 675 |
} |
674 | 676 |
|
675 | 677 |
/// @} |
676 | 678 |
|
677 | 679 |
private: |
678 | 680 |
|
679 | 681 |
// Initialize the algorithm |
680 | 682 |
ProblemType init() { |
681 | 683 |
if (_node_num == 0) return INFEASIBLE; |
682 | 684 |
|
683 | 685 |
// Check the sum of supply values |
684 | 686 |
_sum_supply = 0; |
685 | 687 |
for (int i = 0; i != _root; ++i) { |
686 | 688 |
_sum_supply += _supply[i]; |
687 | 689 |
} |
688 | 690 |
if (_sum_supply > 0) return INFEASIBLE; |
689 | 691 |
|
690 |
// Initialize |
|
692 |
// Initialize vectors |
|
691 | 693 |
for (int i = 0; i != _root; ++i) { |
692 | 694 |
_pi[i] = 0; |
693 | 695 |
_excess[i] = _supply[i]; |
694 | 696 |
} |
695 | 697 |
|
696 | 698 |
// Remove non-zero lower bounds |
699 |
const Value MAX = std::numeric_limits<Value>::max(); |
|
700 |
int last_out; |
|
697 | 701 |
if (_have_lower) { |
698 | 702 |
for (int i = 0; i != _root; ++i) { |
699 |
|
|
703 |
last_out = _first_out[i+1]; |
|
704 |
for (int j = _first_out[i]; j != last_out; ++j) { |
|
700 | 705 |
if (_forward[j]) { |
701 | 706 |
Value c = _lower[j]; |
702 | 707 |
if (c >= 0) { |
703 |
_res_cap[j] = _upper[j] < |
|
708 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
|
704 | 709 |
} else { |
705 |
_res_cap[j] = _upper[j] < |
|
710 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
|
706 | 711 |
} |
707 | 712 |
_excess[i] -= c; |
708 | 713 |
_excess[_target[j]] += c; |
709 | 714 |
} else { |
710 | 715 |
_res_cap[j] = 0; |
711 | 716 |
} |
712 | 717 |
} |
713 | 718 |
} |
714 | 719 |
} else { |
715 | 720 |
for (int j = 0; j != _res_arc_num; ++j) { |
716 | 721 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
717 | 722 |
} |
718 | 723 |
} |
719 | 724 |
|
720 | 725 |
// Handle negative costs |
721 |
for (int u = 0; u != _root; ++u) { |
|
722 |
for (int a = _first_out[u]; a != _first_out[u+1]; ++a) { |
|
723 |
Value rc = _res_cap[a]; |
|
724 |
if (_cost[a] < 0 && rc > 0) { |
|
725 |
if (rc == INF) return UNBOUNDED; |
|
726 |
_excess[u] -= rc; |
|
727 |
_excess[_target[a]] += rc; |
|
728 |
_res_cap[a] = 0; |
|
729 |
|
|
726 |
for (int i = 0; i != _root; ++i) { |
|
727 |
last_out = _first_out[i+1] - 1; |
|
728 |
for (int j = _first_out[i]; j != last_out; ++j) { |
|
729 |
Value rc = _res_cap[j]; |
|
730 |
if (_cost[j] < 0 && rc > 0) { |
|
731 |
if (rc >= MAX) return UNBOUNDED; |
|
732 |
_excess[i] -= rc; |
|
733 |
_excess[_target[j]] += rc; |
|
734 |
_res_cap[j] = 0; |
|
735 |
_res_cap[_reverse[j]] += rc; |
|
730 | 736 |
} |
731 | 737 |
} |
732 | 738 |
} |
733 | 739 |
|
734 | 740 |
// Handle GEQ supply type |
735 | 741 |
if (_sum_supply < 0) { |
736 | 742 |
_pi[_root] = 0; |
737 | 743 |
_excess[_root] = -_sum_supply; |
738 | 744 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
739 |
int u = _target[a]; |
|
740 |
if (_excess[u] < 0) { |
|
741 |
_res_cap[a] = -_excess[u] + 1; |
|
742 |
} else { |
|
743 |
_res_cap[a] = 1; |
|
744 |
} |
|
745 |
|
|
745 |
int ra = _reverse[a]; |
|
746 |
_res_cap[a] = -_sum_supply + 1; |
|
747 |
_res_cap[ra] = 0; |
|
746 | 748 |
_cost[a] = 0; |
747 |
_cost[ |
|
749 |
_cost[ra] = 0; |
|
748 | 750 |
} |
749 | 751 |
} else { |
750 | 752 |
_pi[_root] = 0; |
751 | 753 |
_excess[_root] = 0; |
752 | 754 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
755 |
int ra = _reverse[a]; |
|
753 | 756 |
_res_cap[a] = 1; |
754 |
_res_cap[ |
|
757 |
_res_cap[ra] = 0; |
|
755 | 758 |
_cost[a] = 0; |
756 |
_cost[ |
|
759 |
_cost[ra] = 0; |
|
757 | 760 |
} |
758 | 761 |
} |
759 | 762 |
|
760 | 763 |
// Initialize delta value |
761 | 764 |
if (_factor > 1) { |
762 | 765 |
// With scaling |
763 | 766 |
Value max_sup = 0, max_dem = 0; |
764 | 767 |
for (int i = 0; i != _node_num; ++i) { |
765 |
if ( _excess[i] > max_sup) max_sup = _excess[i]; |
|
766 |
if (-_excess[i] > max_dem) max_dem = -_excess[i]; |
|
768 |
Value ex = _excess[i]; |
|
769 |
if ( ex > max_sup) max_sup = ex; |
|
770 |
if (-ex > max_dem) max_dem = -ex; |
|
767 | 771 |
} |
768 | 772 |
Value max_cap = 0; |
769 | 773 |
for (int j = 0; j != _res_arc_num; ++j) { |
770 | 774 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
771 | 775 |
} |
772 | 776 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
773 | 777 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
774 | 778 |
} else { |
775 | 779 |
// Without scaling |
776 | 780 |
_delta = 1; |
777 | 781 |
} |
778 | 782 |
|
779 | 783 |
return OPTIMAL; |
780 | 784 |
} |
781 | 785 |
|
782 | 786 |
ProblemType start() { |
783 | 787 |
// Execute the algorithm |
784 | 788 |
ProblemType pt; |
785 | 789 |
if (_delta > 1) |
786 | 790 |
pt = startWithScaling(); |
787 | 791 |
else |
788 | 792 |
pt = startWithoutScaling(); |
789 | 793 |
|
790 | 794 |
// Handle non-zero lower bounds |
791 | 795 |
if (_have_lower) { |
792 |
|
|
796 |
int limit = _first_out[_root]; |
|
797 |
for (int j = 0; j != limit; ++j) { |
|
793 | 798 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
794 | 799 |
} |
795 | 800 |
} |
796 | 801 |
|
797 | 802 |
// Shift potentials if necessary |
798 | 803 |
Cost pr = _pi[_root]; |
799 | 804 |
if (_sum_supply < 0 || pr > 0) { |
800 | 805 |
for (int i = 0; i != _node_num; ++i) { |
801 | 806 |
_pi[i] -= pr; |
802 | 807 |
} |
803 | 808 |
} |
804 | 809 |
|
805 | 810 |
return pt; |
806 | 811 |
} |
807 | 812 |
|
808 | 813 |
// Execute the capacity scaling algorithm |
809 | 814 |
ProblemType startWithScaling() { |
810 | 815 |
// Perform capacity scaling phases |
811 | 816 |
int s, t; |
812 | 817 |
ResidualDijkstra _dijkstra(*this); |
813 | 818 |
while (true) { |
814 | 819 |
// Saturate all arcs not satisfying the optimality condition |
820 |
int last_out; |
|
815 | 821 |
for (int u = 0; u != _node_num; ++u) { |
816 |
|
|
822 |
last_out = _sum_supply < 0 ? |
|
823 |
_first_out[u+1] : _first_out[u+1] - 1; |
|
824 |
for (int a = _first_out[u]; a != last_out; ++a) { |
|
817 | 825 |
int v = _target[a]; |
818 | 826 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
819 | 827 |
Value rc = _res_cap[a]; |
820 | 828 |
if (c < 0 && rc >= _delta) { |
821 | 829 |
_excess[u] -= rc; |
822 | 830 |
_excess[v] += rc; |
823 | 831 |
_res_cap[a] = 0; |
824 | 832 |
_res_cap[_reverse[a]] += rc; |
825 | 833 |
} |
826 | 834 |
} |
827 | 835 |
} |
828 | 836 |
|
829 | 837 |
// Find excess nodes and deficit nodes |
830 | 838 |
_excess_nodes.clear(); |
831 | 839 |
_deficit_nodes.clear(); |
832 | 840 |
for (int u = 0; u != _node_num; ++u) { |
833 |
if (_excess[u] >= _delta) _excess_nodes.push_back(u); |
|
834 |
if (_excess[u] <= -_delta) _deficit_nodes.push_back(u); |
|
841 |
Value ex = _excess[u]; |
|
842 |
if (ex >= _delta) _excess_nodes.push_back(u); |
|
843 |
if (ex <= -_delta) _deficit_nodes.push_back(u); |
|
835 | 844 |
} |
836 | 845 |
int next_node = 0, next_def_node = 0; |
837 | 846 |
|
838 | 847 |
// Find augmenting shortest paths |
839 | 848 |
while (next_node < int(_excess_nodes.size())) { |
840 | 849 |
// Check deficit nodes |
841 | 850 |
if (_delta > 1) { |
842 | 851 |
bool delta_deficit = false; |
843 | 852 |
for ( ; next_def_node < int(_deficit_nodes.size()); |
844 | 853 |
++next_def_node ) { |
845 | 854 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) { |
846 | 855 |
delta_deficit = true; |
847 | 856 |
break; |
848 | 857 |
} |
849 | 858 |
} |
850 | 859 |
if (!delta_deficit) break; |
851 | 860 |
} |
852 | 861 |
|
853 | 862 |
// Run Dijkstra in the residual network |
854 | 863 |
s = _excess_nodes[next_node]; |
855 | 864 |
if ((t = _dijkstra.run(s, _delta)) == -1) { |
856 | 865 |
if (_delta > 1) { |
857 | 866 |
++next_node; |
858 | 867 |
continue; |
... | ... |
@@ -143,127 +143,129 @@ |
143 | 143 |
/// The \e Block \e Search pivot rule. |
144 | 144 |
/// A specified number of arcs are examined in every iteration |
145 | 145 |
/// in a wraparound fashion and the best eligible arc is selected |
146 | 146 |
/// from this block. |
147 | 147 |
BLOCK_SEARCH, |
148 | 148 |
|
149 | 149 |
/// The \e Candidate \e List pivot rule. |
150 | 150 |
/// In a major iteration a candidate list is built from eligible arcs |
151 | 151 |
/// in a wraparound fashion and in the following minor iterations |
152 | 152 |
/// the best eligible arc is selected from this list. |
153 | 153 |
CANDIDATE_LIST, |
154 | 154 |
|
155 | 155 |
/// The \e Altering \e Candidate \e List pivot rule. |
156 | 156 |
/// It is a modified version of the Candidate List method. |
157 | 157 |
/// It keeps only the several best eligible arcs from the former |
158 | 158 |
/// candidate list and extends this list in every iteration. |
159 | 159 |
ALTERING_LIST |
160 | 160 |
}; |
161 | 161 |
|
162 | 162 |
private: |
163 | 163 |
|
164 | 164 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
165 | 165 |
|
166 | 166 |
typedef std::vector<int> IntVector; |
167 |
typedef std::vector< |
|
167 |
typedef std::vector<char> CharVector; |
|
168 | 168 |
typedef std::vector<Value> ValueVector; |
169 | 169 |
typedef std::vector<Cost> CostVector; |
170 | 170 |
|
171 | 171 |
// State constants for arcs |
172 | 172 |
enum ArcStateEnum { |
173 | 173 |
STATE_UPPER = -1, |
174 | 174 |
STATE_TREE = 0, |
175 | 175 |
STATE_LOWER = 1 |
176 | 176 |
}; |
177 | 177 |
|
178 | 178 |
private: |
179 | 179 |
|
180 | 180 |
// Data related to the underlying digraph |
181 | 181 |
const GR &_graph; |
182 | 182 |
int _node_num; |
183 | 183 |
int _arc_num; |
184 | 184 |
int _all_arc_num; |
185 | 185 |
int _search_arc_num; |
186 | 186 |
|
187 | 187 |
// Parameters of the problem |
188 | 188 |
bool _have_lower; |
189 | 189 |
SupplyType _stype; |
190 | 190 |
Value _sum_supply; |
191 | 191 |
|
192 | 192 |
// Data structures for storing the digraph |
193 | 193 |
IntNodeMap _node_id; |
194 | 194 |
IntArcMap _arc_id; |
195 | 195 |
IntVector _source; |
196 | 196 |
IntVector _target; |
197 | 197 |
|
198 | 198 |
// Node and arc data |
199 | 199 |
ValueVector _lower; |
200 | 200 |
ValueVector _upper; |
201 | 201 |
ValueVector _cap; |
202 | 202 |
CostVector _cost; |
203 | 203 |
ValueVector _supply; |
204 | 204 |
ValueVector _flow; |
205 | 205 |
CostVector _pi; |
206 | 206 |
|
207 | 207 |
// Data for storing the spanning tree structure |
208 | 208 |
IntVector _parent; |
209 | 209 |
IntVector _pred; |
210 | 210 |
IntVector _thread; |
211 | 211 |
IntVector _rev_thread; |
212 | 212 |
IntVector _succ_num; |
213 | 213 |
IntVector _last_succ; |
214 | 214 |
IntVector _dirty_revs; |
215 |
BoolVector _forward; |
|
216 |
IntVector _state; |
|
215 |
CharVector _forward; |
|
216 |
CharVector _state; |
|
217 | 217 |
int _root; |
218 | 218 |
|
219 | 219 |
// Temporary data used in the current pivot iteration |
220 | 220 |
int in_arc, join, u_in, v_in, u_out, v_out; |
221 | 221 |
int first, second, right, last; |
222 | 222 |
int stem, par_stem, new_stem; |
223 | 223 |
Value delta; |
224 |
|
|
225 |
const Value MAX; |
|
224 | 226 |
|
225 | 227 |
public: |
226 | 228 |
|
227 | 229 |
/// \brief Constant for infinite upper bounds (capacities). |
228 | 230 |
/// |
229 | 231 |
/// Constant for infinite upper bounds (capacities). |
230 | 232 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
231 | 233 |
/// \c std::numeric_limits<Value>::max() otherwise. |
232 | 234 |
const Value INF; |
233 | 235 |
|
234 | 236 |
private: |
235 | 237 |
|
236 | 238 |
// Implementation of the First Eligible pivot rule |
237 | 239 |
class FirstEligiblePivotRule |
238 | 240 |
{ |
239 | 241 |
private: |
240 | 242 |
|
241 | 243 |
// References to the NetworkSimplex class |
242 | 244 |
const IntVector &_source; |
243 | 245 |
const IntVector &_target; |
244 | 246 |
const CostVector &_cost; |
245 |
const |
|
247 |
const CharVector &_state; |
|
246 | 248 |
const CostVector &_pi; |
247 | 249 |
int &_in_arc; |
248 | 250 |
int _search_arc_num; |
249 | 251 |
|
250 | 252 |
// Pivot rule data |
251 | 253 |
int _next_arc; |
252 | 254 |
|
253 | 255 |
public: |
254 | 256 |
|
255 | 257 |
// Constructor |
256 | 258 |
FirstEligiblePivotRule(NetworkSimplex &ns) : |
257 | 259 |
_source(ns._source), _target(ns._target), |
258 | 260 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
259 | 261 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
260 | 262 |
_next_arc(0) |
261 | 263 |
{} |
262 | 264 |
|
263 | 265 |
// Find next entering arc |
264 | 266 |
bool findEnteringArc() { |
265 | 267 |
Cost c; |
266 | 268 |
for (int e = _next_arc; e < _search_arc_num; ++e) { |
267 | 269 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
268 | 270 |
if (c < 0) { |
269 | 271 |
_in_arc = e; |
... | ... |
@@ -273,88 +275,88 @@ |
273 | 275 |
} |
274 | 276 |
for (int e = 0; e < _next_arc; ++e) { |
275 | 277 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
276 | 278 |
if (c < 0) { |
277 | 279 |
_in_arc = e; |
278 | 280 |
_next_arc = e + 1; |
279 | 281 |
return true; |
280 | 282 |
} |
281 | 283 |
} |
282 | 284 |
return false; |
283 | 285 |
} |
284 | 286 |
|
285 | 287 |
}; //class FirstEligiblePivotRule |
286 | 288 |
|
287 | 289 |
|
288 | 290 |
// Implementation of the Best Eligible pivot rule |
289 | 291 |
class BestEligiblePivotRule |
290 | 292 |
{ |
291 | 293 |
private: |
292 | 294 |
|
293 | 295 |
// References to the NetworkSimplex class |
294 | 296 |
const IntVector &_source; |
295 | 297 |
const IntVector &_target; |
296 | 298 |
const CostVector &_cost; |
297 |
const |
|
299 |
const CharVector &_state; |
|
298 | 300 |
const CostVector &_pi; |
299 | 301 |
int &_in_arc; |
300 | 302 |
int _search_arc_num; |
301 | 303 |
|
302 | 304 |
public: |
303 | 305 |
|
304 | 306 |
// Constructor |
305 | 307 |
BestEligiblePivotRule(NetworkSimplex &ns) : |
306 | 308 |
_source(ns._source), _target(ns._target), |
307 | 309 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
308 | 310 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num) |
309 | 311 |
{} |
310 | 312 |
|
311 | 313 |
// Find next entering arc |
312 | 314 |
bool findEnteringArc() { |
313 | 315 |
Cost c, min = 0; |
314 | 316 |
for (int e = 0; e < _search_arc_num; ++e) { |
315 | 317 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
316 | 318 |
if (c < min) { |
317 | 319 |
min = c; |
318 | 320 |
_in_arc = e; |
319 | 321 |
} |
320 | 322 |
} |
321 | 323 |
return min < 0; |
322 | 324 |
} |
323 | 325 |
|
324 | 326 |
}; //class BestEligiblePivotRule |
325 | 327 |
|
326 | 328 |
|
327 | 329 |
// Implementation of the Block Search pivot rule |
328 | 330 |
class BlockSearchPivotRule |
329 | 331 |
{ |
330 | 332 |
private: |
331 | 333 |
|
332 | 334 |
// References to the NetworkSimplex class |
333 | 335 |
const IntVector &_source; |
334 | 336 |
const IntVector &_target; |
335 | 337 |
const CostVector &_cost; |
336 |
const |
|
338 |
const CharVector &_state; |
|
337 | 339 |
const CostVector &_pi; |
338 | 340 |
int &_in_arc; |
339 | 341 |
int _search_arc_num; |
340 | 342 |
|
341 | 343 |
// Pivot rule data |
342 | 344 |
int _block_size; |
343 | 345 |
int _next_arc; |
344 | 346 |
|
345 | 347 |
public: |
346 | 348 |
|
347 | 349 |
// Constructor |
348 | 350 |
BlockSearchPivotRule(NetworkSimplex &ns) : |
349 | 351 |
_source(ns._source), _target(ns._target), |
350 | 352 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
351 | 353 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
352 | 354 |
_next_arc(0) |
353 | 355 |
{ |
354 | 356 |
// The main parameters of the pivot rule |
355 | 357 |
const double BLOCK_SIZE_FACTOR = 0.5; |
356 | 358 |
const int MIN_BLOCK_SIZE = 10; |
357 | 359 |
|
358 | 360 |
_block_size = std::max( int(BLOCK_SIZE_FACTOR * |
359 | 361 |
std::sqrt(double(_search_arc_num))), |
360 | 362 |
MIN_BLOCK_SIZE ); |
... | ... |
@@ -385,49 +387,49 @@ |
385 | 387 |
if (--cnt == 0) { |
386 | 388 |
if (min < 0) goto search_end; |
387 | 389 |
cnt = _block_size; |
388 | 390 |
} |
389 | 391 |
} |
390 | 392 |
if (min >= 0) return false; |
391 | 393 |
|
392 | 394 |
search_end: |
393 | 395 |
_next_arc = e; |
394 | 396 |
return true; |
395 | 397 |
} |
396 | 398 |
|
397 | 399 |
}; //class BlockSearchPivotRule |
398 | 400 |
|
399 | 401 |
|
400 | 402 |
// Implementation of the Candidate List pivot rule |
401 | 403 |
class CandidateListPivotRule |
402 | 404 |
{ |
403 | 405 |
private: |
404 | 406 |
|
405 | 407 |
// References to the NetworkSimplex class |
406 | 408 |
const IntVector &_source; |
407 | 409 |
const IntVector &_target; |
408 | 410 |
const CostVector &_cost; |
409 |
const |
|
411 |
const CharVector &_state; |
|
410 | 412 |
const CostVector &_pi; |
411 | 413 |
int &_in_arc; |
412 | 414 |
int _search_arc_num; |
413 | 415 |
|
414 | 416 |
// Pivot rule data |
415 | 417 |
IntVector _candidates; |
416 | 418 |
int _list_length, _minor_limit; |
417 | 419 |
int _curr_length, _minor_count; |
418 | 420 |
int _next_arc; |
419 | 421 |
|
420 | 422 |
public: |
421 | 423 |
|
422 | 424 |
/// Constructor |
423 | 425 |
CandidateListPivotRule(NetworkSimplex &ns) : |
424 | 426 |
_source(ns._source), _target(ns._target), |
425 | 427 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
426 | 428 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
427 | 429 |
_next_arc(0) |
428 | 430 |
{ |
429 | 431 |
// The main parameters of the pivot rule |
430 | 432 |
const double LIST_LENGTH_FACTOR = 0.25; |
431 | 433 |
const int MIN_LIST_LENGTH = 10; |
432 | 434 |
const double MINOR_LIMIT_FACTOR = 0.1; |
433 | 435 |
const int MIN_MINOR_LIMIT = 3; |
... | ... |
@@ -488,49 +490,49 @@ |
488 | 490 |
} |
489 | 491 |
if (_curr_length == _list_length) goto search_end; |
490 | 492 |
} |
491 | 493 |
} |
492 | 494 |
if (_curr_length == 0) return false; |
493 | 495 |
|
494 | 496 |
search_end: |
495 | 497 |
_minor_count = 1; |
496 | 498 |
_next_arc = e; |
497 | 499 |
return true; |
498 | 500 |
} |
499 | 501 |
|
500 | 502 |
}; //class CandidateListPivotRule |
501 | 503 |
|
502 | 504 |
|
503 | 505 |
// Implementation of the Altering Candidate List pivot rule |
504 | 506 |
class AlteringListPivotRule |
505 | 507 |
{ |
506 | 508 |
private: |
507 | 509 |
|
508 | 510 |
// References to the NetworkSimplex class |
509 | 511 |
const IntVector &_source; |
510 | 512 |
const IntVector &_target; |
511 | 513 |
const CostVector &_cost; |
512 |
const |
|
514 |
const CharVector &_state; |
|
513 | 515 |
const CostVector &_pi; |
514 | 516 |
int &_in_arc; |
515 | 517 |
int _search_arc_num; |
516 | 518 |
|
517 | 519 |
// Pivot rule data |
518 | 520 |
int _block_size, _head_length, _curr_length; |
519 | 521 |
int _next_arc; |
520 | 522 |
IntVector _candidates; |
521 | 523 |
CostVector _cand_cost; |
522 | 524 |
|
523 | 525 |
// Functor class to compare arcs during sort of the candidate list |
524 | 526 |
class SortFunc |
525 | 527 |
{ |
526 | 528 |
private: |
527 | 529 |
const CostVector &_map; |
528 | 530 |
public: |
529 | 531 |
SortFunc(const CostVector &map) : _map(map) {} |
530 | 532 |
bool operator()(int left, int right) { |
531 | 533 |
return _map[left] > _map[right]; |
532 | 534 |
} |
533 | 535 |
}; |
534 | 536 |
|
535 | 537 |
SortFunc _sort_func; |
536 | 538 |
|
... | ... |
@@ -610,51 +612,51 @@ |
610 | 612 |
// Pop the first element of the heap |
611 | 613 |
_in_arc = _candidates[0]; |
612 | 614 |
_next_arc = e; |
613 | 615 |
pop_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
614 | 616 |
_sort_func ); |
615 | 617 |
_curr_length = std::min(_head_length, _curr_length - 1); |
616 | 618 |
return true; |
617 | 619 |
} |
618 | 620 |
|
619 | 621 |
}; //class AlteringListPivotRule |
620 | 622 |
|
621 | 623 |
public: |
622 | 624 |
|
623 | 625 |
/// \brief Constructor. |
624 | 626 |
/// |
625 | 627 |
/// The constructor of the class. |
626 | 628 |
/// |
627 | 629 |
/// \param graph The digraph the algorithm runs on. |
628 | 630 |
/// \param arc_mixing Indicate if the arcs have to be stored in a |
629 | 631 |
/// mixed order in the internal data structure. |
630 | 632 |
/// In special cases, it could lead to better overall performance, |
631 | 633 |
/// but it is usually slower. Therefore it is disabled by default. |
632 | 634 |
NetworkSimplex(const GR& graph, bool arc_mixing = false) : |
633 | 635 |
_graph(graph), _node_id(graph), _arc_id(graph), |
636 |
MAX(std::numeric_limits<Value>::max()), |
|
634 | 637 |
INF(std::numeric_limits<Value>::has_infinity ? |
635 |
std::numeric_limits<Value>::infinity() : |
|
636 |
std::numeric_limits<Value>::max()) |
|
638 |
std::numeric_limits<Value>::infinity() : MAX) |
|
637 | 639 |
{ |
638 | 640 |
// Check the value types |
639 | 641 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
640 | 642 |
"The flow type of NetworkSimplex must be signed"); |
641 | 643 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
642 | 644 |
"The cost type of NetworkSimplex must be signed"); |
643 | 645 |
|
644 | 646 |
// Resize vectors |
645 | 647 |
_node_num = countNodes(_graph); |
646 | 648 |
_arc_num = countArcs(_graph); |
647 | 649 |
int all_node_num = _node_num + 1; |
648 | 650 |
int max_arc_num = _arc_num + 2 * _node_num; |
649 | 651 |
|
650 | 652 |
_source.resize(max_arc_num); |
651 | 653 |
_target.resize(max_arc_num); |
652 | 654 |
|
653 | 655 |
_lower.resize(_arc_num); |
654 | 656 |
_upper.resize(_arc_num); |
655 | 657 |
_cap.resize(max_arc_num); |
656 | 658 |
_cost.resize(max_arc_num); |
657 | 659 |
_supply.resize(all_node_num); |
658 | 660 |
_flow.resize(max_arc_num); |
659 | 661 |
_pi.resize(all_node_num); |
660 | 662 |
|
... | ... |
@@ -999,51 +1001,51 @@ |
999 | 1001 |
} |
1000 | 1002 |
} |
1001 | 1003 |
|
1002 | 1004 |
/// @} |
1003 | 1005 |
|
1004 | 1006 |
private: |
1005 | 1007 |
|
1006 | 1008 |
// Initialize internal data structures |
1007 | 1009 |
bool init() { |
1008 | 1010 |
if (_node_num == 0) return false; |
1009 | 1011 |
|
1010 | 1012 |
// Check the sum of supply values |
1011 | 1013 |
_sum_supply = 0; |
1012 | 1014 |
for (int i = 0; i != _node_num; ++i) { |
1013 | 1015 |
_sum_supply += _supply[i]; |
1014 | 1016 |
} |
1015 | 1017 |
if ( !((_stype == GEQ && _sum_supply <= 0) || |
1016 | 1018 |
(_stype == LEQ && _sum_supply >= 0)) ) return false; |
1017 | 1019 |
|
1018 | 1020 |
// Remove non-zero lower bounds |
1019 | 1021 |
if (_have_lower) { |
1020 | 1022 |
for (int i = 0; i != _arc_num; ++i) { |
1021 | 1023 |
Value c = _lower[i]; |
1022 | 1024 |
if (c >= 0) { |
1023 |
_cap[i] = _upper[i] < |
|
1025 |
_cap[i] = _upper[i] < MAX ? _upper[i] - c : INF; |
|
1024 | 1026 |
} else { |
1025 |
_cap[i] = _upper[i] < |
|
1027 |
_cap[i] = _upper[i] < MAX + c ? _upper[i] - c : INF; |
|
1026 | 1028 |
} |
1027 | 1029 |
_supply[_source[i]] -= c; |
1028 | 1030 |
_supply[_target[i]] += c; |
1029 | 1031 |
} |
1030 | 1032 |
} else { |
1031 | 1033 |
for (int i = 0; i != _arc_num; ++i) { |
1032 | 1034 |
_cap[i] = _upper[i]; |
1033 | 1035 |
} |
1034 | 1036 |
} |
1035 | 1037 |
|
1036 | 1038 |
// Initialize artifical cost |
1037 | 1039 |
Cost ART_COST; |
1038 | 1040 |
if (std::numeric_limits<Cost>::is_exact) { |
1039 | 1041 |
ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
1040 | 1042 |
} else { |
1041 | 1043 |
ART_COST = std::numeric_limits<Cost>::min(); |
1042 | 1044 |
for (int i = 0; i != _arc_num; ++i) { |
1043 | 1045 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
1044 | 1046 |
} |
1045 | 1047 |
ART_COST = (ART_COST + 1) * _node_num; |
1046 | 1048 |
} |
1047 | 1049 |
|
1048 | 1050 |
// Initialize arc maps |
1049 | 1051 |
for (int i = 0; i != _arc_num; ++i) { |
... | ... |
@@ -1193,60 +1195,60 @@ |
1193 | 1195 |
join = u; |
1194 | 1196 |
} |
1195 | 1197 |
|
1196 | 1198 |
// Find the leaving arc of the cycle and returns true if the |
1197 | 1199 |
// leaving arc is not the same as the entering arc |
1198 | 1200 |
bool findLeavingArc() { |
1199 | 1201 |
// Initialize first and second nodes according to the direction |
1200 | 1202 |
// of the cycle |
1201 | 1203 |
if (_state[in_arc] == STATE_LOWER) { |
1202 | 1204 |
first = _source[in_arc]; |
1203 | 1205 |
second = _target[in_arc]; |
1204 | 1206 |
} else { |
1205 | 1207 |
first = _target[in_arc]; |
1206 | 1208 |
second = _source[in_arc]; |
1207 | 1209 |
} |
1208 | 1210 |
delta = _cap[in_arc]; |
1209 | 1211 |
int result = 0; |
1210 | 1212 |
Value d; |
1211 | 1213 |
int e; |
1212 | 1214 |
|
1213 | 1215 |
// Search the cycle along the path form the first node to the root |
1214 | 1216 |
for (int u = first; u != join; u = _parent[u]) { |
1215 | 1217 |
e = _pred[u]; |
1216 | 1218 |
d = _forward[u] ? |
1217 |
_flow[e] : (_cap[e] |
|
1219 |
_flow[e] : (_cap[e] >= MAX ? INF : _cap[e] - _flow[e]); |
|
1218 | 1220 |
if (d < delta) { |
1219 | 1221 |
delta = d; |
1220 | 1222 |
u_out = u; |
1221 | 1223 |
result = 1; |
1222 | 1224 |
} |
1223 | 1225 |
} |
1224 | 1226 |
// Search the cycle along the path form the second node to the root |
1225 | 1227 |
for (int u = second; u != join; u = _parent[u]) { |
1226 | 1228 |
e = _pred[u]; |
1227 | 1229 |
d = _forward[u] ? |
1228 |
(_cap[e] |
|
1230 |
(_cap[e] >= MAX ? INF : _cap[e] - _flow[e]) : _flow[e]; |
|
1229 | 1231 |
if (d <= delta) { |
1230 | 1232 |
delta = d; |
1231 | 1233 |
u_out = u; |
1232 | 1234 |
result = 2; |
1233 | 1235 |
} |
1234 | 1236 |
} |
1235 | 1237 |
|
1236 | 1238 |
if (result == 1) { |
1237 | 1239 |
u_in = first; |
1238 | 1240 |
v_in = second; |
1239 | 1241 |
} else { |
1240 | 1242 |
u_in = second; |
1241 | 1243 |
v_in = first; |
1242 | 1244 |
} |
1243 | 1245 |
return result != 0; |
1244 | 1246 |
} |
1245 | 1247 |
|
1246 | 1248 |
// Change _flow and _state vectors |
1247 | 1249 |
void changeFlow(bool change) { |
1248 | 1250 |
// Augment along the cycle |
1249 | 1251 |
if (delta > 0) { |
1250 | 1252 |
Value val = _state[in_arc] * delta; |
1251 | 1253 |
_flow[in_arc] += val; |
1252 | 1254 |
for (int u = _source[in_arc]; u != join; u = _parent[u]) { |
... | ... |
@@ -1403,49 +1405,49 @@ |
1403 | 1405 |
// Select the pivot rule implementation |
1404 | 1406 |
switch (pivot_rule) { |
1405 | 1407 |
case FIRST_ELIGIBLE: |
1406 | 1408 |
return start<FirstEligiblePivotRule>(); |
1407 | 1409 |
case BEST_ELIGIBLE: |
1408 | 1410 |
return start<BestEligiblePivotRule>(); |
1409 | 1411 |
case BLOCK_SEARCH: |
1410 | 1412 |
return start<BlockSearchPivotRule>(); |
1411 | 1413 |
case CANDIDATE_LIST: |
1412 | 1414 |
return start<CandidateListPivotRule>(); |
1413 | 1415 |
case ALTERING_LIST: |
1414 | 1416 |
return start<AlteringListPivotRule>(); |
1415 | 1417 |
} |
1416 | 1418 |
return INFEASIBLE; // avoid warning |
1417 | 1419 |
} |
1418 | 1420 |
|
1419 | 1421 |
template <typename PivotRuleImpl> |
1420 | 1422 |
ProblemType start() { |
1421 | 1423 |
PivotRuleImpl pivot(*this); |
1422 | 1424 |
|
1423 | 1425 |
// Execute the Network Simplex algorithm |
1424 | 1426 |
while (pivot.findEnteringArc()) { |
1425 | 1427 |
findJoinNode(); |
1426 | 1428 |
bool change = findLeavingArc(); |
1427 |
if (delta >= |
|
1429 |
if (delta >= MAX) return UNBOUNDED; |
|
1428 | 1430 |
changeFlow(change); |
1429 | 1431 |
if (change) { |
1430 | 1432 |
updateTreeStructure(); |
1431 | 1433 |
updatePotential(); |
1432 | 1434 |
} |
1433 | 1435 |
} |
1434 | 1436 |
|
1435 | 1437 |
// Check feasibility |
1436 | 1438 |
for (int e = _search_arc_num; e != _all_arc_num; ++e) { |
1437 | 1439 |
if (_flow[e] != 0) return INFEASIBLE; |
1438 | 1440 |
} |
1439 | 1441 |
|
1440 | 1442 |
// Transform the solution and the supply map to the original form |
1441 | 1443 |
if (_have_lower) { |
1442 | 1444 |
for (int i = 0; i != _arc_num; ++i) { |
1443 | 1445 |
Value c = _lower[i]; |
1444 | 1446 |
if (c != 0) { |
1445 | 1447 |
_flow[i] += c; |
1446 | 1448 |
_supply[_source[i]] += c; |
1447 | 1449 |
_supply[_target[i]] -= c; |
1448 | 1450 |
} |
1449 | 1451 |
} |
1450 | 1452 |
} |
1451 | 1453 |
|
0 comments (0 inline)