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