42 | | /// \tparam Digraph The digraph type the algorithm runs on. |
43 | | /// \tparam LowerMap The type of the lower bound map. |
44 | | /// \tparam CapacityMap The type of the capacity (upper bound) map. |
45 | | /// \tparam CostMap The type of the cost (length) map. |
46 | | /// \tparam SupplyMap The type of the supply map. |
| 45 | /// Most of the parameters of the problem (except for the digraph) |
| 46 | /// can be given using separate functions, and the algorithm can be |
| 47 | /// executed using the \ref run() function. If some parameters are not |
| 48 | /// specified, then default values will be used. |
48 | | /// \warning |
49 | | /// - Arc capacities and costs should be \e non-negative \e integers. |
50 | | /// - Supply values should be \e signed \e integers. |
51 | | /// - The value types of the maps should be convertible to each other. |
52 | | /// - \c CostMap::Value must be signed type. |
| 50 | /// \tparam GR The digraph type the algorithm runs on. |
| 51 | /// \tparam V The value type used for flow amounts, capacity bounds |
| 52 | /// and supply values in the algorithm. By default it is \c int. |
| 53 | /// \tparam C The value type used for costs and potentials in the |
| 54 | /// algorithm. By default it is the same as \c V. |
54 | | /// \author Peter Kovacs |
55 | | template < typename Digraph, |
56 | | typename LowerMap = typename Digraph::template ArcMap<int>, |
57 | | typename CapacityMap = typename Digraph::template ArcMap<int>, |
58 | | typename CostMap = typename Digraph::template ArcMap<int>, |
59 | | typename SupplyMap = typename Digraph::template NodeMap<int> > |
| 56 | /// \warning Both value types must be signed and all input data must |
| 57 | /// be integer. |
| 58 | /// \warning This implementation can handle only non-negative arc costs. |
| 59 | template <typename GR, typename V = int, typename C = V> |
64 | | typedef typename CapacityMap::Value Capacity; |
65 | | typedef typename CostMap::Value Cost; |
66 | | typedef typename SupplyMap::Value Supply; |
67 | | typedef typename Digraph::template ArcMap<Capacity> CapacityArcMap; |
68 | | typedef typename Digraph::template NodeMap<Supply> SupplyNodeMap; |
69 | | typedef typename Digraph::template NodeMap<Arc> PredMap; |
| 64 | /// The type of the flow amounts, capacity bounds and supply values |
| 65 | typedef V Value; |
| 66 | /// The type of the arc costs |
| 67 | typedef C Cost; |
73 | | /// The type of the flow map. |
74 | | typedef typename Digraph::template ArcMap<Capacity> FlowMap; |
75 | | /// The type of the potential map. |
76 | | typedef typename Digraph::template NodeMap<Cost> PotentialMap; |
| 71 | /// \brief Problem type constants for the \c run() function. |
| 72 | /// |
| 73 | /// Enum type containing the problem type constants that can be |
| 74 | /// returned by the \ref run() function of the algorithm. |
| 75 | enum ProblemType { |
| 76 | /// The problem has no feasible solution (flow). |
| 77 | INFEASIBLE, |
| 78 | /// The problem has optimal solution (i.e. it is feasible and |
| 79 | /// bounded), and the algorithm has found optimal flow and node |
| 80 | /// potentials (primal and dual solutions). |
| 81 | OPTIMAL, |
| 82 | /// The objective function of the problem is unbounded, i.e. |
| 83 | /// there is a directed cycle having negative total cost and |
| 84 | /// infinite upper bound. |
| 85 | UNBOUNDED |
| 86 | }; |
| 87 | |
| 88 | private: |
| 89 | |
| 90 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 91 | |
| 92 | typedef std::vector<Arc> ArcVector; |
| 93 | typedef std::vector<Node> NodeVector; |
| 94 | typedef std::vector<int> IntVector; |
| 95 | typedef std::vector<bool> BoolVector; |
| 96 | typedef std::vector<Value> ValueVector; |
| 97 | typedef std::vector<Cost> CostVector; |
83 | | /// \ref ResidualDijkstra is a special implementation of the |
84 | | /// \ref Dijkstra algorithm for finding shortest paths in the |
85 | | /// residual network of the digraph with respect to the reduced arc |
86 | | /// costs and modifying the node potentials according to the |
87 | | /// distance of the nodes. |
| 142 | /// Constant for infinite upper bounds (capacities). |
| 143 | /// It is \c std::numeric_limits<Value>::infinity() if available, |
| 144 | /// \c std::numeric_limits<Value>::max() otherwise. |
| 145 | const Value INF; |
| 146 | |
| 147 | private: |
| 148 | |
| 149 | // Special implementation of the Dijkstra algorithm for finding |
| 150 | // shortest paths in the residual network of the digraph with |
| 151 | // respect to the reduced arc costs and modifying the node |
| 152 | // potentials according to the found distance labels. |
95 | | // The digraph the algorithm runs on |
96 | | const Digraph &_graph; |
97 | | |
98 | | // The main maps |
99 | | const FlowMap &_flow; |
100 | | const CapacityArcMap &_res_cap; |
101 | | const CostMap &_cost; |
102 | | const SupplyNodeMap &_excess; |
103 | | PotentialMap &_potential; |
104 | | |
105 | | // The distance map |
106 | | PotentialMap _dist; |
107 | | // The pred arc map |
108 | | PredMap &_pred; |
109 | | // The processed (i.e. permanently labeled) nodes |
110 | | std::vector<Node> _proc_nodes; |
111 | | |
| 160 | int _node_num; |
| 161 | const IntVector &_first_out; |
| 162 | const IntVector &_target; |
| 163 | const CostVector &_cost; |
| 164 | const ValueVector &_res_cap; |
| 165 | const ValueVector &_excess; |
| 166 | CostVector &_pi; |
| 167 | IntVector &_pred; |
| 168 | |
| 169 | IntVector _proc_nodes; |
| 170 | CostVector _dist; |
| 171 | |
114 | | /// Constructor. |
115 | | ResidualDijkstra( const Digraph &digraph, |
116 | | const FlowMap &flow, |
117 | | const CapacityArcMap &res_cap, |
118 | | const CostMap &cost, |
119 | | const SupplyMap &excess, |
120 | | PotentialMap &potential, |
121 | | PredMap &pred ) : |
122 | | _graph(digraph), _flow(flow), _res_cap(res_cap), _cost(cost), |
123 | | _excess(excess), _potential(potential), _dist(digraph), |
124 | | _pred(pred) |
| 174 | ResidualDijkstra(CapacityScaling& cs) : |
| 175 | _node_num(cs._node_num), _first_out(cs._first_out), |
| 176 | _target(cs._target), _cost(cs._cost), _res_cap(cs._res_cap), |
| 177 | _excess(cs._excess), _pi(cs._pi), _pred(cs._pred), |
| 178 | _dist(cs._node_num) |
200 | | private: |
201 | | |
202 | | // The digraph the algorithm runs on |
203 | | const Digraph &_graph; |
204 | | // The original lower bound map |
205 | | const LowerMap *_lower; |
206 | | // The modified capacity map |
207 | | CapacityArcMap _capacity; |
208 | | // The original cost map |
209 | | const CostMap &_cost; |
210 | | // The modified supply map |
211 | | SupplyNodeMap _supply; |
212 | | bool _valid_supply; |
213 | | |
214 | | // Arc map of the current flow |
215 | | FlowMap *_flow; |
216 | | bool _local_flow; |
217 | | // Node map of the current potentials |
218 | | PotentialMap *_potential; |
219 | | bool _local_potential; |
220 | | |
221 | | // The residual capacity map |
222 | | CapacityArcMap _res_cap; |
223 | | // The excess map |
224 | | SupplyNodeMap _excess; |
225 | | // The excess nodes (i.e. nodes with positive excess) |
226 | | std::vector<Node> _excess_nodes; |
227 | | // The deficit nodes (i.e. nodes with negative excess) |
228 | | std::vector<Node> _deficit_nodes; |
229 | | |
230 | | // The delta parameter used for capacity scaling |
231 | | Capacity _delta; |
232 | | // The maximum number of phases |
233 | | int _phase_num; |
234 | | |
235 | | // The pred arc map |
236 | | PredMap _pred; |
237 | | // Implementation of the Dijkstra algorithm for finding augmenting |
238 | | // shortest paths in the residual network |
239 | | ResidualDijkstra *_dijkstra; |
240 | | |
247 | | /// \param digraph The digraph the algorithm runs on. |
248 | | /// \param lower The lower bounds of the arcs. |
249 | | /// \param capacity The capacities (upper bounds) of the arcs. |
250 | | /// \param cost The cost (length) values of the arcs. |
251 | | /// \param supply The supply values of the nodes (signed). |
252 | | CapacityScaling( const Digraph &digraph, |
253 | | const LowerMap &lower, |
254 | | const CapacityMap &capacity, |
255 | | const CostMap &cost, |
256 | | const SupplyMap &supply ) : |
257 | | _graph(digraph), _lower(&lower), _capacity(digraph), _cost(cost), |
258 | | _supply(digraph), _flow(NULL), _local_flow(false), |
259 | | _potential(NULL), _local_potential(false), |
260 | | _res_cap(digraph), _excess(digraph), _pred(digraph), _dijkstra(NULL) |
| 237 | /// \param graph The digraph the algorithm runs on. |
| 238 | CapacityScaling(const GR& graph) : |
| 239 | _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 240 | INF(std::numeric_limits<Value>::has_infinity ? |
| 241 | std::numeric_limits<Value>::infinity() : |
| 242 | std::numeric_limits<Value>::max()) |
262 | | Supply sum = 0; |
263 | | for (NodeIt n(_graph); n != INVALID; ++n) { |
264 | | _supply[n] = supply[n]; |
265 | | _excess[n] = supply[n]; |
266 | | sum += supply[n]; |
| 244 | // Check the value types |
| 245 | LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 246 | "The flow type of CapacityScaling must be signed"); |
| 247 | LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 248 | "The cost type of CapacityScaling must be signed"); |
| 249 | |
| 250 | // Resize vectors |
| 251 | _node_num = countNodes(_graph); |
| 252 | _arc_num = countArcs(_graph); |
| 253 | _res_arc_num = 2 * (_arc_num + _node_num); |
| 254 | _root = _node_num; |
| 255 | ++_node_num; |
| 256 | |
| 257 | _first_out.resize(_node_num + 1); |
| 258 | _forward.resize(_res_arc_num); |
| 259 | _source.resize(_res_arc_num); |
| 260 | _target.resize(_res_arc_num); |
| 261 | _reverse.resize(_res_arc_num); |
| 262 | |
| 263 | _lower.resize(_res_arc_num); |
| 264 | _upper.resize(_res_arc_num); |
| 265 | _cost.resize(_res_arc_num); |
| 266 | _supply.resize(_node_num); |
| 267 | |
| 268 | _res_cap.resize(_res_arc_num); |
| 269 | _pi.resize(_node_num); |
| 270 | _excess.resize(_node_num); |
| 271 | _pred.resize(_node_num); |
| 272 | |
| 273 | // Copy the graph |
| 274 | int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
| 275 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
| 276 | _node_id[n] = i; |
268 | | _valid_supply = sum == 0; |
| 278 | i = 0; |
| 279 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
| 280 | _first_out[i] = j; |
| 281 | for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
| 282 | _arc_idf[a] = j; |
| 283 | _forward[j] = true; |
| 284 | _source[j] = i; |
| 285 | _target[j] = _node_id[_graph.runningNode(a)]; |
| 286 | } |
| 287 | for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
| 288 | _arc_idb[a] = j; |
| 289 | _forward[j] = false; |
| 290 | _source[j] = i; |
| 291 | _target[j] = _node_id[_graph.runningNode(a)]; |
| 292 | } |
| 293 | _forward[j] = false; |
| 294 | _source[j] = i; |
| 295 | _target[j] = _root; |
| 296 | _reverse[j] = k; |
| 297 | _forward[k] = true; |
| 298 | _source[k] = _root; |
| 299 | _target[k] = i; |
| 300 | _reverse[k] = j; |
| 301 | ++j; ++k; |
| 302 | } |
| 303 | _first_out[i] = j; |
| 304 | _first_out[_node_num] = k; |
273 | | |
274 | | // Remove non-zero lower bounds |
275 | | typename LowerMap::Value lcap; |
276 | | for (ArcIt e(_graph); e != INVALID; ++e) { |
277 | | if ((lcap = lower[e]) != 0) { |
278 | | _capacity[e] -= lcap; |
279 | | _res_cap[e] -= lcap; |
280 | | _supply[_graph.source(e)] -= lcap; |
281 | | _supply[_graph.target(e)] += lcap; |
282 | | _excess[_graph.source(e)] -= lcap; |
283 | | _excess[_graph.target(e)] += lcap; |
284 | | } |
285 | | } |
286 | | } |
287 | | /* |
288 | | /// \brief General constructor (without lower bounds). |
289 | | /// |
290 | | /// General constructor (without lower bounds). |
291 | | /// |
292 | | /// \param digraph The digraph the algorithm runs on. |
293 | | /// \param capacity The capacities (upper bounds) of the arcs. |
294 | | /// \param cost The cost (length) values of the arcs. |
295 | | /// \param supply The supply values of the nodes (signed). |
296 | | CapacityScaling( const Digraph &digraph, |
297 | | const CapacityMap &capacity, |
298 | | const CostMap &cost, |
299 | | const SupplyMap &supply ) : |
300 | | _graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost), |
301 | | _supply(supply), _flow(NULL), _local_flow(false), |
302 | | _potential(NULL), _local_potential(false), |
303 | | _res_cap(capacity), _excess(supply), _pred(digraph), _dijkstra(NULL) |
304 | | { |
305 | | // Check the sum of supply values |
306 | | Supply sum = 0; |
307 | | for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n]; |
308 | | _valid_supply = sum == 0; |
| 311 | |
| 312 | // Reset parameters |
| 313 | reset(); |
315 | | /// \param digraph The digraph the algorithm runs on. |
316 | | /// \param lower The lower bounds of the arcs. |
317 | | /// \param capacity The capacities (upper bounds) of the arcs. |
318 | | /// \param cost The cost (length) values of the arcs. |
319 | | /// \param s The source node. |
320 | | /// \param t The target node. |
321 | | /// \param flow_value The required amount of flow from node \c s |
322 | | /// to node \c t (i.e. the supply of \c s and the demand of \c t). |
323 | | CapacityScaling( const Digraph &digraph, |
324 | | const LowerMap &lower, |
325 | | const CapacityMap &capacity, |
326 | | const CostMap &cost, |
327 | | Node s, Node t, |
328 | | Supply flow_value ) : |
329 | | _graph(digraph), _lower(&lower), _capacity(capacity), _cost(cost), |
330 | | _supply(digraph, 0), _flow(NULL), _local_flow(false), |
331 | | _potential(NULL), _local_potential(false), |
332 | | _res_cap(capacity), _excess(digraph, 0), _pred(digraph), _dijkstra(NULL) |
333 | | { |
334 | | // Remove non-zero lower bounds |
335 | | _supply[s] = _excess[s] = flow_value; |
336 | | _supply[t] = _excess[t] = -flow_value; |
337 | | typename LowerMap::Value lcap; |
338 | | for (ArcIt e(_graph); e != INVALID; ++e) { |
339 | | if ((lcap = lower[e]) != 0) { |
340 | | _capacity[e] -= lcap; |
341 | | _res_cap[e] -= lcap; |
342 | | _supply[_graph.source(e)] -= lcap; |
343 | | _supply[_graph.target(e)] += lcap; |
344 | | _excess[_graph.source(e)] -= lcap; |
345 | | _excess[_graph.target(e)] += lcap; |
346 | | } |
| 328 | /// \param map An arc map storing the lower bounds. |
| 329 | /// Its \c Value type must be convertible to the \c Value type |
| 330 | /// of the algorithm. |
| 331 | /// |
| 332 | /// \return <tt>(*this)</tt> |
| 333 | template <typename LowerMap> |
| 334 | CapacityScaling& lowerMap(const LowerMap& map) { |
| 335 | _have_lower = true; |
| 336 | for (ArcIt a(_graph); a != INVALID; ++a) { |
| 337 | _lower[_arc_idf[a]] = map[a]; |
| 338 | _lower[_arc_idb[a]] = map[a]; |
348 | | _valid_supply = true; |
349 | | } |
350 | | |
351 | | /// \brief Simple constructor (without lower bounds). |
352 | | /// |
353 | | /// Simple constructor (without lower bounds). |
354 | | /// |
355 | | /// \param digraph The digraph the algorithm runs on. |
356 | | /// \param capacity The capacities (upper bounds) of the arcs. |
357 | | /// \param cost The cost (length) values of the arcs. |
358 | | /// \param s The source node. |
359 | | /// \param t The target node. |
360 | | /// \param flow_value The required amount of flow from node \c s |
361 | | /// to node \c t (i.e. the supply of \c s and the demand of \c t). |
362 | | CapacityScaling( const Digraph &digraph, |
363 | | const CapacityMap &capacity, |
364 | | const CostMap &cost, |
365 | | Node s, Node t, |
366 | | Supply flow_value ) : |
367 | | _graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost), |
368 | | _supply(digraph, 0), _flow(NULL), _local_flow(false), |
369 | | _potential(NULL), _local_potential(false), |
370 | | _res_cap(capacity), _excess(digraph, 0), _pred(digraph), _dijkstra(NULL) |
371 | | { |
372 | | _supply[s] = _excess[s] = flow_value; |
373 | | _supply[t] = _excess[t] = -flow_value; |
374 | | _valid_supply = true; |
375 | | } |
376 | | */ |
377 | | /// Destructor. |
378 | | ~CapacityScaling() { |
379 | | if (_local_flow) delete _flow; |
380 | | if (_local_potential) delete _potential; |
381 | | delete _dijkstra; |
382 | | } |
383 | | |
384 | | /// \brief Set the flow map. |
385 | | /// |
386 | | /// Set the flow map. |
387 | | /// |
388 | | /// \return \c (*this) |
389 | | CapacityScaling& flowMap(FlowMap &map) { |
390 | | if (_local_flow) { |
391 | | delete _flow; |
392 | | _local_flow = false; |
393 | | } |
394 | | _flow = ↦ |
402 | | /// \return \c (*this) |
403 | | CapacityScaling& potentialMap(PotentialMap &map) { |
404 | | if (_local_potential) { |
405 | | delete _potential; |
406 | | _local_potential = false; |
| 350 | /// \param map An arc map storing the upper bounds. |
| 351 | /// Its \c Value type must be convertible to the \c Value type |
| 352 | /// of the algorithm. |
| 353 | /// |
| 354 | /// \return <tt>(*this)</tt> |
| 355 | template<typename UpperMap> |
| 356 | CapacityScaling& upperMap(const UpperMap& map) { |
| 357 | for (ArcIt a(_graph); a != INVALID; ++a) { |
| 358 | _upper[_arc_idf[a]] = map[a]; |
| 363 | /// \brief Set the costs of the arcs. |
| 364 | /// |
| 365 | /// This function sets the costs of the arcs. |
| 366 | /// If it is not used before calling \ref run(), the costs |
| 367 | /// will be set to \c 1 on all arcs. |
| 368 | /// |
| 369 | /// \param map An arc map storing the costs. |
| 370 | /// Its \c Value type must be convertible to the \c Cost type |
| 371 | /// of the algorithm. |
| 372 | /// |
| 373 | /// \return <tt>(*this)</tt> |
| 374 | template<typename CostMap> |
| 375 | CapacityScaling& costMap(const CostMap& map) { |
| 376 | for (ArcIt a(_graph); a != INVALID; ++a) { |
| 377 | _cost[_arc_idf[a]] = map[a]; |
| 378 | _cost[_arc_idb[a]] = -map[a]; |
| 379 | } |
| 380 | return *this; |
| 381 | } |
| 382 | |
| 383 | /// \brief Set the supply values of the nodes. |
| 384 | /// |
| 385 | /// This function sets the supply values of the nodes. |
| 386 | /// If neither this function nor \ref stSupply() is used before |
| 387 | /// calling \ref run(), the supply of each node will be set to zero. |
| 388 | /// |
| 389 | /// \param map A node map storing the supply values. |
| 390 | /// Its \c Value type must be convertible to the \c Value type |
| 391 | /// of the algorithm. |
| 392 | /// |
| 393 | /// \return <tt>(*this)</tt> |
| 394 | template<typename SupplyMap> |
| 395 | CapacityScaling& supplyMap(const SupplyMap& map) { |
| 396 | for (NodeIt n(_graph); n != INVALID; ++n) { |
| 397 | _supply[_node_id[n]] = map[n]; |
| 398 | } |
| 399 | return *this; |
| 400 | } |
| 401 | |
| 402 | /// \brief Set single source and target nodes and a supply value. |
| 403 | /// |
| 404 | /// This function sets a single source node and a single target node |
| 405 | /// and the required flow value. |
| 406 | /// If neither this function nor \ref supplyMap() is used before |
| 407 | /// calling \ref run(), the supply of each node will be set to zero. |
| 408 | /// |
| 409 | /// Using this function has the same effect as using \ref supplyMap() |
| 410 | /// with such a map in which \c k is assigned to \c s, \c -k is |
| 411 | /// assigned to \c t and all other nodes have zero supply value. |
| 412 | /// |
| 413 | /// \param s The source node. |
| 414 | /// \param t The target node. |
| 415 | /// \param k The required amount of flow from node \c s to node \c t |
| 416 | /// (i.e. the supply of \c s and the demand of \c t). |
| 417 | /// |
| 418 | /// \return <tt>(*this)</tt> |
| 419 | CapacityScaling& stSupply(const Node& s, const Node& t, Value k) { |
| 420 | for (int i = 0; i != _node_num; ++i) { |
| 421 | _supply[i] = 0; |
| 422 | } |
| 423 | _supply[_node_id[s]] = k; |
| 424 | _supply[_node_id[t]] = -k; |
| 425 | return *this; |
| 426 | } |
| 427 | |
| 428 | /// @} |
| 429 | |
| 437 | /// The paramters can be specified using functions \ref lowerMap(), |
| 438 | /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 439 | /// For example, |
| 440 | /// \code |
| 441 | /// CapacityScaling<ListDigraph> cs(graph); |
| 442 | /// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 443 | /// .supplyMap(sup).run(); |
| 444 | /// \endcode |
| 445 | /// |
| 446 | /// This function can be called more than once. All the parameters |
| 447 | /// that have been given are kept for the next call, unless |
| 448 | /// \ref reset() is called, thus only the modified parameters |
| 449 | /// have to be set again. See \ref reset() for examples. |
| 450 | /// However the underlying digraph must not be modified after this |
| 451 | /// class have been constructed, since it copies the digraph. |
425 | | /// \return \c true if a feasible flow can be found. |
426 | | bool run(bool scaling = true) { |
427 | | return init(scaling) && start(); |
| 458 | /// \return \c INFEASIBLE if no feasible flow exists, |
| 459 | /// \n \c OPTIMAL if the problem has optimal solution |
| 460 | /// (i.e. it is feasible and bounded), and the algorithm has found |
| 461 | /// optimal flow and node potentials (primal and dual solutions), |
| 462 | /// \n \c UNBOUNDED if the objective function of the problem is |
| 463 | /// unbounded, i.e. there is a directed cycle having negative total |
| 464 | /// cost and infinite upper bound. |
| 465 | /// |
| 466 | /// \see ProblemType |
| 467 | ProblemType run(bool scaling = true) { |
| 468 | if (!init(scaling)) return INFEASIBLE; |
| 469 | return start(); |
| 470 | } |
| 471 | |
| 472 | /// \brief Reset all the parameters that have been given before. |
| 473 | /// |
| 474 | /// This function resets all the paramaters that have been given |
| 475 | /// before using functions \ref lowerMap(), \ref upperMap(), |
| 476 | /// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 477 | /// |
| 478 | /// It is useful for multiple run() calls. If this function is not |
| 479 | /// used, all the parameters given before are kept for the next |
| 480 | /// \ref run() call. |
| 481 | /// However the underlying digraph must not be modified after this |
| 482 | /// class have been constructed, since it copies and extends the graph. |
| 483 | /// |
| 484 | /// For example, |
| 485 | /// \code |
| 486 | /// CapacityScaling<ListDigraph> cs(graph); |
| 487 | /// |
| 488 | /// // First run |
| 489 | /// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 490 | /// .supplyMap(sup).run(); |
| 491 | /// |
| 492 | /// // Run again with modified cost map (reset() is not called, |
| 493 | /// // so only the cost map have to be set again) |
| 494 | /// cost[e] += 100; |
| 495 | /// cs.costMap(cost).run(); |
| 496 | /// |
| 497 | /// // Run again from scratch using reset() |
| 498 | /// // (the lower bounds will be set to zero on all arcs) |
| 499 | /// cs.reset(); |
| 500 | /// cs.upperMap(capacity).costMap(cost) |
| 501 | /// .supplyMap(sup).run(); |
| 502 | /// \endcode |
| 503 | /// |
| 504 | /// \return <tt>(*this)</tt> |
| 505 | CapacityScaling& reset() { |
| 506 | for (int i = 0; i != _node_num; ++i) { |
| 507 | _supply[i] = 0; |
| 508 | } |
| 509 | for (int j = 0; j != _res_arc_num; ++j) { |
| 510 | _lower[j] = 0; |
| 511 | _upper[j] = INF; |
| 512 | _cost[j] = _forward[j] ? 1 : -1; |
| 513 | } |
| 514 | _have_lower = false; |
| 515 | return *this; |
443 | | /// Return a const reference to the arc map storing the found flow. |
| 529 | /// This function returns the total cost of the found flow. |
| 530 | /// Its complexity is O(e). |
| 531 | /// |
| 532 | /// \note The return type of the function can be specified as a |
| 533 | /// template parameter. For example, |
| 534 | /// \code |
| 535 | /// cs.totalCost<double>(); |
| 536 | /// \endcode |
| 537 | /// It is useful if the total cost cannot be stored in the \c Cost |
| 538 | /// type of the algorithm, which is the default return type of the |
| 539 | /// function. |
485 | | Cost totalCost() const { |
486 | | Cost c = 0; |
487 | | for (ArcIt e(_graph); e != INVALID; ++e) |
488 | | c += (*_flow)[e] * _cost[e]; |
489 | | return c; |
| 588 | Cost potential(const Node& n) const { |
| 589 | return _pi[_node_id[n]]; |
| 590 | } |
| 591 | |
| 592 | /// \brief Return the potential map (the dual solution). |
| 593 | /// |
| 594 | /// This function copies the potential (dual value) of each node |
| 595 | /// into the given map. |
| 596 | /// The \c Cost type of the algorithm must be convertible to the |
| 597 | /// \c Value type of the map. |
| 598 | /// |
| 599 | /// \pre \ref run() must be called before using this function. |
| 600 | template <typename PotentialMap> |
| 601 | void potentialMap(PotentialMap &map) const { |
| 602 | for (NodeIt n(_graph); n != INVALID; ++n) { |
| 603 | map.set(n, _pi[_node_id[n]]); |
| 604 | } |
512 | | _dijkstra = new ResidualDijkstra( _graph, *_flow, _res_cap, _cost, |
513 | | _excess, *_potential, _pred ); |
| 628 | // Remove non-zero lower bounds |
| 629 | if (_have_lower) { |
| 630 | for (int i = 0; i != _root; ++i) { |
| 631 | for (int j = _first_out[i]; j != _first_out[i+1]; ++j) { |
| 632 | if (_forward[j]) { |
| 633 | Value c = _lower[j]; |
| 634 | if (c >= 0) { |
| 635 | _res_cap[j] = _upper[j] < INF ? _upper[j] - c : INF; |
| 636 | } else { |
| 637 | _res_cap[j] = _upper[j] < INF + c ? _upper[j] - c : INF; |
| 638 | } |
| 639 | _excess[i] -= c; |
| 640 | _excess[_target[j]] += c; |
| 641 | } else { |
| 642 | _res_cap[j] = 0; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } else { |
| 647 | for (int j = 0; j != _res_arc_num; ++j) { |
| 648 | _res_cap[j] = _forward[j] ? _upper[j] : 0; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Handle GEQ supply type |
| 653 | if (_sum_supply < 0) { |
| 654 | _pi[_root] = 0; |
| 655 | _excess[_root] = -_sum_supply; |
| 656 | for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
| 657 | int u = _target[a]; |
| 658 | if (_excess[u] < 0) { |
| 659 | _res_cap[a] = -_excess[u] + 1; |
| 660 | } else { |
| 661 | _res_cap[a] = 1; |
| 662 | } |
| 663 | _res_cap[_reverse[a]] = 0; |
| 664 | _cost[a] = 0; |
| 665 | _cost[_reverse[a]] = 0; |
| 666 | } |
| 667 | } else { |
| 668 | _pi[_root] = 0; |
| 669 | _excess[_root] = 0; |
| 670 | for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
| 671 | _res_cap[a] = 1; |
| 672 | _res_cap[_reverse[a]] = 0; |
| 673 | _cost[a] = 0; |
| 674 | _cost[_reverse[a]] = 0; |
| 675 | } |
| 676 | } |
518 | | Supply max_sup = 0, max_dem = 0; |
519 | | for (NodeIt n(_graph); n != INVALID; ++n) { |
520 | | if ( _supply[n] > max_sup) max_sup = _supply[n]; |
521 | | if (-_supply[n] > max_dem) max_dem = -_supply[n]; |
| 681 | Value max_sup = 0, max_dem = 0; |
| 682 | for (int i = 0; i != _node_num; ++i) { |
| 683 | if ( _excess[i] > max_sup) max_sup = _excess[i]; |
| 684 | if (-_excess[i] > max_dem) max_dem = -_excess[i]; |
553 | | // Saturating all arcs not satisfying the optimality condition |
554 | | for (ArcIt e(_graph); e != INVALID; ++e) { |
555 | | Node u = _graph.source(e), v = _graph.target(e); |
556 | | Cost c = _cost[e] + (*_potential)[u] - (*_potential)[v]; |
557 | | if (c < 0 && _res_cap[e] >= _delta) { |
558 | | _excess[u] -= _res_cap[e]; |
559 | | _excess[v] += _res_cap[e]; |
560 | | (*_flow)[e] = _capacity[e]; |
561 | | _res_cap[e] = 0; |
562 | | } |
563 | | else if (c > 0 && (*_flow)[e] >= _delta) { |
564 | | _excess[u] += (*_flow)[e]; |
565 | | _excess[v] -= (*_flow)[e]; |
566 | | (*_flow)[e] = 0; |
567 | | _res_cap[e] = _capacity[e]; |
| 736 | // Saturate all arcs not satisfying the optimality condition |
| 737 | for (int u = 0; u != _node_num; ++u) { |
| 738 | for (int a = _first_out[u]; a != _first_out[u+1]; ++a) { |
| 739 | int v = _target[a]; |
| 740 | Cost c = _cost[a] + _pi[u] - _pi[v]; |
| 741 | Value rc = _res_cap[a]; |
| 742 | if (c < 0 && rc >= _delta) { |
| 743 | _excess[u] -= rc; |
| 744 | _excess[v] += rc; |
| 745 | _res_cap[a] = 0; |
| 746 | _res_cap[_reverse[a]] += rc; |
| 747 | } |
653 | | /// Execute the successive shortest path algorithm. |
654 | | bool startWithoutScaling() { |
655 | | // Finding excess nodes |
656 | | for (NodeIt n(_graph); n != INVALID; ++n) |
657 | | if (_excess[n] > 0) _excess_nodes.push_back(n); |
658 | | if (_excess_nodes.size() == 0) return true; |
| 815 | // Execute the successive shortest path algorithm |
| 816 | ProblemType startWithoutScaling() { |
| 817 | // Find excess nodes |
| 818 | _excess_nodes.clear(); |
| 819 | for (int i = 0; i != _node_num; ++i) { |
| 820 | if (_excess[i] > 0) _excess_nodes.push_back(i); |
| 821 | } |
| 822 | if (_excess_nodes.size() == 0) return OPTIMAL; |