kpeter@814
|
1 |
/* -*- C++ -*-
|
kpeter@814
|
2 |
*
|
kpeter@814
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
kpeter@814
|
4 |
*
|
kpeter@814
|
5 |
* Copyright (C) 2003-2008
|
kpeter@814
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@814
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@814
|
8 |
*
|
kpeter@814
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@814
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@814
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@814
|
12 |
*
|
kpeter@814
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@814
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@814
|
15 |
* purpose.
|
kpeter@814
|
16 |
*
|
kpeter@814
|
17 |
*/
|
kpeter@814
|
18 |
|
kpeter@814
|
19 |
#ifndef LEMON_CANCEL_AND_TIGHTEN_H
|
kpeter@814
|
20 |
#define LEMON_CANCEL_AND_TIGHTEN_H
|
kpeter@814
|
21 |
|
kpeter@814
|
22 |
/// \ingroup min_cost_flow
|
kpeter@814
|
23 |
///
|
kpeter@814
|
24 |
/// \file
|
kpeter@814
|
25 |
/// \brief Cancel and Tighten algorithm for finding a minimum cost flow.
|
kpeter@814
|
26 |
|
kpeter@814
|
27 |
#include <vector>
|
kpeter@814
|
28 |
|
kpeter@814
|
29 |
#include <lemon/circulation.h>
|
kpeter@814
|
30 |
#include <lemon/bellman_ford.h>
|
kpeter@814
|
31 |
#include <lemon/howard.h>
|
kpeter@814
|
32 |
#include <lemon/adaptors.h>
|
kpeter@814
|
33 |
#include <lemon/tolerance.h>
|
kpeter@814
|
34 |
#include <lemon/math.h>
|
kpeter@814
|
35 |
|
kpeter@814
|
36 |
#include <lemon/static_graph.h>
|
kpeter@814
|
37 |
|
kpeter@814
|
38 |
namespace lemon {
|
kpeter@814
|
39 |
|
kpeter@814
|
40 |
/// \addtogroup min_cost_flow
|
kpeter@814
|
41 |
/// @{
|
kpeter@814
|
42 |
|
kpeter@814
|
43 |
/// \brief Implementation of the Cancel and Tighten algorithm for
|
kpeter@814
|
44 |
/// finding a minimum cost flow.
|
kpeter@814
|
45 |
///
|
kpeter@814
|
46 |
/// \ref CancelAndTighten implements the Cancel and Tighten algorithm for
|
kpeter@814
|
47 |
/// finding a minimum cost flow.
|
kpeter@814
|
48 |
///
|
kpeter@814
|
49 |
/// \tparam Digraph The digraph type the algorithm runs on.
|
kpeter@814
|
50 |
/// \tparam LowerMap The type of the lower bound map.
|
kpeter@814
|
51 |
/// \tparam CapacityMap The type of the capacity (upper bound) map.
|
kpeter@814
|
52 |
/// \tparam CostMap The type of the cost (length) map.
|
kpeter@814
|
53 |
/// \tparam SupplyMap The type of the supply map.
|
kpeter@814
|
54 |
///
|
kpeter@814
|
55 |
/// \warning
|
kpeter@814
|
56 |
/// - Arc capacities and costs should be \e non-negative \e integers.
|
kpeter@814
|
57 |
/// - Supply values should be \e signed \e integers.
|
kpeter@814
|
58 |
/// - The value types of the maps should be convertible to each other.
|
kpeter@814
|
59 |
/// - \c CostMap::Value must be signed type.
|
kpeter@814
|
60 |
///
|
kpeter@814
|
61 |
/// \author Peter Kovacs
|
kpeter@814
|
62 |
template < typename Digraph,
|
kpeter@814
|
63 |
typename LowerMap = typename Digraph::template ArcMap<int>,
|
kpeter@814
|
64 |
typename CapacityMap = typename Digraph::template ArcMap<int>,
|
kpeter@814
|
65 |
typename CostMap = typename Digraph::template ArcMap<int>,
|
kpeter@814
|
66 |
typename SupplyMap = typename Digraph::template NodeMap<int> >
|
kpeter@814
|
67 |
class CancelAndTighten
|
kpeter@814
|
68 |
{
|
kpeter@814
|
69 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
kpeter@814
|
70 |
|
kpeter@814
|
71 |
typedef typename CapacityMap::Value Capacity;
|
kpeter@814
|
72 |
typedef typename CostMap::Value Cost;
|
kpeter@814
|
73 |
typedef typename SupplyMap::Value Supply;
|
kpeter@814
|
74 |
typedef typename Digraph::template ArcMap<Capacity> CapacityArcMap;
|
kpeter@814
|
75 |
typedef typename Digraph::template NodeMap<Supply> SupplyNodeMap;
|
kpeter@814
|
76 |
|
kpeter@814
|
77 |
typedef ResidualDigraph< const Digraph,
|
kpeter@814
|
78 |
CapacityArcMap, CapacityArcMap > ResDigraph;
|
kpeter@814
|
79 |
|
kpeter@814
|
80 |
public:
|
kpeter@814
|
81 |
|
kpeter@814
|
82 |
/// The type of the flow map.
|
kpeter@814
|
83 |
typedef typename Digraph::template ArcMap<Capacity> FlowMap;
|
kpeter@814
|
84 |
/// The type of the potential map.
|
kpeter@814
|
85 |
typedef typename Digraph::template NodeMap<Cost> PotentialMap;
|
kpeter@814
|
86 |
|
kpeter@814
|
87 |
private:
|
kpeter@814
|
88 |
|
kpeter@814
|
89 |
/// \brief Map adaptor class for handling residual arc costs.
|
kpeter@814
|
90 |
///
|
kpeter@814
|
91 |
/// Map adaptor class for handling residual arc costs.
|
kpeter@814
|
92 |
class ResidualCostMap : public MapBase<typename ResDigraph::Arc, Cost>
|
kpeter@814
|
93 |
{
|
kpeter@814
|
94 |
typedef typename ResDigraph::Arc Arc;
|
kpeter@814
|
95 |
|
kpeter@814
|
96 |
private:
|
kpeter@814
|
97 |
|
kpeter@814
|
98 |
const CostMap &_cost_map;
|
kpeter@814
|
99 |
|
kpeter@814
|
100 |
public:
|
kpeter@814
|
101 |
|
kpeter@814
|
102 |
///\e
|
kpeter@814
|
103 |
ResidualCostMap(const CostMap &cost_map) : _cost_map(cost_map) {}
|
kpeter@814
|
104 |
|
kpeter@814
|
105 |
///\e
|
kpeter@814
|
106 |
Cost operator[](const Arc &e) const {
|
kpeter@814
|
107 |
return ResDigraph::forward(e) ? _cost_map[e] : -_cost_map[e];
|
kpeter@814
|
108 |
}
|
kpeter@814
|
109 |
|
kpeter@814
|
110 |
}; //class ResidualCostMap
|
kpeter@814
|
111 |
|
kpeter@814
|
112 |
/// \brief Map adaptor class for handling reduced arc costs.
|
kpeter@814
|
113 |
///
|
kpeter@814
|
114 |
/// Map adaptor class for handling reduced arc costs.
|
kpeter@814
|
115 |
class ReducedCostMap : public MapBase<Arc, Cost>
|
kpeter@814
|
116 |
{
|
kpeter@814
|
117 |
private:
|
kpeter@814
|
118 |
|
kpeter@814
|
119 |
const Digraph &_gr;
|
kpeter@814
|
120 |
const CostMap &_cost_map;
|
kpeter@814
|
121 |
const PotentialMap &_pot_map;
|
kpeter@814
|
122 |
|
kpeter@814
|
123 |
public:
|
kpeter@814
|
124 |
|
kpeter@814
|
125 |
///\e
|
kpeter@814
|
126 |
ReducedCostMap( const Digraph &gr,
|
kpeter@814
|
127 |
const CostMap &cost_map,
|
kpeter@814
|
128 |
const PotentialMap &pot_map ) :
|
kpeter@814
|
129 |
_gr(gr), _cost_map(cost_map), _pot_map(pot_map) {}
|
kpeter@814
|
130 |
|
kpeter@814
|
131 |
///\e
|
kpeter@814
|
132 |
inline Cost operator[](const Arc &e) const {
|
kpeter@814
|
133 |
return _cost_map[e] + _pot_map[_gr.source(e)]
|
kpeter@814
|
134 |
- _pot_map[_gr.target(e)];
|
kpeter@814
|
135 |
}
|
kpeter@814
|
136 |
|
kpeter@814
|
137 |
}; //class ReducedCostMap
|
kpeter@814
|
138 |
|
kpeter@814
|
139 |
struct BFOperationTraits {
|
kpeter@814
|
140 |
static double zero() { return 0; }
|
kpeter@814
|
141 |
|
kpeter@814
|
142 |
static double infinity() {
|
kpeter@814
|
143 |
return std::numeric_limits<double>::infinity();
|
kpeter@814
|
144 |
}
|
kpeter@814
|
145 |
|
kpeter@814
|
146 |
static double plus(const double& left, const double& right) {
|
kpeter@814
|
147 |
return left + right;
|
kpeter@814
|
148 |
}
|
kpeter@814
|
149 |
|
kpeter@814
|
150 |
static bool less(const double& left, const double& right) {
|
kpeter@814
|
151 |
return left + 1e-6 < right;
|
kpeter@814
|
152 |
}
|
kpeter@814
|
153 |
}; // class BFOperationTraits
|
kpeter@814
|
154 |
|
kpeter@814
|
155 |
private:
|
kpeter@814
|
156 |
|
kpeter@814
|
157 |
// The digraph the algorithm runs on
|
kpeter@814
|
158 |
const Digraph &_graph;
|
kpeter@814
|
159 |
// The original lower bound map
|
kpeter@814
|
160 |
const LowerMap *_lower;
|
kpeter@814
|
161 |
// The modified capacity map
|
kpeter@814
|
162 |
CapacityArcMap _capacity;
|
kpeter@814
|
163 |
// The original cost map
|
kpeter@814
|
164 |
const CostMap &_cost;
|
kpeter@814
|
165 |
// The modified supply map
|
kpeter@814
|
166 |
SupplyNodeMap _supply;
|
kpeter@814
|
167 |
bool _valid_supply;
|
kpeter@814
|
168 |
|
kpeter@814
|
169 |
// Arc map of the current flow
|
kpeter@814
|
170 |
FlowMap *_flow;
|
kpeter@814
|
171 |
bool _local_flow;
|
kpeter@814
|
172 |
// Node map of the current potentials
|
kpeter@814
|
173 |
PotentialMap *_potential;
|
kpeter@814
|
174 |
bool _local_potential;
|
kpeter@814
|
175 |
|
kpeter@814
|
176 |
// The residual digraph
|
kpeter@814
|
177 |
ResDigraph *_res_graph;
|
kpeter@814
|
178 |
// The residual cost map
|
kpeter@814
|
179 |
ResidualCostMap _res_cost;
|
kpeter@814
|
180 |
|
kpeter@814
|
181 |
public:
|
kpeter@814
|
182 |
|
kpeter@814
|
183 |
/// \brief General constructor (with lower bounds).
|
kpeter@814
|
184 |
///
|
kpeter@814
|
185 |
/// General constructor (with lower bounds).
|
kpeter@814
|
186 |
///
|
kpeter@814
|
187 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@814
|
188 |
/// \param lower The lower bounds of the arcs.
|
kpeter@814
|
189 |
/// \param capacity The capacities (upper bounds) of the arcs.
|
kpeter@814
|
190 |
/// \param cost The cost (length) values of the arcs.
|
kpeter@814
|
191 |
/// \param supply The supply values of the nodes (signed).
|
kpeter@814
|
192 |
CancelAndTighten( const Digraph &digraph,
|
kpeter@814
|
193 |
const LowerMap &lower,
|
kpeter@814
|
194 |
const CapacityMap &capacity,
|
kpeter@814
|
195 |
const CostMap &cost,
|
kpeter@814
|
196 |
const SupplyMap &supply ) :
|
kpeter@814
|
197 |
_graph(digraph), _lower(&lower), _capacity(digraph), _cost(cost),
|
kpeter@814
|
198 |
_supply(digraph), _flow(NULL), _local_flow(false),
|
kpeter@814
|
199 |
_potential(NULL), _local_potential(false),
|
kpeter@814
|
200 |
_res_graph(NULL), _res_cost(_cost)
|
kpeter@814
|
201 |
{
|
kpeter@814
|
202 |
// Check the sum of supply values
|
kpeter@814
|
203 |
Supply sum = 0;
|
kpeter@814
|
204 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@814
|
205 |
_supply[n] = supply[n];
|
kpeter@814
|
206 |
sum += _supply[n];
|
kpeter@814
|
207 |
}
|
kpeter@814
|
208 |
_valid_supply = sum == 0;
|
kpeter@814
|
209 |
|
kpeter@814
|
210 |
// Remove non-zero lower bounds
|
kpeter@814
|
211 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
kpeter@814
|
212 |
_capacity[e] = capacity[e];
|
kpeter@814
|
213 |
if (lower[e] != 0) {
|
kpeter@814
|
214 |
_capacity[e] -= lower[e];
|
kpeter@814
|
215 |
_supply[_graph.source(e)] -= lower[e];
|
kpeter@814
|
216 |
_supply[_graph.target(e)] += lower[e];
|
kpeter@814
|
217 |
}
|
kpeter@814
|
218 |
}
|
kpeter@814
|
219 |
}
|
kpeter@814
|
220 |
/*
|
kpeter@814
|
221 |
/// \brief General constructor (without lower bounds).
|
kpeter@814
|
222 |
///
|
kpeter@814
|
223 |
/// General constructor (without lower bounds).
|
kpeter@814
|
224 |
///
|
kpeter@814
|
225 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@814
|
226 |
/// \param capacity The capacities (upper bounds) of the arcs.
|
kpeter@814
|
227 |
/// \param cost The cost (length) values of the arcs.
|
kpeter@814
|
228 |
/// \param supply The supply values of the nodes (signed).
|
kpeter@814
|
229 |
CancelAndTighten( const Digraph &digraph,
|
kpeter@814
|
230 |
const CapacityMap &capacity,
|
kpeter@814
|
231 |
const CostMap &cost,
|
kpeter@814
|
232 |
const SupplyMap &supply ) :
|
kpeter@814
|
233 |
_graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost),
|
kpeter@814
|
234 |
_supply(supply), _flow(NULL), _local_flow(false),
|
kpeter@814
|
235 |
_potential(NULL), _local_potential(false),
|
kpeter@814
|
236 |
_res_graph(NULL), _res_cost(_cost)
|
kpeter@814
|
237 |
{
|
kpeter@814
|
238 |
// Check the sum of supply values
|
kpeter@814
|
239 |
Supply sum = 0;
|
kpeter@814
|
240 |
for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
|
kpeter@814
|
241 |
_valid_supply = sum == 0;
|
kpeter@814
|
242 |
}
|
kpeter@814
|
243 |
|
kpeter@814
|
244 |
/// \brief Simple constructor (with lower bounds).
|
kpeter@814
|
245 |
///
|
kpeter@814
|
246 |
/// Simple constructor (with lower bounds).
|
kpeter@814
|
247 |
///
|
kpeter@814
|
248 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@814
|
249 |
/// \param lower The lower bounds of the arcs.
|
kpeter@814
|
250 |
/// \param capacity The capacities (upper bounds) of the arcs.
|
kpeter@814
|
251 |
/// \param cost The cost (length) values of the arcs.
|
kpeter@814
|
252 |
/// \param s The source node.
|
kpeter@814
|
253 |
/// \param t The target node.
|
kpeter@814
|
254 |
/// \param flow_value The required amount of flow from node \c s
|
kpeter@814
|
255 |
/// to node \c t (i.e. the supply of \c s and the demand of \c t).
|
kpeter@814
|
256 |
CancelAndTighten( const Digraph &digraph,
|
kpeter@814
|
257 |
const LowerMap &lower,
|
kpeter@814
|
258 |
const CapacityMap &capacity,
|
kpeter@814
|
259 |
const CostMap &cost,
|
kpeter@814
|
260 |
Node s, Node t,
|
kpeter@814
|
261 |
Supply flow_value ) :
|
kpeter@814
|
262 |
_graph(digraph), _lower(&lower), _capacity(capacity), _cost(cost),
|
kpeter@814
|
263 |
_supply(digraph, 0), _flow(NULL), _local_flow(false),
|
kpeter@814
|
264 |
_potential(NULL), _local_potential(false),
|
kpeter@814
|
265 |
_res_graph(NULL), _res_cost(_cost)
|
kpeter@814
|
266 |
{
|
kpeter@814
|
267 |
// Remove non-zero lower bounds
|
kpeter@814
|
268 |
_supply[s] = flow_value;
|
kpeter@814
|
269 |
_supply[t] = -flow_value;
|
kpeter@814
|
270 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
kpeter@814
|
271 |
if (lower[e] != 0) {
|
kpeter@814
|
272 |
_capacity[e] -= lower[e];
|
kpeter@814
|
273 |
_supply[_graph.source(e)] -= lower[e];
|
kpeter@814
|
274 |
_supply[_graph.target(e)] += lower[e];
|
kpeter@814
|
275 |
}
|
kpeter@814
|
276 |
}
|
kpeter@814
|
277 |
_valid_supply = true;
|
kpeter@814
|
278 |
}
|
kpeter@814
|
279 |
|
kpeter@814
|
280 |
/// \brief Simple constructor (without lower bounds).
|
kpeter@814
|
281 |
///
|
kpeter@814
|
282 |
/// Simple constructor (without lower bounds).
|
kpeter@814
|
283 |
///
|
kpeter@814
|
284 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@814
|
285 |
/// \param capacity The capacities (upper bounds) of the arcs.
|
kpeter@814
|
286 |
/// \param cost The cost (length) values of the arcs.
|
kpeter@814
|
287 |
/// \param s The source node.
|
kpeter@814
|
288 |
/// \param t The target node.
|
kpeter@814
|
289 |
/// \param flow_value The required amount of flow from node \c s
|
kpeter@814
|
290 |
/// to node \c t (i.e. the supply of \c s and the demand of \c t).
|
kpeter@814
|
291 |
CancelAndTighten( const Digraph &digraph,
|
kpeter@814
|
292 |
const CapacityMap &capacity,
|
kpeter@814
|
293 |
const CostMap &cost,
|
kpeter@814
|
294 |
Node s, Node t,
|
kpeter@814
|
295 |
Supply flow_value ) :
|
kpeter@814
|
296 |
_graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost),
|
kpeter@814
|
297 |
_supply(digraph, 0), _flow(NULL), _local_flow(false),
|
kpeter@814
|
298 |
_potential(NULL), _local_potential(false),
|
kpeter@814
|
299 |
_res_graph(NULL), _res_cost(_cost)
|
kpeter@814
|
300 |
{
|
kpeter@814
|
301 |
_supply[s] = flow_value;
|
kpeter@814
|
302 |
_supply[t] = -flow_value;
|
kpeter@814
|
303 |
_valid_supply = true;
|
kpeter@814
|
304 |
}
|
kpeter@814
|
305 |
*/
|
kpeter@814
|
306 |
/// Destructor.
|
kpeter@814
|
307 |
~CancelAndTighten() {
|
kpeter@814
|
308 |
if (_local_flow) delete _flow;
|
kpeter@814
|
309 |
if (_local_potential) delete _potential;
|
kpeter@814
|
310 |
delete _res_graph;
|
kpeter@814
|
311 |
}
|
kpeter@814
|
312 |
|
kpeter@814
|
313 |
/// \brief Set the flow map.
|
kpeter@814
|
314 |
///
|
kpeter@814
|
315 |
/// Set the flow map.
|
kpeter@814
|
316 |
///
|
kpeter@814
|
317 |
/// \return \c (*this)
|
kpeter@814
|
318 |
CancelAndTighten& flowMap(FlowMap &map) {
|
kpeter@814
|
319 |
if (_local_flow) {
|
kpeter@814
|
320 |
delete _flow;
|
kpeter@814
|
321 |
_local_flow = false;
|
kpeter@814
|
322 |
}
|
kpeter@814
|
323 |
_flow = ↦
|
kpeter@814
|
324 |
return *this;
|
kpeter@814
|
325 |
}
|
kpeter@814
|
326 |
|
kpeter@814
|
327 |
/// \brief Set the potential map.
|
kpeter@814
|
328 |
///
|
kpeter@814
|
329 |
/// Set the potential map.
|
kpeter@814
|
330 |
///
|
kpeter@814
|
331 |
/// \return \c (*this)
|
kpeter@814
|
332 |
CancelAndTighten& potentialMap(PotentialMap &map) {
|
kpeter@814
|
333 |
if (_local_potential) {
|
kpeter@814
|
334 |
delete _potential;
|
kpeter@814
|
335 |
_local_potential = false;
|
kpeter@814
|
336 |
}
|
kpeter@814
|
337 |
_potential = ↦
|
kpeter@814
|
338 |
return *this;
|
kpeter@814
|
339 |
}
|
kpeter@814
|
340 |
|
kpeter@814
|
341 |
/// \name Execution control
|
kpeter@814
|
342 |
|
kpeter@814
|
343 |
/// @{
|
kpeter@814
|
344 |
|
kpeter@814
|
345 |
/// \brief Run the algorithm.
|
kpeter@814
|
346 |
///
|
kpeter@814
|
347 |
/// Run the algorithm.
|
kpeter@814
|
348 |
///
|
kpeter@814
|
349 |
/// \return \c true if a feasible flow can be found.
|
kpeter@814
|
350 |
bool run() {
|
kpeter@814
|
351 |
return init() && start();
|
kpeter@814
|
352 |
}
|
kpeter@814
|
353 |
|
kpeter@814
|
354 |
/// @}
|
kpeter@814
|
355 |
|
kpeter@814
|
356 |
/// \name Query Functions
|
kpeter@814
|
357 |
/// The result of the algorithm can be obtained using these
|
kpeter@814
|
358 |
/// functions.\n
|
kpeter@814
|
359 |
/// \ref lemon::CancelAndTighten::run() "run()" must be called before
|
kpeter@814
|
360 |
/// using them.
|
kpeter@814
|
361 |
|
kpeter@814
|
362 |
/// @{
|
kpeter@814
|
363 |
|
kpeter@814
|
364 |
/// \brief Return a const reference to the arc map storing the
|
kpeter@814
|
365 |
/// found flow.
|
kpeter@814
|
366 |
///
|
kpeter@814
|
367 |
/// Return a const reference to the arc map storing the found flow.
|
kpeter@814
|
368 |
///
|
kpeter@814
|
369 |
/// \pre \ref run() must be called before using this function.
|
kpeter@814
|
370 |
const FlowMap& flowMap() const {
|
kpeter@814
|
371 |
return *_flow;
|
kpeter@814
|
372 |
}
|
kpeter@814
|
373 |
|
kpeter@814
|
374 |
/// \brief Return a const reference to the node map storing the
|
kpeter@814
|
375 |
/// found potentials (the dual solution).
|
kpeter@814
|
376 |
///
|
kpeter@814
|
377 |
/// Return a const reference to the node map storing the found
|
kpeter@814
|
378 |
/// potentials (the dual solution).
|
kpeter@814
|
379 |
///
|
kpeter@814
|
380 |
/// \pre \ref run() must be called before using this function.
|
kpeter@814
|
381 |
const PotentialMap& potentialMap() const {
|
kpeter@814
|
382 |
return *_potential;
|
kpeter@814
|
383 |
}
|
kpeter@814
|
384 |
|
kpeter@814
|
385 |
/// \brief Return the flow on the given arc.
|
kpeter@814
|
386 |
///
|
kpeter@814
|
387 |
/// Return the flow on the given arc.
|
kpeter@814
|
388 |
///
|
kpeter@814
|
389 |
/// \pre \ref run() must be called before using this function.
|
kpeter@814
|
390 |
Capacity flow(const Arc& arc) const {
|
kpeter@814
|
391 |
return (*_flow)[arc];
|
kpeter@814
|
392 |
}
|
kpeter@814
|
393 |
|
kpeter@814
|
394 |
/// \brief Return the potential of the given node.
|
kpeter@814
|
395 |
///
|
kpeter@814
|
396 |
/// Return the potential of the given node.
|
kpeter@814
|
397 |
///
|
kpeter@814
|
398 |
/// \pre \ref run() must be called before using this function.
|
kpeter@814
|
399 |
Cost potential(const Node& node) const {
|
kpeter@814
|
400 |
return (*_potential)[node];
|
kpeter@814
|
401 |
}
|
kpeter@814
|
402 |
|
kpeter@814
|
403 |
/// \brief Return the total cost of the found flow.
|
kpeter@814
|
404 |
///
|
kpeter@814
|
405 |
/// Return the total cost of the found flow. The complexity of the
|
kpeter@814
|
406 |
/// function is \f$ O(e) \f$.
|
kpeter@814
|
407 |
///
|
kpeter@814
|
408 |
/// \pre \ref run() must be called before using this function.
|
kpeter@814
|
409 |
Cost totalCost() const {
|
kpeter@814
|
410 |
Cost c = 0;
|
kpeter@814
|
411 |
for (ArcIt e(_graph); e != INVALID; ++e)
|
kpeter@814
|
412 |
c += (*_flow)[e] * _cost[e];
|
kpeter@814
|
413 |
return c;
|
kpeter@814
|
414 |
}
|
kpeter@814
|
415 |
|
kpeter@814
|
416 |
/// @}
|
kpeter@814
|
417 |
|
kpeter@814
|
418 |
private:
|
kpeter@814
|
419 |
|
kpeter@814
|
420 |
/// Initialize the algorithm.
|
kpeter@814
|
421 |
bool init() {
|
kpeter@814
|
422 |
if (!_valid_supply) return false;
|
kpeter@814
|
423 |
|
kpeter@814
|
424 |
// Initialize flow and potential maps
|
kpeter@814
|
425 |
if (!_flow) {
|
kpeter@814
|
426 |
_flow = new FlowMap(_graph);
|
kpeter@814
|
427 |
_local_flow = true;
|
kpeter@814
|
428 |
}
|
kpeter@814
|
429 |
if (!_potential) {
|
kpeter@814
|
430 |
_potential = new PotentialMap(_graph);
|
kpeter@814
|
431 |
_local_potential = true;
|
kpeter@814
|
432 |
}
|
kpeter@814
|
433 |
|
kpeter@814
|
434 |
_res_graph = new ResDigraph(_graph, _capacity, *_flow);
|
kpeter@814
|
435 |
|
kpeter@814
|
436 |
// Find a feasible flow using Circulation
|
kpeter@814
|
437 |
Circulation< Digraph, ConstMap<Arc, Capacity>,
|
kpeter@814
|
438 |
CapacityArcMap, SupplyMap >
|
kpeter@814
|
439 |
circulation( _graph, constMap<Arc>(Capacity(0)),
|
kpeter@814
|
440 |
_capacity, _supply );
|
kpeter@814
|
441 |
return circulation.flowMap(*_flow).run();
|
kpeter@814
|
442 |
}
|
kpeter@814
|
443 |
|
kpeter@814
|
444 |
bool start() {
|
kpeter@814
|
445 |
const double LIMIT_FACTOR = 0.01;
|
kpeter@814
|
446 |
const int MIN_LIMIT = 3;
|
kpeter@814
|
447 |
|
kpeter@814
|
448 |
typedef typename Digraph::template NodeMap<double> FloatPotentialMap;
|
kpeter@814
|
449 |
typedef typename Digraph::template NodeMap<int> LevelMap;
|
kpeter@814
|
450 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap;
|
kpeter@814
|
451 |
typedef typename Digraph::template NodeMap<Node> PredNodeMap;
|
kpeter@814
|
452 |
typedef typename Digraph::template NodeMap<Arc> PredArcMap;
|
kpeter@814
|
453 |
typedef typename ResDigraph::template ArcMap<double> ResShiftCostMap;
|
kpeter@814
|
454 |
FloatPotentialMap pi(_graph);
|
kpeter@814
|
455 |
LevelMap level(_graph);
|
kpeter@814
|
456 |
BoolNodeMap reached(_graph);
|
kpeter@814
|
457 |
BoolNodeMap processed(_graph);
|
kpeter@814
|
458 |
PredNodeMap pred_node(_graph);
|
kpeter@814
|
459 |
PredArcMap pred_arc(_graph);
|
kpeter@814
|
460 |
int node_num = countNodes(_graph);
|
kpeter@814
|
461 |
typedef std::pair<Arc, bool> pair;
|
kpeter@814
|
462 |
std::vector<pair> stack(node_num);
|
kpeter@814
|
463 |
std::vector<Node> proc_vector(node_num);
|
kpeter@814
|
464 |
ResShiftCostMap shift_cost(*_res_graph);
|
kpeter@814
|
465 |
|
kpeter@814
|
466 |
Tolerance<double> tol;
|
kpeter@814
|
467 |
tol.epsilon(1e-6);
|
kpeter@814
|
468 |
|
kpeter@814
|
469 |
Timer t1, t2, t3;
|
kpeter@814
|
470 |
t1.reset();
|
kpeter@814
|
471 |
t2.reset();
|
kpeter@814
|
472 |
t3.reset();
|
kpeter@814
|
473 |
|
kpeter@814
|
474 |
// Initialize epsilon and the node potentials
|
kpeter@814
|
475 |
double epsilon = 0;
|
kpeter@814
|
476 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
kpeter@814
|
477 |
if (_capacity[e] - (*_flow)[e] > 0 && _cost[e] < -epsilon)
|
kpeter@814
|
478 |
epsilon = -_cost[e];
|
kpeter@814
|
479 |
else if ((*_flow)[e] > 0 && _cost[e] > epsilon)
|
kpeter@814
|
480 |
epsilon = _cost[e];
|
kpeter@814
|
481 |
}
|
kpeter@814
|
482 |
for (NodeIt v(_graph); v != INVALID; ++v) {
|
kpeter@814
|
483 |
pi[v] = 0;
|
kpeter@814
|
484 |
}
|
kpeter@814
|
485 |
|
kpeter@814
|
486 |
// Start phases
|
kpeter@814
|
487 |
int limit = int(LIMIT_FACTOR * node_num);
|
kpeter@814
|
488 |
if (limit < MIN_LIMIT) limit = MIN_LIMIT;
|
kpeter@814
|
489 |
int iter = limit;
|
kpeter@814
|
490 |
while (epsilon * node_num >= 1) {
|
kpeter@814
|
491 |
t1.start();
|
kpeter@814
|
492 |
// Find and cancel cycles in the admissible digraph using DFS
|
kpeter@814
|
493 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@814
|
494 |
reached[n] = false;
|
kpeter@814
|
495 |
processed[n] = false;
|
kpeter@814
|
496 |
}
|
kpeter@814
|
497 |
int stack_head = -1;
|
kpeter@814
|
498 |
int proc_head = -1;
|
kpeter@814
|
499 |
|
kpeter@814
|
500 |
for (NodeIt start(_graph); start != INVALID; ++start) {
|
kpeter@814
|
501 |
if (reached[start]) continue;
|
kpeter@814
|
502 |
|
kpeter@814
|
503 |
// New start node
|
kpeter@814
|
504 |
reached[start] = true;
|
kpeter@814
|
505 |
pred_arc[start] = INVALID;
|
kpeter@814
|
506 |
pred_node[start] = INVALID;
|
kpeter@814
|
507 |
|
kpeter@814
|
508 |
// Find the first admissible residual outgoing arc
|
kpeter@814
|
509 |
double p = pi[start];
|
kpeter@814
|
510 |
Arc e;
|
kpeter@814
|
511 |
_graph.firstOut(e, start);
|
kpeter@814
|
512 |
while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 ||
|
kpeter@814
|
513 |
!tol.negative(_cost[e] + p - pi[_graph.target(e)])) )
|
kpeter@814
|
514 |
_graph.nextOut(e);
|
kpeter@814
|
515 |
if (e != INVALID) {
|
kpeter@814
|
516 |
stack[++stack_head] = pair(e, true);
|
kpeter@814
|
517 |
goto next_step_1;
|
kpeter@814
|
518 |
}
|
kpeter@814
|
519 |
_graph.firstIn(e, start);
|
kpeter@814
|
520 |
while ( e != INVALID && ((*_flow)[e] == 0 ||
|
kpeter@814
|
521 |
!tol.negative(-_cost[e] + p - pi[_graph.source(e)])) )
|
kpeter@814
|
522 |
_graph.nextIn(e);
|
kpeter@814
|
523 |
if (e != INVALID) {
|
kpeter@814
|
524 |
stack[++stack_head] = pair(e, false);
|
kpeter@814
|
525 |
goto next_step_1;
|
kpeter@814
|
526 |
}
|
kpeter@814
|
527 |
processed[start] = true;
|
kpeter@814
|
528 |
proc_vector[++proc_head] = start;
|
kpeter@814
|
529 |
continue;
|
kpeter@814
|
530 |
next_step_1:
|
kpeter@814
|
531 |
|
kpeter@814
|
532 |
while (stack_head >= 0) {
|
kpeter@814
|
533 |
Arc se = stack[stack_head].first;
|
kpeter@814
|
534 |
bool sf = stack[stack_head].second;
|
kpeter@814
|
535 |
Node u, v;
|
kpeter@814
|
536 |
if (sf) {
|
kpeter@814
|
537 |
u = _graph.source(se);
|
kpeter@814
|
538 |
v = _graph.target(se);
|
kpeter@814
|
539 |
} else {
|
kpeter@814
|
540 |
u = _graph.target(se);
|
kpeter@814
|
541 |
v = _graph.source(se);
|
kpeter@814
|
542 |
}
|
kpeter@814
|
543 |
|
kpeter@814
|
544 |
if (!reached[v]) {
|
kpeter@814
|
545 |
// A new node is reached
|
kpeter@814
|
546 |
reached[v] = true;
|
kpeter@814
|
547 |
pred_node[v] = u;
|
kpeter@814
|
548 |
pred_arc[v] = se;
|
kpeter@814
|
549 |
// Find the first admissible residual outgoing arc
|
kpeter@814
|
550 |
double p = pi[v];
|
kpeter@814
|
551 |
Arc e;
|
kpeter@814
|
552 |
_graph.firstOut(e, v);
|
kpeter@814
|
553 |
while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 ||
|
kpeter@814
|
554 |
!tol.negative(_cost[e] + p - pi[_graph.target(e)])) )
|
kpeter@814
|
555 |
_graph.nextOut(e);
|
kpeter@814
|
556 |
if (e != INVALID) {
|
kpeter@814
|
557 |
stack[++stack_head] = pair(e, true);
|
kpeter@814
|
558 |
goto next_step_2;
|
kpeter@814
|
559 |
}
|
kpeter@814
|
560 |
_graph.firstIn(e, v);
|
kpeter@814
|
561 |
while ( e != INVALID && ((*_flow)[e] == 0 ||
|
kpeter@814
|
562 |
!tol.negative(-_cost[e] + p - pi[_graph.source(e)])) )
|
kpeter@814
|
563 |
_graph.nextIn(e);
|
kpeter@814
|
564 |
stack[++stack_head] = pair(e, false);
|
kpeter@814
|
565 |
next_step_2: ;
|
kpeter@814
|
566 |
} else {
|
kpeter@814
|
567 |
if (!processed[v]) {
|
kpeter@814
|
568 |
// A cycle is found
|
kpeter@814
|
569 |
Node n, w = u;
|
kpeter@814
|
570 |
Capacity d, delta = sf ? _capacity[se] - (*_flow)[se] :
|
kpeter@814
|
571 |
(*_flow)[se];
|
kpeter@814
|
572 |
for (n = u; n != v; n = pred_node[n]) {
|
kpeter@814
|
573 |
d = _graph.target(pred_arc[n]) == n ?
|
kpeter@814
|
574 |
_capacity[pred_arc[n]] - (*_flow)[pred_arc[n]] :
|
kpeter@814
|
575 |
(*_flow)[pred_arc[n]];
|
kpeter@814
|
576 |
if (d <= delta) {
|
kpeter@814
|
577 |
delta = d;
|
kpeter@814
|
578 |
w = pred_node[n];
|
kpeter@814
|
579 |
}
|
kpeter@814
|
580 |
}
|
kpeter@814
|
581 |
|
kpeter@814
|
582 |
/*
|
kpeter@814
|
583 |
std::cout << "CYCLE FOUND: ";
|
kpeter@814
|
584 |
if (sf)
|
kpeter@814
|
585 |
std::cout << _cost[se] + pi[_graph.source(se)] - pi[_graph.target(se)];
|
kpeter@814
|
586 |
else
|
kpeter@814
|
587 |
std::cout << _graph.id(se) << ":" << -(_cost[se] + pi[_graph.source(se)] - pi[_graph.target(se)]);
|
kpeter@814
|
588 |
for (n = u; n != v; n = pred_node[n]) {
|
kpeter@814
|
589 |
if (_graph.target(pred_arc[n]) == n)
|
kpeter@814
|
590 |
std::cout << " " << _cost[pred_arc[n]] + pi[_graph.source(pred_arc[n])] - pi[_graph.target(pred_arc[n])];
|
kpeter@814
|
591 |
else
|
kpeter@814
|
592 |
std::cout << " " << -(_cost[pred_arc[n]] + pi[_graph.source(pred_arc[n])] - pi[_graph.target(pred_arc[n])]);
|
kpeter@814
|
593 |
}
|
kpeter@814
|
594 |
std::cout << "\n";
|
kpeter@814
|
595 |
*/
|
kpeter@814
|
596 |
// Augment along the cycle
|
kpeter@814
|
597 |
(*_flow)[se] = sf ? (*_flow)[se] + delta :
|
kpeter@814
|
598 |
(*_flow)[se] - delta;
|
kpeter@814
|
599 |
for (n = u; n != v; n = pred_node[n]) {
|
kpeter@814
|
600 |
if (_graph.target(pred_arc[n]) == n)
|
kpeter@814
|
601 |
(*_flow)[pred_arc[n]] += delta;
|
kpeter@814
|
602 |
else
|
kpeter@814
|
603 |
(*_flow)[pred_arc[n]] -= delta;
|
kpeter@814
|
604 |
}
|
kpeter@814
|
605 |
for (n = u; stack_head > 0 && n != w; n = pred_node[n]) {
|
kpeter@814
|
606 |
--stack_head;
|
kpeter@814
|
607 |
reached[n] = false;
|
kpeter@814
|
608 |
}
|
kpeter@814
|
609 |
u = w;
|
kpeter@814
|
610 |
}
|
kpeter@814
|
611 |
v = u;
|
kpeter@814
|
612 |
|
kpeter@814
|
613 |
// Find the next admissible residual outgoing arc
|
kpeter@814
|
614 |
double p = pi[v];
|
kpeter@814
|
615 |
Arc e = stack[stack_head].first;
|
kpeter@814
|
616 |
if (!stack[stack_head].second) {
|
kpeter@814
|
617 |
_graph.nextIn(e);
|
kpeter@814
|
618 |
goto in_arc_3;
|
kpeter@814
|
619 |
}
|
kpeter@814
|
620 |
_graph.nextOut(e);
|
kpeter@814
|
621 |
while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 ||
|
kpeter@814
|
622 |
!tol.negative(_cost[e] + p - pi[_graph.target(e)])) )
|
kpeter@814
|
623 |
_graph.nextOut(e);
|
kpeter@814
|
624 |
if (e != INVALID) {
|
kpeter@814
|
625 |
stack[stack_head] = pair(e, true);
|
kpeter@814
|
626 |
goto next_step_3;
|
kpeter@814
|
627 |
}
|
kpeter@814
|
628 |
_graph.firstIn(e, v);
|
kpeter@814
|
629 |
in_arc_3:
|
kpeter@814
|
630 |
while ( e != INVALID && ((*_flow)[e] == 0 ||
|
kpeter@814
|
631 |
!tol.negative(-_cost[e] + p - pi[_graph.source(e)])) )
|
kpeter@814
|
632 |
_graph.nextIn(e);
|
kpeter@814
|
633 |
stack[stack_head] = pair(e, false);
|
kpeter@814
|
634 |
next_step_3: ;
|
kpeter@814
|
635 |
}
|
kpeter@814
|
636 |
|
kpeter@814
|
637 |
while (stack_head >= 0 && stack[stack_head].first == INVALID) {
|
kpeter@814
|
638 |
processed[v] = true;
|
kpeter@814
|
639 |
proc_vector[++proc_head] = v;
|
kpeter@814
|
640 |
if (--stack_head >= 0) {
|
kpeter@814
|
641 |
v = stack[stack_head].second ?
|
kpeter@814
|
642 |
_graph.source(stack[stack_head].first) :
|
kpeter@814
|
643 |
_graph.target(stack[stack_head].first);
|
kpeter@814
|
644 |
// Find the next admissible residual outgoing arc
|
kpeter@814
|
645 |
double p = pi[v];
|
kpeter@814
|
646 |
Arc e = stack[stack_head].first;
|
kpeter@814
|
647 |
if (!stack[stack_head].second) {
|
kpeter@814
|
648 |
_graph.nextIn(e);
|
kpeter@814
|
649 |
goto in_arc_4;
|
kpeter@814
|
650 |
}
|
kpeter@814
|
651 |
_graph.nextOut(e);
|
kpeter@814
|
652 |
while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 ||
|
kpeter@814
|
653 |
!tol.negative(_cost[e] + p - pi[_graph.target(e)])) )
|
kpeter@814
|
654 |
_graph.nextOut(e);
|
kpeter@814
|
655 |
if (e != INVALID) {
|
kpeter@814
|
656 |
stack[stack_head] = pair(e, true);
|
kpeter@814
|
657 |
goto next_step_4;
|
kpeter@814
|
658 |
}
|
kpeter@814
|
659 |
_graph.firstIn(e, v);
|
kpeter@814
|
660 |
in_arc_4:
|
kpeter@814
|
661 |
while ( e != INVALID && ((*_flow)[e] == 0 ||
|
kpeter@814
|
662 |
!tol.negative(-_cost[e] + p - pi[_graph.source(e)])) )
|
kpeter@814
|
663 |
_graph.nextIn(e);
|
kpeter@814
|
664 |
stack[stack_head] = pair(e, false);
|
kpeter@814
|
665 |
next_step_4: ;
|
kpeter@814
|
666 |
}
|
kpeter@814
|
667 |
}
|
kpeter@814
|
668 |
}
|
kpeter@814
|
669 |
}
|
kpeter@814
|
670 |
t1.stop();
|
kpeter@814
|
671 |
|
kpeter@814
|
672 |
// Tighten potentials and epsilon
|
kpeter@814
|
673 |
if (--iter > 0) {
|
kpeter@814
|
674 |
// Compute levels
|
kpeter@814
|
675 |
t2.start();
|
kpeter@814
|
676 |
for (int i = proc_head; i >= 0; --i) {
|
kpeter@814
|
677 |
Node v = proc_vector[i];
|
kpeter@814
|
678 |
double p = pi[v];
|
kpeter@814
|
679 |
int l = 0;
|
kpeter@814
|
680 |
for (InArcIt e(_graph, v); e != INVALID; ++e) {
|
kpeter@814
|
681 |
Node u = _graph.source(e);
|
kpeter@814
|
682 |
if ( _capacity[e] - (*_flow)[e] > 0 &&
|
kpeter@814
|
683 |
tol.negative(_cost[e] + pi[u] - p) &&
|
kpeter@814
|
684 |
level[u] + 1 > l ) l = level[u] + 1;
|
kpeter@814
|
685 |
}
|
kpeter@814
|
686 |
for (OutArcIt e(_graph, v); e != INVALID; ++e) {
|
kpeter@814
|
687 |
Node u = _graph.target(e);
|
kpeter@814
|
688 |
if ( (*_flow)[e] > 0 &&
|
kpeter@814
|
689 |
tol.negative(-_cost[e] + pi[u] - p) &&
|
kpeter@814
|
690 |
level[u] + 1 > l ) l = level[u] + 1;
|
kpeter@814
|
691 |
}
|
kpeter@814
|
692 |
level[v] = l;
|
kpeter@814
|
693 |
}
|
kpeter@814
|
694 |
|
kpeter@814
|
695 |
// Modify potentials
|
kpeter@814
|
696 |
double p, q = -1;
|
kpeter@814
|
697 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
kpeter@814
|
698 |
Node u = _graph.source(e);
|
kpeter@814
|
699 |
Node v = _graph.target(e);
|
kpeter@814
|
700 |
if (_capacity[e] - (*_flow)[e] > 0 && level[u] - level[v] > 0) {
|
kpeter@814
|
701 |
p = (_cost[e] + pi[u] - pi[v] + epsilon) /
|
kpeter@814
|
702 |
(level[u] - level[v] + 1);
|
kpeter@814
|
703 |
if (q < 0 || p < q) q = p;
|
kpeter@814
|
704 |
}
|
kpeter@814
|
705 |
else if ((*_flow)[e] > 0 && level[v] - level[u] > 0) {
|
kpeter@814
|
706 |
p = (-_cost[e] - pi[u] + pi[v] + epsilon) /
|
kpeter@814
|
707 |
(level[v] - level[u] + 1);
|
kpeter@814
|
708 |
if (q < 0 || p < q) q = p;
|
kpeter@814
|
709 |
}
|
kpeter@814
|
710 |
}
|
kpeter@814
|
711 |
for (NodeIt v(_graph); v != INVALID; ++v) {
|
kpeter@814
|
712 |
pi[v] -= q * level[v];
|
kpeter@814
|
713 |
}
|
kpeter@814
|
714 |
|
kpeter@814
|
715 |
// Modify epsilon
|
kpeter@814
|
716 |
epsilon = 0;
|
kpeter@814
|
717 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
kpeter@814
|
718 |
double curr = _cost[e] + pi[_graph.source(e)]
|
kpeter@814
|
719 |
- pi[_graph.target(e)];
|
kpeter@814
|
720 |
if (_capacity[e] - (*_flow)[e] > 0 && curr < -epsilon)
|
kpeter@814
|
721 |
epsilon = -curr;
|
kpeter@814
|
722 |
else if ((*_flow)[e] > 0 && curr > epsilon)
|
kpeter@814
|
723 |
epsilon = curr;
|
kpeter@814
|
724 |
}
|
kpeter@814
|
725 |
t2.stop();
|
kpeter@814
|
726 |
} else {
|
kpeter@814
|
727 |
// Set epsilon to the minimum cycle mean
|
kpeter@814
|
728 |
t3.start();
|
kpeter@814
|
729 |
|
kpeter@814
|
730 |
/**/
|
kpeter@814
|
731 |
StaticDigraph static_graph;
|
kpeter@814
|
732 |
typename ResDigraph::template NodeMap<typename StaticDigraph::Node> node_ref(*_res_graph);
|
kpeter@814
|
733 |
typename ResDigraph::template ArcMap<typename StaticDigraph::Arc> arc_ref(*_res_graph);
|
kpeter@814
|
734 |
static_graph.build(*_res_graph, node_ref, arc_ref);
|
kpeter@814
|
735 |
typename StaticDigraph::template NodeMap<double> static_pi(static_graph);
|
kpeter@814
|
736 |
typename StaticDigraph::template ArcMap<double> static_cost(static_graph);
|
kpeter@814
|
737 |
|
kpeter@814
|
738 |
for (typename ResDigraph::ArcIt e(*_res_graph); e != INVALID; ++e)
|
kpeter@814
|
739 |
static_cost[arc_ref[e]] = _res_cost[e];
|
kpeter@814
|
740 |
|
kpeter@814
|
741 |
Howard<StaticDigraph, typename StaticDigraph::template ArcMap<double> >
|
kpeter@814
|
742 |
mmc(static_graph, static_cost);
|
kpeter@814
|
743 |
mmc.findMinMean();
|
kpeter@814
|
744 |
epsilon = -mmc.cycleMean();
|
kpeter@814
|
745 |
/**/
|
kpeter@814
|
746 |
|
kpeter@814
|
747 |
/*
|
kpeter@814
|
748 |
Howard<ResDigraph, ResidualCostMap> mmc(*_res_graph, _res_cost);
|
kpeter@814
|
749 |
mmc.findMinMean();
|
kpeter@814
|
750 |
epsilon = -mmc.cycleMean();
|
kpeter@814
|
751 |
*/
|
kpeter@814
|
752 |
|
kpeter@814
|
753 |
// Compute feasible potentials for the current epsilon
|
kpeter@814
|
754 |
for (typename StaticDigraph::ArcIt e(static_graph); e != INVALID; ++e)
|
kpeter@814
|
755 |
static_cost[e] += epsilon;
|
kpeter@814
|
756 |
typename BellmanFord<StaticDigraph, typename StaticDigraph::template ArcMap<double> >::
|
kpeter@814
|
757 |
template SetDistMap<typename StaticDigraph::template NodeMap<double> >::
|
kpeter@814
|
758 |
template SetOperationTraits<BFOperationTraits>::Create
|
kpeter@814
|
759 |
bf(static_graph, static_cost);
|
kpeter@814
|
760 |
bf.distMap(static_pi).init(0);
|
kpeter@814
|
761 |
bf.start();
|
kpeter@814
|
762 |
for (NodeIt n(_graph); n != INVALID; ++n)
|
kpeter@814
|
763 |
pi[n] = static_pi[node_ref[n]];
|
kpeter@814
|
764 |
|
kpeter@814
|
765 |
/*
|
kpeter@814
|
766 |
for (typename ResDigraph::ArcIt e(*_res_graph); e != INVALID; ++e)
|
kpeter@814
|
767 |
shift_cost[e] = _res_cost[e] + epsilon;
|
kpeter@814
|
768 |
typename BellmanFord<ResDigraph, ResShiftCostMap>::
|
kpeter@814
|
769 |
template SetDistMap<FloatPotentialMap>::
|
kpeter@814
|
770 |
template SetOperationTraits<BFOperationTraits>::Create
|
kpeter@814
|
771 |
bf(*_res_graph, shift_cost);
|
kpeter@814
|
772 |
bf.distMap(pi).init(0);
|
kpeter@814
|
773 |
bf.start();
|
kpeter@814
|
774 |
*/
|
kpeter@814
|
775 |
|
kpeter@814
|
776 |
iter = limit;
|
kpeter@814
|
777 |
t3.stop();
|
kpeter@814
|
778 |
}
|
kpeter@814
|
779 |
}
|
kpeter@814
|
780 |
|
kpeter@814
|
781 |
// std::cout << t1.realTime() << " " << t2.realTime() << " " << t3.realTime() << "\n";
|
kpeter@814
|
782 |
|
kpeter@814
|
783 |
// Handle non-zero lower bounds
|
kpeter@814
|
784 |
if (_lower) {
|
kpeter@814
|
785 |
for (ArcIt e(_graph); e != INVALID; ++e)
|
kpeter@814
|
786 |
(*_flow)[e] += (*_lower)[e];
|
kpeter@814
|
787 |
}
|
kpeter@814
|
788 |
return true;
|
kpeter@814
|
789 |
}
|
kpeter@814
|
790 |
|
kpeter@814
|
791 |
}; //class CancelAndTighten
|
kpeter@814
|
792 |
|
kpeter@814
|
793 |
///@}
|
kpeter@814
|
794 |
|
kpeter@814
|
795 |
} //namespace lemon
|
kpeter@814
|
796 |
|
kpeter@814
|
797 |
#endif //LEMON_CANCEL_AND_TIGHTEN_H
|