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