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-2010 |
---|
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_COST_SCALING_H |
---|
20 | #define LEMON_COST_SCALING_H |
---|
21 | |
---|
22 | /// \ingroup min_cost_flow_algs |
---|
23 | /// \file |
---|
24 | /// \brief Cost scaling algorithm for finding a minimum cost flow. |
---|
25 | |
---|
26 | #include <vector> |
---|
27 | #include <deque> |
---|
28 | #include <limits> |
---|
29 | |
---|
30 | #include <lemon/core.h> |
---|
31 | #include <lemon/maps.h> |
---|
32 | #include <lemon/math.h> |
---|
33 | #include <lemon/static_graph.h> |
---|
34 | #include <lemon/circulation.h> |
---|
35 | #include <lemon/bellman_ford.h> |
---|
36 | |
---|
37 | namespace lemon { |
---|
38 | |
---|
39 | /// \brief Default traits class of CostScaling algorithm. |
---|
40 | /// |
---|
41 | /// Default traits class of CostScaling algorithm. |
---|
42 | /// \tparam GR Digraph type. |
---|
43 | /// \tparam V The number type used for flow amounts, capacity bounds |
---|
44 | /// and supply values. By default it is \c int. |
---|
45 | /// \tparam C The number type used for costs and potentials. |
---|
46 | /// By default it is the same as \c V. |
---|
47 | #ifdef DOXYGEN |
---|
48 | template <typename GR, typename V = int, typename C = V> |
---|
49 | #else |
---|
50 | template < typename GR, typename V = int, typename C = V, |
---|
51 | bool integer = std::numeric_limits<C>::is_integer > |
---|
52 | #endif |
---|
53 | struct CostScalingDefaultTraits |
---|
54 | { |
---|
55 | /// The type of the digraph |
---|
56 | typedef GR Digraph; |
---|
57 | /// The type of the flow amounts, capacity bounds and supply values |
---|
58 | typedef V Value; |
---|
59 | /// The type of the arc costs |
---|
60 | typedef C Cost; |
---|
61 | |
---|
62 | /// \brief The large cost type used for internal computations |
---|
63 | /// |
---|
64 | /// The large cost type used for internal computations. |
---|
65 | /// It is \c long \c long if the \c Cost type is integer, |
---|
66 | /// otherwise it is \c double. |
---|
67 | /// \c Cost must be convertible to \c LargeCost. |
---|
68 | typedef double LargeCost; |
---|
69 | }; |
---|
70 | |
---|
71 | // Default traits class for integer cost types |
---|
72 | template <typename GR, typename V, typename C> |
---|
73 | struct CostScalingDefaultTraits<GR, V, C, true> |
---|
74 | { |
---|
75 | typedef GR Digraph; |
---|
76 | typedef V Value; |
---|
77 | typedef C Cost; |
---|
78 | #ifdef LEMON_HAVE_LONG_LONG |
---|
79 | typedef long long LargeCost; |
---|
80 | #else |
---|
81 | typedef long LargeCost; |
---|
82 | #endif |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | /// \addtogroup min_cost_flow_algs |
---|
87 | /// @{ |
---|
88 | |
---|
89 | /// \brief Implementation of the Cost Scaling algorithm for |
---|
90 | /// finding a \ref min_cost_flow "minimum cost flow". |
---|
91 | /// |
---|
92 | /// \ref CostScaling implements a cost scaling algorithm that performs |
---|
93 | /// push/augment and relabel operations for finding a \ref min_cost_flow |
---|
94 | /// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
---|
95 | /// \ref goldberg97efficient, \ref bunnagel98efficient. |
---|
96 | /// It is a highly efficient primal-dual solution method, which |
---|
97 | /// can be viewed as the generalization of the \ref Preflow |
---|
98 | /// "preflow push-relabel" algorithm for the maximum flow problem. |
---|
99 | /// |
---|
100 | /// In general, \ref NetworkSimplex and \ref CostScaling are the fastest |
---|
101 | /// implementations available in LEMON for this problem. |
---|
102 | /// |
---|
103 | /// Most of the parameters of the problem (except for the digraph) |
---|
104 | /// can be given using separate functions, and the algorithm can be |
---|
105 | /// executed using the \ref run() function. If some parameters are not |
---|
106 | /// specified, then default values will be used. |
---|
107 | /// |
---|
108 | /// \tparam GR The digraph type the algorithm runs on. |
---|
109 | /// \tparam V The number type used for flow amounts, capacity bounds |
---|
110 | /// and supply values in the algorithm. By default, it is \c int. |
---|
111 | /// \tparam C The number type used for costs and potentials in the |
---|
112 | /// algorithm. By default, it is the same as \c V. |
---|
113 | /// \tparam TR The traits class that defines various types used by the |
---|
114 | /// algorithm. By default, it is \ref CostScalingDefaultTraits |
---|
115 | /// "CostScalingDefaultTraits<GR, V, C>". |
---|
116 | /// In most cases, this parameter should not be set directly, |
---|
117 | /// consider to use the named template parameters instead. |
---|
118 | /// |
---|
119 | /// \warning Both \c V and \c C must be signed number types. |
---|
120 | /// \warning All input data (capacities, supply values, and costs) must |
---|
121 | /// be integer. |
---|
122 | /// \warning This algorithm does not support negative costs for |
---|
123 | /// arcs having infinite upper bound. |
---|
124 | /// |
---|
125 | /// \note %CostScaling provides three different internal methods, |
---|
126 | /// from which the most efficient one is used by default. |
---|
127 | /// For more information, see \ref Method. |
---|
128 | #ifdef DOXYGEN |
---|
129 | template <typename GR, typename V, typename C, typename TR> |
---|
130 | #else |
---|
131 | template < typename GR, typename V = int, typename C = V, |
---|
132 | typename TR = CostScalingDefaultTraits<GR, V, C> > |
---|
133 | #endif |
---|
134 | class CostScaling |
---|
135 | { |
---|
136 | public: |
---|
137 | |
---|
138 | /// The type of the digraph |
---|
139 | typedef typename TR::Digraph Digraph; |
---|
140 | /// The type of the flow amounts, capacity bounds and supply values |
---|
141 | typedef typename TR::Value Value; |
---|
142 | /// The type of the arc costs |
---|
143 | typedef typename TR::Cost Cost; |
---|
144 | |
---|
145 | /// \brief The large cost type |
---|
146 | /// |
---|
147 | /// The large cost type used for internal computations. |
---|
148 | /// By default, it is \c long \c long if the \c Cost type is integer, |
---|
149 | /// otherwise it is \c double. |
---|
150 | typedef typename TR::LargeCost LargeCost; |
---|
151 | |
---|
152 | /// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
---|
153 | typedef TR Traits; |
---|
154 | |
---|
155 | public: |
---|
156 | |
---|
157 | /// \brief Problem type constants for the \c run() function. |
---|
158 | /// |
---|
159 | /// Enum type containing the problem type constants that can be |
---|
160 | /// returned by the \ref run() function of the algorithm. |
---|
161 | enum ProblemType { |
---|
162 | /// The problem has no feasible solution (flow). |
---|
163 | INFEASIBLE, |
---|
164 | /// The problem has optimal solution (i.e. it is feasible and |
---|
165 | /// bounded), and the algorithm has found optimal flow and node |
---|
166 | /// potentials (primal and dual solutions). |
---|
167 | OPTIMAL, |
---|
168 | /// The digraph contains an arc of negative cost and infinite |
---|
169 | /// upper bound. It means that the objective function is unbounded |
---|
170 | /// on that arc, however, note that it could actually be bounded |
---|
171 | /// over the feasible flows, but this algroithm cannot handle |
---|
172 | /// these cases. |
---|
173 | UNBOUNDED |
---|
174 | }; |
---|
175 | |
---|
176 | /// \brief Constants for selecting the internal method. |
---|
177 | /// |
---|
178 | /// Enum type containing constants for selecting the internal method |
---|
179 | /// for the \ref run() function. |
---|
180 | /// |
---|
181 | /// \ref CostScaling provides three internal methods that differ mainly |
---|
182 | /// in their base operations, which are used in conjunction with the |
---|
183 | /// relabel operation. |
---|
184 | /// By default, the so called \ref PARTIAL_AUGMENT |
---|
185 | /// "Partial Augment-Relabel" method is used, which turned out to be |
---|
186 | /// the most efficient and the most robust on various test inputs. |
---|
187 | /// However, the other methods can be selected using the \ref run() |
---|
188 | /// function with the proper parameter. |
---|
189 | enum Method { |
---|
190 | /// Local push operations are used, i.e. flow is moved only on one |
---|
191 | /// admissible arc at once. |
---|
192 | PUSH, |
---|
193 | /// Augment operations are used, i.e. flow is moved on admissible |
---|
194 | /// paths from a node with excess to a node with deficit. |
---|
195 | AUGMENT, |
---|
196 | /// Partial augment operations are used, i.e. flow is moved on |
---|
197 | /// admissible paths started from a node with excess, but the |
---|
198 | /// lengths of these paths are limited. This method can be viewed |
---|
199 | /// as a combined version of the previous two operations. |
---|
200 | PARTIAL_AUGMENT |
---|
201 | }; |
---|
202 | |
---|
203 | private: |
---|
204 | |
---|
205 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
---|
206 | |
---|
207 | typedef std::vector<int> IntVector; |
---|
208 | typedef std::vector<Value> ValueVector; |
---|
209 | typedef std::vector<Cost> CostVector; |
---|
210 | typedef std::vector<LargeCost> LargeCostVector; |
---|
211 | typedef std::vector<char> BoolVector; |
---|
212 | // Note: vector<char> is used instead of vector<bool> for efficiency reasons |
---|
213 | |
---|
214 | private: |
---|
215 | |
---|
216 | template <typename KT, typename VT> |
---|
217 | class StaticVectorMap { |
---|
218 | public: |
---|
219 | typedef KT Key; |
---|
220 | typedef VT Value; |
---|
221 | |
---|
222 | StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
---|
223 | |
---|
224 | const Value& operator[](const Key& key) const { |
---|
225 | return _v[StaticDigraph::id(key)]; |
---|
226 | } |
---|
227 | |
---|
228 | Value& operator[](const Key& key) { |
---|
229 | return _v[StaticDigraph::id(key)]; |
---|
230 | } |
---|
231 | |
---|
232 | void set(const Key& key, const Value& val) { |
---|
233 | _v[StaticDigraph::id(key)] = val; |
---|
234 | } |
---|
235 | |
---|
236 | private: |
---|
237 | std::vector<Value>& _v; |
---|
238 | }; |
---|
239 | |
---|
240 | typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
---|
241 | typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
---|
242 | |
---|
243 | private: |
---|
244 | |
---|
245 | // Data related to the underlying digraph |
---|
246 | const GR &_graph; |
---|
247 | int _node_num; |
---|
248 | int _arc_num; |
---|
249 | int _res_node_num; |
---|
250 | int _res_arc_num; |
---|
251 | int _root; |
---|
252 | |
---|
253 | // Parameters of the problem |
---|
254 | bool _have_lower; |
---|
255 | Value _sum_supply; |
---|
256 | int _sup_node_num; |
---|
257 | |
---|
258 | // Data structures for storing the digraph |
---|
259 | IntNodeMap _node_id; |
---|
260 | IntArcMap _arc_idf; |
---|
261 | IntArcMap _arc_idb; |
---|
262 | IntVector _first_out; |
---|
263 | BoolVector _forward; |
---|
264 | IntVector _source; |
---|
265 | IntVector _target; |
---|
266 | IntVector _reverse; |
---|
267 | |
---|
268 | // Node and arc data |
---|
269 | ValueVector _lower; |
---|
270 | ValueVector _upper; |
---|
271 | CostVector _scost; |
---|
272 | ValueVector _supply; |
---|
273 | |
---|
274 | ValueVector _res_cap; |
---|
275 | LargeCostVector _cost; |
---|
276 | LargeCostVector _pi; |
---|
277 | ValueVector _excess; |
---|
278 | IntVector _next_out; |
---|
279 | std::deque<int> _active_nodes; |
---|
280 | |
---|
281 | // Data for scaling |
---|
282 | LargeCost _epsilon; |
---|
283 | int _alpha; |
---|
284 | |
---|
285 | IntVector _buckets; |
---|
286 | IntVector _bucket_next; |
---|
287 | IntVector _bucket_prev; |
---|
288 | IntVector _rank; |
---|
289 | int _max_rank; |
---|
290 | |
---|
291 | // Data for a StaticDigraph structure |
---|
292 | typedef std::pair<int, int> IntPair; |
---|
293 | StaticDigraph _sgr; |
---|
294 | std::vector<IntPair> _arc_vec; |
---|
295 | std::vector<LargeCost> _cost_vec; |
---|
296 | LargeCostArcMap _cost_map; |
---|
297 | LargeCostNodeMap _pi_map; |
---|
298 | |
---|
299 | public: |
---|
300 | |
---|
301 | /// \brief Constant for infinite upper bounds (capacities). |
---|
302 | /// |
---|
303 | /// Constant for infinite upper bounds (capacities). |
---|
304 | /// It is \c std::numeric_limits<Value>::infinity() if available, |
---|
305 | /// \c std::numeric_limits<Value>::max() otherwise. |
---|
306 | const Value INF; |
---|
307 | |
---|
308 | public: |
---|
309 | |
---|
310 | /// \name Named Template Parameters |
---|
311 | /// @{ |
---|
312 | |
---|
313 | template <typename T> |
---|
314 | struct SetLargeCostTraits : public Traits { |
---|
315 | typedef T LargeCost; |
---|
316 | }; |
---|
317 | |
---|
318 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
319 | /// \c LargeCost type. |
---|
320 | /// |
---|
321 | /// \ref named-templ-param "Named parameter" for setting \c LargeCost |
---|
322 | /// type, which is used for internal computations in the algorithm. |
---|
323 | /// \c Cost must be convertible to \c LargeCost. |
---|
324 | template <typename T> |
---|
325 | struct SetLargeCost |
---|
326 | : public CostScaling<GR, V, C, SetLargeCostTraits<T> > { |
---|
327 | typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
---|
328 | }; |
---|
329 | |
---|
330 | /// @} |
---|
331 | |
---|
332 | protected: |
---|
333 | |
---|
334 | CostScaling() {} |
---|
335 | |
---|
336 | public: |
---|
337 | |
---|
338 | /// \brief Constructor. |
---|
339 | /// |
---|
340 | /// The constructor of the class. |
---|
341 | /// |
---|
342 | /// \param graph The digraph the algorithm runs on. |
---|
343 | CostScaling(const GR& graph) : |
---|
344 | _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
---|
345 | _cost_map(_cost_vec), _pi_map(_pi), |
---|
346 | INF(std::numeric_limits<Value>::has_infinity ? |
---|
347 | std::numeric_limits<Value>::infinity() : |
---|
348 | std::numeric_limits<Value>::max()) |
---|
349 | { |
---|
350 | // Check the number types |
---|
351 | LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
---|
352 | "The flow type of CostScaling must be signed"); |
---|
353 | LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
---|
354 | "The cost type of CostScaling must be signed"); |
---|
355 | |
---|
356 | // Reset data structures |
---|
357 | reset(); |
---|
358 | } |
---|
359 | |
---|
360 | /// \name Parameters |
---|
361 | /// The parameters of the algorithm can be specified using these |
---|
362 | /// functions. |
---|
363 | |
---|
364 | /// @{ |
---|
365 | |
---|
366 | /// \brief Set the lower bounds on the arcs. |
---|
367 | /// |
---|
368 | /// This function sets the lower bounds on the arcs. |
---|
369 | /// If it is not used before calling \ref run(), the lower bounds |
---|
370 | /// will be set to zero on all arcs. |
---|
371 | /// |
---|
372 | /// \param map An arc map storing the lower bounds. |
---|
373 | /// Its \c Value type must be convertible to the \c Value type |
---|
374 | /// of the algorithm. |
---|
375 | /// |
---|
376 | /// \return <tt>(*this)</tt> |
---|
377 | template <typename LowerMap> |
---|
378 | CostScaling& lowerMap(const LowerMap& map) { |
---|
379 | _have_lower = true; |
---|
380 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
381 | _lower[_arc_idf[a]] = map[a]; |
---|
382 | _lower[_arc_idb[a]] = map[a]; |
---|
383 | } |
---|
384 | return *this; |
---|
385 | } |
---|
386 | |
---|
387 | /// \brief Set the upper bounds (capacities) on the arcs. |
---|
388 | /// |
---|
389 | /// This function sets the upper bounds (capacities) on the arcs. |
---|
390 | /// If it is not used before calling \ref run(), the upper bounds |
---|
391 | /// will be set to \ref INF on all arcs (i.e. the flow value will be |
---|
392 | /// unbounded from above). |
---|
393 | /// |
---|
394 | /// \param map An arc map storing the upper bounds. |
---|
395 | /// Its \c Value type must be convertible to the \c Value type |
---|
396 | /// of the algorithm. |
---|
397 | /// |
---|
398 | /// \return <tt>(*this)</tt> |
---|
399 | template<typename UpperMap> |
---|
400 | CostScaling& upperMap(const UpperMap& map) { |
---|
401 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
402 | _upper[_arc_idf[a]] = map[a]; |
---|
403 | } |
---|
404 | return *this; |
---|
405 | } |
---|
406 | |
---|
407 | /// \brief Set the costs of the arcs. |
---|
408 | /// |
---|
409 | /// This function sets the costs of the arcs. |
---|
410 | /// If it is not used before calling \ref run(), the costs |
---|
411 | /// will be set to \c 1 on all arcs. |
---|
412 | /// |
---|
413 | /// \param map An arc map storing the costs. |
---|
414 | /// Its \c Value type must be convertible to the \c Cost type |
---|
415 | /// of the algorithm. |
---|
416 | /// |
---|
417 | /// \return <tt>(*this)</tt> |
---|
418 | template<typename CostMap> |
---|
419 | CostScaling& costMap(const CostMap& map) { |
---|
420 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
421 | _scost[_arc_idf[a]] = map[a]; |
---|
422 | _scost[_arc_idb[a]] = -map[a]; |
---|
423 | } |
---|
424 | return *this; |
---|
425 | } |
---|
426 | |
---|
427 | /// \brief Set the supply values of the nodes. |
---|
428 | /// |
---|
429 | /// This function sets the supply values of the nodes. |
---|
430 | /// If neither this function nor \ref stSupply() is used before |
---|
431 | /// calling \ref run(), the supply of each node will be set to zero. |
---|
432 | /// |
---|
433 | /// \param map A node map storing the supply values. |
---|
434 | /// Its \c Value type must be convertible to the \c Value type |
---|
435 | /// of the algorithm. |
---|
436 | /// |
---|
437 | /// \return <tt>(*this)</tt> |
---|
438 | template<typename SupplyMap> |
---|
439 | CostScaling& supplyMap(const SupplyMap& map) { |
---|
440 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
441 | _supply[_node_id[n]] = map[n]; |
---|
442 | } |
---|
443 | return *this; |
---|
444 | } |
---|
445 | |
---|
446 | /// \brief Set single source and target nodes and a supply value. |
---|
447 | /// |
---|
448 | /// This function sets a single source node and a single target node |
---|
449 | /// and the required flow value. |
---|
450 | /// If neither this function nor \ref supplyMap() is used before |
---|
451 | /// calling \ref run(), the supply of each node will be set to zero. |
---|
452 | /// |
---|
453 | /// Using this function has the same effect as using \ref supplyMap() |
---|
454 | /// with a map in which \c k is assigned to \c s, \c -k is |
---|
455 | /// assigned to \c t and all other nodes have zero supply value. |
---|
456 | /// |
---|
457 | /// \param s The source node. |
---|
458 | /// \param t The target node. |
---|
459 | /// \param k The required amount of flow from node \c s to node \c t |
---|
460 | /// (i.e. the supply of \c s and the demand of \c t). |
---|
461 | /// |
---|
462 | /// \return <tt>(*this)</tt> |
---|
463 | CostScaling& stSupply(const Node& s, const Node& t, Value k) { |
---|
464 | for (int i = 0; i != _res_node_num; ++i) { |
---|
465 | _supply[i] = 0; |
---|
466 | } |
---|
467 | _supply[_node_id[s]] = k; |
---|
468 | _supply[_node_id[t]] = -k; |
---|
469 | return *this; |
---|
470 | } |
---|
471 | |
---|
472 | /// @} |
---|
473 | |
---|
474 | /// \name Execution control |
---|
475 | /// The algorithm can be executed using \ref run(). |
---|
476 | |
---|
477 | /// @{ |
---|
478 | |
---|
479 | /// \brief Run the algorithm. |
---|
480 | /// |
---|
481 | /// This function runs the algorithm. |
---|
482 | /// The paramters can be specified using functions \ref lowerMap(), |
---|
483 | /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
---|
484 | /// For example, |
---|
485 | /// \code |
---|
486 | /// CostScaling<ListDigraph> cs(graph); |
---|
487 | /// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
---|
488 | /// .supplyMap(sup).run(); |
---|
489 | /// \endcode |
---|
490 | /// |
---|
491 | /// This function can be called more than once. All the given parameters |
---|
492 | /// are kept for the next call, unless \ref resetParams() or \ref reset() |
---|
493 | /// is used, thus only the modified parameters have to be set again. |
---|
494 | /// If the underlying digraph was also modified after the construction |
---|
495 | /// of the class (or the last \ref reset() call), then the \ref reset() |
---|
496 | /// function must be called. |
---|
497 | /// |
---|
498 | /// \param method The internal method that will be used in the |
---|
499 | /// algorithm. For more information, see \ref Method. |
---|
500 | /// \param factor The cost scaling factor. It must be larger than one. |
---|
501 | /// |
---|
502 | /// \return \c INFEASIBLE if no feasible flow exists, |
---|
503 | /// \n \c OPTIMAL if the problem has optimal solution |
---|
504 | /// (i.e. it is feasible and bounded), and the algorithm has found |
---|
505 | /// optimal flow and node potentials (primal and dual solutions), |
---|
506 | /// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
---|
507 | /// and infinite upper bound. It means that the objective function |
---|
508 | /// is unbounded on that arc, however, note that it could actually be |
---|
509 | /// bounded over the feasible flows, but this algroithm cannot handle |
---|
510 | /// these cases. |
---|
511 | /// |
---|
512 | /// \see ProblemType, Method |
---|
513 | /// \see resetParams(), reset() |
---|
514 | ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) { |
---|
515 | _alpha = factor; |
---|
516 | ProblemType pt = init(); |
---|
517 | if (pt != OPTIMAL) return pt; |
---|
518 | start(method); |
---|
519 | return OPTIMAL; |
---|
520 | } |
---|
521 | |
---|
522 | /// \brief Reset all the parameters that have been given before. |
---|
523 | /// |
---|
524 | /// This function resets all the paramaters that have been given |
---|
525 | /// before using functions \ref lowerMap(), \ref upperMap(), |
---|
526 | /// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
---|
527 | /// |
---|
528 | /// It is useful for multiple \ref run() calls. Basically, all the given |
---|
529 | /// parameters are kept for the next \ref run() call, unless |
---|
530 | /// \ref resetParams() or \ref reset() is used. |
---|
531 | /// If the underlying digraph was also modified after the construction |
---|
532 | /// of the class or the last \ref reset() call, then the \ref reset() |
---|
533 | /// function must be used, otherwise \ref resetParams() is sufficient. |
---|
534 | /// |
---|
535 | /// For example, |
---|
536 | /// \code |
---|
537 | /// CostScaling<ListDigraph> cs(graph); |
---|
538 | /// |
---|
539 | /// // First run |
---|
540 | /// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
---|
541 | /// .supplyMap(sup).run(); |
---|
542 | /// |
---|
543 | /// // Run again with modified cost map (resetParams() is not called, |
---|
544 | /// // so only the cost map have to be set again) |
---|
545 | /// cost[e] += 100; |
---|
546 | /// cs.costMap(cost).run(); |
---|
547 | /// |
---|
548 | /// // Run again from scratch using resetParams() |
---|
549 | /// // (the lower bounds will be set to zero on all arcs) |
---|
550 | /// cs.resetParams(); |
---|
551 | /// cs.upperMap(capacity).costMap(cost) |
---|
552 | /// .supplyMap(sup).run(); |
---|
553 | /// \endcode |
---|
554 | /// |
---|
555 | /// \return <tt>(*this)</tt> |
---|
556 | /// |
---|
557 | /// \see reset(), run() |
---|
558 | CostScaling& resetParams() { |
---|
559 | for (int i = 0; i != _res_node_num; ++i) { |
---|
560 | _supply[i] = 0; |
---|
561 | } |
---|
562 | int limit = _first_out[_root]; |
---|
563 | for (int j = 0; j != limit; ++j) { |
---|
564 | _lower[j] = 0; |
---|
565 | _upper[j] = INF; |
---|
566 | _scost[j] = _forward[j] ? 1 : -1; |
---|
567 | } |
---|
568 | for (int j = limit; j != _res_arc_num; ++j) { |
---|
569 | _lower[j] = 0; |
---|
570 | _upper[j] = INF; |
---|
571 | _scost[j] = 0; |
---|
572 | _scost[_reverse[j]] = 0; |
---|
573 | } |
---|
574 | _have_lower = false; |
---|
575 | return *this; |
---|
576 | } |
---|
577 | |
---|
578 | /// \brief Reset all the parameters that have been given before. |
---|
579 | /// |
---|
580 | /// This function resets all the paramaters that have been given |
---|
581 | /// before using functions \ref lowerMap(), \ref upperMap(), |
---|
582 | /// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
---|
583 | /// |
---|
584 | /// It is useful for multiple run() calls. If this function is not |
---|
585 | /// used, all the parameters given before are kept for the next |
---|
586 | /// \ref run() call. |
---|
587 | /// However, the underlying digraph must not be modified after this |
---|
588 | /// class have been constructed, since it copies and extends the graph. |
---|
589 | /// \return <tt>(*this)</tt> |
---|
590 | CostScaling& reset() { |
---|
591 | // Resize vectors |
---|
592 | _node_num = countNodes(_graph); |
---|
593 | _arc_num = countArcs(_graph); |
---|
594 | _res_node_num = _node_num + 1; |
---|
595 | _res_arc_num = 2 * (_arc_num + _node_num); |
---|
596 | _root = _node_num; |
---|
597 | |
---|
598 | _first_out.resize(_res_node_num + 1); |
---|
599 | _forward.resize(_res_arc_num); |
---|
600 | _source.resize(_res_arc_num); |
---|
601 | _target.resize(_res_arc_num); |
---|
602 | _reverse.resize(_res_arc_num); |
---|
603 | |
---|
604 | _lower.resize(_res_arc_num); |
---|
605 | _upper.resize(_res_arc_num); |
---|
606 | _scost.resize(_res_arc_num); |
---|
607 | _supply.resize(_res_node_num); |
---|
608 | |
---|
609 | _res_cap.resize(_res_arc_num); |
---|
610 | _cost.resize(_res_arc_num); |
---|
611 | _pi.resize(_res_node_num); |
---|
612 | _excess.resize(_res_node_num); |
---|
613 | _next_out.resize(_res_node_num); |
---|
614 | |
---|
615 | _arc_vec.reserve(_res_arc_num); |
---|
616 | _cost_vec.reserve(_res_arc_num); |
---|
617 | |
---|
618 | // Copy the graph |
---|
619 | int i = 0, j = 0, k = 2 * _arc_num + _node_num; |
---|
620 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
---|
621 | _node_id[n] = i; |
---|
622 | } |
---|
623 | i = 0; |
---|
624 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
---|
625 | _first_out[i] = j; |
---|
626 | for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
---|
627 | _arc_idf[a] = j; |
---|
628 | _forward[j] = true; |
---|
629 | _source[j] = i; |
---|
630 | _target[j] = _node_id[_graph.runningNode(a)]; |
---|
631 | } |
---|
632 | for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
---|
633 | _arc_idb[a] = j; |
---|
634 | _forward[j] = false; |
---|
635 | _source[j] = i; |
---|
636 | _target[j] = _node_id[_graph.runningNode(a)]; |
---|
637 | } |
---|
638 | _forward[j] = false; |
---|
639 | _source[j] = i; |
---|
640 | _target[j] = _root; |
---|
641 | _reverse[j] = k; |
---|
642 | _forward[k] = true; |
---|
643 | _source[k] = _root; |
---|
644 | _target[k] = i; |
---|
645 | _reverse[k] = j; |
---|
646 | ++j; ++k; |
---|
647 | } |
---|
648 | _first_out[i] = j; |
---|
649 | _first_out[_res_node_num] = k; |
---|
650 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
651 | int fi = _arc_idf[a]; |
---|
652 | int bi = _arc_idb[a]; |
---|
653 | _reverse[fi] = bi; |
---|
654 | _reverse[bi] = fi; |
---|
655 | } |
---|
656 | |
---|
657 | // Reset parameters |
---|
658 | resetParams(); |
---|
659 | return *this; |
---|
660 | } |
---|
661 | |
---|
662 | /// @} |
---|
663 | |
---|
664 | /// \name Query Functions |
---|
665 | /// The results of the algorithm can be obtained using these |
---|
666 | /// functions.\n |
---|
667 | /// The \ref run() function must be called before using them. |
---|
668 | |
---|
669 | /// @{ |
---|
670 | |
---|
671 | /// \brief Return the total cost of the found flow. |
---|
672 | /// |
---|
673 | /// This function returns the total cost of the found flow. |
---|
674 | /// Its complexity is O(e). |
---|
675 | /// |
---|
676 | /// \note The return type of the function can be specified as a |
---|
677 | /// template parameter. For example, |
---|
678 | /// \code |
---|
679 | /// cs.totalCost<double>(); |
---|
680 | /// \endcode |
---|
681 | /// It is useful if the total cost cannot be stored in the \c Cost |
---|
682 | /// type of the algorithm, which is the default return type of the |
---|
683 | /// function. |
---|
684 | /// |
---|
685 | /// \pre \ref run() must be called before using this function. |
---|
686 | template <typename Number> |
---|
687 | Number totalCost() const { |
---|
688 | Number c = 0; |
---|
689 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
690 | int i = _arc_idb[a]; |
---|
691 | c += static_cast<Number>(_res_cap[i]) * |
---|
692 | (-static_cast<Number>(_scost[i])); |
---|
693 | } |
---|
694 | return c; |
---|
695 | } |
---|
696 | |
---|
697 | #ifndef DOXYGEN |
---|
698 | Cost totalCost() const { |
---|
699 | return totalCost<Cost>(); |
---|
700 | } |
---|
701 | #endif |
---|
702 | |
---|
703 | /// \brief Return the flow on the given arc. |
---|
704 | /// |
---|
705 | /// This function returns the flow on the given arc. |
---|
706 | /// |
---|
707 | /// \pre \ref run() must be called before using this function. |
---|
708 | Value flow(const Arc& a) const { |
---|
709 | return _res_cap[_arc_idb[a]]; |
---|
710 | } |
---|
711 | |
---|
712 | /// \brief Return the flow map (the primal solution). |
---|
713 | /// |
---|
714 | /// This function copies the flow value on each arc into the given |
---|
715 | /// map. The \c Value type of the algorithm must be convertible to |
---|
716 | /// the \c Value type of the map. |
---|
717 | /// |
---|
718 | /// \pre \ref run() must be called before using this function. |
---|
719 | template <typename FlowMap> |
---|
720 | void flowMap(FlowMap &map) const { |
---|
721 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
722 | map.set(a, _res_cap[_arc_idb[a]]); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | /// \brief Return the potential (dual value) of the given node. |
---|
727 | /// |
---|
728 | /// This function returns the potential (dual value) of the |
---|
729 | /// given node. |
---|
730 | /// |
---|
731 | /// \pre \ref run() must be called before using this function. |
---|
732 | Cost potential(const Node& n) const { |
---|
733 | return static_cast<Cost>(_pi[_node_id[n]]); |
---|
734 | } |
---|
735 | |
---|
736 | /// \brief Return the potential map (the dual solution). |
---|
737 | /// |
---|
738 | /// This function copies the potential (dual value) of each node |
---|
739 | /// into the given map. |
---|
740 | /// The \c Cost type of the algorithm must be convertible to the |
---|
741 | /// \c Value type of the map. |
---|
742 | /// |
---|
743 | /// \pre \ref run() must be called before using this function. |
---|
744 | template <typename PotentialMap> |
---|
745 | void potentialMap(PotentialMap &map) const { |
---|
746 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
747 | map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
---|
748 | } |
---|
749 | } |
---|
750 | |
---|
751 | /// @} |
---|
752 | |
---|
753 | private: |
---|
754 | |
---|
755 | // Initialize the algorithm |
---|
756 | ProblemType init() { |
---|
757 | if (_res_node_num <= 1) return INFEASIBLE; |
---|
758 | |
---|
759 | // Check the sum of supply values |
---|
760 | _sum_supply = 0; |
---|
761 | for (int i = 0; i != _root; ++i) { |
---|
762 | _sum_supply += _supply[i]; |
---|
763 | } |
---|
764 | if (_sum_supply > 0) return INFEASIBLE; |
---|
765 | |
---|
766 | |
---|
767 | // Initialize vectors |
---|
768 | for (int i = 0; i != _res_node_num; ++i) { |
---|
769 | _pi[i] = 0; |
---|
770 | _excess[i] = _supply[i]; |
---|
771 | } |
---|
772 | |
---|
773 | // Remove infinite upper bounds and check negative arcs |
---|
774 | const Value MAX = std::numeric_limits<Value>::max(); |
---|
775 | int last_out; |
---|
776 | if (_have_lower) { |
---|
777 | for (int i = 0; i != _root; ++i) { |
---|
778 | last_out = _first_out[i+1]; |
---|
779 | for (int j = _first_out[i]; j != last_out; ++j) { |
---|
780 | if (_forward[j]) { |
---|
781 | Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; |
---|
782 | if (c >= MAX) return UNBOUNDED; |
---|
783 | _excess[i] -= c; |
---|
784 | _excess[_target[j]] += c; |
---|
785 | } |
---|
786 | } |
---|
787 | } |
---|
788 | } else { |
---|
789 | for (int i = 0; i != _root; ++i) { |
---|
790 | last_out = _first_out[i+1]; |
---|
791 | for (int j = _first_out[i]; j != last_out; ++j) { |
---|
792 | if (_forward[j] && _scost[j] < 0) { |
---|
793 | Value c = _upper[j]; |
---|
794 | if (c >= MAX) return UNBOUNDED; |
---|
795 | _excess[i] -= c; |
---|
796 | _excess[_target[j]] += c; |
---|
797 | } |
---|
798 | } |
---|
799 | } |
---|
800 | } |
---|
801 | Value ex, max_cap = 0; |
---|
802 | for (int i = 0; i != _res_node_num; ++i) { |
---|
803 | ex = _excess[i]; |
---|
804 | _excess[i] = 0; |
---|
805 | if (ex < 0) max_cap -= ex; |
---|
806 | } |
---|
807 | for (int j = 0; j != _res_arc_num; ++j) { |
---|
808 | if (_upper[j] >= MAX) _upper[j] = max_cap; |
---|
809 | } |
---|
810 | |
---|
811 | // Initialize the large cost vector and the epsilon parameter |
---|
812 | _epsilon = 0; |
---|
813 | LargeCost lc; |
---|
814 | for (int i = 0; i != _root; ++i) { |
---|
815 | last_out = _first_out[i+1]; |
---|
816 | for (int j = _first_out[i]; j != last_out; ++j) { |
---|
817 | lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha; |
---|
818 | _cost[j] = lc; |
---|
819 | if (lc > _epsilon) _epsilon = lc; |
---|
820 | } |
---|
821 | } |
---|
822 | _epsilon /= _alpha; |
---|
823 | |
---|
824 | // Initialize maps for Circulation and remove non-zero lower bounds |
---|
825 | ConstMap<Arc, Value> low(0); |
---|
826 | typedef typename Digraph::template ArcMap<Value> ValueArcMap; |
---|
827 | typedef typename Digraph::template NodeMap<Value> ValueNodeMap; |
---|
828 | ValueArcMap cap(_graph), flow(_graph); |
---|
829 | ValueNodeMap sup(_graph); |
---|
830 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
831 | sup[n] = _supply[_node_id[n]]; |
---|
832 | } |
---|
833 | if (_have_lower) { |
---|
834 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
835 | int j = _arc_idf[a]; |
---|
836 | Value c = _lower[j]; |
---|
837 | cap[a] = _upper[j] - c; |
---|
838 | sup[_graph.source(a)] -= c; |
---|
839 | sup[_graph.target(a)] += c; |
---|
840 | } |
---|
841 | } else { |
---|
842 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
843 | cap[a] = _upper[_arc_idf[a]]; |
---|
844 | } |
---|
845 | } |
---|
846 | |
---|
847 | _sup_node_num = 0; |
---|
848 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
849 | if (sup[n] > 0) ++_sup_node_num; |
---|
850 | } |
---|
851 | |
---|
852 | // Find a feasible flow using Circulation |
---|
853 | Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap> |
---|
854 | circ(_graph, low, cap, sup); |
---|
855 | if (!circ.flowMap(flow).run()) return INFEASIBLE; |
---|
856 | |
---|
857 | // Set residual capacities and handle GEQ supply type |
---|
858 | if (_sum_supply < 0) { |
---|
859 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
860 | Value fa = flow[a]; |
---|
861 | _res_cap[_arc_idf[a]] = cap[a] - fa; |
---|
862 | _res_cap[_arc_idb[a]] = fa; |
---|
863 | sup[_graph.source(a)] -= fa; |
---|
864 | sup[_graph.target(a)] += fa; |
---|
865 | } |
---|
866 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
867 | _excess[_node_id[n]] = sup[n]; |
---|
868 | } |
---|
869 | for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
---|
870 | int u = _target[a]; |
---|
871 | int ra = _reverse[a]; |
---|
872 | _res_cap[a] = -_sum_supply + 1; |
---|
873 | _res_cap[ra] = -_excess[u]; |
---|
874 | _cost[a] = 0; |
---|
875 | _cost[ra] = 0; |
---|
876 | _excess[u] = 0; |
---|
877 | } |
---|
878 | } else { |
---|
879 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
880 | Value fa = flow[a]; |
---|
881 | _res_cap[_arc_idf[a]] = cap[a] - fa; |
---|
882 | _res_cap[_arc_idb[a]] = fa; |
---|
883 | } |
---|
884 | for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
---|
885 | int ra = _reverse[a]; |
---|
886 | _res_cap[a] = 0; |
---|
887 | _res_cap[ra] = 0; |
---|
888 | _cost[a] = 0; |
---|
889 | _cost[ra] = 0; |
---|
890 | } |
---|
891 | } |
---|
892 | |
---|
893 | return OPTIMAL; |
---|
894 | } |
---|
895 | |
---|
896 | // Execute the algorithm and transform the results |
---|
897 | void start(Method method) { |
---|
898 | // Maximum path length for partial augment |
---|
899 | const int MAX_PATH_LENGTH = 4; |
---|
900 | |
---|
901 | // Initialize data structures for buckets |
---|
902 | _max_rank = _alpha * _res_node_num; |
---|
903 | _buckets.resize(_max_rank); |
---|
904 | _bucket_next.resize(_res_node_num + 1); |
---|
905 | _bucket_prev.resize(_res_node_num + 1); |
---|
906 | _rank.resize(_res_node_num + 1); |
---|
907 | |
---|
908 | // Execute the algorithm |
---|
909 | switch (method) { |
---|
910 | case PUSH: |
---|
911 | startPush(); |
---|
912 | break; |
---|
913 | case AUGMENT: |
---|
914 | startAugment(_res_node_num - 1); |
---|
915 | break; |
---|
916 | case PARTIAL_AUGMENT: |
---|
917 | startAugment(MAX_PATH_LENGTH); |
---|
918 | break; |
---|
919 | } |
---|
920 | |
---|
921 | // Compute node potentials for the original costs |
---|
922 | _arc_vec.clear(); |
---|
923 | _cost_vec.clear(); |
---|
924 | for (int j = 0; j != _res_arc_num; ++j) { |
---|
925 | if (_res_cap[j] > 0) { |
---|
926 | _arc_vec.push_back(IntPair(_source[j], _target[j])); |
---|
927 | _cost_vec.push_back(_scost[j]); |
---|
928 | } |
---|
929 | } |
---|
930 | _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
---|
931 | |
---|
932 | typename BellmanFord<StaticDigraph, LargeCostArcMap> |
---|
933 | ::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map); |
---|
934 | bf.distMap(_pi_map); |
---|
935 | bf.init(0); |
---|
936 | bf.start(); |
---|
937 | |
---|
938 | // Handle non-zero lower bounds |
---|
939 | if (_have_lower) { |
---|
940 | int limit = _first_out[_root]; |
---|
941 | for (int j = 0; j != limit; ++j) { |
---|
942 | if (!_forward[j]) _res_cap[j] += _lower[j]; |
---|
943 | } |
---|
944 | } |
---|
945 | } |
---|
946 | |
---|
947 | // Initialize a cost scaling phase |
---|
948 | void initPhase() { |
---|
949 | // Saturate arcs not satisfying the optimality condition |
---|
950 | for (int u = 0; u != _res_node_num; ++u) { |
---|
951 | int last_out = _first_out[u+1]; |
---|
952 | LargeCost pi_u = _pi[u]; |
---|
953 | for (int a = _first_out[u]; a != last_out; ++a) { |
---|
954 | int v = _target[a]; |
---|
955 | if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) { |
---|
956 | Value delta = _res_cap[a]; |
---|
957 | _excess[u] -= delta; |
---|
958 | _excess[v] += delta; |
---|
959 | _res_cap[a] = 0; |
---|
960 | _res_cap[_reverse[a]] += delta; |
---|
961 | } |
---|
962 | } |
---|
963 | } |
---|
964 | |
---|
965 | // Find active nodes (i.e. nodes with positive excess) |
---|
966 | for (int u = 0; u != _res_node_num; ++u) { |
---|
967 | if (_excess[u] > 0) _active_nodes.push_back(u); |
---|
968 | } |
---|
969 | |
---|
970 | // Initialize the next arcs |
---|
971 | for (int u = 0; u != _res_node_num; ++u) { |
---|
972 | _next_out[u] = _first_out[u]; |
---|
973 | } |
---|
974 | } |
---|
975 | |
---|
976 | // Early termination heuristic |
---|
977 | bool earlyTermination() { |
---|
978 | const double EARLY_TERM_FACTOR = 3.0; |
---|
979 | |
---|
980 | // Build a static residual graph |
---|
981 | _arc_vec.clear(); |
---|
982 | _cost_vec.clear(); |
---|
983 | for (int j = 0; j != _res_arc_num; ++j) { |
---|
984 | if (_res_cap[j] > 0) { |
---|
985 | _arc_vec.push_back(IntPair(_source[j], _target[j])); |
---|
986 | _cost_vec.push_back(_cost[j] + 1); |
---|
987 | } |
---|
988 | } |
---|
989 | _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
---|
990 | |
---|
991 | // Run Bellman-Ford algorithm to check if the current flow is optimal |
---|
992 | BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
---|
993 | bf.init(0); |
---|
994 | bool done = false; |
---|
995 | int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num))); |
---|
996 | for (int i = 0; i < K && !done; ++i) { |
---|
997 | done = bf.processNextWeakRound(); |
---|
998 | } |
---|
999 | return done; |
---|
1000 | } |
---|
1001 | |
---|
1002 | // Global potential update heuristic |
---|
1003 | void globalUpdate() { |
---|
1004 | int bucket_end = _root + 1; |
---|
1005 | |
---|
1006 | // Initialize buckets |
---|
1007 | for (int r = 0; r != _max_rank; ++r) { |
---|
1008 | _buckets[r] = bucket_end; |
---|
1009 | } |
---|
1010 | Value total_excess = 0; |
---|
1011 | for (int i = 0; i != _res_node_num; ++i) { |
---|
1012 | if (_excess[i] < 0) { |
---|
1013 | _rank[i] = 0; |
---|
1014 | _bucket_next[i] = _buckets[0]; |
---|
1015 | _bucket_prev[_buckets[0]] = i; |
---|
1016 | _buckets[0] = i; |
---|
1017 | } else { |
---|
1018 | total_excess += _excess[i]; |
---|
1019 | _rank[i] = _max_rank; |
---|
1020 | } |
---|
1021 | } |
---|
1022 | if (total_excess == 0) return; |
---|
1023 | |
---|
1024 | // Search the buckets |
---|
1025 | int r = 0; |
---|
1026 | for ( ; r != _max_rank; ++r) { |
---|
1027 | while (_buckets[r] != bucket_end) { |
---|
1028 | // Remove the first node from the current bucket |
---|
1029 | int u = _buckets[r]; |
---|
1030 | _buckets[r] = _bucket_next[u]; |
---|
1031 | |
---|
1032 | // Search the incomming arcs of u |
---|
1033 | LargeCost pi_u = _pi[u]; |
---|
1034 | int last_out = _first_out[u+1]; |
---|
1035 | for (int a = _first_out[u]; a != last_out; ++a) { |
---|
1036 | int ra = _reverse[a]; |
---|
1037 | if (_res_cap[ra] > 0) { |
---|
1038 | int v = _source[ra]; |
---|
1039 | int old_rank_v = _rank[v]; |
---|
1040 | if (r < old_rank_v) { |
---|
1041 | // Compute the new rank of v |
---|
1042 | LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon; |
---|
1043 | int new_rank_v = old_rank_v; |
---|
1044 | if (nrc < LargeCost(_max_rank)) |
---|
1045 | new_rank_v = r + 1 + int(nrc); |
---|
1046 | |
---|
1047 | // Change the rank of v |
---|
1048 | if (new_rank_v < old_rank_v) { |
---|
1049 | _rank[v] = new_rank_v; |
---|
1050 | _next_out[v] = _first_out[v]; |
---|
1051 | |
---|
1052 | // Remove v from its old bucket |
---|
1053 | if (old_rank_v < _max_rank) { |
---|
1054 | if (_buckets[old_rank_v] == v) { |
---|
1055 | _buckets[old_rank_v] = _bucket_next[v]; |
---|
1056 | } else { |
---|
1057 | _bucket_next[_bucket_prev[v]] = _bucket_next[v]; |
---|
1058 | _bucket_prev[_bucket_next[v]] = _bucket_prev[v]; |
---|
1059 | } |
---|
1060 | } |
---|
1061 | |
---|
1062 | // Insert v to its new bucket |
---|
1063 | _bucket_next[v] = _buckets[new_rank_v]; |
---|
1064 | _bucket_prev[_buckets[new_rank_v]] = v; |
---|
1065 | _buckets[new_rank_v] = v; |
---|
1066 | } |
---|
1067 | } |
---|
1068 | } |
---|
1069 | } |
---|
1070 | |
---|
1071 | // Finish search if there are no more active nodes |
---|
1072 | if (_excess[u] > 0) { |
---|
1073 | total_excess -= _excess[u]; |
---|
1074 | if (total_excess <= 0) break; |
---|
1075 | } |
---|
1076 | } |
---|
1077 | if (total_excess <= 0) break; |
---|
1078 | } |
---|
1079 | |
---|
1080 | // Relabel nodes |
---|
1081 | for (int u = 0; u != _res_node_num; ++u) { |
---|
1082 | int k = std::min(_rank[u], r); |
---|
1083 | if (k > 0) { |
---|
1084 | _pi[u] -= _epsilon * k; |
---|
1085 | _next_out[u] = _first_out[u]; |
---|
1086 | } |
---|
1087 | } |
---|
1088 | } |
---|
1089 | |
---|
1090 | /// Execute the algorithm performing augment and relabel operations |
---|
1091 | void startAugment(int max_length) { |
---|
1092 | // Paramters for heuristics |
---|
1093 | const int EARLY_TERM_EPSILON_LIMIT = 1000; |
---|
1094 | const double GLOBAL_UPDATE_FACTOR = 3.0; |
---|
1095 | |
---|
1096 | const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
---|
1097 | (_res_node_num + _sup_node_num * _sup_node_num)); |
---|
1098 | int next_update_limit = global_update_freq; |
---|
1099 | |
---|
1100 | int relabel_cnt = 0; |
---|
1101 | |
---|
1102 | // Perform cost scaling phases |
---|
1103 | std::vector<int> path; |
---|
1104 | for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
---|
1105 | 1 : _epsilon / _alpha ) |
---|
1106 | { |
---|
1107 | // Early termination heuristic |
---|
1108 | if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) { |
---|
1109 | if (earlyTermination()) break; |
---|
1110 | } |
---|
1111 | |
---|
1112 | // Initialize current phase |
---|
1113 | initPhase(); |
---|
1114 | |
---|
1115 | // Perform partial augment and relabel operations |
---|
1116 | while (true) { |
---|
1117 | // Select an active node (FIFO selection) |
---|
1118 | while (_active_nodes.size() > 0 && |
---|
1119 | _excess[_active_nodes.front()] <= 0) { |
---|
1120 | _active_nodes.pop_front(); |
---|
1121 | } |
---|
1122 | if (_active_nodes.size() == 0) break; |
---|
1123 | int start = _active_nodes.front(); |
---|
1124 | |
---|
1125 | // Find an augmenting path from the start node |
---|
1126 | path.clear(); |
---|
1127 | int tip = start; |
---|
1128 | while (_excess[tip] >= 0 && int(path.size()) < max_length) { |
---|
1129 | int u; |
---|
1130 | LargeCost min_red_cost, rc, pi_tip = _pi[tip]; |
---|
1131 | int last_out = _first_out[tip+1]; |
---|
1132 | for (int a = _next_out[tip]; a != last_out; ++a) { |
---|
1133 | u = _target[a]; |
---|
1134 | if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) { |
---|
1135 | path.push_back(a); |
---|
1136 | _next_out[tip] = a; |
---|
1137 | tip = u; |
---|
1138 | goto next_step; |
---|
1139 | } |
---|
1140 | } |
---|
1141 | |
---|
1142 | // Relabel tip node |
---|
1143 | min_red_cost = std::numeric_limits<LargeCost>::max(); |
---|
1144 | if (tip != start) { |
---|
1145 | int ra = _reverse[path.back()]; |
---|
1146 | min_red_cost = _cost[ra] + pi_tip - _pi[_target[ra]]; |
---|
1147 | } |
---|
1148 | for (int a = _first_out[tip]; a != last_out; ++a) { |
---|
1149 | rc = _cost[a] + pi_tip - _pi[_target[a]]; |
---|
1150 | if (_res_cap[a] > 0 && rc < min_red_cost) { |
---|
1151 | min_red_cost = rc; |
---|
1152 | } |
---|
1153 | } |
---|
1154 | _pi[tip] -= min_red_cost + _epsilon; |
---|
1155 | _next_out[tip] = _first_out[tip]; |
---|
1156 | ++relabel_cnt; |
---|
1157 | |
---|
1158 | // Step back |
---|
1159 | if (tip != start) { |
---|
1160 | tip = _source[path.back()]; |
---|
1161 | path.pop_back(); |
---|
1162 | } |
---|
1163 | |
---|
1164 | next_step: ; |
---|
1165 | } |
---|
1166 | |
---|
1167 | // Augment along the found path (as much flow as possible) |
---|
1168 | Value delta; |
---|
1169 | int pa, u, v = start; |
---|
1170 | for (int i = 0; i != int(path.size()); ++i) { |
---|
1171 | pa = path[i]; |
---|
1172 | u = v; |
---|
1173 | v = _target[pa]; |
---|
1174 | delta = std::min(_res_cap[pa], _excess[u]); |
---|
1175 | _res_cap[pa] -= delta; |
---|
1176 | _res_cap[_reverse[pa]] += delta; |
---|
1177 | _excess[u] -= delta; |
---|
1178 | _excess[v] += delta; |
---|
1179 | if (_excess[v] > 0 && _excess[v] <= delta) |
---|
1180 | _active_nodes.push_back(v); |
---|
1181 | } |
---|
1182 | |
---|
1183 | // Global update heuristic |
---|
1184 | if (relabel_cnt >= next_update_limit) { |
---|
1185 | globalUpdate(); |
---|
1186 | next_update_limit += global_update_freq; |
---|
1187 | } |
---|
1188 | } |
---|
1189 | } |
---|
1190 | } |
---|
1191 | |
---|
1192 | /// Execute the algorithm performing push and relabel operations |
---|
1193 | void startPush() { |
---|
1194 | // Paramters for heuristics |
---|
1195 | const int EARLY_TERM_EPSILON_LIMIT = 1000; |
---|
1196 | const double GLOBAL_UPDATE_FACTOR = 2.0; |
---|
1197 | |
---|
1198 | const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
---|
1199 | (_res_node_num + _sup_node_num * _sup_node_num)); |
---|
1200 | int next_update_limit = global_update_freq; |
---|
1201 | |
---|
1202 | int relabel_cnt = 0; |
---|
1203 | |
---|
1204 | // Perform cost scaling phases |
---|
1205 | BoolVector hyper(_res_node_num, false); |
---|
1206 | LargeCostVector hyper_cost(_res_node_num); |
---|
1207 | for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
---|
1208 | 1 : _epsilon / _alpha ) |
---|
1209 | { |
---|
1210 | // Early termination heuristic |
---|
1211 | if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) { |
---|
1212 | if (earlyTermination()) break; |
---|
1213 | } |
---|
1214 | |
---|
1215 | // Initialize current phase |
---|
1216 | initPhase(); |
---|
1217 | |
---|
1218 | // Perform push and relabel operations |
---|
1219 | while (_active_nodes.size() > 0) { |
---|
1220 | LargeCost min_red_cost, rc, pi_n; |
---|
1221 | Value delta; |
---|
1222 | int n, t, a, last_out = _res_arc_num; |
---|
1223 | |
---|
1224 | next_node: |
---|
1225 | // Select an active node (FIFO selection) |
---|
1226 | n = _active_nodes.front(); |
---|
1227 | last_out = _first_out[n+1]; |
---|
1228 | pi_n = _pi[n]; |
---|
1229 | |
---|
1230 | // Perform push operations if there are admissible arcs |
---|
1231 | if (_excess[n] > 0) { |
---|
1232 | for (a = _next_out[n]; a != last_out; ++a) { |
---|
1233 | if (_res_cap[a] > 0 && |
---|
1234 | _cost[a] + pi_n - _pi[_target[a]] < 0) { |
---|
1235 | delta = std::min(_res_cap[a], _excess[n]); |
---|
1236 | t = _target[a]; |
---|
1237 | |
---|
1238 | // Push-look-ahead heuristic |
---|
1239 | Value ahead = -_excess[t]; |
---|
1240 | int last_out_t = _first_out[t+1]; |
---|
1241 | LargeCost pi_t = _pi[t]; |
---|
1242 | for (int ta = _next_out[t]; ta != last_out_t; ++ta) { |
---|
1243 | if (_res_cap[ta] > 0 && |
---|
1244 | _cost[ta] + pi_t - _pi[_target[ta]] < 0) |
---|
1245 | ahead += _res_cap[ta]; |
---|
1246 | if (ahead >= delta) break; |
---|
1247 | } |
---|
1248 | if (ahead < 0) ahead = 0; |
---|
1249 | |
---|
1250 | // Push flow along the arc |
---|
1251 | if (ahead < delta && !hyper[t]) { |
---|
1252 | _res_cap[a] -= ahead; |
---|
1253 | _res_cap[_reverse[a]] += ahead; |
---|
1254 | _excess[n] -= ahead; |
---|
1255 | _excess[t] += ahead; |
---|
1256 | _active_nodes.push_front(t); |
---|
1257 | hyper[t] = true; |
---|
1258 | hyper_cost[t] = _cost[a] + pi_n - pi_t; |
---|
1259 | _next_out[n] = a; |
---|
1260 | goto next_node; |
---|
1261 | } else { |
---|
1262 | _res_cap[a] -= delta; |
---|
1263 | _res_cap[_reverse[a]] += delta; |
---|
1264 | _excess[n] -= delta; |
---|
1265 | _excess[t] += delta; |
---|
1266 | if (_excess[t] > 0 && _excess[t] <= delta) |
---|
1267 | _active_nodes.push_back(t); |
---|
1268 | } |
---|
1269 | |
---|
1270 | if (_excess[n] == 0) { |
---|
1271 | _next_out[n] = a; |
---|
1272 | goto remove_nodes; |
---|
1273 | } |
---|
1274 | } |
---|
1275 | } |
---|
1276 | _next_out[n] = a; |
---|
1277 | } |
---|
1278 | |
---|
1279 | // Relabel the node if it is still active (or hyper) |
---|
1280 | if (_excess[n] > 0 || hyper[n]) { |
---|
1281 | min_red_cost = hyper[n] ? -hyper_cost[n] : |
---|
1282 | std::numeric_limits<LargeCost>::max(); |
---|
1283 | for (int a = _first_out[n]; a != last_out; ++a) { |
---|
1284 | rc = _cost[a] + pi_n - _pi[_target[a]]; |
---|
1285 | if (_res_cap[a] > 0 && rc < min_red_cost) { |
---|
1286 | min_red_cost = rc; |
---|
1287 | } |
---|
1288 | } |
---|
1289 | _pi[n] -= min_red_cost + _epsilon; |
---|
1290 | _next_out[n] = _first_out[n]; |
---|
1291 | hyper[n] = false; |
---|
1292 | ++relabel_cnt; |
---|
1293 | } |
---|
1294 | |
---|
1295 | // Remove nodes that are not active nor hyper |
---|
1296 | remove_nodes: |
---|
1297 | while ( _active_nodes.size() > 0 && |
---|
1298 | _excess[_active_nodes.front()] <= 0 && |
---|
1299 | !hyper[_active_nodes.front()] ) { |
---|
1300 | _active_nodes.pop_front(); |
---|
1301 | } |
---|
1302 | |
---|
1303 | // Global update heuristic |
---|
1304 | if (relabel_cnt >= next_update_limit) { |
---|
1305 | globalUpdate(); |
---|
1306 | for (int u = 0; u != _res_node_num; ++u) |
---|
1307 | hyper[u] = false; |
---|
1308 | next_update_limit += global_update_freq; |
---|
1309 | } |
---|
1310 | } |
---|
1311 | } |
---|
1312 | } |
---|
1313 | |
---|
1314 | }; //class CostScaling |
---|
1315 | |
---|
1316 | ///@} |
---|
1317 | |
---|
1318 | } //namespace lemon |
---|
1319 | |
---|
1320 | #endif //LEMON_COST_SCALING_H |
---|