lemon/network_simplex.h
changeset 606 c7d160f73d52
parent 605 5232721b3f14
child 607 9ad8d2122b50
     1.1 --- a/lemon/network_simplex.h	Wed Mar 25 15:58:44 2009 +0100
     1.2 +++ b/lemon/network_simplex.h	Wed Mar 25 21:37:50 2009 +0100
     1.3 @@ -41,12 +41,18 @@
     1.4    ///
     1.5    /// \ref NetworkSimplex implements the primal Network Simplex algorithm
     1.6    /// for finding a \ref min_cost_flow "minimum cost flow".
     1.7 +  /// This algorithm is a specialized version of the linear programming
     1.8 +  /// simplex method directly for the minimum cost flow problem.
     1.9 +  /// It is one of the most efficient solution methods.
    1.10 +  ///
    1.11 +  /// In general this class is the fastest implementation available
    1.12 +  /// in LEMON for the minimum cost flow problem.
    1.13    ///
    1.14    /// \tparam GR The digraph type the algorithm runs on.
    1.15    /// \tparam V The value type used in the algorithm.
    1.16    /// By default it is \c int.
    1.17    ///
    1.18 -  /// \warning \c V must be a signed integer type.
    1.19 +  /// \warning The value type must be a signed integer type.
    1.20    ///
    1.21    /// \note %NetworkSimplex provides five different pivot rule
    1.22    /// implementations. For more information see \ref PivotRule.
    1.23 @@ -789,7 +795,7 @@
    1.24      ///
    1.25      /// This function runs the algorithm.
    1.26      /// The paramters can be specified using \ref lowerMap(),
    1.27 -    /// \ref upperMap(), \ref capacityMap(), \ref boundMaps(), 
    1.28 +    /// \ref upperMap(), \ref capacityMap(), \ref boundMaps(),
    1.29      /// \ref costMap(), \ref supplyMap() and \ref stSupply()
    1.30      /// functions. For example,
    1.31      /// \code
    1.32 @@ -798,6 +804,11 @@
    1.33      ///     .supplyMap(sup).run();
    1.34      /// \endcode
    1.35      ///
    1.36 +    /// This function can be called more than once. All the parameters
    1.37 +    /// that have been given are kept for the next call, unless
    1.38 +    /// \ref reset() is called, thus only the modified parameters
    1.39 +    /// have to be set again. See \ref reset() for examples.
    1.40 +    ///
    1.41      /// \param pivot_rule The pivot rule that will be used during the
    1.42      /// algorithm. For more information see \ref PivotRule.
    1.43      ///
    1.44 @@ -806,6 +817,51 @@
    1.45        return init() && start(pivot_rule);
    1.46      }
    1.47  
    1.48 +    /// \brief Reset all the parameters that have been given before.
    1.49 +    ///
    1.50 +    /// This function resets all the paramaters that have been given
    1.51 +    /// using \ref lowerMap(), \ref upperMap(), \ref capacityMap(),
    1.52 +    /// \ref boundMaps(), \ref costMap(), \ref supplyMap() and
    1.53 +    /// \ref stSupply() functions before.
    1.54 +    ///
    1.55 +    /// It is useful for multiple run() calls. If this function is not
    1.56 +    /// used, all the parameters given before are kept for the next
    1.57 +    /// \ref run() call.
    1.58 +    ///
    1.59 +    /// For example,
    1.60 +    /// \code
    1.61 +    ///   NetworkSimplex<ListDigraph> ns(graph);
    1.62 +    ///
    1.63 +    ///   // First run
    1.64 +    ///   ns.lowerMap(lower).capacityMap(cap).costMap(cost)
    1.65 +    ///     .supplyMap(sup).run();
    1.66 +    ///
    1.67 +    ///   // Run again with modified cost map (reset() is not called,
    1.68 +    ///   // so only the cost map have to be set again)
    1.69 +    ///   cost[e] += 100;
    1.70 +    ///   ns.costMap(cost).run();
    1.71 +    ///
    1.72 +    ///   // Run again from scratch using reset()
    1.73 +    ///   // (the lower bounds will be set to zero on all arcs)
    1.74 +    ///   ns.reset();
    1.75 +    ///   ns.capacityMap(cap).costMap(cost)
    1.76 +    ///     .supplyMap(sup).run();
    1.77 +    /// \endcode
    1.78 +    ///
    1.79 +    /// \return <tt>(*this)</tt>
    1.80 +    NetworkSimplex& reset() {
    1.81 +      delete _plower;
    1.82 +      delete _pupper;
    1.83 +      delete _pcost;
    1.84 +      delete _psupply;
    1.85 +      _plower = NULL;
    1.86 +      _pupper = NULL;
    1.87 +      _pcost = NULL;
    1.88 +      _psupply = NULL;
    1.89 +      _pstsup = false;
    1.90 +      return *this;
    1.91 +    }
    1.92 +
    1.93      /// @}
    1.94  
    1.95      /// \name Query Functions
    1.96 @@ -920,8 +976,8 @@
    1.97        _cap.resize(all_arc_num);
    1.98        _cost.resize(all_arc_num);
    1.99        _supply.resize(all_node_num);
   1.100 -      _flow.resize(all_arc_num, 0);
   1.101 -      _pi.resize(all_node_num, 0);
   1.102 +      _flow.resize(all_arc_num);
   1.103 +      _pi.resize(all_node_num);
   1.104  
   1.105        _parent.resize(all_node_num);
   1.106        _pred.resize(all_node_num);
   1.107 @@ -930,7 +986,7 @@
   1.108        _rev_thread.resize(all_node_num);
   1.109        _succ_num.resize(all_node_num);
   1.110        _last_succ.resize(all_node_num);
   1.111 -      _state.resize(all_arc_num, STATE_LOWER);
   1.112 +      _state.resize(all_arc_num);
   1.113  
   1.114        // Initialize node related data
   1.115        bool valid_supply = true;
   1.116 @@ -986,12 +1042,16 @@
   1.117            _target[i] = _node_id[_graph.target(e)];
   1.118            _cap[i] = (*_pupper)[e];
   1.119            _cost[i] = (*_pcost)[e];
   1.120 +          _flow[i] = 0;
   1.121 +          _state[i] = STATE_LOWER;
   1.122          }
   1.123        } else {
   1.124          for (int i = 0; i != _arc_num; ++i) {
   1.125            Arc e = _arc_ref[i];
   1.126            _source[i] = _node_id[_graph.source(e)];
   1.127            _target[i] = _node_id[_graph.target(e)];
   1.128 +          _flow[i] = 0;
   1.129 +          _state[i] = STATE_LOWER;
   1.130          }
   1.131          if (_pupper) {
   1.132            for (int i = 0; i != _arc_num; ++i)
   1.133 @@ -1032,6 +1092,9 @@
   1.134          _last_succ[u] = u;
   1.135          _parent[u] = _root;
   1.136          _pred[u] = e;
   1.137 +        _cost[e] = max_cost;
   1.138 +        _cap[e] = max_cap;
   1.139 +        _state[e] = STATE_TREE;
   1.140          if (_supply[u] >= 0) {
   1.141            _flow[e] = _supply[u];
   1.142            _forward[u] = true;
   1.143 @@ -1041,9 +1104,6 @@
   1.144            _forward[u] = false;
   1.145            _pi[u] = max_cost;
   1.146          }
   1.147 -        _cost[e] = max_cost;
   1.148 -        _cap[e] = max_cap;
   1.149 -        _state[e] = STATE_TREE;
   1.150        }
   1.151  
   1.152        return true;