lemon/cost_scaling.h
author kpeter
Sun, 05 Oct 2008 13:46:07 +0000
changeset 2621 814ba94d9989
parent 2588 4d3bc1d04c1d
child 2623 90defb96ee61
permissions -rw-r--r--
Bug fix in min_cost_flow_test.cc
kpeter@2577
     1
/* -*- C++ -*-
kpeter@2577
     2
 *
kpeter@2577
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@2577
     4
 *
kpeter@2577
     5
 * Copyright (C) 2003-2008
kpeter@2577
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@2577
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@2577
     8
 *
kpeter@2577
     9
 * Permission to use, modify and distribute this software is granted
kpeter@2577
    10
 * provided that this copyright notice appears in all copies. For
kpeter@2577
    11
 * precise terms see the accompanying LICENSE file.
kpeter@2577
    12
 *
kpeter@2577
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@2577
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@2577
    15
 * purpose.
kpeter@2577
    16
 *
kpeter@2577
    17
 */
kpeter@2577
    18
kpeter@2577
    19
#ifndef LEMON_COST_SCALING_H
kpeter@2577
    20
#define LEMON_COST_SCALING_H
kpeter@2577
    21
kpeter@2577
    22
/// \ingroup min_cost_flow
kpeter@2577
    23
///
kpeter@2577
    24
/// \file
kpeter@2577
    25
/// \brief Cost scaling algorithm for finding a minimum cost flow.
kpeter@2577
    26
kpeter@2577
    27
#include <deque>
kpeter@2577
    28
#include <lemon/graph_adaptor.h>
kpeter@2577
    29
#include <lemon/graph_utils.h>
kpeter@2577
    30
#include <lemon/maps.h>
kpeter@2577
    31
#include <lemon/math.h>
kpeter@2577
    32
kpeter@2577
    33
#include <lemon/circulation.h>
kpeter@2577
    34
#include <lemon/bellman_ford.h>
kpeter@2577
    35
kpeter@2577
    36
