0
2
0
... | ... |
@@ -124,25 +124,25 @@ |
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 |
|
... | ... |
@@ -187,61 +187,63 @@ |
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; |
... | ... |
@@ -678,101 +680,103 @@ |
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 |
|
... | ... |
@@ -780,67 +784,72 @@ |
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; |
... | ... |
@@ -155,25 +155,25 @@ |
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 |
|
... | ... |
@@ -203,55 +203,57 @@ |
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), |
... | ... |
@@ -285,25 +287,25 @@ |
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 |
{} |
... | ... |
@@ -324,25 +326,25 @@ |
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) : |
... | ... |
@@ -397,25 +399,25 @@ |
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 |
|
... | ... |
@@ -500,25 +502,25 @@ |
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 |
... | ... |
@@ -622,27 +624,27 @@ |
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; |
... | ... |
@@ -1011,27 +1013,27 @@ |
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; |
... | ... |
@@ -1205,36 +1207,36 @@ |
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; |
... | ... |
@@ -1415,25 +1417,25 @@ |
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 |
|
0 comments (0 inline)