alpar@956
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@871
|
2 |
*
|
alpar@956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@871
|
4 |
*
|
alpar@1270
|
5 |
* Copyright (C) 2003-2013
|
kpeter@871
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@871
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@871
|
8 |
*
|
kpeter@871
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@871
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@871
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@871
|
12 |
*
|
kpeter@871
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@871
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@871
|
15 |
* purpose.
|
kpeter@871
|
16 |
*
|
kpeter@871
|
17 |
*/
|
kpeter@871
|
18 |
|
kpeter@871
|
19 |
#ifndef LEMON_CAPACITY_SCALING_H
|
kpeter@871
|
20 |
#define LEMON_CAPACITY_SCALING_H
|
kpeter@871
|
21 |
|
kpeter@872
|
22 |
/// \ingroup min_cost_flow_algs
|
kpeter@871
|
23 |
///
|
kpeter@871
|
24 |
/// \file
|
kpeter@872
|
25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow.
|
kpeter@871
|
26 |
|
kpeter@871
|
27 |
#include <vector>
|
kpeter@872
|
28 |
#include <limits>
|
kpeter@872
|
29 |
#include <lemon/core.h>
|
kpeter@871
|
30 |
#include <lemon/bin_heap.h>
|
kpeter@871
|
31 |
|
kpeter@871
|
32 |
namespace lemon {
|
kpeter@871
|
33 |
|
kpeter@873
|
34 |
/// \brief Default traits class of CapacityScaling algorithm.
|
kpeter@873
|
35 |
///
|
kpeter@873
|
36 |
/// Default traits class of CapacityScaling algorithm.
|
kpeter@873
|
37 |
/// \tparam GR Digraph type.
|
kpeter@878
|
38 |
/// \tparam V The number type used for flow amounts, capacity bounds
|
kpeter@873
|
39 |
/// and supply values. By default it is \c int.
|
kpeter@878
|
40 |
/// \tparam C The number type used for costs and potentials.
|
kpeter@873
|
41 |
/// By default it is the same as \c V.
|
kpeter@873
|
42 |
template <typename GR, typename V = int, typename C = V>
|
kpeter@873
|
43 |
struct CapacityScalingDefaultTraits
|
kpeter@873
|
44 |
{
|
kpeter@873
|
45 |
/// The type of the digraph
|
kpeter@873
|
46 |
typedef GR Digraph;
|
kpeter@873
|
47 |
/// The type of the flow amounts, capacity bounds and supply values
|
kpeter@873
|
48 |
typedef V Value;
|
kpeter@873
|
49 |
/// The type of the arc costs
|
kpeter@873
|
50 |
typedef C Cost;
|
kpeter@873
|
51 |
|
kpeter@873
|
52 |
/// \brief The type of the heap used for internal Dijkstra computations.
|
kpeter@873
|
53 |
///
|
kpeter@873
|
54 |
/// The type of the heap used for internal Dijkstra computations.
|
kpeter@873
|
55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
|
kpeter@873
|
56 |
/// its priority type must be \c Cost and its cross reference type
|
kpeter@873
|
57 |
/// must be \ref RangeMap "RangeMap<int>".
|
kpeter@873
|
58 |
typedef BinHeap<Cost, RangeMap<int> > Heap;
|
kpeter@873
|
59 |
};
|
kpeter@873
|
60 |
|
kpeter@872
|
61 |
/// \addtogroup min_cost_flow_algs
|
kpeter@871
|
62 |
/// @{
|
kpeter@871
|
63 |
|
kpeter@872
|
64 |
/// \brief Implementation of the Capacity Scaling algorithm for
|
kpeter@872
|
65 |
/// finding a \ref min_cost_flow "minimum cost flow".
|
kpeter@871
|
66 |
///
|
kpeter@871
|
67 |
/// \ref CapacityScaling implements the capacity scaling version
|
kpeter@872
|
68 |
/// of the successive shortest path algorithm for finding a
|
alpar@1221
|
69 |
/// \ref min_cost_flow "minimum cost flow" \cite amo93networkflows,
|
alpar@1221
|
70 |
/// \cite edmondskarp72theoretical. It is an efficient dual
|
kpeter@1217
|
71 |
/// solution method, which runs in polynomial time
|
kpeter@1254
|
72 |
/// \f$O(m\log U (n+m)\log n)\f$, where <i>U</i> denotes the maximum
|
kpeter@1217
|
73 |
/// of node supply and arc capacity values.
|
kpeter@871
|
74 |
///
|
kpeter@1165
|
75 |
/// This algorithm is typically slower than \ref CostScaling and
|
kpeter@1165
|
76 |
/// \ref NetworkSimplex, but in special cases, it can be more
|
kpeter@1165
|
77 |
/// efficient than them.
|
kpeter@1165
|
78 |
/// (For more information, see \ref min_cost_flow_algs "the module page".)
|
kpeter@1165
|
79 |
///
|
kpeter@872
|
80 |
/// Most of the parameters of the problem (except for the digraph)
|
kpeter@872
|
81 |
/// can be given using separate functions, and the algorithm can be
|
kpeter@872
|
82 |
/// executed using the \ref run() function. If some parameters are not
|
kpeter@872
|
83 |
/// specified, then default values will be used.
|
kpeter@871
|
84 |
///
|
kpeter@872
|
85 |
/// \tparam GR The digraph type the algorithm runs on.
|
kpeter@878
|
86 |
/// \tparam V The number type used for flow amounts, capacity bounds
|
kpeter@891
|
87 |
/// and supply values in the algorithm. By default, it is \c int.
|
kpeter@878
|
88 |
/// \tparam C The number type used for costs and potentials in the
|
kpeter@891
|
89 |
/// algorithm. By default, it is the same as \c V.
|
kpeter@891
|
90 |
/// \tparam TR The traits class that defines various types used by the
|
kpeter@891
|
91 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits
|
kpeter@891
|
92 |
/// "CapacityScalingDefaultTraits<GR, V, C>".
|
kpeter@891
|
93 |
/// In most cases, this parameter should not be set directly,
|
kpeter@891
|
94 |
/// consider to use the named template parameters instead.
|
kpeter@871
|
95 |
///
|
kpeter@1025
|
96 |
/// \warning Both \c V and \c C must be signed number types.
|
kpeter@1137
|
97 |
/// \warning Capacity bounds and supply values must be integer, but
|
kpeter@1137
|
98 |
/// arc costs can be arbitrary real numbers.
|
kpeter@1023
|
99 |
/// \warning This algorithm does not support negative costs for
|
kpeter@1023
|
100 |
/// arcs having infinite upper bound.
|
kpeter@873
|
101 |
#ifdef DOXYGEN
|
kpeter@873
|
102 |
template <typename GR, typename V, typename C, typename TR>
|
kpeter@873
|
103 |
#else
|
kpeter@873
|
104 |
template < typename GR, typename V = int, typename C = V,
|
kpeter@873
|
105 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> >
|
kpeter@873
|
106 |
#endif
|
kpeter@871
|
107 |
class CapacityScaling
|
kpeter@871
|
108 |
{
|
kpeter@872
|
109 |
public:
|
kpeter@871
|
110 |
|
kpeter@873
|
111 |
/// The type of the digraph
|
kpeter@873
|
112 |
typedef typename TR::Digraph Digraph;
|
kpeter@872
|
113 |
/// The type of the flow amounts, capacity bounds and supply values
|
kpeter@873
|
114 |
typedef typename TR::Value Value;
|
kpeter@872
|
115 |
/// The type of the arc costs
|
kpeter@873
|
116 |
typedef typename TR::Cost Cost;
|
kpeter@873
|
117 |
|
kpeter@873
|
118 |
/// The type of the heap used for internal Dijkstra computations
|
kpeter@873
|
119 |
typedef typename TR::Heap Heap;
|
kpeter@873
|
120 |
|
alpar@1250
|
121 |
/// \brief The \ref lemon::CapacityScalingDefaultTraits "traits class"
|
alpar@1250
|
122 |
/// of the algorithm
|
kpeter@873
|
123 |
typedef TR Traits;
|
kpeter@871
|
124 |
|
kpeter@871
|
125 |
public:
|
kpeter@871
|
126 |
|
kpeter@872
|
127 |
/// \brief Problem type constants for the \c run() function.
|
kpeter@872
|
128 |
///
|
kpeter@872
|
129 |
/// Enum type containing the problem type constants that can be
|
kpeter@872
|
130 |
/// returned by the \ref run() function of the algorithm.
|
kpeter@872
|
131 |
enum ProblemType {
|
kpeter@872
|
132 |
/// The problem has no feasible solution (flow).
|
kpeter@872
|
133 |
INFEASIBLE,
|
kpeter@872
|
134 |
/// The problem has optimal solution (i.e. it is feasible and
|
kpeter@872
|
135 |
/// bounded), and the algorithm has found optimal flow and node
|
kpeter@872
|
136 |
/// potentials (primal and dual solutions).
|
kpeter@872
|
137 |
OPTIMAL,
|
kpeter@872
|
138 |
/// The digraph contains an arc of negative cost and infinite
|
kpeter@872
|
139 |
/// upper bound. It means that the objective function is unbounded
|
kpeter@878
|
140 |
/// on that arc, however, note that it could actually be bounded
|
kpeter@872
|
141 |
/// over the feasible flows, but this algroithm cannot handle
|
kpeter@872
|
142 |
/// these cases.
|
kpeter@872
|
143 |
UNBOUNDED
|
kpeter@872
|
144 |
};
|
alpar@956
|
145 |
|
kpeter@872
|
146 |
private:
|
kpeter@872
|
147 |
|
kpeter@872
|
148 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
|
kpeter@872
|
149 |
|
kpeter@872
|
150 |
typedef std::vector<int> IntVector;
|
kpeter@872
|
151 |
typedef std::vector<Value> ValueVector;
|
kpeter@872
|
152 |
typedef std::vector<Cost> CostVector;
|
kpeter@910
|
153 |
typedef std::vector<char> BoolVector;
|
kpeter@910
|
154 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons
|
kpeter@871
|
155 |
|
kpeter@871
|
156 |
private:
|
kpeter@871
|
157 |
|
kpeter@872
|
158 |
// Data related to the underlying digraph
|
kpeter@872
|
159 |
const GR &_graph;
|
kpeter@872
|
160 |
int _node_num;
|
kpeter@872
|
161 |
int _arc_num;
|
kpeter@872
|
162 |
int _res_arc_num;
|
kpeter@872
|
163 |
int _root;
|
kpeter@872
|
164 |
|
kpeter@872
|
165 |
// Parameters of the problem
|
kpeter@1297
|
166 |
bool _has_lower;
|
kpeter@872
|
167 |
Value _sum_supply;
|
kpeter@872
|
168 |
|
kpeter@872
|
169 |
// Data structures for storing the digraph
|
kpeter@872
|
170 |
IntNodeMap _node_id;
|
kpeter@872
|
171 |
IntArcMap _arc_idf;
|
kpeter@872
|
172 |
IntArcMap _arc_idb;
|
kpeter@872
|
173 |
IntVector _first_out;
|
kpeter@872
|
174 |
BoolVector _forward;
|
kpeter@872
|
175 |
IntVector _source;
|
kpeter@872
|
176 |
IntVector _target;
|
kpeter@872
|
177 |
IntVector _reverse;
|
kpeter@872
|
178 |
|
kpeter@872
|
179 |
// Node and arc data
|
kpeter@872
|
180 |
ValueVector _lower;
|
kpeter@872
|
181 |
ValueVector _upper;
|
kpeter@872
|
182 |
CostVector _cost;
|
kpeter@872
|
183 |
ValueVector _supply;
|
kpeter@872
|
184 |
|
kpeter@872
|
185 |
ValueVector _res_cap;
|
kpeter@872
|
186 |
CostVector _pi;
|
kpeter@872
|
187 |
ValueVector _excess;
|
kpeter@872
|
188 |
IntVector _excess_nodes;
|
kpeter@872
|
189 |
IntVector _deficit_nodes;
|
kpeter@872
|
190 |
|
kpeter@872
|
191 |
Value _delta;
|
kpeter@876
|
192 |
int _factor;
|
kpeter@872
|
193 |
IntVector _pred;
|
kpeter@872
|
194 |
|
kpeter@872
|
195 |
public:
|
alpar@956
|
196 |
|
kpeter@872
|
197 |
/// \brief Constant for infinite upper bounds (capacities).
|
kpeter@871
|
198 |
///
|
kpeter@872
|
199 |
/// Constant for infinite upper bounds (capacities).
|
kpeter@872
|
200 |
/// It is \c std::numeric_limits<Value>::infinity() if available,
|
kpeter@872
|
201 |
/// \c std::numeric_limits<Value>::max() otherwise.
|
kpeter@872
|
202 |
const Value INF;
|
kpeter@872
|
203 |
|
kpeter@872
|
204 |
private:
|
kpeter@872
|
205 |
|
kpeter@872
|
206 |
// Special implementation of the Dijkstra algorithm for finding
|
kpeter@872
|
207 |
// shortest paths in the residual network of the digraph with
|
kpeter@872
|
208 |
// respect to the reduced arc costs and modifying the node
|
kpeter@872
|
209 |
// potentials according to the found distance labels.
|
kpeter@871
|
210 |
class ResidualDijkstra
|
kpeter@871
|
211 |
{
|
kpeter@871
|
212 |
private:
|
kpeter@871
|
213 |
|
kpeter@872
|
214 |
int _node_num;
|
kpeter@877
|
215 |
bool _geq;
|
kpeter@872
|
216 |
const IntVector &_first_out;
|
kpeter@872
|
217 |
const IntVector &_target;
|
kpeter@872
|
218 |
const CostVector &_cost;
|
kpeter@872
|
219 |
const ValueVector &_res_cap;
|
kpeter@872
|
220 |
const ValueVector &_excess;
|
kpeter@872
|
221 |
CostVector &_pi;
|
kpeter@872
|
222 |
IntVector &_pred;
|
alpar@956
|
223 |
|
kpeter@872
|
224 |
IntVector _proc_nodes;
|
kpeter@872
|
225 |
CostVector _dist;
|
alpar@956
|
226 |
|
kpeter@871
|
227 |
public:
|
kpeter@871
|
228 |
|
kpeter@872
|
229 |
ResidualDijkstra(CapacityScaling& cs) :
|
kpeter@877
|
230 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0),
|
kpeter@877
|
231 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
|
kpeter@877
|
232 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi),
|
kpeter@877
|
233 |
_pred(cs._pred), _dist(cs._node_num)
|
kpeter@871
|
234 |
{}
|
kpeter@871
|
235 |
|
kpeter@872
|
236 |
int run(int s, Value delta = 1) {
|
kpeter@873
|
237 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
|
kpeter@871
|
238 |
Heap heap(heap_cross_ref);
|
kpeter@871
|
239 |
heap.push(s, 0);
|
kpeter@872
|
240 |
_pred[s] = -1;
|
kpeter@871
|
241 |
_proc_nodes.clear();
|
kpeter@871
|
242 |
|
kpeter@872
|
243 |
// Process nodes
|
kpeter@871
|
244 |
while (!heap.empty() && _excess[heap.top()] > -delta) {
|
kpeter@872
|
245 |
int u = heap.top(), v;
|
kpeter@872
|
246 |
Cost d = heap.prio() + _pi[u], dn;
|
kpeter@871
|
247 |
_dist[u] = heap.prio();
|
kpeter@872
|
248 |
_proc_nodes.push_back(u);
|
kpeter@871
|
249 |
heap.pop();
|
kpeter@871
|
250 |
|
kpeter@872
|
251 |
// Traverse outgoing residual arcs
|
kpeter@877
|
252 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1;
|
kpeter@877
|
253 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
kpeter@872
|
254 |
if (_res_cap[a] < delta) continue;
|
kpeter@872
|
255 |
v = _target[a];
|
kpeter@872
|
256 |
switch (heap.state(v)) {
|
kpeter@871
|
257 |
case Heap::PRE_HEAP:
|
kpeter@872
|
258 |
heap.push(v, d + _cost[a] - _pi[v]);
|
kpeter@872
|
259 |
_pred[v] = a;
|
kpeter@871
|
260 |
break;
|
kpeter@871
|
261 |
case Heap::IN_HEAP:
|
kpeter@872
|
262 |
dn = d + _cost[a] - _pi[v];
|
kpeter@872
|
263 |
if (dn < heap[v]) {
|
kpeter@872
|
264 |
heap.decrease(v, dn);
|
kpeter@872
|
265 |
_pred[v] = a;
|
kpeter@871
|
266 |
}
|
kpeter@871
|
267 |
break;
|
kpeter@871
|
268 |
case Heap::POST_HEAP:
|
kpeter@871
|
269 |
break;
|
kpeter@871
|
270 |
}
|
kpeter@871
|
271 |
}
|
kpeter@871
|
272 |
}
|
kpeter@872
|
273 |
if (heap.empty()) return -1;
|
kpeter@871
|
274 |
|
kpeter@872
|
275 |
// Update potentials of processed nodes
|
kpeter@872
|
276 |
int t = heap.top();
|
kpeter@872
|
277 |
Cost dt = heap.prio();
|
kpeter@872
|
278 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) {
|
kpeter@872
|
279 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt;
|
kpeter@872
|
280 |
}
|
kpeter@871
|
281 |
|
kpeter@871
|
282 |
return t;
|
kpeter@871
|
283 |
}
|
kpeter@871
|
284 |
|
kpeter@871
|
285 |
}; //class ResidualDijkstra
|
kpeter@871
|
286 |
|
kpeter@871
|
287 |
public:
|
kpeter@871
|
288 |
|
kpeter@873
|
289 |
/// \name Named Template Parameters
|
kpeter@873
|
290 |
/// @{
|
kpeter@873
|
291 |
|
kpeter@873
|
292 |
template <typename T>
|
kpeter@873
|
293 |
struct SetHeapTraits : public Traits {
|
kpeter@873
|
294 |
typedef T Heap;
|
kpeter@873
|
295 |
};
|
kpeter@873
|
296 |
|
kpeter@873
|
297 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@873
|
298 |
/// \c Heap type.
|
kpeter@873
|
299 |
///
|
kpeter@873
|
300 |
/// \ref named-templ-param "Named parameter" for setting \c Heap
|
kpeter@873
|
301 |
/// type, which is used for internal Dijkstra computations.
|
kpeter@873
|
302 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
|
kpeter@873
|
303 |
/// its priority type must be \c Cost and its cross reference type
|
kpeter@873
|
304 |
/// must be \ref RangeMap "RangeMap<int>".
|
kpeter@873
|
305 |
template <typename T>
|
kpeter@873
|
306 |
struct SetHeap
|
kpeter@873
|
307 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
|
kpeter@873
|
308 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create;
|
kpeter@873
|
309 |
};
|
kpeter@873
|
310 |
|
kpeter@873
|
311 |
/// @}
|
kpeter@873
|
312 |
|
kpeter@941
|
313 |
protected:
|
kpeter@941
|
314 |
|
kpeter@941
|
315 |
CapacityScaling() {}
|
kpeter@941
|
316 |
|
kpeter@873
|
317 |
public:
|
kpeter@873
|
318 |
|
kpeter@872
|
319 |
/// \brief Constructor.
|
kpeter@871
|
320 |
///
|
kpeter@872
|
321 |
/// The constructor of the class.
|
kpeter@871
|
322 |
///
|
kpeter@872
|
323 |
/// \param graph The digraph the algorithm runs on.
|
kpeter@872
|
324 |
CapacityScaling(const GR& graph) :
|
kpeter@872
|
325 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
|
kpeter@872
|
326 |
INF(std::numeric_limits<Value>::has_infinity ?
|
kpeter@872
|
327 |
std::numeric_limits<Value>::infinity() :
|
kpeter@872
|
328 |
std::numeric_limits<Value>::max())
|
kpeter@871
|
329 |
{
|
kpeter@878
|
330 |
// Check the number types
|
kpeter@872
|
331 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
|
kpeter@872
|
332 |
"The flow type of CapacityScaling must be signed");
|
kpeter@872
|
333 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
|
kpeter@872
|
334 |
"The cost type of CapacityScaling must be signed");
|
kpeter@872
|
335 |
|
kpeter@898
|
336 |
// Reset data structures
|
kpeter@872
|
337 |
reset();
|
kpeter@871
|
338 |
}
|
kpeter@871
|
339 |
|
kpeter@872
|
340 |
/// \name Parameters
|
kpeter@872
|
341 |
/// The parameters of the algorithm can be specified using these
|
kpeter@872
|
342 |
/// functions.
|
kpeter@872
|
343 |
|
kpeter@872
|
344 |
/// @{
|
kpeter@872
|
345 |
|
kpeter@872
|
346 |
/// \brief Set the lower bounds on the arcs.
|
kpeter@871
|
347 |
///
|
kpeter@872
|
348 |
/// This function sets the lower bounds on the arcs.
|
kpeter@872
|
349 |
/// If it is not used before calling \ref run(), the lower bounds
|
kpeter@872
|
350 |
/// will be set to zero on all arcs.
|
kpeter@871
|
351 |
///
|
kpeter@872
|
352 |
/// \param map An arc map storing the lower bounds.
|
kpeter@872
|
353 |
/// Its \c Value type must be convertible to the \c Value type
|
kpeter@872
|
354 |
/// of the algorithm.
|
kpeter@872
|
355 |
///
|
kpeter@872
|
356 |
/// \return <tt>(*this)</tt>
|
kpeter@872
|
357 |
template <typename LowerMap>
|
kpeter@872
|
358 |
CapacityScaling& lowerMap(const LowerMap& map) {
|
kpeter@1297
|
359 |
_has_lower = true;
|
kpeter@872
|
360 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@872
|
361 |
_lower[_arc_idf[a]] = map[a];
|
kpeter@871
|
362 |
}
|
kpeter@871
|
363 |
return *this;
|
kpeter@871
|
364 |
}
|
kpeter@871
|
365 |
|
kpeter@872
|
366 |
/// \brief Set the upper bounds (capacities) on the arcs.
|
kpeter@871
|
367 |
///
|
kpeter@872
|
368 |
/// This function sets the upper bounds (capacities) on the arcs.
|
kpeter@872
|
369 |
/// If it is not used before calling \ref run(), the upper bounds
|
kpeter@872
|
370 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be
|
kpeter@878
|
371 |
/// unbounded from above).
|
kpeter@871
|
372 |
///
|
kpeter@872
|
373 |
/// \param map An arc map storing the upper bounds.
|
kpeter@872
|
374 |
/// Its \c Value type must be convertible to the \c Value type
|
kpeter@872
|
375 |
/// of the algorithm.
|
kpeter@872
|
376 |
///
|
kpeter@872
|
377 |
/// \return <tt>(*this)</tt>
|
kpeter@872
|
378 |
template<typename UpperMap>
|
kpeter@872
|
379 |
CapacityScaling& upperMap(const UpperMap& map) {
|
kpeter@872
|
380 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@872
|
381 |
_upper[_arc_idf[a]] = map[a];
|
kpeter@871
|
382 |
}
|
kpeter@871
|
383 |
return *this;
|
kpeter@871
|
384 |
}
|
kpeter@871
|
385 |
|
kpeter@872
|
386 |
/// \brief Set the costs of the arcs.
|
kpeter@872
|
387 |
///
|
kpeter@872
|
388 |
/// This function sets the costs of the arcs.
|
kpeter@872
|
389 |
/// If it is not used before calling \ref run(), the costs
|
kpeter@872
|
390 |
/// will be set to \c 1 on all arcs.
|
kpeter@872
|
391 |
///
|
kpeter@872
|
392 |
/// \param map An arc map storing the costs.
|
kpeter@872
|
393 |
/// Its \c Value type must be convertible to the \c Cost type
|
kpeter@872
|
394 |
/// of the algorithm.
|
kpeter@872
|
395 |
///
|
kpeter@872
|
396 |
/// \return <tt>(*this)</tt>
|
kpeter@872
|
397 |
template<typename CostMap>
|
kpeter@872
|
398 |
CapacityScaling& costMap(const CostMap& map) {
|
kpeter@872
|
399 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@872
|
400 |
_cost[_arc_idf[a]] = map[a];
|
kpeter@872
|
401 |
_cost[_arc_idb[a]] = -map[a];
|
kpeter@872
|
402 |
}
|
kpeter@872
|
403 |
return *this;
|
kpeter@872
|
404 |
}
|
kpeter@872
|
405 |
|
kpeter@872
|
406 |
/// \brief Set the supply values of the nodes.
|
kpeter@872
|
407 |
///
|
kpeter@872
|
408 |
/// This function sets the supply values of the nodes.
|
kpeter@872
|
409 |
/// If neither this function nor \ref stSupply() is used before
|
kpeter@872
|
410 |
/// calling \ref run(), the supply of each node will be set to zero.
|
kpeter@872
|
411 |
///
|
kpeter@872
|
412 |
/// \param map A node map storing the supply values.
|
kpeter@872
|
413 |
/// Its \c Value type must be convertible to the \c Value type
|
kpeter@872
|
414 |
/// of the algorithm.
|
kpeter@872
|
415 |
///
|
kpeter@872
|
416 |
/// \return <tt>(*this)</tt>
|
kpeter@872
|
417 |
template<typename SupplyMap>
|
kpeter@872
|
418 |
CapacityScaling& supplyMap(const SupplyMap& map) {
|
kpeter@872
|
419 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@872
|
420 |
_supply[_node_id[n]] = map[n];
|
kpeter@872
|
421 |
}
|
kpeter@872
|
422 |
return *this;
|
kpeter@872
|
423 |
}
|
kpeter@872
|
424 |
|
kpeter@872
|
425 |
/// \brief Set single source and target nodes and a supply value.
|
kpeter@872
|
426 |
///
|
kpeter@872
|
427 |
/// This function sets a single source node and a single target node
|
kpeter@872
|
428 |
/// and the required flow value.
|
kpeter@872
|
429 |
/// If neither this function nor \ref supplyMap() is used before
|
kpeter@872
|
430 |
/// calling \ref run(), the supply of each node will be set to zero.
|
kpeter@872
|
431 |
///
|
kpeter@872
|
432 |
/// Using this function has the same effect as using \ref supplyMap()
|
kpeter@1023
|
433 |
/// with a map in which \c k is assigned to \c s, \c -k is
|
kpeter@872
|
434 |
/// assigned to \c t and all other nodes have zero supply value.
|
kpeter@872
|
435 |
///
|
kpeter@872
|
436 |
/// \param s The source node.
|
kpeter@872
|
437 |
/// \param t The target node.
|
kpeter@872
|
438 |
/// \param k The required amount of flow from node \c s to node \c t
|
kpeter@872
|
439 |
/// (i.e. the supply of \c s and the demand of \c t).
|
kpeter@872
|
440 |
///
|
kpeter@872
|
441 |
/// \return <tt>(*this)</tt>
|
kpeter@872
|
442 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
|
kpeter@872
|
443 |
for (int i = 0; i != _node_num; ++i) {
|
kpeter@872
|
444 |
_supply[i] = 0;
|
kpeter@872
|
445 |
}
|
kpeter@872
|
446 |
_supply[_node_id[s]] = k;
|
kpeter@872
|
447 |
_supply[_node_id[t]] = -k;
|
kpeter@872
|
448 |
return *this;
|
kpeter@872
|
449 |
}
|
alpar@956
|
450 |
|
kpeter@872
|
451 |
/// @}
|
kpeter@872
|
452 |
|
kpeter@871
|
453 |
/// \name Execution control
|
kpeter@873
|
454 |
/// The algorithm can be executed using \ref run().
|
kpeter@871
|
455 |
|
kpeter@871
|
456 |
/// @{
|
kpeter@871
|
457 |
|
kpeter@871
|
458 |
/// \brief Run the algorithm.
|
kpeter@871
|
459 |
///
|
kpeter@871
|
460 |
/// This function runs the algorithm.
|
kpeter@872
|
461 |
/// The paramters can be specified using functions \ref lowerMap(),
|
kpeter@872
|
462 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
|
kpeter@872
|
463 |
/// For example,
|
kpeter@872
|
464 |
/// \code
|
kpeter@872
|
465 |
/// CapacityScaling<ListDigraph> cs(graph);
|
kpeter@872
|
466 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost)
|
kpeter@872
|
467 |
/// .supplyMap(sup).run();
|
kpeter@872
|
468 |
/// \endcode
|
kpeter@872
|
469 |
///
|
kpeter@898
|
470 |
/// This function can be called more than once. All the given parameters
|
kpeter@898
|
471 |
/// are kept for the next call, unless \ref resetParams() or \ref reset()
|
kpeter@898
|
472 |
/// is used, thus only the modified parameters have to be set again.
|
kpeter@898
|
473 |
/// If the underlying digraph was also modified after the construction
|
kpeter@898
|
474 |
/// of the class (or the last \ref reset() call), then the \ref reset()
|
kpeter@898
|
475 |
/// function must be called.
|
kpeter@871
|
476 |
///
|
kpeter@876
|
477 |
/// \param factor The capacity scaling factor. It must be larger than
|
kpeter@876
|
478 |
/// one to use scaling. If it is less or equal to one, then scaling
|
kpeter@876
|
479 |
/// will be disabled.
|
kpeter@871
|
480 |
///
|
kpeter@872
|
481 |
/// \return \c INFEASIBLE if no feasible flow exists,
|
kpeter@872
|
482 |
/// \n \c OPTIMAL if the problem has optimal solution
|
kpeter@872
|
483 |
/// (i.e. it is feasible and bounded), and the algorithm has found
|
kpeter@872
|
484 |
/// optimal flow and node potentials (primal and dual solutions),
|
kpeter@872
|
485 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost
|
kpeter@872
|
486 |
/// and infinite upper bound. It means that the objective function
|
kpeter@878
|
487 |
/// is unbounded on that arc, however, note that it could actually be
|
kpeter@872
|
488 |
/// bounded over the feasible flows, but this algroithm cannot handle
|
kpeter@872
|
489 |
/// these cases.
|
kpeter@872
|
490 |
///
|
kpeter@872
|
491 |
/// \see ProblemType
|
kpeter@898
|
492 |
/// \see resetParams(), reset()
|
kpeter@876
|
493 |
ProblemType run(int factor = 4) {
|
kpeter@876
|
494 |
_factor = factor;
|
kpeter@876
|
495 |
ProblemType pt = init();
|
kpeter@872
|
496 |
if (pt != OPTIMAL) return pt;
|
kpeter@872
|
497 |
return start();
|
kpeter@872
|
498 |
}
|
kpeter@872
|
499 |
|
kpeter@872
|
500 |
/// \brief Reset all the parameters that have been given before.
|
kpeter@872
|
501 |
///
|
kpeter@872
|
502 |
/// This function resets all the paramaters that have been given
|
kpeter@872
|
503 |
/// before using functions \ref lowerMap(), \ref upperMap(),
|
kpeter@872
|
504 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply().
|
kpeter@872
|
505 |
///
|
kpeter@898
|
506 |
/// It is useful for multiple \ref run() calls. Basically, all the given
|
kpeter@898
|
507 |
/// parameters are kept for the next \ref run() call, unless
|
kpeter@898
|
508 |
/// \ref resetParams() or \ref reset() is used.
|
kpeter@898
|
509 |
/// If the underlying digraph was also modified after the construction
|
kpeter@898
|
510 |
/// of the class or the last \ref reset() call, then the \ref reset()
|
kpeter@898
|
511 |
/// function must be used, otherwise \ref resetParams() is sufficient.
|
kpeter@872
|
512 |
///
|
kpeter@872
|
513 |
/// For example,
|
kpeter@872
|
514 |
/// \code
|
kpeter@872
|
515 |
/// CapacityScaling<ListDigraph> cs(graph);
|
kpeter@872
|
516 |
///
|
kpeter@872
|
517 |
/// // First run
|
kpeter@872
|
518 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost)
|
kpeter@872
|
519 |
/// .supplyMap(sup).run();
|
kpeter@872
|
520 |
///
|
kpeter@898
|
521 |
/// // Run again with modified cost map (resetParams() is not called,
|
kpeter@872
|
522 |
/// // so only the cost map have to be set again)
|
kpeter@872
|
523 |
/// cost[e] += 100;
|
kpeter@872
|
524 |
/// cs.costMap(cost).run();
|
kpeter@872
|
525 |
///
|
kpeter@898
|
526 |
/// // Run again from scratch using resetParams()
|
kpeter@872
|
527 |
/// // (the lower bounds will be set to zero on all arcs)
|
kpeter@898
|
528 |
/// cs.resetParams();
|
kpeter@872
|
529 |
/// cs.upperMap(capacity).costMap(cost)
|
kpeter@872
|
530 |
/// .supplyMap(sup).run();
|
kpeter@872
|
531 |
/// \endcode
|
kpeter@872
|
532 |
///
|
kpeter@872
|
533 |
/// \return <tt>(*this)</tt>
|
kpeter@898
|
534 |
///
|
kpeter@898
|
535 |
/// \see reset(), run()
|
kpeter@898
|
536 |
CapacityScaling& resetParams() {
|
kpeter@872
|
537 |
for (int i = 0; i != _node_num; ++i) {
|
kpeter@872
|
538 |
_supply[i] = 0;
|
kpeter@872
|
539 |
}
|
kpeter@872
|
540 |
for (int j = 0; j != _res_arc_num; ++j) {
|
kpeter@872
|
541 |
_lower[j] = 0;
|
kpeter@872
|
542 |
_upper[j] = INF;
|
kpeter@872
|
543 |
_cost[j] = _forward[j] ? 1 : -1;
|
kpeter@872
|
544 |
}
|
kpeter@1297
|
545 |
_has_lower = false;
|
kpeter@872
|
546 |
return *this;
|
kpeter@871
|
547 |
}
|
kpeter@871
|
548 |
|
kpeter@898
|
549 |
/// \brief Reset the internal data structures and all the parameters
|
kpeter@898
|
550 |
/// that have been given before.
|
kpeter@898
|
551 |
///
|
kpeter@898
|
552 |
/// This function resets the internal data structures and all the
|
kpeter@898
|
553 |
/// paramaters that have been given before using functions \ref lowerMap(),
|
kpeter@898
|
554 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
|
kpeter@898
|
555 |
///
|
kpeter@898
|
556 |
/// It is useful for multiple \ref run() calls. Basically, all the given
|
kpeter@898
|
557 |
/// parameters are kept for the next \ref run() call, unless
|
kpeter@898
|
558 |
/// \ref resetParams() or \ref reset() is used.
|
kpeter@898
|
559 |
/// If the underlying digraph was also modified after the construction
|
kpeter@898
|
560 |
/// of the class or the last \ref reset() call, then the \ref reset()
|
kpeter@898
|
561 |
/// function must be used, otherwise \ref resetParams() is sufficient.
|
kpeter@898
|
562 |
///
|
kpeter@898
|
563 |
/// See \ref resetParams() for examples.
|
kpeter@898
|
564 |
///
|
kpeter@898
|
565 |
/// \return <tt>(*this)</tt>
|
kpeter@898
|
566 |
///
|
kpeter@898
|
567 |
/// \see resetParams(), run()
|
kpeter@898
|
568 |
CapacityScaling& reset() {
|
kpeter@898
|
569 |
// Resize vectors
|
kpeter@898
|
570 |
_node_num = countNodes(_graph);
|
kpeter@898
|
571 |
_arc_num = countArcs(_graph);
|
kpeter@898
|
572 |
_res_arc_num = 2 * (_arc_num + _node_num);
|
kpeter@898
|
573 |
_root = _node_num;
|
kpeter@898
|
574 |
++_node_num;
|
kpeter@898
|
575 |
|
kpeter@898
|
576 |
_first_out.resize(_node_num + 1);
|
kpeter@898
|
577 |
_forward.resize(_res_arc_num);
|
kpeter@898
|
578 |
_source.resize(_res_arc_num);
|
kpeter@898
|
579 |
_target.resize(_res_arc_num);
|
kpeter@898
|
580 |
_reverse.resize(_res_arc_num);
|
kpeter@898
|
581 |
|
kpeter@898
|
582 |
_lower.resize(_res_arc_num);
|
kpeter@898
|
583 |
_upper.resize(_res_arc_num);
|
kpeter@898
|
584 |
_cost.resize(_res_arc_num);
|
kpeter@898
|
585 |
_supply.resize(_node_num);
|
alpar@956
|
586 |
|
kpeter@898
|
587 |
_res_cap.resize(_res_arc_num);
|
kpeter@898
|
588 |
_pi.resize(_node_num);
|
kpeter@898
|
589 |
_excess.resize(_node_num);
|
kpeter@898
|
590 |
_pred.resize(_node_num);
|
kpeter@898
|
591 |
|
kpeter@898
|
592 |
// Copy the graph
|
kpeter@898
|
593 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1;
|
kpeter@898
|
594 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
kpeter@898
|
595 |
_node_id[n] = i;
|
kpeter@898
|
596 |
}
|
kpeter@898
|
597 |
i = 0;
|
kpeter@898
|
598 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
kpeter@898
|
599 |
_first_out[i] = j;
|
kpeter@898
|
600 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
kpeter@898
|
601 |
_arc_idf[a] = j;
|
kpeter@898
|
602 |
_forward[j] = true;
|
kpeter@898
|
603 |
_source[j] = i;
|
kpeter@898
|
604 |
_target[j] = _node_id[_graph.runningNode(a)];
|
kpeter@898
|
605 |
}
|
kpeter@898
|
606 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
kpeter@898
|
607 |
_arc_idb[a] = j;
|
kpeter@898
|
608 |
_forward[j] = false;
|
kpeter@898
|
609 |
_source[j] = i;
|
kpeter@898
|
610 |
_target[j] = _node_id[_graph.runningNode(a)];
|
kpeter@898
|
611 |
}
|
kpeter@898
|
612 |
_forward[j] = false;
|
kpeter@898
|
613 |
_source[j] = i;
|
kpeter@898
|
614 |
_target[j] = _root;
|
kpeter@898
|
615 |
_reverse[j] = k;
|
kpeter@898
|
616 |
_forward[k] = true;
|
kpeter@898
|
617 |
_source[k] = _root;
|
kpeter@898
|
618 |
_target[k] = i;
|
kpeter@898
|
619 |
_reverse[k] = j;
|
kpeter@898
|
620 |
++j; ++k;
|
kpeter@898
|
621 |
}
|
kpeter@898
|
622 |
_first_out[i] = j;
|
kpeter@898
|
623 |
_first_out[_node_num] = k;
|
kpeter@898
|
624 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@898
|
625 |
int fi = _arc_idf[a];
|
kpeter@898
|
626 |
int bi = _arc_idb[a];
|
kpeter@898
|
627 |
_reverse[fi] = bi;
|
kpeter@898
|
628 |
_reverse[bi] = fi;
|
kpeter@898
|
629 |
}
|
alpar@956
|
630 |
|
kpeter@898
|
631 |
// Reset parameters
|
kpeter@898
|
632 |
resetParams();
|
kpeter@898
|
633 |
return *this;
|
kpeter@898
|
634 |
}
|
kpeter@898
|
635 |
|
kpeter@871
|
636 |
/// @}
|
kpeter@871
|
637 |
|
kpeter@871
|
638 |
/// \name Query Functions
|
kpeter@871
|
639 |
/// The results of the algorithm can be obtained using these
|
kpeter@871
|
640 |
/// functions.\n
|
kpeter@872
|
641 |
/// The \ref run() function must be called before using them.
|
kpeter@871
|
642 |
|
kpeter@871
|
643 |
/// @{
|
kpeter@871
|
644 |
|
kpeter@872
|
645 |
/// \brief Return the total cost of the found flow.
|
kpeter@871
|
646 |
///
|
kpeter@872
|
647 |
/// This function returns the total cost of the found flow.
|
kpeter@1254
|
648 |
/// Its complexity is O(m).
|
kpeter@872
|
649 |
///
|
kpeter@872
|
650 |
/// \note The return type of the function can be specified as a
|
kpeter@872
|
651 |
/// template parameter. For example,
|
kpeter@872
|
652 |
/// \code
|
kpeter@872
|
653 |
/// cs.totalCost<double>();
|
kpeter@872
|
654 |
/// \endcode
|
kpeter@872
|
655 |
/// It is useful if the total cost cannot be stored in the \c Cost
|
kpeter@872
|
656 |
/// type of the algorithm, which is the default return type of the
|
kpeter@872
|
657 |
/// function.
|
kpeter@871
|
658 |
///
|
kpeter@871
|
659 |
/// \pre \ref run() must be called before using this function.
|
kpeter@872
|
660 |
template <typename Number>
|
kpeter@872
|
661 |
Number totalCost() const {
|
kpeter@872
|
662 |
Number c = 0;
|
kpeter@872
|
663 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@872
|
664 |
int i = _arc_idb[a];
|
kpeter@872
|
665 |
c += static_cast<Number>(_res_cap[i]) *
|
kpeter@872
|
666 |
(-static_cast<Number>(_cost[i]));
|
kpeter@872
|
667 |
}
|
kpeter@872
|
668 |
return c;
|
kpeter@871
|
669 |
}
|
kpeter@871
|
670 |
|
kpeter@872
|
671 |
#ifndef DOXYGEN
|
kpeter@872
|
672 |
Cost totalCost() const {
|
kpeter@872
|
673 |
return totalCost<Cost>();
|
kpeter@871
|
674 |
}
|
kpeter@872
|
675 |
#endif
|
kpeter@871
|
676 |
|
kpeter@871
|
677 |
/// \brief Return the flow on the given arc.
|
kpeter@871
|
678 |
///
|
kpeter@872
|
679 |
/// This function returns the flow on the given arc.
|
kpeter@871
|
680 |
///
|
kpeter@871
|
681 |
/// \pre \ref run() must be called before using this function.
|
kpeter@872
|
682 |
Value flow(const Arc& a) const {
|
kpeter@872
|
683 |
return _res_cap[_arc_idb[a]];
|
kpeter@871
|
684 |
}
|
kpeter@871
|
685 |
|
kpeter@1165
|
686 |
/// \brief Copy the flow values (the primal solution) into the
|
kpeter@1165
|
687 |
/// given map.
|
kpeter@871
|
688 |
///
|
kpeter@872
|
689 |
/// This function copies the flow value on each arc into the given
|
kpeter@872
|
690 |
/// map. The \c Value type of the algorithm must be convertible to
|
kpeter@872
|
691 |
/// the \c Value type of the map.
|
kpeter@871
|
692 |
///
|
kpeter@871
|
693 |
/// \pre \ref run() must be called before using this function.
|
kpeter@872
|
694 |
template <typename FlowMap>
|
kpeter@872
|
695 |
void flowMap(FlowMap &map) const {
|
kpeter@872
|
696 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
kpeter@872
|
697 |
map.set(a, _res_cap[_arc_idb[a]]);
|
kpeter@872
|
698 |
}
|
kpeter@871
|
699 |
}
|
kpeter@871
|
700 |
|
kpeter@872
|
701 |
/// \brief Return the potential (dual value) of the given node.
|
kpeter@871
|
702 |
///
|
kpeter@872
|
703 |
/// This function returns the potential (dual value) of the
|
kpeter@872
|
704 |
/// given node.
|
kpeter@871
|
705 |
///
|
kpeter@871
|
706 |
/// \pre \ref run() must be called before using this function.
|
kpeter@872
|
707 |
Cost potential(const Node& n) const {
|
kpeter@872
|
708 |
return _pi[_node_id[n]];
|
kpeter@872
|
709 |
}
|
kpeter@872
|
710 |
|
kpeter@1165
|
711 |
/// \brief Copy the potential values (the dual solution) into the
|
kpeter@1165
|
712 |
/// given map.
|
kpeter@872
|
713 |
///
|
kpeter@872
|
714 |
/// This function copies the potential (dual value) of each node
|
kpeter@872
|
715 |
/// into the given map.
|
kpeter@872
|
716 |
/// The \c Cost type of the algorithm must be convertible to the
|
kpeter@872
|
717 |
/// \c Value type of the map.
|
kpeter@872
|
718 |
///
|
kpeter@872
|
719 |
/// \pre \ref run() must be called before using this function.
|
kpeter@872
|
720 |
template <typename PotentialMap>
|
kpeter@872
|
721 |
void potentialMap(PotentialMap &map) const {
|
kpeter@872
|
722 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
kpeter@872
|
723 |
map.set(n, _pi[_node_id[n]]);
|
kpeter@872
|
724 |
}
|
kpeter@871
|
725 |
}
|
kpeter@871
|
726 |
|
kpeter@871
|
727 |
/// @}
|
kpeter@871
|
728 |
|
kpeter@871
|
729 |
private:
|
kpeter@871
|
730 |
|
kpeter@872
|
731 |
// Initialize the algorithm
|
kpeter@876
|
732 |
ProblemType init() {
|
kpeter@887
|
733 |
if (_node_num <= 1) return INFEASIBLE;
|
kpeter@871
|
734 |
|
kpeter@872
|
735 |
// Check the sum of supply values
|
kpeter@872
|
736 |
_sum_supply = 0;
|
kpeter@872
|
737 |
for (int i = 0; i != _root; ++i) {
|
kpeter@872
|
738 |
_sum_supply += _supply[i];
|
kpeter@871
|
739 |
}
|
kpeter@872
|
740 |
if (_sum_supply > 0) return INFEASIBLE;
|
alpar@956
|
741 |
|
kpeter@1240
|
742 |
// Check lower and upper bounds
|
kpeter@1240
|
743 |
LEMON_DEBUG(checkBoundMaps(),
|
kpeter@1240
|
744 |
"Upper bounds must be greater or equal to the lower bounds");
|
kpeter@1240
|
745 |
|
kpeter@1240
|
746 |
|
kpeter@877
|
747 |
// Initialize vectors
|
kpeter@872
|
748 |
for (int i = 0; i != _root; ++i) {
|
kpeter@872
|
749 |
_pi[i] = 0;
|
kpeter@872
|
750 |
_excess[i] = _supply[i];
|
kpeter@871
|
751 |
}
|
kpeter@871
|
752 |
|
kpeter@872
|
753 |
// Remove non-zero lower bounds
|
kpeter@877
|
754 |
const Value MAX = std::numeric_limits<Value>::max();
|
kpeter@877
|
755 |
int last_out;
|
kpeter@1297
|
756 |
if (_has_lower) {
|
kpeter@872
|
757 |
for (int i = 0; i != _root; ++i) {
|
kpeter@877
|
758 |
last_out = _first_out[i+1];
|
kpeter@877
|
759 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
kpeter@872
|
760 |
if (_forward[j]) {
|
kpeter@872
|
761 |
Value c = _lower[j];
|
kpeter@872
|
762 |
if (c >= 0) {
|
kpeter@877
|
763 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF;
|
kpeter@872
|
764 |
} else {
|
kpeter@877
|
765 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF;
|
kpeter@872
|
766 |
}
|
kpeter@872
|
767 |
_excess[i] -= c;
|
kpeter@872
|
768 |
_excess[_target[j]] += c;
|
kpeter@872
|
769 |
} else {
|
kpeter@872
|
770 |
_res_cap[j] = 0;
|
kpeter@872
|
771 |
}
|
kpeter@872
|
772 |
}
|
kpeter@872
|
773 |
}
|
kpeter@872
|
774 |
} else {
|
kpeter@872
|
775 |
for (int j = 0; j != _res_arc_num; ++j) {
|
kpeter@872
|
776 |
_res_cap[j] = _forward[j] ? _upper[j] : 0;
|
kpeter@872
|
777 |
}
|
kpeter@872
|
778 |
}
|
kpeter@871
|
779 |
|
kpeter@872
|
780 |
// Handle negative costs
|
kpeter@877
|
781 |
for (int i = 0; i != _root; ++i) {
|
kpeter@877
|
782 |
last_out = _first_out[i+1] - 1;
|
kpeter@877
|
783 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
kpeter@877
|
784 |
Value rc = _res_cap[j];
|
kpeter@877
|
785 |
if (_cost[j] < 0 && rc > 0) {
|
kpeter@877
|
786 |
if (rc >= MAX) return UNBOUNDED;
|
kpeter@877
|
787 |
_excess[i] -= rc;
|
kpeter@877
|
788 |
_excess[_target[j]] += rc;
|
kpeter@877
|
789 |
_res_cap[j] = 0;
|
kpeter@877
|
790 |
_res_cap[_reverse[j]] += rc;
|
kpeter@872
|
791 |
}
|
kpeter@872
|
792 |
}
|
kpeter@872
|
793 |
}
|
alpar@956
|
794 |
|
kpeter@872
|
795 |
// Handle GEQ supply type
|
kpeter@872
|
796 |
if (_sum_supply < 0) {
|
kpeter@872
|
797 |
_pi[_root] = 0;
|
kpeter@872
|
798 |
_excess[_root] = -_sum_supply;
|
kpeter@872
|
799 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
kpeter@877
|
800 |
int ra = _reverse[a];
|
kpeter@877
|
801 |
_res_cap[a] = -_sum_supply + 1;
|
kpeter@877
|
802 |
_res_cap[ra] = 0;
|
kpeter@872
|
803 |
_cost[a] = 0;
|
kpeter@877
|
804 |
_cost[ra] = 0;
|
kpeter@872
|
805 |
}
|
kpeter@872
|
806 |
} else {
|
kpeter@872
|
807 |
_pi[_root] = 0;
|
kpeter@872
|
808 |
_excess[_root] = 0;
|
kpeter@872
|
809 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
kpeter@877
|
810 |
int ra = _reverse[a];
|
kpeter@872
|
811 |
_res_cap[a] = 1;
|
kpeter@877
|
812 |
_res_cap[ra] = 0;
|
kpeter@872
|
813 |
_cost[a] = 0;
|
kpeter@877
|
814 |
_cost[ra] = 0;
|
kpeter@872
|
815 |
}
|
kpeter@872
|
816 |
}
|
kpeter@872
|
817 |
|
kpeter@872
|
818 |
// Initialize delta value
|
kpeter@876
|
819 |
if (_factor > 1) {
|
kpeter@871
|
820 |
// With scaling
|
kpeter@910
|
821 |
Value max_sup = 0, max_dem = 0, max_cap = 0;
|
kpeter@910
|
822 |
for (int i = 0; i != _root; ++i) {
|
kpeter@877
|
823 |
Value ex = _excess[i];
|
kpeter@877
|
824 |
if ( ex > max_sup) max_sup = ex;
|
kpeter@877
|
825 |
if (-ex > max_dem) max_dem = -ex;
|
kpeter@910
|
826 |
int last_out = _first_out[i+1] - 1;
|
kpeter@910
|
827 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
kpeter@910
|
828 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
|
kpeter@910
|
829 |
}
|
kpeter@871
|
830 |
}
|
kpeter@871
|
831 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap);
|
kpeter@876
|
832 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ;
|
kpeter@871
|
833 |
} else {
|
kpeter@871
|
834 |
// Without scaling
|
kpeter@871
|
835 |
_delta = 1;
|
kpeter@871
|
836 |
}
|
kpeter@871
|
837 |
|
kpeter@872
|
838 |
return OPTIMAL;
|
kpeter@871
|
839 |
}
|
alpar@1270
|
840 |
|
kpeter@1296
|
841 |
// Check if the upper bound is greater than or equal to the lower bound
|
kpeter@1296
|
842 |
// on each forward arc.
|
kpeter@1240
|
843 |
bool checkBoundMaps() {
|
kpeter@1240
|
844 |
for (int j = 0; j != _res_arc_num; ++j) {
|
kpeter@1296
|
845 |
if (_forward[j] && _upper[j] < _lower[j]) return false;
|
kpeter@1240
|
846 |
}
|
kpeter@1240
|
847 |
return true;
|
kpeter@1240
|
848 |
}
|
kpeter@871
|
849 |
|
kpeter@872
|
850 |
ProblemType start() {
|
kpeter@872
|
851 |
// Execute the algorithm
|
kpeter@872
|
852 |
ProblemType pt;
|
kpeter@871
|
853 |
if (_delta > 1)
|
kpeter@872
|
854 |
pt = startWithScaling();
|
kpeter@871
|
855 |
else
|
kpeter@872
|
856 |
pt = startWithoutScaling();
|
kpeter@872
|
857 |
|
kpeter@872
|
858 |
// Handle non-zero lower bounds
|
kpeter@1297
|
859 |
if (_has_lower) {
|
kpeter@877
|
860 |
int limit = _first_out[_root];
|
kpeter@877
|
861 |
for (int j = 0; j != limit; ++j) {
|
kpeter@1296
|
862 |
if (_forward[j]) _res_cap[_reverse[j]] += _lower[j];
|
kpeter@872
|
863 |
}
|
kpeter@872
|
864 |
}
|
kpeter@872
|
865 |
|
kpeter@872
|
866 |
// Shift potentials if necessary
|
kpeter@872
|
867 |
Cost pr = _pi[_root];
|
kpeter@872
|
868 |
if (_sum_supply < 0 || pr > 0) {
|
kpeter@872
|
869 |
for (int i = 0; i != _node_num; ++i) {
|
kpeter@872
|
870 |
_pi[i] -= pr;
|
alpar@956
|
871 |
}
|
kpeter@872
|
872 |
}
|
alpar@956
|
873 |
|
kpeter@872
|
874 |
return pt;
|
kpeter@871
|
875 |
}
|
kpeter@871
|
876 |
|
kpeter@872
|
877 |
// Execute the capacity scaling algorithm
|
kpeter@872
|
878 |
ProblemType startWithScaling() {
|
kpeter@873
|
879 |
// Perform capacity scaling phases
|
kpeter@872
|
880 |
int s, t;
|
kpeter@872
|
881 |
ResidualDijkstra _dijkstra(*this);
|
kpeter@871
|
882 |
while (true) {
|
kpeter@872
|
883 |
// Saturate all arcs not satisfying the optimality condition
|
kpeter@877
|
884 |
int last_out;
|
kpeter@872
|
885 |
for (int u = 0; u != _node_num; ++u) {
|
kpeter@877
|
886 |
last_out = _sum_supply < 0 ?
|
kpeter@877
|
887 |
_first_out[u+1] : _first_out[u+1] - 1;
|
kpeter@877
|
888 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
kpeter@872
|
889 |
int v = _target[a];
|
kpeter@872
|
890 |
Cost c = _cost[a] + _pi[u] - _pi[v];
|
kpeter@872
|
891 |
Value rc = _res_cap[a];
|
kpeter@872
|
892 |
if (c < 0 && rc >= _delta) {
|
kpeter@872
|
893 |
_excess[u] -= rc;
|
kpeter@872
|
894 |
_excess[v] += rc;
|
kpeter@872
|
895 |
_res_cap[a] = 0;
|
kpeter@872
|
896 |
_res_cap[_reverse[a]] += rc;
|
kpeter@872
|
897 |
}
|
kpeter@871
|
898 |
}
|
kpeter@871
|
899 |
}
|
kpeter@871
|
900 |
|
kpeter@872
|
901 |
// Find excess nodes and deficit nodes
|
kpeter@871
|
902 |
_excess_nodes.clear();
|
kpeter@871
|
903 |
_deficit_nodes.clear();
|
kpeter@872
|
904 |
for (int u = 0; u != _node_num; ++u) {
|
kpeter@877
|
905 |
Value ex = _excess[u];
|
kpeter@877
|
906 |
if (ex >= _delta) _excess_nodes.push_back(u);
|
kpeter@877
|
907 |
if (ex <= -_delta) _deficit_nodes.push_back(u);
|
kpeter@871
|
908 |
}
|
kpeter@871
|
909 |
int next_node = 0, next_def_node = 0;
|
kpeter@871
|
910 |
|
kpeter@872
|
911 |
// Find augmenting shortest paths
|
kpeter@871
|
912 |
while (next_node < int(_excess_nodes.size())) {
|
kpeter@872
|
913 |
// Check deficit nodes
|
kpeter@871
|
914 |
if (_delta > 1) {
|
kpeter@871
|
915 |
bool delta_deficit = false;
|
kpeter@871
|
916 |
for ( ; next_def_node < int(_deficit_nodes.size());
|
kpeter@871
|
917 |
++next_def_node ) {
|
kpeter@871
|
918 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
|
kpeter@871
|
919 |
delta_deficit = true;
|
kpeter@871
|
920 |
break;
|
kpeter@871
|
921 |
}
|
kpeter@871
|
922 |
}
|
kpeter@871
|
923 |
if (!delta_deficit) break;
|
kpeter@871
|
924 |
}
|
kpeter@871
|
925 |
|
kpeter@872
|
926 |
// Run Dijkstra in the residual network
|
kpeter@871
|
927 |
s = _excess_nodes[next_node];
|
kpeter@872
|
928 |
if ((t = _dijkstra.run(s, _delta)) == -1) {
|
kpeter@871
|
929 |
if (_delta > 1) {
|
kpeter@871
|
930 |
++next_node;
|
kpeter@871
|
931 |
continue;
|
kpeter@871
|
932 |
}
|
kpeter@872
|
933 |
return INFEASIBLE;
|
kpeter@871
|
934 |
}
|
kpeter@871
|
935 |
|
kpeter@872
|
936 |
// Augment along a shortest path from s to t
|
kpeter@872
|
937 |
Value d = std::min(_excess[s], -_excess[t]);
|
kpeter@872
|
938 |
int u = t;
|
kpeter@872
|
939 |
int a;
|
kpeter@871
|
940 |
if (d > _delta) {
|
kpeter@872
|
941 |
while ((a = _pred[u]) != -1) {
|
kpeter@872
|
942 |
if (_res_cap[a] < d) d = _res_cap[a];
|
kpeter@872
|
943 |
u = _source[a];
|
kpeter@871
|
944 |
}
|
kpeter@871
|
945 |
}
|
kpeter@871
|
946 |
u = t;
|
kpeter@872
|
947 |
while ((a = _pred[u]) != -1) {
|
kpeter@872
|
948 |
_res_cap[a] -= d;
|
kpeter@872
|
949 |
_res_cap[_reverse[a]] += d;
|
kpeter@872
|
950 |
u = _source[a];
|
kpeter@871
|
951 |
}
|
kpeter@871
|
952 |
_excess[s] -= d;
|
kpeter@871
|
953 |
_excess[t] += d;
|
kpeter@871
|
954 |
|
kpeter@871
|
955 |
if (_excess[s] < _delta) ++next_node;
|
kpeter@871
|
956 |
}
|
kpeter@871
|
957 |
|
kpeter@871
|
958 |
if (_delta == 1) break;
|
kpeter@876
|
959 |
_delta = _delta <= _factor ? 1 : _delta / _factor;
|
kpeter@871
|
960 |
}
|
kpeter@871
|
961 |
|
kpeter@872
|
962 |
return OPTIMAL;
|
kpeter@871
|
963 |
}
|
kpeter@871
|
964 |
|
kpeter@872
|
965 |
// Execute the successive shortest path algorithm
|
kpeter@872
|
966 |
ProblemType startWithoutScaling() {
|
kpeter@872
|
967 |
// Find excess nodes
|
kpeter@872
|
968 |
_excess_nodes.clear();
|
kpeter@872
|
969 |
for (int i = 0; i != _node_num; ++i) {
|
kpeter@872
|
970 |
if (_excess[i] > 0) _excess_nodes.push_back(i);
|
kpeter@872
|
971 |
}
|
kpeter@872
|
972 |
if (_excess_nodes.size() == 0) return OPTIMAL;
|
kpeter@871
|
973 |
int next_node = 0;
|
kpeter@871
|
974 |
|
kpeter@872
|
975 |
// Find shortest paths
|
kpeter@872
|
976 |
int s, t;
|
kpeter@872
|
977 |
ResidualDijkstra _dijkstra(*this);
|
kpeter@871
|
978 |
while ( _excess[_excess_nodes[next_node]] > 0 ||
|
kpeter@871
|
979 |
++next_node < int(_excess_nodes.size()) )
|
kpeter@871
|
980 |
{
|
kpeter@872
|
981 |
// Run Dijkstra in the residual network
|
kpeter@871
|
982 |
s = _excess_nodes[next_node];
|
kpeter@872
|
983 |
if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE;
|
kpeter@871
|
984 |
|
kpeter@872
|
985 |
// Augment along a shortest path from s to t
|
kpeter@872
|
986 |
Value d = std::min(_excess[s], -_excess[t]);
|
kpeter@872
|
987 |
int u = t;
|
kpeter@872
|
988 |
int a;
|
kpeter@871
|
989 |
if (d > 1) {
|
kpeter@872
|
990 |
while ((a = _pred[u]) != -1) {
|
kpeter@872
|
991 |
if (_res_cap[a] < d) d = _res_cap[a];
|
kpeter@872
|
992 |
u = _source[a];
|
kpeter@871
|
993 |
}
|
kpeter@871
|
994 |
}
|
kpeter@871
|
995 |
u = t;
|
kpeter@872
|
996 |
while ((a = _pred[u]) != -1) {
|
kpeter@872
|
997 |
_res_cap[a] -= d;
|
kpeter@872
|
998 |
_res_cap[_reverse[a]] += d;
|
kpeter@872
|
999 |
u = _source[a];
|
kpeter@871
|
1000 |
}
|
kpeter@871
|
1001 |
_excess[s] -= d;
|
kpeter@871
|
1002 |
_excess[t] += d;
|
kpeter@871
|
1003 |
}
|
kpeter@871
|
1004 |
|
kpeter@872
|
1005 |
return OPTIMAL;
|
kpeter@871
|
1006 |
}
|
kpeter@871
|
1007 |
|
kpeter@871
|
1008 |
}; //class CapacityScaling
|
kpeter@871
|
1009 |
|
kpeter@871
|
1010 |
///@}
|
kpeter@871
|
1011 |
|
kpeter@871
|
1012 |
} //namespace lemon
|
kpeter@871
|
1013 |
|
kpeter@871
|
1014 |
#endif //LEMON_CAPACITY_SCALING_H
|