namespace lemon {
kpeter@2577
    37
kpeter@2577
    38
  /// \addtogroup min_cost_flow
kpeter@2577
    39
  /// @{
kpeter@2577
    40
kpeter@2577
    41
  /// \brief Implementation of the cost scaling algorithm for finding a
kpeter@2577
    42
  /// minimum cost flow.
kpeter@2577
    43
  ///
kpeter@2577
    44
  /// \ref CostScaling implements the cost scaling algorithm performing
kpeter@2577
    45
  /// generalized push-relabel operations for finding a minimum cost
kpeter@2577
    46
  /// flow.
kpeter@2577
    47
  ///
kpeter@2577
    48
  /// \tparam Graph The directed graph type the algorithm runs on.
kpeter@2577
    49
  /// \tparam LowerMap The type of the lower bound map.
kpeter@2577
    50
  /// \tparam CapacityMap The type of the capacity (upper bound) map.
kpeter@2577
    51
  /// \tparam CostMap The type of the cost (length) map.
kpeter@2577
    52
  /// \tparam SupplyMap The type of the supply map.
kpeter@2577
    53
  ///
kpeter@2577
    54
  /// \warning
kpeter@2577
    55
  /// - Edge capacities and costs should be \e non-negative \e integers.
kpeter@2577
    56
  /// - Supply values should be \e signed \e integers.
kpeter@2581
    57
  /// - The value types of the maps should be convertible to each other.
kpeter@2581
    58
  /// - \c CostMap::Value must be signed type.
kpeter@2577
    59
  ///
kpeter@2577
    60
  /// \note Edge costs are multiplied with the number of nodes during
kpeter@2577
    61
  /// the algorithm so overflow problems may arise more easily than with
kpeter@2577
    62
  /// other minimum cost flow algorithms.
kpeter@2577
    63
  /// If it is available, <tt>long long int</tt> type is used instead of
kpeter@2577
    64
  /// <tt>long int</tt> in the inside computations.
kpeter@2577
    65
  ///
kpeter@2577
    66
  /// \author Peter Kovacs
kpeter@2577
    67
  template < typename Graph,
kpeter@2577
    68
             typename LowerMap = typename Graph::template EdgeMap<int>,
kpeter@2577
    69
             typename CapacityMap = typename Graph::template EdgeMap<int>,
kpeter@2577
    70
             typename CostMap = typename Graph::template EdgeMap<int>,
kpeter@2577
    71
             typename SupplyMap = typename Graph::template NodeMap<int> >
kpeter@2577
    72
  class CostScaling
kpeter@2577
    73
  {
kpeter@2577
    74
    GRAPH_TYPEDEFS(typename Graph);
kpeter@2577
    75
kpeter@2577
    76
    typedef typename CapacityMap::Value Capacity;
kpeter@2577
    77
    typedef typename CostMap::Value Cost;
kpeter@2577
    78
    typedef typename SupplyMap::Value Supply;
kpeter@2577
    79
    typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap;
kpeter@2577
    80
    typedef typename Graph::template NodeMap<Supply> SupplyNodeMap;
kpeter@2577
    81
kpeter@2577
    82
    typedef ResGraphAdaptor< const Graph, Capacity,
kpeter@2577
    83
                             CapacityEdgeMap, CapacityEdgeMap > ResGraph;
kpeter@2577
    84
    typedef typename ResGraph::Edge ResEdge;
kpeter@2577
    85
kpeter@2577
    86
#if defined __GNUC__ && !defined __STRICT_ANSI__
kpeter@2577
    87
    typedef long long int LCost;
kpeter@2577
    88
#else
kpeter@2577
    89
    typedef long int LCost;
kpeter@2577
    90
#endif
kpeter@2577
    91
    typedef typename Graph::template EdgeMap<LCost> LargeCostMap;
kpeter@2577
    92
kpeter@2577
    93
  public:
kpeter@2577
    94
kpeter@2577
    95
    /// The type of the flow map.
kpeter@2581
    96
    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
kpeter@2577
    97
    /// The type of the potential map.
kpeter@2577
    98
    typedef typename Graph::template NodeMap<LCost> PotentialMap;
kpeter@2577
    99
kpeter@2577
   100
  private:
kpeter@2577
   101
kpeter@2577
   102
    /// \brief Map adaptor class for handling residual edge costs.
kpeter@2577
   103
    ///
kpeter@2620
   104
    /// Map adaptor class for handling residual edge costs.
kpeter@2581
   105
    template <typename Map>
kpeter@2581
   106
    class ResidualCostMap : public MapBase<ResEdge, typename Map::Value>
kpeter@2577
   107
    {
kpeter@2577
   108
    private:
kpeter@2577
   109
kpeter@2581
   110
      const Map &_cost_map;
kpeter@2577
   111
kpeter@2577
   112
    public:
kpeter@2577
   113
kpeter@2577
   114
      ///\e
kpeter@2581
   115
      ResidualCostMap(const Map &cost_map) :
kpeter@2577
   116
        _cost_map(cost_map) {}
kpeter@2577
   117
kpeter@2577
   118
      ///\e
kpeter@2581
   119
      typename Map::Value operator[](const ResEdge &e) const {
kpeter@2577
   120
        return ResGraph::forward(e) ?  _cost_map[e] : -_cost_map[e];
kpeter@2577
   121
      }
kpeter@2577
   122
kpeter@2577
   123
    }; //class ResidualCostMap
kpeter@2577
   124
kpeter@2577
   125
    /// \brief Map adaptor class for handling reduced edge costs.
kpeter@2577
   126
    ///
kpeter@2620
   127
    /// Map adaptor class for handling reduced edge costs.
kpeter@2577
   128
    class ReducedCostMap : public MapBase<Edge, LCost>
kpeter@2577
   129
    {
kpeter@2577
   130
    private:
kpeter@2577
   131
kpeter@2577
   132
      const Graph &_gr;
kpeter@2577
   133
      const LargeCostMap &_cost_map;
kpeter@2577
   134
      const PotentialMap &_pot_map;
kpeter@2577
   135
kpeter@2577
   136
    public:
kpeter@2577
   137
kpeter@2577
   138
      ///\e
kpeter@2577
   139
      ReducedCostMap( const Graph &gr,
kpeter@2577
   140
                      const LargeCostMap &cost_map,
kpeter@2577
   141
                      const PotentialMap &pot_map ) :
kpeter@2577
   142
        _gr(gr), _cost_map(cost_map), _pot_map(pot_map) {}
kpeter@2577
   143
kpeter@2577
   144
      ///\e
kpeter@2577
   145
      LCost operator[](const Edge &e) const {
kpeter@2577
   146
        return _cost_map[e] + _pot_map[_gr.source(e)]
kpeter@2577
   147
                            - _pot_map[_gr.target(e)];
kpeter@2577
   148
      }
kpeter@2577
   149
kpeter@2577
   150
    }; //class ReducedCostMap
kpeter@2577
   151
kpeter@2577
   152
  private:
kpeter@2577
   153
kpeter@2577
   154
    // Scaling factor
kpeter@2577
   155
    static const int ALPHA = 4;
kpeter@2577
   156
kpeter@2577
   157
    // Paramters for heuristics
kpeter@2581
   158
    static const int BF_HEURISTIC_EPSILON_BOUND = 5000;
kpeter@2581
   159
    static const int BF_HEURISTIC_BOUND_FACTOR  = 3;
kpeter@2577
   160
kpeter@2577
   161
  private:
kpeter@2577
   162
kpeter@2577
   163
    // The directed graph the algorithm runs on
kpeter@2577
   164
    const Graph &_graph;
kpeter@2577
   165
    // The original lower bound map
kpeter@2577
   166
    const LowerMap *_lower;
kpeter@2577
   167
    // The modified capacity map
kpeter@2577
   168
    CapacityEdgeMap _capacity;
kpeter@2577
   169
    // The original cost map
kpeter@2577
   170
    const CostMap &_orig_cost;
kpeter@2577
   171
    // The scaled cost map
kpeter@2577
   172
    LargeCostMap _cost;
kpeter@2577
   173
    // The modified supply map
kpeter@2577
   174
    SupplyNodeMap _supply;
kpeter@2577
   175
    bool _valid_supply;
kpeter@2577
   176
kpeter@2577
   177
    // Edge map of the current flow
kpeter@2581
   178
    FlowMap *_flow;
kpeter@2581
   179
    bool _local_flow;
kpeter@2577
   180
    // Node map of the current potentials
kpeter@2581
   181
    PotentialMap *_potential;
kpeter@2581
   182
    bool _local_potential;
kpeter@2577
   183
kpeter@2577
   184
    // The residual graph
kpeter@2581
   185
    ResGraph *_res_graph;
kpeter@2577
   186
    // The residual cost map
kpeter@2581
   187
    ResidualCostMap<LargeCostMap> _res_cost;
kpeter@2577
   188
    // The reduced cost map
kpeter@2581
   189
    ReducedCostMap *_red_cost;
kpeter@2577
   190
    // The excess map
kpeter@2577
   191
    SupplyNodeMap _excess;
kpeter@2577
   192
    // The epsilon parameter used for cost scaling
kpeter@2577
   193
    LCost _epsilon;
kpeter@2577
   194
kpeter@2577
   195
  public:
kpeter@2577
   196
kpeter@2581
   197
    /// \brief General constructor (with lower bounds).
kpeter@2577
   198
    ///
kpeter@2581
   199
    /// General constructor (with lower bounds).
kpeter@2577
   200
    ///
kpeter@2577
   201
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   202
    /// \param lower The lower bounds of the edges.
kpeter@2577
   203
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   204
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   205
    /// \param supply The supply values of the nodes (signed).
kpeter@2577
   206
    CostScaling( const Graph &graph,
kpeter@2577
   207
                 const LowerMap &lower,
kpeter@2577
   208
                 const CapacityMap &capacity,
kpeter@2577
   209
                 const CostMap &cost,
kpeter@2577
   210
                 const SupplyMap &supply ) :
kpeter@2577
   211
      _graph(graph), _lower(&lower), _capacity(graph), _orig_cost(cost),
kpeter@2581
   212
      _cost(graph), _supply(graph), _flow(0), _local_flow(false),
kpeter@2581
   213
      _potential(0), _local_potential(false), _res_cost(_cost),
kpeter@2581
   214
      _excess(graph, 0)
kpeter@2577
   215
    {
kpeter@2577
   216
      // Removing non-zero lower bounds
kpeter@2577
   217
      _capacity = subMap(capacity, lower);
kpeter@2577
   218
      Supply sum = 0;
kpeter@2577
   219
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@2577
   220
        Supply s = supply[n];
kpeter@2577
   221
        for (InEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   222
          s += lower[e];
kpeter@2577
   223
        for (OutEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   224
          s -= lower[e];
kpeter@2577
   225
        _supply[n] = s;
kpeter@2577
   226
        sum += s;
kpeter@2577
   227
      }
kpeter@2577
   228
      _valid_supply = sum == 0;
kpeter@2577
   229
    }
kpeter@2577
   230
kpeter@2581
   231
    /// \brief General constructor (without lower bounds).
kpeter@2577
   232
    ///
kpeter@2581
   233
    /// General constructor (without lower bounds).
kpeter@2577
   234
    ///
kpeter@2577
   235
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   236
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   237
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   238
    /// \param supply The supply values of the nodes (signed).
kpeter@2577
   239
    CostScaling( const Graph &graph,
kpeter@2577
   240
                 const CapacityMap &capacity,
kpeter@2577
   241
                 const CostMap &cost,
kpeter@2577
   242
                 const SupplyMap &supply ) :
kpeter@2577
   243
      _graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
kpeter@2581
   244
      _cost(graph), _supply(supply), _flow(0), _local_flow(false),
kpeter@2581
   245
      _potential(0), _local_potential(false), _res_cost(_cost),
kpeter@2581
   246
      _excess(graph, 0)
kpeter@2577
   247
    {
kpeter@2577
   248
      // Checking the sum of supply values
kpeter@2577
   249
      Supply sum = 0;
kpeter@2577
   250
      for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
kpeter@2577
   251
      _valid_supply = sum == 0;
kpeter@2577
   252
    }
kpeter@2577
   253
kpeter@2581
   254
    /// \brief Simple constructor (with lower bounds).
kpeter@2577
   255
    ///
kpeter@2581
   256
    /// Simple constructor (with lower bounds).
kpeter@2577
   257
    ///
kpeter@2577
   258
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   259
    /// \param lower The lower bounds of the edges.
kpeter@2577
   260
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   261
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   262
    /// \param s The source node.
kpeter@2577
   263
    /// \param t The target node.
kpeter@2577
   264
    /// \param flow_value The required amount of flow from node \c s
kpeter@2577
   265
    /// to node \c t (i.e. the supply of \c s and the demand of \c t).
kpeter@2577
   266
    CostScaling( const Graph &graph,
kpeter@2577
   267
                 const LowerMap &lower,
kpeter@2577
   268
                 const CapacityMap &capacity,
kpeter@2577
   269
                 const CostMap &cost,
kpeter@2577
   270
                 Node s, Node t,
kpeter@2577
   271
                 Supply flow_value ) :
kpeter@2577
   272
      _graph(graph), _lower(&lower), _capacity(graph), _orig_cost(cost),
kpeter@2581
   273
      _cost(graph), _supply(graph), _flow(0), _local_flow(false),
kpeter@2581
   274
      _potential(0), _local_potential(false), _res_cost(_cost),
kpeter@2581
   275
      _excess(graph, 0)
kpeter@2577
   276
    {
kpeter@2577
   277
      // Removing nonzero lower bounds
kpeter@2577
   278
      _capacity = subMap(capacity, lower);
kpeter@2577
   279
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@2577
   280
        Supply sum = 0;
kpeter@2577
   281
        if (n == s) sum =  flow_value;
kpeter@2577
   282
        if (n == t) sum = -flow_value;
kpeter@2577
   283
        for (InEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   284
          sum += lower[e];
kpeter@2577
   285
        for (OutEdgeIt e(_graph, n); e != INVALID; ++e)
kpeter@2577
   286
          sum -= lower[e];
kpeter@2577
   287
        _supply[n] = sum;
kpeter@2577
   288
      }
kpeter@2577
   289
      _valid_supply = true;
kpeter@2577
   290
    }
kpeter@2577
   291
kpeter@2581
   292
    /// \brief Simple constructor (without lower bounds).
kpeter@2577
   293
    ///
kpeter@2581
   294
    /// Simple constructor (without lower bounds).
kpeter@2577
   295
    ///
kpeter@2577
   296
    /// \param graph The directed graph the algorithm runs on.
kpeter@2577
   297
    /// \param capacity The capacities (upper bounds) of the edges.
kpeter@2577
   298
    /// \param cost The cost (length) values of the edges.
kpeter@2577
   299
    /// \param s The source node.
kpeter@2577
   300
    /// \param t The target node.
kpeter@2577
   301
    /// \param flow_value The required amount of flow from node \c s
kpeter@2577
   302
    /// to node \c t (i.e. the supply of \c s and the demand of \c t).
kpeter@2577
   303
    CostScaling( const Graph &graph,
kpeter@2577
   304
                 const CapacityMap &capacity,
kpeter@2577
   305
                 const CostMap &cost,
kpeter@2577
   306
                 Node s, Node t,
kpeter@2577
   307
                 Supply flow_value ) :
kpeter@2577
   308
      _graph(graph), _lower(NULL), _capacity(capacity), _orig_cost(cost),
kpeter@2581
   309
      _cost(graph), _supply(graph, 0), _flow(0), _local_flow(false),
kpeter@2581
   310
      _potential(0), _local_potential(false), _res_cost(_cost),
kpeter@2581
   311
      _excess(graph, 0)
kpeter@2577
   312
    {
kpeter@2577
   313
      _supply[s] =  flow_value;
kpeter@2577
   314
      _supply[t] = -flow_value;
kpeter@2577
   315
      _valid_supply = true;
kpeter@2577
   316
    }
kpeter@2577
   317
kpeter@2581
   318
    /// Destructor.
kpeter@2581
   319
    ~CostScaling() {
kpeter@2581
   320
      if (_local_flow) delete _flow;
kpeter@2581
   321
      if (_local_potential) delete _potential;
kpeter@2581
   322
      delete _res_graph;
kpeter@2581
   323
      delete _red_cost;
kpeter@2581
   324
    }
kpeter@2581
   325
kpeter@2620
   326
    /// \brief Set the flow map.
kpeter@2581
   327
    ///
kpeter@2620
   328
    /// Set the flow map.
kpeter@2581
   329
    ///
kpeter@2581
   330
    /// \return \c (*this)
kpeter@2581
   331
    CostScaling& flowMap(FlowMap &map) {
kpeter@2581
   332
      if (_local_flow) {
kpeter@2581
   333
        delete _flow;
kpeter@2581
   334
        _local_flow = false;
kpeter@2581
   335
      }
kpeter@2581
   336
      _flow = &map;
kpeter@2581
   337
      return *this;
kpeter@2581
   338
    }
kpeter@2581
   339
kpeter@2620
   340
    /// \brief Set the potential map.
kpeter@2581
   341
    ///
kpeter@2620
   342
    /// Set the potential map.
kpeter@2581
   343
    ///
kpeter@2581
   344
    /// \return \c (*this)
kpeter@2581
   345
    CostScaling& potentialMap(PotentialMap &map) {
kpeter@2581
   346
      if (_local_potential) {
kpeter@2581
   347
        delete _potential;
kpeter@2581
   348
        _local_potential = false;
kpeter@2581
   349
      }
kpeter@2581
   350
      _potential = &map;
kpeter@2581
   351
      return *this;
kpeter@2581
   352
    }
kpeter@2581
   353
kpeter@2581
   354
    /// \name Execution control
kpeter@2581
   355
kpeter@2581
   356
    /// @{
kpeter@2581
   357
kpeter@2620
   358
    /// \brief Run the algorithm.
kpeter@2577
   359
    ///
kpeter@2620
   360
    /// Run the algorithm.
kpeter@2577
   361
    ///
kpeter@2577
   362
    /// \return \c true if a feasible flow can be found.
kpeter@2577
   363
    bool run() {
kpeter@2581
   364
      return init() && start();
kpeter@2577
   365
    }
kpeter@2577
   366
kpeter@2581
   367
    /// @}
kpeter@2581
   368
kpeter@2581
   369
    /// \name Query Functions
kpeter@2581
   370
    /// The result of the algorithm can be obtained using these
kpeter@2620
   371
    /// functions.\n
kpeter@2620
   372
    /// \ref lemon::CostScaling::run() "run()" must be called before
kpeter@2620
   373
    /// using them.
kpeter@2581
   374
kpeter@2581
   375
    /// @{
kpeter@2581
   376
kpeter@2620
   377
    /// \brief Return a const reference to the edge map storing the
kpeter@2577
   378
    /// found flow.
kpeter@2577
   379
    ///
kpeter@2620
   380
    /// Return a const reference to the edge map storing the found flow.
kpeter@2577
   381
    ///
kpeter@2577
   382
    /// \pre \ref run() must be called before using this function.
kpeter@2577
   383
    const FlowMap& flowMap() const {
kpeter@2581
   384
      return *_flow;
kpeter@2577
   385
    }
kpeter@2577
   386
kpeter@2620
   387
    /// \brief Return a const reference to the node map storing the
kpeter@2577
   388
    /// found potentials (the dual solution).
kpeter@2577
   389
    ///
kpeter@2620
   390
    /// Return a const reference to the node map storing the found
kpeter@2577
   391
    /// potentials (the dual solution).
kpeter@2577
   392
    ///
kpeter@2577
   393
    /// \pre \ref run() must be called before using this function.
kpeter@2577
   394
    const PotentialMap& potentialMap() const {
kpeter@2581
   395
      return *_potential;
kpeter@2581
   396
    }
kpeter@2581
   397
kpeter@2620
   398
    /// \brief Return the flow on the given edge.
kpeter@2581
   399
    ///
kpeter@2620
   400
    /// Return the flow on the given edge.
kpeter@2581
   401
    ///
kpeter@2581
   402
    /// \pre \ref run() must be called before using this function.
kpeter@2581
   403
    Capacity flow(const Edge& edge) const {
kpeter@2581
   404
      return (*_flow)[edge];
kpeter@2581
   405
    }
kpeter@2581
   406
kpeter@2620
   407
    /// \brief Return the potential of the given node.
kpeter@2581
   408
    ///
kpeter@2620
   409
    /// Return the potential of the given node.
kpeter@2581
   410
    ///
kpeter@2581
   411
    /// \pre \ref run() must be called before using this function.
kpeter@2581
   412
    Cost potential(const Node& node) const {
kpeter@2581
   413
      return (*_potential)[node];
kpeter@2577
   414
    }
kpeter@2577
   415
kpeter@2620
   416
    /// \brief Return the total cost of the found flow.
kpeter@2577
   417
    ///
kpeter@2620
   418
    /// Return the total cost of the found flow. The complexity of the
kpeter@2577
   419
    /// function is \f$ O(e) \f$.
kpeter@2577
   420
    ///
kpeter@2577
   421
    /// \pre \ref run() must be called before using this function.
kpeter@2577
   422
    Cost totalCost() const {
kpeter@2577
   423
      Cost c = 0;
kpeter@2577
   424
      for (EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2581
   425
        c += (*_flow)[e] * _orig_cost[e];
kpeter@2577
   426
      return c;
kpeter@2577
   427
    }
kpeter@2577
   428
kpeter@2581
   429
    /// @}
kpeter@2581
   430
kpeter@2577
   431
  private:
kpeter@2577
   432
kpeter@2620
   433
    /// Initialize the algorithm.
kpeter@2577
   434
    bool init() {
kpeter@2577
   435
      if (!_valid_supply) return false;
kpeter@2577
   436
kpeter@2581
   437
      // Initializing flow and potential maps
kpeter@2581
   438
      if (!_flow) {
kpeter@2581
   439
        _flow = new FlowMap(_graph);
kpeter@2581
   440
        _local_flow = true;
kpeter@2581
   441
      }
kpeter@2581
   442
      if (!_potential) {
kpeter@2581
   443
        _potential = new PotentialMap(_graph);
kpeter@2581
   444
        _local_potential = true;
kpeter@2581
   445
      }
kpeter@2581
   446
kpeter@2581
   447
      _red_cost = new ReducedCostMap(_graph, _cost, *_potential);
kpeter@2581
   448
      _res_graph = new ResGraph(_graph, _capacity, *_flow);
kpeter@2581
   449
kpeter@2577
   450
      // Initializing the scaled cost map and the epsilon parameter
kpeter@2577
   451
      Cost max_cost = 0;
kpeter@2577
   452
      int node_num = countNodes(_graph);
kpeter@2577
   453
      for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@2577
   454
        _cost[e] = LCost(_orig_cost[e]) * node_num * ALPHA;
kpeter@2577
   455
        if (_orig_cost[e] > max_cost) max_cost = _orig_cost[e];
kpeter@2577
   456
      }
kpeter@2577
   457
      _epsilon = max_cost * node_num;
kpeter@2577
   458
kpeter@2577
   459
      // Finding a feasible flow using Circulation
kpeter@2577
   460
      Circulation< Graph, ConstMap<Edge, Capacity>, CapacityEdgeMap,
kpeter@2577
   461
                   SupplyMap >
kpeter@2581
   462
        circulation( _graph, constMap<Edge>(Capacity(0)), _capacity,
kpeter@2577
   463
                     _supply );
kpeter@2581
   464
      return circulation.flowMap(*_flow).run();
kpeter@2577
   465
    }
kpeter@2577
   466
kpeter@2577
   467
kpeter@2620
   468
    /// Execute the algorithm.
kpeter@2577
   469
    bool start() {
kpeter@2577
   470
      std::deque<Node> active_nodes;
kpeter@2577
   471
      typename Graph::template NodeMap<bool> hyper(_graph, false);
kpeter@2577
   472
kpeter@2577
   473
      int node_num = countNodes(_graph);
kpeter@2577
   474
      for ( ; _epsilon >= 1; _epsilon = _epsilon < ALPHA && _epsilon > 1 ?
kpeter@2577
   475
                                        1 : _epsilon / ALPHA )
kpeter@2577
   476
      {
kpeter@2577
   477
        // Performing price refinement heuristic using Bellman-Ford
kpeter@2577
   478
        // algorithm
kpeter@2577
   479
        if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
kpeter@2581
   480
          typedef ShiftMap< ResidualCostMap<LargeCostMap> > ShiftCostMap;
kpeter@2577
   481
          ShiftCostMap shift_cost(_res_cost, _epsilon);
kpeter@2581
   482
          BellmanFord<ResGraph, ShiftCostMap> bf(*_res_graph, shift_cost);
kpeter@2577
   483
          bf.init(0);
kpeter@2577
   484
          bool done = false;
kpeter@2577
   485
          int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(node_num));
kpeter@2577
   486
          for (int i = 0; i < K && !done; ++i)
kpeter@2577
   487
            done = bf.processNextWeakRound();
kpeter@2577
   488
          if (done) {
kpeter@2577
   489
            for (NodeIt n(_graph); n != INVALID; ++n)
kpeter@2581
   490
              (*_potential)[n] = bf.dist(n);
kpeter@2577
   491
            continue;
kpeter@2577
   492
          }
kpeter@2577
   493
        }
kpeter@2577
   494
kpeter@2577
   495
        // Saturating edges not satisfying the optimality condition
kpeter@2577
   496
        Capacity delta;
kpeter@2577
   497
        for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@2581
   498
          if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
kpeter@2581
   499
            delta = _capacity[e] - (*_flow)[e];
kpeter@2577
   500
            _excess[_graph.source(e)] -= delta;
kpeter@2577
   501
            _excess[_graph.target(e)] += delta;
kpeter@2581
   502
            (*_flow)[e] = _capacity[e];
kpeter@2577
   503
          }
kpeter@2581
   504
          if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
kpeter@2581
   505
            _excess[_graph.target(e)] -= (*_flow)[e];
kpeter@2581
   506
            _excess[_graph.source(e)] += (*_flow)[e];
kpeter@2581
   507
            (*_flow)[e] = 0;
kpeter@2577
   508
          }
kpeter@2577
   509
        }
