1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2010 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CAPACITY_SCALING_H |
20 | 20 |
#define LEMON_CAPACITY_SCALING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/bin_heap.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \brief Default traits class of CapacityScaling algorithm. |
35 | 35 |
/// |
36 | 36 |
/// Default traits class of CapacityScaling algorithm. |
37 | 37 |
/// \tparam GR Digraph type. |
38 | 38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
39 | 39 |
/// and supply values. By default it is \c int. |
40 | 40 |
/// \tparam C The number type used for costs and potentials. |
41 | 41 |
/// By default it is the same as \c V. |
42 | 42 |
template <typename GR, typename V = int, typename C = V> |
43 | 43 |
struct CapacityScalingDefaultTraits |
44 | 44 |
{ |
45 | 45 |
/// The type of the digraph |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
48 | 48 |
typedef V Value; |
49 | 49 |
/// The type of the arc costs |
50 | 50 |
typedef C Cost; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
53 | 53 |
/// |
54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
/// \addtogroup min_cost_flow_algs |
62 | 62 |
/// @{ |
63 | 63 |
|
64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
66 | 66 |
/// |
67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
68 | 68 |
/// of the successive shortest path algorithm for finding a |
69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
71 | 71 |
/// solution method. |
72 | 72 |
/// |
73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
74 | 74 |
/// can be given using separate functions, and the algorithm can be |
75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
76 | 76 |
/// specified, then default values will be used. |
77 | 77 |
/// |
78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
80 | 80 |
/// and supply values in the algorithm. By default, it is \c int. |
81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
82 | 82 |
/// algorithm. By default, it is the same as \c V. |
83 | 83 |
/// \tparam TR The traits class that defines various types used by the |
84 | 84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
85 | 85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
86 | 86 |
/// In most cases, this parameter should not be set directly, |
87 | 87 |
/// consider to use the named template parameters instead. |
88 | 88 |
/// |
89 | 89 |
/// \warning Both \c V and \c C must be signed number types. |
90 |
/// \warning All input data (capacities, supply values, and costs) must |
|
91 |
/// be integer. |
|
90 |
/// \warning Capacity bounds and supply values must be integer, but |
|
91 |
/// arc costs can be arbitrary real numbers. |
|
92 | 92 |
/// \warning This algorithm does not support negative costs for |
93 | 93 |
/// arcs having infinite upper bound. |
94 | 94 |
#ifdef DOXYGEN |
95 | 95 |
template <typename GR, typename V, typename C, typename TR> |
96 | 96 |
#else |
97 | 97 |
template < typename GR, typename V = int, typename C = V, |
98 | 98 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
99 | 99 |
#endif |
100 | 100 |
class CapacityScaling |
101 | 101 |
{ |
102 | 102 |
public: |
103 | 103 |
|
104 | 104 |
/// The type of the digraph |
105 | 105 |
typedef typename TR::Digraph Digraph; |
106 | 106 |
/// The type of the flow amounts, capacity bounds and supply values |
107 | 107 |
typedef typename TR::Value Value; |
108 | 108 |
/// The type of the arc costs |
109 | 109 |
typedef typename TR::Cost Cost; |
110 | 110 |
|
111 | 111 |
/// The type of the heap used for internal Dijkstra computations |
112 | 112 |
typedef typename TR::Heap Heap; |
113 | 113 |
|
114 | 114 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
115 | 115 |
typedef TR Traits; |
116 | 116 |
|
117 | 117 |
public: |
118 | 118 |
|
119 | 119 |
/// \brief Problem type constants for the \c run() function. |
120 | 120 |
/// |
121 | 121 |
/// Enum type containing the problem type constants that can be |
122 | 122 |
/// returned by the \ref run() function of the algorithm. |
123 | 123 |
enum ProblemType { |
124 | 124 |
/// The problem has no feasible solution (flow). |
125 | 125 |
INFEASIBLE, |
126 | 126 |
/// The problem has optimal solution (i.e. it is feasible and |
127 | 127 |
/// bounded), and the algorithm has found optimal flow and node |
128 | 128 |
/// potentials (primal and dual solutions). |
129 | 129 |
OPTIMAL, |
130 | 130 |
/// The digraph contains an arc of negative cost and infinite |
131 | 131 |
/// upper bound. It means that the objective function is unbounded |
132 | 132 |
/// on that arc, however, note that it could actually be bounded |
133 | 133 |
/// over the feasible flows, but this algroithm cannot handle |
134 | 134 |
/// these cases. |
135 | 135 |
UNBOUNDED |
136 | 136 |
}; |
137 | 137 |
|
138 | 138 |
private: |
139 | 139 |
|
140 | 140 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
141 | 141 |
|
142 | 142 |
typedef std::vector<int> IntVector; |
143 | 143 |
typedef std::vector<Value> ValueVector; |
144 | 144 |
typedef std::vector<Cost> CostVector; |
145 | 145 |
typedef std::vector<char> BoolVector; |
146 | 146 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
147 | 147 |
|
148 | 148 |
private: |
149 | 149 |
|
150 | 150 |
// Data related to the underlying digraph |
151 | 151 |
const GR &_graph; |
152 | 152 |
int _node_num; |
153 | 153 |
int _arc_num; |
154 | 154 |
int _res_arc_num; |
155 | 155 |
int _root; |
156 | 156 |
|
157 | 157 |
// Parameters of the problem |
158 | 158 |
bool _have_lower; |
159 | 159 |
Value _sum_supply; |
160 | 160 |
|
161 | 161 |
// Data structures for storing the digraph |
162 | 162 |
IntNodeMap _node_id; |
163 | 163 |
IntArcMap _arc_idf; |
164 | 164 |
IntArcMap _arc_idb; |
165 | 165 |
IntVector _first_out; |
166 | 166 |
BoolVector _forward; |
167 | 167 |
IntVector _source; |
168 | 168 |
IntVector _target; |
169 | 169 |
IntVector _reverse; |
170 | 170 |
|
171 | 171 |
// Node and arc data |
172 | 172 |
ValueVector _lower; |
173 | 173 |
ValueVector _upper; |
174 | 174 |
CostVector _cost; |
175 | 175 |
ValueVector _supply; |
176 | 176 |
|
177 | 177 |
ValueVector _res_cap; |
178 | 178 |
CostVector _pi; |
179 | 179 |
ValueVector _excess; |
180 | 180 |
IntVector _excess_nodes; |
181 | 181 |
IntVector _deficit_nodes; |
182 | 182 |
|
183 | 183 |
Value _delta; |
184 | 184 |
int _factor; |
185 | 185 |
IntVector _pred; |
186 | 186 |
|
187 | 187 |
public: |
188 | 188 |
|
189 | 189 |
/// \brief Constant for infinite upper bounds (capacities). |
190 | 190 |
/// |
191 | 191 |
/// Constant for infinite upper bounds (capacities). |
192 | 192 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
193 | 193 |
/// \c std::numeric_limits<Value>::max() otherwise. |
194 | 194 |
const Value INF; |
195 | 195 |
|
196 | 196 |
private: |
197 | 197 |
|
198 | 198 |
// Special implementation of the Dijkstra algorithm for finding |
199 | 199 |
// shortest paths in the residual network of the digraph with |
200 | 200 |
// respect to the reduced arc costs and modifying the node |
201 | 201 |
// potentials according to the found distance labels. |
202 | 202 |
class ResidualDijkstra |
203 | 203 |
{ |
204 | 204 |
private: |
205 | 205 |
|
206 | 206 |
int _node_num; |
207 | 207 |
bool _geq; |
208 | 208 |
const IntVector &_first_out; |
209 | 209 |
const IntVector &_target; |
210 | 210 |
const CostVector &_cost; |
211 | 211 |
const ValueVector &_res_cap; |
212 | 212 |
const ValueVector &_excess; |
213 | 213 |
CostVector &_pi; |
214 | 214 |
IntVector &_pred; |
215 | 215 |
|
216 | 216 |
IntVector _proc_nodes; |
217 | 217 |
CostVector _dist; |
218 | 218 |
|
219 | 219 |
public: |
220 | 220 |
|
221 | 221 |
ResidualDijkstra(CapacityScaling& cs) : |
222 | 222 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
223 | 223 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
224 | 224 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
225 | 225 |
_pred(cs._pred), _dist(cs._node_num) |
226 | 226 |
{} |
227 | 227 |
|
228 | 228 |
int run(int s, Value delta = 1) { |
229 | 229 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
230 | 230 |
Heap heap(heap_cross_ref); |
231 | 231 |
heap.push(s, 0); |
232 | 232 |
_pred[s] = -1; |
233 | 233 |
_proc_nodes.clear(); |
234 | 234 |
|
235 | 235 |
// Process nodes |
236 | 236 |
while (!heap.empty() && _excess[heap.top()] > -delta) { |
237 | 237 |
int u = heap.top(), v; |
238 | 238 |
Cost d = heap.prio() + _pi[u], dn; |
239 | 239 |
_dist[u] = heap.prio(); |
240 | 240 |
_proc_nodes.push_back(u); |
241 | 241 |
heap.pop(); |
242 | 242 |
|
243 | 243 |
// Traverse outgoing residual arcs |
244 | 244 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
245 | 245 |
for (int a = _first_out[u]; a != last_out; ++a) { |
246 | 246 |
if (_res_cap[a] < delta) continue; |
247 | 247 |
v = _target[a]; |
248 | 248 |
switch (heap.state(v)) { |
249 | 249 |
case Heap::PRE_HEAP: |
250 | 250 |
heap.push(v, d + _cost[a] - _pi[v]); |
251 | 251 |
_pred[v] = a; |
252 | 252 |
break; |
253 | 253 |
case Heap::IN_HEAP: |
254 | 254 |
dn = d + _cost[a] - _pi[v]; |
255 | 255 |
if (dn < heap[v]) { |
256 | 256 |
heap.decrease(v, dn); |
257 | 257 |
_pred[v] = a; |
258 | 258 |
} |
259 | 259 |
break; |
260 | 260 |
case Heap::POST_HEAP: |
261 | 261 |
break; |
262 | 262 |
} |
263 | 263 |
} |
264 | 264 |
} |
265 | 265 |
if (heap.empty()) return -1; |
266 | 266 |
|
267 | 267 |
// Update potentials of processed nodes |
268 | 268 |
int t = heap.top(); |
269 | 269 |
Cost dt = heap.prio(); |
270 | 270 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) { |
271 | 271 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
return t; |
275 | 275 |
} |
276 | 276 |
|
277 | 277 |
}; //class ResidualDijkstra |
278 | 278 |
|
279 | 279 |
public: |
280 | 280 |
|
281 | 281 |
/// \name Named Template Parameters |
282 | 282 |
/// @{ |
283 | 283 |
|
284 | 284 |
template <typename T> |
285 | 285 |
struct SetHeapTraits : public Traits { |
286 | 286 |
typedef T Heap; |
287 | 287 |
}; |
288 | 288 |
|
289 | 289 |
/// \brief \ref named-templ-param "Named parameter" for setting |
290 | 290 |
/// \c Heap type. |
291 | 291 |
/// |
292 | 292 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
293 | 293 |
/// type, which is used for internal Dijkstra computations. |
294 | 294 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
295 | 295 |
/// its priority type must be \c Cost and its cross reference type |
296 | 296 |
/// must be \ref RangeMap "RangeMap<int>". |
297 | 297 |
template <typename T> |
298 | 298 |
struct SetHeap |
299 | 299 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > { |
300 | 300 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
301 | 301 |
}; |
302 | 302 |
|
303 | 303 |
/// @} |
304 | 304 |
|
305 | 305 |
protected: |
306 | 306 |
|
307 | 307 |
CapacityScaling() {} |
308 | 308 |
|
309 | 309 |
public: |
310 | 310 |
|
311 | 311 |
/// \brief Constructor. |
312 | 312 |
/// |
313 | 313 |
/// The constructor of the class. |
314 | 314 |
/// |
315 | 315 |
/// \param graph The digraph the algorithm runs on. |
316 | 316 |
CapacityScaling(const GR& graph) : |
317 | 317 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
318 | 318 |
INF(std::numeric_limits<Value>::has_infinity ? |
319 | 319 |
std::numeric_limits<Value>::infinity() : |
320 | 320 |
std::numeric_limits<Value>::max()) |
321 | 321 |
{ |
322 | 322 |
// Check the number types |
323 | 323 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
324 | 324 |
"The flow type of CapacityScaling must be signed"); |
325 | 325 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
326 | 326 |
"The cost type of CapacityScaling must be signed"); |
327 | 327 |
|
328 | 328 |
// Reset data structures |
329 | 329 |
reset(); |
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
/// \name Parameters |
333 | 333 |
/// The parameters of the algorithm can be specified using these |
334 | 334 |
/// functions. |
335 | 335 |
|
336 | 336 |
/// @{ |
337 | 337 |
|
338 | 338 |
/// \brief Set the lower bounds on the arcs. |
339 | 339 |
/// |
340 | 340 |
/// This function sets the lower bounds on the arcs. |
341 | 341 |
/// If it is not used before calling \ref run(), the lower bounds |
342 | 342 |
/// will be set to zero on all arcs. |
343 | 343 |
/// |
344 | 344 |
/// \param map An arc map storing the lower bounds. |
345 | 345 |
/// Its \c Value type must be convertible to the \c Value type |
346 | 346 |
/// of the algorithm. |
347 | 347 |
/// |
348 | 348 |
/// \return <tt>(*this)</tt> |
349 | 349 |
template <typename LowerMap> |
350 | 350 |
CapacityScaling& lowerMap(const LowerMap& map) { |
351 | 351 |
_have_lower = true; |
352 | 352 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
353 | 353 |
_lower[_arc_idf[a]] = map[a]; |
354 | 354 |
_lower[_arc_idb[a]] = map[a]; |
355 | 355 |
} |
356 | 356 |
return *this; |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
/// \brief Set the upper bounds (capacities) on the arcs. |
360 | 360 |
/// |
361 | 361 |
/// This function sets the upper bounds (capacities) on the arcs. |
362 | 362 |
/// If it is not used before calling \ref run(), the upper bounds |
363 | 363 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
364 | 364 |
/// unbounded from above). |
365 | 365 |
/// |
366 | 366 |
/// \param map An arc map storing the upper bounds. |
367 | 367 |
/// Its \c Value type must be convertible to the \c Value type |
368 | 368 |
/// of the algorithm. |
369 | 369 |
/// |
370 | 370 |
/// \return <tt>(*this)</tt> |
371 | 371 |
template<typename UpperMap> |
372 | 372 |
CapacityScaling& upperMap(const UpperMap& map) { |
373 | 373 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
374 | 374 |
_upper[_arc_idf[a]] = map[a]; |
375 | 375 |
} |
376 | 376 |
return *this; |
377 | 377 |
} |
378 | 378 |
|
379 | 379 |
/// \brief Set the costs of the arcs. |
380 | 380 |
/// |
381 | 381 |
/// This function sets the costs of the arcs. |
382 | 382 |
/// If it is not used before calling \ref run(), the costs |
383 | 383 |
/// will be set to \c 1 on all arcs. |
384 | 384 |
/// |
385 | 385 |
/// \param map An arc map storing the costs. |
386 | 386 |
/// Its \c Value type must be convertible to the \c Cost type |
387 | 387 |
/// of the algorithm. |
388 | 388 |
/// |
389 | 389 |
/// \return <tt>(*this)</tt> |
390 | 390 |
template<typename CostMap> |
391 | 391 |
CapacityScaling& costMap(const CostMap& map) { |
392 | 392 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
393 | 393 |
_cost[_arc_idf[a]] = map[a]; |
394 | 394 |
_cost[_arc_idb[a]] = -map[a]; |
395 | 395 |
} |
396 | 396 |
return *this; |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
/// \brief Set the supply values of the nodes. |
400 | 400 |
/// |
401 | 401 |
/// This function sets the supply values of the nodes. |
402 | 402 |
/// If neither this function nor \ref stSupply() is used before |
403 | 403 |
/// calling \ref run(), the supply of each node will be set to zero. |
404 | 404 |
/// |
405 | 405 |
/// \param map A node map storing the supply values. |
406 | 406 |
/// Its \c Value type must be convertible to the \c Value type |
407 | 407 |
/// of the algorithm. |
408 | 408 |
/// |
409 | 409 |
/// \return <tt>(*this)</tt> |
410 | 410 |
template<typename SupplyMap> |
411 | 411 |
CapacityScaling& supplyMap(const SupplyMap& map) { |
412 | 412 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
413 | 413 |
_supply[_node_id[n]] = map[n]; |
414 | 414 |
} |
415 | 415 |
return *this; |
416 | 416 |
} |
417 | 417 |
|
418 | 418 |
/// \brief Set single source and target nodes and a supply value. |
419 | 419 |
/// |
420 | 420 |
/// This function sets a single source node and a single target node |
421 | 421 |
/// and the required flow value. |
422 | 422 |
/// If neither this function nor \ref supplyMap() is used before |
423 | 423 |
/// calling \ref run(), the supply of each node will be set to zero. |
424 | 424 |
/// |
425 | 425 |
/// Using this function has the same effect as using \ref supplyMap() |
426 | 426 |
/// with a map in which \c k is assigned to \c s, \c -k is |
427 | 427 |
/// assigned to \c t and all other nodes have zero supply value. |
428 | 428 |
/// |
429 | 429 |
/// \param s The source node. |
430 | 430 |
/// \param t The target node. |
431 | 431 |
/// \param k The required amount of flow from node \c s to node \c t |
432 | 432 |
/// (i.e. the supply of \c s and the demand of \c t). |
433 | 433 |
/// |
434 | 434 |
/// \return <tt>(*this)</tt> |
435 | 435 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) { |
436 | 436 |
for (int i = 0; i != _node_num; ++i) { |
437 | 437 |
_supply[i] = 0; |
438 | 438 |
} |
439 | 439 |
_supply[_node_id[s]] = k; |
440 | 440 |
_supply[_node_id[t]] = -k; |
441 | 441 |
return *this; |
442 | 442 |
} |
443 | 443 |
|
444 | 444 |
/// @} |
445 | 445 |
|
446 | 446 |
/// \name Execution control |
447 | 447 |
/// The algorithm can be executed using \ref run(). |
448 | 448 |
|
449 | 449 |
/// @{ |
450 | 450 |
|
451 | 451 |
/// \brief Run the algorithm. |
452 | 452 |
/// |
453 | 453 |
/// This function runs the algorithm. |
454 | 454 |
/// The paramters can be specified using functions \ref lowerMap(), |
455 | 455 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
456 | 456 |
/// For example, |
457 | 457 |
/// \code |
458 | 458 |
/// CapacityScaling<ListDigraph> cs(graph); |
459 | 459 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
460 | 460 |
/// .supplyMap(sup).run(); |
461 | 461 |
/// \endcode |
462 | 462 |
/// |
463 | 463 |
/// This function can be called more than once. All the given parameters |
464 | 464 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
465 | 465 |
/// is used, thus only the modified parameters have to be set again. |
466 | 466 |
/// If the underlying digraph was also modified after the construction |
467 | 467 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
468 | 468 |
/// function must be called. |
469 | 469 |
/// |
470 | 470 |
/// \param factor The capacity scaling factor. It must be larger than |
471 | 471 |
/// one to use scaling. If it is less or equal to one, then scaling |
472 | 472 |
/// will be disabled. |
473 | 473 |
/// |
474 | 474 |
/// \return \c INFEASIBLE if no feasible flow exists, |
475 | 475 |
/// \n \c OPTIMAL if the problem has optimal solution |
476 | 476 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
477 | 477 |
/// optimal flow and node potentials (primal and dual solutions), |
478 | 478 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
479 | 479 |
/// and infinite upper bound. It means that the objective function |
480 | 480 |
/// is unbounded on that arc, however, note that it could actually be |
481 | 481 |
/// bounded over the feasible flows, but this algroithm cannot handle |
482 | 482 |
/// these cases. |
483 | 483 |
/// |
484 | 484 |
/// \see ProblemType |
485 | 485 |
/// \see resetParams(), reset() |
486 | 486 |
ProblemType run(int factor = 4) { |
487 | 487 |
_factor = factor; |
488 | 488 |
ProblemType pt = init(); |
489 | 489 |
if (pt != OPTIMAL) return pt; |
490 | 490 |
return start(); |
491 | 491 |
} |
492 | 492 |
|
493 | 493 |
/// \brief Reset all the parameters that have been given before. |
494 | 494 |
/// |
495 | 495 |
/// This function resets all the paramaters that have been given |
496 | 496 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
497 | 497 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
498 | 498 |
/// |
499 | 499 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
500 | 500 |
/// parameters are kept for the next \ref run() call, unless |
501 | 501 |
/// \ref resetParams() or \ref reset() is used. |
502 | 502 |
/// If the underlying digraph was also modified after the construction |
503 | 503 |
/// of the class or the last \ref reset() call, then the \ref reset() |
504 | 504 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
505 | 505 |
/// |
506 | 506 |
/// For example, |
507 | 507 |
/// \code |
508 | 508 |
/// CapacityScaling<ListDigraph> cs(graph); |
509 | 509 |
/// |
510 | 510 |
/// // First run |
511 | 511 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
512 | 512 |
/// .supplyMap(sup).run(); |
513 | 513 |
/// |
514 | 514 |
/// // Run again with modified cost map (resetParams() is not called, |
515 | 515 |
/// // so only the cost map have to be set again) |
516 | 516 |
/// cost[e] += 100; |
517 | 517 |
/// cs.costMap(cost).run(); |
518 | 518 |
/// |
519 | 519 |
/// // Run again from scratch using resetParams() |
520 | 520 |
/// // (the lower bounds will be set to zero on all arcs) |
521 | 521 |
/// cs.resetParams(); |
522 | 522 |
/// cs.upperMap(capacity).costMap(cost) |
523 | 523 |
/// .supplyMap(sup).run(); |
524 | 524 |
/// \endcode |
525 | 525 |
/// |
526 | 526 |
/// \return <tt>(*this)</tt> |
527 | 527 |
/// |
528 | 528 |
/// \see reset(), run() |
529 | 529 |
CapacityScaling& resetParams() { |
530 | 530 |
for (int i = 0; i != _node_num; ++i) { |
531 | 531 |
_supply[i] = 0; |
532 | 532 |
} |
533 | 533 |
for (int j = 0; j != _res_arc_num; ++j) { |
534 | 534 |
_lower[j] = 0; |
535 | 535 |
_upper[j] = INF; |
536 | 536 |
_cost[j] = _forward[j] ? 1 : -1; |
537 | 537 |
} |
538 | 538 |
_have_lower = false; |
539 | 539 |
return *this; |
540 | 540 |
} |
541 | 541 |
|
542 | 542 |
/// \brief Reset the internal data structures and all the parameters |
543 | 543 |
/// that have been given before. |
544 | 544 |
/// |
545 | 545 |
/// This function resets the internal data structures and all the |
546 | 546 |
/// paramaters that have been given before using functions \ref lowerMap(), |
547 | 547 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
548 | 548 |
/// |
549 | 549 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
550 | 550 |
/// parameters are kept for the next \ref run() call, unless |
551 | 551 |
/// \ref resetParams() or \ref reset() is used. |
552 | 552 |
/// If the underlying digraph was also modified after the construction |
553 | 553 |
/// of the class or the last \ref reset() call, then the \ref reset() |
554 | 554 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
555 | 555 |
/// |
556 | 556 |
/// See \ref resetParams() for examples. |
557 | 557 |
/// |
558 | 558 |
/// \return <tt>(*this)</tt> |
559 | 559 |
/// |
560 | 560 |
/// \see resetParams(), run() |
561 | 561 |
CapacityScaling& reset() { |
562 | 562 |
// Resize vectors |
563 | 563 |
_node_num = countNodes(_graph); |
564 | 564 |
_arc_num = countArcs(_graph); |
565 | 565 |
_res_arc_num = 2 * (_arc_num + _node_num); |
566 | 566 |
_root = _node_num; |
567 | 567 |
++_node_num; |
568 | 568 |
|
569 | 569 |
_first_out.resize(_node_num + 1); |
570 | 570 |
_forward.resize(_res_arc_num); |
571 | 571 |
_source.resize(_res_arc_num); |
572 | 572 |
_target.resize(_res_arc_num); |
573 | 573 |
_reverse.resize(_res_arc_num); |
574 | 574 |
|
575 | 575 |
_lower.resize(_res_arc_num); |
576 | 576 |
_upper.resize(_res_arc_num); |
577 | 577 |
_cost.resize(_res_arc_num); |
578 | 578 |
_supply.resize(_node_num); |
579 | 579 |
|
580 | 580 |
_res_cap.resize(_res_arc_num); |
581 | 581 |
_pi.resize(_node_num); |
582 | 582 |
_excess.resize(_node_num); |
583 | 583 |
_pred.resize(_node_num); |
584 | 584 |
|
585 | 585 |
// Copy the graph |
586 | 586 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
587 | 587 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
588 | 588 |
_node_id[n] = i; |
589 | 589 |
} |
590 | 590 |
i = 0; |
591 | 591 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
592 | 592 |
_first_out[i] = j; |
593 | 593 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
594 | 594 |
_arc_idf[a] = j; |
595 | 595 |
_forward[j] = true; |
596 | 596 |
_source[j] = i; |
597 | 597 |
_target[j] = _node_id[_graph.runningNode(a)]; |
598 | 598 |
} |
599 | 599 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
600 | 600 |
_arc_idb[a] = j; |
601 | 601 |
_forward[j] = false; |
602 | 602 |
_source[j] = i; |
603 | 603 |
_target[j] = _node_id[_graph.runningNode(a)]; |
0 comments (0 inline)