1 /** @file mcf.cpp Definition of Multi-Commodity-Flow solver. */
4 #include "../core/math_func.hpp"
8 #include "../safeguards.h"
10 typedef std::map
<NodeID
, Path
*> PathViaMap
;
13 * Distance-based annotation for use in the Dijkstra algorithm. This is close
14 * to the original meaning of "annotation" in this context. Paths are rated
15 * according to the sum of distances of their edges.
17 class DistanceAnnotation
: public Path
{
22 * @param n ID of node to be annotated.
23 * @param source If the node is the source of its path.
25 DistanceAnnotation(NodeID n
, bool source
= false) : Path(n
, source
) {}
27 bool IsBetter(const DistanceAnnotation
*base
, uint cap
, int free_cap
, uint dist
) const;
30 * Return the actual value of the annotation, in this case the distance.
33 inline uint
GetAnnotation() const { return this->distance
; }
36 * Update the cached annotation value
38 inline void UpdateAnnotation() { }
41 * Comparator for std containers.
44 bool operator()(const DistanceAnnotation
*x
, const DistanceAnnotation
*y
) const;
49 * Capacity-based annotation for use in the Dijkstra algorithm. This annotation
50 * rates paths according to the maximum capacity of their edges. The Dijkstra
51 * algorithm still gives meaningful results like this as the capacity of a path
52 * can only decrease or stay the same if you add more edges.
54 class CapacityAnnotation
: public Path
{
55 int cached_annotation
;
61 * @param n ID of node to be annotated.
62 * @param source If the node is the source of its path.
64 CapacityAnnotation(NodeID n
, bool source
= false) : Path(n
, source
) {}
66 bool IsBetter(const CapacityAnnotation
*base
, uint cap
, int free_cap
, uint dist
) const;
69 * Return the actual value of the annotation, in this case the capacity.
72 inline int GetAnnotation() const { return this->cached_annotation
; }
75 * Update the cached annotation value
77 inline void UpdateAnnotation()
79 this->cached_annotation
= this->GetCapacityRatio();
83 * Comparator for std containers.
86 bool operator()(const CapacityAnnotation
*x
, const CapacityAnnotation
*y
) const;
91 * Iterator class for getting the edges in the order of their next_edge
94 class GraphEdgeIterator
{
96 LinkGraphJob
&job
; ///< Job being executed
97 EdgeIterator i
; ///< Iterator pointing to current edge.
98 EdgeIterator end
; ///< Iterator pointing beyond last edge.
103 * Construct a GraphEdgeIterator.
104 * @param job Job to iterate on.
106 GraphEdgeIterator(LinkGraphJob
&job
) : job(job
),
107 i(NULL
, NULL
, INVALID_NODE
), end(NULL
, NULL
, INVALID_NODE
)
111 * Setup the node to start iterating at.
112 * @param source Unused.
113 * @param node Node to start iterating at.
115 void SetNode(NodeID source
, NodeID node
)
117 this->i
= this->job
[node
].Begin();
118 this->end
= this->job
[node
].End();
122 * Retrieve the ID of the node the next edge points to.
123 * @return Next edge's target node ID or INVALID_NODE.
127 return this->i
!= this->end
? (this->i
++)->first
: INVALID_NODE
;
132 * Iterator class for getting edges from a FlowStatMap.
134 class FlowEdgeIterator
{
136 LinkGraphJob
&job
; ///< Link graph job we're working with.
138 /** Lookup table for getting NodeIDs from StationIDs. */
139 std::vector
<NodeID
> station_to_node
;
141 /** Current iterator in the shares map. */
142 FlowStat::SharesMap::const_iterator it
;
144 /** End of the shares map. */
145 FlowStat::SharesMap::const_iterator end
;
150 * @param job Link graph job to work with.
152 FlowEdgeIterator(LinkGraphJob
&job
) : job(job
)
154 for (NodeID i
= 0; i
< job
.Size(); ++i
) {
155 StationID st
= job
[i
].Station();
156 if (st
>= this->station_to_node
.size()) {
157 this->station_to_node
.resize(st
+ 1);
159 this->station_to_node
[st
] = i
;
164 * Setup the node to retrieve edges from.
165 * @param source Root of the current path tree.
166 * @param node Current node to be checked for outgoing flows.
168 void SetNode(NodeID source
, NodeID node
)
170 const FlowStatMap
&flows
= this->job
[node
].Flows();
171 FlowStatMap::const_iterator it
= flows
.find(this->job
[source
].Station());
172 if (it
!= flows
.end()) {
173 this->it
= it
->second
.GetShares()->begin();
174 this->end
= it
->second
.GetShares()->end();
176 this->it
= FlowStat::empty_sharesmap
.begin();
177 this->end
= FlowStat::empty_sharesmap
.end();
182 * Get the next node for which a flow exists.
183 * @return ID of next node with flow.
187 if (this->it
== this->end
) return INVALID_NODE
;
188 return this->station_to_node
[(this->it
++)->second
];
193 * Determines if an extension to the given Path with the given parameters is
194 * better than this path.
195 * @param base Other path.
196 * @param cap Capacity of the new edge to be added to base.
197 * @param dist Distance of the new edge.
198 * @return True if base + the new edge would be better than the path associated
199 * with this annotation.
201 bool DistanceAnnotation::IsBetter(const DistanceAnnotation
*base
, uint cap
,
202 int free_cap
, uint dist
) const
204 /* If any of the paths is disconnected, the other one is better. If both
205 * are disconnected, this path is better.*/
206 if (base
->distance
== UINT_MAX
) {
208 } else if (this->distance
== UINT_MAX
) {
212 if (free_cap
> 0 && base
->free_capacity
> 0) {
213 /* If both paths have capacity left, compare their distances.
214 * If the other path has capacity left and this one hasn't, the
215 * other one's better (thus, return true). */
216 return this->free_capacity
> 0 ? (base
->distance
+ dist
< this->distance
) : true;
218 /* If the other path doesn't have capacity left, but this one has,
219 * the other one is worse (thus, return false).
220 * If both paths are out of capacity, do the regular distance
222 return this->free_capacity
> 0 ? false : (base
->distance
+ dist
< this->distance
);
227 * Determines if an extension to the given Path with the given parameters is
228 * better than this path.
229 * @param base Other path.
230 * @param cap Capacity of the new edge to be added to base.
231 * @param dist Distance of the new edge.
232 * @return True if base + the new edge would be better than the path associated
233 * with this annotation.
235 bool CapacityAnnotation::IsBetter(const CapacityAnnotation
*base
, uint cap
,
236 int free_cap
, uint dist
) const
238 int min_cap
= Path::GetCapacityRatio(min(base
->free_capacity
, free_cap
), min(base
->capacity
, cap
));
239 int this_cap
= this->GetCapacityRatio();
240 if (min_cap
== this_cap
) {
241 /* If the capacities are the same and the other path isn't disconnected
242 * choose the shorter path. */
243 return base
->distance
== UINT_MAX
? false : (base
->distance
+ dist
< this->distance
);
245 return min_cap
> this_cap
;
250 * A slightly modified Dijkstra algorithm. Grades the paths not necessarily by
251 * distance, but by the value Tannotation computes. It uses the max_saturation
252 * setting to artificially decrease capacities.
253 * @tparam Tannotation Annotation to be used.
254 * @tparam Tedge_iterator Iterator to be used for getting outgoing edges.
255 * @param source_node Node where the algorithm starts.
256 * @param paths Container for the paths to be calculated.
258 template<class Tannotation
, class Tedge_iterator
>
259 void MultiCommodityFlow::Dijkstra(NodeID source_node
, PathVector
&paths
)
261 typedef std::set
<Tannotation
*, typename
Tannotation::Comparator
> AnnoSet
;
262 Tedge_iterator
iter(this->job
);
263 uint size
= this->job
.Size();
265 paths
.resize(size
, NULL
);
266 for (NodeID node
= 0; node
< size
; ++node
) {
267 Tannotation
*anno
= new Tannotation(node
, node
== source_node
);
268 anno
->UpdateAnnotation();
272 while (!annos
.empty()) {
273 typename
AnnoSet::iterator i
= annos
.begin();
274 Tannotation
*source
= *i
;
276 NodeID from
= source
->GetNode();
277 iter
.SetNode(source_node
, from
);
278 for (NodeID to
= iter
.Next(); to
!= INVALID_NODE
; to
= iter
.Next()) {
279 if (to
== from
) continue; // Not a real edge but a consumption sign.
280 Edge edge
= this->job
[from
][to
];
281 uint capacity
= edge
.Capacity();
282 if (this->max_saturation
!= UINT_MAX
) {
283 capacity
*= this->max_saturation
;
285 if (capacity
== 0) capacity
= 1;
287 /* punish in-between stops a little */
288 uint distance
= DistanceMaxPlusManhattan(this->job
[from
].XY(), this->job
[to
].XY()) + 1;
289 Tannotation
*dest
= static_cast<Tannotation
*>(paths
[to
]);
290 if (dest
->IsBetter(source
, capacity
, capacity
- edge
.Flow(), distance
)) {
292 dest
->Fork(source
, capacity
, capacity
- edge
.Flow(), distance
);
293 dest
->UpdateAnnotation();
301 * Clean up paths that lead nowhere and the root path.
302 * @param source_id ID of the root node.
303 * @param paths Paths to be cleaned up.
305 void MultiCommodityFlow::CleanupPaths(NodeID source_id
, PathVector
&paths
)
307 Path
*source
= paths
[source_id
];
308 paths
[source_id
] = NULL
;
309 for (PathVector::iterator i
= paths
.begin(); i
!= paths
.end(); ++i
) {
311 if (path
== NULL
) continue;
312 if (path
->GetParent() == source
) path
->Detach();
313 while (path
!= source
&& path
!= NULL
&& path
->GetFlow() == 0) {
314 Path
*parent
= path
->GetParent();
316 if (path
->GetNumChildren() == 0) {
317 paths
[path
->GetNode()] = NULL
;
328 * Push flow along a path and update the unsatisfied_demand of the associated
330 * @param edge Edge whose ends the path connects.
331 * @param path End of the path the flow should be pushed on.
332 * @param accuracy Accuracy of the calculation.
333 * @param max_saturation If < UINT_MAX only push flow up to the given
334 * saturation, otherwise the path can be "overloaded".
336 uint
MultiCommodityFlow::PushFlow(Edge
&edge
, Path
*path
, uint accuracy
,
339 assert(edge
.UnsatisfiedDemand() > 0);
340 uint flow
= Clamp(edge
.Demand() / accuracy
, 1, edge
.UnsatisfiedDemand());
341 flow
= path
->AddFlow(flow
, this->job
, max_saturation
);
342 edge
.SatisfyDemand(flow
);
347 * Find the flow along a cycle including cycle_begin in path.
348 * @param path Set of paths that form the cycle.
349 * @param cycle_begin Path to start at.
350 * @return Flow along the cycle.
352 uint
MCF1stPass::FindCycleFlow(const PathVector
&path
, const Path
*cycle_begin
)
354 uint flow
= UINT_MAX
;
355 const Path
*cycle_end
= cycle_begin
;
357 flow
= min(flow
, cycle_begin
->GetFlow());
358 cycle_begin
= path
[cycle_begin
->GetNode()];
359 } while (cycle_begin
!= cycle_end
);
364 * Eliminate a cycle of the given flow in the given set of paths.
365 * @param path Set of paths containing the cycle.
366 * @param cycle_begin Part of the cycle to start at.
367 * @param flow Flow along the cycle.
369 void MCF1stPass::EliminateCycle(PathVector
&path
, Path
*cycle_begin
, uint flow
)
371 Path
*cycle_end
= cycle_begin
;
373 NodeID prev
= cycle_begin
->GetNode();
374 cycle_begin
->ReduceFlow(flow
);
375 if (cycle_begin
->GetFlow() == 0) {
376 PathList
&node_paths
= this->job
[cycle_begin
->GetParent()->GetNode()].Paths();
377 for (PathList::iterator i
= node_paths
.begin(); i
!= node_paths
.end(); ++i
) {
378 if (*i
== cycle_begin
) {
380 node_paths
.push_back(cycle_begin
);
385 cycle_begin
= path
[prev
];
386 Edge edge
= this->job
[prev
][cycle_begin
->GetNode()];
387 edge
.RemoveFlow(flow
);
388 } while (cycle_begin
!= cycle_end
);
392 * Eliminate cycles for origin_id in the graph. Start searching at next_id and
393 * work recursively. Also "summarize" paths: Add up the flows along parallel
395 * @param path Paths checked in parent calls to this method.
396 * @param origin_id Origin of the paths to be checked.
397 * @param next_id Next node to be checked.
398 * @return If any cycles have been found and eliminated.
400 bool MCF1stPass::EliminateCycles(PathVector
&path
, NodeID origin_id
, NodeID next_id
)
402 Path
*at_next_pos
= path
[next_id
];
404 /* this node has already been searched */
405 if (at_next_pos
== Path::invalid_path
) return false;
407 if (at_next_pos
== NULL
) {
408 /* Summarize paths; add up the paths with the same source and next hop
409 * in one path each. */
410 PathList
&paths
= this->job
[next_id
].Paths();
411 PathViaMap next_hops
;
412 for (PathList::iterator i
= paths
.begin(); i
!= paths
.end();) {
413 Path
*new_child
= *i
;
414 uint new_flow
= new_child
->GetFlow();
415 if (new_flow
== 0) break;
416 if (new_child
->GetOrigin() == origin_id
) {
417 PathViaMap::iterator via_it
= next_hops
.find(new_child
->GetNode());
418 if (via_it
== next_hops
.end()) {
419 next_hops
[new_child
->GetNode()] = new_child
;
422 Path
*child
= via_it
->second
;
423 child
->AddFlow(new_flow
);
424 new_child
->ReduceFlow(new_flow
);
426 /* We might hit end() with with the ++ here and skip the
427 * newly push_back'ed path. That's good as the flow of that
428 * path is 0 anyway. */
430 paths
.push_back(new_child
);
437 /* Search the next hops for nodes we have already visited */
438 for (PathViaMap::iterator via_it
= next_hops
.begin();
439 via_it
!= next_hops
.end(); ++via_it
) {
440 Path
*child
= via_it
->second
;
441 if (child
->GetFlow() > 0) {
442 /* Push one child into the path vector and search this child's
444 path
[next_id
] = child
;
445 found
= this->EliminateCycles(path
, origin_id
, child
->GetNode()) || found
;
448 /* All paths departing from this node have been searched. Mark as
449 * resolved if no cycles found. If cycles were found further cycles
450 * could be found in this branch, thus it has to be searched again next
453 path
[next_id
] = found
? NULL
: Path::invalid_path
;
457 /* This node has already been visited => we have a cycle.
458 * Backtrack to find the exact flow. */
459 uint flow
= this->FindCycleFlow(path
, at_next_pos
);
461 this->EliminateCycle(path
, at_next_pos
, flow
);
469 * Eliminate all cycles in the graph. Check paths starting at each node for
471 * @return If any cycles have been found and eliminated.
473 bool MCF1stPass::EliminateCycles()
475 bool cycles_found
= false;
476 uint size
= this->job
.Size();
477 PathVector
path(size
, NULL
);
478 for (NodeID node
= 0; node
< size
; ++node
) {
479 /* Starting at each node in the graph find all cycles involving this
481 std::fill(path
.begin(), path
.end(), (Path
*)NULL
);
482 cycles_found
|= this->EliminateCycles(path
, node
, node
);
488 * Run the first pass of the MCF calculation.
489 * @param job Link graph job to calculate.
491 MCF1stPass::MCF1stPass(LinkGraphJob
&job
) : MultiCommodityFlow(job
)
494 uint size
= job
.Size();
495 uint accuracy
= job
.Settings().accuracy
;
500 for (NodeID source
= 0; source
< size
; ++source
) {
501 /* First saturate the shortest paths. */
502 this->Dijkstra
<DistanceAnnotation
, GraphEdgeIterator
>(source
, paths
);
504 for (NodeID dest
= 0; dest
< size
; ++dest
) {
505 Edge edge
= job
[source
][dest
];
506 if (edge
.UnsatisfiedDemand() > 0) {
507 Path
*path
= paths
[dest
];
508 assert(path
!= NULL
);
509 /* Generally only allow paths that don't exceed the
510 * available capacity. But if no demand has been assigned
511 * yet, make an exception and allow any valid path *once*. */
512 if (path
->GetFreeCapacity() > 0 && this->PushFlow(edge
, path
,
513 accuracy
, this->max_saturation
) > 0) {
514 /* If a path has been found there is a chance we can
516 more_loops
= more_loops
|| (edge
.UnsatisfiedDemand() > 0);
517 } else if (edge
.UnsatisfiedDemand() == edge
.Demand() &&
518 path
->GetFreeCapacity() > INT_MIN
) {
519 this->PushFlow(edge
, path
, accuracy
, UINT_MAX
);
523 this->CleanupPaths(source
, paths
);
525 } while (more_loops
|| this->EliminateCycles());
529 * Run the second pass of the MCF calculation which assigns all remaining
530 * demands to existing paths.
531 * @param job Link graph job to calculate.
533 MCF2ndPass::MCF2ndPass(LinkGraphJob
&job
) : MultiCommodityFlow(job
)
535 this->max_saturation
= UINT_MAX
; // disable artificial cap on saturation
537 uint size
= job
.Size();
538 uint accuracy
= job
.Settings().accuracy
;
539 bool demand_left
= true;
540 while (demand_left
) {
542 for (NodeID source
= 0; source
< size
; ++source
) {
543 this->Dijkstra
<CapacityAnnotation
, FlowEdgeIterator
>(source
, paths
);
544 for (NodeID dest
= 0; dest
< size
; ++dest
) {
545 Edge edge
= this->job
[source
][dest
];
546 Path
*path
= paths
[dest
];
547 if (edge
.UnsatisfiedDemand() > 0 && path
->GetFreeCapacity() > INT_MIN
) {
548 this->PushFlow(edge
, path
, accuracy
, UINT_MAX
);
549 if (edge
.UnsatisfiedDemand() > 0) demand_left
= true;
552 this->CleanupPaths(source
, paths
);
558 * Relation that creates a weak order without duplicates.
559 * Avoid accidentally deleting different paths of the same capacity/distance in
560 * a set. When the annotation is the same node IDs are compared, so there are
562 * @tparam T Type to be compared on.
563 * @param x_anno First value.
564 * @param y_anno Second value.
565 * @param x Node id associated with the first value.
566 * @param y Node id associated with the second value.
568 template <typename T
>
569 bool Greater(T x_anno
, T y_anno
, NodeID x
, NodeID y
)
571 if (x_anno
> y_anno
) return true;
572 if (x_anno
< y_anno
) return false;
577 * Compare two capacity annotations.
578 * @param x First capacity annotation.
579 * @param y Second capacity annotation.
580 * @return If x is better than y.
582 bool CapacityAnnotation::Comparator::operator()(const CapacityAnnotation
*x
,
583 const CapacityAnnotation
*y
) const
585 return x
!= y
&& Greater
<int>(x
->GetAnnotation(), y
->GetAnnotation(),
586 x
->GetNode(), y
->GetNode());
590 * Compare two distance annotations.
591 * @param x First distance annotation.
592 * @param y Second distance annotation.
593 * @return If x is better than y.
595 bool DistanceAnnotation::Comparator::operator()(const DistanceAnnotation
*x
,
596 const DistanceAnnotation
*y
) const
598 return x
!= y
&& !Greater
<uint
>(x
->GetAnnotation(), y
->GetAnnotation(),
599 x
->GetNode(), y
->GetNode());