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