kpeter@2577
   510
kpeter@2577
   511
        // Finding active nodes (i.e. nodes with positive excess)
kpeter@2577
   512
        for (NodeIt n(_graph); n != INVALID; ++n)
kpeter@2577
   513
          if (_excess[n] > 0) active_nodes.push_back(n);
kpeter@2577
   514
kpeter@2577
   515
        // Performing push and relabel operations
kpeter@2577
   516
        while (active_nodes.size() > 0) {
kpeter@2577
   517
          Node n = active_nodes[0], t;
kpeter@2577
   518
          bool relabel_enabled = true;
kpeter@2577
   519
kpeter@2577
   520
          // Performing push operations if there are admissible edges
kpeter@2577
   521
          if (_excess[n] > 0) {
kpeter@2577
   522
            for (OutEdgeIt e(_graph, n); e != INVALID; ++e) {
kpeter@2581
   523
              if (_capacity[e] - (*_flow)[e] > 0 && (*_red_cost)[e] < 0) {
kpeter@2581
   524
                delta = _capacity[e] - (*_flow)[e] <= _excess[n] ?
kpeter@2581
   525
                        _capacity[e] - (*_flow)[e] : _excess[n];
kpeter@2577
   526
                t = _graph.target(e);
kpeter@2577
   527
kpeter@2577
   528
                // Push-look-ahead heuristic
kpeter@2577
   529
                Capacity ahead = -_excess[t];
kpeter@2577
   530
                for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
kpeter@2581
   531
                  if (_capacity[oe] - (*_flow)[oe] > 0 && (*_red_cost)[oe] < 0)
kpeter@2581
   532
                    ahead += _capacity[oe] - (*_flow)[oe];
kpeter@2577
   533
                }
kpeter@2577
   534
                for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
kpeter@2581
   535
                  if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < 0)
kpeter@2581
   536
                    ahead += (*_flow)[ie];
kpeter@2577
   537
                }
kpeter@2577
   538
                if (ahead < 0) ahead = 0;
kpeter@2577
   539
kpeter@2577
   540
                // Pushing flow along the edge
kpeter@2577
   541
                if (ahead < delta) {
kpeter@2581
   542
                  (*_flow)[e] += ahead;
kpeter@2577
   543
                  _excess[n] -= ahead;
kpeter@2577
   544
                  _excess[t] += ahead;
kpeter@2577
   545
                  active_nodes.push_front(t);
kpeter@2577
   546
                  hyper[t] = true;
kpeter@2577
   547
                  relabel_enabled = false;
kpeter@2577
   548
                  break;
kpeter@2577
   549
                } else {
kpeter@2581
   550
                  (*_flow)[e] += delta;
kpeter@2577
   551
                  _excess[n] -= delta;
kpeter@2577
   552
                  _excess[t] += delta;
kpeter@2577
   553
                  if (_excess[t] > 0 && _excess[t] <= delta)
kpeter@2577
   554
                    active_nodes.push_back(t);
kpeter@2577
   555
                }
kpeter@2577
   556
kpeter@2577
   557
                if (_excess[n] == 0) break;
kpeter@2577
   558
              }
