| 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- | 
|---|
| 2 | * | 
|---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (C) 2003-2009 | 
|---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport | 
|---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). | 
|---|
| 8 | * | 
|---|
| 9 | * Permission to use, modify and distribute this software is granted | 
|---|
| 10 | * provided that this copyright notice appears in all copies. For | 
|---|
| 11 | * precise terms see the accompanying LICENSE file. | 
|---|
| 12 | * | 
|---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, | 
|---|
| 14 | * express or implied, and with no claim as to its suitability for any | 
|---|
| 15 | * purpose. | 
|---|
| 16 | * | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 | #ifndef LEMON_NETWORK_SIMPLEX_H | 
|---|
| 20 | #define LEMON_NETWORK_SIMPLEX_H | 
|---|
| 21 |  | 
|---|
| 22 | /// \ingroup min_cost_flow | 
|---|
| 23 | /// | 
|---|
| 24 | /// \file | 
|---|
| 25 | /// \brief Network Simplex algorithm for finding a minimum cost flow. | 
|---|
| 26 |  | 
|---|
| 27 | #include <vector> | 
|---|
| 28 | #include <limits> | 
|---|
| 29 | #include <algorithm> | 
|---|
| 30 |  | 
|---|
| 31 | #include <lemon/core.h> | 
|---|
| 32 | #include <lemon/math.h> | 
|---|
| 33 |  | 
|---|
| 34 | namespace lemon { | 
|---|
| 35 |  | 
|---|
| 36 | /// \addtogroup min_cost_flow | 
|---|
| 37 | /// @{ | 
|---|
| 38 |  | 
|---|
| 39 | /// \brief Implementation of the primal Network Simplex algorithm | 
|---|
| 40 | /// for finding a \ref min_cost_flow "minimum cost flow". | 
|---|
| 41 | /// | 
|---|
| 42 | /// \ref NetworkSimplex implements the primal Network Simplex algorithm | 
|---|
| 43 | /// for finding a \ref min_cost_flow "minimum cost flow". | 
|---|
| 44 | /// This algorithm is a specialized version of the linear programming | 
|---|
| 45 | /// simplex method directly for the minimum cost flow problem. | 
|---|
| 46 | /// It is one of the most efficient solution methods. | 
|---|
| 47 | /// | 
|---|
| 48 | /// In general this class is the fastest implementation available | 
|---|
| 49 | /// in LEMON for the minimum cost flow problem. | 
|---|
| 50 | /// Moreover it supports both directions of the supply/demand inequality | 
|---|
| 51 | /// constraints. For more information see \ref SupplyType. | 
|---|
| 52 | /// | 
|---|
| 53 | /// Most of the parameters of the problem (except for the digraph) | 
|---|
| 54 | /// can be given using separate functions, and the algorithm can be | 
|---|
| 55 | /// executed using the \ref run() function. If some parameters are not | 
|---|
| 56 | /// specified, then default values will be used. | 
|---|
| 57 | /// | 
|---|
| 58 | /// \tparam GR The digraph type the algorithm runs on. | 
|---|
| 59 | /// \tparam V The value type used for flow amounts, capacity bounds | 
|---|
| 60 | /// and supply values in the algorithm. By default it is \c int. | 
|---|
| 61 | /// \tparam C The value type used for costs and potentials in the | 
|---|
| 62 | /// algorithm. By default it is the same as \c V. | 
|---|
| 63 | /// | 
|---|
| 64 | /// \warning Both value types must be signed and all input data must | 
|---|
| 65 | /// be integer. | 
|---|
| 66 | /// | 
|---|
| 67 | /// \note %NetworkSimplex provides five different pivot rule | 
|---|
| 68 | /// implementations, from which the most efficient one is used | 
|---|
| 69 | /// by default. For more information see \ref PivotRule. | 
|---|
| 70 | template <typename GR, typename V = int, typename C = V> | 
|---|
| 71 | class NetworkSimplex | 
|---|
| 72 | { | 
|---|
| 73 | public: | 
|---|
| 74 |  | 
|---|
| 75 | /// The type of the flow amounts, capacity bounds and supply values | 
|---|
| 76 | typedef V Value; | 
|---|
| 77 | /// The type of the arc costs | 
|---|
| 78 | typedef C Cost; | 
|---|
| 79 |  | 
|---|
| 80 | public: | 
|---|
| 81 |  | 
|---|
| 82 | /// \brief Problem type constants for the \c run() function. | 
|---|
| 83 | /// | 
|---|
| 84 | /// Enum type containing the problem type constants that can be | 
|---|
| 85 | /// returned by the \ref run() function of the algorithm. | 
|---|
| 86 | enum ProblemType { | 
|---|
| 87 | /// The problem has no feasible solution (flow). | 
|---|
| 88 | INFEASIBLE, | 
|---|
| 89 | /// The problem has optimal solution (i.e. it is feasible and | 
|---|
| 90 | /// bounded), and the algorithm has found optimal flow and node | 
|---|
| 91 | /// potentials (primal and dual solutions). | 
|---|
| 92 | OPTIMAL, | 
|---|
| 93 | /// The objective function of the problem is unbounded, i.e. | 
|---|
| 94 | /// there is a directed cycle having negative total cost and | 
|---|
| 95 | /// infinite upper bound. | 
|---|
| 96 | UNBOUNDED | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | /// \brief Constants for selecting the type of the supply constraints. | 
|---|
| 100 | /// | 
|---|
| 101 | /// Enum type containing constants for selecting the supply type, | 
|---|
| 102 | /// i.e. the direction of the inequalities in the supply/demand | 
|---|
| 103 | /// constraints of the \ref min_cost_flow "minimum cost flow problem". | 
|---|
| 104 | /// | 
|---|
| 105 | /// The default supply type is \c GEQ, since this form is supported | 
|---|
| 106 | /// by other minimum cost flow algorithms and the \ref Circulation | 
|---|
| 107 | /// algorithm, as well. | 
|---|
| 108 | /// The \c LEQ problem type can be selected using the \ref supplyType() | 
|---|
| 109 | /// function. | 
|---|
| 110 | /// | 
|---|
| 111 | /// Note that the equality form is a special case of both supply types. | 
|---|
| 112 | enum SupplyType { | 
|---|
| 113 |  | 
|---|
| 114 | /// This option means that there are <em>"greater or equal"</em> | 
|---|
| 115 | /// supply/demand constraints in the definition, i.e. the exact | 
|---|
| 116 | /// formulation of the problem is the following. | 
|---|
| 117 | /** | 
|---|
| 118 | \f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] | 
|---|
| 119 | \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq | 
|---|
| 120 | sup(u) \quad \forall u\in V \f] | 
|---|
| 121 | \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] | 
|---|
| 122 | */ | 
|---|
| 123 | /// It means that the total demand must be greater or equal to the | 
|---|
| 124 | /// total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or | 
|---|
| 125 | /// negative) and all the supplies have to be carried out from | 
|---|
| 126 | /// the supply nodes, but there could be demands that are not | 
|---|
| 127 | /// satisfied. | 
|---|
| 128 | GEQ, | 
|---|
| 129 | /// It is just an alias for the \c GEQ option. | 
|---|
| 130 | CARRY_SUPPLIES = GEQ, | 
|---|
| 131 |  | 
|---|
| 132 | /// This option means that there are <em>"less or equal"</em> | 
|---|
| 133 | /// supply/demand constraints in the definition, i.e. the exact | 
|---|
| 134 | /// formulation of the problem is the following. | 
|---|
| 135 | /** | 
|---|
| 136 | \f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] | 
|---|
| 137 | \f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq | 
|---|
| 138 | sup(u) \quad \forall u\in V \f] | 
|---|
| 139 | \f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] | 
|---|
| 140 | */ | 
|---|
| 141 | /// It means that the total demand must be less or equal to the | 
|---|
| 142 | /// total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or | 
|---|
| 143 | /// positive) and all the demands have to be satisfied, but there | 
|---|
| 144 | /// could be supplies that are not carried out from the supply | 
|---|
| 145 | /// nodes. | 
|---|
| 146 | LEQ, | 
|---|
| 147 | /// It is just an alias for the \c LEQ option. | 
|---|
| 148 | SATISFY_DEMANDS = LEQ | 
|---|
| 149 | }; | 
|---|
| 150 |  | 
|---|
| 151 | /// \brief Constants for selecting the pivot rule. | 
|---|
| 152 | /// | 
|---|
| 153 | /// Enum type containing constants for selecting the pivot rule for | 
|---|
| 154 | /// the \ref run() function. | 
|---|
| 155 | /// | 
|---|
| 156 | /// \ref NetworkSimplex provides five different pivot rule | 
|---|
| 157 | /// implementations that significantly affect the running time | 
|---|
| 158 | /// of the algorithm. | 
|---|
| 159 | /// By default \ref BLOCK_SEARCH "Block Search" is used, which | 
|---|
| 160 | /// proved to be the most efficient and the most robust on various | 
|---|
| 161 | /// test inputs according to our benchmark tests. | 
|---|
| 162 | /// However another pivot rule can be selected using the \ref run() | 
|---|
| 163 | /// function with the proper parameter. | 
|---|
| 164 | enum PivotRule { | 
|---|
| 165 |  | 
|---|
| 166 | /// The First Eligible pivot rule. | 
|---|
| 167 | /// The next eligible arc is selected in a wraparound fashion | 
|---|
| 168 | /// in every iteration. | 
|---|
| 169 | FIRST_ELIGIBLE, | 
|---|
| 170 |  | 
|---|
| 171 | /// The Best Eligible pivot rule. | 
|---|
| 172 | /// The best eligible arc is selected in every iteration. | 
|---|
| 173 | BEST_ELIGIBLE, | 
|---|
| 174 |  | 
|---|
| 175 | /// The Block Search pivot rule. | 
|---|
| 176 | /// A specified number of arcs are examined in every iteration | 
|---|
| 177 | /// in a wraparound fashion and the best eligible arc is selected | 
|---|
| 178 | /// from this block. | 
|---|
| 179 | BLOCK_SEARCH, | 
|---|
| 180 |  | 
|---|
| 181 | /// The Candidate List pivot rule. | 
|---|
| 182 | /// In a major iteration a candidate list is built from eligible arcs | 
|---|
| 183 | /// in a wraparound fashion and in the following minor iterations | 
|---|
| 184 | /// the best eligible arc is selected from this list. | 
|---|
| 185 | CANDIDATE_LIST, | 
|---|
| 186 |  | 
|---|
| 187 | /// The Altering Candidate List pivot rule. | 
|---|
| 188 | /// It is a modified version of the Candidate List method. | 
|---|
| 189 | /// It keeps only the several best eligible arcs from the former | 
|---|
| 190 | /// candidate list and extends this list in every iteration. | 
|---|
| 191 | ALTERING_LIST | 
|---|
| 192 | }; | 
|---|
| 193 |  | 
|---|
| 194 | private: | 
|---|
| 195 |  | 
|---|
| 196 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); | 
|---|
| 197 |  | 
|---|
| 198 | typedef std::vector<Arc> ArcVector; | 
|---|
| 199 | typedef std::vector<Node> NodeVector; | 
|---|
| 200 | typedef std::vector<int> IntVector; | 
|---|
| 201 | typedef std::vector<bool> BoolVector; | 
|---|
| 202 | typedef std::vector<Value> ValueVector; | 
|---|
| 203 | typedef std::vector<Cost> CostVector; | 
|---|
| 204 |  | 
|---|
| 205 | // State constants for arcs | 
|---|
| 206 | enum ArcStateEnum { | 
|---|
| 207 | STATE_UPPER = -1, | 
|---|
| 208 | STATE_TREE  =  0, | 
|---|
| 209 | STATE_LOWER =  1 | 
|---|
| 210 | }; | 
|---|
| 211 |  | 
|---|
| 212 | private: | 
|---|
| 213 |  | 
|---|
| 214 | // Data related to the underlying digraph | 
|---|
| 215 | const GR &_graph; | 
|---|
| 216 | int _node_num; | 
|---|
| 217 | int _arc_num; | 
|---|
| 218 |  | 
|---|
| 219 | // Parameters of the problem | 
|---|
| 220 | bool _have_lower; | 
|---|
| 221 | SupplyType _stype; | 
|---|
| 222 | Value _sum_supply; | 
|---|
| 223 |  | 
|---|
| 224 | // Data structures for storing the digraph | 
|---|
| 225 | IntNodeMap _node_id; | 
|---|
| 226 | IntArcMap _arc_id; | 
|---|
| 227 | IntVector _source; | 
|---|
| 228 | IntVector _target; | 
|---|
| 229 |  | 
|---|
| 230 | // Node and arc data | 
|---|
| 231 | ValueVector _lower; | 
|---|
| 232 | ValueVector _upper; | 
|---|
| 233 | ValueVector _cap; | 
|---|
| 234 | CostVector _cost; | 
|---|
| 235 | ValueVector _supply; | 
|---|
| 236 | ValueVector _flow; | 
|---|
| 237 | CostVector _pi; | 
|---|
| 238 |  | 
|---|
| 239 | // Data for storing the spanning tree structure | 
|---|
| 240 | IntVector _parent; | 
|---|
| 241 | IntVector _pred; | 
|---|
| 242 | IntVector _thread; | 
|---|
| 243 | IntVector _rev_thread; | 
|---|
| 244 | IntVector _succ_num; | 
|---|
| 245 | IntVector _last_succ; | 
|---|
| 246 | IntVector _dirty_revs; | 
|---|
| 247 | BoolVector _forward; | 
|---|
| 248 | IntVector _state; | 
|---|
| 249 | int _root; | 
|---|
| 250 |  | 
|---|
| 251 | // Temporary data used in the current pivot iteration | 
|---|
| 252 | int in_arc, join, u_in, v_in, u_out, v_out; | 
|---|
| 253 | int first, second, right, last; | 
|---|
| 254 | int stem, par_stem, new_stem; | 
|---|
| 255 | Value delta; | 
|---|
| 256 |  | 
|---|
| 257 | public: | 
|---|
| 258 |  | 
|---|
| 259 | /// \brief Constant for infinite upper bounds (capacities). | 
|---|
| 260 | /// | 
|---|
| 261 | /// Constant for infinite upper bounds (capacities). | 
|---|
| 262 | /// It is \c std::numeric_limits<Value>::infinity() if available, | 
|---|
| 263 | /// \c std::numeric_limits<Value>::max() otherwise. | 
|---|
| 264 | const Value INF; | 
|---|
| 265 |  | 
|---|
| 266 | private: | 
|---|
| 267 |  | 
|---|
| 268 | // Implementation of the First Eligible pivot rule | 
|---|
| 269 | class FirstEligiblePivotRule | 
|---|
| 270 | { | 
|---|
| 271 | private: | 
|---|
| 272 |  | 
|---|
| 273 | // References to the NetworkSimplex class | 
|---|
| 274 | const IntVector  &_source; | 
|---|
| 275 | const IntVector  &_target; | 
|---|
| 276 | const CostVector &_cost; | 
|---|
| 277 | const IntVector  &_state; | 
|---|
| 278 | const CostVector &_pi; | 
|---|
| 279 | int &_in_arc; | 
|---|
| 280 | int _arc_num; | 
|---|
| 281 |  | 
|---|
| 282 | // Pivot rule data | 
|---|
| 283 | int _next_arc; | 
|---|
| 284 |  | 
|---|
| 285 | public: | 
|---|
| 286 |  | 
|---|
| 287 | // Constructor | 
|---|
| 288 | FirstEligiblePivotRule(NetworkSimplex &ns) : | 
|---|
| 289 | _source(ns._source), _target(ns._target), | 
|---|
| 290 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), | 
|---|
| 291 | _in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0) | 
|---|
| 292 | {} | 
|---|
| 293 |  | 
|---|
| 294 | // Find next entering arc | 
|---|
| 295 | bool findEnteringArc() { | 
|---|
| 296 | Cost c; | 
|---|
| 297 | for (int e = _next_arc; e < _arc_num; ++e) { | 
|---|
| 298 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 299 | if (c < 0) { | 
|---|
| 300 | _in_arc = e; | 
|---|
| 301 | _next_arc = e + 1; | 
|---|
| 302 | return true; | 
|---|
| 303 | } | 
|---|
| 304 | } | 
|---|
| 305 | for (int e = 0; e < _next_arc; ++e) { | 
|---|
| 306 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 307 | if (c < 0) { | 
|---|
| 308 | _in_arc = e; | 
|---|
| 309 | _next_arc = e + 1; | 
|---|
| 310 | return true; | 
|---|
| 311 | } | 
|---|
| 312 | } | 
|---|
| 313 | return false; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | }; //class FirstEligiblePivotRule | 
|---|
| 317 |  | 
|---|
| 318 |  | 
|---|
| 319 | // Implementation of the Best Eligible pivot rule | 
|---|
| 320 | class BestEligiblePivotRule | 
|---|
| 321 | { | 
|---|
| 322 | private: | 
|---|
| 323 |  | 
|---|
| 324 | // References to the NetworkSimplex class | 
|---|
| 325 | const IntVector  &_source; | 
|---|
| 326 | const IntVector  &_target; | 
|---|
| 327 | const CostVector &_cost; | 
|---|
| 328 | const IntVector  &_state; | 
|---|
| 329 | const CostVector &_pi; | 
|---|
| 330 | int &_in_arc; | 
|---|
| 331 | int _arc_num; | 
|---|
| 332 |  | 
|---|
| 333 | public: | 
|---|
| 334 |  | 
|---|
| 335 | // Constructor | 
|---|
| 336 | BestEligiblePivotRule(NetworkSimplex &ns) : | 
|---|
| 337 | _source(ns._source), _target(ns._target), | 
|---|
| 338 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), | 
|---|
| 339 | _in_arc(ns.in_arc), _arc_num(ns._arc_num) | 
|---|
| 340 | {} | 
|---|
| 341 |  | 
|---|
| 342 | // Find next entering arc | 
|---|
| 343 | bool findEnteringArc() { | 
|---|
| 344 | Cost c, min = 0; | 
|---|
| 345 | for (int e = 0; e < _arc_num; ++e) { | 
|---|
| 346 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 347 | if (c < min) { | 
|---|
| 348 | min = c; | 
|---|
| 349 | _in_arc = e; | 
|---|
| 350 | } | 
|---|
| 351 | } | 
|---|
| 352 | return min < 0; | 
|---|
| 353 | } | 
|---|
| 354 |  | 
|---|
| 355 | }; //class BestEligiblePivotRule | 
|---|
| 356 |  | 
|---|
| 357 |  | 
|---|
| 358 | // Implementation of the Block Search pivot rule | 
|---|
| 359 | class BlockSearchPivotRule | 
|---|
| 360 | { | 
|---|
| 361 | private: | 
|---|
| 362 |  | 
|---|
| 363 | // References to the NetworkSimplex class | 
|---|
| 364 | const IntVector  &_source; | 
|---|
| 365 | const IntVector  &_target; | 
|---|
| 366 | const CostVector &_cost; | 
|---|
| 367 | const IntVector  &_state; | 
|---|
| 368 | const CostVector &_pi; | 
|---|
| 369 | int &_in_arc; | 
|---|
| 370 | int _arc_num; | 
|---|
| 371 |  | 
|---|
| 372 | // Pivot rule data | 
|---|
| 373 | int _block_size; | 
|---|
| 374 | int _next_arc; | 
|---|
| 375 |  | 
|---|
| 376 | public: | 
|---|
| 377 |  | 
|---|
| 378 | // Constructor | 
|---|
| 379 | BlockSearchPivotRule(NetworkSimplex &ns) : | 
|---|
| 380 | _source(ns._source), _target(ns._target), | 
|---|
| 381 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), | 
|---|
| 382 | _in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0) | 
|---|
| 383 | { | 
|---|
| 384 | // The main parameters of the pivot rule | 
|---|
| 385 | const double BLOCK_SIZE_FACTOR = 2.0; | 
|---|
| 386 | const int MIN_BLOCK_SIZE = 10; | 
|---|
| 387 |  | 
|---|
| 388 | _block_size = std::max( int(BLOCK_SIZE_FACTOR * | 
|---|
| 389 | std::sqrt(double(_arc_num))), | 
|---|
| 390 | MIN_BLOCK_SIZE ); | 
|---|
| 391 | } | 
|---|
| 392 |  | 
|---|
| 393 | // Find next entering arc | 
|---|
| 394 | bool findEnteringArc() { | 
|---|
| 395 | Cost c, min = 0; | 
|---|
| 396 | int cnt = _block_size; | 
|---|
| 397 | int e, min_arc = _next_arc; | 
|---|
| 398 | for (e = _next_arc; e < _arc_num; ++e) { | 
|---|
| 399 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 400 | if (c < min) { | 
|---|
| 401 | min = c; | 
|---|
| 402 | min_arc = e; | 
|---|
| 403 | } | 
|---|
| 404 | if (--cnt == 0) { | 
|---|
| 405 | if (min < 0) break; | 
|---|
| 406 | cnt = _block_size; | 
|---|
| 407 | } | 
|---|
| 408 | } | 
|---|
| 409 | if (min == 0 || cnt > 0) { | 
|---|
| 410 | for (e = 0; e < _next_arc; ++e) { | 
|---|
| 411 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 412 | if (c < min) { | 
|---|
| 413 | min = c; | 
|---|
| 414 | min_arc = e; | 
|---|
| 415 | } | 
|---|
| 416 | if (--cnt == 0) { | 
|---|
| 417 | if (min < 0) break; | 
|---|
| 418 | cnt = _block_size; | 
|---|
| 419 | } | 
|---|
| 420 | } | 
|---|
| 421 | } | 
|---|
| 422 | if (min >= 0) return false; | 
|---|
| 423 | _in_arc = min_arc; | 
|---|
| 424 | _next_arc = e; | 
|---|
| 425 | return true; | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | }; //class BlockSearchPivotRule | 
|---|
| 429 |  | 
|---|
| 430 |  | 
|---|
| 431 | // Implementation of the Candidate List pivot rule | 
|---|
| 432 | class CandidateListPivotRule | 
|---|
| 433 | { | 
|---|
| 434 | private: | 
|---|
| 435 |  | 
|---|
| 436 | // References to the NetworkSimplex class | 
|---|
| 437 | const IntVector  &_source; | 
|---|
| 438 | const IntVector  &_target; | 
|---|
| 439 | const CostVector &_cost; | 
|---|
| 440 | const IntVector  &_state; | 
|---|
| 441 | const CostVector &_pi; | 
|---|
| 442 | int &_in_arc; | 
|---|
| 443 | int _arc_num; | 
|---|
| 444 |  | 
|---|
| 445 | // Pivot rule data | 
|---|
| 446 | IntVector _candidates; | 
|---|
| 447 | int _list_length, _minor_limit; | 
|---|
| 448 | int _curr_length, _minor_count; | 
|---|
| 449 | int _next_arc; | 
|---|
| 450 |  | 
|---|
| 451 | public: | 
|---|
| 452 |  | 
|---|
| 453 | /// Constructor | 
|---|
| 454 | CandidateListPivotRule(NetworkSimplex &ns) : | 
|---|
| 455 | _source(ns._source), _target(ns._target), | 
|---|
| 456 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), | 
|---|
| 457 | _in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0) | 
|---|
| 458 | { | 
|---|
| 459 | // The main parameters of the pivot rule | 
|---|
| 460 | const double LIST_LENGTH_FACTOR = 1.0; | 
|---|
| 461 | const int MIN_LIST_LENGTH = 10; | 
|---|
| 462 | const double MINOR_LIMIT_FACTOR = 0.1; | 
|---|
| 463 | const int MIN_MINOR_LIMIT = 3; | 
|---|
| 464 |  | 
|---|
| 465 | _list_length = std::max( int(LIST_LENGTH_FACTOR * | 
|---|
| 466 | std::sqrt(double(_arc_num))), | 
|---|
| 467 | MIN_LIST_LENGTH ); | 
|---|
| 468 | _minor_limit = std::max( int(MINOR_LIMIT_FACTOR * _list_length), | 
|---|
| 469 | MIN_MINOR_LIMIT ); | 
|---|
| 470 | _curr_length = _minor_count = 0; | 
|---|
| 471 | _candidates.resize(_list_length); | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | /// Find next entering arc | 
|---|
| 475 | bool findEnteringArc() { | 
|---|
| 476 | Cost min, c; | 
|---|
| 477 | int e, min_arc = _next_arc; | 
|---|
| 478 | if (_curr_length > 0 && _minor_count < _minor_limit) { | 
|---|
| 479 | // Minor iteration: select the best eligible arc from the | 
|---|
| 480 | // current candidate list | 
|---|
| 481 | ++_minor_count; | 
|---|
| 482 | min = 0; | 
|---|
| 483 | for (int i = 0; i < _curr_length; ++i) { | 
|---|
| 484 | e = _candidates[i]; | 
|---|
| 485 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 486 | if (c < min) { | 
|---|
| 487 | min = c; | 
|---|
| 488 | min_arc = e; | 
|---|
| 489 | } | 
|---|
| 490 | if (c >= 0) { | 
|---|
| 491 | _candidates[i--] = _candidates[--_curr_length]; | 
|---|
| 492 | } | 
|---|
| 493 | } | 
|---|
| 494 | if (min < 0) { | 
|---|
| 495 | _in_arc = min_arc; | 
|---|
| 496 | return true; | 
|---|
| 497 | } | 
|---|
| 498 | } | 
|---|
| 499 |  | 
|---|
| 500 | // Major iteration: build a new candidate list | 
|---|
| 501 | min = 0; | 
|---|
| 502 | _curr_length = 0; | 
|---|
| 503 | for (e = _next_arc; e < _arc_num; ++e) { | 
|---|
| 504 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 505 | if (c < 0) { | 
|---|
| 506 | _candidates[_curr_length++] = e; | 
|---|
| 507 | if (c < min) { | 
|---|
| 508 | min = c; | 
|---|
| 509 | min_arc = e; | 
|---|
| 510 | } | 
|---|
| 511 | if (_curr_length == _list_length) break; | 
|---|
| 512 | } | 
|---|
| 513 | } | 
|---|
| 514 | if (_curr_length < _list_length) { | 
|---|
| 515 | for (e = 0; e < _next_arc; ++e) { | 
|---|
| 516 | c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 517 | if (c < 0) { | 
|---|
| 518 | _candidates[_curr_length++] = e; | 
|---|
| 519 | if (c < min) { | 
|---|
| 520 | min = c; | 
|---|
| 521 | min_arc = e; | 
|---|
| 522 | } | 
|---|
| 523 | if (_curr_length == _list_length) break; | 
|---|
| 524 | } | 
|---|
| 525 | } | 
|---|
| 526 | } | 
|---|
| 527 | if (_curr_length == 0) return false; | 
|---|
| 528 | _minor_count = 1; | 
|---|
| 529 | _in_arc = min_arc; | 
|---|
| 530 | _next_arc = e; | 
|---|
| 531 | return true; | 
|---|
| 532 | } | 
|---|
| 533 |  | 
|---|
| 534 | }; //class CandidateListPivotRule | 
|---|
| 535 |  | 
|---|
| 536 |  | 
|---|
| 537 | // Implementation of the Altering Candidate List pivot rule | 
|---|
| 538 | class AlteringListPivotRule | 
|---|
| 539 | { | 
|---|
| 540 | private: | 
|---|
| 541 |  | 
|---|
| 542 | // References to the NetworkSimplex class | 
|---|
| 543 | const IntVector  &_source; | 
|---|
| 544 | const IntVector  &_target; | 
|---|
| 545 | const CostVector &_cost; | 
|---|
| 546 | const IntVector  &_state; | 
|---|
| 547 | const CostVector &_pi; | 
|---|
| 548 | int &_in_arc; | 
|---|
| 549 | int _arc_num; | 
|---|
| 550 |  | 
|---|
| 551 | // Pivot rule data | 
|---|
| 552 | int _block_size, _head_length, _curr_length; | 
|---|
| 553 | int _next_arc; | 
|---|
| 554 | IntVector _candidates; | 
|---|
| 555 | CostVector _cand_cost; | 
|---|
| 556 |  | 
|---|
| 557 | // Functor class to compare arcs during sort of the candidate list | 
|---|
| 558 | class SortFunc | 
|---|
| 559 | { | 
|---|
| 560 | private: | 
|---|
| 561 | const CostVector &_map; | 
|---|
| 562 | public: | 
|---|
| 563 | SortFunc(const CostVector &map) : _map(map) {} | 
|---|
| 564 | bool operator()(int left, int right) { | 
|---|
| 565 | return _map[left] > _map[right]; | 
|---|
| 566 | } | 
|---|
| 567 | }; | 
|---|
| 568 |  | 
|---|
| 569 | SortFunc _sort_func; | 
|---|
| 570 |  | 
|---|
| 571 | public: | 
|---|
| 572 |  | 
|---|
| 573 | // Constructor | 
|---|
| 574 | AlteringListPivotRule(NetworkSimplex &ns) : | 
|---|
| 575 | _source(ns._source), _target(ns._target), | 
|---|
| 576 | _cost(ns._cost), _state(ns._state), _pi(ns._pi), | 
|---|
| 577 | _in_arc(ns.in_arc), _arc_num(ns._arc_num), | 
|---|
| 578 | _next_arc(0), _cand_cost(ns._arc_num), _sort_func(_cand_cost) | 
|---|
| 579 | { | 
|---|
| 580 | // The main parameters of the pivot rule | 
|---|
| 581 | const double BLOCK_SIZE_FACTOR = 1.5; | 
|---|
| 582 | const int MIN_BLOCK_SIZE = 10; | 
|---|
| 583 | const double HEAD_LENGTH_FACTOR = 0.1; | 
|---|
| 584 | const int MIN_HEAD_LENGTH = 3; | 
|---|
| 585 |  | 
|---|
| 586 | _block_size = std::max( int(BLOCK_SIZE_FACTOR * | 
|---|
| 587 | std::sqrt(double(_arc_num))), | 
|---|
| 588 | MIN_BLOCK_SIZE ); | 
|---|
| 589 | _head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size), | 
|---|
| 590 | MIN_HEAD_LENGTH ); | 
|---|
| 591 | _candidates.resize(_head_length + _block_size); | 
|---|
| 592 | _curr_length = 0; | 
|---|
| 593 | } | 
|---|
| 594 |  | 
|---|
| 595 | // Find next entering arc | 
|---|
| 596 | bool findEnteringArc() { | 
|---|
| 597 | // Check the current candidate list | 
|---|
| 598 | int e; | 
|---|
| 599 | for (int i = 0; i < _curr_length; ++i) { | 
|---|
| 600 | e = _candidates[i]; | 
|---|
| 601 | _cand_cost[e] = _state[e] * | 
|---|
| 602 | (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 603 | if (_cand_cost[e] >= 0) { | 
|---|
| 604 | _candidates[i--] = _candidates[--_curr_length]; | 
|---|
| 605 | } | 
|---|
| 606 | } | 
|---|
| 607 |  | 
|---|
| 608 | // Extend the list | 
|---|
| 609 | int cnt = _block_size; | 
|---|
| 610 | int last_arc = 0; | 
|---|
| 611 | int limit = _head_length; | 
|---|
| 612 |  | 
|---|
| 613 | for (int e = _next_arc; e < _arc_num; ++e) { | 
|---|
| 614 | _cand_cost[e] = _state[e] * | 
|---|
| 615 | (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 616 | if (_cand_cost[e] < 0) { | 
|---|
| 617 | _candidates[_curr_length++] = e; | 
|---|
| 618 | last_arc = e; | 
|---|
| 619 | } | 
|---|
| 620 | if (--cnt == 0) { | 
|---|
| 621 | if (_curr_length > limit) break; | 
|---|
| 622 | limit = 0; | 
|---|
| 623 | cnt = _block_size; | 
|---|
| 624 | } | 
|---|
| 625 | } | 
|---|
| 626 | if (_curr_length <= limit) { | 
|---|
| 627 | for (int e = 0; e < _next_arc; ++e) { | 
|---|
| 628 | _cand_cost[e] = _state[e] * | 
|---|
| 629 | (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); | 
|---|
| 630 | if (_cand_cost[e] < 0) { | 
|---|
| 631 | _candidates[_curr_length++] = e; | 
|---|
| 632 | last_arc = e; | 
|---|
| 633 | } | 
|---|
| 634 | if (--cnt == 0) { | 
|---|
| 635 | if (_curr_length > limit) break; | 
|---|
| 636 | limit = 0; | 
|---|
| 637 | cnt = _block_size; | 
|---|
| 638 | } | 
|---|
| 639 | } | 
|---|
| 640 | } | 
|---|
| 641 | if (_curr_length == 0) return false; | 
|---|
| 642 | _next_arc = last_arc + 1; | 
|---|
| 643 |  | 
|---|
| 644 | // Make heap of the candidate list (approximating a partial sort) | 
|---|
| 645 | make_heap( _candidates.begin(), _candidates.begin() + _curr_length, | 
|---|
| 646 | _sort_func ); | 
|---|
| 647 |  | 
|---|
| 648 | // Pop the first element of the heap | 
|---|
| 649 | _in_arc = _candidates[0]; | 
|---|
| 650 | pop_heap( _candidates.begin(), _candidates.begin() + _curr_length, | 
|---|
| 651 | _sort_func ); | 
|---|
| 652 | _curr_length = std::min(_head_length, _curr_length - 1); | 
|---|
| 653 | return true; | 
|---|
| 654 | } | 
|---|
| 655 |  | 
|---|
| 656 | }; //class AlteringListPivotRule | 
|---|
| 657 |  | 
|---|
| 658 | public: | 
|---|
| 659 |  | 
|---|
| 660 | /// \brief Constructor. | 
|---|
| 661 | /// | 
|---|
| 662 | /// The constructor of the class. | 
|---|
| 663 | /// | 
|---|
| 664 | /// \param graph The digraph the algorithm runs on. | 
|---|
| 665 | NetworkSimplex(const GR& graph) : | 
|---|
| 666 | _graph(graph), _node_id(graph), _arc_id(graph), | 
|---|
| 667 | INF(std::numeric_limits<Value>::has_infinity ? | 
|---|
| 668 | std::numeric_limits<Value>::infinity() : | 
|---|
| 669 | std::numeric_limits<Value>::max()) | 
|---|
| 670 | { | 
|---|
| 671 | // Check the value types | 
|---|
| 672 | LEMON_ASSERT(std::numeric_limits<Value>::is_signed, | 
|---|
| 673 | "The flow type of NetworkSimplex must be signed"); | 
|---|
| 674 | LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, | 
|---|
| 675 | "The cost type of NetworkSimplex must be signed"); | 
|---|
| 676 |  | 
|---|
| 677 | // Resize vectors | 
|---|
| 678 | _node_num = countNodes(_graph); | 
|---|
| 679 | _arc_num = countArcs(_graph); | 
|---|
| 680 | int all_node_num = _node_num + 1; | 
|---|
| 681 | int all_arc_num = _arc_num + _node_num; | 
|---|
| 682 |  | 
|---|
| 683 | _source.resize(all_arc_num); | 
|---|
| 684 | _target.resize(all_arc_num); | 
|---|
| 685 |  | 
|---|
| 686 | _lower.resize(all_arc_num); | 
|---|
| 687 | _upper.resize(all_arc_num); | 
|---|
| 688 | _cap.resize(all_arc_num); | 
|---|
| 689 | _cost.resize(all_arc_num); | 
|---|
| 690 | _supply.resize(all_node_num); | 
|---|
| 691 | _flow.resize(all_arc_num); | 
|---|
| 692 | _pi.resize(all_node_num); | 
|---|
| 693 |  | 
|---|
| 694 | _parent.resize(all_node_num); | 
|---|
| 695 | _pred.resize(all_node_num); | 
|---|
| 696 | _forward.resize(all_node_num); | 
|---|
| 697 | _thread.resize(all_node_num); | 
|---|
| 698 | _rev_thread.resize(all_node_num); | 
|---|
| 699 | _succ_num.resize(all_node_num); | 
|---|
| 700 | _last_succ.resize(all_node_num); | 
|---|
| 701 | _state.resize(all_arc_num); | 
|---|
| 702 |  | 
|---|
| 703 | // Copy the graph (store the arcs in a mixed order) | 
|---|
| 704 | int i = 0; | 
|---|
| 705 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { | 
|---|
| 706 | _node_id[n] = i; | 
|---|
| 707 | } | 
|---|
| 708 | int k = std::max(int(std::sqrt(double(_arc_num))), 10); | 
|---|
| 709 | i = 0; | 
|---|
| 710 | for (ArcIt a(_graph); a != INVALID; ++a) { | 
|---|
| 711 | _arc_id[a] = i; | 
|---|
| 712 | _source[i] = _node_id[_graph.source(a)]; | 
|---|
| 713 | _target[i] = _node_id[_graph.target(a)]; | 
|---|
| 714 | if ((i += k) >= _arc_num) i = (i % k) + 1; | 
|---|
| 715 | } | 
|---|
| 716 |  | 
|---|
| 717 | // Initialize maps | 
|---|
| 718 | for (int i = 0; i != _node_num; ++i) { | 
|---|
| 719 | _supply[i] = 0; | 
|---|
| 720 | } | 
|---|
| 721 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 722 | _lower[i] = 0; | 
|---|
| 723 | _upper[i] = INF; | 
|---|
| 724 | _cost[i] = 1; | 
|---|
| 725 | } | 
|---|
| 726 | _have_lower = false; | 
|---|
| 727 | _stype = GEQ; | 
|---|
| 728 | } | 
|---|
| 729 |  | 
|---|
| 730 | /// \name Parameters | 
|---|
| 731 | /// The parameters of the algorithm can be specified using these | 
|---|
| 732 | /// functions. | 
|---|
| 733 |  | 
|---|
| 734 | /// @{ | 
|---|
| 735 |  | 
|---|
| 736 | /// \brief Set the lower bounds on the arcs. | 
|---|
| 737 | /// | 
|---|
| 738 | /// This function sets the lower bounds on the arcs. | 
|---|
| 739 | /// If it is not used before calling \ref run(), the lower bounds | 
|---|
| 740 | /// will be set to zero on all arcs. | 
|---|
| 741 | /// | 
|---|
| 742 | /// \param map An arc map storing the lower bounds. | 
|---|
| 743 | /// Its \c Value type must be convertible to the \c Value type | 
|---|
| 744 | /// of the algorithm. | 
|---|
| 745 | /// | 
|---|
| 746 | /// \return <tt>(*this)</tt> | 
|---|
| 747 | template <typename LowerMap> | 
|---|
| 748 | NetworkSimplex& lowerMap(const LowerMap& map) { | 
|---|
| 749 | _have_lower = true; | 
|---|
| 750 | for (ArcIt a(_graph); a != INVALID; ++a) { | 
|---|
| 751 | _lower[_arc_id[a]] = map[a]; | 
|---|
| 752 | } | 
|---|
| 753 | return *this; | 
|---|
| 754 | } | 
|---|
| 755 |  | 
|---|
| 756 | /// \brief Set the upper bounds (capacities) on the arcs. | 
|---|
| 757 | /// | 
|---|
| 758 | /// This function sets the upper bounds (capacities) on the arcs. | 
|---|
| 759 | /// If it is not used before calling \ref run(), the upper bounds | 
|---|
| 760 | /// will be set to \ref INF on all arcs (i.e. the flow value will be | 
|---|
| 761 | /// unbounded from above on each arc). | 
|---|
| 762 | /// | 
|---|
| 763 | /// \param map An arc map storing the upper bounds. | 
|---|
| 764 | /// Its \c Value type must be convertible to the \c Value type | 
|---|
| 765 | /// of the algorithm. | 
|---|
| 766 | /// | 
|---|
| 767 | /// \return <tt>(*this)</tt> | 
|---|
| 768 | template<typename UpperMap> | 
|---|
| 769 | NetworkSimplex& upperMap(const UpperMap& map) { | 
|---|
| 770 | for (ArcIt a(_graph); a != INVALID; ++a) { | 
|---|
| 771 | _upper[_arc_id[a]] = map[a]; | 
|---|
| 772 | } | 
|---|
| 773 | return *this; | 
|---|
| 774 | } | 
|---|
| 775 |  | 
|---|
| 776 | /// \brief Set the costs of the arcs. | 
|---|
| 777 | /// | 
|---|
| 778 | /// This function sets the costs of the arcs. | 
|---|
| 779 | /// If it is not used before calling \ref run(), the costs | 
|---|
| 780 | /// will be set to \c 1 on all arcs. | 
|---|
| 781 | /// | 
|---|
| 782 | /// \param map An arc map storing the costs. | 
|---|
| 783 | /// Its \c Value type must be convertible to the \c Cost type | 
|---|
| 784 | /// of the algorithm. | 
|---|
| 785 | /// | 
|---|
| 786 | /// \return <tt>(*this)</tt> | 
|---|
| 787 | template<typename CostMap> | 
|---|
| 788 | NetworkSimplex& costMap(const CostMap& map) { | 
|---|
| 789 | for (ArcIt a(_graph); a != INVALID; ++a) { | 
|---|
| 790 | _cost[_arc_id[a]] = map[a]; | 
|---|
| 791 | } | 
|---|
| 792 | return *this; | 
|---|
| 793 | } | 
|---|
| 794 |  | 
|---|
| 795 | /// \brief Set the supply values of the nodes. | 
|---|
| 796 | /// | 
|---|
| 797 | /// This function sets the supply values of the nodes. | 
|---|
| 798 | /// If neither this function nor \ref stSupply() is used before | 
|---|
| 799 | /// calling \ref run(), the supply of each node will be set to zero. | 
|---|
| 800 | /// (It makes sense only if non-zero lower bounds are given.) | 
|---|
| 801 | /// | 
|---|
| 802 | /// \param map A node map storing the supply values. | 
|---|
| 803 | /// Its \c Value type must be convertible to the \c Value type | 
|---|
| 804 | /// of the algorithm. | 
|---|
| 805 | /// | 
|---|
| 806 | /// \return <tt>(*this)</tt> | 
|---|
| 807 | template<typename SupplyMap> | 
|---|
| 808 | NetworkSimplex& supplyMap(const SupplyMap& map) { | 
|---|
| 809 | for (NodeIt n(_graph); n != INVALID; ++n) { | 
|---|
| 810 | _supply[_node_id[n]] = map[n]; | 
|---|
| 811 | } | 
|---|
| 812 | return *this; | 
|---|
| 813 | } | 
|---|
| 814 |  | 
|---|
| 815 | /// \brief Set single source and target nodes and a supply value. | 
|---|
| 816 | /// | 
|---|
| 817 | /// This function sets a single source node and a single target node | 
|---|
| 818 | /// and the required flow value. | 
|---|
| 819 | /// If neither this function nor \ref supplyMap() is used before | 
|---|
| 820 | /// calling \ref run(), the supply of each node will be set to zero. | 
|---|
| 821 | /// (It makes sense only if non-zero lower bounds are given.) | 
|---|
| 822 | /// | 
|---|
| 823 | /// Using this function has the same effect as using \ref supplyMap() | 
|---|
| 824 | /// with such a map in which \c k is assigned to \c s, \c -k is | 
|---|
| 825 | /// assigned to \c t and all other nodes have zero supply value. | 
|---|
| 826 | /// | 
|---|
| 827 | /// \param s The source node. | 
|---|
| 828 | /// \param t The target node. | 
|---|
| 829 | /// \param k The required amount of flow from node \c s to node \c t | 
|---|
| 830 | /// (i.e. the supply of \c s and the demand of \c t). | 
|---|
| 831 | /// | 
|---|
| 832 | /// \return <tt>(*this)</tt> | 
|---|
| 833 | NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) { | 
|---|
| 834 | for (int i = 0; i != _node_num; ++i) { | 
|---|
| 835 | _supply[i] = 0; | 
|---|
| 836 | } | 
|---|
| 837 | _supply[_node_id[s]] =  k; | 
|---|
| 838 | _supply[_node_id[t]] = -k; | 
|---|
| 839 | return *this; | 
|---|
| 840 | } | 
|---|
| 841 |  | 
|---|
| 842 | /// \brief Set the type of the supply constraints. | 
|---|
| 843 | /// | 
|---|
| 844 | /// This function sets the type of the supply/demand constraints. | 
|---|
| 845 | /// If it is not used before calling \ref run(), the \ref GEQ supply | 
|---|
| 846 | /// type will be used. | 
|---|
| 847 | /// | 
|---|
| 848 | /// For more information see \ref SupplyType. | 
|---|
| 849 | /// | 
|---|
| 850 | /// \return <tt>(*this)</tt> | 
|---|
| 851 | NetworkSimplex& supplyType(SupplyType supply_type) { | 
|---|
| 852 | _stype = supply_type; | 
|---|
| 853 | return *this; | 
|---|
| 854 | } | 
|---|
| 855 |  | 
|---|
| 856 | /// @} | 
|---|
| 857 |  | 
|---|
| 858 | /// \name Execution Control | 
|---|
| 859 | /// The algorithm can be executed using \ref run(). | 
|---|
| 860 |  | 
|---|
| 861 | /// @{ | 
|---|
| 862 |  | 
|---|
| 863 | /// \brief Run the algorithm. | 
|---|
| 864 | /// | 
|---|
| 865 | /// This function runs the algorithm. | 
|---|
| 866 | /// The paramters can be specified using functions \ref lowerMap(), | 
|---|
| 867 | /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), | 
|---|
| 868 | /// \ref supplyType(). | 
|---|
| 869 | /// For example, | 
|---|
| 870 | /// \code | 
|---|
| 871 | ///   NetworkSimplex<ListDigraph> ns(graph); | 
|---|
| 872 | ///   ns.lowerMap(lower).upperMap(upper).costMap(cost) | 
|---|
| 873 | ///     .supplyMap(sup).run(); | 
|---|
| 874 | /// \endcode | 
|---|
| 875 | /// | 
|---|
| 876 | /// This function can be called more than once. All the parameters | 
|---|
| 877 | /// that have been given are kept for the next call, unless | 
|---|
| 878 | /// \ref reset() is called, thus only the modified parameters | 
|---|
| 879 | /// have to be set again. See \ref reset() for examples. | 
|---|
| 880 | /// However the underlying digraph must not be modified after this | 
|---|
| 881 | /// class have been constructed, since it copies and extends the graph. | 
|---|
| 882 | /// | 
|---|
| 883 | /// \param pivot_rule The pivot rule that will be used during the | 
|---|
| 884 | /// algorithm. For more information see \ref PivotRule. | 
|---|
| 885 | /// | 
|---|
| 886 | /// \return \c INFEASIBLE if no feasible flow exists, | 
|---|
| 887 | /// \n \c OPTIMAL if the problem has optimal solution | 
|---|
| 888 | /// (i.e. it is feasible and bounded), and the algorithm has found | 
|---|
| 889 | /// optimal flow and node potentials (primal and dual solutions), | 
|---|
| 890 | /// \n \c UNBOUNDED if the objective function of the problem is | 
|---|
| 891 | /// unbounded, i.e. there is a directed cycle having negative total | 
|---|
| 892 | /// cost and infinite upper bound. | 
|---|
| 893 | /// | 
|---|
| 894 | /// \see ProblemType, PivotRule | 
|---|
| 895 | ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) { | 
|---|
| 896 | if (!init()) return INFEASIBLE; | 
|---|
| 897 | return start(pivot_rule); | 
|---|
| 898 | } | 
|---|
| 899 |  | 
|---|
| 900 | /// \brief Reset all the parameters that have been given before. | 
|---|
| 901 | /// | 
|---|
| 902 | /// This function resets all the paramaters that have been given | 
|---|
| 903 | /// before using functions \ref lowerMap(), \ref upperMap(), | 
|---|
| 904 | /// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType(). | 
|---|
| 905 | /// | 
|---|
| 906 | /// It is useful for multiple run() calls. If this function is not | 
|---|
| 907 | /// used, all the parameters given before are kept for the next | 
|---|
| 908 | /// \ref run() call. | 
|---|
| 909 | /// However the underlying digraph must not be modified after this | 
|---|
| 910 | /// class have been constructed, since it copies and extends the graph. | 
|---|
| 911 | /// | 
|---|
| 912 | /// For example, | 
|---|
| 913 | /// \code | 
|---|
| 914 | ///   NetworkSimplex<ListDigraph> ns(graph); | 
|---|
| 915 | /// | 
|---|
| 916 | ///   // First run | 
|---|
| 917 | ///   ns.lowerMap(lower).upperMap(upper).costMap(cost) | 
|---|
| 918 | ///     .supplyMap(sup).run(); | 
|---|
| 919 | /// | 
|---|
| 920 | ///   // Run again with modified cost map (reset() is not called, | 
|---|
| 921 | ///   // so only the cost map have to be set again) | 
|---|
| 922 | ///   cost[e] += 100; | 
|---|
| 923 | ///   ns.costMap(cost).run(); | 
|---|
| 924 | /// | 
|---|
| 925 | ///   // Run again from scratch using reset() | 
|---|
| 926 | ///   // (the lower bounds will be set to zero on all arcs) | 
|---|
| 927 | ///   ns.reset(); | 
|---|
| 928 | ///   ns.upperMap(capacity).costMap(cost) | 
|---|
| 929 | ///     .supplyMap(sup).run(); | 
|---|
| 930 | /// \endcode | 
|---|
| 931 | /// | 
|---|
| 932 | /// \return <tt>(*this)</tt> | 
|---|
| 933 | NetworkSimplex& reset() { | 
|---|
| 934 | for (int i = 0; i != _node_num; ++i) { | 
|---|
| 935 | _supply[i] = 0; | 
|---|
| 936 | } | 
|---|
| 937 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 938 | _lower[i] = 0; | 
|---|
| 939 | _upper[i] = INF; | 
|---|
| 940 | _cost[i] = 1; | 
|---|
| 941 | } | 
|---|
| 942 | _have_lower = false; | 
|---|
| 943 | _stype = GEQ; | 
|---|
| 944 | return *this; | 
|---|
| 945 | } | 
|---|
| 946 |  | 
|---|
| 947 | /// @} | 
|---|
| 948 |  | 
|---|
| 949 | /// \name Query Functions | 
|---|
| 950 | /// The results of the algorithm can be obtained using these | 
|---|
| 951 | /// functions.\n | 
|---|
| 952 | /// The \ref run() function must be called before using them. | 
|---|
| 953 |  | 
|---|
| 954 | /// @{ | 
|---|
| 955 |  | 
|---|
| 956 | /// \brief Return the total cost of the found flow. | 
|---|
| 957 | /// | 
|---|
| 958 | /// This function returns the total cost of the found flow. | 
|---|
| 959 | /// Its complexity is O(e). | 
|---|
| 960 | /// | 
|---|
| 961 | /// \note The return type of the function can be specified as a | 
|---|
| 962 | /// template parameter. For example, | 
|---|
| 963 | /// \code | 
|---|
| 964 | ///   ns.totalCost<double>(); | 
|---|
| 965 | /// \endcode | 
|---|
| 966 | /// It is useful if the total cost cannot be stored in the \c Cost | 
|---|
| 967 | /// type of the algorithm, which is the default return type of the | 
|---|
| 968 | /// function. | 
|---|
| 969 | /// | 
|---|
| 970 | /// \pre \ref run() must be called before using this function. | 
|---|
| 971 | template <typename Number> | 
|---|
| 972 | Number totalCost() const { | 
|---|
| 973 | Number c = 0; | 
|---|
| 974 | for (ArcIt a(_graph); a != INVALID; ++a) { | 
|---|
| 975 | int i = _arc_id[a]; | 
|---|
| 976 | c += Number(_flow[i]) * Number(_cost[i]); | 
|---|
| 977 | } | 
|---|
| 978 | return c; | 
|---|
| 979 | } | 
|---|
| 980 |  | 
|---|
| 981 | #ifndef DOXYGEN | 
|---|
| 982 | Cost totalCost() const { | 
|---|
| 983 | return totalCost<Cost>(); | 
|---|
| 984 | } | 
|---|
| 985 | #endif | 
|---|
| 986 |  | 
|---|
| 987 | /// \brief Return the flow on the given arc. | 
|---|
| 988 | /// | 
|---|
| 989 | /// This function returns the flow on the given arc. | 
|---|
| 990 | /// | 
|---|
| 991 | /// \pre \ref run() must be called before using this function. | 
|---|
| 992 | Value flow(const Arc& a) const { | 
|---|
| 993 | return _flow[_arc_id[a]]; | 
|---|
| 994 | } | 
|---|
| 995 |  | 
|---|
| 996 | /// \brief Return the flow map (the primal solution). | 
|---|
| 997 | /// | 
|---|
| 998 | /// This function copies the flow value on each arc into the given | 
|---|
| 999 | /// map. The \c Value type of the algorithm must be convertible to | 
|---|
| 1000 | /// the \c Value type of the map. | 
|---|
| 1001 | /// | 
|---|
| 1002 | /// \pre \ref run() must be called before using this function. | 
|---|
| 1003 | template <typename FlowMap> | 
|---|
| 1004 | void flowMap(FlowMap &map) const { | 
|---|
| 1005 | for (ArcIt a(_graph); a != INVALID; ++a) { | 
|---|
| 1006 | map.set(a, _flow[_arc_id[a]]); | 
|---|
| 1007 | } | 
|---|
| 1008 | } | 
|---|
| 1009 |  | 
|---|
| 1010 | /// \brief Return the potential (dual value) of the given node. | 
|---|
| 1011 | /// | 
|---|
| 1012 | /// This function returns the potential (dual value) of the | 
|---|
| 1013 | /// given node. | 
|---|
| 1014 | /// | 
|---|
| 1015 | /// \pre \ref run() must be called before using this function. | 
|---|
| 1016 | Cost potential(const Node& n) const { | 
|---|
| 1017 | return _pi[_node_id[n]]; | 
|---|
| 1018 | } | 
|---|
| 1019 |  | 
|---|
| 1020 | /// \brief Return the potential map (the dual solution). | 
|---|
| 1021 | /// | 
|---|
| 1022 | /// This function copies the potential (dual value) of each node | 
|---|
| 1023 | /// into the given map. | 
|---|
| 1024 | /// The \c Cost type of the algorithm must be convertible to the | 
|---|
| 1025 | /// \c Value type of the map. | 
|---|
| 1026 | /// | 
|---|
| 1027 | /// \pre \ref run() must be called before using this function. | 
|---|
| 1028 | template <typename PotentialMap> | 
|---|
| 1029 | void potentialMap(PotentialMap &map) const { | 
|---|
| 1030 | for (NodeIt n(_graph); n != INVALID; ++n) { | 
|---|
| 1031 | map.set(n, _pi[_node_id[n]]); | 
|---|
| 1032 | } | 
|---|
| 1033 | } | 
|---|
| 1034 |  | 
|---|
| 1035 | /// @} | 
|---|
| 1036 |  | 
|---|
| 1037 | private: | 
|---|
| 1038 |  | 
|---|
| 1039 | // Initialize internal data structures | 
|---|
| 1040 | bool init() { | 
|---|
| 1041 | if (_node_num == 0) return false; | 
|---|
| 1042 |  | 
|---|
| 1043 | // Check the sum of supply values | 
|---|
| 1044 | _sum_supply = 0; | 
|---|
| 1045 | for (int i = 0; i != _node_num; ++i) { | 
|---|
| 1046 | _sum_supply += _supply[i]; | 
|---|
| 1047 | } | 
|---|
| 1048 | if ( !(_stype == GEQ && _sum_supply <= 0 || | 
|---|
| 1049 | _stype == LEQ && _sum_supply >= 0) ) return false; | 
|---|
| 1050 |  | 
|---|
| 1051 | // Remove non-zero lower bounds | 
|---|
| 1052 | if (_have_lower) { | 
|---|
| 1053 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 1054 | Value c = _lower[i]; | 
|---|
| 1055 | if (c >= 0) { | 
|---|
| 1056 | _cap[i] = _upper[i] < INF ? _upper[i] - c : INF; | 
|---|
| 1057 | } else { | 
|---|
| 1058 | _cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF; | 
|---|
| 1059 | } | 
|---|
| 1060 | _supply[_source[i]] -= c; | 
|---|
| 1061 | _supply[_target[i]] += c; | 
|---|
| 1062 | } | 
|---|
| 1063 | } else { | 
|---|
| 1064 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 1065 | _cap[i] = _upper[i]; | 
|---|
| 1066 | } | 
|---|
| 1067 | } | 
|---|
| 1068 |  | 
|---|
| 1069 | // Initialize artifical cost | 
|---|
| 1070 | Cost ART_COST; | 
|---|
| 1071 | if (std::numeric_limits<Cost>::is_exact) { | 
|---|
| 1072 | ART_COST = std::numeric_limits<Cost>::max() / 4 + 1; | 
|---|
| 1073 | } else { | 
|---|
| 1074 | ART_COST = std::numeric_limits<Cost>::min(); | 
|---|
| 1075 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 1076 | if (_cost[i] > ART_COST) ART_COST = _cost[i]; | 
|---|
| 1077 | } | 
|---|
| 1078 | ART_COST = (ART_COST + 1) * _node_num; | 
|---|
| 1079 | } | 
|---|
| 1080 |  | 
|---|
| 1081 | // Initialize arc maps | 
|---|
| 1082 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 1083 | _flow[i] = 0; | 
|---|
| 1084 | _state[i] = STATE_LOWER; | 
|---|
| 1085 | } | 
|---|
| 1086 |  | 
|---|
| 1087 | // Set data for the artificial root node | 
|---|
| 1088 | _root = _node_num; | 
|---|
| 1089 | _parent[_root] = -1; | 
|---|
| 1090 | _pred[_root] = -1; | 
|---|
| 1091 | _thread[_root] = 0; | 
|---|
| 1092 | _rev_thread[0] = _root; | 
|---|
| 1093 | _succ_num[_root] = _node_num + 1; | 
|---|
| 1094 | _last_succ[_root] = _root - 1; | 
|---|
| 1095 | _supply[_root] = -_sum_supply; | 
|---|
| 1096 | _pi[_root] = _sum_supply < 0 ? -ART_COST : ART_COST; | 
|---|
| 1097 |  | 
|---|
| 1098 | // Add artificial arcs and initialize the spanning tree data structure | 
|---|
| 1099 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { | 
|---|
| 1100 | _parent[u] = _root; | 
|---|
| 1101 | _pred[u] = e; | 
|---|
| 1102 | _thread[u] = u + 1; | 
|---|
| 1103 | _rev_thread[u + 1] = u; | 
|---|
| 1104 | _succ_num[u] = 1; | 
|---|
| 1105 | _last_succ[u] = u; | 
|---|
| 1106 | _cost[e] = ART_COST; | 
|---|
| 1107 | _cap[e] = INF; | 
|---|
| 1108 | _state[e] = STATE_TREE; | 
|---|
| 1109 | if (_supply[u] > 0 || (_supply[u] == 0 && _sum_supply <= 0)) { | 
|---|
| 1110 | _flow[e] = _supply[u]; | 
|---|
| 1111 | _forward[u] = true; | 
|---|
| 1112 | _pi[u] = -ART_COST + _pi[_root]; | 
|---|
| 1113 | } else { | 
|---|
| 1114 | _flow[e] = -_supply[u]; | 
|---|
| 1115 | _forward[u] = false; | 
|---|
| 1116 | _pi[u] = ART_COST + _pi[_root]; | 
|---|
| 1117 | } | 
|---|
| 1118 | } | 
|---|
| 1119 |  | 
|---|
| 1120 | return true; | 
|---|
| 1121 | } | 
|---|
| 1122 |  | 
|---|
| 1123 | // Find the join node | 
|---|
| 1124 | void findJoinNode() { | 
|---|
| 1125 | int u = _source[in_arc]; | 
|---|
| 1126 | int v = _target[in_arc]; | 
|---|
| 1127 | while (u != v) { | 
|---|
| 1128 | if (_succ_num[u] < _succ_num[v]) { | 
|---|
| 1129 | u = _parent[u]; | 
|---|
| 1130 | } else { | 
|---|
| 1131 | v = _parent[v]; | 
|---|
| 1132 | } | 
|---|
| 1133 | } | 
|---|
| 1134 | join = u; | 
|---|
| 1135 | } | 
|---|
| 1136 |  | 
|---|
| 1137 | // Find the leaving arc of the cycle and returns true if the | 
|---|
| 1138 | // leaving arc is not the same as the entering arc | 
|---|
| 1139 | bool findLeavingArc() { | 
|---|
| 1140 | // Initialize first and second nodes according to the direction | 
|---|
| 1141 | // of the cycle | 
|---|
| 1142 | if (_state[in_arc] == STATE_LOWER) { | 
|---|
| 1143 | first  = _source[in_arc]; | 
|---|
| 1144 | second = _target[in_arc]; | 
|---|
| 1145 | } else { | 
|---|
| 1146 | first  = _target[in_arc]; | 
|---|
| 1147 | second = _source[in_arc]; | 
|---|
| 1148 | } | 
|---|
| 1149 | delta = _cap[in_arc]; | 
|---|
| 1150 | int result = 0; | 
|---|
| 1151 | Value d; | 
|---|
| 1152 | int e; | 
|---|
| 1153 |  | 
|---|
| 1154 | // Search the cycle along the path form the first node to the root | 
|---|
| 1155 | for (int u = first; u != join; u = _parent[u]) { | 
|---|
| 1156 | e = _pred[u]; | 
|---|
| 1157 | d = _forward[u] ? | 
|---|
| 1158 | _flow[e] : (_cap[e] == INF ? INF : _cap[e] - _flow[e]); | 
|---|
| 1159 | if (d < delta) { | 
|---|
| 1160 | delta = d; | 
|---|
| 1161 | u_out = u; | 
|---|
| 1162 | result = 1; | 
|---|
| 1163 | } | 
|---|
| 1164 | } | 
|---|
| 1165 | // Search the cycle along the path form the second node to the root | 
|---|
| 1166 | for (int u = second; u != join; u = _parent[u]) { | 
|---|
| 1167 | e = _pred[u]; | 
|---|
| 1168 | d = _forward[u] ? | 
|---|
| 1169 | (_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e]; | 
|---|
| 1170 | if (d <= delta) { | 
|---|
| 1171 | delta = d; | 
|---|
| 1172 | u_out = u; | 
|---|
| 1173 | result = 2; | 
|---|
| 1174 | } | 
|---|
| 1175 | } | 
|---|
| 1176 |  | 
|---|
| 1177 | if (result == 1) { | 
|---|
| 1178 | u_in = first; | 
|---|
| 1179 | v_in = second; | 
|---|
| 1180 | } else { | 
|---|
| 1181 | u_in = second; | 
|---|
| 1182 | v_in = first; | 
|---|
| 1183 | } | 
|---|
| 1184 | return result != 0; | 
|---|
| 1185 | } | 
|---|
| 1186 |  | 
|---|
| 1187 | // Change _flow and _state vectors | 
|---|
| 1188 | void changeFlow(bool change) { | 
|---|
| 1189 | // Augment along the cycle | 
|---|
| 1190 | if (delta > 0) { | 
|---|
| 1191 | Value val = _state[in_arc] * delta; | 
|---|
| 1192 | _flow[in_arc] += val; | 
|---|
| 1193 | for (int u = _source[in_arc]; u != join; u = _parent[u]) { | 
|---|
| 1194 | _flow[_pred[u]] += _forward[u] ? -val : val; | 
|---|
| 1195 | } | 
|---|
| 1196 | for (int u = _target[in_arc]; u != join; u = _parent[u]) { | 
|---|
| 1197 | _flow[_pred[u]] += _forward[u] ? val : -val; | 
|---|
| 1198 | } | 
|---|
| 1199 | } | 
|---|
| 1200 | // Update the state of the entering and leaving arcs | 
|---|
| 1201 | if (change) { | 
|---|
| 1202 | _state[in_arc] = STATE_TREE; | 
|---|
| 1203 | _state[_pred[u_out]] = | 
|---|
| 1204 | (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; | 
|---|
| 1205 | } else { | 
|---|
| 1206 | _state[in_arc] = -_state[in_arc]; | 
|---|
| 1207 | } | 
|---|
| 1208 | } | 
|---|
| 1209 |  | 
|---|
| 1210 | // Update the tree structure | 
|---|
| 1211 | void updateTreeStructure() { | 
|---|
| 1212 | int u, w; | 
|---|
| 1213 | int old_rev_thread = _rev_thread[u_out]; | 
|---|
| 1214 | int old_succ_num = _succ_num[u_out]; | 
|---|
| 1215 | int old_last_succ = _last_succ[u_out]; | 
|---|
| 1216 | v_out = _parent[u_out]; | 
|---|
| 1217 |  | 
|---|
| 1218 | u = _last_succ[u_in];  // the last successor of u_in | 
|---|
| 1219 | right = _thread[u];    // the node after it | 
|---|
| 1220 |  | 
|---|
| 1221 | // Handle the case when old_rev_thread equals to v_in | 
|---|
| 1222 | // (it also means that join and v_out coincide) | 
|---|
| 1223 | if (old_rev_thread == v_in) { | 
|---|
| 1224 | last = _thread[_last_succ[u_out]]; | 
|---|
| 1225 | } else { | 
|---|
| 1226 | last = _thread[v_in]; | 
|---|
| 1227 | } | 
|---|
| 1228 |  | 
|---|
| 1229 | // Update _thread and _parent along the stem nodes (i.e. the nodes | 
|---|
| 1230 | // between u_in and u_out, whose parent have to be changed) | 
|---|
| 1231 | _thread[v_in] = stem = u_in; | 
|---|
| 1232 | _dirty_revs.clear(); | 
|---|
| 1233 | _dirty_revs.push_back(v_in); | 
|---|
| 1234 | par_stem = v_in; | 
|---|
| 1235 | while (stem != u_out) { | 
|---|
| 1236 | // Insert the next stem node into the thread list | 
|---|
| 1237 | new_stem = _parent[stem]; | 
|---|
| 1238 | _thread[u] = new_stem; | 
|---|
| 1239 | _dirty_revs.push_back(u); | 
|---|
| 1240 |  | 
|---|
| 1241 | // Remove the subtree of stem from the thread list | 
|---|
| 1242 | w = _rev_thread[stem]; | 
|---|
| 1243 | _thread[w] = right; | 
|---|
| 1244 | _rev_thread[right] = w; | 
|---|
| 1245 |  | 
|---|
| 1246 | // Change the parent node and shift stem nodes | 
|---|
| 1247 | _parent[stem] = par_stem; | 
|---|
| 1248 | par_stem = stem; | 
|---|
| 1249 | stem = new_stem; | 
|---|
| 1250 |  | 
|---|
| 1251 | // Update u and right | 
|---|
| 1252 | u = _last_succ[stem] == _last_succ[par_stem] ? | 
|---|
| 1253 | _rev_thread[par_stem] : _last_succ[stem]; | 
|---|
| 1254 | right = _thread[u]; | 
|---|
| 1255 | } | 
|---|
| 1256 | _parent[u_out] = par_stem; | 
|---|
| 1257 | _thread[u] = last; | 
|---|
| 1258 | _rev_thread[last] = u; | 
|---|
| 1259 | _last_succ[u_out] = u; | 
|---|
| 1260 |  | 
|---|
| 1261 | // Remove the subtree of u_out from the thread list except for | 
|---|
| 1262 | // the case when old_rev_thread equals to v_in | 
|---|
| 1263 | // (it also means that join and v_out coincide) | 
|---|
| 1264 | if (old_rev_thread != v_in) { | 
|---|
| 1265 | _thread[old_rev_thread] = right; | 
|---|
| 1266 | _rev_thread[right] = old_rev_thread; | 
|---|
| 1267 | } | 
|---|
| 1268 |  | 
|---|
| 1269 | // Update _rev_thread using the new _thread values | 
|---|
| 1270 | for (int i = 0; i < int(_dirty_revs.size()); ++i) { | 
|---|
| 1271 | u = _dirty_revs[i]; | 
|---|
| 1272 | _rev_thread[_thread[u]] = u; | 
|---|
| 1273 | } | 
|---|
| 1274 |  | 
|---|
| 1275 | // Update _pred, _forward, _last_succ and _succ_num for the | 
|---|
| 1276 | // stem nodes from u_out to u_in | 
|---|
| 1277 | int tmp_sc = 0, tmp_ls = _last_succ[u_out]; | 
|---|
| 1278 | u = u_out; | 
|---|
| 1279 | while (u != u_in) { | 
|---|
| 1280 | w = _parent[u]; | 
|---|
| 1281 | _pred[u] = _pred[w]; | 
|---|
| 1282 | _forward[u] = !_forward[w]; | 
|---|
| 1283 | tmp_sc += _succ_num[u] - _succ_num[w]; | 
|---|
| 1284 | _succ_num[u] = tmp_sc; | 
|---|
| 1285 | _last_succ[w] = tmp_ls; | 
|---|
| 1286 | u = w; | 
|---|
| 1287 | } | 
|---|
| 1288 | _pred[u_in] = in_arc; | 
|---|
| 1289 | _forward[u_in] = (u_in == _source[in_arc]); | 
|---|
| 1290 | _succ_num[u_in] = old_succ_num; | 
|---|
| 1291 |  | 
|---|
| 1292 | // Set limits for updating _last_succ form v_in and v_out | 
|---|
| 1293 | // towards the root | 
|---|
| 1294 | int up_limit_in = -1; | 
|---|
| 1295 | int up_limit_out = -1; | 
|---|
| 1296 | if (_last_succ[join] == v_in) { | 
|---|
| 1297 | up_limit_out = join; | 
|---|
| 1298 | } else { | 
|---|
| 1299 | up_limit_in = join; | 
|---|
| 1300 | } | 
|---|
| 1301 |  | 
|---|
| 1302 | // Update _last_succ from v_in towards the root | 
|---|
| 1303 | for (u = v_in; u != up_limit_in && _last_succ[u] == v_in; | 
|---|
| 1304 | u = _parent[u]) { | 
|---|
| 1305 | _last_succ[u] = _last_succ[u_out]; | 
|---|
| 1306 | } | 
|---|
| 1307 | // Update _last_succ from v_out towards the root | 
|---|
| 1308 | if (join != old_rev_thread && v_in != old_rev_thread) { | 
|---|
| 1309 | for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; | 
|---|
| 1310 | u = _parent[u]) { | 
|---|
| 1311 | _last_succ[u] = old_rev_thread; | 
|---|
| 1312 | } | 
|---|
| 1313 | } else { | 
|---|
| 1314 | for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; | 
|---|
| 1315 | u = _parent[u]) { | 
|---|
| 1316 | _last_succ[u] = _last_succ[u_out]; | 
|---|
| 1317 | } | 
|---|
| 1318 | } | 
|---|
| 1319 |  | 
|---|
| 1320 | // Update _succ_num from v_in to join | 
|---|
| 1321 | for (u = v_in; u != join; u = _parent[u]) { | 
|---|
| 1322 | _succ_num[u] += old_succ_num; | 
|---|
| 1323 | } | 
|---|
| 1324 | // Update _succ_num from v_out to join | 
|---|
| 1325 | for (u = v_out; u != join; u = _parent[u]) { | 
|---|
| 1326 | _succ_num[u] -= old_succ_num; | 
|---|
| 1327 | } | 
|---|
| 1328 | } | 
|---|
| 1329 |  | 
|---|
| 1330 | // Update potentials | 
|---|
| 1331 | void updatePotential() { | 
|---|
| 1332 | Cost sigma = _forward[u_in] ? | 
|---|
| 1333 | _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] : | 
|---|
| 1334 | _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]]; | 
|---|
| 1335 | // Update potentials in the subtree, which has been moved | 
|---|
| 1336 | int end = _thread[_last_succ[u_in]]; | 
|---|
| 1337 | for (int u = u_in; u != end; u = _thread[u]) { | 
|---|
| 1338 | _pi[u] += sigma; | 
|---|
| 1339 | } | 
|---|
| 1340 | } | 
|---|
| 1341 |  | 
|---|
| 1342 | // Execute the algorithm | 
|---|
| 1343 | ProblemType start(PivotRule pivot_rule) { | 
|---|
| 1344 | // Select the pivot rule implementation | 
|---|
| 1345 | switch (pivot_rule) { | 
|---|
| 1346 | case FIRST_ELIGIBLE: | 
|---|
| 1347 | return start<FirstEligiblePivotRule>(); | 
|---|
| 1348 | case BEST_ELIGIBLE: | 
|---|
| 1349 | return start<BestEligiblePivotRule>(); | 
|---|
| 1350 | case BLOCK_SEARCH: | 
|---|
| 1351 | return start<BlockSearchPivotRule>(); | 
|---|
| 1352 | case CANDIDATE_LIST: | 
|---|
| 1353 | return start<CandidateListPivotRule>(); | 
|---|
| 1354 | case ALTERING_LIST: | 
|---|
| 1355 | return start<AlteringListPivotRule>(); | 
|---|
| 1356 | } | 
|---|
| 1357 | return INFEASIBLE; // avoid warning | 
|---|
| 1358 | } | 
|---|
| 1359 |  | 
|---|
| 1360 | template <typename PivotRuleImpl> | 
|---|
| 1361 | ProblemType start() { | 
|---|
| 1362 | PivotRuleImpl pivot(*this); | 
|---|
| 1363 |  | 
|---|
| 1364 | // Execute the Network Simplex algorithm | 
|---|
| 1365 | while (pivot.findEnteringArc()) { | 
|---|
| 1366 | findJoinNode(); | 
|---|
| 1367 | bool change = findLeavingArc(); | 
|---|
| 1368 | if (delta >= INF) return UNBOUNDED; | 
|---|
| 1369 | changeFlow(change); | 
|---|
| 1370 | if (change) { | 
|---|
| 1371 | updateTreeStructure(); | 
|---|
| 1372 | updatePotential(); | 
|---|
| 1373 | } | 
|---|
| 1374 | } | 
|---|
| 1375 |  | 
|---|
| 1376 | // Check feasibility | 
|---|
| 1377 | if (_sum_supply < 0) { | 
|---|
| 1378 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { | 
|---|
| 1379 | if (_supply[u] >= 0 && _flow[e] != 0) return INFEASIBLE; | 
|---|
| 1380 | } | 
|---|
| 1381 | } | 
|---|
| 1382 | else if (_sum_supply > 0) { | 
|---|
| 1383 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { | 
|---|
| 1384 | if (_supply[u] <= 0 && _flow[e] != 0) return INFEASIBLE; | 
|---|
| 1385 | } | 
|---|
| 1386 | } | 
|---|
| 1387 | else { | 
|---|
| 1388 | for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) { | 
|---|
| 1389 | if (_flow[e] != 0) return INFEASIBLE; | 
|---|
| 1390 | } | 
|---|
| 1391 | } | 
|---|
| 1392 |  | 
|---|
| 1393 | // Transform the solution and the supply map to the original form | 
|---|
| 1394 | if (_have_lower) { | 
|---|
| 1395 | for (int i = 0; i != _arc_num; ++i) { | 
|---|
| 1396 | Value c = _lower[i]; | 
|---|
| 1397 | if (c != 0) { | 
|---|
| 1398 | _flow[i] += c; | 
|---|
| 1399 | _supply[_source[i]] += c; | 
|---|
| 1400 | _supply[_target[i]] -= c; | 
|---|
| 1401 | } | 
|---|
| 1402 | } | 
|---|
| 1403 | } | 
|---|
| 1404 |  | 
|---|
| 1405 | return OPTIMAL; | 
|---|
| 1406 | } | 
|---|
| 1407 |  | 
|---|
| 1408 | }; //class NetworkSimplex | 
|---|
| 1409 |  | 
|---|
| 1410 | ///@} | 
|---|
| 1411 |  | 
|---|
| 1412 | } //namespace lemon | 
|---|
| 1413 |  | 
|---|
| 1414 | #endif //LEMON_NETWORK_SIMPLEX_H | 
|---|