kpeter@2577
|
1 |
/* -*- C++ -*-
|
kpeter@2577
|
2 |
*
|
kpeter@2577
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
kpeter@2577
|
4 |
*
|
kpeter@2577
|
5 |
* Copyright (C) 2003-2008
|
kpeter@2577
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@2577
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@2577
|
8 |
*
|
kpeter@2577
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@2577
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@2577
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@2577
|
12 |
*
|
kpeter@2577
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@2577
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@2577
|
15 |
* purpose.
|
kpeter@2577
|
16 |
*
|
kpeter@2577
|
17 |
*/
|
kpeter@2577
|
18 |
|
kpeter@2577
|
19 |
#ifndef LEMON_COST_SCALING_H
|
kpeter@2577
|
20 |
#define LEMON_COST_SCALING_H
|
kpeter@2577
|
21 |
|
kpeter@2577
|
22 |
/// \ingroup min_cost_flow
|
kpeter@2577
|
23 |
/// \file
|
kpeter@2577
|
24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow.
|
kpeter@2577
|
25 |
|
kpeter@2577
|
26 |
#include <deque>
|
kpeter@2577
|
27 |
#include <lemon/graph_adaptor.h>
|
kpeter@2577
|
28 |
#include <lemon/graph_utils.h>
|
kpeter@2577
|
29 |
#include <lemon/maps.h>
|
kpeter@2577
|
30 |
#include <lemon/math.h>
|
kpeter@2577
|
31 |
|
kpeter@2577
|
32 |
#include <lemon/circulation.h>
|
kpeter@2577
|
33 |
#include <lemon/bellman_ford.h>
|
kpeter@2577
|
34 |
|
kpeter@2577
|
35 |
namespace lemon {
|
kpeter@2577
|
36 |
|
kpeter@2577
|
37 |
/// \addtogroup min_cost_flow
|
kpeter@2577
|
38 |
/// @{
|
kpeter@2577
|
39 |
|
kpeter@2577
|
40 |
/// \brief Implementation of the cost scaling algorithm for finding a
|
kpeter@2577
|
41 |
/// minimum cost flow.
|
kpeter@2577
|
42 |
///
|
kpeter@2577
|
43 |
/// \ref CostScaling implements the cost scaling algorithm performing
|
kpeter@2625
|
44 |
/// augment/push and relabel operations for finding a minimum cost
|
kpeter@2577
|
45 |
/// flow.
|
kpeter@2577
|
46 |
///
|
kpeter@2577
|
47 |
/// \tparam Graph The directed graph type the algorithm runs on.
|
kpeter@2577
|
48 |
/// \tparam LowerMap The type of the lower bound map.
|
kpeter@2577
|
49 |
/// \tparam CapacityMap The type of the capacity (upper bound) map.
|
kpeter@2577
|
50 |
/// \tparam CostMap The type of the cost (length) map.
|
kpeter@2577
|
51 |
/// \tparam SupplyMap The type of the supply map.
|
kpeter@2577
|
52 |
///
|
kpeter@2577
|
53 |
/// \warning
|
kpeter@2577
|
54 |
/// - Edge capacities and costs should be \e non-negative \e integers.
|
kpeter@2577
|
55 |
/// - Supply values should be \e signed \e integers.
|
kpeter@2581
|
56 |
/// - The value types of the maps should be convertible to each other.
|
kpeter@2581
|
57 |
/// - \c CostMap::Value must be signed type.
|
kpeter@2577
|
58 |
///
|
kpeter@2577
|
59 |
/// \note Edge costs are multiplied with the number of nodes during
|
kpeter@2577
|
60 |
/// the algorithm so overflow problems may arise more easily than with
|
kpeter@2577
|
61 |
/// other minimum cost flow algorithms.
|
kpeter@2577
|
62 |
/// If it is available, <tt>long long int</tt> type is used instead of
|
kpeter@2577
|
63 |
/// <tt>long int</tt> in the inside computations.
|
kpeter@2577
|
64 |
///
|
kpeter@2577
|
65 |
/// \author Peter Kovacs
|
kpeter@2577
|
66 |
template < typename Graph,
|
kpeter@2577
|
67 |
typename LowerMap = typename Graph::template EdgeMap<int>,
|
kpeter@2577
|
68 |
typename CapacityMap = typename Graph::template EdgeMap<int>,
|
kpeter@2577
|
69 |
typename CostMap = typename Graph::template EdgeMap<int>,
|
kpeter@2577
|
70 |
typename SupplyMap = typename Graph::template NodeMap<int> >
|
kpeter@2577
|
71 |
class CostScaling
|
kpeter@2577
|
72 |
{
|
kpeter@2577
|
73 |
GRAPH_TYPEDEFS(typename Graph);
|
kpeter@2577
|
74 |
|
kpeter@2577
|
75 |
typedef typename CapacityMap::Value Capacity;
|
kpeter@2577
|
76 |
typedef typename CostMap::Value Cost;
|
kpeter@2577
|
77 |
typedef typename SupplyMap::Value Supply;
|
kpeter@2577
|
78 |
typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap;
|
kpeter@2577
|
79 |
typedef typename Graph::template NodeMap<Supply> SupplyNodeMap;
|
kpeter@2577
|
80 |
|
kpeter@2577
|
81 |
typedef ResGraphAdaptor< const Graph, Capacity,
|
kpeter@2577
|
82 |
CapacityEdgeMap, CapacityEdgeMap > ResGraph;
|
kpeter@2577
|
83 |
typedef typename ResGraph::Edge ResEdge;
|
kpeter@2577
|
84 |
|
kpeter@2577
|
85 |
#if defined __GNUC__ && !defined __STRICT_ANSI__
|
kpeter@2577
|
86 |
typedef long long int LCost;
|
kpeter@2577
|
87 |
#else
|
kpeter@2577
|
88 |
typedef long int LCost;
|
kpeter@2577
|
89 |
#endif
|
kpeter@2577
|
90 |
typedef typename Graph::template EdgeMap<LCost> LargeCostMap;
|
kpeter@2577
|
91 |
|
kpeter@2577
|
92 |
public:
|
kpeter@2577
|
93 |
|
kpeter@2577
|
94 |
/// The type of the flow map.
|
kpeter@2581
|
95 |
typedef typename Graph::template EdgeMap<Capacity> FlowMap;
|
kpeter@2577
|
96 |
/// The type of the potential map.
|
kpeter@2577
|
97 |
typedef typename Graph::template NodeMap<LCost> PotentialMap;
|
kpeter@2577
|
98 |
|
kpeter@2577
|
99 |
private:
|
kpeter@2577
|
100 |
|
kpeter@2577
|
101 |
/// \brief Map adaptor class for handling residual edge costs.
|
kpeter@2577
|
102 |
///
|
kpeter@2620
|
103 |
/// Map adaptor class for handling residual edge costs.
|
kpeter@2581
|
104 |
template <typename Map>
|
kpeter@2581
|
105 |
class ResidualCostMap : public MapBase<ResEdge, typename Map::Value>
|
kpeter@2577
|
106 |
{
|
kpeter@2577
|
107 |
private:
|
kpeter@2577
|
108 |
|
kpeter@2581
|
109 |
const Map &_cost_map;
|
kpeter@2577
|
110 |
|
kpeter@2577
|
111 |
public:
|
kpeter@2577
|
112 |
|
kpeter@2577
|
113 |
///\e
|
kpeter@2581
|
114 |
ResidualCostMap(const Map &cost_map) :
|
kpeter@2577
|
115 |
_cost_map(cost_map) {}
|
kpeter@2577
|
116 |
|
kpeter@2577
|
117 |
///\e
|
kpeter@2625
|
118 |
inline typename Map::Value operator[](const ResEdge &e) const {
|
kpeter@2625
|
119 |
return ResGraph::forward(e) ? _cost_map[e] : -_cost_map[e];
|
kpeter@2577
|
120 |
}
|
kpeter@2577
|
121 |
|
kpeter@2577
|
122 |
}; //class ResidualCostMap
|
kpeter@2577
|
123 |
|
kpeter@2577
|
124 |
/// \brief Map adaptor class for handling reduced edge costs.
|
kpeter@2577
|
125 |
///
|
kpeter@2620
|
126 |
/// Map adaptor class for handling reduced edge costs.
|
kpeter@2577
|
127 |
class ReducedCostMap : public MapBase<Edge, LCost>
|
kpeter@2577
|
128 |
{
|
kpeter@2577
|
129 |
private:
|
kpeter@2577
|
130 |
|
kpeter@2577
|
131 |
const Graph &_gr;
|
kpeter@2577
|
132 |
const LargeCostMap &_cost_map;
|
kpeter@2577
|
133 |
const PotentialMap &_pot_map;
|
kpeter@2577
|
134 |
|
kpeter@2577
|
135 |
public:
|
kpeter@2577
|
136 |
|
kpeter@2577
|
137 |
///\e
|
kpeter@2577
|
138 |
ReducedCostMap( const Graph &gr,
|
kpeter@2577
|
139 |
const LargeCostMap &cost_map,
|
kpeter@2577
|
140 |
const PotentialMap &pot_map ) :
|
kpeter@2577
|
141 |
_gr(gr), _cost_map(cost_map), _pot_map(pot_map) {}
|
kpeter@2577
|
142 |
|
kpeter@2577
|
143 |
///\e
|
kpeter@2625
|
144 |
inline LCost operator[](const Edge &e) const {
|
kpeter@2577
|
145 |
return _cost_map[e] + _pot_map[_gr.source(e)]
|
kpeter@2577
|
146 |
- _pot_map[_gr.target(e)];
|
kpeter@2577
|
147 |
}
|
kpeter@2577
|
148 |
|
kpeter@2577
|
149 |
}; //class ReducedCostMap
|
kpeter@2577
|
150 |
|
kpeter@2577
|
151 |
private:
|
kpeter@2577
|
152 |
|
kpeter@2577
|
153 |
// The directed graph the algorithm runs on
|
kpeter@2577
|
154 |
const Graph &_graph;
|
kpeter@2577
|
155 |
// The original lower bound map
|
kpeter@2577
|
156 |
const LowerMap *_lower;
|
kpeter@2577
|
157 |
// The modified capacity map
|
kpeter@2577
|
158 |
CapacityEdgeMap _capacity;
|
kpeter@2577
|
159 |
// The original cost map
|
kpeter@2577
|
160 |
const CostMap &_orig_cost;
|
kpeter@2577
|
161 |
// The scaled cost map
|
kpeter@2577
|
162 |
LargeCostMap _cost;
|
kpeter@2577
|
163 |
// The modified supply map
|
kpeter@2577
|
164 |
SupplyNodeMap _supply;
|
kpeter@2577
|
165 |
bool _valid_supply;
|
kpeter@2577
|
166 |
|
kpeter@2577
|
167 |
// Edge map of the current flow
|
kpeter@2581
|
168 |
FlowMap *_flow;
|
kpeter@2581
|
169 |
bool _local_flow;
|
kpeter@2577
|
170 |
// Node map of the current potentials
|
kpeter@2581
|
171 |
PotentialMap *_potential;
|
kpeter@2581
|
172 |
bool _local_potential;
|
kpeter@2577
|
173 |
|
kpeter@2623
|
174 |
// The residual cost map
|
kpeter@2623
|
175 |
ResidualCostMap<LargeCostMap> _res_cost;
|
kpeter@2577
|
176 |
// The residual graph
|
kpeter@2581
|
177 |
ResGraph *_res_graph;
|
kpeter@2577
|
178 |
// The reduced cost map
|
kpeter@2581
|
179 |
ReducedCostMap *_red_cost;
|
kpeter@2577
|
180 |
// The excess map
|
kpeter@2577
|
181 |
SupplyNodeMap _excess;
|
kpeter@2577
|
182 |
// The epsilon parameter used for cost scaling
|
kpeter@2577
|
183 |
LCost _epsilon;
|
kpeter@2625
|
184 |
// The scaling factor
|
kpeter@2625
|
185 |
int _alpha;
|
kpeter@2577
|
186 |
|
kpeter@2577
|
187 |
public:
|
kpeter@2577
|
188 |
|
kpeter@2581
|
189 |
/// \brief General constructor (with lower bounds).
|
kpeter@2577
|
190 |
///
|
kpeter@2581
|
191 |
/// General constructor (with lower bounds).
|
kpeter@2577
|
192 |
///
|
kpeter@2577
|
193 |
/// \param graph The directed graph the algorithm runs on.
|
kpeter@2577
|
194 |
/// \param lower The lower bounds of the edges.
|
kpeter@2577
|
195 |
/// \param capacity The capacities (upper bounds) of the edges.
|
kpeter@2577
|
196 |
/// \param cost The cost (length) values of the edges.
|
kpeter@2577
|
197 |
/// \param supply The supply values of the nodes (signed).
|
kpeter@2577
|
198 |
CostScaling( const Graph &graph,
|
kpeter@2577
|
199 |
const LowerMap &lower,
|
kpeter@2577
|
200 |
const CapacityMap &capacity,
|
kpeter@2577
|
201 |
const CostMap &cost,
|
kpeter@2577
|
202 |
const SupplyMap &supply ) :
|
kpeter@2629
|
203 |
_graph(graph), _lower(&lower), _capacity(capacity), _orig_cost(cost),
|
kpeter@2629
|
204 |
_cost(graph), _supply(supply), _flow(NULL), _local_flow(false),
|
kpeter@2623
|
205 |
_potential(NULL), _local_potential(false), _res_cost(_cost),
|
kpeter@2623
|
206 |
_res_graph(NULL), _red_cost(NULL), _excess(graph, 0)
|
kpeter@2577
|
207 |
{
|
kpeter@2629
|
208 |
// Check the sum of supply values
|
kpeter@2629
|
209 |
Supply sum = 0;
|
kpeter@2629
|
210 |
for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
|
kpeter@2629
|
211 |
_valid_supply = sum == 0;
|
kpeter@2629
|
212 |
|
kpeter@2625
|
213 |
// Remove non-zero lower bounds
|
kpeter@2629
|
214 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
kpeter@2629
|
215 |
if (lower[e] != 0) {
|
kpeter@2629
|
216 |
_capacity[e] -= lower[e];
|
kpeter@2629
|
217 |
_supply[_graph.source(e)] -= lower[e];
|
kpeter@2629
|
218 |
_supply[_graph.target(e)] += lower[e];
|
kpeter@2629
|
219 |
}
|
kpeter@2577
|
220 |
}
|
kpeter@2577
|
221 |
}
|
kpeter@2577
|
222 |
|
kpeter@2581
|
223 |
/// \brief General constructor (without lower bounds).
|
kpeter@2577
|
224 |
///
|
kpeter@2581
|
225 |
/// General constructor (without lower bounds).
|
kpeter@2577
|
226 |
///
|
kpeter@2577
|
227 |
/// \param graph The directed graph the algorithm runs on.
|
kpeter@2577
|
228 |
/// \param capacity The capacities (upper bounds) of the edges.
|
kpeter@2577
|
229 |
/// \param cost The cost (length) values of the edges.
|
kpeter@2577
|
230 |
/// \param supply The supply values of the nodes (signed).
|
kpeter@2577
|
231 |
CostScaling( const Graph &graph,
|
kpeter@2577
|
232 |
const CapacityMap &capacity,
|
kpeter@2577
|
233 |
const CostMap &cost,
|
kpeter@2577
|
234 |
const SupplyMap &supply ) :
|
kpeter@2577
|
235 |
_graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
|
kpeter@2623
|
236 |
_cost(graph), _supply(supply), _flow(NULL), _local_flow(false),
|
kpeter@2623
|
237 |
_potential(NULL), _local_potential(false), _res_cost(_cost),
|
kpeter@2623
|
238 |
_res_graph(NULL), _red_cost(NULL), _excess(graph, 0)
|
kpeter@2577
|
239 |
{
|
kpeter@2625
|
240 |
// Check the sum of supply values
|
kpeter@2577
|
241 |
Supply sum = 0;
|
kpeter@2577
|
242 |
for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
|
kpeter@2577
|
243 |
_valid_supply = sum == 0;
|
kpeter@2577
|
244 |
}
|
kpeter@2577
|
245 |
|
kpeter@2581
|
246 |
/// \brief Simple constructor (with lower bounds).
|
kpeter@2577
|
247 |
///
|
kpeter@2581
|
248 |
/// Simple constructor (with lower bounds).
|
kpeter@2577
|
249 |
///
|
kpeter@2577
|
250 |
/// \param graph The directed graph the algorithm runs on.
|
kpeter@2577
|
251 |
/// \param lower The lower bounds of the edges.
|
kpeter@2577
|
252 |
/// \param capacity The capacities (upper bounds) of the edges.
|
kpeter@2577
|
253 |
/// \param cost The cost (length) values of the edges.
|
kpeter@2577
|
254 |
/// \param s The source node.
|
kpeter@2577
|
255 |
/// \param t The target node.
|
kpeter@2577
|
256 |
/// \param flow_value The required amount of flow from node \c s
|
kpeter@2577
|
257 |
/// to node \c t (i.e. the supply of \c s and the demand of \c t).
|
kpeter@2577
|
258 |
CostScaling( const Graph &graph,
|
kpeter@2577
|
259 |
const LowerMap &lower,
|
kpeter@2577
|
260 |
const CapacityMap &capacity,
|
kpeter@2577
|
261 |
const CostMap &cost,
|
kpeter@2577
|
262 |
Node s, Node t,
|
kpeter@2577
|
263 |
Supply flow_value ) :
|
kpeter@2629
|
264 |
_graph(graph), _lower(&lower), _capacity(capacity), _orig_cost(cost),
|
kpeter@2629
|
265 |
_cost(graph), _supply(graph, 0), _flow(NULL), _local_flow(false),
|
kpeter@2623
|
266 |
_potential(NULL), _local_potential(false), _res_cost(_cost),
|
kpeter@2623
|
267 |
_res_graph(NULL), _red_cost(NULL), _excess(graph, 0)
|
kpeter@2577
|
268 |
{
|
kpeter@2629
|
269 |
// Remove non-zero lower bounds
|
kpeter@2629
|
270 |
_supply[s] = flow_value;
|
kpeter@2629
|
271 |
_supply[t] = -flow_value;
|
kpeter@2629
|
272 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
kpeter@2629
|
273 |
if (lower[e] != 0) {
|
kpeter@2629
|
274 |
_capacity[e] -= lower[e];
|
kpeter@2629
|
275 |
_supply[_graph.source(e)] -= lower[e];
|
kpeter@2629
|
276 |
_supply[_graph.target(e)] += lower[e];
|
kpeter@2629
|
277 |
}
|
kpeter@2577
|
278 |
}
|
kpeter@2577
|
279 |
_valid_supply = true;
|
kpeter@2577
|
280 |
}
|
kpeter@2577
|
281 |
|
kpeter@2581
|
282 |
/// \brief Simple constructor (without lower bounds).
|
kpeter@2577
|
283 |
///
|
kpeter@2581
|
284 |
/// Simple constructor (without lower bounds).
|
kpeter@2577
|
285 |
///
|
kpeter@2577
|
286 |
/// \param graph The directed graph the algorithm runs on.
|
kpeter@2577
|
287 |
/// \param capacity The capacities (upper bounds) of the edges.
|
kpeter@2577
|
288 |
/// \param cost The cost (length) values of the edges.
|
kpeter@2577
|
289 |
/// \param s The source node.
|
kpeter@2577
|
290 |
/// \param t The target node.
|
kpeter@2577
|
291 |
/// \param flow_value The required amount of flow from node \c s
|
kpeter@2577
|
292 |
/// to node \c t (i.e. the supply of \c s and the demand of \c t).
|
kpeter@2577
|
293 |
CostScaling( const Graph &graph,
|
kpeter@2577
|
294 |
const CapacityMap &capacity,
|
kpeter@2577
|
295 |
const CostMap &cost,
|
kpeter@2577
|
296 |
Node s, Node t,
|
kpeter@2577
|
297 |
Supply flow_value ) :
|
kpeter@2577
|
298 |
_graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
|
kpeter@2623
|
299 |
_cost(graph), _supply(graph, 0), _flow(NULL), _local_flow(false),
|
kpeter@2623
|
300 |
_potential(NULL), _local_potential(false), _res_cost(_cost),
|
kpeter@2623
|
301 |
_res_graph(NULL), _red_cost(NULL), _excess(graph, 0)
|
kpeter@2577
|
302 |
{
|
kpeter@2577
|
303 |
_supply[s] = flow_value;
|
kpeter@2577
|
304 |
_supply[t] = -flow_value;
|
kpeter@2577
|
305 |
_valid_supply = true;
|
kpeter@2577
|
306 |
}
|
kpeter@2577
|
307 |
|
kpeter@2581
|
308 |
/// Destructor.
|
kpeter@2581
|
309 |
~CostScaling() {
|
kpeter@2581
|
310 |
if (_local_flow) delete _flow;
|
kpeter@2581
|
311 |
if (_local_potential) delete _potential;
|
kpeter@2581
|
312 |
delete _res_graph;
|
kpeter@2581
|
313 |
delete _red_cost;
|
kpeter@2581
|
314 |
}
|
kpeter@2581
|
315 |
|
kpeter@2620
|
316 |
/// \brief Set the flow map.
|
kpeter@2581
|
317 |
///
|
kpeter@2620
|
318 |
/// Set the flow map.
|
kpeter@2581
|
319 |
///
|
kpeter@2581
|
320 |
/// \return \c (*this)
|
kpeter@2581
|
321 |
CostScaling& flowMap(FlowMap &map) {
|
kpeter@2581
|
322 |
if (_local_flow) {
|
kpeter@2581
|
323 |
delete _flow;
|
kpeter@2581
|
324 |
_local_flow = false;
|
kpeter@2581
|
325 |
}
|
kpeter@2581
|
326 |
_flow = ↦
|
kpeter@2581
|
327 |
return *this;
|
kpeter@2581
|
328 |
}
|
kpeter@2581
|
329 |
|
kpeter@2620
|
330 |
/// \brief Set the potential map.
|
kpeter@2581
|
331 |
///
|
kpeter@2620
|
332 |
/// Set the potential map.
|
kpeter@2581
|
333 |
///
|
kpeter@2581
|
334 |
/// \return \c (*this)
|
kpeter@2581
|
335 |
CostScaling& potentialMap(PotentialMap &map) {
|
kpeter@2581
|
336 |
if (_local_potential) {
|
kpeter@2581
|
337 |
delete _potential;
|
kpeter@2581
|
338 |
_local_potential = false;
|
kpeter@2581
|
339 |
}
|
kpeter@2581
|
340 |
_potential = ↦
|
kpeter@2581
|
341 |
return *this;
|
kpeter@2581
|
342 |
}
|
kpeter@2581
|
343 |
|
kpeter@2581
|
344 |
/// \name Execution control
|
kpeter@2581
|
345 |
|
kpeter@2581
|
346 |
/// @{
|
kpeter@2581
|
347 |
|
kpeter@2620
|
348 |
/// \brief Run the algorithm.
|
kpeter@2577
|
349 |
///
|
kpeter@2620
|
350 |
/// Run the algorithm.
|
kpeter@2577
|
351 |
///
|
kpeter@2625
|
352 |
/// \param partial_augment By default the algorithm performs
|
kpeter@2625
|
353 |
/// partial augment and relabel operations in the cost scaling
|
kpeter@2625
|
354 |
/// phases. Set this parameter to \c false for using local push and
|
kpeter@2625
|
355 |
/// relabel operations instead.
|
kpeter@2625
|
356 |
///
|
kpeter@2577
|
357 |
/// \return \c true if a feasible flow can be found.
|
kpeter@2625
|
358 |
bool run(bool partial_augment = true) {
|
kpeter@2625
|
359 |
if (partial_augment) {
|
kpeter@2625
|
360 |
return init() && startPartialAugment();
|
kpeter@2625
|
361 |
} else {
|
kpeter@2625
|
362 |
return init() && startPushRelabel();
|
kpeter@2625
|
363 |
}
|
kpeter@2577
|
364 |
}
|
kpeter@2577
|
365 |
|
kpeter@2581
|
366 |
/// @}
|
kpeter@2581
|
367 |
|
kpeter@2581
|
368 |
/// \name Query Functions
|
kpeter@2581
|
369 |
/// The result of the algorithm can be obtained using these
|
kpeter@2620
|
370 |
/// functions.\n
|
kpeter@2620
|
371 |
/// \ref lemon::CostScaling::run() "run()" must be called before
|
kpeter@2620
|
372 |
/// using them.
|
kpeter@2581
|
373 |
|
kpeter@2581
|
374 |
/// @{
|
kpeter@2581
|
375 |
|
kpeter@2620
|
376 |
/// \brief Return a const reference to the edge map storing the
|
kpeter@2577
|
377 |
/// found flow.
|
kpeter@2577
|
378 |
///
|
kpeter@2620
|
379 |
/// Return a const reference to the edge map storing the found flow.
|
kpeter@2577
|
380 |
///
|
kpeter@2577
|
381 |
/// \pre \ref run() must be called before using this function.
|
kpeter@2577
|
382 |
const FlowMap& flowMap() const {
|
kpeter@2581
|
383 |
return *_flow;
|
kpeter@2577
|
384 |
}
|
kpeter@2577
|
385 |
|
kpeter@2620
|
386 |
/// \brief Return a const reference to the node map storing the
|
kpeter@2577
|
387 |
/// found potentials (the dual solution).
|
kpeter@2577
|
388 |
///
|
kpeter@2620
|
389 |
/// Return a const reference to the node map storing the found
|
kpeter@2577
|
390 |
/// potentials (the dual solution).
|
kpeter@2577
|
391 |
///
|
kpeter@2577
|
392 |
/// \pre \ref run() must be called before using this function.
|
kpeter@2577
|
393 |
const PotentialMap& potentialMap() const {
|
kpeter@2581
|
394 |
return *_potential;
|
kpeter@2581
|
395 |
}
|
kpeter@2581
|
396 |
|
kpeter@2620
|
397 |
/// \brief Return the flow on the given edge.
|
kpeter@2581
|
398 |
///
|
kpeter@2620
|
399 |
/// Return the flow on the given edge.
|
kpeter@2581
|
400 |
///
|
kpeter@2581
|
401 |
/// \pre \ref run() must be called before using this function.
|
kpeter@2581
|
402 |
Capacity flow(const Edge& edge) const {
|
kpeter@2581
|
403 |
return (*_flow)[edge];
|
kpeter@2581
|
404 |
}
|
kpeter@2581
|
405 |
|
kpeter@2620
|
406 |
/// \brief Return the potential of the given node.
|
kpeter@2581
|
407 |
///
|
kpeter@2620
|
408 |
/// Return the potential of the given node.
|
kpeter@2581
|
409 |
///
|
kpeter@2581
|
410 |
/// \pre \ref run() must be called before using this function.
|
kpeter@2581
|
411 |
Cost potential(const Node& node) const {
|
kpeter@2581
|
412 |
return (*_potential)[node];
|
kpeter@2577
|
413 |
}
|
kpeter@2577
|
414 |
|
kpeter@2620
|
415 |
/// \brief Return the total cost of the found flow.
|
kpeter@2577
|
416 |
///
|
kpeter@2620
|
417 |
/// Return the total cost of the found flow. The complexity of the
|
kpeter@2577
|
418 |
/// function is \f$ O(e) \f$.
|
kpeter@2577
|
419 |
///
|
kpeter@2577
|
420 |
/// \pre \ref run() must be called before using this function.
|
kpeter@2577
|
421 |
Cost totalCost() const {
|
kpeter@2577
|
422 |
Cost c = 0;
|
kpeter@2577
|
423 |
for (EdgeIt e(_graph); e != INVALID; ++e)
|
kpeter@2581
|
424 |
c += (*_flow)[e] * _orig_cost[e];
|
kpeter@2577
|
425 |
return c;
|
kpeter@2577
|
426 |
}
|
kpeter@2577
|
427 |
|
kpeter@2581
|
428 |
/// @}
|
kpeter@2581
|
429 |
|
kpeter@2577
|
430 |
private:
|
kpeter@2577
|
431 |
|
kpeter@2620
|
432 |
/// Initialize the algorithm.
|
kpeter@2577
|
433 |
bool init() {
|
kpeter@2577
|
434 |
if (!_valid_supply) return false;
|
kpeter@2625
|
435 |
// The scaling factor
|
kpeter@2625
|
436 |
_alpha = 8;
|
kpeter@2577
|
437 |
|
kpeter@2625
|
438 |
// Initialize flow and potential maps
|
kpeter@2581
|
439 |
if (!_flow) {
|
kpeter@2581
|
440 |
_flow = new FlowMap(_graph);
|
kpeter@2581
|
441 |
_local_flow = true;
|
kpeter@2581
|
442 |
}
|
kpeter@2581
|
443 |
if (!_potential) {
|
kpeter@2581
|
444 |
_potential = new PotentialMap(_graph);
|
kpeter@2581
|
445 |
_local_potential = true;
|
kpeter@2581
|
446 |
}
|
kpeter@2581
|
447 |
|
kpeter@2581
|
448 |
_red_cost = new ReducedCostMap(_graph, _cost, *_potential);
|
kpeter@2581
|
449 |
_res_graph = new ResGraph(_graph, _capacity, *_flow);
|
kpeter@2581
|
450 |
|
kpeter@2625
|
451 |
// Initialize the scaled cost map and the epsilon parameter
|
kpeter@2577
|
452 |
Cost max_cost = 0;
|
kpeter@2577
|
453 |
int node_num = countNodes(_graph);
|
kpeter@2577
|
454 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
kpeter@2625
|
455 |
_cost[e] = LCost(_orig_cost[e]) * node_num * _alpha;
|
kpeter@2577
|
456 |
if (_orig_cost[e] > max_cost) max_cost = _orig_cost[e];
|
kpeter@2577
|
457 |
}
|
kpeter@2577
|
458 |
_epsilon = max_cost * node_num;
|
kpeter@2577
|
459 |
|
kpeter@2625
|
460 |
// Find a feasible flow using Circulation
|
kpeter@2577
|
461 |
Circulation< Graph, ConstMap<Edge, Capacity>, CapacityEdgeMap,
|
kpeter@2577
|
462 |
SupplyMap >
|
kpeter@2581
|
463 |
circulation( _graph, constMap<Edge>(Capacity(0)), _capacity,
|
kpeter@2577
|
464 |
_supply );
|
kpeter@2581
|
465 |
return circulation.flowMap(*_flow).run();
|
kpeter@2577
|
466 |
}
|
kpeter@2577
|
467 |
|
kpeter@2625
|
468 |
/// Execute the algorithm performing partial augmentation and
|
kpeter@2625
|
469 |
/// relabel operations.
|
kpeter@2625
|
470 |
bool startPartialAugment() {
|
kpeter@2625
|
471 |
// Paramters for heuristics
|
kpeter@2625
|
472 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000;
|
kpeter@2625
|
473 |
const int BF_HEURISTIC_BOUND_FACTOR = 3;
|
kpeter@2625
|
474 |
// Maximum augment path length
|
kpeter@2625
|
475 |
const int MAX_PATH_LENGTH = 4;
|
kpeter@2577
|
476 |
|
kpeter@2625
|
477 |
// Variables
|
kpeter@2625
|
478 |
typename Graph::template NodeMap<Edge> pred_edge(_graph);
|
kpeter@2625
|
479 |
typename Graph::template NodeMap<bool> forward(_graph);
|
kpeter@2625
|
480 |
typename Graph::template NodeMap<OutEdgeIt> next_out(_graph);
|
kpeter@2625
|
481 |
typename Graph::template NodeMap<InEdgeIt> next_in(_graph);
|
kpeter@2625
|
482 |
typename Graph::template NodeMap<bool> next_dir(_graph);
|
kpeter@2577
|
483 |
std::deque<Node> active_nodes;
|
kpeter@2625
|
484 |
std::vector<Node> path_nodes;
|
kpeter@2577
|
485 |
|
kpeter@2577
|
486 |
int node_num = countNodes(_graph);
|
kpeter@2625
|
487 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
|
kpeter@2625
|
488 |
1 : _epsilon / _alpha )
|
kpeter@2577
|
489 |
{
|
kpeter@2625
|
490 |
// "Early Termination" heuristic: use Bellman-Ford algorithm
|
kpeter@2625
|
491 |
// to check if the current flow is optimal
|
kpeter@2577
|
492 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
|
kpeter@2581
|
493 |
typedef ShiftMap< ResidualCostMap<LargeCostMap> > ShiftCostMap;
|
kpeter@2625
|
494 |
ShiftCostMap shift_cost(_res_cost, 1);
|
kpeter@2581
|
495 |
BellmanFord<ResGraph, ShiftCostMap> bf(*_res_graph, shift_cost);
|
kpeter@2577
|
496 |
bf.init(0);
|
kpeter@2577
|
497 |
bool done = false;
|
kpeter@2577
|
498 |
int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(node_num));
|
kpeter@2577
|
499 |
for (int i = 0; i < K && !done; ++i)
|
kpeter@2577
|
500 |
done = bf.processNextWeakRound();
|
kpeter@2625
|
501 |
if (done) break;
|
kpeter@2577
|
502 |
}
|
kpeter@2577
|
503 |
|
kpeter@2625
|
504 |
// Saturate edges not satisfying the optimality condition
|
kpeter@2577
|
505 |
Capacity delta;
|
kpeter@2577
|
506 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
kpeter@2581
|
507 |
if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
|
kpeter@2581
|
508 |
delta = _capacity[e] - (*_flow)[e];
|
kpeter@2577
|
509 |
_excess[_graph.source(e)] -= delta;
|
kpeter@2577
|
510 |
_excess[_graph.target(e)] += delta;
|
kpeter@2581
|
511 |
(*_flow)[e] = _capacity[e];
|
kpeter@2577
|
512 |
}
|
kpeter@2581
|
513 |
if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
|
kpeter@2581
|
514 |
_excess[_graph.target(e)] -= (*_flow)[e];
|
kpeter@2581
|
515 |
_excess[_graph.source(e)] += (*_flow)[e];
|
kpeter@2581
|
516 |
(*_flow)[e] = 0;
|
kpeter@2577
|
517 |
}
|
kpeter@2577
|
518 |
}
|
kpeter@2577
|
519 |
|
kpeter@2625
|
520 |
// Find active nodes (i.e. nodes with positive excess)
|
kpeter@2625
|
521 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@2577
|
522 |
if (_excess[n] > 0) active_nodes.push_back(n);
|
kpeter@2625
|
523 |
}
|
kpeter@2577
|
524 |
|
kpeter@2625
|
525 |
// Initialize the next edge maps
|
kpeter@2625
|
526 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@2625
|
527 |
next_out[n] = OutEdgeIt(_graph, n);
|
kpeter@2625
|
528 |
next_in[n] = InEdgeIt(_graph, n);
|
kpeter@2625
|
529 |
next_dir[n] = true;
|
kpeter@2625
|
530 |
}
|
kpeter@2625
|
531 |
|
kpeter@2625
|
532 |
// Perform partial augment and relabel operations
|
kpeter@2577
|
533 |
while (active_nodes.size() > 0) {
|
kpeter@2625
|
534 |
// Select an active node (FIFO selection)
|
kpeter@2625
|
535 |
if (_excess[active_nodes[0]] <= 0) {
|
kpeter@2625
|
536 |
active_nodes.pop_front();
|
kpeter@2625
|
537 |
continue;
|
kpeter@2625
|
538 |
}
|
kpeter@2625
|
539 |
Node start = active_nodes[0];
|
kpeter@2625
|
540 |
path_nodes.clear();
|
kpeter@2625
|
541 |
path_nodes.push_back(start);
|
kpeter@2625
|
542 |
|
kpeter@2625
|
543 |
// Find an augmenting path from the start node
|
kpeter@2625
|
544 |
Node u, tip = start;
|
kpeter@2625
|
545 |
LCost min_red_cost;
|
kpeter@2625
|
546 |
while ( _excess[tip] >= 0 &&
|
kpeter@2625
|
547 |
int(path_nodes.size()) <= MAX_PATH_LENGTH )
|
kpeter@2625
|
548 |
{
|
kpeter@2625
|
549 |
if (next_dir[tip]) {
|
kpeter@2625
|
550 |
for (OutEdgeIt e = next_out[tip]; e != INVALID; ++e) {
|
kpeter@2625
|
551 |
if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
|
kpeter@2625
|
552 |
u = _graph.target(e);
|
kpeter@2625
|
553 |
pred_edge[u] = e;
|
kpeter@2625
|
554 |
forward[u] = true;
|
kpeter@2625
|
555 |
next_out[tip] = e;
|
kpeter@2625
|
556 |
tip = u;
|
kpeter@2625
|
557 |
path_nodes.push_back(tip);
|
kpeter@2625
|
558 |
goto next_step;
|
kpeter@2625
|
559 |
}
|
kpeter@2625
|
560 |
}
|
kpeter@2625
|
561 |
next_dir[tip] = false;
|
kpeter@2625
|
562 |
}
|
kpeter@2625
|
563 |
for (InEdgeIt e = next_in[tip]; e != INVALID; ++e) {
|
kpeter@2625
|
564 |
if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
|
kpeter@2625
|
565 |
u = _graph.source(e);
|
kpeter@2625
|
566 |
pred_edge[u] = e;
|
kpeter@2625
|
567 |
forward[u] = false;
|
kpeter@2625
|
568 |
next_in[tip] = e;
|
kpeter@2625
|
569 |
tip = u;
|
kpeter@2625
|
570 |
path_nodes.push_back(tip);
|
kpeter@2625
|
571 |
goto next_step;
|
kpeter@2625
|
572 |
}
|
kpeter@2625
|
573 |
}
|
kpeter@2625
|
574 |
|
kpeter@2625
|
575 |
// Relabel tip node
|
kpeter@2625
|
576 |
min_red_cost = std::numeric_limits<LCost>::max() / 2;
|
kpeter@2625
|
577 |
for (OutEdgeIt oe(_graph, tip); oe != INVALID; ++oe) {
|
kpeter@2625
|
578 |
if ( _capacity[oe] - (*_flow)[oe] > 0 &&
|
kpeter@2625
|
579 |
(*_red_cost)[oe] < min_red_cost )
|
kpeter@2625
|
580 |
min_red_cost = (*_red_cost)[oe];
|
kpeter@2625
|
581 |
}
|
kpeter@2625
|
582 |
for (InEdgeIt ie(_graph, tip); ie != INVALID; ++ie) {
|
kpeter@2625
|
583 |
if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < min_red_cost)
|
kpeter@2625
|
584 |
min_red_cost = -(*_red_cost)[ie];
|
kpeter@2625
|
585 |
}
|
kpeter@2625
|
586 |
(*_potential)[tip] -= min_red_cost + _epsilon;
|
kpeter@2625
|
587 |
|
kpeter@2625
|
588 |
// Reset the next edge maps
|
kpeter@2625
|
589 |
next_out[tip] = OutEdgeIt(_graph, tip);
|
kpeter@2625
|
590 |
next_in[tip] = InEdgeIt(_graph, tip);
|
kpeter@2625
|
591 |
next_dir[tip] = true;
|
kpeter@2625
|
592 |
|
kpeter@2625
|
593 |
// Step back
|
kpeter@2625
|
594 |
if (tip != start) {
|
kpeter@2625
|
595 |
path_nodes.pop_back();
|
kpeter@2625
|
596 |
tip = path_nodes[path_nodes.size()-1];
|
kpeter@2625
|
597 |
}
|
kpeter@2625
|
598 |
|
kpeter@2625
|
599 |
next_step:
|
kpeter@2625
|
600 |
continue;
|
kpeter@2625
|
601 |
}
|
kpeter@2625
|
602 |
|
kpeter@2625
|
603 |
// Augment along the found path (as much flow as possible)
|
kpeter@2625
|
604 |
Capacity delta;
|
kpeter@2625
|
605 |
for (int i = 1; i < int(path_nodes.size()); ++i) {
|
kpeter@2625
|
606 |
u = path_nodes[i];
|
kpeter@2625
|
607 |
delta = forward[u] ?
|
kpeter@2625
|
608 |
_capacity[pred_edge[u]] - (*_flow)[pred_edge[u]] :
|
kpeter@2625
|
609 |
(*_flow)[pred_edge[u]];
|
kpeter@2625
|
610 |
delta = std::min(delta, _excess[path_nodes[i-1]]);
|
kpeter@2625
|
611 |
(*_flow)[pred_edge[u]] += forward[u] ? delta : -delta;
|
kpeter@2625
|
612 |
_excess[path_nodes[i-1]] -= delta;
|
kpeter@2625
|
613 |
_excess[u] += delta;
|
kpeter@2625
|
614 |
if (_excess[u] > 0 && _excess[u] <= delta) active_nodes.push_back(u);
|
kpeter@2625
|
615 |
}
|
kpeter@2625
|
616 |
}
|
kpeter@2625
|
617 |
}
|
kpeter@2625
|
618 |
|
kpeter@2625
|
619 |
// Compute node potentials for the original costs
|
kpeter@2625
|
620 |
ResidualCostMap<CostMap> res_cost(_orig_cost);
|
kpeter@2625
|
621 |
BellmanFord< ResGraph, ResidualCostMap<CostMap> >
|
kpeter@2625
|
622 |
bf(*_res_graph, res_cost);
|
kpeter@2625
|
623 |
bf.init(0); bf.start();
|
kpeter@2625
|
624 |
for (NodeIt n(_graph); n != INVALID; ++n)
|
kpeter@2625
|
625 |
(*_potential)[n] = bf.dist(n);
|
kpeter@2625
|
626 |
|
kpeter@2625
|
627 |
// Handle non-zero lower bounds
|
kpeter@2625
|
628 |
if (_lower) {
|
kpeter@2625
|
629 |
for (EdgeIt e(_graph); e != INVALID; ++e)
|
kpeter@2625
|
630 |
(*_flow)[e] += (*_lower)[e];
|
kpeter@2625
|
631 |
}
|
kpeter@2625
|
632 |
return true;
|
kpeter@2625
|
633 |
}
|
kpeter@2625
|
634 |
|
kpeter@2625
|
635 |
/// Execute the algorithm performing push and relabel operations.
|
kpeter@2625
|
636 |
bool startPushRelabel() {
|
kpeter@2625
|
637 |
// Paramters for heuristics
|
kpeter@2625
|
638 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000;
|
kpeter@2625
|
639 |
const int BF_HEURISTIC_BOUND_FACTOR = 3;
|
kpeter@2625
|
640 |
|
kpeter@2625
|
641 |
typename Graph::template NodeMap<bool> hyper(_graph, false);
|
kpeter@2625
|
642 |
typename Graph::template NodeMap<Edge> pred_edge(_graph);
|
kpeter@2625
|
643 |
typename Graph::template NodeMap<bool> forward(_graph);
|
kpeter@2625
|
644 |
typename Graph::template NodeMap<OutEdgeIt> next_out(_graph);
|
kpeter@2625
|
645 |
typename Graph::template NodeMap<InEdgeIt> next_in(_graph);
|
kpeter@2625
|
646 |
typename Graph::template NodeMap<bool> next_dir(_graph);
|
kpeter@2625
|
647 |
std::deque<Node> active_nodes;
|
kpeter@2625
|
648 |
|
kpeter@2625
|
649 |
int node_num = countNodes(_graph);
|
kpeter@2625
|
650 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
|
kpeter@2625
|
651 |
1 : _epsilon / _alpha )
|
kpeter@2625
|
652 |
{
|
kpeter@2625
|
653 |
// "Early Termination" heuristic: use Bellman-Ford algorithm
|
kpeter@2625
|
654 |
// to check if the current flow is optimal
|
kpeter@2625
|
655 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
|
kpeter@2625
|
656 |
typedef ShiftMap< ResidualCostMap<LargeCostMap> > ShiftCostMap;
|
kpeter@2625
|
657 |
ShiftCostMap shift_cost(_res_cost, 1);
|
kpeter@2625
|
658 |
BellmanFord<ResGraph, ShiftCostMap> bf(*_res_graph, shift_cost);
|
kpeter@2625
|
659 |
bf.init(0);
|
kpeter@2625
|
660 |
bool done = false;
|
kpeter@2625
|
661 |
int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(node_num));
|
kpeter@2625
|
662 |
for (int i = 0; i < K && !done; ++i)
|
kpeter@2625
|
663 |
done = bf.processNextWeakRound();
|
kpeter@2625
|
664 |
if (done) break;
|
kpeter@2625
|
665 |
}
|
kpeter@2625
|
666 |
|
kpeter@2625
|
667 |
// Saturate edges not satisfying the optimality condition
|
kpeter@2625
|
668 |
Capacity delta;
|
kpeter@2625
|
669 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
kpeter@2625
|
670 |
if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
|
kpeter@2625
|
671 |
delta = _capacity[e] - (*_flow)[e];
|
kpeter@2625
|
672 |
_excess[_graph.source(e)] -= delta;
|
kpeter@2625
|
673 |
_excess[_graph.target(e)] += delta;
|
kpeter@2625
|
674 |
(*_flow)[e] = _capacity[e];
|
kpeter@2625
|
675 |
}
|
kpeter@2625
|
676 |
if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
|
kpeter@2625
|
677 |
_excess[_graph.target(e)] -= (*_flow)[e];
|
kpeter@2625
|
678 |
_excess[_graph.source(e)] += (*_flow)[e];
|
kpeter@2625
|
679 |
(*_flow)[e] = 0;
|
kpeter@2625
|
680 |
}
|
kpeter@2625
|
681 |
}
|
kpeter@2625
|
682 |
|
kpeter@2625
|
683 |
// Find active nodes (i.e. nodes with positive excess)
|
kpeter@2625
|
684 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@2625
|
685 |
if (_excess[n] > 0) active_nodes.push_back(n);
|
kpeter@2625
|
686 |
}
|
kpeter@2625
|
687 |
|
kpeter@2625
|
688 |
// Initialize the next edge maps
|
kpeter@2625
|
689 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@2625
|
690 |
next_out[n] = OutEdgeIt(_graph, n);
|
kpeter@2625
|
691 |
next_in[n] = InEdgeIt(_graph, n);
|
kpeter@2625
|
692 |
next_dir[n] = true;
|
kpeter@2625
|
693 |
}
|
kpeter@2625
|
694 |
|
kpeter@2625
|
695 |
// Perform push and relabel operations
|
kpeter@2625
|
696 |
while (active_nodes.size() > 0) {
|
kpeter@2625
|
697 |
// Select an active node (FIFO selection)
|
kpeter@2577
|
698 |
Node n = active_nodes[0], t;
|
kpeter@2577
|
699 |
bool relabel_enabled = true;
|
kpeter@2577
|
700 |
|
kpeter@2625
|
701 |
// Perform push operations if there are admissible edges
|
kpeter@2625
|
702 |
if (_excess[n] > 0 && next_dir[n]) {
|
kpeter@2625
|
703 |
OutEdgeIt e = next_out[n];
|
kpeter@2625
|
704 |
for ( ; e != INVALID; ++e) {
|
kpeter@2581
|
705 |
if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
|
kpeter@2625
|
706 |
delta = std::min(_capacity[e] - (*_flow)[e], _excess[n]);
|
kpeter@2577
|
707 |
t = _graph.target(e);
|
kpeter@2577
|
708 |
|
kpeter@2577
|
709 |
// Push-look-ahead heuristic
|
kpeter@2577
|
710 |
Capacity ahead = -_excess[t];
|
kpeter@2577
|
711 |
for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
|
kpeter@2581
|
712 |
if (_capacity[oe] - (*_flow)[oe] > 0 && (*_red_cost)[oe] < 0)
|
kpeter@2581
|
713 |
ahead += _capacity[oe] - (*_flow)[oe];
|
kpeter@2577
|
714 |
}
|
kpeter@2577
|
715 |
for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
|
kpeter@2581
|
716 |
if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < 0)
|
kpeter@2581
|
717 |
ahead += (*_flow)[ie];
|
kpeter@2577
|
718 |
}
|
kpeter@2577
|
719 |
if (ahead < 0) ahead = 0;
|
kpeter@2577
|
720 |
|
kpeter@2625
|
721 |
// Push flow along the edge
|
kpeter@2577
|
722 |
if (ahead < delta) {
|
kpeter@2581
|
723 |
(*_flow)[e] += ahead;
|
kpeter@2577
|
724 |
_excess[n] -= ahead;
|
kpeter@2577
|
725 |
_excess[t] += ahead;
|
kpeter@2577
|
726 |
active_nodes.push_front(t);
|
kpeter@2577
|
727 |
hyper[t] = true;
|
kpeter@2577
|
728 |
relabel_enabled = false;
|
kpeter@2577
|
729 |
break;
|
kpeter@2577
|
730 |
} else {
|
kpeter@2581
|
731 |
(*_flow)[e] += delta;
|
kpeter@2577
|
732 |
_excess[n] -= delta;
|
kpeter@2577
|
733 |
_excess[t] += delta;
|
kpeter@2577
|
734 |
if (_excess[t] > 0 && _excess[t] <= delta)
|
kpeter@2577
|
735 |
active_nodes.push_back(t);
|
kpeter@2577
|
736 |
}
|
kpeter@2577
|
737 |
|
kpeter@2577
|
738 |
if (_excess[n] == 0) break;
|
kpeter@2577
|
739 |
}
|
kpeter@2577
|
740 |
}
|
kpeter@2625
|
741 |
if (e != INVALID) {
|
kpeter@2625
|
742 |
next_out[n] = e;
|
kpeter@2625
|
743 |
} else {
|
kpeter@2625
|
744 |
next_dir[n] = false;
|
kpeter@2625
|
745 |
}
|
kpeter@2577
|
746 |
}
|
kpeter@2577
|
747 |
|
kpeter@2625
|
748 |
if (_excess[n] > 0 && !next_dir[n]) {
|
kpeter@2625
|
749 |
InEdgeIt e = next_in[n];
|
kpeter@2625
|
750 |
for ( ; e != INVALID; ++e) {
|
kpeter@2581
|
751 |
if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
|
kpeter@2625
|
752 |
delta = std::min((*_flow)[e], _excess[n]);
|
kpeter@2577
|
753 |
t = _graph.source(e);
|
kpeter@2577
|
754 |
|
kpeter@2577
|
755 |
// Push-look-ahead heuristic
|
kpeter@2577
|
756 |
Capacity ahead = -_excess[t];
|
kpeter@2577
|
757 |
for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
|
kpeter@2581
|
758 |
if (_capacity[oe] - (*_flow)[oe] > 0 && (*_red_cost)[oe] < 0)
|
kpeter@2581
|
759 |
ahead += _capacity[oe] - (*_flow)[oe];
|
kpeter@2577
|
760 |
}
|
kpeter@2577
|
761 |
for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
|
kpeter@2581
|
762 |
if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < 0)
|
kpeter@2581
|
763 |
ahead += (*_flow)[ie];
|
kpeter@2577
|
764 |
}
|
kpeter@2577
|
765 |
if (ahead < 0) ahead = 0;
|
kpeter@2577
|
766 |
|
kpeter@2625
|
767 |
// Push flow along the edge
|
kpeter@2577
|
768 |
if (ahead < delta) {
|
kpeter@2581
|
769 |
(*_flow)[e] -= ahead;
|
kpeter@2577
|
770 |
_excess[n] -= ahead;
|
kpeter@2577
|
771 |
_excess[t] += ahead;
|
kpeter@2577
|
772 |
active_nodes.push_front(t);
|
kpeter@2577
|
773 |
hyper[t] = true;
|
kpeter@2577
|
774 |
relabel_enabled = false;
|
kpeter@2577
|
775 |
break;
|
kpeter@2577
|
776 |
} else {
|
kpeter@2581
|
777 |
(*_flow)[e] -= delta;
|
kpeter@2577
|
778 |
_excess[n] -= delta;
|
kpeter@2577
|
779 |
_excess[t] += delta;
|
kpeter@2577
|
780 |
if (_excess[t] > 0 && _excess[t] <= delta)
|
kpeter@2577
|
781 |
active_nodes.push_back(t);
|
kpeter@2577
|
782 |
}
|
kpeter@2577
|
783 |
|
kpeter@2577
|
784 |
if (_excess[n] == 0) break;
|
kpeter@2577
|
785 |
}
|
kpeter@2577
|
786 |
}
|
kpeter@2625
|
787 |
next_in[n] = e;
|
kpeter@2577
|
788 |
}
|
kpeter@2577
|
789 |
|
kpeter@2625
|
790 |
// Relabel the node if it is still active (or hyper)
|
kpeter@2577
|
791 |
if (relabel_enabled && (_excess[n] > 0 || hyper[n])) {
|
kpeter@2625
|
792 |
LCost min_red_cost = std::numeric_limits<LCost>::max() / 2;
|
kpeter@2577
|
793 |
for (OutEdgeIt oe(_graph, n); oe != INVALID; ++oe) {
|
kpeter@2581
|
794 |
if ( _capacity[oe] - (*_flow)[oe] > 0 &&
|
kpeter@2581
|
795 |
(*_red_cost)[oe] < min_red_cost )
|
kpeter@2581
|
796 |
min_red_cost = (*_red_cost)[oe];
|
kpeter@2577
|
797 |
}
|
kpeter@2577
|
798 |
for (InEdgeIt ie(_graph, n); ie != INVALID; ++ie) {
|
kpeter@2581
|
799 |
if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < min_red_cost)
|
kpeter@2581
|
800 |
min_red_cost = -(*_red_cost)[ie];
|
kpeter@2577
|
801 |
}
|
kpeter@2581
|
802 |
(*_potential)[n] -= min_red_cost + _epsilon;
|
kpeter@2577
|
803 |
hyper[n] = false;
|
kpeter@2625
|
804 |
|
kpeter@2625
|
805 |
// Reset the next edge maps
|
kpeter@2625
|
806 |
next_out[n] = OutEdgeIt(_graph, n);
|
kpeter@2625
|
807 |
next_in[n] = InEdgeIt(_graph, n);
|
kpeter@2625
|
808 |
next_dir[n] = true;
|
kpeter@2577
|
809 |
}
|
kpeter@2577
|
810 |
|
kpeter@2625
|
811 |
// Remove nodes that are not active nor hyper
|
kpeter@2577
|
812 |
while ( active_nodes.size() > 0 &&
|
kpeter@2577
|
813 |
_excess[active_nodes[0]] <= 0 &&
|
kpeter@2577
|
814 |
!hyper[active_nodes[0]] ) {
|
kpeter@2577
|
815 |
active_nodes.pop_front();
|
kpeter@2577
|
816 |
}
|
kpeter@2577
|
817 |
}
|
kpeter@2577
|
818 |
}
|
kpeter@2577
|
819 |
|
kpeter@2625
|
820 |
// Compute node potentials for the original costs
|
kpeter@2581
|
821 |
ResidualCostMap<CostMap> res_cost(_orig_cost);
|
kpeter@2581
|
822 |
BellmanFord< ResGraph, ResidualCostMap<CostMap> >
|
kpeter@2581
|
823 |
bf(*_res_graph, res_cost);
|
kpeter@2581
|
824 |
bf.init(0); bf.start();
|
kpeter@2581
|
825 |
for (NodeIt n(_graph); n != INVALID; ++n)
|
kpeter@2581
|
826 |
(*_potential)[n] = bf.dist(n);
|
kpeter@2581
|
827 |
|
kpeter@2625
|
828 |
// Handle non-zero lower bounds
|
kpeter@2577
|
829 |
if (_lower) {
|
kpeter@2577
|
830 |
for (EdgeIt e(_graph); e != INVALID; ++e)
|
kpeter@2581
|
831 |
(*_flow)[e] += (*_lower)[e];
|
kpeter@2577
|
832 |
}
|
kpeter@2577
|
833 |
return true;
|
kpeter@2577
|
834 |
}
|
kpeter@2577
|
835 |
|
kpeter@2577
|
836 |
}; //class CostScaling
|
kpeter@2577
|
837 |
|
kpeter@2577
|
838 |
///@}
|
kpeter@2577
|
839 |
|
kpeter@2577
|
840 |
} //namespace lemon
|
kpeter@2577
|
841 |
|
kpeter@2577
|
842 |
#endif //LEMON_COST_SCALING_H
|