kpeter@2577
   559
            }
kpeter@2577
   560
          }
kpeter@2577
   561
kpeter@2577
   562
          if (_excess[n] > 0) {
kpeter@2577
   563
            for (InEdgeIt e(_graph, n); e != INVALID; ++e) {
kpeter@2581
   564
              if ((*_flow)[e] > 0 && -(*_red_cost)[e] < 0) {
kpeter@2581
   565
                delta = (*_flow)[e] <= _excess[n] ? (*_flow)[e] : _excess[n];
kpeter@2577
   566
                t = _graph.source(e);
kpeter@2577
   567
kpeter@2577
   568
                // Push-look-ahead heuristic
kpeter@2577
   569
                Capacity ahead = -_excess[t];
kpeter@2577
   570
                for (OutEdgeIt oe(_graph, t); oe != INVALID; ++oe) {
kpeter@2581
   571
                  if (_capacity[oe] - (*_flow)[oe] > 0 && (*_red_cost)[oe] < 0)
kpeter@2581
   572
                    ahead += _capacity[oe] - (*_flow)[oe];
kpeter@2577
   573
                }
kpeter@2577
   574
                for (InEdgeIt ie(_graph, t); ie != INVALID; ++ie) {
kpeter@2581
   575
                  if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < 0)
kpeter@2581
   576
                    ahead += (*_flow)[ie];
kpeter@2577
   577
                }
kpeter@2577
   578
                if (ahead < 0) ahead = 0;
kpeter@2577
   579
kpeter@2577
   580
                // Pushing flow along the edge
kpeter@2577
   581
                if (ahead < delta) {
kpeter@2581
   582
                  (*_flow)[e] -= ahead;
kpeter@2577
   583
                  _excess[n] -= ahead;
kpeter@2577
   584
                  _excess[t] += ahead;
kpeter@2577
   585
                  active_nodes.push_front(t);
kpeter@2577
   586
                  hyper[t] = true;
kpeter@2577
   587
                  relabel_enabled = false;
kpeter@2577
   588
                  break;
kpeter@2577
   589
                } else {
kpeter@2581
   590
                  (*_flow)[e] -= delta;
kpeter@2577
   591
                  _excess[n] -= delta;
kpeter@2577
   592
                  _excess[t] += delta;
kpeter@2577
   593
                  if (_excess[t] > 0 && _excess[t] <= delta)
kpeter@2577
   594
                    active_nodes.push_back(t);
kpeter@2577
   595
                }
kpeter@2577
   596
kpeter@2577
   597
                if (_excess[n] == 0) break;
kpeter@2577
   598
              }
kpeter@2577
   599
            }
