0
4
0
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 number types must be signed and all input data must |
90 | 90 |
/// be integer. |
91 | 91 |
/// \warning This algorithm does not support negative costs for such |
92 | 92 |
/// arcs that have infinite upper bound. |
93 | 93 |
#ifdef DOXYGEN |
94 | 94 |
template <typename GR, typename V, typename C, typename TR> |
95 | 95 |
#else |
96 | 96 |
template < typename GR, typename V = int, typename C = V, |
97 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
98 | 98 |
#endif |
99 | 99 |
class CapacityScaling |
100 | 100 |
{ |
101 | 101 |
public: |
102 | 102 |
|
103 | 103 |
/// The type of the digraph |
104 | 104 |
typedef typename TR::Digraph Digraph; |
105 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
106 | 106 |
typedef typename TR::Value Value; |
107 | 107 |
/// The type of the arc costs |
108 | 108 |
typedef typename TR::Cost Cost; |
109 | 109 |
|
110 | 110 |
/// The type of the heap used for internal Dijkstra computations |
111 | 111 |
typedef typename TR::Heap Heap; |
112 | 112 |
|
113 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
114 | 114 |
typedef TR Traits; |
115 | 115 |
|
116 | 116 |
public: |
117 | 117 |
|
118 | 118 |
/// \brief Problem type constants for the \c run() function. |
119 | 119 |
/// |
120 | 120 |
/// Enum type containing the problem type constants that can be |
121 | 121 |
/// returned by the \ref run() function of the algorithm. |
122 | 122 |
enum ProblemType { |
123 | 123 |
/// The problem has no feasible solution (flow). |
124 | 124 |
INFEASIBLE, |
125 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
126 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
127 | 127 |
/// potentials (primal and dual solutions). |
128 | 128 |
OPTIMAL, |
129 | 129 |
/// The digraph contains an arc of negative cost and infinite |
130 | 130 |
/// upper bound. It means that the objective function is unbounded |
131 | 131 |
/// on that arc, however, note that it could actually be bounded |
132 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
133 | 133 |
/// these cases. |
134 | 134 |
UNBOUNDED |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
private: |
138 | 138 |
|
139 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
140 | 140 |
|
141 | 141 |
typedef std::vector<int> IntVector; |
142 |
typedef std::vector<char> BoolVector; |
|
143 | 142 |
typedef std::vector<Value> ValueVector; |
144 | 143 |
typedef std::vector<Cost> CostVector; |
144 |
typedef std::vector<char> BoolVector; |
|
145 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
|
145 | 146 |
|
146 | 147 |
private: |
147 | 148 |
|
148 | 149 |
// Data related to the underlying digraph |
149 | 150 |
const GR &_graph; |
150 | 151 |
int _node_num; |
151 | 152 |
int _arc_num; |
152 | 153 |
int _res_arc_num; |
153 | 154 |
int _root; |
154 | 155 |
|
155 | 156 |
// Parameters of the problem |
156 | 157 |
bool _have_lower; |
157 | 158 |
Value _sum_supply; |
158 | 159 |
|
159 | 160 |
// Data structures for storing the digraph |
160 | 161 |
IntNodeMap _node_id; |
161 | 162 |
IntArcMap _arc_idf; |
162 | 163 |
IntArcMap _arc_idb; |
163 | 164 |
IntVector _first_out; |
164 | 165 |
BoolVector _forward; |
165 | 166 |
IntVector _source; |
166 | 167 |
IntVector _target; |
167 | 168 |
IntVector _reverse; |
168 | 169 |
|
169 | 170 |
// Node and arc data |
170 | 171 |
ValueVector _lower; |
171 | 172 |
ValueVector _upper; |
172 | 173 |
CostVector _cost; |
173 | 174 |
ValueVector _supply; |
174 | 175 |
|
175 | 176 |
ValueVector _res_cap; |
176 | 177 |
CostVector _pi; |
177 | 178 |
ValueVector _excess; |
178 | 179 |
IntVector _excess_nodes; |
179 | 180 |
IntVector _deficit_nodes; |
180 | 181 |
|
181 | 182 |
Value _delta; |
182 | 183 |
int _factor; |
183 | 184 |
IntVector _pred; |
184 | 185 |
|
185 | 186 |
public: |
186 | 187 |
|
187 | 188 |
/// \brief Constant for infinite upper bounds (capacities). |
188 | 189 |
/// |
189 | 190 |
/// Constant for infinite upper bounds (capacities). |
190 | 191 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
191 | 192 |
/// \c std::numeric_limits<Value>::max() otherwise. |
192 | 193 |
const Value INF; |
193 | 194 |
|
194 | 195 |
private: |
195 | 196 |
|
196 | 197 |
// Special implementation of the Dijkstra algorithm for finding |
197 | 198 |
// shortest paths in the residual network of the digraph with |
198 | 199 |
// respect to the reduced arc costs and modifying the node |
199 | 200 |
// potentials according to the found distance labels. |
200 | 201 |
class ResidualDijkstra |
201 | 202 |
{ |
202 | 203 |
private: |
203 | 204 |
|
204 | 205 |
int _node_num; |
205 | 206 |
bool _geq; |
206 | 207 |
const IntVector &_first_out; |
207 | 208 |
const IntVector &_target; |
208 | 209 |
const CostVector &_cost; |
209 | 210 |
const ValueVector &_res_cap; |
210 | 211 |
const ValueVector &_excess; |
211 | 212 |
CostVector &_pi; |
212 | 213 |
IntVector &_pred; |
213 | 214 |
|
214 | 215 |
IntVector _proc_nodes; |
215 | 216 |
CostVector _dist; |
216 | 217 |
|
217 | 218 |
public: |
218 | 219 |
|
219 | 220 |
ResidualDijkstra(CapacityScaling& cs) : |
220 | 221 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
221 | 222 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
222 | 223 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
223 | 224 |
_pred(cs._pred), _dist(cs._node_num) |
224 | 225 |
{} |
225 | 226 |
|
226 | 227 |
int run(int s, Value delta = 1) { |
227 | 228 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
228 | 229 |
Heap heap(heap_cross_ref); |
229 | 230 |
heap.push(s, 0); |
230 | 231 |
_pred[s] = -1; |
231 | 232 |
_proc_nodes.clear(); |
232 | 233 |
|
233 | 234 |
// Process nodes |
234 | 235 |
while (!heap.empty() && _excess[heap.top()] > -delta) { |
235 | 236 |
int u = heap.top(), v; |
236 | 237 |
Cost d = heap.prio() + _pi[u], dn; |
237 | 238 |
_dist[u] = heap.prio(); |
238 | 239 |
_proc_nodes.push_back(u); |
239 | 240 |
heap.pop(); |
240 | 241 |
|
241 | 242 |
// Traverse outgoing residual arcs |
242 | 243 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
243 | 244 |
for (int a = _first_out[u]; a != last_out; ++a) { |
244 | 245 |
if (_res_cap[a] < delta) continue; |
245 | 246 |
v = _target[a]; |
246 | 247 |
switch (heap.state(v)) { |
247 | 248 |
case Heap::PRE_HEAP: |
248 | 249 |
heap.push(v, d + _cost[a] - _pi[v]); |
249 | 250 |
_pred[v] = a; |
250 | 251 |
break; |
251 | 252 |
case Heap::IN_HEAP: |
252 | 253 |
dn = d + _cost[a] - _pi[v]; |
253 | 254 |
if (dn < heap[v]) { |
254 | 255 |
heap.decrease(v, dn); |
255 | 256 |
_pred[v] = a; |
256 | 257 |
} |
257 | 258 |
break; |
258 | 259 |
case Heap::POST_HEAP: |
259 | 260 |
break; |
260 | 261 |
} |
261 | 262 |
} |
262 | 263 |
} |
263 | 264 |
if (heap.empty()) return -1; |
264 | 265 |
|
265 | 266 |
// Update potentials of processed nodes |
266 | 267 |
int t = heap.top(); |
267 | 268 |
Cost dt = heap.prio(); |
268 | 269 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) { |
269 | 270 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
270 | 271 |
} |
271 | 272 |
|
272 | 273 |
return t; |
273 | 274 |
} |
274 | 275 |
|
275 | 276 |
}; //class ResidualDijkstra |
276 | 277 |
|
277 | 278 |
public: |
278 | 279 |
|
279 | 280 |
/// \name Named Template Parameters |
280 | 281 |
/// @{ |
281 | 282 |
|
282 | 283 |
template <typename T> |
283 | 284 |
struct SetHeapTraits : public Traits { |
284 | 285 |
typedef T Heap; |
285 | 286 |
}; |
286 | 287 |
|
287 | 288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
288 | 289 |
/// \c Heap type. |
289 | 290 |
/// |
290 | 291 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
291 | 292 |
/// type, which is used for internal Dijkstra computations. |
292 | 293 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
293 | 294 |
/// its priority type must be \c Cost and its cross reference type |
294 | 295 |
/// must be \ref RangeMap "RangeMap<int>". |
295 | 296 |
template <typename T> |
296 | 297 |
struct SetHeap |
297 | 298 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > { |
298 | 299 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
299 | 300 |
}; |
300 | 301 |
|
301 | 302 |
/// @} |
302 | 303 |
|
303 | 304 |
public: |
304 | 305 |
|
305 | 306 |
/// \brief Constructor. |
306 | 307 |
/// |
307 | 308 |
/// The constructor of the class. |
308 | 309 |
/// |
309 | 310 |
/// \param graph The digraph the algorithm runs on. |
310 | 311 |
CapacityScaling(const GR& graph) : |
311 | 312 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
312 | 313 |
INF(std::numeric_limits<Value>::has_infinity ? |
313 | 314 |
std::numeric_limits<Value>::infinity() : |
314 | 315 |
std::numeric_limits<Value>::max()) |
315 | 316 |
{ |
316 | 317 |
// Check the number types |
317 | 318 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
318 | 319 |
"The flow type of CapacityScaling must be signed"); |
319 | 320 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
320 | 321 |
"The cost type of CapacityScaling must be signed"); |
321 | 322 |
|
322 | 323 |
// Reset data structures |
323 | 324 |
reset(); |
324 | 325 |
} |
325 | 326 |
|
326 | 327 |
/// \name Parameters |
327 | 328 |
/// The parameters of the algorithm can be specified using these |
328 | 329 |
/// functions. |
329 | 330 |
|
330 | 331 |
/// @{ |
331 | 332 |
|
332 | 333 |
/// \brief Set the lower bounds on the arcs. |
333 | 334 |
/// |
334 | 335 |
/// This function sets the lower bounds on the arcs. |
335 | 336 |
/// If it is not used before calling \ref run(), the lower bounds |
336 | 337 |
/// will be set to zero on all arcs. |
... | ... |
@@ -609,377 +610,377 @@ |
609 | 610 |
_first_out[i] = j; |
610 | 611 |
_first_out[_node_num] = k; |
611 | 612 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
612 | 613 |
int fi = _arc_idf[a]; |
613 | 614 |
int bi = _arc_idb[a]; |
614 | 615 |
_reverse[fi] = bi; |
615 | 616 |
_reverse[bi] = fi; |
616 | 617 |
} |
617 | 618 |
|
618 | 619 |
// Reset parameters |
619 | 620 |
resetParams(); |
620 | 621 |
return *this; |
621 | 622 |
} |
622 | 623 |
|
623 | 624 |
/// @} |
624 | 625 |
|
625 | 626 |
/// \name Query Functions |
626 | 627 |
/// The results of the algorithm can be obtained using these |
627 | 628 |
/// functions.\n |
628 | 629 |
/// The \ref run() function must be called before using them. |
629 | 630 |
|
630 | 631 |
/// @{ |
631 | 632 |
|
632 | 633 |
/// \brief Return the total cost of the found flow. |
633 | 634 |
/// |
634 | 635 |
/// This function returns the total cost of the found flow. |
635 | 636 |
/// Its complexity is O(e). |
636 | 637 |
/// |
637 | 638 |
/// \note The return type of the function can be specified as a |
638 | 639 |
/// template parameter. For example, |
639 | 640 |
/// \code |
640 | 641 |
/// cs.totalCost<double>(); |
641 | 642 |
/// \endcode |
642 | 643 |
/// It is useful if the total cost cannot be stored in the \c Cost |
643 | 644 |
/// type of the algorithm, which is the default return type of the |
644 | 645 |
/// function. |
645 | 646 |
/// |
646 | 647 |
/// \pre \ref run() must be called before using this function. |
647 | 648 |
template <typename Number> |
648 | 649 |
Number totalCost() const { |
649 | 650 |
Number c = 0; |
650 | 651 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
651 | 652 |
int i = _arc_idb[a]; |
652 | 653 |
c += static_cast<Number>(_res_cap[i]) * |
653 | 654 |
(-static_cast<Number>(_cost[i])); |
654 | 655 |
} |
655 | 656 |
return c; |
656 | 657 |
} |
657 | 658 |
|
658 | 659 |
#ifndef DOXYGEN |
659 | 660 |
Cost totalCost() const { |
660 | 661 |
return totalCost<Cost>(); |
661 | 662 |
} |
662 | 663 |
#endif |
663 | 664 |
|
664 | 665 |
/// \brief Return the flow on the given arc. |
665 | 666 |
/// |
666 | 667 |
/// This function returns the flow on the given arc. |
667 | 668 |
/// |
668 | 669 |
/// \pre \ref run() must be called before using this function. |
669 | 670 |
Value flow(const Arc& a) const { |
670 | 671 |
return _res_cap[_arc_idb[a]]; |
671 | 672 |
} |
672 | 673 |
|
673 | 674 |
/// \brief Return the flow map (the primal solution). |
674 | 675 |
/// |
675 | 676 |
/// This function copies the flow value on each arc into the given |
676 | 677 |
/// map. The \c Value type of the algorithm must be convertible to |
677 | 678 |
/// the \c Value type of the map. |
678 | 679 |
/// |
679 | 680 |
/// \pre \ref run() must be called before using this function. |
680 | 681 |
template <typename FlowMap> |
681 | 682 |
void flowMap(FlowMap &map) const { |
682 | 683 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
683 | 684 |
map.set(a, _res_cap[_arc_idb[a]]); |
684 | 685 |
} |
685 | 686 |
} |
686 | 687 |
|
687 | 688 |
/// \brief Return the potential (dual value) of the given node. |
688 | 689 |
/// |
689 | 690 |
/// This function returns the potential (dual value) of the |
690 | 691 |
/// given node. |
691 | 692 |
/// |
692 | 693 |
/// \pre \ref run() must be called before using this function. |
693 | 694 |
Cost potential(const Node& n) const { |
694 | 695 |
return _pi[_node_id[n]]; |
695 | 696 |
} |
696 | 697 |
|
697 | 698 |
/// \brief Return the potential map (the dual solution). |
698 | 699 |
/// |
699 | 700 |
/// This function copies the potential (dual value) of each node |
700 | 701 |
/// into the given map. |
701 | 702 |
/// The \c Cost type of the algorithm must be convertible to the |
702 | 703 |
/// \c Value type of the map. |
703 | 704 |
/// |
704 | 705 |
/// \pre \ref run() must be called before using this function. |
705 | 706 |
template <typename PotentialMap> |
706 | 707 |
void potentialMap(PotentialMap &map) const { |
707 | 708 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
708 | 709 |
map.set(n, _pi[_node_id[n]]); |
709 | 710 |
} |
710 | 711 |
} |
711 | 712 |
|
712 | 713 |
/// @} |
713 | 714 |
|
714 | 715 |
private: |
715 | 716 |
|
716 | 717 |
// Initialize the algorithm |
717 | 718 |
ProblemType init() { |
718 | 719 |
if (_node_num <= 1) return INFEASIBLE; |
719 | 720 |
|
720 | 721 |
// Check the sum of supply values |
721 | 722 |
_sum_supply = 0; |
722 | 723 |
for (int i = 0; i != _root; ++i) { |
723 | 724 |
_sum_supply += _supply[i]; |
724 | 725 |
} |
725 | 726 |
if (_sum_supply > 0) return INFEASIBLE; |
726 | 727 |
|
727 | 728 |
// Initialize vectors |
728 | 729 |
for (int i = 0; i != _root; ++i) { |
729 | 730 |
_pi[i] = 0; |
730 | 731 |
_excess[i] = _supply[i]; |
731 | 732 |
} |
732 | 733 |
|
733 | 734 |
// Remove non-zero lower bounds |
734 | 735 |
const Value MAX = std::numeric_limits<Value>::max(); |
735 | 736 |
int last_out; |
736 | 737 |
if (_have_lower) { |
737 | 738 |
for (int i = 0; i != _root; ++i) { |
738 | 739 |
last_out = _first_out[i+1]; |
739 | 740 |
for (int j = _first_out[i]; j != last_out; ++j) { |
740 | 741 |
if (_forward[j]) { |
741 | 742 |
Value c = _lower[j]; |
742 | 743 |
if (c >= 0) { |
743 | 744 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
744 | 745 |
} else { |
745 | 746 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
746 | 747 |
} |
747 | 748 |
_excess[i] -= c; |
748 | 749 |
_excess[_target[j]] += c; |
749 | 750 |
} else { |
750 | 751 |
_res_cap[j] = 0; |
751 | 752 |
} |
752 | 753 |
} |
753 | 754 |
} |
754 | 755 |
} else { |
755 | 756 |
for (int j = 0; j != _res_arc_num; ++j) { |
756 | 757 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
757 | 758 |
} |
758 | 759 |
} |
759 | 760 |
|
760 | 761 |
// Handle negative costs |
761 | 762 |
for (int i = 0; i != _root; ++i) { |
762 | 763 |
last_out = _first_out[i+1] - 1; |
763 | 764 |
for (int j = _first_out[i]; j != last_out; ++j) { |
764 | 765 |
Value rc = _res_cap[j]; |
765 | 766 |
if (_cost[j] < 0 && rc > 0) { |
766 | 767 |
if (rc >= MAX) return UNBOUNDED; |
767 | 768 |
_excess[i] -= rc; |
768 | 769 |
_excess[_target[j]] += rc; |
769 | 770 |
_res_cap[j] = 0; |
770 | 771 |
_res_cap[_reverse[j]] += rc; |
771 | 772 |
} |
772 | 773 |
} |
773 | 774 |
} |
774 | 775 |
|
775 | 776 |
// Handle GEQ supply type |
776 | 777 |
if (_sum_supply < 0) { |
777 | 778 |
_pi[_root] = 0; |
778 | 779 |
_excess[_root] = -_sum_supply; |
779 | 780 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
780 | 781 |
int ra = _reverse[a]; |
781 | 782 |
_res_cap[a] = -_sum_supply + 1; |
782 | 783 |
_res_cap[ra] = 0; |
783 | 784 |
_cost[a] = 0; |
784 | 785 |
_cost[ra] = 0; |
785 | 786 |
} |
786 | 787 |
} else { |
787 | 788 |
_pi[_root] = 0; |
788 | 789 |
_excess[_root] = 0; |
789 | 790 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
790 | 791 |
int ra = _reverse[a]; |
791 | 792 |
_res_cap[a] = 1; |
792 | 793 |
_res_cap[ra] = 0; |
793 | 794 |
_cost[a] = 0; |
794 | 795 |
_cost[ra] = 0; |
795 | 796 |
} |
796 | 797 |
} |
797 | 798 |
|
798 | 799 |
// Initialize delta value |
799 | 800 |
if (_factor > 1) { |
800 | 801 |
// With scaling |
801 |
Value max_sup = 0, max_dem = 0; |
|
802 |
for (int i = 0; i != _node_num; ++i) { |
|
802 |
Value max_sup = 0, max_dem = 0, max_cap = 0; |
|
803 |
for (int i = 0; i != _root; ++i) { |
|
803 | 804 |
Value ex = _excess[i]; |
804 | 805 |
if ( ex > max_sup) max_sup = ex; |
805 | 806 |
if (-ex > max_dem) max_dem = -ex; |
806 |
} |
|
807 |
Value max_cap = 0; |
|
808 |
for (int j = 0; j != _res_arc_num; ++j) { |
|
809 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
|
807 |
int last_out = _first_out[i+1] - 1; |
|
808 |
for (int j = _first_out[i]; j != last_out; ++j) { |
|
809 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
|
810 |
} |
|
810 | 811 |
} |
811 | 812 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
812 | 813 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
813 | 814 |
} else { |
814 | 815 |
// Without scaling |
815 | 816 |
_delta = 1; |
816 | 817 |
} |
817 | 818 |
|
818 | 819 |
return OPTIMAL; |
819 | 820 |
} |
820 | 821 |
|
821 | 822 |
ProblemType start() { |
822 | 823 |
// Execute the algorithm |
823 | 824 |
ProblemType pt; |
824 | 825 |
if (_delta > 1) |
825 | 826 |
pt = startWithScaling(); |
826 | 827 |
else |
827 | 828 |
pt = startWithoutScaling(); |
828 | 829 |
|
829 | 830 |
// Handle non-zero lower bounds |
830 | 831 |
if (_have_lower) { |
831 | 832 |
int limit = _first_out[_root]; |
832 | 833 |
for (int j = 0; j != limit; ++j) { |
833 | 834 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
834 | 835 |
} |
835 | 836 |
} |
836 | 837 |
|
837 | 838 |
// Shift potentials if necessary |
838 | 839 |
Cost pr = _pi[_root]; |
839 | 840 |
if (_sum_supply < 0 || pr > 0) { |
840 | 841 |
for (int i = 0; i != _node_num; ++i) { |
841 | 842 |
_pi[i] -= pr; |
842 | 843 |
} |
843 | 844 |
} |
844 | 845 |
|
845 | 846 |
return pt; |
846 | 847 |
} |
847 | 848 |
|
848 | 849 |
// Execute the capacity scaling algorithm |
849 | 850 |
ProblemType startWithScaling() { |
850 | 851 |
// Perform capacity scaling phases |
851 | 852 |
int s, t; |
852 | 853 |
ResidualDijkstra _dijkstra(*this); |
853 | 854 |
while (true) { |
854 | 855 |
// Saturate all arcs not satisfying the optimality condition |
855 | 856 |
int last_out; |
856 | 857 |
for (int u = 0; u != _node_num; ++u) { |
857 | 858 |
last_out = _sum_supply < 0 ? |
858 | 859 |
_first_out[u+1] : _first_out[u+1] - 1; |
859 | 860 |
for (int a = _first_out[u]; a != last_out; ++a) { |
860 | 861 |
int v = _target[a]; |
861 | 862 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
862 | 863 |
Value rc = _res_cap[a]; |
863 | 864 |
if (c < 0 && rc >= _delta) { |
864 | 865 |
_excess[u] -= rc; |
865 | 866 |
_excess[v] += rc; |
866 | 867 |
_res_cap[a] = 0; |
867 | 868 |
_res_cap[_reverse[a]] += rc; |
868 | 869 |
} |
869 | 870 |
} |
870 | 871 |
} |
871 | 872 |
|
872 | 873 |
// Find excess nodes and deficit nodes |
873 | 874 |
_excess_nodes.clear(); |
874 | 875 |
_deficit_nodes.clear(); |
875 | 876 |
for (int u = 0; u != _node_num; ++u) { |
876 | 877 |
Value ex = _excess[u]; |
877 | 878 |
if (ex >= _delta) _excess_nodes.push_back(u); |
878 | 879 |
if (ex <= -_delta) _deficit_nodes.push_back(u); |
879 | 880 |
} |
880 | 881 |
int next_node = 0, next_def_node = 0; |
881 | 882 |
|
882 | 883 |
// Find augmenting shortest paths |
883 | 884 |
while (next_node < int(_excess_nodes.size())) { |
884 | 885 |
// Check deficit nodes |
885 | 886 |
if (_delta > 1) { |
886 | 887 |
bool delta_deficit = false; |
887 | 888 |
for ( ; next_def_node < int(_deficit_nodes.size()); |
888 | 889 |
++next_def_node ) { |
889 | 890 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) { |
890 | 891 |
delta_deficit = true; |
891 | 892 |
break; |
892 | 893 |
} |
893 | 894 |
} |
894 | 895 |
if (!delta_deficit) break; |
895 | 896 |
} |
896 | 897 |
|
897 | 898 |
// Run Dijkstra in the residual network |
898 | 899 |
s = _excess_nodes[next_node]; |
899 | 900 |
if ((t = _dijkstra.run(s, _delta)) == -1) { |
900 | 901 |
if (_delta > 1) { |
901 | 902 |
++next_node; |
902 | 903 |
continue; |
903 | 904 |
} |
904 | 905 |
return INFEASIBLE; |
905 | 906 |
} |
906 | 907 |
|
907 | 908 |
// Augment along a shortest path from s to t |
908 | 909 |
Value d = std::min(_excess[s], -_excess[t]); |
909 | 910 |
int u = t; |
910 | 911 |
int a; |
911 | 912 |
if (d > _delta) { |
912 | 913 |
while ((a = _pred[u]) != -1) { |
913 | 914 |
if (_res_cap[a] < d) d = _res_cap[a]; |
914 | 915 |
u = _source[a]; |
915 | 916 |
} |
916 | 917 |
} |
917 | 918 |
u = t; |
918 | 919 |
while ((a = _pred[u]) != -1) { |
919 | 920 |
_res_cap[a] -= d; |
920 | 921 |
_res_cap[_reverse[a]] += d; |
921 | 922 |
u = _source[a]; |
922 | 923 |
} |
923 | 924 |
_excess[s] -= d; |
924 | 925 |
_excess[t] += d; |
925 | 926 |
|
926 | 927 |
if (_excess[s] < _delta) ++next_node; |
927 | 928 |
} |
928 | 929 |
|
929 | 930 |
if (_delta == 1) break; |
930 | 931 |
_delta = _delta <= _factor ? 1 : _delta / _factor; |
931 | 932 |
} |
932 | 933 |
|
933 | 934 |
return OPTIMAL; |
934 | 935 |
} |
935 | 936 |
|
936 | 937 |
// Execute the successive shortest path algorithm |
937 | 938 |
ProblemType startWithoutScaling() { |
938 | 939 |
// Find excess nodes |
939 | 940 |
_excess_nodes.clear(); |
940 | 941 |
for (int i = 0; i != _node_num; ++i) { |
941 | 942 |
if (_excess[i] > 0) _excess_nodes.push_back(i); |
942 | 943 |
} |
943 | 944 |
if (_excess_nodes.size() == 0) return OPTIMAL; |
944 | 945 |
int next_node = 0; |
945 | 946 |
|
946 | 947 |
// Find shortest paths |
947 | 948 |
int s, t; |
948 | 949 |
ResidualDijkstra _dijkstra(*this); |
949 | 950 |
while ( _excess[_excess_nodes[next_node]] > 0 || |
950 | 951 |
++next_node < int(_excess_nodes.size()) ) |
951 | 952 |
{ |
952 | 953 |
// Run Dijkstra in the residual network |
953 | 954 |
s = _excess_nodes[next_node]; |
954 | 955 |
if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE; |
955 | 956 |
|
956 | 957 |
// Augment along a shortest path from s to t |
957 | 958 |
Value d = std::min(_excess[s], -_excess[t]); |
958 | 959 |
int u = t; |
959 | 960 |
int a; |
960 | 961 |
if (d > 1) { |
961 | 962 |
while ((a = _pred[u]) != -1) { |
962 | 963 |
if (_res_cap[a] < d) d = _res_cap[a]; |
963 | 964 |
u = _source[a]; |
964 | 965 |
} |
965 | 966 |
} |
966 | 967 |
u = t; |
967 | 968 |
while ((a = _pred[u]) != -1) { |
968 | 969 |
_res_cap[a] -= d; |
969 | 970 |
_res_cap[_reverse[a]] += d; |
970 | 971 |
u = _source[a]; |
971 | 972 |
} |
972 | 973 |
_excess[s] -= d; |
973 | 974 |
_excess[t] += d; |
974 | 975 |
} |
975 | 976 |
|
976 | 977 |
return OPTIMAL; |
977 | 978 |
} |
978 | 979 |
|
979 | 980 |
}; //class CapacityScaling |
980 | 981 |
|
981 | 982 |
///@} |
982 | 983 |
|
983 | 984 |
} //namespace lemon |
984 | 985 |
|
985 | 986 |
#endif //LEMON_CAPACITY_SCALING_H |
... | ... |
@@ -12,459 +12,467 @@ |
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_COST_SCALING_H |
20 | 20 |
#define LEMON_COST_SCALING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <deque> |
28 | 28 |
#include <limits> |
29 | 29 |
|
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/circulation.h> |
35 | 35 |
#include <lemon/bellman_ford.h> |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \brief Default traits class of CostScaling algorithm. |
40 | 40 |
/// |
41 | 41 |
/// Default traits class of CostScaling algorithm. |
42 | 42 |
/// \tparam GR Digraph type. |
43 | 43 |
/// \tparam V The number type used for flow amounts, capacity bounds |
44 | 44 |
/// and supply values. By default it is \c int. |
45 | 45 |
/// \tparam C The number type used for costs and potentials. |
46 | 46 |
/// By default it is the same as \c V. |
47 | 47 |
#ifdef DOXYGEN |
48 | 48 |
template <typename GR, typename V = int, typename C = V> |
49 | 49 |
#else |
50 | 50 |
template < typename GR, typename V = int, typename C = V, |
51 | 51 |
bool integer = std::numeric_limits<C>::is_integer > |
52 | 52 |
#endif |
53 | 53 |
struct CostScalingDefaultTraits |
54 | 54 |
{ |
55 | 55 |
/// The type of the digraph |
56 | 56 |
typedef GR Digraph; |
57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
58 | 58 |
typedef V Value; |
59 | 59 |
/// The type of the arc costs |
60 | 60 |
typedef C Cost; |
61 | 61 |
|
62 | 62 |
/// \brief The large cost type used for internal computations |
63 | 63 |
/// |
64 | 64 |
/// The large cost type used for internal computations. |
65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
66 | 66 |
/// otherwise it is \c double. |
67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
68 | 68 |
typedef double LargeCost; |
69 | 69 |
}; |
70 | 70 |
|
71 | 71 |
// Default traits class for integer cost types |
72 | 72 |
template <typename GR, typename V, typename C> |
73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
74 | 74 |
{ |
75 | 75 |
typedef GR Digraph; |
76 | 76 |
typedef V Value; |
77 | 77 |
typedef C Cost; |
78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
79 | 79 |
typedef long long LargeCost; |
80 | 80 |
#else |
81 | 81 |
typedef long LargeCost; |
82 | 82 |
#endif |
83 | 83 |
}; |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// \addtogroup min_cost_flow_algs |
87 | 87 |
/// @{ |
88 | 88 |
|
89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
91 | 91 |
/// |
92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
95 | 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
99 | 99 |
/// |
100 | 100 |
/// Most of the parameters of the problem (except for the digraph) |
101 | 101 |
/// can be given using separate functions, and the algorithm can be |
102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
103 | 103 |
/// specified, then default values will be used. |
104 | 104 |
/// |
105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
107 | 107 |
/// and supply values in the algorithm. By default, it is \c int. |
108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
109 | 109 |
/// algorithm. By default, it is the same as \c V. |
110 | 110 |
/// \tparam TR The traits class that defines various types used by the |
111 | 111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
112 | 112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
113 | 113 |
/// In most cases, this parameter should not be set directly, |
114 | 114 |
/// consider to use the named template parameters instead. |
115 | 115 |
/// |
116 | 116 |
/// \warning Both number types must be signed and all input data must |
117 | 117 |
/// be integer. |
118 | 118 |
/// \warning This algorithm does not support negative costs for such |
119 | 119 |
/// arcs that have infinite upper bound. |
120 | 120 |
/// |
121 | 121 |
/// \note %CostScaling provides three different internal methods, |
122 | 122 |
/// from which the most efficient one is used by default. |
123 | 123 |
/// For more information, see \ref Method. |
124 | 124 |
#ifdef DOXYGEN |
125 | 125 |
template <typename GR, typename V, typename C, typename TR> |
126 | 126 |
#else |
127 | 127 |
template < typename GR, typename V = int, typename C = V, |
128 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
129 | 129 |
#endif |
130 | 130 |
class CostScaling |
131 | 131 |
{ |
132 | 132 |
public: |
133 | 133 |
|
134 | 134 |
/// The type of the digraph |
135 | 135 |
typedef typename TR::Digraph Digraph; |
136 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
137 | 137 |
typedef typename TR::Value Value; |
138 | 138 |
/// The type of the arc costs |
139 | 139 |
typedef typename TR::Cost Cost; |
140 | 140 |
|
141 | 141 |
/// \brief The large cost type |
142 | 142 |
/// |
143 | 143 |
/// The large cost type used for internal computations. |
144 | 144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
145 | 145 |
/// otherwise it is \c double. |
146 | 146 |
typedef typename TR::LargeCost LargeCost; |
147 | 147 |
|
148 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
149 | 149 |
typedef TR Traits; |
150 | 150 |
|
151 | 151 |
public: |
152 | 152 |
|
153 | 153 |
/// \brief Problem type constants for the \c run() function. |
154 | 154 |
/// |
155 | 155 |
/// Enum type containing the problem type constants that can be |
156 | 156 |
/// returned by the \ref run() function of the algorithm. |
157 | 157 |
enum ProblemType { |
158 | 158 |
/// The problem has no feasible solution (flow). |
159 | 159 |
INFEASIBLE, |
160 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
161 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
162 | 162 |
/// potentials (primal and dual solutions). |
163 | 163 |
OPTIMAL, |
164 | 164 |
/// The digraph contains an arc of negative cost and infinite |
165 | 165 |
/// upper bound. It means that the objective function is unbounded |
166 | 166 |
/// on that arc, however, note that it could actually be bounded |
167 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
168 | 168 |
/// these cases. |
169 | 169 |
UNBOUNDED |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
/// \brief Constants for selecting the internal method. |
173 | 173 |
/// |
174 | 174 |
/// Enum type containing constants for selecting the internal method |
175 | 175 |
/// for the \ref run() function. |
176 | 176 |
/// |
177 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
178 | 178 |
/// in their base operations, which are used in conjunction with the |
179 | 179 |
/// relabel operation. |
180 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
181 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
182 | 182 |
/// the most efficient and the most robust on various test inputs. |
183 | 183 |
/// However, the other methods can be selected using the \ref run() |
184 | 184 |
/// function with the proper parameter. |
185 | 185 |
enum Method { |
186 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
187 | 187 |
/// admissible arc at once. |
188 | 188 |
PUSH, |
189 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
190 | 190 |
/// paths from a node with excess to a node with deficit. |
191 | 191 |
AUGMENT, |
192 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
193 | 193 |
/// admissible paths started from a node with excess, but the |
194 | 194 |
/// lengths of these paths are limited. This method can be viewed |
195 | 195 |
/// as a combined version of the previous two operations. |
196 | 196 |
PARTIAL_AUGMENT |
197 | 197 |
}; |
198 | 198 |
|
199 | 199 |
private: |
200 | 200 |
|
201 | 201 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
202 | 202 |
|
203 | 203 |
typedef std::vector<int> IntVector; |
204 |
typedef std::vector<char> BoolVector; |
|
205 | 204 |
typedef std::vector<Value> ValueVector; |
206 | 205 |
typedef std::vector<Cost> CostVector; |
207 | 206 |
typedef std::vector<LargeCost> LargeCostVector; |
207 |
typedef std::vector<char> BoolVector; |
|
208 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
|
208 | 209 |
|
209 | 210 |
private: |
210 | 211 |
|
211 | 212 |
template <typename KT, typename VT> |
212 | 213 |
class StaticVectorMap { |
213 | 214 |
public: |
214 | 215 |
typedef KT Key; |
215 | 216 |
typedef VT Value; |
216 | 217 |
|
217 | 218 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
218 | 219 |
|
219 | 220 |
const Value& operator[](const Key& key) const { |
220 | 221 |
return _v[StaticDigraph::id(key)]; |
221 | 222 |
} |
222 | 223 |
|
223 | 224 |
Value& operator[](const Key& key) { |
224 | 225 |
return _v[StaticDigraph::id(key)]; |
225 | 226 |
} |
226 | 227 |
|
227 | 228 |
void set(const Key& key, const Value& val) { |
228 | 229 |
_v[StaticDigraph::id(key)] = val; |
229 | 230 |
} |
230 | 231 |
|
231 | 232 |
private: |
232 | 233 |
std::vector<Value>& _v; |
233 | 234 |
}; |
234 | 235 |
|
235 | 236 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
236 | 237 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
237 | 238 |
|
238 | 239 |
private: |
239 | 240 |
|
240 | 241 |
// Data related to the underlying digraph |
241 | 242 |
const GR &_graph; |
242 | 243 |
int _node_num; |
243 | 244 |
int _arc_num; |
244 | 245 |
int _res_node_num; |
245 | 246 |
int _res_arc_num; |
246 | 247 |
int _root; |
247 | 248 |
|
248 | 249 |
// Parameters of the problem |
249 | 250 |
bool _have_lower; |
250 | 251 |
Value _sum_supply; |
252 |
int _sup_node_num; |
|
251 | 253 |
|
252 | 254 |
// Data structures for storing the digraph |
253 | 255 |
IntNodeMap _node_id; |
254 | 256 |
IntArcMap _arc_idf; |
255 | 257 |
IntArcMap _arc_idb; |
256 | 258 |
IntVector _first_out; |
257 | 259 |
BoolVector _forward; |
258 | 260 |
IntVector _source; |
259 | 261 |
IntVector _target; |
260 | 262 |
IntVector _reverse; |
261 | 263 |
|
262 | 264 |
// Node and arc data |
263 | 265 |
ValueVector _lower; |
264 | 266 |
ValueVector _upper; |
265 | 267 |
CostVector _scost; |
266 | 268 |
ValueVector _supply; |
267 | 269 |
|
268 | 270 |
ValueVector _res_cap; |
269 | 271 |
LargeCostVector _cost; |
270 | 272 |
LargeCostVector _pi; |
271 | 273 |
ValueVector _excess; |
272 | 274 |
IntVector _next_out; |
273 | 275 |
std::deque<int> _active_nodes; |
274 | 276 |
|
275 | 277 |
// Data for scaling |
276 | 278 |
LargeCost _epsilon; |
277 | 279 |
int _alpha; |
278 | 280 |
|
281 |
IntVector _buckets; |
|
282 |
IntVector _bucket_next; |
|
283 |
IntVector _bucket_prev; |
|
284 |
IntVector _rank; |
|
285 |
int _max_rank; |
|
286 |
|
|
279 | 287 |
// Data for a StaticDigraph structure |
280 | 288 |
typedef std::pair<int, int> IntPair; |
281 | 289 |
StaticDigraph _sgr; |
282 | 290 |
std::vector<IntPair> _arc_vec; |
283 | 291 |
std::vector<LargeCost> _cost_vec; |
284 | 292 |
LargeCostArcMap _cost_map; |
285 | 293 |
LargeCostNodeMap _pi_map; |
286 | 294 |
|
287 | 295 |
public: |
288 | 296 |
|
289 | 297 |
/// \brief Constant for infinite upper bounds (capacities). |
290 | 298 |
/// |
291 | 299 |
/// Constant for infinite upper bounds (capacities). |
292 | 300 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
293 | 301 |
/// \c std::numeric_limits<Value>::max() otherwise. |
294 | 302 |
const Value INF; |
295 | 303 |
|
296 | 304 |
public: |
297 | 305 |
|
298 | 306 |
/// \name Named Template Parameters |
299 | 307 |
/// @{ |
300 | 308 |
|
301 | 309 |
template <typename T> |
302 | 310 |
struct SetLargeCostTraits : public Traits { |
303 | 311 |
typedef T LargeCost; |
304 | 312 |
}; |
305 | 313 |
|
306 | 314 |
/// \brief \ref named-templ-param "Named parameter" for setting |
307 | 315 |
/// \c LargeCost type. |
308 | 316 |
/// |
309 | 317 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
310 | 318 |
/// type, which is used for internal computations in the algorithm. |
311 | 319 |
/// \c Cost must be convertible to \c LargeCost. |
312 | 320 |
template <typename T> |
313 | 321 |
struct SetLargeCost |
314 | 322 |
: public CostScaling<GR, V, C, SetLargeCostTraits<T> > { |
315 | 323 |
typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
316 | 324 |
}; |
317 | 325 |
|
318 | 326 |
/// @} |
319 | 327 |
|
320 | 328 |
public: |
321 | 329 |
|
322 | 330 |
/// \brief Constructor. |
323 | 331 |
/// |
324 | 332 |
/// The constructor of the class. |
325 | 333 |
/// |
326 | 334 |
/// \param graph The digraph the algorithm runs on. |
327 | 335 |
CostScaling(const GR& graph) : |
328 | 336 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
329 | 337 |
_cost_map(_cost_vec), _pi_map(_pi), |
330 | 338 |
INF(std::numeric_limits<Value>::has_infinity ? |
331 | 339 |
std::numeric_limits<Value>::infinity() : |
332 | 340 |
std::numeric_limits<Value>::max()) |
333 | 341 |
{ |
334 | 342 |
// Check the number types |
335 | 343 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
336 | 344 |
"The flow type of CostScaling must be signed"); |
337 | 345 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
338 | 346 |
"The cost type of CostScaling must be signed"); |
339 | 347 |
|
340 | 348 |
// Reset data structures |
341 | 349 |
reset(); |
342 | 350 |
} |
343 | 351 |
|
344 | 352 |
/// \name Parameters |
345 | 353 |
/// The parameters of the algorithm can be specified using these |
346 | 354 |
/// functions. |
347 | 355 |
|
348 | 356 |
/// @{ |
349 | 357 |
|
350 | 358 |
/// \brief Set the lower bounds on the arcs. |
351 | 359 |
/// |
352 | 360 |
/// This function sets the lower bounds on the arcs. |
353 | 361 |
/// If it is not used before calling \ref run(), the lower bounds |
354 | 362 |
/// will be set to zero on all arcs. |
355 | 363 |
/// |
356 | 364 |
/// \param map An arc map storing the lower bounds. |
357 | 365 |
/// Its \c Value type must be convertible to the \c Value type |
358 | 366 |
/// of the algorithm. |
359 | 367 |
/// |
360 | 368 |
/// \return <tt>(*this)</tt> |
361 | 369 |
template <typename LowerMap> |
362 | 370 |
CostScaling& lowerMap(const LowerMap& map) { |
363 | 371 |
_have_lower = true; |
364 | 372 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
365 | 373 |
_lower[_arc_idf[a]] = map[a]; |
366 | 374 |
_lower[_arc_idb[a]] = map[a]; |
367 | 375 |
} |
368 | 376 |
return *this; |
369 | 377 |
} |
370 | 378 |
|
371 | 379 |
/// \brief Set the upper bounds (capacities) on the arcs. |
372 | 380 |
/// |
373 | 381 |
/// This function sets the upper bounds (capacities) on the arcs. |
374 | 382 |
/// If it is not used before calling \ref run(), the upper bounds |
375 | 383 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
376 | 384 |
/// unbounded from above). |
377 | 385 |
/// |
378 | 386 |
/// \param map An arc map storing the upper bounds. |
379 | 387 |
/// Its \c Value type must be convertible to the \c Value type |
380 | 388 |
/// of the algorithm. |
381 | 389 |
/// |
382 | 390 |
/// \return <tt>(*this)</tt> |
383 | 391 |
template<typename UpperMap> |
384 | 392 |
CostScaling& upperMap(const UpperMap& map) { |
385 | 393 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
386 | 394 |
_upper[_arc_idf[a]] = map[a]; |
387 | 395 |
} |
388 | 396 |
return *this; |
389 | 397 |
} |
390 | 398 |
|
391 | 399 |
/// \brief Set the costs of the arcs. |
392 | 400 |
/// |
393 | 401 |
/// This function sets the costs of the arcs. |
394 | 402 |
/// If it is not used before calling \ref run(), the costs |
395 | 403 |
/// will be set to \c 1 on all arcs. |
396 | 404 |
/// |
397 | 405 |
/// \param map An arc map storing the costs. |
398 | 406 |
/// Its \c Value type must be convertible to the \c Cost type |
399 | 407 |
/// of the algorithm. |
400 | 408 |
/// |
401 | 409 |
/// \return <tt>(*this)</tt> |
402 | 410 |
template<typename CostMap> |
403 | 411 |
CostScaling& costMap(const CostMap& map) { |
404 | 412 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
405 | 413 |
_scost[_arc_idf[a]] = map[a]; |
406 | 414 |
_scost[_arc_idb[a]] = -map[a]; |
407 | 415 |
} |
408 | 416 |
return *this; |
409 | 417 |
} |
410 | 418 |
|
411 | 419 |
/// \brief Set the supply values of the nodes. |
412 | 420 |
/// |
413 | 421 |
/// This function sets the supply values of the nodes. |
414 | 422 |
/// If neither this function nor \ref stSupply() is used before |
415 | 423 |
/// calling \ref run(), the supply of each node will be set to zero. |
416 | 424 |
/// |
417 | 425 |
/// \param map A node map storing the supply values. |
418 | 426 |
/// Its \c Value type must be convertible to the \c Value type |
419 | 427 |
/// of the algorithm. |
420 | 428 |
/// |
421 | 429 |
/// \return <tt>(*this)</tt> |
422 | 430 |
template<typename SupplyMap> |
423 | 431 |
CostScaling& supplyMap(const SupplyMap& map) { |
424 | 432 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
425 | 433 |
_supply[_node_id[n]] = map[n]; |
426 | 434 |
} |
427 | 435 |
return *this; |
428 | 436 |
} |
429 | 437 |
|
430 | 438 |
/// \brief Set single source and target nodes and a supply value. |
431 | 439 |
/// |
432 | 440 |
/// This function sets a single source node and a single target node |
433 | 441 |
/// and the required flow value. |
434 | 442 |
/// If neither this function nor \ref supplyMap() is used before |
435 | 443 |
/// calling \ref run(), the supply of each node will be set to zero. |
436 | 444 |
/// |
437 | 445 |
/// Using this function has the same effect as using \ref supplyMap() |
438 | 446 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
439 | 447 |
/// assigned to \c t and all other nodes have zero supply value. |
440 | 448 |
/// |
441 | 449 |
/// \param s The source node. |
442 | 450 |
/// \param t The target node. |
443 | 451 |
/// \param k The required amount of flow from node \c s to node \c t |
444 | 452 |
/// (i.e. the supply of \c s and the demand of \c t). |
445 | 453 |
/// |
446 | 454 |
/// \return <tt>(*this)</tt> |
447 | 455 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) { |
448 | 456 |
for (int i = 0; i != _res_node_num; ++i) { |
449 | 457 |
_supply[i] = 0; |
450 | 458 |
} |
451 | 459 |
_supply[_node_id[s]] = k; |
452 | 460 |
_supply[_node_id[t]] = -k; |
453 | 461 |
return *this; |
454 | 462 |
} |
455 | 463 |
|
456 | 464 |
/// @} |
457 | 465 |
|
458 | 466 |
/// \name Execution control |
459 | 467 |
/// The algorithm can be executed using \ref run(). |
460 | 468 |
|
461 | 469 |
/// @{ |
462 | 470 |
|
463 | 471 |
/// \brief Run the algorithm. |
464 | 472 |
/// |
465 | 473 |
/// This function runs the algorithm. |
466 | 474 |
/// The paramters can be specified using functions \ref lowerMap(), |
467 | 475 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
468 | 476 |
/// For example, |
469 | 477 |
/// \code |
470 | 478 |
/// CostScaling<ListDigraph> cs(graph); |
... | ... |
@@ -639,558 +647,666 @@ |
639 | 647 |
} |
640 | 648 |
|
641 | 649 |
// Reset parameters |
642 | 650 |
resetParams(); |
643 | 651 |
return *this; |
644 | 652 |
} |
645 | 653 |
|
646 | 654 |
/// @} |
647 | 655 |
|
648 | 656 |
/// \name Query Functions |
649 | 657 |
/// The results of the algorithm can be obtained using these |
650 | 658 |
/// functions.\n |
651 | 659 |
/// The \ref run() function must be called before using them. |
652 | 660 |
|
653 | 661 |
/// @{ |
654 | 662 |
|
655 | 663 |
/// \brief Return the total cost of the found flow. |
656 | 664 |
/// |
657 | 665 |
/// This function returns the total cost of the found flow. |
658 | 666 |
/// Its complexity is O(e). |
659 | 667 |
/// |
660 | 668 |
/// \note The return type of the function can be specified as a |
661 | 669 |
/// template parameter. For example, |
662 | 670 |
/// \code |
663 | 671 |
/// cs.totalCost<double>(); |
664 | 672 |
/// \endcode |
665 | 673 |
/// It is useful if the total cost cannot be stored in the \c Cost |
666 | 674 |
/// type of the algorithm, which is the default return type of the |
667 | 675 |
/// function. |
668 | 676 |
/// |
669 | 677 |
/// \pre \ref run() must be called before using this function. |
670 | 678 |
template <typename Number> |
671 | 679 |
Number totalCost() const { |
672 | 680 |
Number c = 0; |
673 | 681 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
674 | 682 |
int i = _arc_idb[a]; |
675 | 683 |
c += static_cast<Number>(_res_cap[i]) * |
676 | 684 |
(-static_cast<Number>(_scost[i])); |
677 | 685 |
} |
678 | 686 |
return c; |
679 | 687 |
} |
680 | 688 |
|
681 | 689 |
#ifndef DOXYGEN |
682 | 690 |
Cost totalCost() const { |
683 | 691 |
return totalCost<Cost>(); |
684 | 692 |
} |
685 | 693 |
#endif |
686 | 694 |
|
687 | 695 |
/// \brief Return the flow on the given arc. |
688 | 696 |
/// |
689 | 697 |
/// This function returns the flow on the given arc. |
690 | 698 |
/// |
691 | 699 |
/// \pre \ref run() must be called before using this function. |
692 | 700 |
Value flow(const Arc& a) const { |
693 | 701 |
return _res_cap[_arc_idb[a]]; |
694 | 702 |
} |
695 | 703 |
|
696 | 704 |
/// \brief Return the flow map (the primal solution). |
697 | 705 |
/// |
698 | 706 |
/// This function copies the flow value on each arc into the given |
699 | 707 |
/// map. The \c Value type of the algorithm must be convertible to |
700 | 708 |
/// the \c Value type of the map. |
701 | 709 |
/// |
702 | 710 |
/// \pre \ref run() must be called before using this function. |
703 | 711 |
template <typename FlowMap> |
704 | 712 |
void flowMap(FlowMap &map) const { |
705 | 713 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
706 | 714 |
map.set(a, _res_cap[_arc_idb[a]]); |
707 | 715 |
} |
708 | 716 |
} |
709 | 717 |
|
710 | 718 |
/// \brief Return the potential (dual value) of the given node. |
711 | 719 |
/// |
712 | 720 |
/// This function returns the potential (dual value) of the |
713 | 721 |
/// given node. |
714 | 722 |
/// |
715 | 723 |
/// \pre \ref run() must be called before using this function. |
716 | 724 |
Cost potential(const Node& n) const { |
717 | 725 |
return static_cast<Cost>(_pi[_node_id[n]]); |
718 | 726 |
} |
719 | 727 |
|
720 | 728 |
/// \brief Return the potential map (the dual solution). |
721 | 729 |
/// |
722 | 730 |
/// This function copies the potential (dual value) of each node |
723 | 731 |
/// into the given map. |
724 | 732 |
/// The \c Cost type of the algorithm must be convertible to the |
725 | 733 |
/// \c Value type of the map. |
726 | 734 |
/// |
727 | 735 |
/// \pre \ref run() must be called before using this function. |
728 | 736 |
template <typename PotentialMap> |
729 | 737 |
void potentialMap(PotentialMap &map) const { |
730 | 738 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
731 | 739 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
732 | 740 |
} |
733 | 741 |
} |
734 | 742 |
|
735 | 743 |
/// @} |
736 | 744 |
|
737 | 745 |
private: |
738 | 746 |
|
739 | 747 |
// Initialize the algorithm |
740 | 748 |
ProblemType init() { |
741 | 749 |
if (_res_node_num <= 1) return INFEASIBLE; |
742 | 750 |
|
743 | 751 |
// Check the sum of supply values |
744 | 752 |
_sum_supply = 0; |
745 | 753 |
for (int i = 0; i != _root; ++i) { |
746 | 754 |
_sum_supply += _supply[i]; |
747 | 755 |
} |
748 | 756 |
if (_sum_supply > 0) return INFEASIBLE; |
749 | 757 |
|
750 | 758 |
|
751 | 759 |
// Initialize vectors |
752 | 760 |
for (int i = 0; i != _res_node_num; ++i) { |
753 | 761 |
_pi[i] = 0; |
754 | 762 |
_excess[i] = _supply[i]; |
755 | 763 |
} |
756 | 764 |
|
757 | 765 |
// Remove infinite upper bounds and check negative arcs |
758 | 766 |
const Value MAX = std::numeric_limits<Value>::max(); |
759 | 767 |
int last_out; |
760 | 768 |
if (_have_lower) { |
761 | 769 |
for (int i = 0; i != _root; ++i) { |
762 | 770 |
last_out = _first_out[i+1]; |
763 | 771 |
for (int j = _first_out[i]; j != last_out; ++j) { |
764 | 772 |
if (_forward[j]) { |
765 | 773 |
Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; |
766 | 774 |
if (c >= MAX) return UNBOUNDED; |
767 | 775 |
_excess[i] -= c; |
768 | 776 |
_excess[_target[j]] += c; |
769 | 777 |
} |
770 | 778 |
} |
771 | 779 |
} |
772 | 780 |
} else { |
773 | 781 |
for (int i = 0; i != _root; ++i) { |
774 | 782 |
last_out = _first_out[i+1]; |
775 | 783 |
for (int j = _first_out[i]; j != last_out; ++j) { |
776 | 784 |
if (_forward[j] && _scost[j] < 0) { |
777 | 785 |
Value c = _upper[j]; |
778 | 786 |
if (c >= MAX) return UNBOUNDED; |
779 | 787 |
_excess[i] -= c; |
780 | 788 |
_excess[_target[j]] += c; |
781 | 789 |
} |
782 | 790 |
} |
783 | 791 |
} |
784 | 792 |
} |
785 | 793 |
Value ex, max_cap = 0; |
786 | 794 |
for (int i = 0; i != _res_node_num; ++i) { |
787 | 795 |
ex = _excess[i]; |
788 | 796 |
_excess[i] = 0; |
789 | 797 |
if (ex < 0) max_cap -= ex; |
790 | 798 |
} |
791 | 799 |
for (int j = 0; j != _res_arc_num; ++j) { |
792 | 800 |
if (_upper[j] >= MAX) _upper[j] = max_cap; |
793 | 801 |
} |
794 | 802 |
|
795 | 803 |
// Initialize the large cost vector and the epsilon parameter |
796 | 804 |
_epsilon = 0; |
797 | 805 |
LargeCost lc; |
798 | 806 |
for (int i = 0; i != _root; ++i) { |
799 | 807 |
last_out = _first_out[i+1]; |
800 | 808 |
for (int j = _first_out[i]; j != last_out; ++j) { |
801 | 809 |
lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha; |
802 | 810 |
_cost[j] = lc; |
803 | 811 |
if (lc > _epsilon) _epsilon = lc; |
804 | 812 |
} |
805 | 813 |
} |
806 | 814 |
_epsilon /= _alpha; |
807 | 815 |
|
808 | 816 |
// Initialize maps for Circulation and remove non-zero lower bounds |
809 | 817 |
ConstMap<Arc, Value> low(0); |
810 | 818 |
typedef typename Digraph::template ArcMap<Value> ValueArcMap; |
811 | 819 |
typedef typename Digraph::template NodeMap<Value> ValueNodeMap; |
812 | 820 |
ValueArcMap cap(_graph), flow(_graph); |
813 | 821 |
ValueNodeMap sup(_graph); |
814 | 822 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
815 | 823 |
sup[n] = _supply[_node_id[n]]; |
816 | 824 |
} |
817 | 825 |
if (_have_lower) { |
818 | 826 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
819 | 827 |
int j = _arc_idf[a]; |
820 | 828 |
Value c = _lower[j]; |
821 | 829 |
cap[a] = _upper[j] - c; |
822 | 830 |
sup[_graph.source(a)] -= c; |
823 | 831 |
sup[_graph.target(a)] += c; |
824 | 832 |
} |
825 | 833 |
} else { |
826 | 834 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
827 | 835 |
cap[a] = _upper[_arc_idf[a]]; |
828 | 836 |
} |
829 | 837 |
} |
830 | 838 |
|
839 |
_sup_node_num = 0; |
|
840 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
841 |
if (sup[n] > 0) ++_sup_node_num; |
|
842 |
} |
|
843 |
|
|
831 | 844 |
// Find a feasible flow using Circulation |
832 | 845 |
Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap> |
833 | 846 |
circ(_graph, low, cap, sup); |
834 | 847 |
if (!circ.flowMap(flow).run()) return INFEASIBLE; |
835 | 848 |
|
836 | 849 |
// Set residual capacities and handle GEQ supply type |
837 | 850 |
if (_sum_supply < 0) { |
838 | 851 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
839 | 852 |
Value fa = flow[a]; |
840 | 853 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
841 | 854 |
_res_cap[_arc_idb[a]] = fa; |
842 | 855 |
sup[_graph.source(a)] -= fa; |
843 | 856 |
sup[_graph.target(a)] += fa; |
844 | 857 |
} |
845 | 858 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
846 | 859 |
_excess[_node_id[n]] = sup[n]; |
847 | 860 |
} |
848 | 861 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
849 | 862 |
int u = _target[a]; |
850 | 863 |
int ra = _reverse[a]; |
851 | 864 |
_res_cap[a] = -_sum_supply + 1; |
852 | 865 |
_res_cap[ra] = -_excess[u]; |
853 | 866 |
_cost[a] = 0; |
854 | 867 |
_cost[ra] = 0; |
855 | 868 |
_excess[u] = 0; |
856 | 869 |
} |
857 | 870 |
} else { |
858 | 871 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
859 | 872 |
Value fa = flow[a]; |
860 | 873 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
861 | 874 |
_res_cap[_arc_idb[a]] = fa; |
862 | 875 |
} |
863 | 876 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
864 | 877 |
int ra = _reverse[a]; |
865 |
_res_cap[a] = |
|
878 |
_res_cap[a] = 0; |
|
866 | 879 |
_res_cap[ra] = 0; |
867 | 880 |
_cost[a] = 0; |
868 | 881 |
_cost[ra] = 0; |
869 | 882 |
} |
870 | 883 |
} |
871 | 884 |
|
872 | 885 |
return OPTIMAL; |
873 | 886 |
} |
874 | 887 |
|
875 | 888 |
// Execute the algorithm and transform the results |
876 | 889 |
void start(Method method) { |
877 | 890 |
// Maximum path length for partial augment |
878 | 891 |
const int MAX_PATH_LENGTH = 4; |
879 |
|
|
892 |
|
|
893 |
// Initialize data structures for buckets |
|
894 |
_max_rank = _alpha * _res_node_num; |
|
895 |
_buckets.resize(_max_rank); |
|
896 |
_bucket_next.resize(_res_node_num + 1); |
|
897 |
_bucket_prev.resize(_res_node_num + 1); |
|
898 |
_rank.resize(_res_node_num + 1); |
|
899 |
|
|
880 | 900 |
// Execute the algorithm |
881 | 901 |
switch (method) { |
882 | 902 |
case PUSH: |
883 | 903 |
startPush(); |
884 | 904 |
break; |
885 | 905 |
case AUGMENT: |
886 | 906 |
startAugment(); |
887 | 907 |
break; |
888 | 908 |
case PARTIAL_AUGMENT: |
889 | 909 |
startAugment(MAX_PATH_LENGTH); |
890 | 910 |
break; |
891 | 911 |
} |
892 | 912 |
|
893 | 913 |
// Compute node potentials for the original costs |
894 | 914 |
_arc_vec.clear(); |
895 | 915 |
_cost_vec.clear(); |
896 | 916 |
for (int j = 0; j != _res_arc_num; ++j) { |
897 | 917 |
if (_res_cap[j] > 0) { |
898 | 918 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
899 | 919 |
_cost_vec.push_back(_scost[j]); |
900 | 920 |
} |
901 | 921 |
} |
902 | 922 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
903 | 923 |
|
904 | 924 |
typename BellmanFord<StaticDigraph, LargeCostArcMap> |
905 | 925 |
::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map); |
906 | 926 |
bf.distMap(_pi_map); |
907 | 927 |
bf.init(0); |
908 | 928 |
bf.start(); |
909 | 929 |
|
910 | 930 |
// Handle non-zero lower bounds |
911 | 931 |
if (_have_lower) { |
912 | 932 |
int limit = _first_out[_root]; |
913 | 933 |
for (int j = 0; j != limit; ++j) { |
914 | 934 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
915 | 935 |
} |
916 | 936 |
} |
917 | 937 |
} |
938 |
|
|
939 |
// Initialize a cost scaling phase |
|
940 |
void initPhase() { |
|
941 |
// Saturate arcs not satisfying the optimality condition |
|
942 |
for (int u = 0; u != _res_node_num; ++u) { |
|
943 |
int last_out = _first_out[u+1]; |
|
944 |
LargeCost pi_u = _pi[u]; |
|
945 |
for (int a = _first_out[u]; a != last_out; ++a) { |
|
946 |
int v = _target[a]; |
|
947 |
if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) { |
|
948 |
Value delta = _res_cap[a]; |
|
949 |
_excess[u] -= delta; |
|
950 |
_excess[v] += delta; |
|
951 |
_res_cap[a] = 0; |
|
952 |
_res_cap[_reverse[a]] += delta; |
|
953 |
} |
|
954 |
} |
|
955 |
} |
|
956 |
|
|
957 |
// Find active nodes (i.e. nodes with positive excess) |
|
958 |
for (int u = 0; u != _res_node_num; ++u) { |
|
959 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
|
960 |
} |
|
961 |
|
|
962 |
// Initialize the next arcs |
|
963 |
for (int u = 0; u != _res_node_num; ++u) { |
|
964 |
_next_out[u] = _first_out[u]; |
|
965 |
} |
|
966 |
} |
|
967 |
|
|
968 |
// Early termination heuristic |
|
969 |
bool earlyTermination() { |
|
970 |
const double EARLY_TERM_FACTOR = 3.0; |
|
971 |
|
|
972 |
// Build a static residual graph |
|
973 |
_arc_vec.clear(); |
|
974 |
_cost_vec.clear(); |
|
975 |
for (int j = 0; j != _res_arc_num; ++j) { |
|
976 |
if (_res_cap[j] > 0) { |
|
977 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
|
978 |
_cost_vec.push_back(_cost[j] + 1); |
|
979 |
} |
|
980 |
} |
|
981 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
|
982 |
|
|
983 |
// Run Bellman-Ford algorithm to check if the current flow is optimal |
|
984 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
|
985 |
bf.init(0); |
|
986 |
bool done = false; |
|
987 |
int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num))); |
|
988 |
for (int i = 0; i < K && !done; ++i) { |
|
989 |
done = bf.processNextWeakRound(); |
|
990 |
} |
|
991 |
return done; |
|
992 |
} |
|
993 |
|
|
994 |
// Global potential update heuristic |
|
995 |
void globalUpdate() { |
|
996 |
int bucket_end = _root + 1; |
|
997 |
|
|
998 |
// Initialize buckets |
|
999 |
for (int r = 0; r != _max_rank; ++r) { |
|
1000 |
_buckets[r] = bucket_end; |
|
1001 |
} |
|
1002 |
Value total_excess = 0; |
|
1003 |
for (int i = 0; i != _res_node_num; ++i) { |
|
1004 |
if (_excess[i] < 0) { |
|
1005 |
_rank[i] = 0; |
|
1006 |
_bucket_next[i] = _buckets[0]; |
|
1007 |
_bucket_prev[_buckets[0]] = i; |
|
1008 |
_buckets[0] = i; |
|
1009 |
} else { |
|
1010 |
total_excess += _excess[i]; |
|
1011 |
_rank[i] = _max_rank; |
|
1012 |
} |
|
1013 |
} |
|
1014 |
if (total_excess == 0) return; |
|
1015 |
|
|
1016 |
// Search the buckets |
|
1017 |
int r = 0; |
|
1018 |
for ( ; r != _max_rank; ++r) { |
|
1019 |
while (_buckets[r] != bucket_end) { |
|
1020 |
// Remove the first node from the current bucket |
|
1021 |
int u = _buckets[r]; |
|
1022 |
_buckets[r] = _bucket_next[u]; |
|
1023 |
|
|
1024 |
// Search the incomming arcs of u |
|
1025 |
LargeCost pi_u = _pi[u]; |
|
1026 |
int last_out = _first_out[u+1]; |
|
1027 |
for (int a = _first_out[u]; a != last_out; ++a) { |
|
1028 |
int ra = _reverse[a]; |
|
1029 |
if (_res_cap[ra] > 0) { |
|
1030 |
int v = _source[ra]; |
|
1031 |
int old_rank_v = _rank[v]; |
|
1032 |
if (r < old_rank_v) { |
|
1033 |
// Compute the new rank of v |
|
1034 |
LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon; |
|
1035 |
int new_rank_v = old_rank_v; |
|
1036 |
if (nrc < LargeCost(_max_rank)) |
|
1037 |
new_rank_v = r + 1 + int(nrc); |
|
1038 |
|
|
1039 |
// Change the rank of v |
|
1040 |
if (new_rank_v < old_rank_v) { |
|
1041 |
_rank[v] = new_rank_v; |
|
1042 |
_next_out[v] = _first_out[v]; |
|
1043 |
|
|
1044 |
// Remove v from its old bucket |
|
1045 |
if (old_rank_v < _max_rank) { |
|
1046 |
if (_buckets[old_rank_v] == v) { |
|
1047 |
_buckets[old_rank_v] = _bucket_next[v]; |
|
1048 |
} else { |
|
1049 |
_bucket_next[_bucket_prev[v]] = _bucket_next[v]; |
|
1050 |
_bucket_prev[_bucket_next[v]] = _bucket_prev[v]; |
|
1051 |
} |
|
1052 |
} |
|
1053 |
|
|
1054 |
// Insert v to its new bucket |
|
1055 |
_bucket_next[v] = _buckets[new_rank_v]; |
|
1056 |
_bucket_prev[_buckets[new_rank_v]] = v; |
|
1057 |
_buckets[new_rank_v] = v; |
|
1058 |
} |
|
1059 |
} |
|
1060 |
} |
|
1061 |
} |
|
1062 |
|
|
1063 |
// Finish search if there are no more active nodes |
|
1064 |
if (_excess[u] > 0) { |
|
1065 |
total_excess -= _excess[u]; |
|
1066 |
if (total_excess <= 0) break; |
|
1067 |
} |
|
1068 |
} |
|
1069 |
if (total_excess <= 0) break; |
|
1070 |
} |
|
1071 |
|
|
1072 |
// Relabel nodes |
|
1073 |
for (int u = 0; u != _res_node_num; ++u) { |
|
1074 |
int k = std::min(_rank[u], r); |
|
1075 |
if (k > 0) { |
|
1076 |
_pi[u] -= _epsilon * k; |
|
1077 |
_next_out[u] = _first_out[u]; |
|
1078 |
} |
|
1079 |
} |
|
1080 |
} |
|
918 | 1081 |
|
919 | 1082 |
/// Execute the algorithm performing augment and relabel operations |
920 | 1083 |
void startAugment(int max_length = std::numeric_limits<int>::max()) { |
921 | 1084 |
// Paramters for heuristics |
922 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000; |
|
923 |
const int BF_HEURISTIC_BOUND_FACTOR = 3; |
|
1085 |
const int EARLY_TERM_EPSILON_LIMIT = 1000; |
|
1086 |
const double GLOBAL_UPDATE_FACTOR = 3.0; |
|
924 | 1087 |
|
1088 |
const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
|
1089 |
(_res_node_num + _sup_node_num * _sup_node_num)); |
|
1090 |
int next_update_limit = global_update_freq; |
|
1091 |
|
|
1092 |
int relabel_cnt = 0; |
|
1093 |
|
|
925 | 1094 |
// Perform cost scaling phases |
926 |
IntVector pred_arc(_res_node_num); |
|
927 |
std::vector<int> path_nodes; |
|
1095 |
std::vector<int> path; |
|
928 | 1096 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
929 | 1097 |
1 : _epsilon / _alpha ) |
930 | 1098 |
{ |
931 |
// "Early Termination" heuristic: use Bellman-Ford algorithm |
|
932 |
// to check if the current flow is optimal |
|
933 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) { |
|
934 |
_arc_vec.clear(); |
|
935 |
_cost_vec.clear(); |
|
936 |
for (int j = 0; j != _res_arc_num; ++j) { |
|
937 |
if (_res_cap[j] > 0) { |
|
938 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
|
939 |
_cost_vec.push_back(_cost[j] + 1); |
|
940 |
} |
|
941 |
} |
|
942 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
|
943 |
|
|
944 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
|
945 |
bf.init(0); |
|
946 |
bool done = false; |
|
947 |
int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num)); |
|
948 |
for (int i = 0; i < K && !done; ++i) |
|
949 |
done = bf.processNextWeakRound(); |
|
950 |
if (done) break; |
|
951 |
} |
|
952 |
|
|
953 |
// Saturate arcs not satisfying the optimality condition |
|
954 |
for (int a = 0; a != _res_arc_num; ++a) { |
|
955 |
if (_res_cap[a] > 0 && |
|
956 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { |
|
957 |
Value delta = _res_cap[a]; |
|
958 |
_excess[_source[a]] -= delta; |
|
959 |
_excess[_target[a]] += delta; |
|
960 |
_res_cap[a] = 0; |
|
961 |
_res_cap[_reverse[a]] += delta; |
|
962 |
} |
|
1099 |
// Early termination heuristic |
|
1100 |
if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) { |
|
1101 |
if (earlyTermination()) break; |
|
963 | 1102 |
} |
964 | 1103 |
|
965 |
// Find active nodes (i.e. nodes with positive excess) |
|
966 |
for (int u = 0; u != _res_node_num; ++u) { |
|
967 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
|
968 |
} |
|
969 |
|
|
970 |
// Initialize the next arcs |
|
971 |
for (int u = 0; u != _res_node_num; ++u) { |
|
972 |
_next_out[u] = _first_out[u]; |
|
973 |
} |
|
974 |
|
|
1104 |
// Initialize current phase |
|
1105 |
initPhase(); |
|
1106 |
|
|
975 | 1107 |
// Perform partial augment and relabel operations |
976 | 1108 |
while (true) { |
977 | 1109 |
// Select an active node (FIFO selection) |
978 | 1110 |
while (_active_nodes.size() > 0 && |
979 | 1111 |
_excess[_active_nodes.front()] <= 0) { |
980 | 1112 |
_active_nodes.pop_front(); |
981 | 1113 |
} |
982 | 1114 |
if (_active_nodes.size() == 0) break; |
983 | 1115 |
int start = _active_nodes.front(); |
984 |
path_nodes.clear(); |
|
985 |
path_nodes.push_back(start); |
|
986 | 1116 |
|
987 | 1117 |
// Find an augmenting path from the start node |
1118 |
path.clear(); |
|
988 | 1119 |
int tip = start; |
989 |
while (_excess[tip] >= 0 && |
|
990 |
int(path_nodes.size()) <= max_length) { |
|
1120 |
while (_excess[tip] >= 0 && int(path.size()) < max_length) { |
|
991 | 1121 |
int u; |
992 |
LargeCost min_red_cost, rc; |
|
993 |
int last_out = _sum_supply < 0 ? |
|
994 |
|
|
1122 |
LargeCost min_red_cost, rc, pi_tip = _pi[tip]; |
|
1123 |
int last_out = _first_out[tip+1]; |
|
995 | 1124 |
for (int a = _next_out[tip]; a != last_out; ++a) { |
996 |
if (_res_cap[a] > 0 && |
|
997 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { |
|
998 |
u = _target[a]; |
|
999 |
pred_arc[u] = a; |
|
1125 |
u = _target[a]; |
|
1126 |
if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) { |
|
1127 |
path.push_back(a); |
|
1000 | 1128 |
_next_out[tip] = a; |
1001 | 1129 |
tip = u; |
1002 |
path_nodes.push_back(tip); |
|
1003 | 1130 |
goto next_step; |
1004 | 1131 |
} |
1005 | 1132 |
} |
1006 | 1133 |
|
1007 | 1134 |
// Relabel tip node |
1008 |
min_red_cost = std::numeric_limits<LargeCost>::max() |
|
1135 |
min_red_cost = std::numeric_limits<LargeCost>::max(); |
|
1136 |
if (tip != start) { |
|
1137 |
int ra = _reverse[path.back()]; |
|
1138 |
min_red_cost = _cost[ra] + pi_tip - _pi[_target[ra]]; |
|
1139 |
} |
|
1009 | 1140 |
for (int a = _first_out[tip]; a != last_out; ++a) { |
1010 |
rc = _cost[a] + |
|
1141 |
rc = _cost[a] + pi_tip - _pi[_target[a]]; |
|
1011 | 1142 |
if (_res_cap[a] > 0 && rc < min_red_cost) { |
1012 | 1143 |
min_red_cost = rc; |
1013 | 1144 |
} |
1014 | 1145 |
} |
1015 | 1146 |
_pi[tip] -= min_red_cost + _epsilon; |
1016 |
|
|
1017 |
// Reset the next arc of tip |
|
1018 | 1147 |
_next_out[tip] = _first_out[tip]; |
1148 |
++relabel_cnt; |
|
1019 | 1149 |
|
1020 | 1150 |
// Step back |
1021 | 1151 |
if (tip != start) { |
1022 |
path_nodes.pop_back(); |
|
1023 |
tip = path_nodes.back(); |
|
1152 |
tip = _source[path.back()]; |
|
1153 |
path.pop_back(); |
|
1024 | 1154 |
} |
1025 | 1155 |
|
1026 | 1156 |
next_step: ; |
1027 | 1157 |
} |
1028 | 1158 |
|
1029 | 1159 |
// Augment along the found path (as much flow as possible) |
1030 | 1160 |
Value delta; |
1031 |
int u, v = path_nodes.front(), pa; |
|
1032 |
for (int i = 1; i < int(path_nodes.size()); ++i) { |
|
1161 |
int pa, u, v = start; |
|
1162 |
for (int i = 0; i != int(path.size()); ++i) { |
|
1163 |
pa = path[i]; |
|
1033 | 1164 |
u = v; |
1034 |
v = path_nodes[i]; |
|
1035 |
pa = pred_arc[v]; |
|
1165 |
v = _target[pa]; |
|
1036 | 1166 |
delta = std::min(_res_cap[pa], _excess[u]); |
1037 | 1167 |
_res_cap[pa] -= delta; |
1038 | 1168 |
_res_cap[_reverse[pa]] += delta; |
1039 | 1169 |
_excess[u] -= delta; |
1040 | 1170 |
_excess[v] += delta; |
1041 | 1171 |
if (_excess[v] > 0 && _excess[v] <= delta) |
1042 | 1172 |
_active_nodes.push_back(v); |
1043 | 1173 |
} |
1174 |
|
|
1175 |
// Global update heuristic |
|
1176 |
if (relabel_cnt >= next_update_limit) { |
|
1177 |
globalUpdate(); |
|
1178 |
next_update_limit += global_update_freq; |
|
1179 |
} |
|
1044 | 1180 |
} |
1045 | 1181 |
} |
1046 | 1182 |
} |
1047 | 1183 |
|
1048 | 1184 |
/// Execute the algorithm performing push and relabel operations |
1049 | 1185 |
void startPush() { |
1050 | 1186 |
// Paramters for heuristics |
1051 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000; |
|
1052 |
const int BF_HEURISTIC_BOUND_FACTOR = 3; |
|
1187 |
const int EARLY_TERM_EPSILON_LIMIT = 1000; |
|
1188 |
const double GLOBAL_UPDATE_FACTOR = 2.0; |
|
1053 | 1189 |
|
1190 |
const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
|
1191 |
(_res_node_num + _sup_node_num * _sup_node_num)); |
|
1192 |
int next_update_limit = global_update_freq; |
|
1193 |
|
|
1194 |
int relabel_cnt = 0; |
|
1195 |
|
|
1054 | 1196 |
// Perform cost scaling phases |
1055 | 1197 |
BoolVector hyper(_res_node_num, false); |
1198 |
LargeCostVector hyper_cost(_res_node_num); |
|
1056 | 1199 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
1057 | 1200 |
1 : _epsilon / _alpha ) |
1058 | 1201 |
{ |
1059 |
// "Early Termination" heuristic: use Bellman-Ford algorithm |
|
1060 |
// to check if the current flow is optimal |
|
1061 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) { |
|
1062 |
_arc_vec.clear(); |
|
1063 |
_cost_vec.clear(); |
|
1064 |
for (int j = 0; j != _res_arc_num; ++j) { |
|
1065 |
if (_res_cap[j] > 0) { |
|
1066 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
|
1067 |
_cost_vec.push_back(_cost[j] + 1); |
|
1068 |
} |
|
1069 |
} |
|
1070 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
|
1071 |
|
|
1072 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
|
1073 |
bf.init(0); |
|
1074 |
bool done = false; |
|
1075 |
int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num)); |
|
1076 |
for (int i = 0; i < K && !done; ++i) |
|
1077 |
done = bf.processNextWeakRound(); |
|
1078 |
if (done) break; |
|
1202 |
// Early termination heuristic |
|
1203 |
if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) { |
|
1204 |
if (earlyTermination()) break; |
|
1079 | 1205 |
} |
1080 |
|
|
1081 |
// Saturate arcs not satisfying the optimality condition |
|
1082 |
for (int a = 0; a != _res_arc_num; ++a) { |
|
1083 |
if (_res_cap[a] > 0 && |
|
1084 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { |
|
1085 |
Value delta = _res_cap[a]; |
|
1086 |
_excess[_source[a]] -= delta; |
|
1087 |
_excess[_target[a]] += delta; |
|
1088 |
_res_cap[a] = 0; |
|
1089 |
_res_cap[_reverse[a]] += delta; |
|
1090 |
} |
|
1091 |
} |
|
1092 |
|
|
1093 |
// Find active nodes (i.e. nodes with positive excess) |
|
1094 |
for (int u = 0; u != _res_node_num; ++u) { |
|
1095 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
|
1096 |
} |
|
1097 |
|
|
1098 |
// Initialize the next arcs |
|
1099 |
for (int u = 0; u != _res_node_num; ++u) { |
|
1100 |
_next_out[u] = _first_out[u]; |
|
1101 |
} |
|
1206 |
|
|
1207 |
// Initialize current phase |
|
1208 |
initPhase(); |
|
1102 | 1209 |
|
1103 | 1210 |
// Perform push and relabel operations |
1104 | 1211 |
while (_active_nodes.size() > 0) { |
1105 |
LargeCost min_red_cost, rc; |
|
1212 |
LargeCost min_red_cost, rc, pi_n; |
|
1106 | 1213 |
Value delta; |
1107 | 1214 |
int n, t, a, last_out = _res_arc_num; |
1108 | 1215 |
|
1216 |
next_node: |
|
1109 | 1217 |
// Select an active node (FIFO selection) |
1110 |
next_node: |
|
1111 | 1218 |
n = _active_nodes.front(); |
1112 |
last_out = _sum_supply < 0 ? |
|
1113 |
_first_out[n+1] : _first_out[n+1] - 1; |
|
1114 |
|
|
1219 |
last_out = _first_out[n+1]; |
|
1220 |
pi_n = _pi[n]; |
|
1221 |
|
|
1115 | 1222 |
// Perform push operations if there are admissible arcs |
1116 | 1223 |
if (_excess[n] > 0) { |
1117 | 1224 |
for (a = _next_out[n]; a != last_out; ++a) { |
1118 | 1225 |
if (_res_cap[a] > 0 && |
1119 |
_cost[a] + |
|
1226 |
_cost[a] + pi_n - _pi[_target[a]] < 0) { |
|
1120 | 1227 |
delta = std::min(_res_cap[a], _excess[n]); |
1121 | 1228 |
t = _target[a]; |
1122 | 1229 |
|
1123 | 1230 |
// Push-look-ahead heuristic |
1124 | 1231 |
Value ahead = -_excess[t]; |
1125 |
int last_out_t = _sum_supply < 0 ? |
|
1126 |
_first_out[t+1] : _first_out[t+1] - 1; |
|
1232 |
int last_out_t = _first_out[t+1]; |
|
1233 |
LargeCost pi_t = _pi[t]; |
|
1127 | 1234 |
for (int ta = _next_out[t]; ta != last_out_t; ++ta) { |
1128 | 1235 |
if (_res_cap[ta] > 0 && |
1129 |
_cost[ta] + |
|
1236 |
_cost[ta] + pi_t - _pi[_target[ta]] < 0) |
|
1130 | 1237 |
ahead += _res_cap[ta]; |
1131 | 1238 |
if (ahead >= delta) break; |
1132 | 1239 |
} |
1133 | 1240 |
if (ahead < 0) ahead = 0; |
1134 | 1241 |
|
1135 | 1242 |
// Push flow along the arc |
1136 |
if (ahead < delta) { |
|
1243 |
if (ahead < delta && !hyper[t]) { |
|
1137 | 1244 |
_res_cap[a] -= ahead; |
1138 | 1245 |
_res_cap[_reverse[a]] += ahead; |
1139 | 1246 |
_excess[n] -= ahead; |
1140 | 1247 |
_excess[t] += ahead; |
1141 | 1248 |
_active_nodes.push_front(t); |
1142 | 1249 |
hyper[t] = true; |
1250 |
hyper_cost[t] = _cost[a] + pi_n - pi_t; |
|
1143 | 1251 |
_next_out[n] = a; |
1144 | 1252 |
goto next_node; |
1145 | 1253 |
} else { |
1146 | 1254 |
_res_cap[a] -= delta; |
1147 | 1255 |
_res_cap[_reverse[a]] += delta; |
1148 | 1256 |
_excess[n] -= delta; |
1149 | 1257 |
_excess[t] += delta; |
1150 | 1258 |
if (_excess[t] > 0 && _excess[t] <= delta) |
1151 | 1259 |
_active_nodes.push_back(t); |
1152 | 1260 |
} |
1153 | 1261 |
|
1154 | 1262 |
if (_excess[n] == 0) { |
1155 | 1263 |
_next_out[n] = a; |
1156 | 1264 |
goto remove_nodes; |
1157 | 1265 |
} |
1158 | 1266 |
} |
1159 | 1267 |
} |
1160 | 1268 |
_next_out[n] = a; |
1161 | 1269 |
} |
1162 | 1270 |
|
1163 | 1271 |
// Relabel the node if it is still active (or hyper) |
1164 | 1272 |
if (_excess[n] > 0 || hyper[n]) { |
1165 |
min_red_cost = |
|
1273 |
min_red_cost = hyper[n] ? -hyper_cost[n] : |
|
1274 |
std::numeric_limits<LargeCost>::max(); |
|
1166 | 1275 |
for (int a = _first_out[n]; a != last_out; ++a) { |
1167 |
rc = _cost[a] + |
|
1276 |
rc = _cost[a] + pi_n - _pi[_target[a]]; |
|
1168 | 1277 |
if (_res_cap[a] > 0 && rc < min_red_cost) { |
1169 | 1278 |
min_red_cost = rc; |
1170 | 1279 |
} |
1171 | 1280 |
} |
1172 | 1281 |
_pi[n] -= min_red_cost + _epsilon; |
1282 |
_next_out[n] = _first_out[n]; |
|
1173 | 1283 |
hyper[n] = false; |
1174 |
|
|
1175 |
// Reset the next arc |
|
1176 |
|
|
1284 |
++relabel_cnt; |
|
1177 | 1285 |
} |
1178 | 1286 |
|
1179 | 1287 |
// Remove nodes that are not active nor hyper |
1180 | 1288 |
remove_nodes: |
1181 | 1289 |
while ( _active_nodes.size() > 0 && |
1182 | 1290 |
_excess[_active_nodes.front()] <= 0 && |
1183 | 1291 |
!hyper[_active_nodes.front()] ) { |
1184 | 1292 |
_active_nodes.pop_front(); |
1185 | 1293 |
} |
1294 |
|
|
1295 |
// Global update heuristic |
|
1296 |
if (relabel_cnt >= next_update_limit) { |
|
1297 |
globalUpdate(); |
|
1298 |
for (int u = 0; u != _res_node_num; ++u) |
|
1299 |
hyper[u] = false; |
|
1300 |
next_update_limit += global_update_freq; |
|
1301 |
} |
|
1186 | 1302 |
} |
1187 | 1303 |
} |
1188 | 1304 |
} |
1189 | 1305 |
|
1190 | 1306 |
}; //class CostScaling |
1191 | 1307 |
|
1192 | 1308 |
///@} |
1193 | 1309 |
|
1194 | 1310 |
} //namespace lemon |
1195 | 1311 |
|
1196 | 1312 |
#endif //LEMON_COST_SCALING_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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_CYCLE_CANCELING_H |
20 | 20 |
#define LEMON_CYCLE_CANCELING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Cycle-canceling algorithms for finding a minimum cost flow. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <limits> |
28 | 28 |
|
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/adaptors.h> |
35 | 35 |
#include <lemon/circulation.h> |
36 | 36 |
#include <lemon/bellman_ford.h> |
37 | 37 |
#include <lemon/howard.h> |
38 | 38 |
|
39 | 39 |
namespace lemon { |
40 | 40 |
|
41 | 41 |
/// \addtogroup min_cost_flow_algs |
42 | 42 |
/// @{ |
43 | 43 |
|
44 | 44 |
/// \brief Implementation of cycle-canceling algorithms for |
45 | 45 |
/// finding a \ref min_cost_flow "minimum cost flow". |
46 | 46 |
/// |
47 | 47 |
/// \ref CycleCanceling implements three different cycle-canceling |
48 | 48 |
/// algorithms for finding a \ref min_cost_flow "minimum cost flow" |
49 | 49 |
/// \ref amo93networkflows, \ref klein67primal, |
50 | 50 |
/// \ref goldberg89cyclecanceling. |
51 | 51 |
/// The most efficent one (both theoretically and practically) |
52 | 52 |
/// is the \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" algorithm, |
53 | 53 |
/// thus it is the default method. |
54 | 54 |
/// It is strongly polynomial, but in practice, it is typically much |
55 | 55 |
/// slower than the scaling algorithms and NetworkSimplex. |
56 | 56 |
/// |
57 | 57 |
/// Most of the parameters of the problem (except for the digraph) |
58 | 58 |
/// can be given using separate functions, and the algorithm can be |
59 | 59 |
/// executed using the \ref run() function. If some parameters are not |
60 | 60 |
/// specified, then default values will be used. |
61 | 61 |
/// |
62 | 62 |
/// \tparam GR The digraph type the algorithm runs on. |
63 | 63 |
/// \tparam V The number type used for flow amounts, capacity bounds |
64 | 64 |
/// and supply values in the algorithm. By default, it is \c int. |
65 | 65 |
/// \tparam C The number type used for costs and potentials in the |
66 | 66 |
/// algorithm. By default, it is the same as \c V. |
67 | 67 |
/// |
68 | 68 |
/// \warning Both number types must be signed and all input data must |
69 | 69 |
/// be integer. |
70 | 70 |
/// \warning This algorithm does not support negative costs for such |
71 | 71 |
/// arcs that have infinite upper bound. |
72 | 72 |
/// |
73 | 73 |
/// \note For more information about the three available methods, |
74 | 74 |
/// see \ref Method. |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
template <typename GR, typename V, typename C> |
77 | 77 |
#else |
78 | 78 |
template <typename GR, typename V = int, typename C = V> |
79 | 79 |
#endif |
80 | 80 |
class CycleCanceling |
81 | 81 |
{ |
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
/// The type of the digraph |
85 | 85 |
typedef GR Digraph; |
86 | 86 |
/// The type of the flow amounts, capacity bounds and supply values |
87 | 87 |
typedef V Value; |
88 | 88 |
/// The type of the arc costs |
89 | 89 |
typedef C Cost; |
90 | 90 |
|
91 | 91 |
public: |
92 | 92 |
|
93 | 93 |
/// \brief Problem type constants for the \c run() function. |
94 | 94 |
/// |
95 | 95 |
/// Enum type containing the problem type constants that can be |
96 | 96 |
/// returned by the \ref run() function of the algorithm. |
97 | 97 |
enum ProblemType { |
98 | 98 |
/// The problem has no feasible solution (flow). |
99 | 99 |
INFEASIBLE, |
100 | 100 |
/// The problem has optimal solution (i.e. it is feasible and |
101 | 101 |
/// bounded), and the algorithm has found optimal flow and node |
102 | 102 |
/// potentials (primal and dual solutions). |
103 | 103 |
OPTIMAL, |
104 | 104 |
/// The digraph contains an arc of negative cost and infinite |
105 | 105 |
/// upper bound. It means that the objective function is unbounded |
106 | 106 |
/// on that arc, however, note that it could actually be bounded |
107 | 107 |
/// over the feasible flows, but this algroithm cannot handle |
108 | 108 |
/// these cases. |
109 | 109 |
UNBOUNDED |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
/// \brief Constants for selecting the used method. |
113 | 113 |
/// |
114 | 114 |
/// Enum type containing constants for selecting the used method |
115 | 115 |
/// for the \ref run() function. |
116 | 116 |
/// |
117 | 117 |
/// \ref CycleCanceling provides three different cycle-canceling |
118 | 118 |
/// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" |
119 | 119 |
/// is used, which proved to be the most efficient and the most robust |
120 | 120 |
/// on various test inputs. |
121 | 121 |
/// However, the other methods can be selected using the \ref run() |
122 | 122 |
/// function with the proper parameter. |
123 | 123 |
enum Method { |
124 | 124 |
/// A simple cycle-canceling method, which uses the |
125 | 125 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration |
126 | 126 |
/// number for detecting negative cycles in the residual network. |
127 | 127 |
SIMPLE_CYCLE_CANCELING, |
128 | 128 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a |
129 | 129 |
/// well-known strongly polynomial method |
130 | 130 |
/// \ref goldberg89cyclecanceling. It improves along a |
131 | 131 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration. |
132 | 132 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)). |
133 | 133 |
MINIMUM_MEAN_CYCLE_CANCELING, |
134 | 134 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an |
135 | 135 |
/// improved version of the previous method |
136 | 136 |
/// \ref goldberg89cyclecanceling. |
137 | 137 |
/// It is faster both in theory and in practice, its running time |
138 | 138 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)). |
139 | 139 |
CANCEL_AND_TIGHTEN |
140 | 140 |
}; |
141 | 141 |
|
142 | 142 |
private: |
143 | 143 |
|
144 | 144 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
145 | 145 |
|
146 | 146 |
typedef std::vector<int> IntVector; |
147 |
typedef std::vector<char> CharVector; |
|
148 | 147 |
typedef std::vector<double> DoubleVector; |
149 | 148 |
typedef std::vector<Value> ValueVector; |
150 | 149 |
typedef std::vector<Cost> CostVector; |
150 |
typedef std::vector<char> BoolVector; |
|
151 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
|
151 | 152 |
|
152 | 153 |
private: |
153 | 154 |
|
154 | 155 |
template <typename KT, typename VT> |
155 | 156 |
class StaticVectorMap { |
156 | 157 |
public: |
157 | 158 |
typedef KT Key; |
158 | 159 |
typedef VT Value; |
159 | 160 |
|
160 | 161 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {} |
161 | 162 |
|
162 | 163 |
const Value& operator[](const Key& key) const { |
163 | 164 |
return _v[StaticDigraph::id(key)]; |
164 | 165 |
} |
165 | 166 |
|
166 | 167 |
Value& operator[](const Key& key) { |
167 | 168 |
return _v[StaticDigraph::id(key)]; |
168 | 169 |
} |
169 | 170 |
|
170 | 171 |
void set(const Key& key, const Value& val) { |
171 | 172 |
_v[StaticDigraph::id(key)] = val; |
172 | 173 |
} |
173 | 174 |
|
174 | 175 |
private: |
175 | 176 |
std::vector<Value>& _v; |
176 | 177 |
}; |
177 | 178 |
|
178 | 179 |
typedef StaticVectorMap<StaticDigraph::Node, Cost> CostNodeMap; |
179 | 180 |
typedef StaticVectorMap<StaticDigraph::Arc, Cost> CostArcMap; |
180 | 181 |
|
181 | 182 |
private: |
182 | 183 |
|
183 | 184 |
|
184 | 185 |
// Data related to the underlying digraph |
185 | 186 |
const GR &_graph; |
186 | 187 |
int _node_num; |
187 | 188 |
int _arc_num; |
188 | 189 |
int _res_node_num; |
189 | 190 |
int _res_arc_num; |
190 | 191 |
int _root; |
191 | 192 |
|
192 | 193 |
// Parameters of the problem |
193 | 194 |
bool _have_lower; |
194 | 195 |
Value _sum_supply; |
195 | 196 |
|
196 | 197 |
// Data structures for storing the digraph |
197 | 198 |
IntNodeMap _node_id; |
198 | 199 |
IntArcMap _arc_idf; |
199 | 200 |
IntArcMap _arc_idb; |
200 | 201 |
IntVector _first_out; |
201 |
|
|
202 |
BoolVector _forward; |
|
202 | 203 |
IntVector _source; |
203 | 204 |
IntVector _target; |
204 | 205 |
IntVector _reverse; |
205 | 206 |
|
206 | 207 |
// Node and arc data |
207 | 208 |
ValueVector _lower; |
208 | 209 |
ValueVector _upper; |
209 | 210 |
CostVector _cost; |
210 | 211 |
ValueVector _supply; |
211 | 212 |
|
212 | 213 |
ValueVector _res_cap; |
213 | 214 |
CostVector _pi; |
214 | 215 |
|
215 | 216 |
// Data for a StaticDigraph structure |
216 | 217 |
typedef std::pair<int, int> IntPair; |
217 | 218 |
StaticDigraph _sgr; |
218 | 219 |
std::vector<IntPair> _arc_vec; |
219 | 220 |
std::vector<Cost> _cost_vec; |
220 | 221 |
IntVector _id_vec; |
221 | 222 |
CostArcMap _cost_map; |
222 | 223 |
CostNodeMap _pi_map; |
223 | 224 |
|
224 | 225 |
public: |
225 | 226 |
|
226 | 227 |
/// \brief Constant for infinite upper bounds (capacities). |
227 | 228 |
/// |
228 | 229 |
/// Constant for infinite upper bounds (capacities). |
229 | 230 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
230 | 231 |
/// \c std::numeric_limits<Value>::max() otherwise. |
231 | 232 |
const Value INF; |
232 | 233 |
|
233 | 234 |
public: |
234 | 235 |
|
235 | 236 |
/// \brief Constructor. |
236 | 237 |
/// |
237 | 238 |
/// The constructor of the class. |
238 | 239 |
/// |
239 | 240 |
/// \param graph The digraph the algorithm runs on. |
240 | 241 |
CycleCanceling(const GR& graph) : |
241 | 242 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
242 | 243 |
_cost_map(_cost_vec), _pi_map(_pi), |
243 | 244 |
INF(std::numeric_limits<Value>::has_infinity ? |
244 | 245 |
std::numeric_limits<Value>::infinity() : |
245 | 246 |
std::numeric_limits<Value>::max()) |
246 | 247 |
{ |
247 | 248 |
// Check the number types |
248 | 249 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
249 | 250 |
"The flow type of CycleCanceling must be signed"); |
250 | 251 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
251 | 252 |
"The cost type of CycleCanceling must be signed"); |
252 | 253 |
|
253 | 254 |
// Reset data structures |
254 | 255 |
reset(); |
255 | 256 |
} |
256 | 257 |
|
257 | 258 |
/// \name Parameters |
258 | 259 |
/// The parameters of the algorithm can be specified using these |
259 | 260 |
/// functions. |
260 | 261 |
|
261 | 262 |
/// @{ |
262 | 263 |
|
263 | 264 |
/// \brief Set the lower bounds on the arcs. |
264 | 265 |
/// |
265 | 266 |
/// This function sets the lower bounds on the arcs. |
266 | 267 |
/// If it is not used before calling \ref run(), the lower bounds |
267 | 268 |
/// will be set to zero on all arcs. |
268 | 269 |
/// |
269 | 270 |
/// \param map An arc map storing the lower bounds. |
270 | 271 |
/// Its \c Value type must be convertible to the \c Value type |
271 | 272 |
/// of the algorithm. |
272 | 273 |
/// |
273 | 274 |
/// \return <tt>(*this)</tt> |
274 | 275 |
template <typename LowerMap> |
275 | 276 |
CycleCanceling& lowerMap(const LowerMap& map) { |
276 | 277 |
_have_lower = true; |
277 | 278 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
278 | 279 |
_lower[_arc_idf[a]] = map[a]; |
279 | 280 |
_lower[_arc_idb[a]] = map[a]; |
280 | 281 |
} |
281 | 282 |
return *this; |
282 | 283 |
} |
283 | 284 |
|
284 | 285 |
/// \brief Set the upper bounds (capacities) on the arcs. |
285 | 286 |
/// |
286 | 287 |
/// This function sets the upper bounds (capacities) on the arcs. |
287 | 288 |
/// If it is not used before calling \ref run(), the upper bounds |
288 | 289 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
289 | 290 |
/// unbounded from above). |
290 | 291 |
/// |
291 | 292 |
/// \param map An arc map storing the upper bounds. |
292 | 293 |
/// Its \c Value type must be convertible to the \c Value type |
293 | 294 |
/// of the algorithm. |
294 | 295 |
/// |
295 | 296 |
/// \return <tt>(*this)</tt> |
296 | 297 |
template<typename UpperMap> |
297 | 298 |
CycleCanceling& upperMap(const UpperMap& map) { |
298 | 299 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
299 | 300 |
_upper[_arc_idf[a]] = map[a]; |
300 | 301 |
} |
301 | 302 |
return *this; |
302 | 303 |
} |
303 | 304 |
|
304 | 305 |
/// \brief Set the costs of the arcs. |
305 | 306 |
/// |
306 | 307 |
/// This function sets the costs of the arcs. |
307 | 308 |
/// If it is not used before calling \ref run(), the costs |
308 | 309 |
/// will be set to \c 1 on all arcs. |
309 | 310 |
/// |
310 | 311 |
/// \param map An arc map storing the costs. |
311 | 312 |
/// Its \c Value type must be convertible to the \c Cost type |
312 | 313 |
/// of the algorithm. |
313 | 314 |
/// |
314 | 315 |
/// \return <tt>(*this)</tt> |
315 | 316 |
template<typename CostMap> |
316 | 317 |
CycleCanceling& costMap(const CostMap& map) { |
317 | 318 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
318 | 319 |
_cost[_arc_idf[a]] = map[a]; |
319 | 320 |
_cost[_arc_idb[a]] = -map[a]; |
320 | 321 |
} |
321 | 322 |
return *this; |
322 | 323 |
} |
323 | 324 |
|
324 | 325 |
/// \brief Set the supply values of the nodes. |
325 | 326 |
/// |
326 | 327 |
/// This function sets the supply values of the nodes. |
327 | 328 |
/// If neither this function nor \ref stSupply() is used before |
328 | 329 |
/// calling \ref run(), the supply of each node will be set to zero. |
329 | 330 |
/// |
330 | 331 |
/// \param map A node map storing the supply values. |
331 | 332 |
/// Its \c Value type must be convertible to the \c Value type |
332 | 333 |
/// of the algorithm. |
333 | 334 |
/// |
334 | 335 |
/// \return <tt>(*this)</tt> |
335 | 336 |
template<typename SupplyMap> |
336 | 337 |
CycleCanceling& supplyMap(const SupplyMap& map) { |
337 | 338 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
338 | 339 |
_supply[_node_id[n]] = map[n]; |
339 | 340 |
} |
340 | 341 |
return *this; |
341 | 342 |
} |
342 | 343 |
|
343 | 344 |
/// \brief Set single source and target nodes and a supply value. |
344 | 345 |
/// |
345 | 346 |
/// This function sets a single source node and a single target node |
346 | 347 |
/// and the required flow value. |
347 | 348 |
/// If neither this function nor \ref supplyMap() is used before |
348 | 349 |
/// calling \ref run(), the supply of each node will be set to zero. |
349 | 350 |
/// |
350 | 351 |
/// Using this function has the same effect as using \ref supplyMap() |
351 | 352 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
352 | 353 |
/// assigned to \c t and all other nodes have zero supply value. |
353 | 354 |
/// |
354 | 355 |
/// \param s The source node. |
355 | 356 |
/// \param t The target node. |
356 | 357 |
/// \param k The required amount of flow from node \c s to node \c t |
357 | 358 |
/// (i.e. the supply of \c s and the demand of \c t). |
358 | 359 |
/// |
359 | 360 |
/// \return <tt>(*this)</tt> |
360 | 361 |
CycleCanceling& stSupply(const Node& s, const Node& t, Value k) { |
361 | 362 |
for (int i = 0; i != _res_node_num; ++i) { |
362 | 363 |
_supply[i] = 0; |
363 | 364 |
} |
364 | 365 |
_supply[_node_id[s]] = k; |
365 | 366 |
_supply[_node_id[t]] = -k; |
366 | 367 |
return *this; |
367 | 368 |
} |
368 | 369 |
|
369 | 370 |
/// @} |
370 | 371 |
|
371 | 372 |
/// \name Execution control |
372 | 373 |
/// The algorithm can be executed using \ref run(). |
373 | 374 |
|
374 | 375 |
/// @{ |
375 | 376 |
|
376 | 377 |
/// \brief Run the algorithm. |
377 | 378 |
/// |
378 | 379 |
/// This function runs the algorithm. |
379 | 380 |
/// The paramters can be specified using functions \ref lowerMap(), |
380 | 381 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
381 | 382 |
/// For example, |
382 | 383 |
/// \code |
383 | 384 |
/// CycleCanceling<ListDigraph> cc(graph); |
384 | 385 |
/// cc.lowerMap(lower).upperMap(upper).costMap(cost) |
385 | 386 |
/// .supplyMap(sup).run(); |
386 | 387 |
/// \endcode |
387 | 388 |
/// |
388 | 389 |
/// This function can be called more than once. All the given parameters |
389 | 390 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
390 | 391 |
/// is used, thus only the modified parameters have to be set again. |
391 | 392 |
/// If the underlying digraph was also modified after the construction |
392 | 393 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
393 | 394 |
/// function must be called. |
... | ... |
@@ -773,386 +774,386 @@ |
773 | 774 |
return OPTIMAL; |
774 | 775 |
} |
775 | 776 |
|
776 | 777 |
// Build a StaticDigraph structure containing the current |
777 | 778 |
// residual network |
778 | 779 |
void buildResidualNetwork() { |
779 | 780 |
_arc_vec.clear(); |
780 | 781 |
_cost_vec.clear(); |
781 | 782 |
_id_vec.clear(); |
782 | 783 |
for (int j = 0; j != _res_arc_num; ++j) { |
783 | 784 |
if (_res_cap[j] > 0) { |
784 | 785 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
785 | 786 |
_cost_vec.push_back(_cost[j]); |
786 | 787 |
_id_vec.push_back(j); |
787 | 788 |
} |
788 | 789 |
} |
789 | 790 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
790 | 791 |
} |
791 | 792 |
|
792 | 793 |
// Execute the algorithm and transform the results |
793 | 794 |
void start(Method method) { |
794 | 795 |
// Execute the algorithm |
795 | 796 |
switch (method) { |
796 | 797 |
case SIMPLE_CYCLE_CANCELING: |
797 | 798 |
startSimpleCycleCanceling(); |
798 | 799 |
break; |
799 | 800 |
case MINIMUM_MEAN_CYCLE_CANCELING: |
800 | 801 |
startMinMeanCycleCanceling(); |
801 | 802 |
break; |
802 | 803 |
case CANCEL_AND_TIGHTEN: |
803 | 804 |
startCancelAndTighten(); |
804 | 805 |
break; |
805 | 806 |
} |
806 | 807 |
|
807 | 808 |
// Compute node potentials |
808 | 809 |
if (method != SIMPLE_CYCLE_CANCELING) { |
809 | 810 |
buildResidualNetwork(); |
810 | 811 |
typename BellmanFord<StaticDigraph, CostArcMap> |
811 | 812 |
::template SetDistMap<CostNodeMap>::Create bf(_sgr, _cost_map); |
812 | 813 |
bf.distMap(_pi_map); |
813 | 814 |
bf.init(0); |
814 | 815 |
bf.start(); |
815 | 816 |
} |
816 | 817 |
|
817 | 818 |
// Handle non-zero lower bounds |
818 | 819 |
if (_have_lower) { |
819 | 820 |
int limit = _first_out[_root]; |
820 | 821 |
for (int j = 0; j != limit; ++j) { |
821 | 822 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
822 | 823 |
} |
823 | 824 |
} |
824 | 825 |
} |
825 | 826 |
|
826 | 827 |
// Execute the "Simple Cycle Canceling" method |
827 | 828 |
void startSimpleCycleCanceling() { |
828 | 829 |
// Constants for computing the iteration limits |
829 | 830 |
const int BF_FIRST_LIMIT = 2; |
830 | 831 |
const double BF_LIMIT_FACTOR = 1.5; |
831 | 832 |
|
832 | 833 |
typedef StaticVectorMap<StaticDigraph::Arc, Value> FilterMap; |
833 | 834 |
typedef FilterArcs<StaticDigraph, FilterMap> ResDigraph; |
834 | 835 |
typedef StaticVectorMap<StaticDigraph::Node, StaticDigraph::Arc> PredMap; |
835 | 836 |
typedef typename BellmanFord<ResDigraph, CostArcMap> |
836 | 837 |
::template SetDistMap<CostNodeMap> |
837 | 838 |
::template SetPredMap<PredMap>::Create BF; |
838 | 839 |
|
839 | 840 |
// Build the residual network |
840 | 841 |
_arc_vec.clear(); |
841 | 842 |
_cost_vec.clear(); |
842 | 843 |
for (int j = 0; j != _res_arc_num; ++j) { |
843 | 844 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
844 | 845 |
_cost_vec.push_back(_cost[j]); |
845 | 846 |
} |
846 | 847 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
847 | 848 |
|
848 | 849 |
FilterMap filter_map(_res_cap); |
849 | 850 |
ResDigraph rgr(_sgr, filter_map); |
850 | 851 |
std::vector<int> cycle; |
851 | 852 |
std::vector<StaticDigraph::Arc> pred(_res_arc_num); |
852 | 853 |
PredMap pred_map(pred); |
853 | 854 |
BF bf(rgr, _cost_map); |
854 | 855 |
bf.distMap(_pi_map).predMap(pred_map); |
855 | 856 |
|
856 | 857 |
int length_bound = BF_FIRST_LIMIT; |
857 | 858 |
bool optimal = false; |
858 | 859 |
while (!optimal) { |
859 | 860 |
bf.init(0); |
860 | 861 |
int iter_num = 0; |
861 | 862 |
bool cycle_found = false; |
862 | 863 |
while (!cycle_found) { |
863 | 864 |
// Perform some iterations of the Bellman-Ford algorithm |
864 | 865 |
int curr_iter_num = iter_num + length_bound <= _node_num ? |
865 | 866 |
length_bound : _node_num - iter_num; |
866 | 867 |
iter_num += curr_iter_num; |
867 | 868 |
int real_iter_num = curr_iter_num; |
868 | 869 |
for (int i = 0; i < curr_iter_num; ++i) { |
869 | 870 |
if (bf.processNextWeakRound()) { |
870 | 871 |
real_iter_num = i; |
871 | 872 |
break; |
872 | 873 |
} |
873 | 874 |
} |
874 | 875 |
if (real_iter_num < curr_iter_num) { |
875 | 876 |
// Optimal flow is found |
876 | 877 |
optimal = true; |
877 | 878 |
break; |
878 | 879 |
} else { |
879 | 880 |
// Search for node disjoint negative cycles |
880 | 881 |
std::vector<int> state(_res_node_num, 0); |
881 | 882 |
int id = 0; |
882 | 883 |
for (int u = 0; u != _res_node_num; ++u) { |
883 | 884 |
if (state[u] != 0) continue; |
884 | 885 |
++id; |
885 | 886 |
int v = u; |
886 | 887 |
for (; v != -1 && state[v] == 0; v = pred[v] == INVALID ? |
887 | 888 |
-1 : rgr.id(rgr.source(pred[v]))) { |
888 | 889 |
state[v] = id; |
889 | 890 |
} |
890 | 891 |
if (v != -1 && state[v] == id) { |
891 | 892 |
// A negative cycle is found |
892 | 893 |
cycle_found = true; |
893 | 894 |
cycle.clear(); |
894 | 895 |
StaticDigraph::Arc a = pred[v]; |
895 | 896 |
Value d, delta = _res_cap[rgr.id(a)]; |
896 | 897 |
cycle.push_back(rgr.id(a)); |
897 | 898 |
while (rgr.id(rgr.source(a)) != v) { |
898 | 899 |
a = pred_map[rgr.source(a)]; |
899 | 900 |
d = _res_cap[rgr.id(a)]; |
900 | 901 |
if (d < delta) delta = d; |
901 | 902 |
cycle.push_back(rgr.id(a)); |
902 | 903 |
} |
903 | 904 |
|
904 | 905 |
// Augment along the cycle |
905 | 906 |
for (int i = 0; i < int(cycle.size()); ++i) { |
906 | 907 |
int j = cycle[i]; |
907 | 908 |
_res_cap[j] -= delta; |
908 | 909 |
_res_cap[_reverse[j]] += delta; |
909 | 910 |
} |
910 | 911 |
} |
911 | 912 |
} |
912 | 913 |
} |
913 | 914 |
|
914 | 915 |
// Increase iteration limit if no cycle is found |
915 | 916 |
if (!cycle_found) { |
916 | 917 |
length_bound = static_cast<int>(length_bound * BF_LIMIT_FACTOR); |
917 | 918 |
} |
918 | 919 |
} |
919 | 920 |
} |
920 | 921 |
} |
921 | 922 |
|
922 | 923 |
// Execute the "Minimum Mean Cycle Canceling" method |
923 | 924 |
void startMinMeanCycleCanceling() { |
924 | 925 |
typedef SimplePath<StaticDigraph> SPath; |
925 | 926 |
typedef typename SPath::ArcIt SPathArcIt; |
926 | 927 |
typedef typename Howard<StaticDigraph, CostArcMap> |
927 | 928 |
::template SetPath<SPath>::Create MMC; |
928 | 929 |
|
929 | 930 |
SPath cycle; |
930 | 931 |
MMC mmc(_sgr, _cost_map); |
931 | 932 |
mmc.cycle(cycle); |
932 | 933 |
buildResidualNetwork(); |
933 | 934 |
while (mmc.findMinMean() && mmc.cycleLength() < 0) { |
934 | 935 |
// Find the cycle |
935 | 936 |
mmc.findCycle(); |
936 | 937 |
|
937 | 938 |
// Compute delta value |
938 | 939 |
Value delta = INF; |
939 | 940 |
for (SPathArcIt a(cycle); a != INVALID; ++a) { |
940 | 941 |
Value d = _res_cap[_id_vec[_sgr.id(a)]]; |
941 | 942 |
if (d < delta) delta = d; |
942 | 943 |
} |
943 | 944 |
|
944 | 945 |
// Augment along the cycle |
945 | 946 |
for (SPathArcIt a(cycle); a != INVALID; ++a) { |
946 | 947 |
int j = _id_vec[_sgr.id(a)]; |
947 | 948 |
_res_cap[j] -= delta; |
948 | 949 |
_res_cap[_reverse[j]] += delta; |
949 | 950 |
} |
950 | 951 |
|
951 | 952 |
// Rebuild the residual network |
952 | 953 |
buildResidualNetwork(); |
953 | 954 |
} |
954 | 955 |
} |
955 | 956 |
|
956 | 957 |
// Execute the "Cancel And Tighten" method |
957 | 958 |
void startCancelAndTighten() { |
958 | 959 |
// Constants for the min mean cycle computations |
959 | 960 |
const double LIMIT_FACTOR = 1.0; |
960 | 961 |
const int MIN_LIMIT = 5; |
961 | 962 |
|
962 | 963 |
// Contruct auxiliary data vectors |
963 | 964 |
DoubleVector pi(_res_node_num, 0.0); |
964 | 965 |
IntVector level(_res_node_num); |
965 |
CharVector reached(_res_node_num); |
|
966 |
CharVector processed(_res_node_num); |
|
966 |
BoolVector reached(_res_node_num); |
|
967 |
BoolVector processed(_res_node_num); |
|
967 | 968 |
IntVector pred_node(_res_node_num); |
968 | 969 |
IntVector pred_arc(_res_node_num); |
969 | 970 |
std::vector<int> stack(_res_node_num); |
970 | 971 |
std::vector<int> proc_vector(_res_node_num); |
971 | 972 |
|
972 | 973 |
// Initialize epsilon |
973 | 974 |
double epsilon = 0; |
974 | 975 |
for (int a = 0; a != _res_arc_num; ++a) { |
975 | 976 |
if (_res_cap[a] > 0 && -_cost[a] > epsilon) |
976 | 977 |
epsilon = -_cost[a]; |
977 | 978 |
} |
978 | 979 |
|
979 | 980 |
// Start phases |
980 | 981 |
Tolerance<double> tol; |
981 | 982 |
tol.epsilon(1e-6); |
982 | 983 |
int limit = int(LIMIT_FACTOR * std::sqrt(double(_res_node_num))); |
983 | 984 |
if (limit < MIN_LIMIT) limit = MIN_LIMIT; |
984 | 985 |
int iter = limit; |
985 | 986 |
while (epsilon * _res_node_num >= 1) { |
986 | 987 |
// Find and cancel cycles in the admissible network using DFS |
987 | 988 |
for (int u = 0; u != _res_node_num; ++u) { |
988 | 989 |
reached[u] = false; |
989 | 990 |
processed[u] = false; |
990 | 991 |
} |
991 | 992 |
int stack_head = -1; |
992 | 993 |
int proc_head = -1; |
993 | 994 |
for (int start = 0; start != _res_node_num; ++start) { |
994 | 995 |
if (reached[start]) continue; |
995 | 996 |
|
996 | 997 |
// New start node |
997 | 998 |
reached[start] = true; |
998 | 999 |
pred_arc[start] = -1; |
999 | 1000 |
pred_node[start] = -1; |
1000 | 1001 |
|
1001 | 1002 |
// Find the first admissible outgoing arc |
1002 | 1003 |
double p = pi[start]; |
1003 | 1004 |
int a = _first_out[start]; |
1004 | 1005 |
int last_out = _first_out[start+1]; |
1005 | 1006 |
for (; a != last_out && (_res_cap[a] == 0 || |
1006 | 1007 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; |
1007 | 1008 |
if (a == last_out) { |
1008 | 1009 |
processed[start] = true; |
1009 | 1010 |
proc_vector[++proc_head] = start; |
1010 | 1011 |
continue; |
1011 | 1012 |
} |
1012 | 1013 |
stack[++stack_head] = a; |
1013 | 1014 |
|
1014 | 1015 |
while (stack_head >= 0) { |
1015 | 1016 |
int sa = stack[stack_head]; |
1016 | 1017 |
int u = _source[sa]; |
1017 | 1018 |
int v = _target[sa]; |
1018 | 1019 |
|
1019 | 1020 |
if (!reached[v]) { |
1020 | 1021 |
// A new node is reached |
1021 | 1022 |
reached[v] = true; |
1022 | 1023 |
pred_node[v] = u; |
1023 | 1024 |
pred_arc[v] = sa; |
1024 | 1025 |
p = pi[v]; |
1025 | 1026 |
a = _first_out[v]; |
1026 | 1027 |
last_out = _first_out[v+1]; |
1027 | 1028 |
for (; a != last_out && (_res_cap[a] == 0 || |
1028 | 1029 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; |
1029 | 1030 |
stack[++stack_head] = a == last_out ? -1 : a; |
1030 | 1031 |
} else { |
1031 | 1032 |
if (!processed[v]) { |
1032 | 1033 |
// A cycle is found |
1033 | 1034 |
int n, w = u; |
1034 | 1035 |
Value d, delta = _res_cap[sa]; |
1035 | 1036 |
for (n = u; n != v; n = pred_node[n]) { |
1036 | 1037 |
d = _res_cap[pred_arc[n]]; |
1037 | 1038 |
if (d <= delta) { |
1038 | 1039 |
delta = d; |
1039 | 1040 |
w = pred_node[n]; |
1040 | 1041 |
} |
1041 | 1042 |
} |
1042 | 1043 |
|
1043 | 1044 |
// Augment along the cycle |
1044 | 1045 |
_res_cap[sa] -= delta; |
1045 | 1046 |
_res_cap[_reverse[sa]] += delta; |
1046 | 1047 |
for (n = u; n != v; n = pred_node[n]) { |
1047 | 1048 |
int pa = pred_arc[n]; |
1048 | 1049 |
_res_cap[pa] -= delta; |
1049 | 1050 |
_res_cap[_reverse[pa]] += delta; |
1050 | 1051 |
} |
1051 | 1052 |
for (n = u; stack_head > 0 && n != w; n = pred_node[n]) { |
1052 | 1053 |
--stack_head; |
1053 | 1054 |
reached[n] = false; |
1054 | 1055 |
} |
1055 | 1056 |
u = w; |
1056 | 1057 |
} |
1057 | 1058 |
v = u; |
1058 | 1059 |
|
1059 | 1060 |
// Find the next admissible outgoing arc |
1060 | 1061 |
p = pi[v]; |
1061 | 1062 |
a = stack[stack_head] + 1; |
1062 | 1063 |
last_out = _first_out[v+1]; |
1063 | 1064 |
for (; a != last_out && (_res_cap[a] == 0 || |
1064 | 1065 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; |
1065 | 1066 |
stack[stack_head] = a == last_out ? -1 : a; |
1066 | 1067 |
} |
1067 | 1068 |
|
1068 | 1069 |
while (stack_head >= 0 && stack[stack_head] == -1) { |
1069 | 1070 |
processed[v] = true; |
1070 | 1071 |
proc_vector[++proc_head] = v; |
1071 | 1072 |
if (--stack_head >= 0) { |
1072 | 1073 |
// Find the next admissible outgoing arc |
1073 | 1074 |
v = _source[stack[stack_head]]; |
1074 | 1075 |
p = pi[v]; |
1075 | 1076 |
a = stack[stack_head] + 1; |
1076 | 1077 |
last_out = _first_out[v+1]; |
1077 | 1078 |
for (; a != last_out && (_res_cap[a] == 0 || |
1078 | 1079 |
!tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; |
1079 | 1080 |
stack[stack_head] = a == last_out ? -1 : a; |
1080 | 1081 |
} |
1081 | 1082 |
} |
1082 | 1083 |
} |
1083 | 1084 |
} |
1084 | 1085 |
|
1085 | 1086 |
// Tighten potentials and epsilon |
1086 | 1087 |
if (--iter > 0) { |
1087 | 1088 |
for (int u = 0; u != _res_node_num; ++u) { |
1088 | 1089 |
level[u] = 0; |
1089 | 1090 |
} |
1090 | 1091 |
for (int i = proc_head; i > 0; --i) { |
1091 | 1092 |
int u = proc_vector[i]; |
1092 | 1093 |
double p = pi[u]; |
1093 | 1094 |
int l = level[u] + 1; |
1094 | 1095 |
int last_out = _first_out[u+1]; |
1095 | 1096 |
for (int a = _first_out[u]; a != last_out; ++a) { |
1096 | 1097 |
int v = _target[a]; |
1097 | 1098 |
if (_res_cap[a] > 0 && tol.negative(_cost[a] + p - pi[v]) && |
1098 | 1099 |
l > level[v]) level[v] = l; |
1099 | 1100 |
} |
1100 | 1101 |
} |
1101 | 1102 |
|
1102 | 1103 |
// Modify potentials |
1103 | 1104 |
double q = std::numeric_limits<double>::max(); |
1104 | 1105 |
for (int u = 0; u != _res_node_num; ++u) { |
1105 | 1106 |
int lu = level[u]; |
1106 | 1107 |
double p, pu = pi[u]; |
1107 | 1108 |
int last_out = _first_out[u+1]; |
1108 | 1109 |
for (int a = _first_out[u]; a != last_out; ++a) { |
1109 | 1110 |
if (_res_cap[a] == 0) continue; |
1110 | 1111 |
int v = _target[a]; |
1111 | 1112 |
int ld = lu - level[v]; |
1112 | 1113 |
if (ld > 0) { |
1113 | 1114 |
p = (_cost[a] + pu - pi[v] + epsilon) / (ld + 1); |
1114 | 1115 |
if (p < q) q = p; |
1115 | 1116 |
} |
1116 | 1117 |
} |
1117 | 1118 |
} |
1118 | 1119 |
for (int u = 0; u != _res_node_num; ++u) { |
1119 | 1120 |
pi[u] -= q * level[u]; |
1120 | 1121 |
} |
1121 | 1122 |
|
1122 | 1123 |
// Modify epsilon |
1123 | 1124 |
epsilon = 0; |
1124 | 1125 |
for (int u = 0; u != _res_node_num; ++u) { |
1125 | 1126 |
double curr, pu = pi[u]; |
1126 | 1127 |
int last_out = _first_out[u+1]; |
1127 | 1128 |
for (int a = _first_out[u]; a != last_out; ++a) { |
1128 | 1129 |
if (_res_cap[a] == 0) continue; |
1129 | 1130 |
curr = _cost[a] + pu - pi[_target[a]]; |
1130 | 1131 |
if (-curr > epsilon) epsilon = -curr; |
1131 | 1132 |
} |
1132 | 1133 |
} |
1133 | 1134 |
} else { |
1134 | 1135 |
typedef Howard<StaticDigraph, CostArcMap> MMC; |
1135 | 1136 |
typedef typename BellmanFord<StaticDigraph, CostArcMap> |
1136 | 1137 |
::template SetDistMap<CostNodeMap>::Create BF; |
1137 | 1138 |
|
1138 | 1139 |
// Set epsilon to the minimum cycle mean |
1139 | 1140 |
buildResidualNetwork(); |
1140 | 1141 |
MMC mmc(_sgr, _cost_map); |
1141 | 1142 |
mmc.findMinMean(); |
1142 | 1143 |
epsilon = -mmc.cycleMean(); |
1143 | 1144 |
Cost cycle_cost = mmc.cycleLength(); |
1144 | 1145 |
int cycle_size = mmc.cycleArcNum(); |
1145 | 1146 |
|
1146 | 1147 |
// Compute feasible potentials for the current epsilon |
1147 | 1148 |
for (int i = 0; i != int(_cost_vec.size()); ++i) { |
1148 | 1149 |
_cost_vec[i] = cycle_size * _cost_vec[i] - cycle_cost; |
1149 | 1150 |
} |
1150 | 1151 |
BF bf(_sgr, _cost_map); |
1151 | 1152 |
bf.distMap(_pi_map); |
1152 | 1153 |
bf.init(0); |
1153 | 1154 |
bf.start(); |
1154 | 1155 |
for (int u = 0; u != _res_node_num; ++u) { |
1155 | 1156 |
pi[u] = static_cast<double>(_pi[u]) / cycle_size; |
1156 | 1157 |
} |
1157 | 1158 |
|
1158 | 1159 |
iter = limit; |
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-2009 |
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_NETWORK_SIMPLEX_H |
20 | 20 |
#define LEMON_NETWORK_SIMPLEX_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Network Simplex algorithm for finding a minimum cost flow. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <algorithm> |
30 | 30 |
|
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup min_cost_flow_algs |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
/// \brief Implementation of the primal Network Simplex algorithm |
40 | 40 |
/// for finding a \ref min_cost_flow "minimum cost flow". |
41 | 41 |
/// |
42 | 42 |
/// \ref NetworkSimplex implements the primal Network Simplex algorithm |
43 | 43 |
/// for finding a \ref min_cost_flow "minimum cost flow" |
44 | 44 |
/// \ref amo93networkflows, \ref dantzig63linearprog, |
45 | 45 |
/// \ref kellyoneill91netsimplex. |
46 | 46 |
/// This algorithm is a highly efficient specialized version of the |
47 | 47 |
/// linear programming simplex method directly for the minimum cost |
48 | 48 |
/// flow problem. |
49 | 49 |
/// |
50 | 50 |
/// In general, %NetworkSimplex is the fastest implementation available |
51 | 51 |
/// in LEMON for this problem. |
52 | 52 |
/// Moreover, it supports both directions of the supply/demand inequality |
53 | 53 |
/// constraints. For more information, see \ref SupplyType. |
54 | 54 |
/// |
55 | 55 |
/// Most of the parameters of the problem (except for the digraph) |
56 | 56 |
/// can be given using separate functions, and the algorithm can be |
57 | 57 |
/// executed using the \ref run() function. If some parameters are not |
58 | 58 |
/// specified, then default values will be used. |
59 | 59 |
/// |
60 | 60 |
/// \tparam GR The digraph type the algorithm runs on. |
61 | 61 |
/// \tparam V The number type used for flow amounts, capacity bounds |
62 | 62 |
/// and supply values in the algorithm. By default, it is \c int. |
63 | 63 |
/// \tparam C The number type used for costs and potentials in the |
64 | 64 |
/// algorithm. By default, it is the same as \c V. |
65 | 65 |
/// |
66 | 66 |
/// \warning Both number types must be signed and all input data must |
67 | 67 |
/// be integer. |
68 | 68 |
/// |
69 | 69 |
/// \note %NetworkSimplex provides five different pivot rule |
70 | 70 |
/// implementations, from which the most efficient one is used |
71 | 71 |
/// by default. For more information, see \ref PivotRule. |
72 | 72 |
template <typename GR, typename V = int, typename C = V> |
73 | 73 |
class NetworkSimplex |
74 | 74 |
{ |
75 | 75 |
public: |
76 | 76 |
|
77 | 77 |
/// The type of the flow amounts, capacity bounds and supply values |
78 | 78 |
typedef V Value; |
79 | 79 |
/// The type of the arc costs |
80 | 80 |
typedef C Cost; |
81 | 81 |
|
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
/// \brief Problem type constants for the \c run() function. |
85 | 85 |
/// |
86 | 86 |
/// Enum type containing the problem type constants that can be |
87 | 87 |
/// returned by the \ref run() function of the algorithm. |
88 | 88 |
enum ProblemType { |
89 | 89 |
/// The problem has no feasible solution (flow). |
90 | 90 |
INFEASIBLE, |
91 | 91 |
/// The problem has optimal solution (i.e. it is feasible and |
92 | 92 |
/// bounded), and the algorithm has found optimal flow and node |
93 | 93 |
/// potentials (primal and dual solutions). |
94 | 94 |
OPTIMAL, |
95 | 95 |
/// The objective function of the problem is unbounded, i.e. |
96 | 96 |
/// there is a directed cycle having negative total cost and |
97 | 97 |
/// infinite upper bound. |
98 | 98 |
UNBOUNDED |
99 | 99 |
}; |
100 | 100 |
|
101 | 101 |
/// \brief Constants for selecting the type of the supply constraints. |
102 | 102 |
/// |
103 | 103 |
/// Enum type containing constants for selecting the supply type, |
104 | 104 |
/// i.e. the direction of the inequalities in the supply/demand |
105 | 105 |
/// constraints of the \ref min_cost_flow "minimum cost flow problem". |
106 | 106 |
/// |
107 | 107 |
/// The default supply type is \c GEQ, the \c LEQ type can be |
108 | 108 |
/// selected using \ref supplyType(). |
109 | 109 |
/// The equality form is a special case of both supply types. |
110 | 110 |
enum SupplyType { |
111 | 111 |
/// This option means that there are <em>"greater or equal"</em> |
112 | 112 |
/// supply/demand constraints in the definition of the problem. |
113 | 113 |
GEQ, |
114 | 114 |
/// This option means that there are <em>"less or equal"</em> |
115 | 115 |
/// supply/demand constraints in the definition of the problem. |
116 | 116 |
LEQ |
117 | 117 |
}; |
118 | 118 |
|
119 | 119 |
/// \brief Constants for selecting the pivot rule. |
120 | 120 |
/// |
121 | 121 |
/// Enum type containing constants for selecting the pivot rule for |
122 | 122 |
/// the \ref run() function. |
123 | 123 |
/// |
124 | 124 |
/// \ref NetworkSimplex provides five different pivot rule |
125 | 125 |
/// implementations that significantly affect the running time |
126 | 126 |
/// of the algorithm. |
127 | 127 |
/// By default, \ref BLOCK_SEARCH "Block Search" is used, which |
128 | 128 |
/// proved to be the most efficient and the most robust on various |
129 | 129 |
/// test inputs. |
130 | 130 |
/// However, another pivot rule can be selected using the \ref run() |
131 | 131 |
/// function with the proper parameter. |
132 | 132 |
enum PivotRule { |
133 | 133 |
|
134 | 134 |
/// The \e First \e Eligible pivot rule. |
135 | 135 |
/// The next eligible arc is selected in a wraparound fashion |
136 | 136 |
/// in every iteration. |
137 | 137 |
FIRST_ELIGIBLE, |
138 | 138 |
|
139 | 139 |
/// The \e Best \e Eligible pivot rule. |
140 | 140 |
/// The best eligible arc is selected in every iteration. |
141 | 141 |
BEST_ELIGIBLE, |
142 | 142 |
|
143 | 143 |
/// The \e Block \e Search pivot rule. |
144 | 144 |
/// A specified number of arcs are examined in every iteration |
145 | 145 |
/// in a wraparound fashion and the best eligible arc is selected |
146 | 146 |
/// from this block. |
147 | 147 |
BLOCK_SEARCH, |
148 | 148 |
|
149 | 149 |
/// The \e Candidate \e List pivot rule. |
150 | 150 |
/// In a major iteration a candidate list is built from eligible arcs |
151 | 151 |
/// in a wraparound fashion and in the following minor iterations |
152 | 152 |
/// the best eligible arc is selected from this list. |
153 | 153 |
CANDIDATE_LIST, |
154 | 154 |
|
155 | 155 |
/// The \e Altering \e Candidate \e List pivot rule. |
156 | 156 |
/// It is a modified version of the Candidate List method. |
157 | 157 |
/// It keeps only the several best eligible arcs from the former |
158 | 158 |
/// candidate list and extends this list in every iteration. |
159 | 159 |
ALTERING_LIST |
160 | 160 |
}; |
161 | 161 |
|
162 | 162 |
private: |
163 | 163 |
|
164 | 164 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
165 | 165 |
|
166 | 166 |
typedef std::vector<int> IntVector; |
167 |
typedef std::vector<char> CharVector; |
|
168 | 167 |
typedef std::vector<Value> ValueVector; |
169 | 168 |
typedef std::vector<Cost> CostVector; |
169 |
typedef std::vector<char> BoolVector; |
|
170 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
|
170 | 171 |
|
171 | 172 |
// State constants for arcs |
172 | 173 |
enum ArcStateEnum { |
173 | 174 |
STATE_UPPER = -1, |
174 | 175 |
STATE_TREE = 0, |
175 | 176 |
STATE_LOWER = 1 |
176 | 177 |
}; |
177 | 178 |
|
178 | 179 |
private: |
179 | 180 |
|
180 | 181 |
// Data related to the underlying digraph |
181 | 182 |
const GR &_graph; |
182 | 183 |
int _node_num; |
183 | 184 |
int _arc_num; |
184 | 185 |
int _all_arc_num; |
185 | 186 |
int _search_arc_num; |
186 | 187 |
|
187 | 188 |
// Parameters of the problem |
188 | 189 |
bool _have_lower; |
189 | 190 |
SupplyType _stype; |
190 | 191 |
Value _sum_supply; |
191 | 192 |
|
192 | 193 |
// Data structures for storing the digraph |
193 | 194 |
IntNodeMap _node_id; |
194 | 195 |
IntArcMap _arc_id; |
195 | 196 |
IntVector _source; |
196 | 197 |
IntVector _target; |
197 | 198 |
bool _arc_mixing; |
198 | 199 |
|
199 | 200 |
// Node and arc data |
200 | 201 |
ValueVector _lower; |
201 | 202 |
ValueVector _upper; |
202 | 203 |
ValueVector _cap; |
203 | 204 |
CostVector _cost; |
204 | 205 |
ValueVector _supply; |
205 | 206 |
ValueVector _flow; |
206 | 207 |
CostVector _pi; |
207 | 208 |
|
208 | 209 |
// Data for storing the spanning tree structure |
209 | 210 |
IntVector _parent; |
210 | 211 |
IntVector _pred; |
211 | 212 |
IntVector _thread; |
212 | 213 |
IntVector _rev_thread; |
213 | 214 |
IntVector _succ_num; |
214 | 215 |
IntVector _last_succ; |
215 | 216 |
IntVector _dirty_revs; |
216 |
CharVector _forward; |
|
217 |
CharVector _state; |
|
217 |
BoolVector _forward; |
|
218 |
BoolVector _state; |
|
218 | 219 |
int _root; |
219 | 220 |
|
220 | 221 |
// Temporary data used in the current pivot iteration |
221 | 222 |
int in_arc, join, u_in, v_in, u_out, v_out; |
222 | 223 |
int first, second, right, last; |
223 | 224 |
int stem, par_stem, new_stem; |
224 | 225 |
Value delta; |
225 | 226 |
|
226 | 227 |
const Value MAX; |
227 | 228 |
|
228 | 229 |
public: |
229 | 230 |
|
230 | 231 |
/// \brief Constant for infinite upper bounds (capacities). |
231 | 232 |
/// |
232 | 233 |
/// Constant for infinite upper bounds (capacities). |
233 | 234 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
234 | 235 |
/// \c std::numeric_limits<Value>::max() otherwise. |
235 | 236 |
const Value INF; |
236 | 237 |
|
237 | 238 |
private: |
238 | 239 |
|
239 | 240 |
// Implementation of the First Eligible pivot rule |
240 | 241 |
class FirstEligiblePivotRule |
241 | 242 |
{ |
242 | 243 |
private: |
243 | 244 |
|
244 | 245 |
// References to the NetworkSimplex class |
245 | 246 |
const IntVector &_source; |
246 | 247 |
const IntVector &_target; |
247 | 248 |
const CostVector &_cost; |
248 |
const |
|
249 |
const BoolVector &_state; |
|
249 | 250 |
const CostVector &_pi; |
250 | 251 |
int &_in_arc; |
251 | 252 |
int _search_arc_num; |
252 | 253 |
|
253 | 254 |
// Pivot rule data |
254 | 255 |
int _next_arc; |
255 | 256 |
|
256 | 257 |
public: |
257 | 258 |
|
258 | 259 |
// Constructor |
259 | 260 |
FirstEligiblePivotRule(NetworkSimplex &ns) : |
260 | 261 |
_source(ns._source), _target(ns._target), |
261 | 262 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
262 | 263 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
263 | 264 |
_next_arc(0) |
264 | 265 |
{} |
265 | 266 |
|
266 | 267 |
// Find next entering arc |
267 | 268 |
bool findEnteringArc() { |
268 | 269 |
Cost c; |
269 |
for (int e = _next_arc; e |
|
270 |
for (int e = _next_arc; e != _search_arc_num; ++e) { |
|
270 | 271 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
271 | 272 |
if (c < 0) { |
272 | 273 |
_in_arc = e; |
273 | 274 |
_next_arc = e + 1; |
274 | 275 |
return true; |
275 | 276 |
} |
276 | 277 |
} |
277 |
for (int e = 0; e |
|
278 |
for (int e = 0; e != _next_arc; ++e) { |
|
278 | 279 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
279 | 280 |
if (c < 0) { |
280 | 281 |
_in_arc = e; |
281 | 282 |
_next_arc = e + 1; |
282 | 283 |
return true; |
283 | 284 |
} |
284 | 285 |
} |
285 | 286 |
return false; |
286 | 287 |
} |
287 | 288 |
|
288 | 289 |
}; //class FirstEligiblePivotRule |
289 | 290 |
|
290 | 291 |
|
291 | 292 |
// Implementation of the Best Eligible pivot rule |
292 | 293 |
class BestEligiblePivotRule |
293 | 294 |
{ |
294 | 295 |
private: |
295 | 296 |
|
296 | 297 |
// References to the NetworkSimplex class |
297 | 298 |
const IntVector &_source; |
298 | 299 |
const IntVector &_target; |
299 | 300 |
const CostVector &_cost; |
300 |
const |
|
301 |
const BoolVector &_state; |
|
301 | 302 |
const CostVector &_pi; |
302 | 303 |
int &_in_arc; |
303 | 304 |
int _search_arc_num; |
304 | 305 |
|
305 | 306 |
public: |
306 | 307 |
|
307 | 308 |
// Constructor |
308 | 309 |
BestEligiblePivotRule(NetworkSimplex &ns) : |
309 | 310 |
_source(ns._source), _target(ns._target), |
310 | 311 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
311 | 312 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num) |
312 | 313 |
{} |
313 | 314 |
|
314 | 315 |
// Find next entering arc |
315 | 316 |
bool findEnteringArc() { |
316 | 317 |
Cost c, min = 0; |
317 |
for (int e = 0; e |
|
318 |
for (int e = 0; e != _search_arc_num; ++e) { |
|
318 | 319 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
319 | 320 |
if (c < min) { |
320 | 321 |
min = c; |
321 | 322 |
_in_arc = e; |
322 | 323 |
} |
323 | 324 |
} |
324 | 325 |
return min < 0; |
325 | 326 |
} |
326 | 327 |
|
327 | 328 |
}; //class BestEligiblePivotRule |
328 | 329 |
|
329 | 330 |
|
330 | 331 |
// Implementation of the Block Search pivot rule |
331 | 332 |
class BlockSearchPivotRule |
332 | 333 |
{ |
333 | 334 |
private: |
334 | 335 |
|
335 | 336 |
// References to the NetworkSimplex class |
336 | 337 |
const IntVector &_source; |
337 | 338 |
const IntVector &_target; |
338 | 339 |
const CostVector &_cost; |
339 |
const |
|
340 |
const BoolVector &_state; |
|
340 | 341 |
const CostVector &_pi; |
341 | 342 |
int &_in_arc; |
342 | 343 |
int _search_arc_num; |
343 | 344 |
|
344 | 345 |
// Pivot rule data |
345 | 346 |
int _block_size; |
346 | 347 |
int _next_arc; |
347 | 348 |
|
348 | 349 |
public: |
349 | 350 |
|
350 | 351 |
// Constructor |
351 | 352 |
BlockSearchPivotRule(NetworkSimplex &ns) : |
352 | 353 |
_source(ns._source), _target(ns._target), |
353 | 354 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
354 | 355 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
355 | 356 |
_next_arc(0) |
356 | 357 |
{ |
357 | 358 |
// The main parameters of the pivot rule |
358 |
const double BLOCK_SIZE_FACTOR = |
|
359 |
const double BLOCK_SIZE_FACTOR = 1.0; |
|
359 | 360 |
const int MIN_BLOCK_SIZE = 10; |
360 | 361 |
|
361 | 362 |
_block_size = std::max( int(BLOCK_SIZE_FACTOR * |
362 | 363 |
std::sqrt(double(_search_arc_num))), |
363 | 364 |
MIN_BLOCK_SIZE ); |
364 | 365 |
} |
365 | 366 |
|
366 | 367 |
// Find next entering arc |
367 | 368 |
bool findEnteringArc() { |
368 | 369 |
Cost c, min = 0; |
369 | 370 |
int cnt = _block_size; |
370 | 371 |
int e; |
371 |
for (e = _next_arc; e |
|
372 |
for (e = _next_arc; e != _search_arc_num; ++e) { |
|
372 | 373 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
373 | 374 |
if (c < min) { |
374 | 375 |
min = c; |
375 | 376 |
_in_arc = e; |
376 | 377 |
} |
377 | 378 |
if (--cnt == 0) { |
378 | 379 |
if (min < 0) goto search_end; |
379 | 380 |
cnt = _block_size; |
380 | 381 |
} |
381 | 382 |
} |
382 |
for (e = 0; e |
|
383 |
for (e = 0; e != _next_arc; ++e) { |
|
383 | 384 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
384 | 385 |
if (c < min) { |
385 | 386 |
min = c; |
386 | 387 |
_in_arc = e; |
387 | 388 |
} |
388 | 389 |
if (--cnt == 0) { |
389 | 390 |
if (min < 0) goto search_end; |
390 | 391 |
cnt = _block_size; |
391 | 392 |
} |
392 | 393 |
} |
393 | 394 |
if (min >= 0) return false; |
394 | 395 |
|
395 | 396 |
search_end: |
396 | 397 |
_next_arc = e; |
397 | 398 |
return true; |
398 | 399 |
} |
399 | 400 |
|
400 | 401 |
}; //class BlockSearchPivotRule |
401 | 402 |
|
402 | 403 |
|
403 | 404 |
// Implementation of the Candidate List pivot rule |
404 | 405 |
class CandidateListPivotRule |
405 | 406 |
{ |
406 | 407 |
private: |
407 | 408 |
|
408 | 409 |
// References to the NetworkSimplex class |
409 | 410 |
const IntVector &_source; |
410 | 411 |
const IntVector &_target; |
411 | 412 |
const CostVector &_cost; |
412 |
const |
|
413 |
const BoolVector &_state; |
|
413 | 414 |
const CostVector &_pi; |
414 | 415 |
int &_in_arc; |
415 | 416 |
int _search_arc_num; |
416 | 417 |
|
417 | 418 |
// Pivot rule data |
418 | 419 |
IntVector _candidates; |
419 | 420 |
int _list_length, _minor_limit; |
420 | 421 |
int _curr_length, _minor_count; |
421 | 422 |
int _next_arc; |
422 | 423 |
|
423 | 424 |
public: |
424 | 425 |
|
425 | 426 |
/// Constructor |
426 | 427 |
CandidateListPivotRule(NetworkSimplex &ns) : |
427 | 428 |
_source(ns._source), _target(ns._target), |
428 | 429 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
429 | 430 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
430 | 431 |
_next_arc(0) |
431 | 432 |
{ |
432 | 433 |
// The main parameters of the pivot rule |
433 | 434 |
const double LIST_LENGTH_FACTOR = 0.25; |
434 | 435 |
const int MIN_LIST_LENGTH = 10; |
435 | 436 |
const double MINOR_LIMIT_FACTOR = 0.1; |
436 | 437 |
const int MIN_MINOR_LIMIT = 3; |
437 | 438 |
|
438 | 439 |
_list_length = std::max( int(LIST_LENGTH_FACTOR * |
439 | 440 |
std::sqrt(double(_search_arc_num))), |
440 | 441 |
MIN_LIST_LENGTH ); |
441 | 442 |
_minor_limit = std::max( int(MINOR_LIMIT_FACTOR * _list_length), |
442 | 443 |
MIN_MINOR_LIMIT ); |
443 | 444 |
_curr_length = _minor_count = 0; |
444 | 445 |
_candidates.resize(_list_length); |
445 | 446 |
} |
446 | 447 |
|
447 | 448 |
/// Find next entering arc |
448 | 449 |
bool findEnteringArc() { |
449 | 450 |
Cost min, c; |
450 | 451 |
int e; |
451 | 452 |
if (_curr_length > 0 && _minor_count < _minor_limit) { |
452 | 453 |
// Minor iteration: select the best eligible arc from the |
453 | 454 |
// current candidate list |
454 | 455 |
++_minor_count; |
455 | 456 |
min = 0; |
456 | 457 |
for (int i = 0; i < _curr_length; ++i) { |
457 | 458 |
e = _candidates[i]; |
458 | 459 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
459 | 460 |
if (c < min) { |
460 | 461 |
min = c; |
461 | 462 |
_in_arc = e; |
462 | 463 |
} |
463 | 464 |
else if (c >= 0) { |
464 | 465 |
_candidates[i--] = _candidates[--_curr_length]; |
465 | 466 |
} |
466 | 467 |
} |
467 | 468 |
if (min < 0) return true; |
468 | 469 |
} |
469 | 470 |
|
470 | 471 |
// Major iteration: build a new candidate list |
471 | 472 |
min = 0; |
472 | 473 |
_curr_length = 0; |
473 |
for (e = _next_arc; e |
|
474 |
for (e = _next_arc; e != _search_arc_num; ++e) { |
|
474 | 475 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
475 | 476 |
if (c < 0) { |
476 | 477 |
_candidates[_curr_length++] = e; |
477 | 478 |
if (c < min) { |
478 | 479 |
min = c; |
479 | 480 |
_in_arc = e; |
480 | 481 |
} |
481 | 482 |
if (_curr_length == _list_length) goto search_end; |
482 | 483 |
} |
483 | 484 |
} |
484 |
for (e = 0; e |
|
485 |
for (e = 0; e != _next_arc; ++e) { |
|
485 | 486 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
486 | 487 |
if (c < 0) { |
487 | 488 |
_candidates[_curr_length++] = e; |
488 | 489 |
if (c < min) { |
489 | 490 |
min = c; |
490 | 491 |
_in_arc = e; |
491 | 492 |
} |
492 | 493 |
if (_curr_length == _list_length) goto search_end; |
493 | 494 |
} |
494 | 495 |
} |
495 | 496 |
if (_curr_length == 0) return false; |
496 | 497 |
|
497 | 498 |
search_end: |
498 | 499 |
_minor_count = 1; |
499 | 500 |
_next_arc = e; |
500 | 501 |
return true; |
501 | 502 |
} |
502 | 503 |
|
503 | 504 |
}; //class CandidateListPivotRule |
504 | 505 |
|
505 | 506 |
|
506 | 507 |
// Implementation of the Altering Candidate List pivot rule |
507 | 508 |
class AlteringListPivotRule |
508 | 509 |
{ |
509 | 510 |
private: |
510 | 511 |
|
511 | 512 |
// References to the NetworkSimplex class |
512 | 513 |
const IntVector &_source; |
513 | 514 |
const IntVector &_target; |
514 | 515 |
const CostVector &_cost; |
515 |
const |
|
516 |
const BoolVector &_state; |
|
516 | 517 |
const CostVector &_pi; |
517 | 518 |
int &_in_arc; |
518 | 519 |
int _search_arc_num; |
519 | 520 |
|
520 | 521 |
// Pivot rule data |
521 | 522 |
int _block_size, _head_length, _curr_length; |
522 | 523 |
int _next_arc; |
523 | 524 |
IntVector _candidates; |
524 | 525 |
CostVector _cand_cost; |
525 | 526 |
|
526 | 527 |
// Functor class to compare arcs during sort of the candidate list |
527 | 528 |
class SortFunc |
528 | 529 |
{ |
529 | 530 |
private: |
530 | 531 |
const CostVector &_map; |
531 | 532 |
public: |
532 | 533 |
SortFunc(const CostVector &map) : _map(map) {} |
533 | 534 |
bool operator()(int left, int right) { |
534 | 535 |
return _map[left] > _map[right]; |
535 | 536 |
} |
536 | 537 |
}; |
537 | 538 |
|
538 | 539 |
SortFunc _sort_func; |
539 | 540 |
|
540 | 541 |
public: |
541 | 542 |
|
542 | 543 |
// Constructor |
543 | 544 |
AlteringListPivotRule(NetworkSimplex &ns) : |
544 | 545 |
_source(ns._source), _target(ns._target), |
545 | 546 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
546 | 547 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
547 | 548 |
_next_arc(0), _cand_cost(ns._search_arc_num), _sort_func(_cand_cost) |
548 | 549 |
{ |
549 | 550 |
// The main parameters of the pivot rule |
550 | 551 |
const double BLOCK_SIZE_FACTOR = 1.0; |
551 | 552 |
const int MIN_BLOCK_SIZE = 10; |
552 | 553 |
const double HEAD_LENGTH_FACTOR = 0.1; |
553 | 554 |
const int MIN_HEAD_LENGTH = 3; |
554 | 555 |
|
555 | 556 |
_block_size = std::max( int(BLOCK_SIZE_FACTOR * |
556 | 557 |
std::sqrt(double(_search_arc_num))), |
557 | 558 |
MIN_BLOCK_SIZE ); |
558 | 559 |
_head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size), |
559 | 560 |
MIN_HEAD_LENGTH ); |
560 | 561 |
_candidates.resize(_head_length + _block_size); |
561 | 562 |
_curr_length = 0; |
562 | 563 |
} |
563 | 564 |
|
564 | 565 |
// Find next entering arc |
565 | 566 |
bool findEnteringArc() { |
566 | 567 |
// Check the current candidate list |
567 | 568 |
int e; |
568 |
for (int i = 0; i |
|
569 |
for (int i = 0; i != _curr_length; ++i) { |
|
569 | 570 |
e = _candidates[i]; |
570 | 571 |
_cand_cost[e] = _state[e] * |
571 | 572 |
(_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
572 | 573 |
if (_cand_cost[e] >= 0) { |
573 | 574 |
_candidates[i--] = _candidates[--_curr_length]; |
574 | 575 |
} |
575 | 576 |
} |
576 | 577 |
|
577 | 578 |
// Extend the list |
578 | 579 |
int cnt = _block_size; |
579 | 580 |
int limit = _head_length; |
580 | 581 |
|
581 |
for (e = _next_arc; e |
|
582 |
for (e = _next_arc; e != _search_arc_num; ++e) { |
|
582 | 583 |
_cand_cost[e] = _state[e] * |
583 | 584 |
(_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
584 | 585 |
if (_cand_cost[e] < 0) { |
585 | 586 |
_candidates[_curr_length++] = e; |
586 | 587 |
} |
587 | 588 |
if (--cnt == 0) { |
588 | 589 |
if (_curr_length > limit) goto search_end; |
589 | 590 |
limit = 0; |
590 | 591 |
cnt = _block_size; |
591 | 592 |
} |
592 | 593 |
} |
593 |
for (e = 0; e |
|
594 |
for (e = 0; e != _next_arc; ++e) { |
|
594 | 595 |
_cand_cost[e] = _state[e] * |
595 | 596 |
(_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
596 | 597 |
if (_cand_cost[e] < 0) { |
597 | 598 |
_candidates[_curr_length++] = e; |
598 | 599 |
} |
599 | 600 |
if (--cnt == 0) { |
600 | 601 |
if (_curr_length > limit) goto search_end; |
601 | 602 |
limit = 0; |
602 | 603 |
cnt = _block_size; |
603 | 604 |
} |
604 | 605 |
} |
605 | 606 |
if (_curr_length == 0) return false; |
606 | 607 |
|
607 | 608 |
search_end: |
608 | 609 |
|
609 | 610 |
// Make heap of the candidate list (approximating a partial sort) |
610 | 611 |
make_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
611 | 612 |
_sort_func ); |
612 | 613 |
|
613 | 614 |
// Pop the first element of the heap |
614 | 615 |
_in_arc = _candidates[0]; |
615 | 616 |
_next_arc = e; |
616 | 617 |
pop_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
617 | 618 |
_sort_func ); |
618 | 619 |
_curr_length = std::min(_head_length, _curr_length - 1); |
619 | 620 |
return true; |
620 | 621 |
} |
621 | 622 |
|
622 | 623 |
}; //class AlteringListPivotRule |
623 | 624 |
|
624 | 625 |
public: |
625 | 626 |
|
626 | 627 |
/// \brief Constructor. |
627 | 628 |
/// |
628 | 629 |
/// The constructor of the class. |
629 | 630 |
/// |
630 | 631 |
/// \param graph The digraph the algorithm runs on. |
631 | 632 |
/// \param arc_mixing Indicate if the arcs have to be stored in a |
632 | 633 |
/// mixed order in the internal data structure. |
633 | 634 |
/// In special cases, it could lead to better overall performance, |
634 | 635 |
/// but it is usually slower. Therefore it is disabled by default. |
635 | 636 |
NetworkSimplex(const GR& graph, bool arc_mixing = false) : |
636 | 637 |
_graph(graph), _node_id(graph), _arc_id(graph), |
637 | 638 |
_arc_mixing(arc_mixing), |
638 | 639 |
MAX(std::numeric_limits<Value>::max()), |
639 | 640 |
INF(std::numeric_limits<Value>::has_infinity ? |
640 | 641 |
std::numeric_limits<Value>::infinity() : MAX) |
641 | 642 |
{ |
642 | 643 |
// Check the number types |
643 | 644 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
644 | 645 |
"The flow type of NetworkSimplex must be signed"); |
645 | 646 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
646 | 647 |
"The cost type of NetworkSimplex must be signed"); |
647 | 648 |
|
648 | 649 |
// Reset data structures |
649 | 650 |
reset(); |
650 | 651 |
} |
651 | 652 |
|
652 | 653 |
/// \name Parameters |
653 | 654 |
/// The parameters of the algorithm can be specified using these |
654 | 655 |
/// functions. |
655 | 656 |
|
656 | 657 |
/// @{ |
657 | 658 |
|
658 | 659 |
/// \brief Set the lower bounds on the arcs. |
659 | 660 |
/// |
660 | 661 |
/// This function sets the lower bounds on the arcs. |
661 | 662 |
/// If it is not used before calling \ref run(), the lower bounds |
662 | 663 |
/// will be set to zero on all arcs. |
663 | 664 |
/// |
664 | 665 |
/// \param map An arc map storing the lower bounds. |
665 | 666 |
/// Its \c Value type must be convertible to the \c Value type |
666 | 667 |
/// of the algorithm. |
667 | 668 |
/// |
668 | 669 |
/// \return <tt>(*this)</tt> |
669 | 670 |
template <typename LowerMap> |
670 | 671 |
NetworkSimplex& lowerMap(const LowerMap& map) { |
671 | 672 |
_have_lower = true; |
672 | 673 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
673 | 674 |
_lower[_arc_id[a]] = map[a]; |
674 | 675 |
} |
675 | 676 |
return *this; |
676 | 677 |
} |
677 | 678 |
|
678 | 679 |
/// \brief Set the upper bounds (capacities) on the arcs. |
679 | 680 |
/// |
680 | 681 |
/// This function sets the upper bounds (capacities) on the arcs. |
681 | 682 |
/// If it is not used before calling \ref run(), the upper bounds |
682 | 683 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
683 | 684 |
/// unbounded from above). |
684 | 685 |
/// |
685 | 686 |
/// \param map An arc map storing the upper bounds. |
686 | 687 |
/// Its \c Value type must be convertible to the \c Value type |
687 | 688 |
/// of the algorithm. |
688 | 689 |
/// |
689 | 690 |
/// \return <tt>(*this)</tt> |
690 | 691 |
template<typename UpperMap> |
691 | 692 |
NetworkSimplex& upperMap(const UpperMap& map) { |
692 | 693 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
693 | 694 |
_upper[_arc_id[a]] = map[a]; |
694 | 695 |
} |
695 | 696 |
return *this; |
696 | 697 |
} |
697 | 698 |
|
698 | 699 |
/// \brief Set the costs of the arcs. |
699 | 700 |
/// |
700 | 701 |
/// This function sets the costs of the arcs. |
701 | 702 |
/// If it is not used before calling \ref run(), the costs |
702 | 703 |
/// will be set to \c 1 on all arcs. |
703 | 704 |
/// |
704 | 705 |
/// \param map An arc map storing the costs. |
705 | 706 |
/// Its \c Value type must be convertible to the \c Cost type |
706 | 707 |
/// of the algorithm. |
707 | 708 |
/// |
708 | 709 |
/// \return <tt>(*this)</tt> |
709 | 710 |
template<typename CostMap> |
710 | 711 |
NetworkSimplex& costMap(const CostMap& map) { |
711 | 712 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
712 | 713 |
_cost[_arc_id[a]] = map[a]; |
713 | 714 |
} |
714 | 715 |
return *this; |
715 | 716 |
} |
716 | 717 |
|
717 | 718 |
/// \brief Set the supply values of the nodes. |
718 | 719 |
/// |
719 | 720 |
/// This function sets the supply values of the nodes. |
720 | 721 |
/// If neither this function nor \ref stSupply() is used before |
721 | 722 |
/// calling \ref run(), the supply of each node will be set to zero. |
722 | 723 |
/// |
723 | 724 |
/// \param map A node map storing the supply values. |
724 | 725 |
/// Its \c Value type must be convertible to the \c Value type |
725 | 726 |
/// of the algorithm. |
726 | 727 |
/// |
727 | 728 |
/// \return <tt>(*this)</tt> |
728 | 729 |
template<typename SupplyMap> |
729 | 730 |
NetworkSimplex& supplyMap(const SupplyMap& map) { |
730 | 731 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
731 | 732 |
_supply[_node_id[n]] = map[n]; |
732 | 733 |
} |
733 | 734 |
return *this; |
734 | 735 |
} |
735 | 736 |
|
736 | 737 |
/// \brief Set single source and target nodes and a supply value. |
737 | 738 |
/// |
738 | 739 |
/// This function sets a single source node and a single target node |
739 | 740 |
/// and the required flow value. |
740 | 741 |
/// If neither this function nor \ref supplyMap() is used before |
741 | 742 |
/// calling \ref run(), the supply of each node will be set to zero. |
742 | 743 |
/// |
743 | 744 |
/// Using this function has the same effect as using \ref supplyMap() |
744 | 745 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
745 | 746 |
/// assigned to \c t and all other nodes have zero supply value. |
746 | 747 |
/// |
747 | 748 |
/// \param s The source node. |
748 | 749 |
/// \param t The target node. |
749 | 750 |
/// \param k The required amount of flow from node \c s to node \c t |
750 | 751 |
/// (i.e. the supply of \c s and the demand of \c t). |
751 | 752 |
/// |
752 | 753 |
/// \return <tt>(*this)</tt> |
753 | 754 |
NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) { |
754 | 755 |
for (int i = 0; i != _node_num; ++i) { |
755 | 756 |
_supply[i] = 0; |
756 | 757 |
} |
757 | 758 |
_supply[_node_id[s]] = k; |
758 | 759 |
_supply[_node_id[t]] = -k; |
759 | 760 |
return *this; |
760 | 761 |
} |
761 | 762 |
|
762 | 763 |
/// \brief Set the type of the supply constraints. |
763 | 764 |
/// |
764 | 765 |
/// This function sets the type of the supply/demand constraints. |
765 | 766 |
/// If it is not used before calling \ref run(), the \ref GEQ supply |
766 | 767 |
/// type will be used. |
767 | 768 |
/// |
768 | 769 |
/// For more information, see \ref SupplyType. |
769 | 770 |
/// |
770 | 771 |
/// \return <tt>(*this)</tt> |
771 | 772 |
NetworkSimplex& supplyType(SupplyType supply_type) { |
772 | 773 |
_stype = supply_type; |
773 | 774 |
return *this; |
774 | 775 |
} |
775 | 776 |
|
776 | 777 |
/// @} |
777 | 778 |
|
778 | 779 |
/// \name Execution Control |
779 | 780 |
/// The algorithm can be executed using \ref run(). |
780 | 781 |
|
781 | 782 |
/// @{ |
782 | 783 |
|
783 | 784 |
/// \brief Run the algorithm. |
784 | 785 |
/// |
785 | 786 |
/// This function runs the algorithm. |
... | ... |
@@ -1171,349 +1172,446 @@ |
1171 | 1172 |
else { |
1172 | 1173 |
// GEQ supply constraints |
1173 | 1174 |
_search_arc_num = _arc_num + _node_num; |
1174 | 1175 |
int f = _arc_num + _node_num; |
1175 | 1176 |
for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
1176 | 1177 |
_parent[u] = _root; |
1177 | 1178 |
_thread[u] = u + 1; |
1178 | 1179 |
_rev_thread[u + 1] = u; |
1179 | 1180 |
_succ_num[u] = 1; |
1180 | 1181 |
_last_succ[u] = u; |
1181 | 1182 |
if (_supply[u] <= 0) { |
1182 | 1183 |
_forward[u] = false; |
1183 | 1184 |
_pi[u] = 0; |
1184 | 1185 |
_pred[u] = e; |
1185 | 1186 |
_source[e] = _root; |
1186 | 1187 |
_target[e] = u; |
1187 | 1188 |
_cap[e] = INF; |
1188 | 1189 |
_flow[e] = -_supply[u]; |
1189 | 1190 |
_cost[e] = 0; |
1190 | 1191 |
_state[e] = STATE_TREE; |
1191 | 1192 |
} else { |
1192 | 1193 |
_forward[u] = true; |
1193 | 1194 |
_pi[u] = -ART_COST; |
1194 | 1195 |
_pred[u] = f; |
1195 | 1196 |
_source[f] = u; |
1196 | 1197 |
_target[f] = _root; |
1197 | 1198 |
_cap[f] = INF; |
1198 | 1199 |
_flow[f] = _supply[u]; |
1199 | 1200 |
_state[f] = STATE_TREE; |
1200 | 1201 |
_cost[f] = ART_COST; |
1201 | 1202 |
_source[e] = _root; |
1202 | 1203 |
_target[e] = u; |
1203 | 1204 |
_cap[e] = INF; |
1204 | 1205 |
_flow[e] = 0; |
1205 | 1206 |
_cost[e] = 0; |
1206 | 1207 |
_state[e] = STATE_LOWER; |
1207 | 1208 |
++f; |
1208 | 1209 |
} |
1209 | 1210 |
} |
1210 | 1211 |
_all_arc_num = f; |
1211 | 1212 |
} |
1212 | 1213 |
|
1213 | 1214 |
return true; |
1214 | 1215 |
} |
1215 | 1216 |
|
1216 | 1217 |
// Find the join node |
1217 | 1218 |
void findJoinNode() { |
1218 | 1219 |
int u = _source[in_arc]; |
1219 | 1220 |
int v = _target[in_arc]; |
1220 | 1221 |
while (u != v) { |
1221 | 1222 |
if (_succ_num[u] < _succ_num[v]) { |
1222 | 1223 |
u = _parent[u]; |
1223 | 1224 |
} else { |
1224 | 1225 |
v = _parent[v]; |
1225 | 1226 |
} |
1226 | 1227 |
} |
1227 | 1228 |
join = u; |
1228 | 1229 |
} |
1229 | 1230 |
|
1230 | 1231 |
// Find the leaving arc of the cycle and returns true if the |
1231 | 1232 |
// leaving arc is not the same as the entering arc |
1232 | 1233 |
bool findLeavingArc() { |
1233 | 1234 |
// Initialize first and second nodes according to the direction |
1234 | 1235 |
// of the cycle |
1235 | 1236 |
if (_state[in_arc] == STATE_LOWER) { |
1236 | 1237 |
first = _source[in_arc]; |
1237 | 1238 |
second = _target[in_arc]; |
1238 | 1239 |
} else { |
1239 | 1240 |
first = _target[in_arc]; |
1240 | 1241 |
second = _source[in_arc]; |
1241 | 1242 |
} |
1242 | 1243 |
delta = _cap[in_arc]; |
1243 | 1244 |
int result = 0; |
1244 | 1245 |
Value d; |
1245 | 1246 |
int e; |
1246 | 1247 |
|
1247 | 1248 |
// Search the cycle along the path form the first node to the root |
1248 | 1249 |
for (int u = first; u != join; u = _parent[u]) { |
1249 | 1250 |
e = _pred[u]; |
1250 | 1251 |
d = _forward[u] ? |
1251 | 1252 |
_flow[e] : (_cap[e] >= MAX ? INF : _cap[e] - _flow[e]); |
1252 | 1253 |
if (d < delta) { |
1253 | 1254 |
delta = d; |
1254 | 1255 |
u_out = u; |
1255 | 1256 |
result = 1; |
1256 | 1257 |
} |
1257 | 1258 |
} |
1258 | 1259 |
// Search the cycle along the path form the second node to the root |
1259 | 1260 |
for (int u = second; u != join; u = _parent[u]) { |
1260 | 1261 |
e = _pred[u]; |
1261 | 1262 |
d = _forward[u] ? |
1262 | 1263 |
(_cap[e] >= MAX ? INF : _cap[e] - _flow[e]) : _flow[e]; |
1263 | 1264 |
if (d <= delta) { |
1264 | 1265 |
delta = d; |
1265 | 1266 |
u_out = u; |
1266 | 1267 |
result = 2; |
1267 | 1268 |
} |
1268 | 1269 |
} |
1269 | 1270 |
|
1270 | 1271 |
if (result == 1) { |
1271 | 1272 |
u_in = first; |
1272 | 1273 |
v_in = second; |
1273 | 1274 |
} else { |
1274 | 1275 |
u_in = second; |
1275 | 1276 |
v_in = first; |
1276 | 1277 |
} |
1277 | 1278 |
return result != 0; |
1278 | 1279 |
} |
1279 | 1280 |
|
1280 | 1281 |
// Change _flow and _state vectors |
1281 | 1282 |
void changeFlow(bool change) { |
1282 | 1283 |
// Augment along the cycle |
1283 | 1284 |
if (delta > 0) { |
1284 | 1285 |
Value val = _state[in_arc] * delta; |
1285 | 1286 |
_flow[in_arc] += val; |
1286 | 1287 |
for (int u = _source[in_arc]; u != join; u = _parent[u]) { |
1287 | 1288 |
_flow[_pred[u]] += _forward[u] ? -val : val; |
1288 | 1289 |
} |
1289 | 1290 |
for (int u = _target[in_arc]; u != join; u = _parent[u]) { |
1290 | 1291 |
_flow[_pred[u]] += _forward[u] ? val : -val; |
1291 | 1292 |
} |
1292 | 1293 |
} |
1293 | 1294 |
// Update the state of the entering and leaving arcs |
1294 | 1295 |
if (change) { |
1295 | 1296 |
_state[in_arc] = STATE_TREE; |
1296 | 1297 |
_state[_pred[u_out]] = |
1297 | 1298 |
(_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; |
1298 | 1299 |
} else { |
1299 | 1300 |
_state[in_arc] = -_state[in_arc]; |
1300 | 1301 |
} |
1301 | 1302 |
} |
1302 | 1303 |
|
1303 | 1304 |
// Update the tree structure |
1304 | 1305 |
void updateTreeStructure() { |
1305 | 1306 |
int u, w; |
1306 | 1307 |
int old_rev_thread = _rev_thread[u_out]; |
1307 | 1308 |
int old_succ_num = _succ_num[u_out]; |
1308 | 1309 |
int old_last_succ = _last_succ[u_out]; |
1309 | 1310 |
v_out = _parent[u_out]; |
1310 | 1311 |
|
1311 | 1312 |
u = _last_succ[u_in]; // the last successor of u_in |
1312 | 1313 |
right = _thread[u]; // the node after it |
1313 | 1314 |
|
1314 | 1315 |
// Handle the case when old_rev_thread equals to v_in |
1315 | 1316 |
// (it also means that join and v_out coincide) |
1316 | 1317 |
if (old_rev_thread == v_in) { |
1317 | 1318 |
last = _thread[_last_succ[u_out]]; |
1318 | 1319 |
} else { |
1319 | 1320 |
last = _thread[v_in]; |
1320 | 1321 |
} |
1321 | 1322 |
|
1322 | 1323 |
// Update _thread and _parent along the stem nodes (i.e. the nodes |
1323 | 1324 |
// between u_in and u_out, whose parent have to be changed) |
1324 | 1325 |
_thread[v_in] = stem = u_in; |
1325 | 1326 |
_dirty_revs.clear(); |
1326 | 1327 |
_dirty_revs.push_back(v_in); |
1327 | 1328 |
par_stem = v_in; |
1328 | 1329 |
while (stem != u_out) { |
1329 | 1330 |
// Insert the next stem node into the thread list |
1330 | 1331 |
new_stem = _parent[stem]; |
1331 | 1332 |
_thread[u] = new_stem; |
1332 | 1333 |
_dirty_revs.push_back(u); |
1333 | 1334 |
|
1334 | 1335 |
// Remove the subtree of stem from the thread list |
1335 | 1336 |
w = _rev_thread[stem]; |
1336 | 1337 |
_thread[w] = right; |
1337 | 1338 |
_rev_thread[right] = w; |
1338 | 1339 |
|
1339 | 1340 |
// Change the parent node and shift stem nodes |
1340 | 1341 |
_parent[stem] = par_stem; |
1341 | 1342 |
par_stem = stem; |
1342 | 1343 |
stem = new_stem; |
1343 | 1344 |
|
1344 | 1345 |
// Update u and right |
1345 | 1346 |
u = _last_succ[stem] == _last_succ[par_stem] ? |
1346 | 1347 |
_rev_thread[par_stem] : _last_succ[stem]; |
1347 | 1348 |
right = _thread[u]; |
1348 | 1349 |
} |
1349 | 1350 |
_parent[u_out] = par_stem; |
1350 | 1351 |
_thread[u] = last; |
1351 | 1352 |
_rev_thread[last] = u; |
1352 | 1353 |
_last_succ[u_out] = u; |
1353 | 1354 |
|
1354 | 1355 |
// Remove the subtree of u_out from the thread list except for |
1355 | 1356 |
// the case when old_rev_thread equals to v_in |
1356 | 1357 |
// (it also means that join and v_out coincide) |
1357 | 1358 |
if (old_rev_thread != v_in) { |
1358 | 1359 |
_thread[old_rev_thread] = right; |
1359 | 1360 |
_rev_thread[right] = old_rev_thread; |
1360 | 1361 |
} |
1361 | 1362 |
|
1362 | 1363 |
// Update _rev_thread using the new _thread values |
1363 |
for (int i = 0; i |
|
1364 |
for (int i = 0; i != int(_dirty_revs.size()); ++i) { |
|
1364 | 1365 |
u = _dirty_revs[i]; |
1365 | 1366 |
_rev_thread[_thread[u]] = u; |
1366 | 1367 |
} |
1367 | 1368 |
|
1368 | 1369 |
// Update _pred, _forward, _last_succ and _succ_num for the |
1369 | 1370 |
// stem nodes from u_out to u_in |
1370 | 1371 |
int tmp_sc = 0, tmp_ls = _last_succ[u_out]; |
1371 | 1372 |
u = u_out; |
1372 | 1373 |
while (u != u_in) { |
1373 | 1374 |
w = _parent[u]; |
1374 | 1375 |
_pred[u] = _pred[w]; |
1375 | 1376 |
_forward[u] = !_forward[w]; |
1376 | 1377 |
tmp_sc += _succ_num[u] - _succ_num[w]; |
1377 | 1378 |
_succ_num[u] = tmp_sc; |
1378 | 1379 |
_last_succ[w] = tmp_ls; |
1379 | 1380 |
u = w; |
1380 | 1381 |
} |
1381 | 1382 |
_pred[u_in] = in_arc; |
1382 | 1383 |
_forward[u_in] = (u_in == _source[in_arc]); |
1383 | 1384 |
_succ_num[u_in] = old_succ_num; |
1384 | 1385 |
|
1385 | 1386 |
// Set limits for updating _last_succ form v_in and v_out |
1386 | 1387 |
// towards the root |
1387 | 1388 |
int up_limit_in = -1; |
1388 | 1389 |
int up_limit_out = -1; |
1389 | 1390 |
if (_last_succ[join] == v_in) { |
1390 | 1391 |
up_limit_out = join; |
1391 | 1392 |
} else { |
1392 | 1393 |
up_limit_in = join; |
1393 | 1394 |
} |
1394 | 1395 |
|
1395 | 1396 |
// Update _last_succ from v_in towards the root |
1396 | 1397 |
for (u = v_in; u != up_limit_in && _last_succ[u] == v_in; |
1397 | 1398 |
u = _parent[u]) { |
1398 | 1399 |
_last_succ[u] = _last_succ[u_out]; |
1399 | 1400 |
} |
1400 | 1401 |
// Update _last_succ from v_out towards the root |
1401 | 1402 |
if (join != old_rev_thread && v_in != old_rev_thread) { |
1402 | 1403 |
for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
1403 | 1404 |
u = _parent[u]) { |
1404 | 1405 |
_last_succ[u] = old_rev_thread; |
1405 | 1406 |
} |
1406 | 1407 |
} else { |
1407 | 1408 |
for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
1408 | 1409 |
u = _parent[u]) { |
1409 | 1410 |
_last_succ[u] = _last_succ[u_out]; |
1410 | 1411 |
} |
1411 | 1412 |
} |
1412 | 1413 |
|
1413 | 1414 |
// Update _succ_num from v_in to join |
1414 | 1415 |
for (u = v_in; u != join; u = _parent[u]) { |
1415 | 1416 |
_succ_num[u] += old_succ_num; |
1416 | 1417 |
} |
1417 | 1418 |
// Update _succ_num from v_out to join |
1418 | 1419 |
for (u = v_out; u != join; u = _parent[u]) { |
1419 | 1420 |
_succ_num[u] -= old_succ_num; |
1420 | 1421 |
} |
1421 | 1422 |
} |
1422 | 1423 |
|
1423 | 1424 |
// Update potentials |
1424 | 1425 |
void updatePotential() { |
1425 | 1426 |
Cost sigma = _forward[u_in] ? |
1426 | 1427 |
_pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] : |
1427 | 1428 |
_pi[v_in] - _pi[u_in] + _cost[_pred[u_in]]; |
1428 | 1429 |
// Update potentials in the subtree, which has been moved |
1429 | 1430 |
int end = _thread[_last_succ[u_in]]; |
1430 | 1431 |
for (int u = u_in; u != end; u = _thread[u]) { |
1431 | 1432 |
_pi[u] += sigma; |
1432 | 1433 |
} |
1433 | 1434 |
} |
1434 | 1435 |
|
1436 |
// Heuristic initial pivots |
|
1437 |
bool initialPivots() { |
|
1438 |
Value curr, total = 0; |
|
1439 |
std::vector<Node> supply_nodes, demand_nodes; |
|
1440 |
for (NodeIt u(_graph); u != INVALID; ++u) { |
|
1441 |
curr = _supply[_node_id[u]]; |
|
1442 |
if (curr > 0) { |
|
1443 |
total += curr; |
|
1444 |
supply_nodes.push_back(u); |
|
1445 |
} |
|
1446 |
else if (curr < 0) { |
|
1447 |
demand_nodes.push_back(u); |
|
1448 |
} |
|
1449 |
} |
|
1450 |
if (_sum_supply > 0) total -= _sum_supply; |
|
1451 |
if (total <= 0) return true; |
|
1452 |
|
|
1453 |
IntVector arc_vector; |
|
1454 |
if (_sum_supply >= 0) { |
|
1455 |
if (supply_nodes.size() == 1 && demand_nodes.size() == 1) { |
|
1456 |
// Perform a reverse graph search from the sink to the source |
|
1457 |
typename GR::template NodeMap<bool> reached(_graph, false); |
|
1458 |
Node s = supply_nodes[0], t = demand_nodes[0]; |
|
1459 |
std::vector<Node> stack; |
|
1460 |
reached[t] = true; |
|
1461 |
stack.push_back(t); |
|
1462 |
while (!stack.empty()) { |
|
1463 |
Node u, v = stack.back(); |
|
1464 |
stack.pop_back(); |
|
1465 |
if (v == s) break; |
|
1466 |
for (InArcIt a(_graph, v); a != INVALID; ++a) { |
|
1467 |
if (reached[u = _graph.source(a)]) continue; |
|
1468 |
int j = _arc_id[a]; |
|
1469 |
if (_cap[j] >= total) { |
|
1470 |
arc_vector.push_back(j); |
|
1471 |
reached[u] = true; |
|
1472 |
stack.push_back(u); |
|
1473 |
} |
|
1474 |
} |
|
1475 |
} |
|
1476 |
} else { |
|
1477 |
// Find the min. cost incomming arc for each demand node |
|
1478 |
for (int i = 0; i != int(demand_nodes.size()); ++i) { |
|
1479 |
Node v = demand_nodes[i]; |
|
1480 |
Cost c, min_cost = std::numeric_limits<Cost>::max(); |
|
1481 |
Arc min_arc = INVALID; |
|
1482 |
for (InArcIt a(_graph, v); a != INVALID; ++a) { |
|
1483 |
c = _cost[_arc_id[a]]; |
|
1484 |
if (c < min_cost) { |
|
1485 |
min_cost = c; |
|
1486 |
min_arc = a; |
|
1487 |
} |
|
1488 |
} |
|
1489 |
if (min_arc != INVALID) { |
|
1490 |
arc_vector.push_back(_arc_id[min_arc]); |
|
1491 |
} |
|
1492 |
} |
|
1493 |
} |
|
1494 |
} else { |
|
1495 |
// Find the min. cost outgoing arc for each supply node |
|
1496 |
for (int i = 0; i != int(supply_nodes.size()); ++i) { |
|
1497 |
Node u = supply_nodes[i]; |
|
1498 |
Cost c, min_cost = std::numeric_limits<Cost>::max(); |
|
1499 |
Arc min_arc = INVALID; |
|
1500 |
for (OutArcIt a(_graph, u); a != INVALID; ++a) { |
|
1501 |
c = _cost[_arc_id[a]]; |
|
1502 |
if (c < min_cost) { |
|
1503 |
min_cost = c; |
|
1504 |
min_arc = a; |
|
1505 |
} |
|
1506 |
} |
|
1507 |
if (min_arc != INVALID) { |
|
1508 |
arc_vector.push_back(_arc_id[min_arc]); |
|
1509 |
} |
|
1510 |
} |
|
1511 |
} |
|
1512 |
|
|
1513 |
// Perform heuristic initial pivots |
|
1514 |
for (int i = 0; i != int(arc_vector.size()); ++i) { |
|
1515 |
in_arc = arc_vector[i]; |
|
1516 |
if (_state[in_arc] * (_cost[in_arc] + _pi[_source[in_arc]] - |
|
1517 |
_pi[_target[in_arc]]) >= 0) continue; |
|
1518 |
findJoinNode(); |
|
1519 |
bool change = findLeavingArc(); |
|
1520 |
if (delta >= MAX) return false; |
|
1521 |
changeFlow(change); |
|
1522 |
if (change) { |
|
1523 |
updateTreeStructure(); |
|
1524 |
updatePotential(); |
|
1525 |
} |
|
1526 |
} |
|
1527 |
return true; |
|
1528 |
} |
|
1529 |
|
|
1435 | 1530 |
// Execute the algorithm |
1436 | 1531 |
ProblemType start(PivotRule pivot_rule) { |
1437 | 1532 |
// Select the pivot rule implementation |
1438 | 1533 |
switch (pivot_rule) { |
1439 | 1534 |
case FIRST_ELIGIBLE: |
1440 | 1535 |
return start<FirstEligiblePivotRule>(); |
1441 | 1536 |
case BEST_ELIGIBLE: |
1442 | 1537 |
return start<BestEligiblePivotRule>(); |
1443 | 1538 |
case BLOCK_SEARCH: |
1444 | 1539 |
return start<BlockSearchPivotRule>(); |
1445 | 1540 |
case CANDIDATE_LIST: |
1446 | 1541 |
return start<CandidateListPivotRule>(); |
1447 | 1542 |
case ALTERING_LIST: |
1448 | 1543 |
return start<AlteringListPivotRule>(); |
1449 | 1544 |
} |
1450 | 1545 |
return INFEASIBLE; // avoid warning |
1451 | 1546 |
} |
1452 | 1547 |
|
1453 | 1548 |
template <typename PivotRuleImpl> |
1454 | 1549 |
ProblemType start() { |
1455 | 1550 |
PivotRuleImpl pivot(*this); |
1456 | 1551 |
|
1552 |
// Perform heuristic initial pivots |
|
1553 |
if (!initialPivots()) return UNBOUNDED; |
|
1554 |
|
|
1457 | 1555 |
// Execute the Network Simplex algorithm |
1458 | 1556 |
while (pivot.findEnteringArc()) { |
1459 | 1557 |
findJoinNode(); |
1460 | 1558 |
bool change = findLeavingArc(); |
1461 | 1559 |
if (delta >= MAX) return UNBOUNDED; |
1462 | 1560 |
changeFlow(change); |
1463 | 1561 |
if (change) { |
1464 | 1562 |
updateTreeStructure(); |
1465 | 1563 |
updatePotential(); |
1466 | 1564 |
} |
1467 | 1565 |
} |
1468 | 1566 |
|
1469 | 1567 |
// Check feasibility |
1470 | 1568 |
for (int e = _search_arc_num; e != _all_arc_num; ++e) { |
1471 | 1569 |
if (_flow[e] != 0) return INFEASIBLE; |
1472 | 1570 |
} |
1473 | 1571 |
|
1474 | 1572 |
// Transform the solution and the supply map to the original form |
1475 | 1573 |
if (_have_lower) { |
1476 | 1574 |
for (int i = 0; i != _arc_num; ++i) { |
1477 | 1575 |
Value c = _lower[i]; |
1478 | 1576 |
if (c != 0) { |
1479 | 1577 |
_flow[i] += c; |
1480 | 1578 |
_supply[_source[i]] += c; |
1481 | 1579 |
_supply[_target[i]] -= c; |
1482 | 1580 |
} |
1483 | 1581 |
} |
1484 | 1582 |
} |
1485 | 1583 |
|
1486 | 1584 |
// Shift potentials to meet the requirements of the GEQ/LEQ type |
1487 | 1585 |
// optimality conditions |
1488 | 1586 |
if (_sum_supply == 0) { |
1489 | 1587 |
if (_stype == GEQ) { |
1490 | 1588 |
Cost max_pot = std::numeric_limits<Cost>::min(); |
1491 | 1589 |
for (int i = 0; i != _node_num; ++i) { |
1492 | 1590 |
if (_pi[i] > max_pot) max_pot = _pi[i]; |
1493 | 1591 |
} |
1494 | 1592 |
if (max_pot > 0) { |
1495 | 1593 |
for (int i = 0; i != _node_num; ++i) |
1496 | 1594 |
_pi[i] -= max_pot; |
1497 | 1595 |
} |
1498 | 1596 |
} else { |
1499 | 1597 |
Cost min_pot = std::numeric_limits<Cost>::max(); |
1500 | 1598 |
for (int i = 0; i != _node_num; ++i) { |
1501 | 1599 |
if (_pi[i] < min_pot) min_pot = _pi[i]; |
1502 | 1600 |
} |
1503 | 1601 |
if (min_pot < 0) { |
1504 | 1602 |
for (int i = 0; i != _node_num; ++i) |
1505 | 1603 |
_pi[i] -= min_pot; |
1506 | 1604 |
} |
1507 | 1605 |
} |
1508 | 1606 |
} |
1509 | 1607 |
|
1510 | 1608 |
return OPTIMAL; |
1511 | 1609 |
} |
1512 | 1610 |
|
1513 | 1611 |
}; //class NetworkSimplex |
1514 | 1612 |
|
1515 | 1613 |
///@} |
1516 | 1614 |
|
1517 | 1615 |
} //namespace lemon |
1518 | 1616 |
|
1519 | 1617 |
#endif //LEMON_NETWORK_SIMPLEX_H |
0 comments (0 inline)