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