1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2009 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
9 | * Permission to use, modify and distribute this software is granted |
---|
10 | * provided that this copyright notice appears in all copies. For |
---|
11 | * precise terms see the accompanying LICENSE file. |
---|
12 | * |
---|
13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
14 | * express or implied, and with no claim as to its suitability for any |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef LEMON_NETWORK_SIMPLEX_H |
---|
20 | #define LEMON_NETWORK_SIMPLEX_H |
---|
21 | |
---|
22 | /// \ingroup min_cost_flow_algs |
---|
23 | /// |
---|
24 | /// \file |
---|
25 | /// \brief Network Simplex algorithm for finding a minimum cost flow. |
---|
26 | |
---|
27 | #include <vector> |
---|
28 | #include <limits> |
---|
29 | #include <algorithm> |
---|
30 | |
---|
31 | #include <lemon/core.h> |
---|
32 | #include <lemon/math.h> |
---|
33 | |
---|
34 | namespace lemon { |
---|
35 | |
---|
36 | /// \addtogroup min_cost_flow_algs |
---|
37 | /// @{ |
---|
38 | |
---|
39 | /// \brief Implementation of the primal Network Simplex algorithm |
---|
40 | /// for finding a \ref min_cost_flow "minimum cost flow". |
---|
41 | /// |
---|
42 | /// \ref NetworkSimplex implements the primal Network Simplex algorithm |
---|
43 | /// for finding a \ref min_cost_flow "minimum cost flow". |
---|
44 | /// This algorithm is a specialized version of the linear programming |
---|
45 | /// simplex method directly for the minimum cost flow problem. |
---|
46 | /// It is one of the most efficient solution methods. |
---|
47 | /// |
---|
48 | /// In general this class is the fastest implementation available |
---|
49 | /// in LEMON for the minimum cost flow problem. |
---|
50 | /// Moreover it supports both directions of the supply/demand inequality |
---|
51 | /// constraints. For more information see \ref SupplyType. |
---|
52 | /// |
---|
53 | /// Most of the parameters of the problem (except for the digraph) |
---|
54 | /// can be given using separate functions, and the algorithm can be |
---|
55 | /// executed using the \ref run() function. If some parameters are not |
---|
56 | /// specified, then default values will be used. |
---|
57 | /// |
---|
58 | /// \tparam GR The digraph type the algorithm runs on. |
---|
59 | /// \tparam V The value type used for flow amounts, capacity bounds |
---|
60 | /// and supply values in the algorithm. By default it is \c int. |
---|
61 | /// \tparam C The value type used for costs and potentials in the |
---|
62 | /// algorithm. By default it is the same as \c V. |
---|
63 | /// |
---|
64 | /// \warning Both value types must be signed and all input data must |
---|
65 | /// be integer. |
---|
66 | /// |
---|
67 | /// \note %NetworkSimplex provides five different pivot rule |
---|
68 | /// implementations, from which the most efficient one is used |
---|
69 | /// by default. For more information see \ref PivotRule. |
---|
70 | template <typename GR, typename V = int, typename C = V> |
---|
71 | class NetworkSimplex |
---|
72 | { |
---|
73 | public: |
---|
74 | |
---|
75 | /// The type of the flow amounts, capacity bounds and supply values |
---|
76 | typedef V Value; |
---|
77 | /// The type of the arc costs |
---|
78 | typedef C Cost; |
---|
79 | |
---|
80 | public: |
---|
81 | |
---|
82 | /// \brief Problem type constants for the \c run() function. |
---|
83 | /// |
---|
84 | /// Enum type containing the problem type constants that can be |
---|
85 | /// returned by the \ref run() function of the algorithm. |
---|
86 | enum ProblemType { |
---|
87 | /// The problem has no feasible solution (flow). |
---|
88 | INFEASIBLE, |
---|
89 | /// The problem has optimal solution (i.e. it is feasible and |
---|
90 | /// bounded), and the algorithm has found optimal flow and node |
---|
91 | /// potentials (primal and dual solutions). |
---|
92 | OPTIMAL, |
---|
93 | /// The objective function of the problem is unbounded, i.e. |
---|
94 | /// there is a directed cycle having negative total cost and |
---|
95 | /// infinite upper bound. |
---|
96 | UNBOUNDED |
---|
97 | }; |
---|
98 | |
---|
99 | /// \brief Constants for selecting the type of the supply constraints. |
---|
100 | /// |
---|
101 | /// Enum type containing constants for selecting the supply type, |
---|
102 | /// i.e. the direction of the inequalities in the supply/demand |
---|
103 | /// constraints of the \ref min_cost_flow "minimum cost flow problem". |
---|
104 | /// |
---|
105 | /// The default supply type is \c GEQ, the \c LEQ type can be |
---|
106 | /// selected using \ref supplyType(). |
---|
107 | /// The equality form is a special case of both supply types. |
---|
108 | enum SupplyType { |
---|
109 | /// This option means that there are <em>"greater or equal"</em> |
---|
110 | /// supply/demand constraints in the definition of the problem. |
---|
111 | GEQ, |
---|
112 | /// This option means that there are <em>"less or equal"</em> |
---|
113 | /// supply/demand constraints in the definition of the problem. |
---|
114 | LEQ |
---|
115 | }; |
---|
116 | |
---|
117 | /// \brief Constants for selecting the pivot rule. |
---|
118 | /// |
---|
119 | /// Enum type containing constants for selecting the pivot rule for |
---|
120 | /// the \ref run() function. |
---|
121 | /// |
---|
122 | /// \ref NetworkSimplex provides five different pivot rule |
---|
123 | /// implementations that significantly affect the running time |
---|
124 | /// of the algorithm. |
---|
125 | /// By default \ref BLOCK_SEARCH "Block Search" is used, which |
---|
126 | /// proved to be the most efficient and the most robust on various |
---|
127 | /// test inputs according to our benchmark tests. |
---|
128 | /// However another pivot rule can be selected using the \ref run() |
---|
129 | /// function with the proper parameter. |
---|
130 | enum PivotRule { |
---|
131 | |
---|
132 | /// The First Eligible pivot rule. |
---|
133 | /// The next eligible arc is selected in a wraparound fashion |
---|
134 | /// in every iteration. |
---|
135 | FIRST_ELIGIBLE, |
---|
136 | |
---|
137 | /// The Best Eligible pivot rule. |
---|
138 | /// The best eligible arc is selected in every iteration. |
---|
139 | BEST_ELIGIBLE, |
---|
140 | |
---|
141 | /// The Block Search pivot rule. |
---|
142 | /// A specified number of arcs are examined in every iteration |
---|
143 | /// in a wraparound fashion and the best eligible arc is selected |
---|
144 | /// from this block. |
---|
145 | BLOCK_SEARCH, |
---|
146 | |
---|
147 | /// The Candidate List pivot rule. |
---|
148 | /// In a major iteration a candidate list is built from eligible arcs |
---|
149 | /// in a wraparound fashion and in the following minor iterations |
---|
150 | /// the best eligible arc is selected from this list. |
---|
151 | CANDIDATE_LIST, |
---|
152 | |
---|
153 | /// The Altering Candidate List pivot rule. |
---|
154 | /// It is a modified version of the Candidate List method. |
---|
155 | /// It keeps only the several best eligible arcs from the former |
---|
156 | /// candidate list and extends this list in every iteration. |
---|
157 | ALTERING_LIST |
---|
158 | }; |
---|
159 | |
---|
160 | private: |
---|
161 | |
---|
162 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
---|
163 | |
---|
164 | typedef std::vector<int> IntVector; |
---|
165 | typedef std::vector<bool> BoolVector; |
---|
166 | typedef std::vector<Value> ValueVector; |
---|
167 | typedef std::vector<Cost> CostVector; |
---|
168 | |
---|
169 | // State constants for arcs |
---|
170 | enum ArcStateEnum { |
---|
171 | STATE_UPPER = -1, |
---|
172 | STATE_TREE = 0, |
---|
173 | STATE_LOWER = 1 |
---|
174 | }; |
---|
175 | |
---|
176 | private: |
---|
177 | |
---|
178 | // Data related to the underlying digraph |
---|
179 | const GR &_graph; |
---|
180 | int _node_num; |
---|
181 | int _arc_num; |
---|
182 | int _all_arc_num; |
---|
183 | int _search_arc_num; |
---|
184 | |
---|
185 | // Parameters of the problem |
---|
186 | bool _have_lower; |
---|
187 | SupplyType _stype; |
---|
188 | Value _sum_supply; |
---|
189 | |
---|
190 | // Data structures for storing the digraph |
---|
191 | IntNodeMap _node_id; |
---|
192 | IntArcMap _arc_id; |
---|
193 | IntVector _source; |
---|
194 | IntVector _target; |
---|
195 | |
---|
196 | // Node and arc data |
---|
197 | ValueVector _lower; |
---|
198 | ValueVector _upper; |
---|
199 | ValueVector _cap; |
---|
200 | CostVector _cost; |
---|
201 | ValueVector _supply; |
---|
202 | ValueVector _flow; |
---|
203 | CostVector _pi; |
---|
204 | |
---|
205 | // Data for storing the spanning tree structure |
---|
206 | IntVector _parent; |
---|
207 | IntVector _pred; |
---|
208 | IntVector _thread; |
---|
209 | IntVector _rev_thread; |
---|
210 | IntVector _succ_num; |
---|
211 | IntVector _last_succ; |
---|
212 | IntVector _dirty_revs; |
---|
213 | BoolVector _forward; |
---|
214 | IntVector _state; |
---|
215 | int _root; |
---|
216 | |
---|
217 | // Temporary data used in the current pivot iteration |
---|
218 | int in_arc, join, u_in, v_in, u_out, v_out; |
---|
219 | int first, second, right, last; |
---|
220 | int stem, par_stem, new_stem; |
---|
221 | Value delta; |
---|
222 | |
---|
223 | public: |
---|
224 | |
---|
225 | /// \brief Constant for infinite upper bounds (capacities). |
---|
226 | /// |
---|
227 | /// Constant for infinite upper bounds (capacities). |
---|
228 | /// It is \c std::numeric_limits<Value>::infinity() if available, |
---|
229 | /// \c std::numeric_limits<Value>::max() otherwise. |
---|
230 | const Value INF; |
---|
231 | |
---|
232 | private: |
---|
233 | |
---|
234 | // Implementation of the First Eligible pivot rule |
---|
235 | class FirstEligiblePivotRule |
---|
236 | { |
---|
237 | private: |
---|
238 | |
---|
239 | // References to the NetworkSimplex class |
---|
240 | const IntVector &_source; |
---|
241 | const IntVector &_target; |
---|
242 | const CostVector &_cost; |
---|
243 | const IntVector &_state; |
---|
244 | const CostVector &_pi; |
---|
245 | int &_in_arc; |
---|
246 | int _search_arc_num; |
---|
247 | |
---|
248 | // Pivot rule data |
---|
249 | int _next_arc; |
---|
250 | |
---|
251 | public: |
---|
252 | |
---|
253 | // Constructor |
---|
254 | FirstEligiblePivotRule(NetworkSimplex &ns) : |
---|
255 | _source(ns._source), _target(ns._target), |
---|
256 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), |
---|
257 | _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
---|
258 | _next_arc(0) |
---|
259 | {} |
---|
260 | |
---|
261 | // Find next entering arc |
---|
262 | bool findEnteringArc() { |
---|
263 | Cost c; |
---|
264 | for (int e = _next_arc; e < _search_arc_num; ++e) { |
---|
265 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
266 | if (c < 0) { |
---|
267 | _in_arc = e; |
---|
268 | _next_arc = e + 1; |
---|
269 | return true; |
---|
270 | } |
---|
271 | } |
---|
272 | for (int e = 0; e < _next_arc; ++e) { |
---|
273 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
274 | if (c < 0) { |
---|
275 | _in_arc = e; |
---|
276 | _next_arc = e + 1; |
---|
277 | return true; |
---|
278 | } |
---|
279 | } |
---|
280 | return false; |
---|
281 | } |
---|
282 | |
---|
283 | }; //class FirstEligiblePivotRule |
---|
284 | |
---|
285 | |
---|
286 | // Implementation of the Best Eligible pivot rule |
---|
287 | class BestEligiblePivotRule |
---|
288 | { |
---|
289 | private: |
---|
290 | |
---|
291 | // References to the NetworkSimplex class |
---|
292 | const IntVector &_source; |
---|
293 | const IntVector &_target; |
---|
294 | const CostVector &_cost; |
---|
295 | const IntVector &_state; |
---|
296 | const CostVector &_pi; |
---|
297 | int &_in_arc; |
---|
298 | int _search_arc_num; |
---|
299 | |
---|
300 | public: |
---|
301 | |
---|
302 | // Constructor |
---|
303 | BestEligiblePivotRule(NetworkSimplex &ns) : |
---|
304 | _source(ns._source), _target(ns._target), |
---|
305 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), |
---|
306 | _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num) |
---|
307 | {} |
---|
308 | |
---|
309 | // Find next entering arc |
---|
310 | bool findEnteringArc() { |
---|
311 | Cost c, min = 0; |
---|
312 | for (int e = 0; e < _search_arc_num; ++e) { |
---|
313 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
314 | if (c < min) { |
---|
315 | min = c; |
---|
316 | _in_arc = e; |
---|
317 | } |
---|
318 | } |
---|
319 | return min < 0; |
---|
320 | } |
---|
321 | |
---|
322 | }; //class BestEligiblePivotRule |
---|
323 | |
---|
324 | |
---|
325 | // Implementation of the Block Search pivot rule |
---|
326 | class BlockSearchPivotRule |
---|
327 | { |
---|
328 | private: |
---|
329 | |
---|
330 | // References to the NetworkSimplex class |
---|
331 | const IntVector &_source; |
---|
332 | const IntVector &_target; |
---|
333 | const CostVector &_cost; |
---|
334 | const IntVector &_state; |
---|
335 | const CostVector &_pi; |
---|
336 | int &_in_arc; |
---|
337 | int _search_arc_num; |
---|
338 | |
---|
339 | // Pivot rule data |
---|
340 | int _block_size; |
---|
341 | int _next_arc; |
---|
342 | |
---|
343 | public: |
---|
344 | |
---|
345 | // Constructor |
---|
346 | BlockSearchPivotRule(NetworkSimplex &ns) : |
---|
347 | _source(ns._source), _target(ns._target), |
---|
348 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), |
---|
349 | _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
---|
350 | _next_arc(0) |
---|
351 | { |
---|
352 | // The main parameters of the pivot rule |
---|
353 | const double BLOCK_SIZE_FACTOR = 0.5; |
---|
354 | const int MIN_BLOCK_SIZE = 10; |
---|
355 | |
---|
356 | _block_size = std::max( int(BLOCK_SIZE_FACTOR * |
---|
357 | std::sqrt(double(_search_arc_num))), |
---|
358 | MIN_BLOCK_SIZE ); |
---|
359 | } |
---|
360 | |
---|
361 | // Find next entering arc |
---|
362 | bool findEnteringArc() { |
---|
363 | Cost c, min = 0; |
---|
364 | int cnt = _block_size; |
---|
365 | int e, min_arc = _next_arc; |
---|
366 | for (e = _next_arc; e < _search_arc_num; ++e) { |
---|
367 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
368 | if (c < min) { |
---|
369 | min = c; |
---|
370 | min_arc = e; |
---|
371 | } |
---|
372 | if (--cnt == 0) { |
---|
373 | if (min < 0) break; |
---|
374 | cnt = _block_size; |
---|
375 | } |
---|
376 | } |
---|
377 | if (min == 0 || cnt > 0) { |
---|
378 | for (e = 0; e < _next_arc; ++e) { |
---|
379 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
380 | if (c < min) { |
---|
381 | min = c; |
---|
382 | min_arc = e; |
---|
383 | } |
---|
384 | if (--cnt == 0) { |
---|
385 | if (min < 0) break; |
---|
386 | cnt = _block_size; |
---|
387 | } |
---|
388 | } |
---|
389 | } |
---|
390 | if (min >= 0) return false; |
---|
391 | _in_arc = min_arc; |
---|
392 | _next_arc = e; |
---|
393 | return true; |
---|
394 | } |
---|
395 | |
---|
396 | }; //class BlockSearchPivotRule |
---|
397 | |
---|
398 | |
---|
399 | // Implementation of the Candidate List pivot rule |
---|
400 | class CandidateListPivotRule |
---|
401 | { |
---|
402 | private: |
---|
403 | |
---|
404 | // References to the NetworkSimplex class |
---|
405 | const IntVector &_source; |
---|
406 | const IntVector &_target; |
---|
407 | const CostVector &_cost; |
---|
408 | const IntVector &_state; |
---|
409 | const CostVector &_pi; |
---|
410 | int &_in_arc; |
---|
411 | int _search_arc_num; |
---|
412 | |
---|
413 | // Pivot rule data |
---|
414 | IntVector _candidates; |
---|
415 | int _list_length, _minor_limit; |
---|
416 | int _curr_length, _minor_count; |
---|
417 | int _next_arc; |
---|
418 | |
---|
419 | public: |
---|
420 | |
---|
421 | /// Constructor |
---|
422 | CandidateListPivotRule(NetworkSimplex &ns) : |
---|
423 | _source(ns._source), _target(ns._target), |
---|
424 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), |
---|
425 | _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
---|
426 | _next_arc(0) |
---|
427 | { |
---|
428 | // The main parameters of the pivot rule |
---|
429 | const double LIST_LENGTH_FACTOR = 1.0; |
---|
430 | const int MIN_LIST_LENGTH = 10; |
---|
431 | const double MINOR_LIMIT_FACTOR = 0.1; |
---|
432 | const int MIN_MINOR_LIMIT = 3; |
---|
433 | |
---|
434 | _list_length = std::max( int(LIST_LENGTH_FACTOR * |
---|
435 | std::sqrt(double(_search_arc_num))), |
---|
436 | MIN_LIST_LENGTH ); |
---|
437 | _minor_limit = std::max( int(MINOR_LIMIT_FACTOR * _list_length), |
---|
438 | MIN_MINOR_LIMIT ); |
---|
439 | _curr_length = _minor_count = 0; |
---|
440 | _candidates.resize(_list_length); |
---|
441 | } |
---|
442 | |
---|
443 | /// Find next entering arc |
---|
444 | bool findEnteringArc() { |
---|
445 | Cost min, c; |
---|
446 | int e, min_arc = _next_arc; |
---|
447 | if (_curr_length > 0 && _minor_count < _minor_limit) { |
---|
448 | // Minor iteration: select the best eligible arc from the |
---|
449 | // current candidate list |
---|
450 | ++_minor_count; |
---|
451 | min = 0; |
---|
452 | for (int i = 0; i < _curr_length; ++i) { |
---|
453 | e = _candidates[i]; |
---|
454 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
455 | if (c < min) { |
---|
456 | min = c; |
---|
457 | min_arc = e; |
---|
458 | } |
---|
459 | if (c >= 0) { |
---|
460 | _candidates[i--] = _candidates[--_curr_length]; |
---|
461 | } |
---|
462 | } |
---|
463 | if (min < 0) { |
---|
464 | _in_arc = min_arc; |
---|
465 | return true; |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | // Major iteration: build a new candidate list |
---|
470 | min = 0; |
---|
471 | _curr_length = 0; |
---|
472 | for (e = _next_arc; e < _search_arc_num; ++e) { |
---|
473 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
474 | if (c < 0) { |
---|
475 | _candidates[_curr_length++] = e; |
---|
476 | if (c < min) { |
---|
477 | min = c; |
---|
478 | min_arc = e; |
---|
479 | } |
---|
480 | if (_curr_length == _list_length) break; |
---|
481 | } |
---|
482 | } |
---|
483 | if (_curr_length < _list_length) { |
---|
484 | for (e = 0; e < _next_arc; ++e) { |
---|
485 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
486 | if (c < 0) { |
---|
487 | _candidates[_curr_length++] = e; |
---|
488 | if (c < min) { |
---|
489 | min = c; |
---|
490 | min_arc = e; |
---|
491 | } |
---|
492 | if (_curr_length == _list_length) break; |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | if (_curr_length == 0) return false; |
---|
497 | _minor_count = 1; |
---|
498 | _in_arc = min_arc; |
---|
499 | _next_arc = e; |
---|
500 | return true; |
---|
501 | } |
---|
502 | |
---|
503 | }; //class CandidateListPivotRule |
---|
504 | |
---|
505 | |
---|
506 | // Implementation of the Altering Candidate List pivot rule |
---|
507 | class AlteringListPivotRule |
---|
508 | { |
---|
509 | private: |
---|
510 | |
---|
511 | // References to the NetworkSimplex class |
---|
512 | const IntVector &_source; |
---|
513 | const IntVector &_target; |
---|
514 | const CostVector &_cost; |
---|
515 | const IntVector &_state; |
---|
516 | const CostVector &_pi; |
---|
517 | int &_in_arc; |
---|
518 | int _search_arc_num; |
---|
519 | |
---|
520 | // Pivot rule data |
---|
521 | int _block_size, _head_length, _curr_length; |
---|
522 | int _next_arc; |
---|
523 | IntVector _candidates; |
---|
524 | CostVector _cand_cost; |
---|
525 | |
---|
526 | // Functor class to compare arcs during sort of the candidate list |
---|
527 | class SortFunc |
---|
528 | { |
---|
529 | private: |
---|
530 | const CostVector &_map; |
---|
531 | public: |
---|
532 | SortFunc(const CostVector &map) : _map(map) {} |
---|
533 | bool operator()(int left, int right) { |
---|
534 | return _map[left] > _map[right]; |
---|
535 | } |
---|
536 | }; |
---|
537 | |
---|
538 | SortFunc _sort_func; |
---|
539 | |
---|
540 | public: |
---|
541 | |
---|
542 | // Constructor |
---|
543 | AlteringListPivotRule(NetworkSimplex &ns) : |
---|
544 | _source(ns._source), _target(ns._target), |
---|
545 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), |
---|
546 | _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
---|
547 | _next_arc(0), _cand_cost(ns._search_arc_num), _sort_func(_cand_cost) |
---|
548 | { |
---|
549 | // The main parameters of the pivot rule |
---|
550 | const double BLOCK_SIZE_FACTOR = 1.5; |
---|
551 | const int MIN_BLOCK_SIZE = 10; |
---|
552 | const double HEAD_LENGTH_FACTOR = 0.1; |
---|
553 | const int MIN_HEAD_LENGTH = 3; |
---|
554 | |
---|
555 | _block_size = std::max( int(BLOCK_SIZE_FACTOR * |
---|
556 | std::sqrt(double(_search_arc_num))), |
---|
557 | MIN_BLOCK_SIZE ); |
---|
558 | _head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size), |
---|
559 | MIN_HEAD_LENGTH ); |
---|
560 | _candidates.resize(_head_length + _block_size); |
---|
561 | _curr_length = 0; |
---|
562 | } |
---|
563 | |
---|
564 | // Find next entering arc |
---|
565 | bool findEnteringArc() { |
---|
566 | // Check the current candidate list |
---|
567 | int e; |
---|
568 | for (int i = 0; i < _curr_length; ++i) { |
---|
569 | e = _candidates[i]; |
---|
570 | _cand_cost[e] = _state[e] * |
---|
571 | (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
572 | if (_cand_cost[e] >= 0) { |
---|
573 | _candidates[i--] = _candidates[--_curr_length]; |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | // Extend the list |
---|
578 | int cnt = _block_size; |
---|
579 | int last_arc = 0; |
---|
580 | int limit = _head_length; |
---|
581 | |
---|
582 | for (int e = _next_arc; e < _search_arc_num; ++e) { |
---|
583 | _cand_cost[e] = _state[e] * |
---|
584 | (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
585 | if (_cand_cost[e] < 0) { |
---|
586 | _candidates[_curr_length++] = e; |
---|
587 | last_arc = e; |
---|
588 | } |
---|
589 | if (--cnt == 0) { |
---|
590 | if (_curr_length > limit) break; |
---|
591 | limit = 0; |
---|
592 | cnt = _block_size; |
---|
593 | } |
---|
594 | } |
---|
595 | if (_curr_length <= limit) { |
---|
596 | for (int e = 0; e < _next_arc; ++e) { |
---|
597 | _cand_cost[e] = _state[e] * |
---|
598 | (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
---|
599 | if (_cand_cost[e] < 0) { |
---|
600 | _candidates[_curr_length++] = e; |
---|
601 | last_arc = e; |
---|
602 | } |
---|
603 | if (--cnt == 0) { |
---|
604 | if (_curr_length > limit) break; |
---|
605 | limit = 0; |
---|
606 | cnt = _block_size; |
---|
607 | } |
---|
608 | } |
---|
609 | } |
---|
610 | if (_curr_length == 0) return false; |
---|
611 | _next_arc = last_arc + 1; |
---|
612 | |
---|
613 | // Make heap of the candidate list (approximating a partial sort) |
---|
614 | make_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
---|
615 | _sort_func ); |
---|
616 | |
---|
617 | // Pop the first element of the heap |
---|
618 | _in_arc = _candidates[0]; |
---|
619 | pop_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
---|
620 | _sort_func ); |
---|
621 | _curr_length = std::min(_head_length, _curr_length - 1); |
---|
622 | return true; |
---|
623 | } |
---|
624 | |
---|
625 | }; //class AlteringListPivotRule |
---|
626 | |
---|
627 | public: |
---|
628 | |
---|
629 | /// \brief Constructor. |
---|
630 | /// |
---|
631 | /// The constructor of the class. |
---|
632 | /// |
---|
633 | /// \param graph The digraph the algorithm runs on. |
---|
634 | NetworkSimplex(const GR& graph) : |
---|
635 | _graph(graph), _node_id(graph), _arc_id(graph), |
---|
636 | INF(std::numeric_limits<Value>::has_infinity ? |
---|
637 | std::numeric_limits<Value>::infinity() : |
---|
638 | std::numeric_limits<Value>::max()) |
---|
639 | { |
---|
640 | // Check the value types |
---|
641 | LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
---|
642 | "The flow type of NetworkSimplex must be signed"); |
---|
643 | LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
---|
644 | "The cost type of NetworkSimplex must be signed"); |
---|
645 | |
---|
646 | // Resize vectors |
---|
647 | _node_num = countNodes(_graph); |
---|
648 | _arc_num = countArcs(_graph); |
---|
649 | int all_node_num = _node_num + 1; |
---|
650 | int max_arc_num = _arc_num + 2 * _node_num; |
---|
651 | |
---|
652 | _source.resize(max_arc_num); |
---|
653 | _target.resize(max_arc_num); |
---|
654 | |
---|
655 | _lower.resize(_arc_num); |
---|
656 | _upper.resize(_arc_num); |
---|
657 | _cap.resize(max_arc_num); |
---|
658 | _cost.resize(max_arc_num); |
---|
659 | _supply.resize(all_node_num); |
---|
660 | _flow.resize(max_arc_num); |
---|
661 | _pi.resize(all_node_num); |
---|
662 | |
---|
663 | _parent.resize(all_node_num); |
---|
664 | _pred.resize(all_node_num); |
---|
665 | _forward.resize(all_node_num); |
---|
666 | _thread.resize(all_node_num); |
---|
667 | _rev_thread.resize(all_node_num); |
---|
668 | _succ_num.resize(all_node_num); |
---|
669 | _last_succ.resize(all_node_num); |
---|
670 | _state.resize(max_arc_num); |
---|
671 | |
---|
672 | // Copy the graph (store the arcs in a mixed order) |
---|
673 | int i = 0; |
---|
674 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
---|
675 | _node_id[n] = i; |
---|
676 | } |
---|
677 | int k = std::max(int(std::sqrt(double(_arc_num))), 10); |
---|
678 | i = 0; |
---|
679 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
680 | _arc_id[a] = i; |
---|
681 | _source[i] = _node_id[_graph.source(a)]; |
---|
682 | _target[i] = _node_id[_graph.target(a)]; |
---|
683 | if ((i += k) >= _arc_num) i = (i % k) + 1; |
---|
684 | } |
---|
685 | |
---|
686 | // Reset parameters |
---|
687 | reset(); |
---|
688 | } |
---|
689 | |
---|
690 | /// \name Parameters |
---|
691 | /// The parameters of the algorithm can be specified using these |
---|
692 | /// functions. |
---|
693 | |
---|
694 | /// @{ |
---|
695 | |
---|
696 | /// \brief Set the lower bounds on the arcs. |
---|
697 | /// |
---|
698 | /// This function sets the lower bounds on the arcs. |
---|
699 | /// If it is not used before calling \ref run(), the lower bounds |
---|
700 | /// will be set to zero on all arcs. |
---|
701 | /// |
---|
702 | /// \param map An arc map storing the lower bounds. |
---|
703 | /// Its \c Value type must be convertible to the \c Value type |
---|
704 | /// of the algorithm. |
---|
705 | /// |
---|
706 | /// \return <tt>(*this)</tt> |
---|
707 | template <typename LowerMap> |
---|
708 | NetworkSimplex& lowerMap(const LowerMap& map) { |
---|
709 | _have_lower = true; |
---|
710 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
711 | _lower[_arc_id[a]] = map[a]; |
---|
712 | } |
---|
713 | return *this; |
---|
714 | } |
---|
715 | |
---|
716 | /// \brief Set the upper bounds (capacities) on the arcs. |
---|
717 | /// |
---|
718 | /// This function sets the upper bounds (capacities) on the arcs. |
---|
719 | /// If it is not used before calling \ref run(), the upper bounds |
---|
720 | /// will be set to \ref INF on all arcs (i.e. the flow value will be |
---|
721 | /// unbounded from above on each arc). |
---|
722 | /// |
---|
723 | /// \param map An arc map storing the upper bounds. |
---|
724 | /// Its \c Value type must be convertible to the \c Value type |
---|
725 | /// of the algorithm. |
---|
726 | /// |
---|
727 | /// \return <tt>(*this)</tt> |
---|
728 | template<typename UpperMap> |
---|
729 | NetworkSimplex& upperMap(const UpperMap& map) { |
---|
730 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
731 | _upper[_arc_id[a]] = map[a]; |
---|
732 | } |
---|
733 | return *this; |
---|
734 | } |
---|
735 | |
---|
736 | /// \brief Set the costs of the arcs. |
---|
737 | /// |
---|
738 | /// This function sets the costs of the arcs. |
---|
739 | /// If it is not used before calling \ref run(), the costs |
---|
740 | /// will be set to \c 1 on all arcs. |
---|
741 | /// |
---|
742 | /// \param map An arc map storing the costs. |
---|
743 | /// Its \c Value type must be convertible to the \c Cost type |
---|
744 | /// of the algorithm. |
---|
745 | /// |
---|
746 | /// \return <tt>(*this)</tt> |
---|
747 | template<typename CostMap> |
---|
748 | NetworkSimplex& costMap(const CostMap& map) { |
---|
749 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
750 | _cost[_arc_id[a]] = map[a]; |
---|
751 | } |
---|
752 | return *this; |
---|
753 | } |
---|
754 | |
---|
755 | /// \brief Set the supply values of the nodes. |
---|
756 | /// |
---|
757 | /// This function sets the supply values of the nodes. |
---|
758 | /// If neither this function nor \ref stSupply() is used before |
---|
759 | /// calling \ref run(), the supply of each node will be set to zero. |
---|
760 | /// |
---|
761 | /// \param map A node map storing the supply values. |
---|
762 | /// Its \c Value type must be convertible to the \c Value type |
---|
763 | /// of the algorithm. |
---|
764 | /// |
---|
765 | /// \return <tt>(*this)</tt> |
---|
766 | template<typename SupplyMap> |
---|
767 | NetworkSimplex& supplyMap(const SupplyMap& map) { |
---|
768 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
769 | _supply[_node_id[n]] = map[n]; |
---|
770 | } |
---|
771 | return *this; |
---|
772 | } |
---|
773 | |
---|
774 | /// \brief Set single source and target nodes and a supply value. |
---|
775 | /// |
---|
776 | /// This function sets a single source node and a single target node |
---|
777 | /// and the required flow value. |
---|
778 | /// If neither this function nor \ref supplyMap() is used before |
---|
779 | /// calling \ref run(), the supply of each node will be set to zero. |
---|
780 | /// |
---|
781 | /// Using this function has the same effect as using \ref supplyMap() |
---|
782 | /// with such a map in which \c k is assigned to \c s, \c -k is |
---|
783 | /// assigned to \c t and all other nodes have zero supply value. |
---|
784 | /// |
---|
785 | /// \param s The source node. |
---|
786 | /// \param t The target node. |
---|
787 | /// \param k The required amount of flow from node \c s to node \c t |
---|
788 | /// (i.e. the supply of \c s and the demand of \c t). |
---|
789 | /// |
---|
790 | /// \return <tt>(*this)</tt> |
---|
791 | NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) { |
---|
792 | for (int i = 0; i != _node_num; ++i) { |
---|
793 | _supply[i] = 0; |
---|
794 | } |
---|
795 | _supply[_node_id[s]] = k; |
---|
796 | _supply[_node_id[t]] = -k; |
---|
797 | return *this; |
---|
798 | } |
---|
799 | |
---|
800 | /// \brief Set the type of the supply constraints. |
---|
801 | /// |
---|
802 | /// This function sets the type of the supply/demand constraints. |
---|
803 | /// If it is not used before calling \ref run(), the \ref GEQ supply |
---|
804 | /// type will be used. |
---|
805 | /// |
---|
806 | /// For more information see \ref SupplyType. |
---|
807 | /// |
---|
808 | /// \return <tt>(*this)</tt> |
---|
809 | NetworkSimplex& supplyType(SupplyType supply_type) { |
---|
810 | _stype = supply_type; |
---|
811 | return *this; |
---|
812 | } |
---|
813 | |
---|
814 | /// @} |
---|
815 | |
---|
816 | /// \name Execution Control |
---|
817 | /// The algorithm can be executed using \ref run(). |
---|
818 | |
---|
819 | /// @{ |
---|
820 | |
---|
821 | /// \brief Run the algorithm. |
---|
822 | /// |
---|
823 | /// This function runs the algorithm. |
---|
824 | /// The paramters can be specified using functions \ref lowerMap(), |
---|
825 | /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
---|
826 | /// \ref supplyType(). |
---|
827 | /// For example, |
---|
828 | /// \code |
---|
829 | /// NetworkSimplex<ListDigraph> ns(graph); |
---|
830 | /// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
---|
831 | /// .supplyMap(sup).run(); |
---|
832 | /// \endcode |
---|
833 | /// |
---|
834 | /// This function can be called more than once. All the parameters |
---|
835 | /// that have been given are kept for the next call, unless |
---|
836 | /// \ref reset() is called, thus only the modified parameters |
---|
837 | /// have to be set again. See \ref reset() for examples. |
---|
838 | /// However the underlying digraph must not be modified after this |
---|
839 | /// class have been constructed, since it copies and extends the graph. |
---|
840 | /// |
---|
841 | /// \param pivot_rule The pivot rule that will be used during the |
---|
842 | /// algorithm. For more information see \ref PivotRule. |
---|
843 | /// |
---|
844 | /// \return \c INFEASIBLE if no feasible flow exists, |
---|
845 | /// \n \c OPTIMAL if the problem has optimal solution |
---|
846 | /// (i.e. it is feasible and bounded), and the algorithm has found |
---|
847 | /// optimal flow and node potentials (primal and dual solutions), |
---|
848 | /// \n \c UNBOUNDED if the objective function of the problem is |
---|
849 | /// unbounded, i.e. there is a directed cycle having negative total |
---|
850 | /// cost and infinite upper bound. |
---|
851 | /// |
---|
852 | /// \see ProblemType, PivotRule |
---|
853 | ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) { |
---|
854 | if (!init()) return INFEASIBLE; |
---|
855 | return start(pivot_rule); |
---|
856 | } |
---|
857 | |
---|
858 | /// \brief Reset all the parameters that have been given before. |
---|
859 | /// |
---|
860 | /// This function resets all the paramaters that have been given |
---|
861 | /// before using functions \ref lowerMap(), \ref upperMap(), |
---|
862 | /// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType(). |
---|
863 | /// |
---|
864 | /// It is useful for multiple run() calls. If this function is not |
---|
865 | /// used, all the parameters given before are kept for the next |
---|
866 | /// \ref run() call. |
---|
867 | /// However the underlying digraph must not be modified after this |
---|
868 | /// class have been constructed, since it copies and extends the graph. |
---|
869 | /// |
---|
870 | /// For example, |
---|
871 | /// \code |
---|
872 | /// NetworkSimplex<ListDigraph> ns(graph); |
---|
873 | /// |
---|
874 | /// // First run |
---|
875 | /// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
---|
876 | /// .supplyMap(sup).run(); |
---|
877 | /// |
---|
878 | /// // Run again with modified cost map (reset() is not called, |
---|
879 | /// // so only the cost map have to be set again) |
---|
880 | /// cost[e] += 100; |
---|
881 | /// ns.costMap(cost).run(); |
---|
882 | /// |
---|
883 | /// // Run again from scratch using reset() |
---|
884 | /// // (the lower bounds will be set to zero on all arcs) |
---|
885 | /// ns.reset(); |
---|
886 | /// ns.upperMap(capacity).costMap(cost) |
---|
887 | /// .supplyMap(sup).run(); |
---|
888 | /// \endcode |
---|
889 | /// |
---|
890 | /// \return <tt>(*this)</tt> |
---|
891 | NetworkSimplex& reset() { |
---|
892 | for (int i = 0; i != _node_num; ++i) { |
---|
893 | _supply[i] = 0; |
---|
894 | } |
---|
895 | for (int i = 0; i != _arc_num; ++i) { |
---|
896 | _lower[i] = 0; |
---|
897 | _upper[i] = INF; |
---|
898 | _cost[i] = 1; |
---|
899 | } |
---|
900 | _have_lower = false; |
---|
901 | _stype = GEQ; |
---|
902 | return *this; |
---|
903 | } |
---|
904 | |
---|
905 | /// @} |
---|
906 | |
---|
907 | /// \name Query Functions |
---|
908 | /// The results of the algorithm can be obtained using these |
---|
909 | /// functions.\n |
---|
910 | /// The \ref run() function must be called before using them. |
---|
911 | |
---|
912 | /// @{ |
---|
913 | |
---|
914 | /// \brief Return the total cost of the found flow. |
---|
915 | /// |
---|
916 | /// This function returns the total cost of the found flow. |
---|
917 | /// Its complexity is O(e). |
---|
918 | /// |
---|
919 | /// \note The return type of the function can be specified as a |
---|
920 | /// template parameter. For example, |
---|
921 | /// \code |
---|
922 | /// ns.totalCost<double>(); |
---|
923 | /// \endcode |
---|
924 | /// It is useful if the total cost cannot be stored in the \c Cost |
---|
925 | /// type of the algorithm, which is the default return type of the |
---|
926 | /// function. |
---|
927 | /// |
---|
928 | /// \pre \ref run() must be called before using this function. |
---|
929 | template <typename Number> |
---|
930 | Number totalCost() const { |
---|
931 | Number c = 0; |
---|
932 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
933 | int i = _arc_id[a]; |
---|
934 | c += Number(_flow[i]) * Number(_cost[i]); |
---|
935 | } |
---|
936 | return c; |
---|
937 | } |
---|
938 | |
---|
939 | #ifndef DOXYGEN |
---|
940 | Cost totalCost() const { |
---|
941 | return totalCost<Cost>(); |
---|
942 | } |
---|
943 | #endif |
---|
944 | |
---|
945 | /// \brief Return the flow on the given arc. |
---|
946 | /// |
---|
947 | /// This function returns the flow on the given arc. |
---|
948 | /// |
---|
949 | /// \pre \ref run() must be called before using this function. |
---|
950 | Value flow(const Arc& a) const { |
---|
951 | return _flow[_arc_id[a]]; |
---|
952 | } |
---|
953 | |
---|
954 | /// \brief Return the flow map (the primal solution). |
---|
955 | /// |
---|
956 | /// This function copies the flow value on each arc into the given |
---|
957 | /// map. The \c Value type of the algorithm must be convertible to |
---|
958 | /// the \c Value type of the map. |
---|
959 | /// |
---|
960 | /// \pre \ref run() must be called before using this function. |
---|
961 | template <typename FlowMap> |
---|
962 | void flowMap(FlowMap &map) const { |
---|
963 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
964 | map.set(a, _flow[_arc_id[a]]); |
---|
965 | } |
---|
966 | } |
---|
967 | |
---|
968 | /// \brief Return the potential (dual value) of the given node. |
---|
969 | /// |
---|
970 | /// This function returns the potential (dual value) of the |
---|
971 | /// given node. |
---|
972 | /// |
---|
973 | /// \pre \ref run() must be called before using this function. |
---|
974 | Cost potential(const Node& n) const { |
---|
975 | return _pi[_node_id[n]]; |
---|
976 | } |
---|
977 | |
---|
978 | /// \brief Return the potential map (the dual solution). |
---|
979 | /// |
---|
980 | /// This function copies the potential (dual value) of each node |
---|
981 | /// into the given map. |
---|
982 | /// The \c Cost type of the algorithm must be convertible to the |
---|
983 | /// \c Value type of the map. |
---|
984 | /// |
---|
985 | /// \pre \ref run() must be called before using this function. |
---|
986 | template <typename PotentialMap> |
---|
987 | void potentialMap(PotentialMap &map) const { |
---|
988 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
989 | map.set(n, _pi[_node_id[n]]); |
---|
990 | } |
---|
991 | } |
---|
992 | |
---|
993 | /// @} |
---|
994 | |
---|
995 | private: |
---|
996 | |
---|
997 | // Initialize internal data structures |
---|
998 | bool init() { |
---|
999 | if (_node_num == 0) return false; |
---|
1000 | |
---|
1001 | // Check the sum of supply values |
---|
1002 | _sum_supply = 0; |
---|
1003 | for (int i = 0; i != _node_num; ++i) { |
---|
1004 | _sum_supply += _supply[i]; |
---|
1005 | } |
---|
1006 | if ( !((_stype == GEQ && _sum_supply <= 0) || |
---|
1007 | (_stype == LEQ && _sum_supply >= 0)) ) return false; |
---|
1008 | |
---|
1009 | // Remove non-zero lower bounds |
---|
1010 | if (_have_lower) { |
---|
1011 | for (int i = 0; i != _arc_num; ++i) { |
---|
1012 | Value c = _lower[i]; |
---|
1013 | if (c >= 0) { |
---|
1014 | _cap[i] = _upper[i] < INF ? _upper[i] - c : INF; |
---|
1015 | } else { |
---|
1016 | _cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF; |
---|
1017 | } |
---|
1018 | _supply[_source[i]] -= c; |
---|
1019 | _supply[_target[i]] += c; |
---|
1020 | } |
---|
1021 | } else { |
---|
1022 | for (int i = 0; i != _arc_num; ++i) { |
---|
1023 | _cap[i] = _upper[i]; |
---|
1024 | } |
---|
1025 | } |
---|
1026 | |
---|
1027 | // Initialize artifical cost |
---|
1028 | Cost ART_COST; |
---|
1029 | if (std::numeric_limits<Cost>::is_exact) { |
---|
1030 | ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
---|
1031 | } else { |
---|
1032 | ART_COST = std::numeric_limits<Cost>::min(); |
---|
1033 | for (int i = 0; i != _arc_num; ++i) { |
---|
1034 | if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
---|
1035 | } |
---|
1036 | ART_COST = (ART_COST + 1) * _node_num; |
---|
1037 | } |
---|
1038 | |
---|
1039 | // Initialize arc maps |
---|
1040 | for (int i = 0; i != _arc_num; ++i) { |
---|
1041 | _flow[i] = 0; |
---|
1042 | _state[i] = STATE_LOWER; |
---|
1043 | } |
---|
1044 | |
---|
1045 | // Set data for the artificial root node |
---|
1046 | _root = _node_num; |
---|
1047 | _parent[_root] = -1; |
---|
1048 | _pred[_root] = -1; |
---|
1049 | _thread[_root] = 0; |
---|
1050 | _rev_thread[0] = _root; |
---|
1051 | _succ_num[_root] = _node_num + 1; |
---|
1052 | _last_succ[_root] = _root - 1; |
---|
1053 | _supply[_root] = -_sum_supply; |
---|
1054 | _pi[_root] = 0; |
---|
1055 | |
---|
1056 | // Add artificial arcs and initialize the spanning tree data structure |
---|
1057 | if (_sum_supply == 0) { |
---|
1058 | // EQ supply constraints |
---|
1059 | _search_arc_num = _arc_num; |
---|
1060 | _all_arc_num = _arc_num + _node_num; |
---|
1061 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
---|
1062 | _parent[u] = _root; |
---|
1063 | _pred[u] = e; |
---|
1064 | _thread[u] = u + 1; |
---|
1065 | _rev_thread[u + 1] = u; |
---|
1066 | _succ_num[u] = 1; |
---|
1067 | _last_succ[u] = u; |
---|
1068 | _cap[e] = INF; |
---|
1069 | _state[e] = STATE_TREE; |
---|
1070 | if (_supply[u] >= 0) { |
---|
1071 | _forward[u] = true; |
---|
1072 | _pi[u] = 0; |
---|
1073 | _source[e] = u; |
---|
1074 | _target[e] = _root; |
---|
1075 | _flow[e] = _supply[u]; |
---|
1076 | _cost[e] = 0; |
---|
1077 | } else { |
---|
1078 | _forward[u] = false; |
---|
1079 | _pi[u] = ART_COST; |
---|
1080 | _source[e] = _root; |
---|
1081 | _target[e] = u; |
---|
1082 | _flow[e] = -_supply[u]; |
---|
1083 | _cost[e] = ART_COST; |
---|
1084 | } |
---|
1085 | } |
---|
1086 | } |
---|
1087 | else if (_sum_supply > 0) { |
---|
1088 | // LEQ supply constraints |
---|
1089 | _search_arc_num = _arc_num + _node_num; |
---|
1090 | int f = _arc_num + _node_num; |
---|
1091 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
---|
1092 | _parent[u] = _root; |
---|
1093 | _thread[u] = u + 1; |
---|
1094 | _rev_thread[u + 1] = u; |
---|
1095 | _succ_num[u] = 1; |
---|
1096 | _last_succ[u] = u; |
---|
1097 | if (_supply[u] >= 0) { |
---|
1098 | _forward[u] = true; |
---|
1099 | _pi[u] = 0; |
---|
1100 | _pred[u] = e; |
---|
1101 | _source[e] = u; |
---|
1102 | _target[e] = _root; |
---|
1103 | _cap[e] = INF; |
---|
1104 | _flow[e] = _supply[u]; |
---|
1105 | _cost[e] = 0; |
---|
1106 | _state[e] = STATE_TREE; |
---|
1107 | } else { |
---|
1108 | _forward[u] = false; |
---|
1109 | _pi[u] = ART_COST; |
---|
1110 | _pred[u] = f; |
---|
1111 | _source[f] = _root; |
---|
1112 | _target[f] = u; |
---|
1113 | _cap[f] = INF; |
---|
1114 | _flow[f] = -_supply[u]; |
---|
1115 | _cost[f] = ART_COST; |
---|
1116 | _state[f] = STATE_TREE; |
---|
1117 | _source[e] = u; |
---|
1118 | _target[e] = _root; |
---|
1119 | _cap[e] = INF; |
---|
1120 | _flow[e] = 0; |
---|
1121 | _cost[e] = 0; |
---|
1122 | _state[e] = STATE_LOWER; |
---|
1123 | ++f; |
---|
1124 | } |
---|
1125 | } |
---|
1126 | _all_arc_num = f; |
---|
1127 | } |
---|
1128 | else { |
---|
1129 | // GEQ supply constraints |
---|
1130 | _search_arc_num = _arc_num + _node_num; |
---|
1131 | int f = _arc_num + _node_num; |
---|
1132 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
---|
1133 | _parent[u] = _root; |
---|
1134 | _thread[u] = u + 1; |
---|
1135 | _rev_thread[u + 1] = u; |
---|
1136 | _succ_num[u] = 1; |
---|
1137 | _last_succ[u] = u; |
---|
1138 | if (_supply[u] <= 0) { |
---|
1139 | _forward[u] = false; |
---|
1140 | _pi[u] = 0; |
---|
1141 | _pred[u] = e; |
---|
1142 | _source[e] = _root; |
---|
1143 | _target[e] = u; |
---|
1144 | _cap[e] = INF; |
---|
1145 | _flow[e] = -_supply[u]; |
---|
1146 | _cost[e] = 0; |
---|
1147 | _state[e] = STATE_TREE; |
---|
1148 | } else { |
---|
1149 | _forward[u] = true; |
---|
1150 | _pi[u] = -ART_COST; |
---|
1151 | _pred[u] = f; |
---|
1152 | _source[f] = u; |
---|
1153 | _target[f] = _root; |
---|
1154 | _cap[f] = INF; |
---|
1155 | _flow[f] = _supply[u]; |
---|
1156 | _state[f] = STATE_TREE; |
---|
1157 | _cost[f] = ART_COST; |
---|
1158 | _source[e] = _root; |
---|
1159 | _target[e] = u; |
---|
1160 | _cap[e] = INF; |
---|
1161 | _flow[e] = 0; |
---|
1162 | _cost[e] = 0; |
---|
1163 | _state[e] = STATE_LOWER; |
---|
1164 | ++f; |
---|
1165 | } |
---|
1166 | } |
---|
1167 | _all_arc_num = f; |
---|
1168 | } |
---|
1169 | |
---|
1170 | return true; |
---|
1171 | } |
---|
1172 | |
---|
1173 | // Find the join node |
---|
1174 | void findJoinNode() { |
---|
1175 | int u = _source[in_arc]; |
---|
1176 | int v = _target[in_arc]; |
---|
1177 | while (u != v) { |
---|
1178 | if (_succ_num[u] < _succ_num[v]) { |
---|
1179 | u = _parent[u]; |
---|
1180 | } else { |
---|
1181 | v = _parent[v]; |
---|
1182 | } |
---|
1183 | } |
---|
1184 | join = u; |
---|
1185 | } |
---|
1186 | |
---|
1187 | // Find the leaving arc of the cycle and returns true if the |
---|
1188 | // leaving arc is not the same as the entering arc |
---|
1189 | bool findLeavingArc() { |
---|
1190 | // Initialize first and second nodes according to the direction |
---|
1191 | // of the cycle |
---|
1192 | if (_state[in_arc] == STATE_LOWER) { |
---|
1193 | first = _source[in_arc]; |
---|
1194 | second = _target[in_arc]; |
---|
1195 | } else { |
---|
1196 | first = _target[in_arc]; |
---|
1197 | second = _source[in_arc]; |
---|
1198 | } |
---|
1199 | delta = _cap[in_arc]; |
---|
1200 | int result = 0; |
---|
1201 | Value d; |
---|
1202 | int e; |
---|
1203 | |
---|
1204 | // Search the cycle along the path form the first node to the root |
---|
1205 | for (int u = first; u != join; u = _parent[u]) { |
---|
1206 | e = _pred[u]; |
---|
1207 | d = _forward[u] ? |
---|
1208 | _flow[e] : (_cap[e] == INF ? INF : _cap[e] - _flow[e]); |
---|
1209 | if (d < delta) { |
---|
1210 | delta = d; |
---|
1211 | u_out = u; |
---|
1212 | result = 1; |
---|
1213 | } |
---|
1214 | } |
---|
1215 | // Search the cycle along the path form the second node to the root |
---|
1216 | for (int u = second; u != join; u = _parent[u]) { |
---|
1217 | e = _pred[u]; |
---|
1218 | d = _forward[u] ? |
---|
1219 | (_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e]; |
---|
1220 | if (d <= delta) { |
---|
1221 | delta = d; |
---|
1222 | u_out = u; |
---|
1223 | result = 2; |
---|
1224 | } |
---|
1225 | } |
---|
1226 | |
---|
1227 | if (result == 1) { |
---|
1228 | u_in = first; |
---|
1229 | v_in = second; |
---|
1230 | } else { |
---|
1231 | u_in = second; |
---|
1232 | v_in = first; |
---|
1233 | } |
---|
1234 | return result != 0; |
---|
1235 | } |
---|
1236 | |
---|
1237 | // Change _flow and _state vectors |
---|
1238 | void changeFlow(bool change) { |
---|
1239 | // Augment along the cycle |
---|
1240 | if (delta > 0) { |
---|
1241 | Value val = _state[in_arc] * delta; |
---|
1242 | _flow[in_arc] += val; |
---|
1243 | for (int u = _source[in_arc]; u != join; u = _parent[u]) { |
---|
1244 | _flow[_pred[u]] += _forward[u] ? -val : val; |
---|
1245 | } |
---|
1246 | for (int u = _target[in_arc]; u != join; u = _parent[u]) { |
---|
1247 | _flow[_pred[u]] += _forward[u] ? val : -val; |
---|
1248 | } |
---|
1249 | } |
---|
1250 | // Update the state of the entering and leaving arcs |
---|
1251 | if (change) { |
---|
1252 | _state[in_arc] = STATE_TREE; |
---|
1253 | _state[_pred[u_out]] = |
---|
1254 | (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; |
---|
1255 | } else { |
---|
1256 | _state[in_arc] = -_state[in_arc]; |
---|
1257 | } |
---|
1258 | } |
---|
1259 | |
---|
1260 | // Update the tree structure |
---|
1261 | void updateTreeStructure() { |
---|
1262 | int u, w; |
---|
1263 | int old_rev_thread = _rev_thread[u_out]; |
---|
1264 | int old_succ_num = _succ_num[u_out]; |
---|
1265 | int old_last_succ = _last_succ[u_out]; |
---|
1266 | v_out = _parent[u_out]; |
---|
1267 | |
---|
1268 | u = _last_succ[u_in]; // the last successor of u_in |
---|
1269 | right = _thread[u]; // the node after it |
---|
1270 | |
---|
1271 | // Handle the case when old_rev_thread equals to v_in |
---|
1272 | // (it also means that join and v_out coincide) |
---|
1273 | if (old_rev_thread == v_in) { |
---|
1274 | last = _thread[_last_succ[u_out]]; |
---|
1275 | } else { |
---|
1276 | last = _thread[v_in]; |
---|
1277 | } |
---|
1278 | |
---|
1279 | // Update _thread and _parent along the stem nodes (i.e. the nodes |
---|
1280 | // between u_in and u_out, whose parent have to be changed) |
---|
1281 | _thread[v_in] = stem = u_in; |
---|
1282 | _dirty_revs.clear(); |
---|
1283 | _dirty_revs.push_back(v_in); |
---|
1284 | par_stem = v_in; |
---|
1285 | while (stem != u_out) { |
---|
1286 | // Insert the next stem node into the thread list |
---|
1287 | new_stem = _parent[stem]; |
---|
1288 | _thread[u] = new_stem; |
---|
1289 | _dirty_revs.push_back(u); |
---|
1290 | |
---|
1291 | // Remove the subtree of stem from the thread list |
---|
1292 | w = _rev_thread[stem]; |
---|
1293 | _thread[w] = right; |
---|
1294 | _rev_thread[right] = w; |
---|
1295 | |
---|
1296 | // Change the parent node and shift stem nodes |
---|
1297 | _parent[stem] = par_stem; |
---|
1298 | par_stem = stem; |
---|
1299 | stem = new_stem; |
---|
1300 | |
---|
1301 | // Update u and right |
---|
1302 | u = _last_succ[stem] == _last_succ[par_stem] ? |
---|
1303 | _rev_thread[par_stem] : _last_succ[stem]; |
---|
1304 | right = _thread[u]; |
---|
1305 | } |
---|
1306 | _parent[u_out] = par_stem; |
---|
1307 | _thread[u] = last; |
---|
1308 | _rev_thread[last] = u; |
---|
1309 | _last_succ[u_out] = u; |
---|
1310 | |
---|
1311 | // Remove the subtree of u_out from the thread list except for |
---|
1312 | // the case when old_rev_thread equals to v_in |
---|
1313 | // (it also means that join and v_out coincide) |
---|
1314 | if (old_rev_thread != v_in) { |
---|
1315 | _thread[old_rev_thread] = right; |
---|
1316 | _rev_thread[right] = old_rev_thread; |
---|
1317 | } |
---|
1318 | |
---|
1319 | // Update _rev_thread using the new _thread values |
---|
1320 | for (int i = 0; i < int(_dirty_revs.size()); ++i) { |
---|
1321 | u = _dirty_revs[i]; |
---|
1322 | _rev_thread[_thread[u]] = u; |
---|
1323 | } |
---|
1324 | |
---|
1325 | // Update _pred, _forward, _last_succ and _succ_num for the |
---|
1326 | // stem nodes from u_out to u_in |
---|
1327 | int tmp_sc = 0, tmp_ls = _last_succ[u_out]; |
---|
1328 | u = u_out; |
---|
1329 | while (u != u_in) { |
---|
1330 | w = _parent[u]; |
---|
1331 | _pred[u] = _pred[w]; |
---|
1332 | _forward[u] = !_forward[w]; |
---|
1333 | tmp_sc += _succ_num[u] - _succ_num[w]; |
---|
1334 | _succ_num[u] = tmp_sc; |
---|
1335 | _last_succ[w] = tmp_ls; |
---|
1336 | u = w; |
---|
1337 | } |
---|
1338 | _pred[u_in] = in_arc; |
---|
1339 | _forward[u_in] = (u_in == _source[in_arc]); |
---|
1340 | _succ_num[u_in] = old_succ_num; |
---|
1341 | |
---|
1342 | // Set limits for updating _last_succ form v_in and v_out |
---|
1343 | // towards the root |
---|
1344 | int up_limit_in = -1; |
---|
1345 | int up_limit_out = -1; |
---|
1346 | if (_last_succ[join] == v_in) { |
---|
1347 | up_limit_out = join; |
---|
1348 | } else { |
---|
1349 | up_limit_in = join; |
---|
1350 | } |
---|
1351 | |
---|
1352 | // Update _last_succ from v_in towards the root |
---|
1353 | for (u = v_in; u != up_limit_in && _last_succ[u] == v_in; |
---|
1354 | u = _parent[u]) { |
---|
1355 | _last_succ[u] = _last_succ[u_out]; |
---|
1356 | } |
---|
1357 | // Update _last_succ from v_out towards the root |
---|
1358 | if (join != old_rev_thread && v_in != old_rev_thread) { |
---|
1359 | for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
---|
1360 | u = _parent[u]) { |
---|
1361 | _last_succ[u] = old_rev_thread; |
---|
1362 | } |
---|
1363 | } else { |
---|
1364 | for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
---|
1365 | u = _parent[u]) { |
---|
1366 | _last_succ[u] = _last_succ[u_out]; |
---|
1367 | } |
---|
1368 | } |
---|
1369 | |
---|
1370 | // Update _succ_num from v_in to join |
---|
1371 | for (u = v_in; u != join; u = _parent[u]) { |
---|
1372 | _succ_num[u] += old_succ_num; |
---|
1373 | } |
---|
1374 | // Update _succ_num from v_out to join |
---|
1375 | for (u = v_out; u != join; u = _parent[u]) { |
---|
1376 | _succ_num[u] -= old_succ_num; |
---|
1377 | } |
---|
1378 | } |
---|
1379 | |
---|
1380 | // Update potentials |
---|
1381 | void updatePotential() { |
---|
1382 | Cost sigma = _forward[u_in] ? |
---|
1383 | _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] : |
---|
1384 | _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]]; |
---|
1385 | // Update potentials in the subtree, which has been moved |
---|
1386 | int end = _thread[_last_succ[u_in]]; |
---|
1387 | for (int u = u_in; u != end; u = _thread[u]) { |
---|
1388 | _pi[u] += sigma; |
---|
1389 | } |
---|
1390 | } |
---|
1391 | |
---|
1392 | // Execute the algorithm |
---|
1393 | ProblemType start(PivotRule pivot_rule) { |
---|
1394 | // Select the pivot rule implementation |
---|
1395 | switch (pivot_rule) { |
---|
1396 | case FIRST_ELIGIBLE: |
---|
1397 | return start<FirstEligiblePivotRule>(); |
---|
1398 | case BEST_ELIGIBLE: |
---|
1399 | return start<BestEligiblePivotRule>(); |
---|
1400 | case BLOCK_SEARCH: |
---|
1401 | return start<BlockSearchPivotRule>(); |
---|
1402 | case CANDIDATE_LIST: |
---|
1403 | return start<CandidateListPivotRule>(); |
---|
1404 | case ALTERING_LIST: |
---|
1405 | return start<AlteringListPivotRule>(); |
---|
1406 | } |
---|
1407 | return INFEASIBLE; // avoid warning |
---|
1408 | } |
---|
1409 | |
---|
1410 | template <typename PivotRuleImpl> |
---|
1411 | ProblemType start() { |
---|
1412 | PivotRuleImpl pivot(*this); |
---|
1413 | |
---|
1414 | // Execute the Network Simplex algorithm |
---|
1415 | while (pivot.findEnteringArc()) { |
---|
1416 | findJoinNode(); |
---|
1417 | bool change = findLeavingArc(); |
---|
1418 | if (delta >= INF) return UNBOUNDED; |
---|
1419 | changeFlow(change); |
---|
1420 | if (change) { |
---|
1421 | updateTreeStructure(); |
---|
1422 | updatePotential(); |
---|
1423 | } |
---|
1424 | } |
---|
1425 | |
---|
1426 | // Check feasibility |
---|
1427 | for (int e = _search_arc_num; e != _all_arc_num; ++e) { |
---|
1428 | if (_flow[e] != 0) return INFEASIBLE; |
---|
1429 | } |
---|
1430 | |
---|
1431 | // Transform the solution and the supply map to the original form |
---|
1432 | if (_have_lower) { |
---|
1433 | for (int i = 0; i != _arc_num; ++i) { |
---|
1434 | Value c = _lower[i]; |
---|
1435 | if (c != 0) { |
---|
1436 | _flow[i] += c; |
---|
1437 | _supply[_source[i]] += c; |
---|
1438 | _supply[_target[i]] -= c; |
---|
1439 | } |
---|
1440 | } |
---|
1441 | } |
---|
1442 | |
---|
1443 | // Shift potentials to meet the requirements of the GEQ/LEQ type |
---|
1444 | // optimality conditions |
---|
1445 | if (_sum_supply == 0) { |
---|
1446 | if (_stype == GEQ) { |
---|
1447 | Cost max_pot = std::numeric_limits<Cost>::min(); |
---|
1448 | for (int i = 0; i != _node_num; ++i) { |
---|
1449 | if (_pi[i] > max_pot) max_pot = _pi[i]; |
---|
1450 | } |
---|
1451 | if (max_pot > 0) { |
---|
1452 | for (int i = 0; i != _node_num; ++i) |
---|
1453 | _pi[i] -= max_pot; |
---|
1454 | } |
---|
1455 | } else { |
---|
1456 | Cost min_pot = std::numeric_limits<Cost>::max(); |
---|
1457 | for (int i = 0; i != _node_num; ++i) { |
---|
1458 | if (_pi[i] < min_pot) min_pot = _pi[i]; |
---|
1459 | } |
---|
1460 | if (min_pot < 0) { |
---|
1461 | for (int i = 0; i != _node_num; ++i) |
---|
1462 | _pi[i] -= min_pot; |
---|
1463 | } |
---|
1464 | } |
---|
1465 | } |
---|
1466 | |
---|
1467 | return OPTIMAL; |
---|
1468 | } |
---|
1469 | |
---|
1470 | }; //class NetworkSimplex |
---|
1471 | |
---|
1472 | ///@} |
---|
1473 | |
---|
1474 | } //namespace lemon |
---|
1475 | |
---|
1476 | #endif //LEMON_NETWORK_SIMPLEX_H |
---|