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