0
2
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 | 137 |
typedef std::vector<char> BoolVector; |
| 138 | 138 |
typedef std::vector<Value> ValueVector; |
| 139 | 139 |
typedef std::vector<Cost> CostVector; |
| 140 | 140 |
|
| 141 | 141 |
private: |
| 142 | 142 |
|
| 143 | 143 |
// Data related to the underlying digraph |
| 144 | 144 |
const GR &_graph; |
| 145 | 145 |
int _node_num; |
| 146 | 146 |
int _arc_num; |
| 147 | 147 |
int _res_arc_num; |
| 148 | 148 |
int _root; |
| 149 | 149 |
|
| 150 | 150 |
// Parameters of the problem |
| 151 | 151 |
bool _have_lower; |
| 152 | 152 |
Value _sum_supply; |
| 153 | 153 |
|
| 154 | 154 |
// Data structures for storing the digraph |
| 155 | 155 |
IntNodeMap _node_id; |
| 156 | 156 |
IntArcMap _arc_idf; |
| 157 | 157 |
IntArcMap _arc_idb; |
| 158 | 158 |
IntVector _first_out; |
| 159 | 159 |
BoolVector _forward; |
| 160 | 160 |
IntVector _source; |
| 161 | 161 |
IntVector _target; |
| 162 | 162 |
IntVector _reverse; |
| 163 | 163 |
|
| 164 | 164 |
// Node and arc data |
| 165 | 165 |
ValueVector _lower; |
| 166 | 166 |
ValueVector _upper; |
| 167 | 167 |
CostVector _cost; |
| 168 | 168 |
ValueVector _supply; |
| 169 | 169 |
|
| 170 | 170 |
ValueVector _res_cap; |
| 171 | 171 |
CostVector _pi; |
| 172 | 172 |
ValueVector _excess; |
| 173 | 173 |
IntVector _excess_nodes; |
| 174 | 174 |
IntVector _deficit_nodes; |
| 175 | 175 |
|
| 176 | 176 |
Value _delta; |
| 177 | 177 |
int _factor; |
| 178 | 178 |
IntVector _pred; |
| 179 | 179 |
|
| 180 | 180 |
public: |
| 181 | 181 |
|
| 182 | 182 |
/// \brief Constant for infinite upper bounds (capacities). |
| 183 | 183 |
/// |
| 184 | 184 |
/// Constant for infinite upper bounds (capacities). |
| 185 | 185 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 186 | 186 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 187 | 187 |
const Value INF; |
| 188 | 188 |
|
| 189 | 189 |
private: |
| 190 | 190 |
|
| 191 | 191 |
// Special implementation of the Dijkstra algorithm for finding |
| 192 | 192 |
// shortest paths in the residual network of the digraph with |
| 193 | 193 |
// respect to the reduced arc costs and modifying the node |
| 194 | 194 |
// potentials according to the found distance labels. |
| 195 | 195 |
class ResidualDijkstra |
| 196 | 196 |
{
|
| 197 | 197 |
private: |
| 198 | 198 |
|
| 199 | 199 |
int _node_num; |
| 200 | 200 |
bool _geq; |
| 201 | 201 |
const IntVector &_first_out; |
| 202 | 202 |
const IntVector &_target; |
| 203 | 203 |
const CostVector &_cost; |
| 204 | 204 |
const ValueVector &_res_cap; |
| 205 | 205 |
const ValueVector &_excess; |
| 206 | 206 |
CostVector &_pi; |
| 207 | 207 |
IntVector &_pred; |
| 208 | 208 |
|
| 209 | 209 |
IntVector _proc_nodes; |
| 210 | 210 |
CostVector _dist; |
| 211 | 211 |
|
| 212 | 212 |
public: |
| 213 | 213 |
|
| 214 | 214 |
ResidualDijkstra(CapacityScaling& cs) : |
| 215 | 215 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
| 216 | 216 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
| 217 | 217 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
| 218 | 218 |
_pred(cs._pred), _dist(cs._node_num) |
| 219 | 219 |
{}
|
| 220 | 220 |
|
| 221 | 221 |
int run(int s, Value delta = 1) {
|
| 222 | 222 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
| 223 | 223 |
Heap heap(heap_cross_ref); |
| 224 | 224 |
heap.push(s, 0); |
| 225 | 225 |
_pred[s] = -1; |
| 226 | 226 |
_proc_nodes.clear(); |
| 227 | 227 |
|
| 228 | 228 |
// Process nodes |
| 229 | 229 |
while (!heap.empty() && _excess[heap.top()] > -delta) {
|
| 230 | 230 |
int u = heap.top(), v; |
| 231 | 231 |
Cost d = heap.prio() + _pi[u], dn; |
| 232 | 232 |
_dist[u] = heap.prio(); |
| 233 | 233 |
_proc_nodes.push_back(u); |
| 234 | 234 |
heap.pop(); |
| 235 | 235 |
|
| 236 | 236 |
// Traverse outgoing residual arcs |
| 237 | 237 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
| 238 | 238 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 239 | 239 |
if (_res_cap[a] < delta) continue; |
| 240 | 240 |
v = _target[a]; |
| 241 | 241 |
switch (heap.state(v)) {
|
| 242 | 242 |
case Heap::PRE_HEAP: |
| 243 | 243 |
heap.push(v, d + _cost[a] - _pi[v]); |
| 244 | 244 |
_pred[v] = a; |
| 245 | 245 |
break; |
| 246 | 246 |
case Heap::IN_HEAP: |
| 247 | 247 |
dn = d + _cost[a] - _pi[v]; |
| 248 | 248 |
if (dn < heap[v]) {
|
| 249 | 249 |
heap.decrease(v, dn); |
| 250 | 250 |
_pred[v] = a; |
| 251 | 251 |
} |
| 252 | 252 |
break; |
| 253 | 253 |
case Heap::POST_HEAP: |
| 254 | 254 |
break; |
| 255 | 255 |
} |
| 256 | 256 |
} |
| 257 | 257 |
} |
| 258 | 258 |
if (heap.empty()) return -1; |
| 259 | 259 |
|
| 260 | 260 |
// Update potentials of processed nodes |
| 261 | 261 |
int t = heap.top(); |
| 262 | 262 |
Cost dt = heap.prio(); |
| 263 | 263 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) {
|
| 264 | 264 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
return t; |
| 268 | 268 |
} |
| 269 | 269 |
|
| 270 | 270 |
}; //class ResidualDijkstra |
| 271 | 271 |
|
| 272 | 272 |
public: |
| 273 | 273 |
|
| 274 | 274 |
/// \name Named Template Parameters |
| 275 | 275 |
/// @{
|
| 276 | 276 |
|
| 277 | 277 |
template <typename T> |
| 278 | 278 |
struct SetHeapTraits : public Traits {
|
| 279 | 279 |
typedef T Heap; |
| 280 | 280 |
}; |
| 281 | 281 |
|
| 282 | 282 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 283 | 283 |
/// \c Heap type. |
| 284 | 284 |
/// |
| 285 | 285 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
| 286 | 286 |
/// type, which is used for internal Dijkstra computations. |
| 287 | 287 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
| 288 | 288 |
/// its priority type must be \c Cost and its cross reference type |
| 289 | 289 |
/// must be \ref RangeMap "RangeMap<int>". |
| 290 | 290 |
template <typename T> |
| 291 | 291 |
struct SetHeap |
| 292 | 292 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
|
| 293 | 293 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
| 294 | 294 |
}; |
| 295 | 295 |
|
| 296 | 296 |
/// @} |
| 297 | 297 |
|
| 298 | 298 |
public: |
| 299 | 299 |
|
| 300 | 300 |
/// \brief Constructor. |
| 301 | 301 |
/// |
| 302 | 302 |
/// The constructor of the class. |
| 303 | 303 |
/// |
| 304 | 304 |
/// \param graph The digraph the algorithm runs on. |
| 305 | 305 |
CapacityScaling(const GR& graph) : |
| 306 | 306 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 307 | 307 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 308 | 308 |
std::numeric_limits<Value>::infinity() : |
| 309 | 309 |
std::numeric_limits<Value>::max()) |
| 310 | 310 |
{
|
| 311 | 311 |
// Check the number types |
| 312 | 312 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 313 | 313 |
"The flow type of CapacityScaling must be signed"); |
| 314 | 314 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 315 | 315 |
"The cost type of CapacityScaling must be signed"); |
| 316 | 316 |
|
| 317 | 317 |
// Resize vectors |
| 318 | 318 |
_node_num = countNodes(_graph); |
| 319 | 319 |
_arc_num = countArcs(_graph); |
| 320 | 320 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 321 | 321 |
_root = _node_num; |
| 322 | 322 |
++_node_num; |
| 323 | 323 |
|
| 324 | 324 |
_first_out.resize(_node_num + 1); |
| 325 | 325 |
_forward.resize(_res_arc_num); |
| 326 | 326 |
_source.resize(_res_arc_num); |
| 327 | 327 |
_target.resize(_res_arc_num); |
| 328 | 328 |
_reverse.resize(_res_arc_num); |
| 329 | 329 |
|
| 330 | 330 |
_lower.resize(_res_arc_num); |
| 331 | 331 |
_upper.resize(_res_arc_num); |
| 332 | 332 |
_cost.resize(_res_arc_num); |
| 333 | 333 |
_supply.resize(_node_num); |
| 334 | 334 |
|
| 335 | 335 |
_res_cap.resize(_res_arc_num); |
| 336 | 336 |
_pi.resize(_node_num); |
| 337 | 337 |
_excess.resize(_node_num); |
| 338 | 338 |
_pred.resize(_node_num); |
| 339 | 339 |
|
| 340 | 340 |
// Copy the graph |
| 341 | 341 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
| 342 | 342 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 343 | 343 |
_node_id[n] = i; |
| 344 | 344 |
} |
| 345 | 345 |
i = 0; |
| 346 | 346 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 347 | 347 |
_first_out[i] = j; |
| 348 | 348 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 349 | 349 |
_arc_idf[a] = j; |
| 350 | 350 |
_forward[j] = true; |
| 351 | 351 |
_source[j] = i; |
| 352 | 352 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 353 | 353 |
} |
| 354 | 354 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 355 | 355 |
_arc_idb[a] = j; |
| 356 | 356 |
_forward[j] = false; |
| 357 | 357 |
_source[j] = i; |
| 358 | 358 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 359 | 359 |
} |
| 360 | 360 |
_forward[j] = false; |
| 361 | 361 |
_source[j] = i; |
| 362 | 362 |
_target[j] = _root; |
| 363 | 363 |
_reverse[j] = k; |
| 364 | 364 |
_forward[k] = true; |
| 365 | 365 |
_source[k] = _root; |
| 366 | 366 |
_target[k] = i; |
| 367 | 367 |
_reverse[k] = j; |
| 368 | 368 |
++j; ++k; |
| 369 | 369 |
} |
| 370 | 370 |
_first_out[i] = j; |
| 371 | 371 |
_first_out[_node_num] = k; |
| 372 | 372 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 373 | 373 |
int fi = _arc_idf[a]; |
| 374 | 374 |
int bi = _arc_idb[a]; |
| 375 | 375 |
_reverse[fi] = bi; |
| 376 | 376 |
_reverse[bi] = fi; |
| 377 | 377 |
} |
| 378 | 378 |
|
| 379 | 379 |
// Reset parameters |
| 380 | 380 |
reset(); |
| 381 | 381 |
} |
| 382 | 382 |
|
| 383 | 383 |
/// \name Parameters |
| 384 | 384 |
/// The parameters of the algorithm can be specified using these |
| 385 | 385 |
/// functions. |
| 386 | 386 |
|
| 387 | 387 |
/// @{
|
| 388 | 388 |
|
| 389 | 389 |
/// \brief Set the lower bounds on the arcs. |
| 390 | 390 |
/// |
| 391 | 391 |
/// This function sets the lower bounds on the arcs. |
| 392 | 392 |
/// If it is not used before calling \ref run(), the lower bounds |
| 393 | 393 |
/// will be set to zero on all arcs. |
| 394 | 394 |
/// |
| 395 | 395 |
/// \param map An arc map storing the lower bounds. |
| 396 | 396 |
/// Its \c Value type must be convertible to the \c Value type |
| 397 | 397 |
/// of the algorithm. |
| 398 | 398 |
/// |
| 399 | 399 |
/// \return <tt>(*this)</tt> |
| 400 | 400 |
template <typename LowerMap> |
| 401 | 401 |
CapacityScaling& lowerMap(const LowerMap& map) {
|
| 402 | 402 |
_have_lower = true; |
| 403 | 403 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 404 | 404 |
_lower[_arc_idf[a]] = map[a]; |
| 405 | 405 |
_lower[_arc_idb[a]] = map[a]; |
| 406 | 406 |
} |
| 407 | 407 |
return *this; |
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 | 410 |
/// \brief Set the upper bounds (capacities) on the arcs. |
| 411 | 411 |
/// |
| 412 | 412 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 413 | 413 |
/// If it is not used before calling \ref run(), the upper bounds |
| 414 | 414 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 415 | 415 |
/// unbounded from above). |
| 416 | 416 |
/// |
| 417 | 417 |
/// \param map An arc map storing the upper bounds. |
| 418 | 418 |
/// Its \c Value type must be convertible to the \c Value type |
| 419 | 419 |
/// of the algorithm. |
| 420 | 420 |
/// |
| 421 | 421 |
/// \return <tt>(*this)</tt> |
| 422 | 422 |
template<typename UpperMap> |
| 423 | 423 |
CapacityScaling& upperMap(const UpperMap& map) {
|
| 424 | 424 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 425 | 425 |
_upper[_arc_idf[a]] = map[a]; |
| 426 | 426 |
} |
| 427 | 427 |
return *this; |
| 428 | 428 |
} |
| 429 | 429 |
|
| 430 | 430 |
/// \brief Set the costs of the arcs. |
| 431 | 431 |
/// |
| 432 | 432 |
/// This function sets the costs of the arcs. |
| 433 | 433 |
/// If it is not used before calling \ref run(), the costs |
| 434 | 434 |
/// will be set to \c 1 on all arcs. |
| 435 | 435 |
/// |
| 436 | 436 |
/// \param map An arc map storing the costs. |
| 437 | 437 |
/// Its \c Value type must be convertible to the \c Cost type |
| 438 | 438 |
/// of the algorithm. |
| 439 | 439 |
/// |
| 440 | 440 |
/// \return <tt>(*this)</tt> |
| 441 | 441 |
template<typename CostMap> |
| 442 | 442 |
CapacityScaling& costMap(const CostMap& map) {
|
| 443 | 443 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 444 | 444 |
_cost[_arc_idf[a]] = map[a]; |
| 445 | 445 |
_cost[_arc_idb[a]] = -map[a]; |
| 446 | 446 |
} |
| 447 | 447 |
return *this; |
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 | 450 |
/// \brief Set the supply values of the nodes. |
| 451 | 451 |
/// |
| 452 | 452 |
/// This function sets the supply values of the nodes. |
| 453 | 453 |
/// If neither this function nor \ref stSupply() is used before |
| 454 | 454 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 455 | 455 |
/// |
| 456 | 456 |
/// \param map A node map storing the supply values. |
| 457 | 457 |
/// Its \c Value type must be convertible to the \c Value type |
| 458 | 458 |
/// of the algorithm. |
| 459 | 459 |
/// |
| 460 | 460 |
/// \return <tt>(*this)</tt> |
| 461 | 461 |
template<typename SupplyMap> |
| 462 | 462 |
CapacityScaling& supplyMap(const SupplyMap& map) {
|
| 463 | 463 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 464 | 464 |
_supply[_node_id[n]] = map[n]; |
| 465 | 465 |
} |
| 466 | 466 |
return *this; |
| 467 | 467 |
} |
| 468 | 468 |
|
| 469 | 469 |
/// \brief Set single source and target nodes and a supply value. |
| 470 | 470 |
/// |
| 471 | 471 |
/// This function sets a single source node and a single target node |
| 472 | 472 |
/// and the required flow value. |
| 473 | 473 |
/// If neither this function nor \ref supplyMap() is used before |
| 474 | 474 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 475 | 475 |
/// |
| 476 | 476 |
/// Using this function has the same effect as using \ref supplyMap() |
| 477 | 477 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 478 | 478 |
/// assigned to \c t and all other nodes have zero supply value. |
| 479 | 479 |
/// |
| 480 | 480 |
/// \param s The source node. |
| 481 | 481 |
/// \param t The target node. |
| 482 | 482 |
/// \param k The required amount of flow from node \c s to node \c t |
| 483 | 483 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 484 | 484 |
/// |
| 485 | 485 |
/// \return <tt>(*this)</tt> |
| 486 | 486 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 487 | 487 |
for (int i = 0; i != _node_num; ++i) {
|
| 488 | 488 |
_supply[i] = 0; |
| 489 | 489 |
} |
| 490 | 490 |
_supply[_node_id[s]] = k; |
| 491 | 491 |
_supply[_node_id[t]] = -k; |
| 492 | 492 |
return *this; |
| 493 | 493 |
} |
| 494 | 494 |
|
| 495 | 495 |
/// @} |
| 496 | 496 |
|
| 497 | 497 |
/// \name Execution control |
| 498 | 498 |
/// The algorithm can be executed using \ref run(). |
| 499 | 499 |
|
| 500 | 500 |
/// @{
|
| 501 | 501 |
|
| 502 | 502 |
/// \brief Run the algorithm. |
| 503 | 503 |
/// |
| 504 | 504 |
/// This function runs the algorithm. |
| 505 | 505 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 506 | 506 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 507 | 507 |
/// For example, |
| 508 | 508 |
/// \code |
| 509 | 509 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 510 | 510 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 511 | 511 |
/// .supplyMap(sup).run(); |
| 512 | 512 |
/// \endcode |
| 513 | 513 |
/// |
| 514 | 514 |
/// This function can be called more than once. All the parameters |
| 515 | 515 |
/// that have been given are kept for the next call, unless |
| 516 | 516 |
/// \ref reset() is called, thus only the modified parameters |
| 517 | 517 |
/// have to be set again. See \ref reset() for examples. |
| 518 | 518 |
/// However, the underlying digraph must not be modified after this |
| 519 | 519 |
/// class have been constructed, since it copies and extends the graph. |
| 520 | 520 |
/// |
| 521 | 521 |
/// \param factor The capacity scaling factor. It must be larger than |
| 522 | 522 |
/// one to use scaling. If it is less or equal to one, then scaling |
| 523 | 523 |
/// will be disabled. |
| 524 | 524 |
/// |
| 525 | 525 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 526 | 526 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 527 | 527 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 528 | 528 |
/// optimal flow and node potentials (primal and dual solutions), |
| 529 | 529 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 530 | 530 |
/// and infinite upper bound. It means that the objective function |
| 531 | 531 |
/// is unbounded on that arc, however, note that it could actually be |
| 532 | 532 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 533 | 533 |
/// these cases. |
| 534 | 534 |
/// |
| 535 | 535 |
/// \see ProblemType |
| 536 | 536 |
ProblemType run(int factor = 4) {
|
| 537 | 537 |
_factor = factor; |
| 538 | 538 |
ProblemType pt = init(); |
| 539 | 539 |
if (pt != OPTIMAL) return pt; |
| 540 | 540 |
return start(); |
| 541 | 541 |
} |
| 542 | 542 |
|
| 543 | 543 |
/// \brief Reset all the parameters that have been given before. |
| 544 | 544 |
/// |
| 545 | 545 |
/// This function resets all the paramaters that have been given |
| 546 | 546 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 547 | 547 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 548 | 548 |
/// |
| 549 | 549 |
/// It is useful for multiple run() calls. If this function is not |
| 550 | 550 |
/// used, all the parameters given before are kept for the next |
| 551 | 551 |
/// \ref run() call. |
| 552 | 552 |
/// However, the underlying digraph must not be modified after this |
| 553 | 553 |
/// class have been constructed, since it copies and extends the graph. |
| 554 | 554 |
/// |
| 555 | 555 |
/// For example, |
| 556 | 556 |
/// \code |
| 557 | 557 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 558 | 558 |
/// |
| 559 | 559 |
/// // First run |
| 560 | 560 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 561 | 561 |
/// .supplyMap(sup).run(); |
| 562 | 562 |
/// |
| 563 | 563 |
/// // Run again with modified cost map (reset() is not called, |
| 564 | 564 |
/// // so only the cost map have to be set again) |
| 565 | 565 |
/// cost[e] += 100; |
| 566 | 566 |
/// cs.costMap(cost).run(); |
| 567 | 567 |
/// |
| 568 | 568 |
/// // Run again from scratch using reset() |
| 569 | 569 |
/// // (the lower bounds will be set to zero on all arcs) |
| 570 | 570 |
/// cs.reset(); |
| 571 | 571 |
/// cs.upperMap(capacity).costMap(cost) |
| 572 | 572 |
/// .supplyMap(sup).run(); |
| 573 | 573 |
/// \endcode |
| 574 | 574 |
/// |
| 575 | 575 |
/// \return <tt>(*this)</tt> |
| 576 | 576 |
CapacityScaling& reset() {
|
| 577 | 577 |
for (int i = 0; i != _node_num; ++i) {
|
| 578 | 578 |
_supply[i] = 0; |
| 579 | 579 |
} |
| 580 | 580 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 581 | 581 |
_lower[j] = 0; |
| 582 | 582 |
_upper[j] = INF; |
| 583 | 583 |
_cost[j] = _forward[j] ? 1 : -1; |
| 584 | 584 |
} |
| 585 | 585 |
_have_lower = false; |
| 586 | 586 |
return *this; |
| 587 | 587 |
} |
| 588 | 588 |
|
| 589 | 589 |
/// @} |
| 590 | 590 |
|
| 591 | 591 |
/// \name Query Functions |
| 592 | 592 |
/// The results of the algorithm can be obtained using these |
| 593 | 593 |
/// functions.\n |
| 594 | 594 |
/// The \ref run() function must be called before using them. |
| 595 | 595 |
|
| 596 | 596 |
/// @{
|
| 597 | 597 |
|
| 598 | 598 |
/// \brief Return the total cost of the found flow. |
| 599 | 599 |
/// |
| 600 | 600 |
/// This function returns the total cost of the found flow. |
| 601 | 601 |
/// Its complexity is O(e). |
| 602 | 602 |
/// |
| 603 | 603 |
/// \note The return type of the function can be specified as a |
| 604 | 604 |
/// template parameter. For example, |
| 605 | 605 |
/// \code |
| 606 | 606 |
/// cs.totalCost<double>(); |
| 607 | 607 |
/// \endcode |
| 608 | 608 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 609 | 609 |
/// type of the algorithm, which is the default return type of the |
| 610 | 610 |
/// function. |
| 611 | 611 |
/// |
| 612 | 612 |
/// \pre \ref run() must be called before using this function. |
| 613 | 613 |
template <typename Number> |
| 614 | 614 |
Number totalCost() const {
|
| 615 | 615 |
Number c = 0; |
| 616 | 616 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 617 | 617 |
int i = _arc_idb[a]; |
| 618 | 618 |
c += static_cast<Number>(_res_cap[i]) * |
| 619 | 619 |
(-static_cast<Number>(_cost[i])); |
| 620 | 620 |
} |
| 621 | 621 |
return c; |
| 622 | 622 |
} |
| 623 | 623 |
|
| 624 | 624 |
#ifndef DOXYGEN |
| 625 | 625 |
Cost totalCost() const {
|
| 626 | 626 |
return totalCost<Cost>(); |
| 627 | 627 |
} |
| 628 | 628 |
#endif |
| 629 | 629 |
|
| 630 | 630 |
/// \brief Return the flow on the given arc. |
| 631 | 631 |
/// |
| 632 | 632 |
/// This function returns the flow on the given arc. |
| 633 | 633 |
/// |
| 634 | 634 |
/// \pre \ref run() must be called before using this function. |
| 635 | 635 |
Value flow(const Arc& a) const {
|
| 636 | 636 |
return _res_cap[_arc_idb[a]]; |
| 637 | 637 |
} |
| 638 | 638 |
|
| 639 | 639 |
/// \brief Return the flow map (the primal solution). |
| 640 | 640 |
/// |
| 641 | 641 |
/// This function copies the flow value on each arc into the given |
| 642 | 642 |
/// map. The \c Value type of the algorithm must be convertible to |
| 643 | 643 |
/// the \c Value type of the map. |
| 644 | 644 |
/// |
| 645 | 645 |
/// \pre \ref run() must be called before using this function. |
| 646 | 646 |
template <typename FlowMap> |
| 647 | 647 |
void flowMap(FlowMap &map) const {
|
| 648 | 648 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 649 | 649 |
map.set(a, _res_cap[_arc_idb[a]]); |
| 650 | 650 |
} |
| 651 | 651 |
} |
| 652 | 652 |
|
| 653 | 653 |
/// \brief Return the potential (dual value) of the given node. |
| 654 | 654 |
/// |
| 655 | 655 |
/// This function returns the potential (dual value) of the |
| 656 | 656 |
/// given node. |
| 657 | 657 |
/// |
| 658 | 658 |
/// \pre \ref run() must be called before using this function. |
| 659 | 659 |
Cost potential(const Node& n) const {
|
| 660 | 660 |
return _pi[_node_id[n]]; |
| 661 | 661 |
} |
| 662 | 662 |
|
| 663 | 663 |
/// \brief Return the potential map (the dual solution). |
| 664 | 664 |
/// |
| 665 | 665 |
/// This function copies the potential (dual value) of each node |
| 666 | 666 |
/// into the given map. |
| 667 | 667 |
/// The \c Cost type of the algorithm must be convertible to the |
| 668 | 668 |
/// \c Value type of the map. |
| 669 | 669 |
/// |
| 670 | 670 |
/// \pre \ref run() must be called before using this function. |
| 671 | 671 |
template <typename PotentialMap> |
| 672 | 672 |
void potentialMap(PotentialMap &map) const {
|
| 673 | 673 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 674 | 674 |
map.set(n, _pi[_node_id[n]]); |
| 675 | 675 |
} |
| 676 | 676 |
} |
| 677 | 677 |
|
| 678 | 678 |
/// @} |
| 679 | 679 |
|
| 680 | 680 |
private: |
| 681 | 681 |
|
| 682 | 682 |
// Initialize the algorithm |
| 683 | 683 |
ProblemType init() {
|
| 684 |
if (_node_num |
|
| 684 |
if (_node_num <= 1) return INFEASIBLE; |
|
| 685 | 685 |
|
| 686 | 686 |
// Check the sum of supply values |
| 687 | 687 |
_sum_supply = 0; |
| 688 | 688 |
for (int i = 0; i != _root; ++i) {
|
| 689 | 689 |
_sum_supply += _supply[i]; |
| 690 | 690 |
} |
| 691 | 691 |
if (_sum_supply > 0) return INFEASIBLE; |
| 692 | 692 |
|
| 693 | 693 |
// Initialize vectors |
| 694 | 694 |
for (int i = 0; i != _root; ++i) {
|
| 695 | 695 |
_pi[i] = 0; |
| 696 | 696 |
_excess[i] = _supply[i]; |
| 697 | 697 |
} |
| 698 | 698 |
|
| 699 | 699 |
// Remove non-zero lower bounds |
| 700 | 700 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 701 | 701 |
int last_out; |
| 702 | 702 |
if (_have_lower) {
|
| 703 | 703 |
for (int i = 0; i != _root; ++i) {
|
| 704 | 704 |
last_out = _first_out[i+1]; |
| 705 | 705 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 706 | 706 |
if (_forward[j]) {
|
| 707 | 707 |
Value c = _lower[j]; |
| 708 | 708 |
if (c >= 0) {
|
| 709 | 709 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
| 710 | 710 |
} else {
|
| 711 | 711 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
| 712 | 712 |
} |
| 713 | 713 |
_excess[i] -= c; |
| 714 | 714 |
_excess[_target[j]] += c; |
| 715 | 715 |
} else {
|
| 716 | 716 |
_res_cap[j] = 0; |
| 717 | 717 |
} |
| 718 | 718 |
} |
| 719 | 719 |
} |
| 720 | 720 |
} else {
|
| 721 | 721 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 722 | 722 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
| 723 | 723 |
} |
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 | 726 |
// Handle negative costs |
| 727 | 727 |
for (int i = 0; i != _root; ++i) {
|
| 728 | 728 |
last_out = _first_out[i+1] - 1; |
| 729 | 729 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 730 | 730 |
Value rc = _res_cap[j]; |
| 731 | 731 |
if (_cost[j] < 0 && rc > 0) {
|
| 732 | 732 |
if (rc >= MAX) return UNBOUNDED; |
| 733 | 733 |
_excess[i] -= rc; |
| 734 | 734 |
_excess[_target[j]] += rc; |
| 735 | 735 |
_res_cap[j] = 0; |
| 736 | 736 |
_res_cap[_reverse[j]] += rc; |
| 737 | 737 |
} |
| 738 | 738 |
} |
| 739 | 739 |
} |
| 740 | 740 |
|
| 741 | 741 |
// Handle GEQ supply type |
| 742 | 742 |
if (_sum_supply < 0) {
|
| 743 | 743 |
_pi[_root] = 0; |
| 744 | 744 |
_excess[_root] = -_sum_supply; |
| 745 | 745 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 746 | 746 |
int ra = _reverse[a]; |
| 747 | 747 |
_res_cap[a] = -_sum_supply + 1; |
| 748 | 748 |
_res_cap[ra] = 0; |
| 749 | 749 |
_cost[a] = 0; |
| 750 | 750 |
_cost[ra] = 0; |
| 751 | 751 |
} |
| 752 | 752 |
} else {
|
| 753 | 753 |
_pi[_root] = 0; |
| 754 | 754 |
_excess[_root] = 0; |
| 755 | 755 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 756 | 756 |
int ra = _reverse[a]; |
| 757 | 757 |
_res_cap[a] = 1; |
| 758 | 758 |
_res_cap[ra] = 0; |
| 759 | 759 |
_cost[a] = 0; |
| 760 | 760 |
_cost[ra] = 0; |
| 761 | 761 |
} |
| 762 | 762 |
} |
| 763 | 763 |
|
| 764 | 764 |
// Initialize delta value |
| 765 | 765 |
if (_factor > 1) {
|
| 766 | 766 |
// With scaling |
| 767 | 767 |
Value max_sup = 0, max_dem = 0; |
| 768 | 768 |
for (int i = 0; i != _node_num; ++i) {
|
| 769 | 769 |
Value ex = _excess[i]; |
| 770 | 770 |
if ( ex > max_sup) max_sup = ex; |
| 771 | 771 |
if (-ex > max_dem) max_dem = -ex; |
| 772 | 772 |
} |
| 773 | 773 |
Value max_cap = 0; |
| 774 | 774 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 775 | 775 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
| 776 | 776 |
} |
| 777 | 777 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
| 778 | 778 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
| 779 | 779 |
} else {
|
| 780 | 780 |
// Without scaling |
| 781 | 781 |
_delta = 1; |
| 782 | 782 |
} |
| 783 | 783 |
|
| 784 | 784 |
return OPTIMAL; |
| 785 | 785 |
} |
| 786 | 786 |
|
| 787 | 787 |
ProblemType start() {
|
| 788 | 788 |
// Execute the algorithm |
| 789 | 789 |
ProblemType pt; |
| 790 | 790 |
if (_delta > 1) |
| 791 | 791 |
pt = startWithScaling(); |
| 792 | 792 |
else |
| 793 | 793 |
pt = startWithoutScaling(); |
| 794 | 794 |
|
| 795 | 795 |
// Handle non-zero lower bounds |
| 796 | 796 |
if (_have_lower) {
|
| 797 | 797 |
int limit = _first_out[_root]; |
| 798 | 798 |
for (int j = 0; j != limit; ++j) {
|
| 799 | 799 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 800 | 800 |
} |
| 801 | 801 |
} |
| 802 | 802 |
|
| 803 | 803 |
// Shift potentials if necessary |
| 804 | 804 |
Cost pr = _pi[_root]; |
| 805 | 805 |
if (_sum_supply < 0 || pr > 0) {
|
| 806 | 806 |
for (int i = 0; i != _node_num; ++i) {
|
| 807 | 807 |
_pi[i] -= pr; |
| 808 | 808 |
} |
| 809 | 809 |
} |
| 810 | 810 |
|
| 811 | 811 |
return pt; |
| 812 | 812 |
} |
| 813 | 813 |
|
| 814 | 814 |
// Execute the capacity scaling algorithm |
| 815 | 815 |
ProblemType startWithScaling() {
|
| 816 | 816 |
// Perform capacity scaling phases |
| 817 | 817 |
int s, t; |
| 818 | 818 |
ResidualDijkstra _dijkstra(*this); |
| 819 | 819 |
while (true) {
|
| 820 | 820 |
// Saturate all arcs not satisfying the optimality condition |
| 821 | 821 |
int last_out; |
| 822 | 822 |
for (int u = 0; u != _node_num; ++u) {
|
| 823 | 823 |
last_out = _sum_supply < 0 ? |
| 824 | 824 |
_first_out[u+1] : _first_out[u+1] - 1; |
| 825 | 825 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 826 | 826 |
int v = _target[a]; |
| 827 | 827 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
| 828 | 828 |
Value rc = _res_cap[a]; |
| 829 | 829 |
if (c < 0 && rc >= _delta) {
|
| 830 | 830 |
_excess[u] -= rc; |
| 831 | 831 |
_excess[v] += rc; |
| 832 | 832 |
_res_cap[a] = 0; |
| 833 | 833 |
_res_cap[_reverse[a]] += rc; |
| 834 | 834 |
} |
| 835 | 835 |
} |
| 836 | 836 |
} |
| 837 | 837 |
|
| 838 | 838 |
// Find excess nodes and deficit nodes |
| 839 | 839 |
_excess_nodes.clear(); |
| 840 | 840 |
_deficit_nodes.clear(); |
| 841 | 841 |
for (int u = 0; u != _node_num; ++u) {
|
| 842 | 842 |
Value ex = _excess[u]; |
| 843 | 843 |
if (ex >= _delta) _excess_nodes.push_back(u); |
| 844 | 844 |
if (ex <= -_delta) _deficit_nodes.push_back(u); |
| 845 | 845 |
} |
| 846 | 846 |
int next_node = 0, next_def_node = 0; |
| 847 | 847 |
|
| 848 | 848 |
// Find augmenting shortest paths |
| 849 | 849 |
while (next_node < int(_excess_nodes.size())) {
|
| 850 | 850 |
// Check deficit nodes |
| 851 | 851 |
if (_delta > 1) {
|
| 852 | 852 |
bool delta_deficit = false; |
| 853 | 853 |
for ( ; next_def_node < int(_deficit_nodes.size()); |
| 854 | 854 |
++next_def_node ) {
|
| 855 | 855 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
|
| 856 | 856 |
delta_deficit = true; |
| 857 | 857 |
break; |
| 858 | 858 |
} |
| 859 | 859 |
} |
| 860 | 860 |
if (!delta_deficit) break; |
| 861 | 861 |
} |
| 862 | 862 |
|
| 863 | 863 |
// Run Dijkstra in the residual network |
| 864 | 864 |
s = _excess_nodes[next_node]; |
| 865 | 865 |
if ((t = _dijkstra.run(s, _delta)) == -1) {
|
| 866 | 866 |
if (_delta > 1) {
|
| 867 | 867 |
++next_node; |
| 868 | 868 |
continue; |
| 869 | 869 |
} |
| 870 | 870 |
return INFEASIBLE; |
| 871 | 871 |
} |
| 872 | 872 |
|
| 873 | 873 |
// Augment along a shortest path from s to t |
| 874 | 874 |
Value d = std::min(_excess[s], -_excess[t]); |
| 875 | 875 |
int u = t; |
| 876 | 876 |
int a; |
| 877 | 877 |
if (d > _delta) {
|
| 878 | 878 |
while ((a = _pred[u]) != -1) {
|
| 879 | 879 |
if (_res_cap[a] < d) d = _res_cap[a]; |
| 880 | 880 |
u = _source[a]; |
| 881 | 881 |
} |
| 882 | 882 |
} |
| 883 | 883 |
u = t; |
| 884 | 884 |
while ((a = _pred[u]) != -1) {
|
| 885 | 885 |
_res_cap[a] -= d; |
| 886 | 886 |
_res_cap[_reverse[a]] += d; |
| 887 | 887 |
u = _source[a]; |
| 888 | 888 |
} |
| 889 | 889 |
_excess[s] -= d; |
| 890 | 890 |
_excess[t] += d; |
| 891 | 891 |
|
| 892 | 892 |
if (_excess[s] < _delta) ++next_node; |
| 893 | 893 |
} |
| 894 | 894 |
|
| 895 | 895 |
if (_delta == 1) break; |
| 896 | 896 |
_delta = _delta <= _factor ? 1 : _delta / _factor; |
| 897 | 897 |
} |
| 898 | 898 |
|
| 899 | 899 |
return OPTIMAL; |
| 900 | 900 |
} |
| 901 | 901 |
|
| 902 | 902 |
// Execute the successive shortest path algorithm |
| 903 | 903 |
ProblemType startWithoutScaling() {
|
| 904 | 904 |
// Find excess nodes |
| 905 | 905 |
_excess_nodes.clear(); |
| 906 | 906 |
for (int i = 0; i != _node_num; ++i) {
|
| 907 | 907 |
if (_excess[i] > 0) _excess_nodes.push_back(i); |
| 908 | 908 |
} |
| 909 | 909 |
if (_excess_nodes.size() == 0) return OPTIMAL; |
| 910 | 910 |
int next_node = 0; |
| 911 | 911 |
|
| 912 | 912 |
// Find shortest paths |
| 913 | 913 |
int s, t; |
| 914 | 914 |
ResidualDijkstra _dijkstra(*this); |
| 915 | 915 |
while ( _excess[_excess_nodes[next_node]] > 0 || |
| 916 | 916 |
++next_node < int(_excess_nodes.size()) ) |
| 917 | 917 |
{
|
| 918 | 918 |
// Run Dijkstra in the residual network |
| 919 | 919 |
s = _excess_nodes[next_node]; |
| 920 | 920 |
if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE; |
| 921 | 921 |
|
| 922 | 922 |
// Augment along a shortest path from s to t |
| 923 | 923 |
Value d = std::min(_excess[s], -_excess[t]); |
| 924 | 924 |
int u = t; |
| 925 | 925 |
int a; |
| 926 | 926 |
if (d > 1) {
|
| 927 | 927 |
while ((a = _pred[u]) != -1) {
|
| 928 | 928 |
if (_res_cap[a] < d) d = _res_cap[a]; |
| 929 | 929 |
u = _source[a]; |
| 930 | 930 |
} |
| 931 | 931 |
} |
| 932 | 932 |
u = t; |
| 933 | 933 |
while ((a = _pred[u]) != -1) {
|
| 934 | 934 |
_res_cap[a] -= d; |
| 935 | 935 |
_res_cap[_reverse[a]] += d; |
| 936 | 936 |
u = _source[a]; |
| 937 | 937 |
} |
| 938 | 938 |
_excess[s] -= d; |
| 939 | 939 |
_excess[t] += d; |
| 940 | 940 |
} |
| 941 | 941 |
|
| 942 | 942 |
return OPTIMAL; |
| 943 | 943 |
} |
| 944 | 944 |
|
| 945 | 945 |
}; //class CapacityScaling |
| 946 | 946 |
|
| 947 | 947 |
///@} |
| 948 | 948 |
|
| 949 | 949 |
} //namespace lemon |
| 950 | 950 |
|
| 951 | 951 |
#endif //LEMON_CAPACITY_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_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 | 200 |
typedef std::vector<char> BoolVector; |
| 201 | 201 |
typedef std::vector<Value> ValueVector; |
| 202 | 202 |
typedef std::vector<Cost> CostVector; |
| 203 | 203 |
typedef std::vector<LargeCost> LargeCostVector; |
| 204 | 204 |
|
| 205 | 205 |
private: |
| 206 | 206 |
|
| 207 | 207 |
template <typename KT, typename VT> |
| 208 | 208 |
class StaticVectorMap {
|
| 209 | 209 |
public: |
| 210 | 210 |
typedef KT Key; |
| 211 | 211 |
typedef VT Value; |
| 212 | 212 |
|
| 213 | 213 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 214 | 214 |
|
| 215 | 215 |
const Value& operator[](const Key& key) const {
|
| 216 | 216 |
return _v[StaticDigraph::id(key)]; |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
Value& operator[](const Key& key) {
|
| 220 | 220 |
return _v[StaticDigraph::id(key)]; |
| 221 | 221 |
} |
| 222 | 222 |
|
| 223 | 223 |
void set(const Key& key, const Value& val) {
|
| 224 | 224 |
_v[StaticDigraph::id(key)] = val; |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
private: |
| 228 | 228 |
std::vector<Value>& _v; |
| 229 | 229 |
}; |
| 230 | 230 |
|
| 231 | 231 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
| 232 | 232 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
| 233 | 233 |
|
| 234 | 234 |
private: |
| 235 | 235 |
|
| 236 | 236 |
// Data related to the underlying digraph |
| 237 | 237 |
const GR &_graph; |
| 238 | 238 |
int _node_num; |
| 239 | 239 |
int _arc_num; |
| 240 | 240 |
int _res_node_num; |
| 241 | 241 |
int _res_arc_num; |
| 242 | 242 |
int _root; |
| 243 | 243 |
|
| 244 | 244 |
// Parameters of the problem |
| 245 | 245 |
bool _have_lower; |
| 246 | 246 |
Value _sum_supply; |
| 247 | 247 |
|
| 248 | 248 |
// Data structures for storing the digraph |
| 249 | 249 |
IntNodeMap _node_id; |
| 250 | 250 |
IntArcMap _arc_idf; |
| 251 | 251 |
IntArcMap _arc_idb; |
| 252 | 252 |
IntVector _first_out; |
| 253 | 253 |
BoolVector _forward; |
| 254 | 254 |
IntVector _source; |
| 255 | 255 |
IntVector _target; |
| 256 | 256 |
IntVector _reverse; |
| 257 | 257 |
|
| 258 | 258 |
// Node and arc data |
| 259 | 259 |
ValueVector _lower; |
| 260 | 260 |
ValueVector _upper; |
| 261 | 261 |
CostVector _scost; |
| 262 | 262 |
ValueVector _supply; |
| 263 | 263 |
|
| 264 | 264 |
ValueVector _res_cap; |
| 265 | 265 |
LargeCostVector _cost; |
| 266 | 266 |
LargeCostVector _pi; |
| 267 | 267 |
ValueVector _excess; |
| 268 | 268 |
IntVector _next_out; |
| 269 | 269 |
std::deque<int> _active_nodes; |
| 270 | 270 |
|
| 271 | 271 |
// Data for scaling |
| 272 | 272 |
LargeCost _epsilon; |
| 273 | 273 |
int _alpha; |
| 274 | 274 |
|
| 275 | 275 |
// Data for a StaticDigraph structure |
| 276 | 276 |
typedef std::pair<int, int> IntPair; |
| 277 | 277 |
StaticDigraph _sgr; |
| 278 | 278 |
std::vector<IntPair> _arc_vec; |
| 279 | 279 |
std::vector<LargeCost> _cost_vec; |
| 280 | 280 |
LargeCostArcMap _cost_map; |
| 281 | 281 |
LargeCostNodeMap _pi_map; |
| 282 | 282 |
|
| 283 | 283 |
public: |
| 284 | 284 |
|
| 285 | 285 |
/// \brief Constant for infinite upper bounds (capacities). |
| 286 | 286 |
/// |
| 287 | 287 |
/// Constant for infinite upper bounds (capacities). |
| 288 | 288 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 289 | 289 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 290 | 290 |
const Value INF; |
| 291 | 291 |
|
| 292 | 292 |
public: |
| 293 | 293 |
|
| 294 | 294 |
/// \name Named Template Parameters |
| 295 | 295 |
/// @{
|
| 296 | 296 |
|
| 297 | 297 |
template <typename T> |
| 298 | 298 |
struct SetLargeCostTraits : public Traits {
|
| 299 | 299 |
typedef T LargeCost; |
| 300 | 300 |
}; |
| 301 | 301 |
|
| 302 | 302 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 303 | 303 |
/// \c LargeCost type. |
| 304 | 304 |
/// |
| 305 | 305 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
| 306 | 306 |
/// type, which is used for internal computations in the algorithm. |
| 307 | 307 |
/// \c Cost must be convertible to \c LargeCost. |
| 308 | 308 |
template <typename T> |
| 309 | 309 |
struct SetLargeCost |
| 310 | 310 |
: public CostScaling<GR, V, C, SetLargeCostTraits<T> > {
|
| 311 | 311 |
typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
| 312 | 312 |
}; |
| 313 | 313 |
|
| 314 | 314 |
/// @} |
| 315 | 315 |
|
| 316 | 316 |
public: |
| 317 | 317 |
|
| 318 | 318 |
/// \brief Constructor. |
| 319 | 319 |
/// |
| 320 | 320 |
/// The constructor of the class. |
| 321 | 321 |
/// |
| 322 | 322 |
/// \param graph The digraph the algorithm runs on. |
| 323 | 323 |
CostScaling(const GR& graph) : |
| 324 | 324 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 325 | 325 |
_cost_map(_cost_vec), _pi_map(_pi), |
| 326 | 326 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 327 | 327 |
std::numeric_limits<Value>::infinity() : |
| 328 | 328 |
std::numeric_limits<Value>::max()) |
| 329 | 329 |
{
|
| 330 | 330 |
// Check the number types |
| 331 | 331 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 332 | 332 |
"The flow type of CostScaling must be signed"); |
| 333 | 333 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 334 | 334 |
"The cost type of CostScaling must be signed"); |
| 335 | 335 |
|
| 336 | 336 |
// Resize vectors |
| 337 | 337 |
_node_num = countNodes(_graph); |
| 338 | 338 |
_arc_num = countArcs(_graph); |
| 339 | 339 |
_res_node_num = _node_num + 1; |
| 340 | 340 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 341 | 341 |
_root = _node_num; |
| 342 | 342 |
|
| 343 | 343 |
_first_out.resize(_res_node_num + 1); |
| 344 | 344 |
_forward.resize(_res_arc_num); |
| 345 | 345 |
_source.resize(_res_arc_num); |
| 346 | 346 |
_target.resize(_res_arc_num); |
| 347 | 347 |
_reverse.resize(_res_arc_num); |
| 348 | 348 |
|
| 349 | 349 |
_lower.resize(_res_arc_num); |
| 350 | 350 |
_upper.resize(_res_arc_num); |
| 351 | 351 |
_scost.resize(_res_arc_num); |
| 352 | 352 |
_supply.resize(_res_node_num); |
| 353 | 353 |
|
| 354 | 354 |
_res_cap.resize(_res_arc_num); |
| 355 | 355 |
_cost.resize(_res_arc_num); |
| 356 | 356 |
_pi.resize(_res_node_num); |
| 357 | 357 |
_excess.resize(_res_node_num); |
| 358 | 358 |
_next_out.resize(_res_node_num); |
| 359 | 359 |
|
| 360 | 360 |
_arc_vec.reserve(_res_arc_num); |
| 361 | 361 |
_cost_vec.reserve(_res_arc_num); |
| 362 | 362 |
|
| 363 | 363 |
// Copy the graph |
| 364 | 364 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num; |
| 365 | 365 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 366 | 366 |
_node_id[n] = i; |
| 367 | 367 |
} |
| 368 | 368 |
i = 0; |
| 369 | 369 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 370 | 370 |
_first_out[i] = j; |
| 371 | 371 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 372 | 372 |
_arc_idf[a] = j; |
| 373 | 373 |
_forward[j] = true; |
| 374 | 374 |
_source[j] = i; |
| 375 | 375 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 376 | 376 |
} |
| 377 | 377 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 378 | 378 |
_arc_idb[a] = j; |
| 379 | 379 |
_forward[j] = false; |
| 380 | 380 |
_source[j] = i; |
| 381 | 381 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 382 | 382 |
} |
| 383 | 383 |
_forward[j] = false; |
| 384 | 384 |
_source[j] = i; |
| 385 | 385 |
_target[j] = _root; |
| 386 | 386 |
_reverse[j] = k; |
| 387 | 387 |
_forward[k] = true; |
| 388 | 388 |
_source[k] = _root; |
| 389 | 389 |
_target[k] = i; |
| 390 | 390 |
_reverse[k] = j; |
| 391 | 391 |
++j; ++k; |
| 392 | 392 |
} |
| 393 | 393 |
_first_out[i] = j; |
| 394 | 394 |
_first_out[_res_node_num] = k; |
| 395 | 395 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 396 | 396 |
int fi = _arc_idf[a]; |
| 397 | 397 |
int bi = _arc_idb[a]; |
| 398 | 398 |
_reverse[fi] = bi; |
| 399 | 399 |
_reverse[bi] = fi; |
| 400 | 400 |
} |
| 401 | 401 |
|
| 402 | 402 |
// Reset parameters |
| 403 | 403 |
reset(); |
| 404 | 404 |
} |
| 405 | 405 |
|
| 406 | 406 |
/// \name Parameters |
| 407 | 407 |
/// The parameters of the algorithm can be specified using these |
| 408 | 408 |
/// functions. |
| 409 | 409 |
|
| 410 | 410 |
/// @{
|
| 411 | 411 |
|
| 412 | 412 |
/// \brief Set the lower bounds on the arcs. |
| 413 | 413 |
/// |
| 414 | 414 |
/// This function sets the lower bounds on the arcs. |
| 415 | 415 |
/// If it is not used before calling \ref run(), the lower bounds |
| 416 | 416 |
/// will be set to zero on all arcs. |
| 417 | 417 |
/// |
| 418 | 418 |
/// \param map An arc map storing the lower bounds. |
| 419 | 419 |
/// Its \c Value type must be convertible to the \c Value type |
| 420 | 420 |
/// of the algorithm. |
| 421 | 421 |
/// |
| 422 | 422 |
/// \return <tt>(*this)</tt> |
| 423 | 423 |
template <typename LowerMap> |
| 424 | 424 |
CostScaling& lowerMap(const LowerMap& map) {
|
| 425 | 425 |
_have_lower = true; |
| 426 | 426 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 427 | 427 |
_lower[_arc_idf[a]] = map[a]; |
| 428 | 428 |
_lower[_arc_idb[a]] = map[a]; |
| 429 | 429 |
} |
| 430 | 430 |
return *this; |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
/// \brief Set the upper bounds (capacities) on the arcs. |
| 434 | 434 |
/// |
| 435 | 435 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 436 | 436 |
/// If it is not used before calling \ref run(), the upper bounds |
| 437 | 437 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 438 | 438 |
/// unbounded from above). |
| 439 | 439 |
/// |
| 440 | 440 |
/// \param map An arc map storing the upper bounds. |
| 441 | 441 |
/// Its \c Value type must be convertible to the \c Value type |
| 442 | 442 |
/// of the algorithm. |
| 443 | 443 |
/// |
| 444 | 444 |
/// \return <tt>(*this)</tt> |
| 445 | 445 |
template<typename UpperMap> |
| 446 | 446 |
CostScaling& upperMap(const UpperMap& map) {
|
| 447 | 447 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 448 | 448 |
_upper[_arc_idf[a]] = map[a]; |
| 449 | 449 |
} |
| 450 | 450 |
return *this; |
| 451 | 451 |
} |
| 452 | 452 |
|
| 453 | 453 |
/// \brief Set the costs of the arcs. |
| 454 | 454 |
/// |
| 455 | 455 |
/// This function sets the costs of the arcs. |
| 456 | 456 |
/// If it is not used before calling \ref run(), the costs |
| 457 | 457 |
/// will be set to \c 1 on all arcs. |
| 458 | 458 |
/// |
| 459 | 459 |
/// \param map An arc map storing the costs. |
| 460 | 460 |
/// Its \c Value type must be convertible to the \c Cost type |
| 461 | 461 |
/// of the algorithm. |
| 462 | 462 |
/// |
| 463 | 463 |
/// \return <tt>(*this)</tt> |
| 464 | 464 |
template<typename CostMap> |
| 465 | 465 |
CostScaling& costMap(const CostMap& map) {
|
| 466 | 466 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 467 | 467 |
_scost[_arc_idf[a]] = map[a]; |
| 468 | 468 |
_scost[_arc_idb[a]] = -map[a]; |
| 469 | 469 |
} |
| 470 | 470 |
return *this; |
| 471 | 471 |
} |
| 472 | 472 |
|
| 473 | 473 |
/// \brief Set the supply values of the nodes. |
| 474 | 474 |
/// |
| 475 | 475 |
/// This function sets the supply values of the nodes. |
| 476 | 476 |
/// If neither this function nor \ref stSupply() is used before |
| 477 | 477 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 478 | 478 |
/// |
| 479 | 479 |
/// \param map A node map storing the supply values. |
| 480 | 480 |
/// Its \c Value type must be convertible to the \c Value type |
| 481 | 481 |
/// of the algorithm. |
| 482 | 482 |
/// |
| 483 | 483 |
/// \return <tt>(*this)</tt> |
| 484 | 484 |
template<typename SupplyMap> |
| 485 | 485 |
CostScaling& supplyMap(const SupplyMap& map) {
|
| 486 | 486 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 487 | 487 |
_supply[_node_id[n]] = map[n]; |
| 488 | 488 |
} |
| 489 | 489 |
return *this; |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
/// \brief Set single source and target nodes and a supply value. |
| 493 | 493 |
/// |
| 494 | 494 |
/// This function sets a single source node and a single target node |
| 495 | 495 |
/// and the required flow value. |
| 496 | 496 |
/// If neither this function nor \ref supplyMap() is used before |
| 497 | 497 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 498 | 498 |
/// |
| 499 | 499 |
/// Using this function has the same effect as using \ref supplyMap() |
| 500 | 500 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 501 | 501 |
/// assigned to \c t and all other nodes have zero supply value. |
| 502 | 502 |
/// |
| 503 | 503 |
/// \param s The source node. |
| 504 | 504 |
/// \param t The target node. |
| 505 | 505 |
/// \param k The required amount of flow from node \c s to node \c t |
| 506 | 506 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 507 | 507 |
/// |
| 508 | 508 |
/// \return <tt>(*this)</tt> |
| 509 | 509 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 510 | 510 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 511 | 511 |
_supply[i] = 0; |
| 512 | 512 |
} |
| 513 | 513 |
_supply[_node_id[s]] = k; |
| 514 | 514 |
_supply[_node_id[t]] = -k; |
| 515 | 515 |
return *this; |
| 516 | 516 |
} |
| 517 | 517 |
|
| 518 | 518 |
/// @} |
| 519 | 519 |
|
| 520 | 520 |
/// \name Execution control |
| 521 | 521 |
/// The algorithm can be executed using \ref run(). |
| 522 | 522 |
|
| 523 | 523 |
/// @{
|
| 524 | 524 |
|
| 525 | 525 |
/// \brief Run the algorithm. |
| 526 | 526 |
/// |
| 527 | 527 |
/// This function runs the algorithm. |
| 528 | 528 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 529 | 529 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 530 | 530 |
/// For example, |
| 531 | 531 |
/// \code |
| 532 | 532 |
/// CostScaling<ListDigraph> cs(graph); |
| 533 | 533 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 534 | 534 |
/// .supplyMap(sup).run(); |
| 535 | 535 |
/// \endcode |
| 536 | 536 |
/// |
| 537 | 537 |
/// This function can be called more than once. All the parameters |
| 538 | 538 |
/// that have been given are kept for the next call, unless |
| 539 | 539 |
/// \ref reset() is called, thus only the modified parameters |
| 540 | 540 |
/// have to be set again. See \ref reset() for examples. |
| 541 | 541 |
/// However, the underlying digraph must not be modified after this |
| 542 | 542 |
/// class have been constructed, since it copies and extends the graph. |
| 543 | 543 |
/// |
| 544 | 544 |
/// \param method The internal method that will be used in the |
| 545 | 545 |
/// algorithm. For more information, see \ref Method. |
| 546 | 546 |
/// \param factor The cost scaling factor. It must be larger than one. |
| 547 | 547 |
/// |
| 548 | 548 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 549 | 549 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 550 | 550 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 551 | 551 |
/// optimal flow and node potentials (primal and dual solutions), |
| 552 | 552 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 553 | 553 |
/// and infinite upper bound. It means that the objective function |
| 554 | 554 |
/// is unbounded on that arc, however, note that it could actually be |
| 555 | 555 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 556 | 556 |
/// these cases. |
| 557 | 557 |
/// |
| 558 | 558 |
/// \see ProblemType, Method |
| 559 | 559 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) {
|
| 560 | 560 |
_alpha = factor; |
| 561 | 561 |
ProblemType pt = init(); |
| 562 | 562 |
if (pt != OPTIMAL) return pt; |
| 563 | 563 |
start(method); |
| 564 | 564 |
return OPTIMAL; |
| 565 | 565 |
} |
| 566 | 566 |
|
| 567 | 567 |
/// \brief Reset all the parameters that have been given before. |
| 568 | 568 |
/// |
| 569 | 569 |
/// This function resets all the paramaters that have been given |
| 570 | 570 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 571 | 571 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 572 | 572 |
/// |
| 573 | 573 |
/// It is useful for multiple run() calls. If this function is not |
| 574 | 574 |
/// used, all the parameters given before are kept for the next |
| 575 | 575 |
/// \ref run() call. |
| 576 | 576 |
/// However, the underlying digraph must not be modified after this |
| 577 | 577 |
/// class have been constructed, since it copies and extends the graph. |
| 578 | 578 |
/// |
| 579 | 579 |
/// For example, |
| 580 | 580 |
/// \code |
| 581 | 581 |
/// CostScaling<ListDigraph> cs(graph); |
| 582 | 582 |
/// |
| 583 | 583 |
/// // First run |
| 584 | 584 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 585 | 585 |
/// .supplyMap(sup).run(); |
| 586 | 586 |
/// |
| 587 | 587 |
/// // Run again with modified cost map (reset() is not called, |
| 588 | 588 |
/// // so only the cost map have to be set again) |
| 589 | 589 |
/// cost[e] += 100; |
| 590 | 590 |
/// cs.costMap(cost).run(); |
| 591 | 591 |
/// |
| 592 | 592 |
/// // Run again from scratch using reset() |
| 593 | 593 |
/// // (the lower bounds will be set to zero on all arcs) |
| 594 | 594 |
/// cs.reset(); |
| 595 | 595 |
/// cs.upperMap(capacity).costMap(cost) |
| 596 | 596 |
/// .supplyMap(sup).run(); |
| 597 | 597 |
/// \endcode |
| 598 | 598 |
/// |
| 599 | 599 |
/// \return <tt>(*this)</tt> |
| 600 | 600 |
CostScaling& reset() {
|
| 601 | 601 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 602 | 602 |
_supply[i] = 0; |
| 603 | 603 |
} |
| 604 | 604 |
int limit = _first_out[_root]; |
| 605 | 605 |
for (int j = 0; j != limit; ++j) {
|
| 606 | 606 |
_lower[j] = 0; |
| 607 | 607 |
_upper[j] = INF; |
| 608 | 608 |
_scost[j] = _forward[j] ? 1 : -1; |
| 609 | 609 |
} |
| 610 | 610 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 611 | 611 |
_lower[j] = 0; |
| 612 | 612 |
_upper[j] = INF; |
| 613 | 613 |
_scost[j] = 0; |
| 614 | 614 |
_scost[_reverse[j]] = 0; |
| 615 | 615 |
} |
| 616 | 616 |
_have_lower = false; |
| 617 | 617 |
return *this; |
| 618 | 618 |
} |
| 619 | 619 |
|
| 620 | 620 |
/// @} |
| 621 | 621 |
|
| 622 | 622 |
/// \name Query Functions |
| 623 | 623 |
/// The results of the algorithm can be obtained using these |
| 624 | 624 |
/// functions.\n |
| 625 | 625 |
/// The \ref run() function must be called before using them. |
| 626 | 626 |
|
| 627 | 627 |
/// @{
|
| 628 | 628 |
|
| 629 | 629 |
/// \brief Return the total cost of the found flow. |
| 630 | 630 |
/// |
| 631 | 631 |
/// This function returns the total cost of the found flow. |
| 632 | 632 |
/// Its complexity is O(e). |
| 633 | 633 |
/// |
| 634 | 634 |
/// \note The return type of the function can be specified as a |
| 635 | 635 |
/// template parameter. For example, |
| 636 | 636 |
/// \code |
| 637 | 637 |
/// cs.totalCost<double>(); |
| 638 | 638 |
/// \endcode |
| 639 | 639 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 640 | 640 |
/// type of the algorithm, which is the default return type of the |
| 641 | 641 |
/// function. |
| 642 | 642 |
/// |
| 643 | 643 |
/// \pre \ref run() must be called before using this function. |
| 644 | 644 |
template <typename Number> |
| 645 | 645 |
Number totalCost() const {
|
| 646 | 646 |
Number c = 0; |
| 647 | 647 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 648 | 648 |
int i = _arc_idb[a]; |
| 649 | 649 |
c += static_cast<Number>(_res_cap[i]) * |
| 650 | 650 |
(-static_cast<Number>(_scost[i])); |
| 651 | 651 |
} |
| 652 | 652 |
return c; |
| 653 | 653 |
} |
| 654 | 654 |
|
| 655 | 655 |
#ifndef DOXYGEN |
| 656 | 656 |
Cost totalCost() const {
|
| 657 | 657 |
return totalCost<Cost>(); |
| 658 | 658 |
} |
| 659 | 659 |
#endif |
| 660 | 660 |
|
| 661 | 661 |
/// \brief Return the flow on the given arc. |
| 662 | 662 |
/// |
| 663 | 663 |
/// This function returns the flow on the given arc. |
| 664 | 664 |
/// |
| 665 | 665 |
/// \pre \ref run() must be called before using this function. |
| 666 | 666 |
Value flow(const Arc& a) const {
|
| 667 | 667 |
return _res_cap[_arc_idb[a]]; |
| 668 | 668 |
} |
| 669 | 669 |
|
| 670 | 670 |
/// \brief Return the flow map (the primal solution). |
| 671 | 671 |
/// |
| 672 | 672 |
/// This function copies the flow value on each arc into the given |
| 673 | 673 |
/// map. The \c Value type of the algorithm must be convertible to |
| 674 | 674 |
/// the \c Value type of the map. |
| 675 | 675 |
/// |
| 676 | 676 |
/// \pre \ref run() must be called before using this function. |
| 677 | 677 |
template <typename FlowMap> |
| 678 | 678 |
void flowMap(FlowMap &map) const {
|
| 679 | 679 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 680 | 680 |
map.set(a, _res_cap[_arc_idb[a]]); |
| 681 | 681 |
} |
| 682 | 682 |
} |
| 683 | 683 |
|
| 684 | 684 |
/// \brief Return the potential (dual value) of the given node. |
| 685 | 685 |
/// |
| 686 | 686 |
/// This function returns the potential (dual value) of the |
| 687 | 687 |
/// given node. |
| 688 | 688 |
/// |
| 689 | 689 |
/// \pre \ref run() must be called before using this function. |
| 690 | 690 |
Cost potential(const Node& n) const {
|
| 691 | 691 |
return static_cast<Cost>(_pi[_node_id[n]]); |
| 692 | 692 |
} |
| 693 | 693 |
|
| 694 | 694 |
/// \brief Return the potential map (the dual solution). |
| 695 | 695 |
/// |
| 696 | 696 |
/// This function copies the potential (dual value) of each node |
| 697 | 697 |
/// into the given map. |
| 698 | 698 |
/// The \c Cost type of the algorithm must be convertible to the |
| 699 | 699 |
/// \c Value type of the map. |
| 700 | 700 |
/// |
| 701 | 701 |
/// \pre \ref run() must be called before using this function. |
| 702 | 702 |
template <typename PotentialMap> |
| 703 | 703 |
void potentialMap(PotentialMap &map) const {
|
| 704 | 704 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 705 | 705 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
| 706 | 706 |
} |
| 707 | 707 |
} |
| 708 | 708 |
|
| 709 | 709 |
/// @} |
| 710 | 710 |
|
| 711 | 711 |
private: |
| 712 | 712 |
|
| 713 | 713 |
// Initialize the algorithm |
| 714 | 714 |
ProblemType init() {
|
| 715 |
if (_res_node_num |
|
| 715 |
if (_res_node_num <= 1) return INFEASIBLE; |
|
| 716 | 716 |
|
| 717 | 717 |
// Check the sum of supply values |
| 718 | 718 |
_sum_supply = 0; |
| 719 | 719 |
for (int i = 0; i != _root; ++i) {
|
| 720 | 720 |
_sum_supply += _supply[i]; |
| 721 | 721 |
} |
| 722 | 722 |
if (_sum_supply > 0) return INFEASIBLE; |
| 723 | 723 |
|
| 724 | 724 |
|
| 725 | 725 |
// Initialize vectors |
| 726 | 726 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 727 | 727 |
_pi[i] = 0; |
| 728 | 728 |
_excess[i] = _supply[i]; |
| 729 | 729 |
} |
| 730 | 730 |
|
| 731 | 731 |
// Remove infinite upper bounds and check negative arcs |
| 732 | 732 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 733 | 733 |
int last_out; |
| 734 | 734 |
if (_have_lower) {
|
| 735 | 735 |
for (int i = 0; i != _root; ++i) {
|
| 736 | 736 |
last_out = _first_out[i+1]; |
| 737 | 737 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 738 | 738 |
if (_forward[j]) {
|
| 739 | 739 |
Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; |
| 740 | 740 |
if (c >= MAX) return UNBOUNDED; |
| 741 | 741 |
_excess[i] -= c; |
| 742 | 742 |
_excess[_target[j]] += c; |
| 743 | 743 |
} |
| 744 | 744 |
} |
| 745 | 745 |
} |
| 746 | 746 |
} else {
|
| 747 | 747 |
for (int i = 0; i != _root; ++i) {
|
| 748 | 748 |
last_out = _first_out[i+1]; |
| 749 | 749 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 750 | 750 |
if (_forward[j] && _scost[j] < 0) {
|
| 751 | 751 |
Value c = _upper[j]; |
| 752 | 752 |
if (c >= MAX) return UNBOUNDED; |
| 753 | 753 |
_excess[i] -= c; |
| 754 | 754 |
_excess[_target[j]] += c; |
| 755 | 755 |
} |
| 756 | 756 |
} |
| 757 | 757 |
} |
| 758 | 758 |
} |
| 759 | 759 |
Value ex, max_cap = 0; |
| 760 | 760 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 761 | 761 |
ex = _excess[i]; |
| 762 | 762 |
_excess[i] = 0; |
| 763 | 763 |
if (ex < 0) max_cap -= ex; |
| 764 | 764 |
} |
| 765 | 765 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 766 | 766 |
if (_upper[j] >= MAX) _upper[j] = max_cap; |
| 767 | 767 |
} |
| 768 | 768 |
|
| 769 | 769 |
// Initialize the large cost vector and the epsilon parameter |
| 770 | 770 |
_epsilon = 0; |
| 771 | 771 |
LargeCost lc; |
| 772 | 772 |
for (int i = 0; i != _root; ++i) {
|
| 773 | 773 |
last_out = _first_out[i+1]; |
| 774 | 774 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 775 | 775 |
lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha; |
| 776 | 776 |
_cost[j] = lc; |
| 777 | 777 |
if (lc > _epsilon) _epsilon = lc; |
| 778 | 778 |
} |
| 779 | 779 |
} |
| 780 | 780 |
_epsilon /= _alpha; |
| 781 | 781 |
|
| 782 | 782 |
// Initialize maps for Circulation and remove non-zero lower bounds |
| 783 | 783 |
ConstMap<Arc, Value> low(0); |
| 784 | 784 |
typedef typename Digraph::template ArcMap<Value> ValueArcMap; |
| 785 | 785 |
typedef typename Digraph::template NodeMap<Value> ValueNodeMap; |
| 786 | 786 |
ValueArcMap cap(_graph), flow(_graph); |
| 787 | 787 |
ValueNodeMap sup(_graph); |
| 788 | 788 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 789 | 789 |
sup[n] = _supply[_node_id[n]]; |
| 790 | 790 |
} |
| 791 | 791 |
if (_have_lower) {
|
| 792 | 792 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 793 | 793 |
int j = _arc_idf[a]; |
| 794 | 794 |
Value c = _lower[j]; |
| 795 | 795 |
cap[a] = _upper[j] - c; |
| 796 | 796 |
sup[_graph.source(a)] -= c; |
| 797 | 797 |
sup[_graph.target(a)] += c; |
| 798 | 798 |
} |
| 799 | 799 |
} else {
|
| 800 | 800 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 801 | 801 |
cap[a] = _upper[_arc_idf[a]]; |
| 802 | 802 |
} |
| 803 | 803 |
} |
| 804 | 804 |
|
| 805 | 805 |
// Find a feasible flow using Circulation |
| 806 | 806 |
Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap> |
| 807 | 807 |
circ(_graph, low, cap, sup); |
| 808 | 808 |
if (!circ.flowMap(flow).run()) return INFEASIBLE; |
| 809 | 809 |
|
| 810 | 810 |
// Set residual capacities and handle GEQ supply type |
| 811 | 811 |
if (_sum_supply < 0) {
|
| 812 | 812 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 813 | 813 |
Value fa = flow[a]; |
| 814 | 814 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 815 | 815 |
_res_cap[_arc_idb[a]] = fa; |
| 816 | 816 |
sup[_graph.source(a)] -= fa; |
| 817 | 817 |
sup[_graph.target(a)] += fa; |
| 818 | 818 |
} |
| 819 | 819 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 820 | 820 |
_excess[_node_id[n]] = sup[n]; |
| 821 | 821 |
} |
| 822 | 822 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 823 | 823 |
int u = _target[a]; |
| 824 | 824 |
int ra = _reverse[a]; |
| 825 | 825 |
_res_cap[a] = -_sum_supply + 1; |
| 826 | 826 |
_res_cap[ra] = -_excess[u]; |
| 827 | 827 |
_cost[a] = 0; |
| 828 | 828 |
_cost[ra] = 0; |
| 829 | 829 |
_excess[u] = 0; |
| 830 | 830 |
} |
| 831 | 831 |
} else {
|
| 832 | 832 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 833 | 833 |
Value fa = flow[a]; |
| 834 | 834 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 835 | 835 |
_res_cap[_arc_idb[a]] = fa; |
| 836 | 836 |
} |
| 837 | 837 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 838 | 838 |
int ra = _reverse[a]; |
| 839 | 839 |
_res_cap[a] = 1; |
| 840 | 840 |
_res_cap[ra] = 0; |
| 841 | 841 |
_cost[a] = 0; |
| 842 | 842 |
_cost[ra] = 0; |
| 843 | 843 |
} |
| 844 | 844 |
} |
| 845 | 845 |
|
| 846 | 846 |
return OPTIMAL; |
| 847 | 847 |
} |
| 848 | 848 |
|
| 849 | 849 |
// Execute the algorithm and transform the results |
| 850 | 850 |
void start(Method method) {
|
| 851 | 851 |
// Maximum path length for partial augment |
| 852 | 852 |
const int MAX_PATH_LENGTH = 4; |
| 853 | 853 |
|
| 854 | 854 |
// Execute the algorithm |
| 855 | 855 |
switch (method) {
|
| 856 | 856 |
case PUSH: |
| 857 | 857 |
startPush(); |
| 858 | 858 |
break; |
| 859 | 859 |
case AUGMENT: |
| 860 | 860 |
startAugment(); |
| 861 | 861 |
break; |
| 862 | 862 |
case PARTIAL_AUGMENT: |
| 863 | 863 |
startAugment(MAX_PATH_LENGTH); |
| 864 | 864 |
break; |
| 865 | 865 |
} |
| 866 | 866 |
|
| 867 | 867 |
// Compute node potentials for the original costs |
| 868 | 868 |
_arc_vec.clear(); |
| 869 | 869 |
_cost_vec.clear(); |
| 870 | 870 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 871 | 871 |
if (_res_cap[j] > 0) {
|
| 872 | 872 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 873 | 873 |
_cost_vec.push_back(_scost[j]); |
| 874 | 874 |
} |
| 875 | 875 |
} |
| 876 | 876 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 877 | 877 |
|
| 878 | 878 |
typename BellmanFord<StaticDigraph, LargeCostArcMap> |
| 879 | 879 |
::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map); |
| 880 | 880 |
bf.distMap(_pi_map); |
| 881 | 881 |
bf.init(0); |
| 882 | 882 |
bf.start(); |
| 883 | 883 |
|
| 884 | 884 |
// Handle non-zero lower bounds |
| 885 | 885 |
if (_have_lower) {
|
| 886 | 886 |
int limit = _first_out[_root]; |
| 887 | 887 |
for (int j = 0; j != limit; ++j) {
|
| 888 | 888 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 889 | 889 |
} |
| 890 | 890 |
} |
| 891 | 891 |
} |
| 892 | 892 |
|
| 893 | 893 |
/// Execute the algorithm performing augment and relabel operations |
| 894 | 894 |
void startAugment(int max_length = std::numeric_limits<int>::max()) {
|
| 895 | 895 |
// Paramters for heuristics |
| 896 | 896 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000; |
| 897 | 897 |
const int BF_HEURISTIC_BOUND_FACTOR = 3; |
| 898 | 898 |
|
| 899 | 899 |
// Perform cost scaling phases |
| 900 | 900 |
IntVector pred_arc(_res_node_num); |
| 901 | 901 |
std::vector<int> path_nodes; |
| 902 | 902 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
| 903 | 903 |
1 : _epsilon / _alpha ) |
| 904 | 904 |
{
|
| 905 | 905 |
// "Early Termination" heuristic: use Bellman-Ford algorithm |
| 906 | 906 |
// to check if the current flow is optimal |
| 907 | 907 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
|
| 908 | 908 |
_arc_vec.clear(); |
| 909 | 909 |
_cost_vec.clear(); |
| 910 | 910 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 911 | 911 |
if (_res_cap[j] > 0) {
|
| 912 | 912 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 913 | 913 |
_cost_vec.push_back(_cost[j] + 1); |
| 914 | 914 |
} |
| 915 | 915 |
} |
| 916 | 916 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 917 | 917 |
|
| 918 | 918 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
| 919 | 919 |
bf.init(0); |
| 920 | 920 |
bool done = false; |
| 921 | 921 |
int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num)); |
| 922 | 922 |
for (int i = 0; i < K && !done; ++i) |
| 923 | 923 |
done = bf.processNextWeakRound(); |
| 924 | 924 |
if (done) break; |
| 925 | 925 |
} |
| 926 | 926 |
|
| 927 | 927 |
// Saturate arcs not satisfying the optimality condition |
| 928 | 928 |
for (int a = 0; a != _res_arc_num; ++a) {
|
| 929 | 929 |
if (_res_cap[a] > 0 && |
| 930 | 930 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
|
| 931 | 931 |
Value delta = _res_cap[a]; |
| 932 | 932 |
_excess[_source[a]] -= delta; |
| 933 | 933 |
_excess[_target[a]] += delta; |
| 934 | 934 |
_res_cap[a] = 0; |
| 935 | 935 |
_res_cap[_reverse[a]] += delta; |
| 936 | 936 |
} |
| 937 | 937 |
} |
| 938 | 938 |
|
| 939 | 939 |
// Find active nodes (i.e. nodes with positive excess) |
| 940 | 940 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 941 | 941 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
| 942 | 942 |
} |
| 943 | 943 |
|
| 944 | 944 |
// Initialize the next arcs |
| 945 | 945 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 946 | 946 |
_next_out[u] = _first_out[u]; |
| 947 | 947 |
} |
| 948 | 948 |
|
| 949 | 949 |
// Perform partial augment and relabel operations |
| 950 | 950 |
while (true) {
|
| 951 | 951 |
// Select an active node (FIFO selection) |
| 952 | 952 |
while (_active_nodes.size() > 0 && |
| 953 | 953 |
_excess[_active_nodes.front()] <= 0) {
|
| 954 | 954 |
_active_nodes.pop_front(); |
| 955 | 955 |
} |
| 956 | 956 |
if (_active_nodes.size() == 0) break; |
| 957 | 957 |
int start = _active_nodes.front(); |
| 958 | 958 |
path_nodes.clear(); |
| 959 | 959 |
path_nodes.push_back(start); |
| 960 | 960 |
|
| 961 | 961 |
// Find an augmenting path from the start node |
| 962 | 962 |
int tip = start; |
| 963 | 963 |
while (_excess[tip] >= 0 && |
| 964 | 964 |
int(path_nodes.size()) <= max_length) {
|
| 965 | 965 |
int u; |
| 966 | 966 |
LargeCost min_red_cost, rc; |
| 967 | 967 |
int last_out = _sum_supply < 0 ? |
| 968 | 968 |
_first_out[tip+1] : _first_out[tip+1] - 1; |
| 969 | 969 |
for (int a = _next_out[tip]; a != last_out; ++a) {
|
| 970 | 970 |
if (_res_cap[a] > 0 && |
| 971 | 971 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
|
| 972 | 972 |
u = _target[a]; |
| 973 | 973 |
pred_arc[u] = a; |
| 974 | 974 |
_next_out[tip] = a; |
| 975 | 975 |
tip = u; |
| 976 | 976 |
path_nodes.push_back(tip); |
| 977 | 977 |
goto next_step; |
| 978 | 978 |
} |
| 979 | 979 |
} |
| 980 | 980 |
|
| 981 | 981 |
// Relabel tip node |
| 982 | 982 |
min_red_cost = std::numeric_limits<LargeCost>::max() / 2; |
| 983 | 983 |
for (int a = _first_out[tip]; a != last_out; ++a) {
|
| 984 | 984 |
rc = _cost[a] + _pi[_source[a]] - _pi[_target[a]]; |
| 985 | 985 |
if (_res_cap[a] > 0 && rc < min_red_cost) {
|
| 986 | 986 |
min_red_cost = rc; |
| 987 | 987 |
} |
| 988 | 988 |
} |
| 989 | 989 |
_pi[tip] -= min_red_cost + _epsilon; |
| 990 | 990 |
|
| 991 | 991 |
// Reset the next arc of tip |
| 992 | 992 |
_next_out[tip] = _first_out[tip]; |
| 993 | 993 |
|
| 994 | 994 |
// Step back |
| 995 | 995 |
if (tip != start) {
|
| 996 | 996 |
path_nodes.pop_back(); |
| 997 | 997 |
tip = path_nodes.back(); |
| 998 | 998 |
} |
| 999 | 999 |
|
| 1000 | 1000 |
next_step: ; |
| 1001 | 1001 |
} |
| 1002 | 1002 |
|
| 1003 | 1003 |
// Augment along the found path (as much flow as possible) |
| 1004 | 1004 |
Value delta; |
| 1005 | 1005 |
int u, v = path_nodes.front(), pa; |
| 1006 | 1006 |
for (int i = 1; i < int(path_nodes.size()); ++i) {
|
| 1007 | 1007 |
u = v; |
| 1008 | 1008 |
v = path_nodes[i]; |
| 1009 | 1009 |
pa = pred_arc[v]; |
| 1010 | 1010 |
delta = std::min(_res_cap[pa], _excess[u]); |
| 1011 | 1011 |
_res_cap[pa] -= delta; |
| 1012 | 1012 |
_res_cap[_reverse[pa]] += delta; |
| 1013 | 1013 |
_excess[u] -= delta; |
| 1014 | 1014 |
_excess[v] += delta; |
| 1015 | 1015 |
if (_excess[v] > 0 && _excess[v] <= delta) |
| 1016 | 1016 |
_active_nodes.push_back(v); |
| 1017 | 1017 |
} |
| 1018 | 1018 |
} |
| 1019 | 1019 |
} |
| 1020 | 1020 |
} |
| 1021 | 1021 |
|
| 1022 | 1022 |
/// Execute the algorithm performing push and relabel operations |
| 1023 | 1023 |
void startPush() {
|
| 1024 | 1024 |
// Paramters for heuristics |
| 1025 | 1025 |
const int BF_HEURISTIC_EPSILON_BOUND = 1000; |
| 1026 | 1026 |
const int BF_HEURISTIC_BOUND_FACTOR = 3; |
| 1027 | 1027 |
|
| 1028 | 1028 |
// Perform cost scaling phases |
| 1029 | 1029 |
BoolVector hyper(_res_node_num, false); |
| 1030 | 1030 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
| 1031 | 1031 |
1 : _epsilon / _alpha ) |
| 1032 | 1032 |
{
|
| 1033 | 1033 |
// "Early Termination" heuristic: use Bellman-Ford algorithm |
| 1034 | 1034 |
// to check if the current flow is optimal |
| 1035 | 1035 |
if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
|
| 1036 | 1036 |
_arc_vec.clear(); |
| 1037 | 1037 |
_cost_vec.clear(); |
| 1038 | 1038 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 1039 | 1039 |
if (_res_cap[j] > 0) {
|
| 1040 | 1040 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 1041 | 1041 |
_cost_vec.push_back(_cost[j] + 1); |
| 1042 | 1042 |
} |
| 1043 | 1043 |
} |
| 1044 | 1044 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 1045 | 1045 |
|
| 1046 | 1046 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
| 1047 | 1047 |
bf.init(0); |
| 1048 | 1048 |
bool done = false; |
| 1049 | 1049 |
int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num)); |
| 1050 | 1050 |
for (int i = 0; i < K && !done; ++i) |
| 1051 | 1051 |
done = bf.processNextWeakRound(); |
| 1052 | 1052 |
if (done) break; |
| 1053 | 1053 |
} |
| 1054 | 1054 |
|
| 1055 | 1055 |
// Saturate arcs not satisfying the optimality condition |
| 1056 | 1056 |
for (int a = 0; a != _res_arc_num; ++a) {
|
| 1057 | 1057 |
if (_res_cap[a] > 0 && |
| 1058 | 1058 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
|
| 1059 | 1059 |
Value delta = _res_cap[a]; |
| 1060 | 1060 |
_excess[_source[a]] -= delta; |
| 1061 | 1061 |
_excess[_target[a]] += delta; |
| 1062 | 1062 |
_res_cap[a] = 0; |
| 1063 | 1063 |
_res_cap[_reverse[a]] += delta; |
| 1064 | 1064 |
} |
| 1065 | 1065 |
} |
| 1066 | 1066 |
|
| 1067 | 1067 |
// Find active nodes (i.e. nodes with positive excess) |
| 1068 | 1068 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 1069 | 1069 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
| 1070 | 1070 |
} |
| 1071 | 1071 |
|
| 1072 | 1072 |
// Initialize the next arcs |
| 1073 | 1073 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 1074 | 1074 |
_next_out[u] = _first_out[u]; |
| 1075 | 1075 |
} |
| 1076 | 1076 |
|
| 1077 | 1077 |
// Perform push and relabel operations |
| 1078 | 1078 |
while (_active_nodes.size() > 0) {
|
| 1079 | 1079 |
LargeCost min_red_cost, rc; |
| 1080 | 1080 |
Value delta; |
| 1081 | 1081 |
int n, t, a, last_out = _res_arc_num; |
| 1082 | 1082 |
|
| 1083 | 1083 |
// Select an active node (FIFO selection) |
| 1084 | 1084 |
next_node: |
| 1085 | 1085 |
n = _active_nodes.front(); |
| 1086 | 1086 |
last_out = _sum_supply < 0 ? |
| 1087 | 1087 |
_first_out[n+1] : _first_out[n+1] - 1; |
| 1088 | 1088 |
|
| 1089 | 1089 |
// Perform push operations if there are admissible arcs |
| 1090 | 1090 |
if (_excess[n] > 0) {
|
| 1091 | 1091 |
for (a = _next_out[n]; a != last_out; ++a) {
|
| 1092 | 1092 |
if (_res_cap[a] > 0 && |
| 1093 | 1093 |
_cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
|
| 1094 | 1094 |
delta = std::min(_res_cap[a], _excess[n]); |
| 1095 | 1095 |
t = _target[a]; |
| 1096 | 1096 |
|
| 1097 | 1097 |
// Push-look-ahead heuristic |
| 1098 | 1098 |
Value ahead = -_excess[t]; |
| 1099 | 1099 |
int last_out_t = _sum_supply < 0 ? |
| 1100 | 1100 |
_first_out[t+1] : _first_out[t+1] - 1; |
| 1101 | 1101 |
for (int ta = _next_out[t]; ta != last_out_t; ++ta) {
|
| 1102 | 1102 |
if (_res_cap[ta] > 0 && |
| 1103 | 1103 |
_cost[ta] + _pi[_source[ta]] - _pi[_target[ta]] < 0) |
| 1104 | 1104 |
ahead += _res_cap[ta]; |
| 1105 | 1105 |
if (ahead >= delta) break; |
| 1106 | 1106 |
} |
| 1107 | 1107 |
if (ahead < 0) ahead = 0; |
| 1108 | 1108 |
|
| 1109 | 1109 |
// Push flow along the arc |
| 1110 | 1110 |
if (ahead < delta) {
|
| 1111 | 1111 |
_res_cap[a] -= ahead; |
| 1112 | 1112 |
_res_cap[_reverse[a]] += ahead; |
| 1113 | 1113 |
_excess[n] -= ahead; |
| 1114 | 1114 |
_excess[t] += ahead; |
| 1115 | 1115 |
_active_nodes.push_front(t); |
| 1116 | 1116 |
hyper[t] = true; |
| 1117 | 1117 |
_next_out[n] = a; |
| 1118 | 1118 |
goto next_node; |
| 1119 | 1119 |
} else {
|
| 1120 | 1120 |
_res_cap[a] -= delta; |
| 1121 | 1121 |
_res_cap[_reverse[a]] += delta; |
| 1122 | 1122 |
_excess[n] -= delta; |
| 1123 | 1123 |
_excess[t] += delta; |
| 1124 | 1124 |
if (_excess[t] > 0 && _excess[t] <= delta) |
| 1125 | 1125 |
_active_nodes.push_back(t); |
| 1126 | 1126 |
} |
| 1127 | 1127 |
|
| 1128 | 1128 |
if (_excess[n] == 0) {
|
| 1129 | 1129 |
_next_out[n] = a; |
| 1130 | 1130 |
goto remove_nodes; |
| 1131 | 1131 |
} |
| 1132 | 1132 |
} |
| 1133 | 1133 |
} |
| 1134 | 1134 |
_next_out[n] = a; |
| 1135 | 1135 |
} |
| 1136 | 1136 |
|
| 1137 | 1137 |
// Relabel the node if it is still active (or hyper) |
| 1138 | 1138 |
if (_excess[n] > 0 || hyper[n]) {
|
| 1139 | 1139 |
min_red_cost = std::numeric_limits<LargeCost>::max() / 2; |
| 1140 | 1140 |
for (int a = _first_out[n]; a != last_out; ++a) {
|
| 1141 | 1141 |
rc = _cost[a] + _pi[_source[a]] - _pi[_target[a]]; |
| 1142 | 1142 |
if (_res_cap[a] > 0 && rc < min_red_cost) {
|
| 1143 | 1143 |
min_red_cost = rc; |
| 1144 | 1144 |
} |
| 1145 | 1145 |
} |
| 1146 | 1146 |
_pi[n] -= min_red_cost + _epsilon; |
| 1147 | 1147 |
hyper[n] = false; |
| 1148 | 1148 |
|
| 1149 | 1149 |
// Reset the next arc |
| 1150 | 1150 |
_next_out[n] = _first_out[n]; |
| 1151 | 1151 |
} |
| 1152 | 1152 |
|
| 1153 | 1153 |
// Remove nodes that are not active nor hyper |
| 1154 | 1154 |
remove_nodes: |
| 1155 | 1155 |
while ( _active_nodes.size() > 0 && |
| 1156 | 1156 |
_excess[_active_nodes.front()] <= 0 && |
| 1157 | 1157 |
!hyper[_active_nodes.front()] ) {
|
| 1158 | 1158 |
_active_nodes.pop_front(); |
| 1159 | 1159 |
} |
| 1160 | 1160 |
} |
| 1161 | 1161 |
} |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
}; //class CostScaling |
| 1165 | 1165 |
|
| 1166 | 1166 |
///@} |
| 1167 | 1167 |
|
| 1168 | 1168 |
} //namespace lemon |
| 1169 | 1169 |
|
| 1170 | 1170 |
#endif //LEMON_COST_SCALING_H |
0 comments (0 inline)