kpeter@2577
   600
          }
kpeter@2577
   601
kpeter@2577
   602
          if (relabel_enabled && (_excess[n] > 0 || hyper[n])) {
kpeter@2577
   603
            // Performing relabel operation if the node is still active
kpeter@2577
   604
            LCost min_red_cost = std::numeric_limits<LCost>::max();
kpeter@2577
   605
            for (OutEdgeIt oe(_graph, n); oe != INVALID; ++oe) {
kpeter@2581
   606
              if ( _capacity[oe] - (*_flow)[oe] > 0 &&
kpeter@2581
   607
                   (*_red_cost)[oe] < min_red_cost )
kpeter@2581
   608
                min_red_cost = (*_red_cost)[oe];
kpeter@2577
   609
            }
kpeter@2577
   610
            for (InEdgeIt ie(_graph, n); ie != INVALID; ++ie) {
kpeter@2581
   611
              if ((*_flow)[ie] > 0 && -(*_red_cost)[ie] < min_red_cost)
kpeter@2581
   612
                min_red_cost = -(*_red_cost)[ie];
kpeter@2577
   613
            }
kpeter@2581
   614
            (*_potential)[n] -= min_red_cost + _epsilon;
kpeter@2577
   615
            hyper[n] = false;
kpeter@2577
   616
          }
