alpar@956
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@880
|
2 |
*
|
alpar@956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@880
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
kpeter@880
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@880
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@880
|
8 |
*
|
kpeter@880
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@880
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@880
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@880
|
12 |
*
|
kpeter@880
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@880
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@880
|
15 |
* purpose.
|
kpeter@880
|
16 |
*
|
kpeter@880
|
17 |
*/
|
kpeter@880
|
18 |
|
kpeter@880
|
19 |
#ifndef LEMON_CYCLE_CANCELING_H
|
kpeter@880
|
20 |
#define LEMON_CYCLE_CANCELING_H
|
kpeter@880
|
21 |
|
kpeter@881
|
22 |
/// \ingroup min_cost_flow_algs
|
kpeter@880
|
23 |
/// \file
|
kpeter@881
|
24 |
/// \brief Cycle-canceling algorithms for finding a minimum cost flow.
|
kpeter@880
|
25 |
|
kpeter@880
|
26 |
#include <vector>
|
kpeter@881
|
27 |
#include <limits>
|
kpeter@881
|
28 |
|
kpeter@881
|
29 |
#include <lemon/core.h>
|
kpeter@881
|
30 |
#include <lemon/maps.h>
|
kpeter@881
|
31 |
#include <lemon/path.h>
|
kpeter@881
|
32 |
#include <lemon/math.h>
|
kpeter@881
|
33 |
#include <lemon/static_graph.h>
|
kpeter@880
|
34 |
#include <lemon/adaptors.h>
|
kpeter@880
|
35 |
#include <lemon/circulation.h>
|
kpeter@880
|
36 |
#include <lemon/bellman_ford.h>
|
kpeter@942
|
37 |
#include <lemon/howard_mmc.h>
|
kpeter@880
|
38 |
|
kpeter@880
|
39 |
namespace lemon {
|
kpeter@880
|
40 |
|
kpeter@881
|
41 |
/// \addtogroup min_cost_flow_algs
|
kpeter@880
|
42 |
/// @{
|
kpeter@880
|
43 |
|
kpeter@881
|
44 |
/// \brief Implementation of cycle-canceling algorithms for
|
kpeter@881
|
45 |
/// finding a \ref min_cost_flow "minimum cost flow".
|
kpeter@880
|
46 |
///
|
kpeter@881
|
47 |
/// \ref CycleCanceling implements three different cycle-canceling
|
kpeter@882
|
48 |
/// algorithms for finding a \ref min_cost_flow "minimum cost flow"
|
kpeter@882
|
49 |
/// \ref amo93networkflows, \ref klein67primal,
|
kpeter@882
|
50 |
/// \ref goldberg89cyclecanceling.
|
kpeter@881
|
51 |
/// The most efficent one (both theoretically and practically)
|
kpeter@881
|
52 |
/// is the \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" algorithm,
|
kpeter@881
|
53 |
/// thus it is the default method.
|
kpeter@881
|
54 |
/// It is strongly polynomial, but in practice, it is typically much
|
kpeter@881
|
55 |
/// slower than the scaling algorithms and NetworkSimplex.
|
kpeter@880
|
56 |
///
|
kpeter@881
|
57 |
/// Most of the parameters of the problem (except for the digraph)
|
kpeter@881
|
58 |
/// can be given using separate functions, and the algorithm can be
|
kpeter@881
|
59 |
/// executed using the \ref run() function. If some parameters are not
|
kpeter@881
|
60 |
/// specified, then default values will be used.
|
kpeter@880
|
61 |
///
|
kpeter@881
|
62 |
/// \tparam GR The digraph type the algorithm runs on.
|
kpeter@881
|
63 |
/// \tparam V The number type used for flow amounts, capacity bounds
|
kpeter@881
|
64 |
/// and supply values in the algorithm. By default, it is \c int.
|
kpeter@881
|
65 |
/// \tparam C The number type used for costs and potentials in the
|
kpeter@881
|
66 |
/// algorithm. By default, it is the same as \c V.
|
kpeter@880
|
67 |
///
|
kpeter@881
|
68 |
/// \warning Both number types must be signed and all input data must
|
kpeter@881
|
69 |
/// be integer.
|
kpeter@881
|
70 |
/// \warning This algorithm does not support negative costs for such
|
kpeter@881
|
71 |
/// arcs that have infinite upper bound.
|
kpeter@880
|
72 |
///
|
kpeter@881
|
73 |
/// \note For more information about the three available methods,
|
kpeter@881
|
74 |
/// see \ref Method.
|
kpeter@881
|
75 |
#ifdef DOXYGEN
|
kpeter@881
|
76 |
template <typename GR, typename V, typename C>
|
kpeter@881
|
77 |
#else
|
kpeter@881
|
78 |
template <typename GR, typename V = int, typename C = V>
|
kpeter@881
|
79 |
#endif
|
kpeter@880
|
80 |
class CycleCanceling
|
kpeter@880
|
81 |
{
|
kpeter@881
|
82 |
public:
|
kpeter@880
|
83 |
|
kpeter@881
|
84 |
/// The type of the digraph
|
kpeter@881
|
85 |
typedef GR Digraph;
|
kpeter@881
|
86 |
/// The type of the flow amounts, capacity bounds and supply values
|
kpeter@881
|
87 |
typedef V Value;
|
kpeter@881
|
88 |
/// The type of the arc costs
|
kpeter@881
|
89 |
typedef C Cost;
|
kpeter@880
|
90 |
|
kpeter@880
|
91 |
public:
|
kpeter@880
|
92 |
|
kpeter@881
|
93 |
/// \brief Problem type constants for the \c run() function.
|
kpeter@881
|
94 |
///
|
kpeter@881
|
95 |
/// Enum type containing the problem type constants that can be
|
kpeter@881
|
96 |
/// returned by the \ref run() function of the algorithm.
|
kpeter@881
|
97 |
enum ProblemType {
|
kpeter@881
|
98 |
/// The problem has no feasible solution (flow).
|
kpeter@881
|
99 |
INFEASIBLE,
|
kpeter@881
|
100 |
/// The problem has optimal solution (i.e. it is feasible and
|
kpeter@881
|
101 |
/// bounded), and the algorithm has found optimal flow and node
|
kpeter@881
|
102 |
/// potentials (primal and dual solutions).
|
kpeter@881
|
103 |
OPTIMAL,
|
kpeter@881
|
104 |
/// The digraph contains an arc of negative cost and infinite
|
kpeter@881
|
105 |
/// upper bound. It means that the objective function is unbounded
|
kpeter@881
|
106 |
/// on that arc, however, note that it could actually be bounded
|
kpeter@881
|
107 |
/// over the feasible flows, but this algroithm cannot handle
|
kpeter@881
|
108 |
/// these cases.
|
kpeter@881
|
109 |
UNBOUNDED
|
kpeter@881
|
110 |
};
|
kpeter@881
|
111 |
|
kpeter@881
|
112 |
/// \brief Constants for selecting the used method.
|
kpeter@881
|
113 |
///
|
kpeter@881
|
114 |
/// Enum type containing constants for selecting the used method
|
kpeter@881
|
115 |
/// for the \ref run() function.
|
kpeter@881
|
116 |
///
|
kpeter@881
|
117 |
/// \ref CycleCanceling provides three different cycle-canceling
|
kpeter@881
|
118 |
/// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel and Tighten"
|
kpeter@881
|
119 |
/// is used, which proved to be the most efficient and the most robust
|
kpeter@881
|
120 |
/// on various test inputs.
|
kpeter@881
|
121 |
/// However, the other methods can be selected using the \ref run()
|
kpeter@881
|
122 |
/// function with the proper parameter.
|
kpeter@881
|
123 |
enum Method {
|
kpeter@881
|
124 |
/// A simple cycle-canceling method, which uses the
|
kpeter@881
|
125 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration
|
kpeter@881
|
126 |
/// number for detecting negative cycles in the residual network.
|
kpeter@881
|
127 |
SIMPLE_CYCLE_CANCELING,
|
kpeter@881
|
128 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a
|
kpeter@882
|
129 |
/// well-known strongly polynomial method
|
kpeter@882
|
130 |
/// \ref goldberg89cyclecanceling. It improves along a
|
kpeter@881
|
131 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration.
|
kpeter@881
|
132 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)).
|
kpeter@881
|
133 |
MINIMUM_MEAN_CYCLE_CANCELING,
|
kpeter@881
|
134 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an
|
kpeter@882
|
135 |
/// improved version of the previous method
|
kpeter@882
|
136 |
/// \ref goldberg89cyclecanceling.
|
kpeter@881
|
137 |
/// It is faster both in theory and in practice, its running time
|
kpeter@881
|
138 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)).
|
kpeter@881
|
139 |
CANCEL_AND_TIGHTEN
|
kpeter@881
|
140 |
};
|
kpeter@880
|
141 |
|
kpeter@880
|
142 |
private:
|
kpeter@880
|
143 |
|
kpeter@881
|
144 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
|
alpar@956
|
145 |
|
kpeter@881
|
146 |
typedef std::vector<int> IntVector;
|
kpeter@881
|
147 |
typedef std::vector<double> DoubleVector;
|
kpeter@881
|
148 |
typedef std::vector<Value> ValueVector;
|
kpeter@881
|
149 |
typedef std::vector<Cost> CostVector;
|
kpeter@910
|
150 |
typedef std::vector<char> BoolVector;
|
kpeter@910
|
151 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons
|
kpeter@880
|
152 |
|
kpeter@881
|
153 |
private:
|
alpar@956
|
154 |
|
kpeter@881
|
155 |
template <typename KT, typename VT>
|
kpeter@886
|
156 |
class StaticVectorMap {
|
kpeter@880
|
157 |
public:
|
kpeter@881
|
158 |
typedef KT Key;
|
kpeter@881
|
159 |
typedef VT Value;
|
alpar@956
|
160 |
|
kpeter@886
|
161 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
alpar@956
|
162 |
|
kpeter@881
|
163 |
const Value& operator[](const Key& key) const {
|
kpeter@881
|
164 |
return _v[StaticDigraph::id(key)];
|
kpeter@880
|
165 |
}
|
kpeter@880
|
166 |
|
kpeter@881
|
167 |
Value& operator[](const Key& key) {
|
kpeter@881
|
168 |
return _v[StaticDigraph::id(key)];
|
kpeter@881
|
169 |
}
|
alpar@956
|
170 |
|
kpeter@881
|
171 |
void set(const Key& key, const Value& val) {
|
kpeter@881
|
172 |
_v[StaticDigraph::id(key)] = val;
|
kpeter@881
|
173 |
}
|
kpeter@881
|
174 |
|
kpeter@881
|
175 |
private:
|
kpeter@881
|
176 |
std::vector<Value>& _v;
|
kpeter@881
|
177 |
};
|
kpeter@881
|
178 |
|
kpeter@886
|
179 |
typedef StaticVectorMap<StaticDigraph::Node, Cost> CostNodeMap;
|
kpeter@886
|
180 |
typedef StaticVectorMap<StaticDigraph::Arc, Cost> CostArcMap;
|
kpeter@880
|
181 |
|
kpeter@880
|
182 |
private:
|
kpeter@880
|
183 |
|
kpeter@880
|
184 |
|
kpeter@881
|
185 |
// Data related to the underlying digraph
|
kpeter@881
|
186 |
const GR &_graph;
|
kpeter@881
|
187 |
int _node_num;
|
kpeter@881
|
188 |
int _arc_num;
|
kpeter@881
|
189 |
int _res_node_num;
|
kpeter@881
|
190 |
int _res_arc_num;
|
kpeter@881
|
191 |
int _root;
|
kpeter@880
|
192 |
|
kpeter@881
|
193 |
// Parameters of the problem
|
kpeter@881
|
194 |
bool _have_lower;
|
kpeter@881
|
195 |
Value _sum_supply;
|
kpeter@880
|
196 |
|
kpeter@881
|
197 |
// Data structures for storing the digraph
|
kpeter@881
|
198 |
IntNodeMap _node_id;
|
kpeter@881
|
199 |
IntArcMap _arc_idf;
|
kpeter@881
|
200 |
IntArcMap _arc_idb;
|
kpeter@881
|
201 |
IntVector _first_out;
|
kpeter@910
|
202 |
BoolVector _forward;
|
kpeter@881
|
203 |
IntVector _source;
|
kpeter@881
|
204 |
IntVector _target;
|
kpeter@881
|
205 |
IntVector _reverse;
|
kpeter@880
|
206 |
|
kpeter@881
|
207 |
// Node and arc data
|
kpeter@881
|
208 |
ValueVector _lower;
|
kpeter@881
|
209 |
ValueVector _upper;
|
kpeter@881
|
210 |
CostVector _cost;
|
kpeter@881
|
211 |
ValueVector _supply;
|
kpeter@881
|
212 |
|
kpeter@881
|
213 |
ValueVector _res_cap;
|
kpeter@881
|
214 |
CostVector _pi;
|
kpeter@881
|
215 |
|
kpeter@881
|
216 |
// Data for a StaticDigraph structure
|
kpeter@881
|
217 |
typedef std::pair<int, int> IntPair;
|
kpeter@881
|
218 |
StaticDigraph _sgr;
|
kpeter@881
|
219 |
std::vector<IntPair> _arc_vec;
|
kpeter@881
|
220 |
std::vector<Cost> _cost_vec;
|
kpeter@881
|
221 |
IntVector _id_vec;
|
kpeter@881
|
222 |
CostArcMap _cost_map;
|
kpeter@881
|
223 |
CostNodeMap _pi_map;
|
alpar@956
|
224 |
|
kpeter@881
|
225 |
public:
|
alpar@956
|
226 |
|
kpeter@881
|
227 |
/// \brief Constant for infinite upper bounds (capacities).
|
kpeter@881
|
228 |
///
|
kpeter@881
|
229 |
/// Constant for infinite upper bounds (capacities).
|
kpeter@881
|
230 |
/// It is \c std::numeric_limits<Value>::infinity() if available,
|
kpeter@881
|
231 |
/// \c std::numeric_limits<Value>::max() otherwise.
|
kpeter@881
|
232 |
const Value INF;
|
kpeter@880
|
233 |
|
kpeter@880
|
234 |
public:
|
kpeter@880
|
235 |
|
kpeter@881
|
236 |
/// \brief Constructor.
|
kpeter@880
|
237 |
///
|
kpeter@881
|
238 |
/// The constructor of the class.
|
kpeter@880
|
239 |
///
|
kpeter@881
|
240 |
/// \param graph The digraph the algorithm runs on.
|
kpeter@881
|
241 |
CycleCanceling(const GR& graph) :
|
kpeter@881
|
242 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
|
kpeter@881
|
243 |
_cost_map(_cost_vec), _pi_map(_pi),
|
kpeter@881
|
244 |
INF(std::numeric_limits<Value>::has_infinity ?
|
kpeter@881
|
245 |
std::numeric_limits<Value>::infinity() :
|
kpeter@881
|
246 |
std::numeric_limits<Value>::max())
|
kpeter@880
|
247 |
{
|
kpeter@881
|
248 |
// Check the number types
|
kpeter@881
|
249 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
|
kpeter@881
|
250 |
"The flow type of CycleCanceling must be signed");
|
kpeter@881
|
251 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
|
kpeter@881
|
252 |
"The cost type of CycleCanceling must be signed");
|
kpeter@881
|
253 |
|
kpeter@898
|
254 |
// Reset data structures
|
kpeter@881
|
255 |
reset();
|
kpeter@880
|
256 |
}
|
kpeter@880
|
257 |
|
kpeter@881
|
258 |
/// \name Parameters
|
kpeter@881
|
259 |
/// The parameters of the algorithm can be specified using these
|
kpeter@881
|
260 |
/// functions.
|
kpeter@881
|
261 |
|
kpeter@881
|
262 |
/// @{
|
kpeter@881
|
263 |
|
kpeter@881
|
264 |
/// \brief Set the lower bounds on the arcs.
|
kpeter@880
|
265 |
///
|
kpeter@881
|
266 |
/// This function sets the lower bounds on the arcs.
|
kpeter@881
|
267 |
/// If it is not used before calling \ref run(), the lower bounds
|
kpeter@881
|
268 |
/// will be set to zero on all arcs.
|
kpeter@880
|
269 |
///
|
kpeter@881
|
270 |
/// \param map An arc map storing the lower bounds.
|
kpeter@881
|
271 |
/// Its \c Value type must be convertible to the \c Value type
|
kpeter@881
|
272 |
/// of the algorithm.
|
kpeter@881
|
273 |
///
|
kpeter@881
|
274 |
/// \return <tt>(*this)</tt>
|
kpeter@881
|
275 |
template <typename LowerMap>
|
kpeter@881
|
276 |
CycleCanceling& lowerMap(const LowerMap& map) {
|
kpeter@881
|
277 |
_have_lower = true;
|
kpeter@881
|
278 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
279 |
_lower[_arc_idf[a]] = map[a];
|
kpeter@881
|
280 |
_lower[_arc_idb[a]] = map[a];
|
kpeter@880
|
281 |
}
|
kpeter@880
|
282 |
return *this;
|
kpeter@880
|
283 |
}
|
kpeter@880
|
284 |
|
kpeter@881
|
285 |
/// \brief Set the upper bounds (capacities) on the arcs.
|
kpeter@880
|
286 |
///
|
kpeter@881
|
287 |
/// This function sets the upper bounds (capacities) on the arcs.
|
kpeter@881
|
288 |
/// If it is not used before calling \ref run(), the upper bounds
|
kpeter@881
|
289 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be
|
kpeter@881
|
290 |
/// unbounded from above).
|
kpeter@880
|
291 |
///
|
kpeter@881
|
292 |
/// \param map An arc map storing the upper bounds.
|
kpeter@881
|
293 |
/// Its \c Value type must be convertible to the \c Value type
|
kpeter@881
|
294 |
/// of the algorithm.
|
kpeter@881
|
295 |
///
|
kpeter@881
|
296 |
/// \return <tt>(*this)</tt>
|
kpeter@881
|
297 |
template<typename UpperMap>
|
kpeter@881
|
298 |
CycleCanceling& upperMap(const UpperMap& map) {
|
kpeter@881
|
299 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
300 |
_upper[_arc_idf[a]] = map[a];
|
kpeter@880
|
301 |
}
|
kpeter@880
|
302 |
return *this;
|
kpeter@880
|
303 |
}
|
kpeter@880
|
304 |
|
kpeter@881
|
305 |
/// \brief Set the costs of the arcs.
|
kpeter@881
|
306 |
///
|
kpeter@881
|
307 |
/// This function sets the costs of the arcs.
|
kpeter@881
|
308 |
/// If it is not used before calling \ref run(), the costs
|
kpeter@881
|
309 |
/// will be set to \c 1 on all arcs.
|
kpeter@881
|
310 |
///
|
kpeter@881
|
311 |
/// \param map An arc map storing the costs.
|
kpeter@881
|
312 |
/// Its \c Value type must be convertible to the \c Cost type
|
kpeter@881
|
313 |
/// of the algorithm.
|
kpeter@881
|
314 |
///
|
kpeter@881
|
315 |
/// \return <tt>(*this)</tt>
|
kpeter@881
|
316 |
template<typename CostMap>
|
kpeter@881
|
317 |
CycleCanceling& costMap(const CostMap& map) {
|
kpeter@881
|
318 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
319 |
_cost[_arc_idf[a]] = map[a];
|
kpeter@881
|
320 |
_cost[_arc_idb[a]] = -map[a];
|
kpeter@881
|
321 |
}
|
kpeter@881
|
322 |
return *this;
|
kpeter@881
|
323 |
}
|
kpeter@881
|
324 |
|
kpeter@881
|
325 |
/// \brief Set the supply values of the nodes.
|
kpeter@881
|
326 |
///
|
kpeter@881
|
327 |
/// This function sets the supply values of the nodes.
|
kpeter@881
|
328 |
/// If neither this function nor \ref stSupply() is used before
|
kpeter@881
|
329 |
/// calling \ref run(), the supply of each node will be set to zero.
|
kpeter@881
|
330 |
///
|
kpeter@881
|
331 |
/// \param map A node map storing the supply values.
|
kpeter@881
|
332 |
/// Its \c Value type must be convertible to the \c Value type
|
kpeter@881
|
333 |
/// of the algorithm.
|
kpeter@881
|
334 |
///
|
kpeter@881
|
335 |
/// \return <tt>(*this)</tt>
|
kpeter@881
|
336 |
template<typename SupplyMap>
|
kpeter@881
|
337 |
CycleCanceling& supplyMap(const SupplyMap& map) {
|
kpeter@881
|
338 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@881
|
339 |
_supply[_node_id[n]] = map[n];
|
kpeter@881
|
340 |
}
|
kpeter@881
|
341 |
return *this;
|
kpeter@881
|
342 |
}
|
kpeter@881
|
343 |
|
kpeter@881
|
344 |
/// \brief Set single source and target nodes and a supply value.
|
kpeter@881
|
345 |
///
|
kpeter@881
|
346 |
/// This function sets a single source node and a single target node
|
kpeter@881
|
347 |
/// and the required flow value.
|
kpeter@881
|
348 |
/// If neither this function nor \ref supplyMap() is used before
|
kpeter@881
|
349 |
/// calling \ref run(), the supply of each node will be set to zero.
|
kpeter@881
|
350 |
///
|
kpeter@881
|
351 |
/// Using this function has the same effect as using \ref supplyMap()
|
kpeter@881
|
352 |
/// with such a map in which \c k is assigned to \c s, \c -k is
|
kpeter@881
|
353 |
/// assigned to \c t and all other nodes have zero supply value.
|
kpeter@881
|
354 |
///
|
kpeter@881
|
355 |
/// \param s The source node.
|
kpeter@881
|
356 |
/// \param t The target node.
|
kpeter@881
|
357 |
/// \param k The required amount of flow from node \c s to node \c t
|
kpeter@881
|
358 |
/// (i.e. the supply of \c s and the demand of \c t).
|
kpeter@881
|
359 |
///
|
kpeter@881
|
360 |
/// \return <tt>(*this)</tt>
|
kpeter@881
|
361 |
CycleCanceling& stSupply(const Node& s, const Node& t, Value k) {
|
kpeter@881
|
362 |
for (int i = 0; i != _res_node_num; ++i) {
|
kpeter@881
|
363 |
_supply[i] = 0;
|
kpeter@881
|
364 |
}
|
kpeter@881
|
365 |
_supply[_node_id[s]] = k;
|
kpeter@881
|
366 |
_supply[_node_id[t]] = -k;
|
kpeter@881
|
367 |
return *this;
|
kpeter@881
|
368 |
}
|
alpar@956
|
369 |
|
kpeter@881
|
370 |
/// @}
|
kpeter@881
|
371 |
|
kpeter@880
|
372 |
/// \name Execution control
|
kpeter@881
|
373 |
/// The algorithm can be executed using \ref run().
|
kpeter@880
|
374 |
|
kpeter@880
|
375 |
/// @{
|
kpeter@880
|
376 |
|
kpeter@880
|
377 |
/// \brief Run the algorithm.
|
kpeter@880
|
378 |
///
|
kpeter@881
|
379 |
/// This function runs the algorithm.
|
kpeter@881
|
380 |
/// The paramters can be specified using functions \ref lowerMap(),
|
kpeter@881
|
381 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
|
kpeter@881
|
382 |
/// For example,
|
kpeter@881
|
383 |
/// \code
|
kpeter@881
|
384 |
/// CycleCanceling<ListDigraph> cc(graph);
|
kpeter@881
|
385 |
/// cc.lowerMap(lower).upperMap(upper).costMap(cost)
|
kpeter@881
|
386 |
/// .supplyMap(sup).run();
|
kpeter@881
|
387 |
/// \endcode
|
kpeter@880
|
388 |
///
|
kpeter@898
|
389 |
/// This function can be called more than once. All the given parameters
|
kpeter@898
|
390 |
/// are kept for the next call, unless \ref resetParams() or \ref reset()
|
kpeter@898
|
391 |
/// is used, thus only the modified parameters have to be set again.
|
kpeter@898
|
392 |
/// If the underlying digraph was also modified after the construction
|
kpeter@898
|
393 |
/// of the class (or the last \ref reset() call), then the \ref reset()
|
kpeter@898
|
394 |
/// function must be called.
|
kpeter@880
|
395 |
///
|
kpeter@881
|
396 |
/// \param method The cycle-canceling method that will be used.
|
kpeter@881
|
397 |
/// For more information, see \ref Method.
|
kpeter@881
|
398 |
///
|
kpeter@881
|
399 |
/// \return \c INFEASIBLE if no feasible flow exists,
|
kpeter@881
|
400 |
/// \n \c OPTIMAL if the problem has optimal solution
|
kpeter@881
|
401 |
/// (i.e. it is feasible and bounded), and the algorithm has found
|
kpeter@881
|
402 |
/// optimal flow and node potentials (primal and dual solutions),
|
kpeter@881
|
403 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost
|
kpeter@881
|
404 |
/// and infinite upper bound. It means that the objective function
|
kpeter@881
|
405 |
/// is unbounded on that arc, however, note that it could actually be
|
kpeter@881
|
406 |
/// bounded over the feasible flows, but this algroithm cannot handle
|
kpeter@881
|
407 |
/// these cases.
|
kpeter@881
|
408 |
///
|
kpeter@881
|
409 |
/// \see ProblemType, Method
|
kpeter@898
|
410 |
/// \see resetParams(), reset()
|
kpeter@881
|
411 |
ProblemType run(Method method = CANCEL_AND_TIGHTEN) {
|
kpeter@881
|
412 |
ProblemType pt = init();
|
kpeter@881
|
413 |
if (pt != OPTIMAL) return pt;
|
kpeter@881
|
414 |
start(method);
|
kpeter@881
|
415 |
return OPTIMAL;
|
kpeter@881
|
416 |
}
|
kpeter@881
|
417 |
|
kpeter@881
|
418 |
/// \brief Reset all the parameters that have been given before.
|
kpeter@881
|
419 |
///
|
kpeter@881
|
420 |
/// This function resets all the paramaters that have been given
|
kpeter@881
|
421 |
/// before using functions \ref lowerMap(), \ref upperMap(),
|
kpeter@881
|
422 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply().
|
kpeter@881
|
423 |
///
|
kpeter@898
|
424 |
/// It is useful for multiple \ref run() calls. Basically, all the given
|
kpeter@898
|
425 |
/// parameters are kept for the next \ref run() call, unless
|
kpeter@898
|
426 |
/// \ref resetParams() or \ref reset() is used.
|
kpeter@898
|
427 |
/// If the underlying digraph was also modified after the construction
|
kpeter@898
|
428 |
/// of the class or the last \ref reset() call, then the \ref reset()
|
kpeter@898
|
429 |
/// function must be used, otherwise \ref resetParams() is sufficient.
|
kpeter@881
|
430 |
///
|
kpeter@881
|
431 |
/// For example,
|
kpeter@881
|
432 |
/// \code
|
kpeter@881
|
433 |
/// CycleCanceling<ListDigraph> cs(graph);
|
kpeter@881
|
434 |
///
|
kpeter@881
|
435 |
/// // First run
|
kpeter@881
|
436 |
/// cc.lowerMap(lower).upperMap(upper).costMap(cost)
|
kpeter@881
|
437 |
/// .supplyMap(sup).run();
|
kpeter@881
|
438 |
///
|
kpeter@898
|
439 |
/// // Run again with modified cost map (resetParams() is not called,
|
kpeter@881
|
440 |
/// // so only the cost map have to be set again)
|
kpeter@881
|
441 |
/// cost[e] += 100;
|
kpeter@881
|
442 |
/// cc.costMap(cost).run();
|
kpeter@881
|
443 |
///
|
kpeter@898
|
444 |
/// // Run again from scratch using resetParams()
|
kpeter@881
|
445 |
/// // (the lower bounds will be set to zero on all arcs)
|
kpeter@898
|
446 |
/// cc.resetParams();
|
kpeter@881
|
447 |
/// cc.upperMap(capacity).costMap(cost)
|
kpeter@881
|
448 |
/// .supplyMap(sup).run();
|
kpeter@881
|
449 |
/// \endcode
|
kpeter@881
|
450 |
///
|
kpeter@881
|
451 |
/// \return <tt>(*this)</tt>
|
kpeter@898
|
452 |
///
|
kpeter@898
|
453 |
/// \see reset(), run()
|
kpeter@898
|
454 |
CycleCanceling& resetParams() {
|
kpeter@881
|
455 |
for (int i = 0; i != _res_node_num; ++i) {
|
kpeter@881
|
456 |
_supply[i] = 0;
|
kpeter@881
|
457 |
}
|
kpeter@881
|
458 |
int limit = _first_out[_root];
|
kpeter@881
|
459 |
for (int j = 0; j != limit; ++j) {
|
kpeter@881
|
460 |
_lower[j] = 0;
|
kpeter@881
|
461 |
_upper[j] = INF;
|
kpeter@881
|
462 |
_cost[j] = _forward[j] ? 1 : -1;
|
kpeter@881
|
463 |
}
|
kpeter@881
|
464 |
for (int j = limit; j != _res_arc_num; ++j) {
|
kpeter@881
|
465 |
_lower[j] = 0;
|
kpeter@881
|
466 |
_upper[j] = INF;
|
kpeter@881
|
467 |
_cost[j] = 0;
|
kpeter@881
|
468 |
_cost[_reverse[j]] = 0;
|
alpar@956
|
469 |
}
|
kpeter@881
|
470 |
_have_lower = false;
|
kpeter@881
|
471 |
return *this;
|
kpeter@880
|
472 |
}
|
kpeter@880
|
473 |
|
kpeter@898
|
474 |
/// \brief Reset the internal data structures and all the parameters
|
kpeter@898
|
475 |
/// that have been given before.
|
kpeter@898
|
476 |
///
|
kpeter@898
|
477 |
/// This function resets the internal data structures and all the
|
kpeter@898
|
478 |
/// paramaters that have been given before using functions \ref lowerMap(),
|
kpeter@898
|
479 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
|
kpeter@898
|
480 |
///
|
kpeter@898
|
481 |
/// It is useful for multiple \ref run() calls. Basically, all the given
|
kpeter@898
|
482 |
/// parameters are kept for the next \ref run() call, unless
|
kpeter@898
|
483 |
/// \ref resetParams() or \ref reset() is used.
|
kpeter@898
|
484 |
/// If the underlying digraph was also modified after the construction
|
kpeter@898
|
485 |
/// of the class or the last \ref reset() call, then the \ref reset()
|
kpeter@898
|
486 |
/// function must be used, otherwise \ref resetParams() is sufficient.
|
kpeter@898
|
487 |
///
|
kpeter@898
|
488 |
/// See \ref resetParams() for examples.
|
kpeter@898
|
489 |
///
|
kpeter@898
|
490 |
/// \return <tt>(*this)</tt>
|
kpeter@898
|
491 |
///
|
kpeter@898
|
492 |
/// \see resetParams(), run()
|
kpeter@898
|
493 |
CycleCanceling& reset() {
|
kpeter@898
|
494 |
// Resize vectors
|
kpeter@898
|
495 |
_node_num = countNodes(_graph);
|
kpeter@898
|
496 |
_arc_num = countArcs(_graph);
|
kpeter@898
|
497 |
_res_node_num = _node_num + 1;
|
kpeter@898
|
498 |
_res_arc_num = 2 * (_arc_num + _node_num);
|
kpeter@898
|
499 |
_root = _node_num;
|
kpeter@898
|
500 |
|
kpeter@898
|
501 |
_first_out.resize(_res_node_num + 1);
|
kpeter@898
|
502 |
_forward.resize(_res_arc_num);
|
kpeter@898
|
503 |
_source.resize(_res_arc_num);
|
kpeter@898
|
504 |
_target.resize(_res_arc_num);
|
kpeter@898
|
505 |
_reverse.resize(_res_arc_num);
|
kpeter@898
|
506 |
|
kpeter@898
|
507 |
_lower.resize(_res_arc_num);
|
kpeter@898
|
508 |
_upper.resize(_res_arc_num);
|
kpeter@898
|
509 |
_cost.resize(_res_arc_num);
|
kpeter@898
|
510 |
_supply.resize(_res_node_num);
|
alpar@956
|
511 |
|
kpeter@898
|
512 |
_res_cap.resize(_res_arc_num);
|
kpeter@898
|
513 |
_pi.resize(_res_node_num);
|
kpeter@898
|
514 |
|
kpeter@898
|
515 |
_arc_vec.reserve(_res_arc_num);
|
kpeter@898
|
516 |
_cost_vec.reserve(_res_arc_num);
|
kpeter@898
|
517 |
_id_vec.reserve(_res_arc_num);
|
kpeter@898
|
518 |
|
kpeter@898
|
519 |
// Copy the graph
|
kpeter@898
|
520 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num;
|
kpeter@898
|
521 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
kpeter@898
|
522 |
_node_id[n] = i;
|
kpeter@898
|
523 |
}
|
kpeter@898
|
524 |
i = 0;
|
kpeter@898
|
525 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
kpeter@898
|
526 |
_first_out[i] = j;
|
kpeter@898
|
527 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
kpeter@898
|
528 |
_arc_idf[a] = j;
|
kpeter@898
|
529 |
_forward[j] = true;
|
kpeter@898
|
530 |
_source[j] = i;
|
kpeter@898
|
531 |
_target[j] = _node_id[_graph.runningNode(a)];
|
kpeter@898
|
532 |
}
|
kpeter@898
|
533 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
kpeter@898
|
534 |
_arc_idb[a] = j;
|
kpeter@898
|
535 |
_forward[j] = false;
|
kpeter@898
|
536 |
_source[j] = i;
|
kpeter@898
|
537 |
_target[j] = _node_id[_graph.runningNode(a)];
|
kpeter@898
|
538 |
}
|
kpeter@898
|
539 |
_forward[j] = false;
|
kpeter@898
|
540 |
_source[j] = i;
|
kpeter@898
|
541 |
_target[j] = _root;
|
kpeter@898
|
542 |
_reverse[j] = k;
|
kpeter@898
|
543 |
_forward[k] = true;
|
kpeter@898
|
544 |
_source[k] = _root;
|
kpeter@898
|
545 |
_target[k] = i;
|
kpeter@898
|
546 |
_reverse[k] = j;
|
kpeter@898
|
547 |
++j; ++k;
|
kpeter@898
|
548 |
}
|
kpeter@898
|
549 |
_first_out[i] = j;
|
kpeter@898
|
550 |
_first_out[_res_node_num] = k;
|
kpeter@898
|
551 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@898
|
552 |
int fi = _arc_idf[a];
|
kpeter@898
|
553 |
int bi = _arc_idb[a];
|
kpeter@898
|
554 |
_reverse[fi] = bi;
|
kpeter@898
|
555 |
_reverse[bi] = fi;
|
kpeter@898
|
556 |
}
|
alpar@956
|
557 |
|
kpeter@898
|
558 |
// Reset parameters
|
kpeter@898
|
559 |
resetParams();
|
kpeter@898
|
560 |
return *this;
|
kpeter@898
|
561 |
}
|
kpeter@898
|
562 |
|
kpeter@880
|
563 |
/// @}
|
kpeter@880
|
564 |
|
kpeter@880
|
565 |
/// \name Query Functions
|
kpeter@881
|
566 |
/// The results of the algorithm can be obtained using these
|
kpeter@880
|
567 |
/// functions.\n
|
kpeter@881
|
568 |
/// The \ref run() function must be called before using them.
|
kpeter@880
|
569 |
|
kpeter@880
|
570 |
/// @{
|
kpeter@880
|
571 |
|
kpeter@881
|
572 |
/// \brief Return the total cost of the found flow.
|
kpeter@880
|
573 |
///
|
kpeter@881
|
574 |
/// This function returns the total cost of the found flow.
|
kpeter@881
|
575 |
/// Its complexity is O(e).
|
kpeter@881
|
576 |
///
|
kpeter@881
|
577 |
/// \note The return type of the function can be specified as a
|
kpeter@881
|
578 |
/// template parameter. For example,
|
kpeter@881
|
579 |
/// \code
|
kpeter@881
|
580 |
/// cc.totalCost<double>();
|
kpeter@881
|
581 |
/// \endcode
|
kpeter@881
|
582 |
/// It is useful if the total cost cannot be stored in the \c Cost
|
kpeter@881
|
583 |
/// type of the algorithm, which is the default return type of the
|
kpeter@881
|
584 |
/// function.
|
kpeter@880
|
585 |
///
|
kpeter@880
|
586 |
/// \pre \ref run() must be called before using this function.
|
kpeter@881
|
587 |
template <typename Number>
|
kpeter@881
|
588 |
Number totalCost() const {
|
kpeter@881
|
589 |
Number c = 0;
|
kpeter@881
|
590 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
591 |
int i = _arc_idb[a];
|
kpeter@881
|
592 |
c += static_cast<Number>(_res_cap[i]) *
|
kpeter@881
|
593 |
(-static_cast<Number>(_cost[i]));
|
kpeter@881
|
594 |
}
|
kpeter@881
|
595 |
return c;
|
kpeter@880
|
596 |
}
|
kpeter@880
|
597 |
|
kpeter@881
|
598 |
#ifndef DOXYGEN
|
kpeter@881
|
599 |
Cost totalCost() const {
|
kpeter@881
|
600 |
return totalCost<Cost>();
|
kpeter@880
|
601 |
}
|
kpeter@881
|
602 |
#endif
|
kpeter@880
|
603 |
|
kpeter@880
|
604 |
/// \brief Return the flow on the given arc.
|
kpeter@880
|
605 |
///
|
kpeter@881
|
606 |
/// This function returns the flow on the given arc.
|
kpeter@880
|
607 |
///
|
kpeter@880
|
608 |
/// \pre \ref run() must be called before using this function.
|
kpeter@881
|
609 |
Value flow(const Arc& a) const {
|
kpeter@881
|
610 |
return _res_cap[_arc_idb[a]];
|
kpeter@880
|
611 |
}
|
kpeter@880
|
612 |
|
kpeter@881
|
613 |
/// \brief Return the flow map (the primal solution).
|
kpeter@880
|
614 |
///
|
kpeter@881
|
615 |
/// This function copies the flow value on each arc into the given
|
kpeter@881
|
616 |
/// map. The \c Value type of the algorithm must be convertible to
|
kpeter@881
|
617 |
/// the \c Value type of the map.
|
kpeter@880
|
618 |
///
|
kpeter@880
|
619 |
/// \pre \ref run() must be called before using this function.
|
kpeter@881
|
620 |
template <typename FlowMap>
|
kpeter@881
|
621 |
void flowMap(FlowMap &map) const {
|
kpeter@881
|
622 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
623 |
map.set(a, _res_cap[_arc_idb[a]]);
|
kpeter@881
|
624 |
}
|
kpeter@880
|
625 |
}
|
kpeter@880
|
626 |
|
kpeter@881
|
627 |
/// \brief Return the potential (dual value) of the given node.
|
kpeter@880
|
628 |
///
|
kpeter@881
|
629 |
/// This function returns the potential (dual value) of the
|
kpeter@881
|
630 |
/// given node.
|
kpeter@880
|
631 |
///
|
kpeter@880
|
632 |
/// \pre \ref run() must be called before using this function.
|
kpeter@881
|
633 |
Cost potential(const Node& n) const {
|
kpeter@881
|
634 |
return static_cast<Cost>(_pi[_node_id[n]]);
|
kpeter@881
|
635 |
}
|
kpeter@881
|
636 |
|
kpeter@881
|
637 |
/// \brief Return the potential map (the dual solution).
|
kpeter@881
|
638 |
///
|
kpeter@881
|
639 |
/// This function copies the potential (dual value) of each node
|
kpeter@881
|
640 |
/// into the given map.
|
kpeter@881
|
641 |
/// The \c Cost type of the algorithm must be convertible to the
|
kpeter@881
|
642 |
/// \c Value type of the map.
|
kpeter@881
|
643 |
///
|
kpeter@881
|
644 |
/// \pre \ref run() must be called before using this function.
|
kpeter@881
|
645 |
template <typename PotentialMap>
|
kpeter@881
|
646 |
void potentialMap(PotentialMap &map) const {
|
kpeter@881
|
647 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@881
|
648 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]]));
|
kpeter@881
|
649 |
}
|
kpeter@880
|
650 |
}
|
kpeter@880
|
651 |
|
kpeter@880
|
652 |
/// @}
|
kpeter@880
|
653 |
|
kpeter@880
|
654 |
private:
|
kpeter@880
|
655 |
|
kpeter@881
|
656 |
// Initialize the algorithm
|
kpeter@881
|
657 |
ProblemType init() {
|
kpeter@881
|
658 |
if (_res_node_num <= 1) return INFEASIBLE;
|
kpeter@880
|
659 |
|
kpeter@881
|
660 |
// Check the sum of supply values
|
kpeter@881
|
661 |
_sum_supply = 0;
|
kpeter@881
|
662 |
for (int i = 0; i != _root; ++i) {
|
kpeter@881
|
663 |
_sum_supply += _supply[i];
|
kpeter@880
|
664 |
}
|
kpeter@881
|
665 |
if (_sum_supply > 0) return INFEASIBLE;
|
alpar@956
|
666 |
|
kpeter@881
|
667 |
|
kpeter@881
|
668 |
// Initialize vectors
|
kpeter@881
|
669 |
for (int i = 0; i != _res_node_num; ++i) {
|
kpeter@881
|
670 |
_pi[i] = 0;
|
kpeter@881
|
671 |
}
|
kpeter@881
|
672 |
ValueVector excess(_supply);
|
alpar@956
|
673 |
|
kpeter@881
|
674 |
// Remove infinite upper bounds and check negative arcs
|
kpeter@881
|
675 |
const Value MAX = std::numeric_limits<Value>::max();
|
kpeter@881
|
676 |
int last_out;
|
kpeter@881
|
677 |
if (_have_lower) {
|
kpeter@881
|
678 |
for (int i = 0; i != _root; ++i) {
|
kpeter@881
|
679 |
last_out = _first_out[i+1];
|
kpeter@881
|
680 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
kpeter@881
|
681 |
if (_forward[j]) {
|
kpeter@881
|
682 |
Value c = _cost[j] < 0 ? _upper[j] : _lower[j];
|
kpeter@881
|
683 |
if (c >= MAX) return UNBOUNDED;
|
kpeter@881
|
684 |
excess[i] -= c;
|
kpeter@881
|
685 |
excess[_target[j]] += c;
|
kpeter@881
|
686 |
}
|
kpeter@881
|
687 |
}
|
kpeter@881
|
688 |
}
|
kpeter@881
|
689 |
} else {
|
kpeter@881
|
690 |
for (int i = 0; i != _root; ++i) {
|
kpeter@881
|
691 |
last_out = _first_out[i+1];
|
kpeter@881
|
692 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
kpeter@881
|
693 |
if (_forward[j] && _cost[j] < 0) {
|
kpeter@881
|
694 |
Value c = _upper[j];
|
kpeter@881
|
695 |
if (c >= MAX) return UNBOUNDED;
|
kpeter@881
|
696 |
excess[i] -= c;
|
kpeter@881
|
697 |
excess[_target[j]] += c;
|
kpeter@881
|
698 |
}
|
kpeter@881
|
699 |
}
|
kpeter@881
|
700 |
}
|
kpeter@881
|
701 |
}
|
kpeter@881
|
702 |
Value ex, max_cap = 0;
|
kpeter@881
|
703 |
for (int i = 0; i != _res_node_num; ++i) {
|
kpeter@881
|
704 |
ex = excess[i];
|
kpeter@881
|
705 |
if (ex < 0) max_cap -= ex;
|
kpeter@881
|
706 |
}
|
kpeter@881
|
707 |
for (int j = 0; j != _res_arc_num; ++j) {
|
kpeter@881
|
708 |
if (_upper[j] >= MAX) _upper[j] = max_cap;
|
kpeter@880
|
709 |
}
|
kpeter@880
|
710 |
|
kpeter@881
|
711 |
// Initialize maps for Circulation and remove non-zero lower bounds
|
kpeter@881
|
712 |
ConstMap<Arc, Value> low(0);
|
kpeter@881
|
713 |
typedef typename Digraph::template ArcMap<Value> ValueArcMap;
|
kpeter@881
|
714 |
typedef typename Digraph::template NodeMap<Value> ValueNodeMap;
|
kpeter@881
|
715 |
ValueArcMap cap(_graph), flow(_graph);
|
kpeter@881
|
716 |
ValueNodeMap sup(_graph);
|
kpeter@881
|
717 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@881
|
718 |
sup[n] = _supply[_node_id[n]];
|
kpeter@881
|
719 |
}
|
kpeter@881
|
720 |
if (_have_lower) {
|
kpeter@881
|
721 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
722 |
int j = _arc_idf[a];
|
kpeter@881
|
723 |
Value c = _lower[j];
|
kpeter@881
|
724 |
cap[a] = _upper[j] - c;
|
kpeter@881
|
725 |
sup[_graph.source(a)] -= c;
|
kpeter@881
|
726 |
sup[_graph.target(a)] += c;
|
kpeter@881
|
727 |
}
|
kpeter@881
|
728 |
} else {
|
kpeter@881
|
729 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
730 |
cap[a] = _upper[_arc_idf[a]];
|
kpeter@881
|
731 |
}
|
kpeter@881
|
732 |
}
|
kpeter@880
|
733 |
|
kpeter@881
|
734 |
// Find a feasible flow using Circulation
|
kpeter@881
|
735 |
Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap>
|
kpeter@881
|
736 |
circ(_graph, low, cap, sup);
|
kpeter@881
|
737 |
if (!circ.flowMap(flow).run()) return INFEASIBLE;
|
kpeter@881
|
738 |
|
kpeter@881
|
739 |
// Set residual capacities and handle GEQ supply type
|
kpeter@881
|
740 |
if (_sum_supply < 0) {
|
kpeter@881
|
741 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
742 |
Value fa = flow[a];
|
kpeter@881
|
743 |
_res_cap[_arc_idf[a]] = cap[a] - fa;
|
kpeter@881
|
744 |
_res_cap[_arc_idb[a]] = fa;
|
kpeter@881
|
745 |
sup[_graph.source(a)] -= fa;
|
kpeter@881
|
746 |
sup[_graph.target(a)] += fa;
|
kpeter@881
|
747 |
}
|
kpeter@881
|
748 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@881
|
749 |
excess[_node_id[n]] = sup[n];
|
kpeter@881
|
750 |
}
|
kpeter@881
|
751 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
kpeter@881
|
752 |
int u = _target[a];
|
kpeter@881
|
753 |
int ra = _reverse[a];
|
kpeter@881
|
754 |
_res_cap[a] = -_sum_supply + 1;
|
kpeter@881
|
755 |
_res_cap[ra] = -excess[u];
|
kpeter@881
|
756 |
_cost[a] = 0;
|
kpeter@881
|
757 |
_cost[ra] = 0;
|
kpeter@881
|
758 |
}
|
kpeter@881
|
759 |
} else {
|
kpeter@881
|
760 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@881
|
761 |
Value fa = flow[a];
|
kpeter@881
|
762 |
_res_cap[_arc_idf[a]] = cap[a] - fa;
|
kpeter@881
|
763 |
_res_cap[_arc_idb[a]] = fa;
|
kpeter@881
|
764 |
}
|
kpeter@881
|
765 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
kpeter@881
|
766 |
int ra = _reverse[a];
|
kpeter@881
|
767 |
_res_cap[a] = 1;
|
kpeter@881
|
768 |
_res_cap[ra] = 0;
|
kpeter@881
|
769 |
_cost[a] = 0;
|
kpeter@881
|
770 |
_cost[ra] = 0;
|
kpeter@881
|
771 |
}
|
kpeter@881
|
772 |
}
|
alpar@956
|
773 |
|
kpeter@881
|
774 |
return OPTIMAL;
|
kpeter@881
|
775 |
}
|
alpar@956
|
776 |
|
kpeter@881
|
777 |
// Build a StaticDigraph structure containing the current
|
kpeter@881
|
778 |
// residual network
|
kpeter@881
|
779 |
void buildResidualNetwork() {
|
kpeter@881
|
780 |
_arc_vec.clear();
|
kpeter@881
|
781 |
_cost_vec.clear();
|
kpeter@881
|
782 |
_id_vec.clear();
|
kpeter@881
|
783 |
for (int j = 0; j != _res_arc_num; ++j) {
|
kpeter@881
|
784 |
if (_res_cap[j] > 0) {
|
kpeter@881
|
785 |
_arc_vec.push_back(IntPair(_source[j], _target[j]));
|
kpeter@881
|
786 |
_cost_vec.push_back(_cost[j]);
|
kpeter@881
|
787 |
_id_vec.push_back(j);
|
kpeter@881
|
788 |
}
|
kpeter@881
|
789 |
}
|
kpeter@881
|
790 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
|
kpeter@880
|
791 |
}
|
kpeter@880
|
792 |
|
kpeter@881
|
793 |
// Execute the algorithm and transform the results
|
kpeter@881
|
794 |
void start(Method method) {
|
kpeter@881
|
795 |
// Execute the algorithm
|
kpeter@881
|
796 |
switch (method) {
|
kpeter@881
|
797 |
case SIMPLE_CYCLE_CANCELING:
|
kpeter@881
|
798 |
startSimpleCycleCanceling();
|
kpeter@881
|
799 |
break;
|
kpeter@881
|
800 |
case MINIMUM_MEAN_CYCLE_CANCELING:
|
kpeter@881
|
801 |
startMinMeanCycleCanceling();
|
kpeter@881
|
802 |
break;
|
kpeter@881
|
803 |
case CANCEL_AND_TIGHTEN:
|
kpeter@881
|
804 |
startCancelAndTighten();
|
kpeter@881
|
805 |
break;
|
kpeter@881
|
806 |
}
|
kpeter@880
|
807 |
|
kpeter@881
|
808 |
// Compute node potentials
|
kpeter@881
|
809 |
if (method != SIMPLE_CYCLE_CANCELING) {
|
kpeter@881
|
810 |
buildResidualNetwork();
|
kpeter@881
|
811 |
typename BellmanFord<StaticDigraph, CostArcMap>
|
kpeter@881
|
812 |
::template SetDistMap<CostNodeMap>::Create bf(_sgr, _cost_map);
|
kpeter@881
|
813 |
bf.distMap(_pi_map);
|
kpeter@881
|
814 |
bf.init(0);
|
kpeter@881
|
815 |
bf.start();
|
kpeter@880
|
816 |
}
|
kpeter@881
|
817 |
|
kpeter@881
|
818 |
// Handle non-zero lower bounds
|
kpeter@881
|
819 |
if (_have_lower) {
|
kpeter@881
|
820 |
int limit = _first_out[_root];
|
kpeter@881
|
821 |
for (int j = 0; j != limit; ++j) {
|
kpeter@881
|
822 |
if (!_forward[j]) _res_cap[j] += _lower[j];
|
kpeter@881
|
823 |
}
|
kpeter@881
|
824 |
}
|
kpeter@880
|
825 |
}
|
kpeter@880
|
826 |
|
kpeter@881
|
827 |
// Execute the "Simple Cycle Canceling" method
|
kpeter@881
|
828 |
void startSimpleCycleCanceling() {
|
kpeter@881
|
829 |
// Constants for computing the iteration limits
|
kpeter@881
|
830 |
const int BF_FIRST_LIMIT = 2;
|
kpeter@881
|
831 |
const double BF_LIMIT_FACTOR = 1.5;
|
alpar@956
|
832 |
|
kpeter@886
|
833 |
typedef StaticVectorMap<StaticDigraph::Arc, Value> FilterMap;
|
kpeter@881
|
834 |
typedef FilterArcs<StaticDigraph, FilterMap> ResDigraph;
|
kpeter@886
|
835 |
typedef StaticVectorMap<StaticDigraph::Node, StaticDigraph::Arc> PredMap;
|
kpeter@881
|
836 |
typedef typename BellmanFord<ResDigraph, CostArcMap>
|
kpeter@881
|
837 |
::template SetDistMap<CostNodeMap>
|
kpeter@881
|
838 |
::template SetPredMap<PredMap>::Create BF;
|
alpar@956
|
839 |
|
kpeter@881
|
840 |
// Build the residual network
|
kpeter@881
|
841 |
_arc_vec.clear();
|
kpeter@881
|
842 |
_cost_vec.clear();
|
kpeter@881
|
843 |
for (int j = 0; j != _res_arc_num; ++j) {
|
kpeter@881
|
844 |
_arc_vec.push_back(IntPair(_source[j], _target[j]));
|
kpeter@881
|
845 |
_cost_vec.push_back(_cost[j]);
|
kpeter@881
|
846 |
}
|
kpeter@881
|
847 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
|
kpeter@881
|
848 |
|
kpeter@881
|
849 |
FilterMap filter_map(_res_cap);
|
kpeter@881
|
850 |
ResDigraph rgr(_sgr, filter_map);
|
kpeter@881
|
851 |
std::vector<int> cycle;
|
kpeter@881
|
852 |
std::vector<StaticDigraph::Arc> pred(_res_arc_num);
|
kpeter@881
|
853 |
PredMap pred_map(pred);
|
kpeter@881
|
854 |
BF bf(rgr, _cost_map);
|
kpeter@881
|
855 |
bf.distMap(_pi_map).predMap(pred_map);
|
kpeter@880
|
856 |
|
kpeter@880
|
857 |
int length_bound = BF_FIRST_LIMIT;
|
kpeter@880
|
858 |
bool optimal = false;
|
kpeter@880
|
859 |
while (!optimal) {
|
kpeter@880
|
860 |
bf.init(0);
|
kpeter@880
|
861 |
int iter_num = 0;
|
kpeter@880
|
862 |
bool cycle_found = false;
|
kpeter@880
|
863 |
while (!cycle_found) {
|
kpeter@881
|
864 |
// Perform some iterations of the Bellman-Ford algorithm
|
kpeter@881
|
865 |
int curr_iter_num = iter_num + length_bound <= _node_num ?
|
kpeter@881
|
866 |
length_bound : _node_num - iter_num;
|
kpeter@880
|
867 |
iter_num += curr_iter_num;
|
kpeter@880
|
868 |
int real_iter_num = curr_iter_num;
|
kpeter@880
|
869 |
for (int i = 0; i < curr_iter_num; ++i) {
|
kpeter@880
|
870 |
if (bf.processNextWeakRound()) {
|
kpeter@880
|
871 |
real_iter_num = i;
|
kpeter@880
|
872 |
break;
|
kpeter@880
|
873 |
}
|
kpeter@880
|
874 |
}
|
kpeter@880
|
875 |
if (real_iter_num < curr_iter_num) {
|
kpeter@880
|
876 |
// Optimal flow is found
|
kpeter@880
|
877 |
optimal = true;
|
kpeter@880
|
878 |
break;
|
kpeter@880
|
879 |
} else {
|
kpeter@881
|
880 |
// Search for node disjoint negative cycles
|
kpeter@881
|
881 |
std::vector<int> state(_res_node_num, 0);
|
kpeter@880
|
882 |
int id = 0;
|
kpeter@881
|
883 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
884 |
if (state[u] != 0) continue;
|
kpeter@881
|
885 |
++id;
|
kpeter@881
|
886 |
int v = u;
|
kpeter@881
|
887 |
for (; v != -1 && state[v] == 0; v = pred[v] == INVALID ?
|
kpeter@881
|
888 |
-1 : rgr.id(rgr.source(pred[v]))) {
|
kpeter@881
|
889 |
state[v] = id;
|
kpeter@880
|
890 |
}
|
kpeter@881
|
891 |
if (v != -1 && state[v] == id) {
|
kpeter@881
|
892 |
// A negative cycle is found
|
kpeter@880
|
893 |
cycle_found = true;
|
kpeter@880
|
894 |
cycle.clear();
|
kpeter@881
|
895 |
StaticDigraph::Arc a = pred[v];
|
kpeter@881
|
896 |
Value d, delta = _res_cap[rgr.id(a)];
|
kpeter@881
|
897 |
cycle.push_back(rgr.id(a));
|
kpeter@881
|
898 |
while (rgr.id(rgr.source(a)) != v) {
|
kpeter@881
|
899 |
a = pred_map[rgr.source(a)];
|
kpeter@881
|
900 |
d = _res_cap[rgr.id(a)];
|
kpeter@881
|
901 |
if (d < delta) delta = d;
|
kpeter@881
|
902 |
cycle.push_back(rgr.id(a));
|
kpeter@880
|
903 |
}
|
kpeter@880
|
904 |
|
kpeter@881
|
905 |
// Augment along the cycle
|
kpeter@881
|
906 |
for (int i = 0; i < int(cycle.size()); ++i) {
|
kpeter@881
|
907 |
int j = cycle[i];
|
kpeter@881
|
908 |
_res_cap[j] -= delta;
|
kpeter@881
|
909 |
_res_cap[_reverse[j]] += delta;
|
kpeter@881
|
910 |
}
|
kpeter@880
|
911 |
}
|
kpeter@880
|
912 |
}
|
kpeter@880
|
913 |
}
|
kpeter@880
|
914 |
|
kpeter@881
|
915 |
// Increase iteration limit if no cycle is found
|
kpeter@881
|
916 |
if (!cycle_found) {
|
kpeter@881
|
917 |
length_bound = static_cast<int>(length_bound * BF_LIMIT_FACTOR);
|
kpeter@881
|
918 |
}
|
kpeter@880
|
919 |
}
|
kpeter@880
|
920 |
}
|
kpeter@880
|
921 |
}
|
kpeter@880
|
922 |
|
kpeter@881
|
923 |
// Execute the "Minimum Mean Cycle Canceling" method
|
kpeter@881
|
924 |
void startMinMeanCycleCanceling() {
|
kpeter@881
|
925 |
typedef SimplePath<StaticDigraph> SPath;
|
kpeter@881
|
926 |
typedef typename SPath::ArcIt SPathArcIt;
|
kpeter@942
|
927 |
typedef typename HowardMmc<StaticDigraph, CostArcMap>
|
kpeter@881
|
928 |
::template SetPath<SPath>::Create MMC;
|
alpar@956
|
929 |
|
kpeter@881
|
930 |
SPath cycle;
|
kpeter@881
|
931 |
MMC mmc(_sgr, _cost_map);
|
kpeter@881
|
932 |
mmc.cycle(cycle);
|
kpeter@881
|
933 |
buildResidualNetwork();
|
kpeter@942
|
934 |
while (mmc.findCycleMean() && mmc.cycleCost() < 0) {
|
kpeter@881
|
935 |
// Find the cycle
|
kpeter@881
|
936 |
mmc.findCycle();
|
kpeter@880
|
937 |
|
kpeter@881
|
938 |
// Compute delta value
|
kpeter@881
|
939 |
Value delta = INF;
|
kpeter@881
|
940 |
for (SPathArcIt a(cycle); a != INVALID; ++a) {
|
kpeter@881
|
941 |
Value d = _res_cap[_id_vec[_sgr.id(a)]];
|
kpeter@881
|
942 |
if (d < delta) delta = d;
|
kpeter@881
|
943 |
}
|
kpeter@880
|
944 |
|
kpeter@881
|
945 |
// Augment along the cycle
|
kpeter@881
|
946 |
for (SPathArcIt a(cycle); a != INVALID; ++a) {
|
kpeter@881
|
947 |
int j = _id_vec[_sgr.id(a)];
|
kpeter@881
|
948 |
_res_cap[j] -= delta;
|
kpeter@881
|
949 |
_res_cap[_reverse[j]] += delta;
|
kpeter@881
|
950 |
}
|
kpeter@881
|
951 |
|
alpar@956
|
952 |
// Rebuild the residual network
|
kpeter@881
|
953 |
buildResidualNetwork();
|
kpeter@881
|
954 |
}
|
kpeter@881
|
955 |
}
|
kpeter@881
|
956 |
|
kpeter@881
|
957 |
// Execute the "Cancel And Tighten" method
|
kpeter@881
|
958 |
void startCancelAndTighten() {
|
kpeter@881
|
959 |
// Constants for the min mean cycle computations
|
kpeter@881
|
960 |
const double LIMIT_FACTOR = 1.0;
|
kpeter@881
|
961 |
const int MIN_LIMIT = 5;
|
kpeter@881
|
962 |
|
kpeter@881
|
963 |
// Contruct auxiliary data vectors
|
kpeter@881
|
964 |
DoubleVector pi(_res_node_num, 0.0);
|
kpeter@881
|
965 |
IntVector level(_res_node_num);
|
kpeter@910
|
966 |
BoolVector reached(_res_node_num);
|
kpeter@910
|
967 |
BoolVector processed(_res_node_num);
|
kpeter@881
|
968 |
IntVector pred_node(_res_node_num);
|
kpeter@881
|
969 |
IntVector pred_arc(_res_node_num);
|
kpeter@881
|
970 |
std::vector<int> stack(_res_node_num);
|
kpeter@881
|
971 |
std::vector<int> proc_vector(_res_node_num);
|
kpeter@881
|
972 |
|
kpeter@881
|
973 |
// Initialize epsilon
|
kpeter@881
|
974 |
double epsilon = 0;
|
kpeter@881
|
975 |
for (int a = 0; a != _res_arc_num; ++a) {
|
kpeter@881
|
976 |
if (_res_cap[a] > 0 && -_cost[a] > epsilon)
|
kpeter@881
|
977 |
epsilon = -_cost[a];
|
kpeter@881
|
978 |
}
|
kpeter@881
|
979 |
|
kpeter@881
|
980 |
// Start phases
|
kpeter@881
|
981 |
Tolerance<double> tol;
|
kpeter@881
|
982 |
tol.epsilon(1e-6);
|
kpeter@881
|
983 |
int limit = int(LIMIT_FACTOR * std::sqrt(double(_res_node_num)));
|
kpeter@881
|
984 |
if (limit < MIN_LIMIT) limit = MIN_LIMIT;
|
kpeter@881
|
985 |
int iter = limit;
|
kpeter@881
|
986 |
while (epsilon * _res_node_num >= 1) {
|
kpeter@881
|
987 |
// Find and cancel cycles in the admissible network using DFS
|
kpeter@881
|
988 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
989 |
reached[u] = false;
|
kpeter@881
|
990 |
processed[u] = false;
|
kpeter@881
|
991 |
}
|
kpeter@881
|
992 |
int stack_head = -1;
|
kpeter@881
|
993 |
int proc_head = -1;
|
kpeter@881
|
994 |
for (int start = 0; start != _res_node_num; ++start) {
|
kpeter@881
|
995 |
if (reached[start]) continue;
|
kpeter@881
|
996 |
|
kpeter@881
|
997 |
// New start node
|
kpeter@881
|
998 |
reached[start] = true;
|
kpeter@881
|
999 |
pred_arc[start] = -1;
|
kpeter@881
|
1000 |
pred_node[start] = -1;
|
kpeter@881
|
1001 |
|
kpeter@881
|
1002 |
// Find the first admissible outgoing arc
|
kpeter@881
|
1003 |
double p = pi[start];
|
kpeter@881
|
1004 |
int a = _first_out[start];
|
kpeter@881
|
1005 |
int last_out = _first_out[start+1];
|
kpeter@881
|
1006 |
for (; a != last_out && (_res_cap[a] == 0 ||
|
kpeter@881
|
1007 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ;
|
kpeter@881
|
1008 |
if (a == last_out) {
|
kpeter@881
|
1009 |
processed[start] = true;
|
kpeter@881
|
1010 |
proc_vector[++proc_head] = start;
|
kpeter@881
|
1011 |
continue;
|
kpeter@881
|
1012 |
}
|
kpeter@881
|
1013 |
stack[++stack_head] = a;
|
kpeter@881
|
1014 |
|
kpeter@881
|
1015 |
while (stack_head >= 0) {
|
kpeter@881
|
1016 |
int sa = stack[stack_head];
|
kpeter@881
|
1017 |
int u = _source[sa];
|
kpeter@881
|
1018 |
int v = _target[sa];
|
kpeter@881
|
1019 |
|
kpeter@881
|
1020 |
if (!reached[v]) {
|
kpeter@881
|
1021 |
// A new node is reached
|
kpeter@881
|
1022 |
reached[v] = true;
|
kpeter@881
|
1023 |
pred_node[v] = u;
|
kpeter@881
|
1024 |
pred_arc[v] = sa;
|
kpeter@881
|
1025 |
p = pi[v];
|
kpeter@881
|
1026 |
a = _first_out[v];
|
kpeter@881
|
1027 |
last_out = _first_out[v+1];
|
kpeter@881
|
1028 |
for (; a != last_out && (_res_cap[a] == 0 ||
|
kpeter@881
|
1029 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ;
|
kpeter@881
|
1030 |
stack[++stack_head] = a == last_out ? -1 : a;
|
kpeter@881
|
1031 |
} else {
|
kpeter@881
|
1032 |
if (!processed[v]) {
|
kpeter@881
|
1033 |
// A cycle is found
|
kpeter@881
|
1034 |
int n, w = u;
|
kpeter@881
|
1035 |
Value d, delta = _res_cap[sa];
|
kpeter@881
|
1036 |
for (n = u; n != v; n = pred_node[n]) {
|
kpeter@881
|
1037 |
d = _res_cap[pred_arc[n]];
|
kpeter@881
|
1038 |
if (d <= delta) {
|
kpeter@881
|
1039 |
delta = d;
|
kpeter@881
|
1040 |
w = pred_node[n];
|
kpeter@881
|
1041 |
}
|
kpeter@881
|
1042 |
}
|
kpeter@881
|
1043 |
|
kpeter@881
|
1044 |
// Augment along the cycle
|
kpeter@881
|
1045 |
_res_cap[sa] -= delta;
|
kpeter@881
|
1046 |
_res_cap[_reverse[sa]] += delta;
|
kpeter@881
|
1047 |
for (n = u; n != v; n = pred_node[n]) {
|
kpeter@881
|
1048 |
int pa = pred_arc[n];
|
kpeter@881
|
1049 |
_res_cap[pa] -= delta;
|
kpeter@881
|
1050 |
_res_cap[_reverse[pa]] += delta;
|
kpeter@881
|
1051 |
}
|
kpeter@881
|
1052 |
for (n = u; stack_head > 0 && n != w; n = pred_node[n]) {
|
kpeter@881
|
1053 |
--stack_head;
|
kpeter@881
|
1054 |
reached[n] = false;
|
kpeter@881
|
1055 |
}
|
kpeter@881
|
1056 |
u = w;
|
kpeter@881
|
1057 |
}
|
kpeter@881
|
1058 |
v = u;
|
kpeter@881
|
1059 |
|
kpeter@881
|
1060 |
// Find the next admissible outgoing arc
|
kpeter@881
|
1061 |
p = pi[v];
|
kpeter@881
|
1062 |
a = stack[stack_head] + 1;
|
kpeter@881
|
1063 |
last_out = _first_out[v+1];
|
kpeter@881
|
1064 |
for (; a != last_out && (_res_cap[a] == 0 ||
|
kpeter@881
|
1065 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ;
|
kpeter@881
|
1066 |
stack[stack_head] = a == last_out ? -1 : a;
|
kpeter@881
|
1067 |
}
|
kpeter@881
|
1068 |
|
kpeter@881
|
1069 |
while (stack_head >= 0 && stack[stack_head] == -1) {
|
kpeter@881
|
1070 |
processed[v] = true;
|
kpeter@881
|
1071 |
proc_vector[++proc_head] = v;
|
kpeter@881
|
1072 |
if (--stack_head >= 0) {
|
kpeter@881
|
1073 |
// Find the next admissible outgoing arc
|
kpeter@881
|
1074 |
v = _source[stack[stack_head]];
|
kpeter@881
|
1075 |
p = pi[v];
|
kpeter@881
|
1076 |
a = stack[stack_head] + 1;
|
kpeter@881
|
1077 |
last_out = _first_out[v+1];
|
kpeter@881
|
1078 |
for (; a != last_out && (_res_cap[a] == 0 ||
|
kpeter@881
|
1079 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ;
|
kpeter@881
|
1080 |
stack[stack_head] = a == last_out ? -1 : a;
|
kpeter@881
|
1081 |
}
|
kpeter@881
|
1082 |
}
|
kpeter@881
|
1083 |
}
|
kpeter@881
|
1084 |
}
|
kpeter@881
|
1085 |
|
kpeter@881
|
1086 |
// Tighten potentials and epsilon
|
kpeter@881
|
1087 |
if (--iter > 0) {
|
kpeter@881
|
1088 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
1089 |
level[u] = 0;
|
kpeter@881
|
1090 |
}
|
kpeter@881
|
1091 |
for (int i = proc_head; i > 0; --i) {
|
kpeter@881
|
1092 |
int u = proc_vector[i];
|
kpeter@881
|
1093 |
double p = pi[u];
|
kpeter@881
|
1094 |
int l = level[u] + 1;
|
kpeter@881
|
1095 |
int last_out = _first_out[u+1];
|
kpeter@881
|
1096 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
kpeter@881
|
1097 |
int v = _target[a];
|
kpeter@881
|
1098 |
if (_res_cap[a] > 0 && tol.negative(_cost[a] + p - pi[v]) &&
|
kpeter@881
|
1099 |
l > level[v]) level[v] = l;
|
kpeter@881
|
1100 |
}
|
kpeter@880
|
1101 |
}
|
kpeter@880
|
1102 |
|
kpeter@881
|
1103 |
// Modify potentials
|
kpeter@881
|
1104 |
double q = std::numeric_limits<double>::max();
|
kpeter@881
|
1105 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
1106 |
int lu = level[u];
|
kpeter@881
|
1107 |
double p, pu = pi[u];
|
kpeter@881
|
1108 |
int last_out = _first_out[u+1];
|
kpeter@881
|
1109 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
kpeter@881
|
1110 |
if (_res_cap[a] == 0) continue;
|
kpeter@881
|
1111 |
int v = _target[a];
|
kpeter@881
|
1112 |
int ld = lu - level[v];
|
kpeter@881
|
1113 |
if (ld > 0) {
|
kpeter@881
|
1114 |
p = (_cost[a] + pu - pi[v] + epsilon) / (ld + 1);
|
kpeter@881
|
1115 |
if (p < q) q = p;
|
kpeter@881
|
1116 |
}
|
kpeter@881
|
1117 |
}
|
kpeter@881
|
1118 |
}
|
kpeter@881
|
1119 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
1120 |
pi[u] -= q * level[u];
|
kpeter@881
|
1121 |
}
|
kpeter@880
|
1122 |
|
kpeter@881
|
1123 |
// Modify epsilon
|
kpeter@881
|
1124 |
epsilon = 0;
|
kpeter@881
|
1125 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
1126 |
double curr, pu = pi[u];
|
kpeter@881
|
1127 |
int last_out = _first_out[u+1];
|
kpeter@881
|
1128 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
kpeter@881
|
1129 |
if (_res_cap[a] == 0) continue;
|
kpeter@881
|
1130 |
curr = _cost[a] + pu - pi[_target[a]];
|
kpeter@881
|
1131 |
if (-curr > epsilon) epsilon = -curr;
|
kpeter@881
|
1132 |
}
|
kpeter@881
|
1133 |
}
|
kpeter@881
|
1134 |
} else {
|
kpeter@942
|
1135 |
typedef HowardMmc<StaticDigraph, CostArcMap> MMC;
|
kpeter@881
|
1136 |
typedef typename BellmanFord<StaticDigraph, CostArcMap>
|
kpeter@881
|
1137 |
::template SetDistMap<CostNodeMap>::Create BF;
|
kpeter@881
|
1138 |
|
kpeter@881
|
1139 |
// Set epsilon to the minimum cycle mean
|
kpeter@881
|
1140 |
buildResidualNetwork();
|
kpeter@881
|
1141 |
MMC mmc(_sgr, _cost_map);
|
kpeter@942
|
1142 |
mmc.findCycleMean();
|
kpeter@881
|
1143 |
epsilon = -mmc.cycleMean();
|
kpeter@942
|
1144 |
Cost cycle_cost = mmc.cycleCost();
|
kpeter@942
|
1145 |
int cycle_size = mmc.cycleSize();
|
alpar@956
|
1146 |
|
kpeter@881
|
1147 |
// Compute feasible potentials for the current epsilon
|
kpeter@881
|
1148 |
for (int i = 0; i != int(_cost_vec.size()); ++i) {
|
kpeter@881
|
1149 |
_cost_vec[i] = cycle_size * _cost_vec[i] - cycle_cost;
|
kpeter@881
|
1150 |
}
|
kpeter@881
|
1151 |
BF bf(_sgr, _cost_map);
|
kpeter@881
|
1152 |
bf.distMap(_pi_map);
|
kpeter@881
|
1153 |
bf.init(0);
|
kpeter@881
|
1154 |
bf.start();
|
kpeter@881
|
1155 |
for (int u = 0; u != _res_node_num; ++u) {
|
kpeter@881
|
1156 |
pi[u] = static_cast<double>(_pi[u]) / cycle_size;
|
kpeter@881
|
1157 |
}
|
alpar@956
|
1158 |
|
kpeter@881
|
1159 |
iter = limit;
|
kpeter@880
|
1160 |
}
|
kpeter@880
|
1161 |
}
|
kpeter@880
|
1162 |
}
|
kpeter@880
|
1163 |
|
kpeter@880
|
1164 |
}; //class CycleCanceling
|
kpeter@880
|
1165 |
|
kpeter@880
|
1166 |
///@}
|
kpeter@880
|
1167 |
|
kpeter@880
|
1168 |
} //namespace lemon
|
kpeter@880
|
1169 |
|
kpeter@880
|
1170 |
#endif //LEMON_CYCLE_CANCELING_H
|