lemon/capacity_scaling.h
changeset 810 3b53491bf643
parent 807 78071e00de00
child 811 fe80a8145653
equal deleted inserted replaced
2:5979e08ce50b 3:598654f6b73b
   171     ValueVector _excess;
   171     ValueVector _excess;
   172     IntVector _excess_nodes;
   172     IntVector _excess_nodes;
   173     IntVector _deficit_nodes;
   173     IntVector _deficit_nodes;
   174 
   174 
   175     Value _delta;
   175     Value _delta;
   176     int _phase_num;
   176     int _factor;
   177     IntVector _pred;
   177     IntVector _pred;
   178 
   178 
   179   public:
   179   public:
   180   
   180   
   181     /// \brief Constant for infinite upper bounds (capacities).
   181     /// \brief Constant for infinite upper bounds (capacities).
   511     /// This function can be called more than once. All the parameters
   511     /// This function can be called more than once. All the parameters
   512     /// that have been given are kept for the next call, unless
   512     /// that have been given are kept for the next call, unless
   513     /// \ref reset() is called, thus only the modified parameters
   513     /// \ref reset() is called, thus only the modified parameters
   514     /// have to be set again. See \ref reset() for examples.
   514     /// have to be set again. See \ref reset() for examples.
   515     /// However the underlying digraph must not be modified after this
   515     /// However the underlying digraph must not be modified after this
   516     /// class have been constructed, since it copies the digraph.
   516     /// class have been constructed, since it copies and extends the graph.
   517     ///
   517     ///
   518     /// \param scaling Enable or disable capacity scaling.
   518     /// \param factor The capacity scaling factor. It must be larger than
   519     /// If the maximum upper bound and/or the amount of total supply
   519     /// one to use scaling. If it is less or equal to one, then scaling
   520     /// is rather small, the algorithm could be slightly faster without
   520     /// will be disabled.
   521     /// scaling.
       
   522     ///
   521     ///
   523     /// \return \c INFEASIBLE if no feasible flow exists,
   522     /// \return \c INFEASIBLE if no feasible flow exists,
   524     /// \n \c OPTIMAL if the problem has optimal solution
   523     /// \n \c OPTIMAL if the problem has optimal solution
   525     /// (i.e. it is feasible and bounded), and the algorithm has found
   524     /// (i.e. it is feasible and bounded), and the algorithm has found
   526     /// optimal flow and node potentials (primal and dual solutions),
   525     /// optimal flow and node potentials (primal and dual solutions),
   529     /// is unbounded on that arc, however note that it could actually be
   528     /// is unbounded on that arc, however note that it could actually be
   530     /// bounded over the feasible flows, but this algroithm cannot handle
   529     /// bounded over the feasible flows, but this algroithm cannot handle
   531     /// these cases.
   530     /// these cases.
   532     ///
   531     ///
   533     /// \see ProblemType
   532     /// \see ProblemType
   534     ProblemType run(bool scaling = true) {
   533     ProblemType run(int factor = 4) {
   535       ProblemType pt = init(scaling);
   534       _factor = factor;
       
   535       ProblemType pt = init();
   536       if (pt != OPTIMAL) return pt;
   536       if (pt != OPTIMAL) return pt;
   537       return start();
   537       return start();
   538     }
   538     }
   539 
   539 
   540     /// \brief Reset all the parameters that have been given before.
   540     /// \brief Reset all the parameters that have been given before.
   544     /// \ref costMap(), \ref supplyMap(), \ref stSupply().
   544     /// \ref costMap(), \ref supplyMap(), \ref stSupply().
   545     ///
   545     ///
   546     /// It is useful for multiple run() calls. If this function is not
   546     /// It is useful for multiple run() calls. If this function is not
   547     /// used, all the parameters given before are kept for the next
   547     /// used, all the parameters given before are kept for the next
   548     /// \ref run() call.
   548     /// \ref run() call.
   549     /// However the underlying digraph must not be modified after this
   549     /// However, the underlying digraph must not be modified after this
   550     /// class have been constructed, since it copies and extends the graph.
   550     /// class have been constructed, since it copies and extends the graph.
   551     ///
   551     ///
   552     /// For example,
   552     /// For example,
   553     /// \code
   553     /// \code
   554     ///   CapacityScaling<ListDigraph> cs(graph);
   554     ///   CapacityScaling<ListDigraph> cs(graph);
   675     /// @}
   675     /// @}
   676 
   676 
   677   private:
   677   private:
   678 
   678 
   679     // Initialize the algorithm
   679     // Initialize the algorithm
   680     ProblemType init(bool scaling) {
   680     ProblemType init() {
   681       if (_node_num == 0) return INFEASIBLE;
   681       if (_node_num == 0) return INFEASIBLE;
   682 
   682 
   683       // Check the sum of supply values
   683       // Check the sum of supply values
   684       _sum_supply = 0;
   684       _sum_supply = 0;
   685       for (int i = 0; i != _root; ++i) {
   685       for (int i = 0; i != _root; ++i) {
   756           _cost[_reverse[a]] = 0;
   756           _cost[_reverse[a]] = 0;
   757         }
   757         }
   758       }
   758       }
   759 
   759 
   760       // Initialize delta value
   760       // Initialize delta value
   761       if (scaling) {
   761       if (_factor > 1) {
   762         // With scaling
   762         // With scaling
   763         Value max_sup = 0, max_dem = 0;
   763         Value max_sup = 0, max_dem = 0;
   764         for (int i = 0; i != _node_num; ++i) {
   764         for (int i = 0; i != _node_num; ++i) {
   765           if ( _excess[i] > max_sup) max_sup =  _excess[i];
   765           if ( _excess[i] > max_sup) max_sup =  _excess[i];
   766           if (-_excess[i] > max_dem) max_dem = -_excess[i];
   766           if (-_excess[i] > max_dem) max_dem = -_excess[i];
   768         Value max_cap = 0;
   768         Value max_cap = 0;
   769         for (int j = 0; j != _res_arc_num; ++j) {
   769         for (int j = 0; j != _res_arc_num; ++j) {
   770           if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
   770           if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
   771         }
   771         }
   772         max_sup = std::min(std::min(max_sup, max_dem), max_cap);
   772         max_sup = std::min(std::min(max_sup, max_dem), max_cap);
   773         _phase_num = 0;
   773         for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ;
   774         for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2)
       
   775           ++_phase_num;
       
   776       } else {
   774       } else {
   777         // Without scaling
   775         // Without scaling
   778         _delta = 1;
   776         _delta = 1;
   779       }
   777       }
   780 
   778 
   809 
   807 
   810     // Execute the capacity scaling algorithm
   808     // Execute the capacity scaling algorithm
   811     ProblemType startWithScaling() {
   809     ProblemType startWithScaling() {
   812       // Perform capacity scaling phases
   810       // Perform capacity scaling phases
   813       int s, t;
   811       int s, t;
   814       int phase_cnt = 0;
       
   815       int factor = 4;
       
   816       ResidualDijkstra _dijkstra(*this);
   812       ResidualDijkstra _dijkstra(*this);
   817       while (true) {
   813       while (true) {
   818         // Saturate all arcs not satisfying the optimality condition
   814         // Saturate all arcs not satisfying the optimality condition
   819         for (int u = 0; u != _node_num; ++u) {
   815         for (int u = 0; u != _node_num; ++u) {
   820           for (int a = _first_out[u]; a != _first_out[u+1]; ++a) {
   816           for (int a = _first_out[u]; a != _first_out[u+1]; ++a) {
   885 
   881 
   886           if (_excess[s] < _delta) ++next_node;
   882           if (_excess[s] < _delta) ++next_node;
   887         }
   883         }
   888 
   884 
   889         if (_delta == 1) break;
   885         if (_delta == 1) break;
   890         if (++phase_cnt == _phase_num / 4) factor = 2;
   886         _delta = _delta <= _factor ? 1 : _delta / _factor;
   891         _delta = _delta <= factor ? 1 : _delta / factor;
       
   892       }
   887       }
   893 
   888 
   894       return OPTIMAL;
   889       return OPTIMAL;
   895     }
   890     }
   896 
   891