kpeter@2577
   617
kpeter@2577
   618
          // Removing active nodes with non-positive excess
kpeter@2577
   619
          while ( active_nodes.size() > 0 &&
kpeter@2577
   620
                  _excess[active_nodes[0]] <= 0 &&
kpeter@2577
   621
                  !hyper[active_nodes[0]] ) {
kpeter@2577
   622
            active_nodes.pop_front();
kpeter@2577
   623
          }
kpeter@2577
   624
        }
kpeter@2577
   625
      }
kpeter@2577
   626
kpeter@2581
   627
      // Computing node potentials for the original costs
kpeter@2581
   628
      ResidualCostMap<CostMap> res_cost(_orig_cost);
kpeter@2581
   629
      BellmanFord< ResGraph, ResidualCostMap<CostMap> >
kpeter@2581
   630
        bf(*_res_graph, res_cost);
kpeter@2581
   631
      bf.init(0); bf.start();
kpeter@2581
   632
      for (NodeIt n(_graph); n != INVALID; ++n)
kpeter@2581
   633
        (*_potential)[n] = bf.dist(n);
kpeter@2581
   634
kpeter@2577
   635
      // Handling non-zero lower bounds
kpeter@2577
   636
      if (_lower) {
kpeter@2577
   637
        for (EdgeIt e(_graph); e != INVALID; ++e)
kpeter@2581
   638
          (*_flow)[e] += (*_lower)[e];
kpeter@2577
   639
      }
kpeter@2577
   640
      return true;
kpeter@2577
   641
    }
kpeter@2577
   642
kpeter@2577
   643
  }; //class CostScaling
kpeter@2577
   644
kpeter@2577
   645
  ///@}
kpeter@2577
   646
kpeter@2577
   647
} //namespace lemon
kpeter@2577
   648
kpeter@2577
   649
#endif //LEMON_COST_SCALING_H