gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
New heuristics for MCF algorithms (#340) and some implementation improvements. - A useful heuristic is added to NetworkSimplex to make the initial pivots faster. - A powerful global update heuristic is added to CostScaling and the implementation is reworked with various improvements. - Better relabeling in CostScaling to improve numerical stability and make the code faster. - A small improvement is made in CapacityScaling for better delta computation. - Add notes to the classes about the usage of vector<char> instead of vector<bool> for efficiency reasons.
0 4 0
default
4 files changed with 377 insertions and 161 deletions:
↑ Collapse diff ↑
Ignore white space 24 line context
... ...
@@ -125,27 +125,28 @@
125 125
      /// upper bound. It means that the objective function is unbounded
126 126
      /// on that arc, however, note that it could actually be bounded
127 127
      /// over the feasible flows, but this algroithm cannot handle
128 128
      /// these cases.
129 129
      UNBOUNDED
130 130
    };
131 131
  
132 132
  private:
133 133

	
134 134
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
135 135

	
136 136
    typedef std::vector<int> IntVector;
137
    typedef std::vector<char> BoolVector;
138 137
    typedef std::vector<Value> ValueVector;
139 138
    typedef std::vector<Cost> CostVector;
139
    typedef std::vector<char> BoolVector;
140
    // Note: vector<char> is used instead of vector<bool> for efficiency reasons
140 141

	
141 142
  private:
142 143

	
143 144
    // Data related to the underlying digraph
144 145
    const GR &_graph;
145 146
    int _node_num;
146 147
    int _arc_num;
147 148
    int _res_arc_num;
148 149
    int _root;
149 150

	
150 151
    // Parameters of the problem
151 152
    bool _have_lower;
... ...
@@ -755,33 +756,33 @@
755 756
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
756 757
          int ra = _reverse[a];
757 758
          _res_cap[a] = 1;
758 759
          _res_cap[ra] = 0;
759 760
          _cost[a] = 0;
760 761
          _cost[ra] = 0;
761 762
        }
762 763
      }
763 764

	
764 765
      // Initialize delta value
765 766
      if (_factor > 1) {
766 767
        // With scaling
767
        Value max_sup = 0, max_dem = 0;
768
        for (int i = 0; i != _node_num; ++i) {
768
        Value max_sup = 0, max_dem = 0, max_cap = 0;
769
        for (int i = 0; i != _root; ++i) {
769 770
          Value ex = _excess[i];
770 771
          if ( ex > max_sup) max_sup =  ex;
771 772
          if (-ex > max_dem) max_dem = -ex;
772
        }
773
        Value max_cap = 0;
774
        for (int j = 0; j != _res_arc_num; ++j) {
775
          if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
773
          int last_out = _first_out[i+1] - 1;
774
          for (int j = _first_out[i]; j != last_out; ++j) {
775
            if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
776
          }
776 777
        }
777 778
        max_sup = std::min(std::min(max_sup, max_dem), max_cap);
778 779
        for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ;
779 780
      } else {
780 781
        // Without scaling
781 782
        _delta = 1;
782 783
      }
783 784

	
784 785
      return OPTIMAL;
785 786
    }
786 787

	
787 788
    ProblemType start() {
Ignore white space 24 line context
... ...
@@ -188,28 +188,29 @@
188 188
      /// Partial augment operations are used, i.e. flow is moved on 
189 189
      /// admissible paths started from a node with excess, but the
190 190
      /// lengths of these paths are limited. This method can be viewed
191 191
      /// as a combined version of the previous two operations.
192 192
      PARTIAL_AUGMENT
193 193
    };
194 194

	
195 195
  private:
196 196

	
197 197
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
198 198

	
199 199
    typedef std::vector<int> IntVector;
200
    typedef std::vector<char> BoolVector;
201 200
    typedef std::vector<Value> ValueVector;
202 201
    typedef std::vector<Cost> CostVector;
203 202
    typedef std::vector<LargeCost> LargeCostVector;
203
    typedef std::vector<char> BoolVector;
204
    // Note: vector<char> is used instead of vector<bool> for efficiency reasons
204 205

	
205 206
  private:
206 207
  
207 208
    template <typename KT, typename VT>
208 209
    class StaticVectorMap {
209 210
    public:
210 211
      typedef KT Key;
211 212
      typedef VT Value;
212 213
      
213 214
      StaticVectorMap(std::vector<Value>& v) : _v(v) {}
214 215
      
215 216
      const Value& operator[](const Key& key) const {
... ...
@@ -235,24 +236,25 @@
235 236

	
236 237
    // Data related to the underlying digraph
237 238
    const GR &_graph;
238 239
    int _node_num;
239 240
    int _arc_num;
240 241
    int _res_node_num;
241 242
    int _res_arc_num;
242 243
    int _root;
243 244

	
244 245
    // Parameters of the problem
245 246
    bool _have_lower;
246 247
    Value _sum_supply;
248
    int _sup_node_num;
247 249

	
248 250
    // Data structures for storing the digraph
249 251
    IntNodeMap _node_id;
250 252
    IntArcMap _arc_idf;
251 253
    IntArcMap _arc_idb;
252 254
    IntVector _first_out;
253 255
    BoolVector _forward;
254 256
    IntVector _source;
255 257
    IntVector _target;
256 258
    IntVector _reverse;
257 259

	
258 260
    // Node and arc data
... ...
@@ -263,24 +265,30 @@
263 265

	
264 266
    ValueVector _res_cap;
265 267
    LargeCostVector _cost;
266 268
    LargeCostVector _pi;
267 269
    ValueVector _excess;
268 270
    IntVector _next_out;
269 271
    std::deque<int> _active_nodes;
270 272

	
271 273
    // Data for scaling
272 274
    LargeCost _epsilon;
273 275
    int _alpha;
274 276

	
277
    IntVector _buckets;
278
    IntVector _bucket_next;
279
    IntVector _bucket_prev;
280
    IntVector _rank;
281
    int _max_rank;
282
  
275 283
    // Data for a StaticDigraph structure
276 284
    typedef std::pair<int, int> IntPair;
277 285
    StaticDigraph _sgr;
278 286
    std::vector<IntPair> _arc_vec;
279 287
    std::vector<LargeCost> _cost_vec;
280 288
    LargeCostArcMap _cost_map;
281 289
    LargeCostNodeMap _pi_map;
282 290
  
283 291
  public:
284 292
  
285 293
    /// \brief Constant for infinite upper bounds (capacities).
286 294
    ///
... ...
@@ -793,24 +801,29 @@
793 801
          int j = _arc_idf[a];
794 802
          Value c = _lower[j];
795 803
          cap[a] = _upper[j] - c;
796 804
          sup[_graph.source(a)] -= c;
797 805
          sup[_graph.target(a)] += c;
798 806
        }
799 807
      } else {
800 808
        for (ArcIt a(_graph); a != INVALID; ++a) {
801 809
          cap[a] = _upper[_arc_idf[a]];
802 810
        }
803 811
      }
804 812

	
813
      _sup_node_num = 0;
814
      for (NodeIt n(_graph); n != INVALID; ++n) {
815
        if (sup[n] > 0) ++_sup_node_num;
816
      }
817

	
805 818
      // Find a feasible flow using Circulation
806 819
      Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap>
807 820
        circ(_graph, low, cap, sup);
808 821
      if (!circ.flowMap(flow).run()) return INFEASIBLE;
809 822

	
810 823
      // Set residual capacities and handle GEQ supply type
811 824
      if (_sum_supply < 0) {
812 825
        for (ArcIt a(_graph); a != INVALID; ++a) {
813 826
          Value fa = flow[a];
814 827
          _res_cap[_arc_idf[a]] = cap[a] - fa;
815 828
          _res_cap[_arc_idb[a]] = fa;
816 829
          sup[_graph.source(a)] -= fa;
... ...
@@ -827,39 +840,46 @@
827 840
          _cost[a] = 0;
828 841
          _cost[ra] = 0;
829 842
          _excess[u] = 0;
830 843
        }
831 844
      } else {
832 845
        for (ArcIt a(_graph); a != INVALID; ++a) {
833 846
          Value fa = flow[a];
834 847
          _res_cap[_arc_idf[a]] = cap[a] - fa;
835 848
          _res_cap[_arc_idb[a]] = fa;
836 849
        }
837 850
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
838 851
          int ra = _reverse[a];
839
          _res_cap[a] = 1;
852
          _res_cap[a] = 0;
840 853
          _res_cap[ra] = 0;
841 854
          _cost[a] = 0;
842 855
          _cost[ra] = 0;
843 856
        }
844 857
      }
845 858
      
846 859
      return OPTIMAL;
847 860
    }
848 861

	
849 862
    // Execute the algorithm and transform the results
850 863
    void start(Method method) {
851 864
      // Maximum path length for partial augment
852 865
      const int MAX_PATH_LENGTH = 4;
853
      
866

	
867
      // Initialize data structures for buckets      
868
      _max_rank = _alpha * _res_node_num;
869
      _buckets.resize(_max_rank);
870
      _bucket_next.resize(_res_node_num + 1);
871
      _bucket_prev.resize(_res_node_num + 1);
872
      _rank.resize(_res_node_num + 1);
873
  
854 874
      // Execute the algorithm
855 875
      switch (method) {
856 876
        case PUSH:
857 877
          startPush();
858 878
          break;
859 879
        case AUGMENT:
860 880
          startAugment();
861 881
          break;
862 882
        case PARTIAL_AUGMENT:
863 883
          startAugment(MAX_PATH_LENGTH);
864 884
          break;
865 885
      }
... ...
@@ -880,291 +900,387 @@
880 900
      bf.distMap(_pi_map);
881 901
      bf.init(0);
882 902
      bf.start();
883 903

	
884 904
      // Handle non-zero lower bounds
885 905
      if (_have_lower) {
886 906
        int limit = _first_out[_root];
887 907
        for (int j = 0; j != limit; ++j) {
888 908
          if (!_forward[j]) _res_cap[j] += _lower[j];
889 909
        }
890 910
      }
891 911
    }
912
    
913
    // Initialize a cost scaling phase
914
    void initPhase() {
915
      // Saturate arcs not satisfying the optimality condition
916
      for (int u = 0; u != _res_node_num; ++u) {
917
        int last_out = _first_out[u+1];
918
        LargeCost pi_u = _pi[u];
919
        for (int a = _first_out[u]; a != last_out; ++a) {
920
          int v = _target[a];
921
          if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) {
922
            Value delta = _res_cap[a];
923
            _excess[u] -= delta;
924
            _excess[v] += delta;
925
            _res_cap[a] = 0;
926
            _res_cap[_reverse[a]] += delta;
927
          }
928
        }
929
      }
930
      
931
      // Find active nodes (i.e. nodes with positive excess)
932
      for (int u = 0; u != _res_node_num; ++u) {
933
        if (_excess[u] > 0) _active_nodes.push_back(u);
934
      }
935

	
936
      // Initialize the next arcs
937
      for (int u = 0; u != _res_node_num; ++u) {
938
        _next_out[u] = _first_out[u];
939
      }
940
    }
941
    
942
    // Early termination heuristic
943
    bool earlyTermination() {
944
      const double EARLY_TERM_FACTOR = 3.0;
945

	
946
      // Build a static residual graph
947
      _arc_vec.clear();
948
      _cost_vec.clear();
949
      for (int j = 0; j != _res_arc_num; ++j) {
950
        if (_res_cap[j] > 0) {
951
          _arc_vec.push_back(IntPair(_source[j], _target[j]));
952
          _cost_vec.push_back(_cost[j] + 1);
953
        }
954
      }
955
      _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
956

	
957
      // Run Bellman-Ford algorithm to check if the current flow is optimal
958
      BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map);
959
      bf.init(0);
960
      bool done = false;
961
      int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num)));
962
      for (int i = 0; i < K && !done; ++i) {
963
        done = bf.processNextWeakRound();
964
      }
965
      return done;
966
    }
967

	
968
    // Global potential update heuristic
969
    void globalUpdate() {
970
      int bucket_end = _root + 1;
971
    
972
      // Initialize buckets
973
      for (int r = 0; r != _max_rank; ++r) {
974
        _buckets[r] = bucket_end;
975
      }
976
      Value total_excess = 0;
977
      for (int i = 0; i != _res_node_num; ++i) {
978
        if (_excess[i] < 0) {
979
          _rank[i] = 0;
980
          _bucket_next[i] = _buckets[0];
981
          _bucket_prev[_buckets[0]] = i;
982
          _buckets[0] = i;
983
        } else {
984
          total_excess += _excess[i];
985
          _rank[i] = _max_rank;
986
        }
987
      }
988
      if (total_excess == 0) return;
989

	
990
      // Search the buckets
991
      int r = 0;
992
      for ( ; r != _max_rank; ++r) {
993
        while (_buckets[r] != bucket_end) {
994
          // Remove the first node from the current bucket
995
          int u = _buckets[r];
996
          _buckets[r] = _bucket_next[u];
997
          
998
          // Search the incomming arcs of u
999
          LargeCost pi_u = _pi[u];
1000
          int last_out = _first_out[u+1];
1001
          for (int a = _first_out[u]; a != last_out; ++a) {
1002
            int ra = _reverse[a];
1003
            if (_res_cap[ra] > 0) {
1004
              int v = _source[ra];
1005
              int old_rank_v = _rank[v];
1006
              if (r < old_rank_v) {
1007
                // Compute the new rank of v
1008
                LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon;
1009
                int new_rank_v = old_rank_v;
1010
                if (nrc < LargeCost(_max_rank))
1011
                  new_rank_v = r + 1 + int(nrc);
1012
                  
1013
                // Change the rank of v
1014
                if (new_rank_v < old_rank_v) {
1015
                  _rank[v] = new_rank_v;
1016
                  _next_out[v] = _first_out[v];
1017
                  
1018
                  // Remove v from its old bucket
1019
                  if (old_rank_v < _max_rank) {
1020
                    if (_buckets[old_rank_v] == v) {
1021
                      _buckets[old_rank_v] = _bucket_next[v];
1022
                    } else {
1023
                      _bucket_next[_bucket_prev[v]] = _bucket_next[v];
1024
                      _bucket_prev[_bucket_next[v]] = _bucket_prev[v];
1025
                    }
1026
                  }
1027
                  
1028
                  // Insert v to its new bucket
1029
                  _bucket_next[v] = _buckets[new_rank_v];
1030
                  _bucket_prev[_buckets[new_rank_v]] = v;
1031
                  _buckets[new_rank_v] = v;
1032
                }
1033
              }
1034
            }
1035
          }
1036

	
1037
          // Finish search if there are no more active nodes
1038
          if (_excess[u] > 0) {
1039
            total_excess -= _excess[u];
1040
            if (total_excess <= 0) break;
1041
          }
1042
        }
1043
        if (total_excess <= 0) break;
1044
      }
1045
      
1046
      // Relabel nodes
1047
      for (int u = 0; u != _res_node_num; ++u) {
1048
        int k = std::min(_rank[u], r);
1049
        if (k > 0) {
1050
          _pi[u] -= _epsilon * k;
1051
          _next_out[u] = _first_out[u];
1052
        }
1053
      }
1054
    }
892 1055

	
893 1056
    /// Execute the algorithm performing augment and relabel operations
894 1057
    void startAugment(int max_length = std::numeric_limits<int>::max()) {
895 1058
      // Paramters for heuristics
896
      const int BF_HEURISTIC_EPSILON_BOUND = 1000;
897
      const int BF_HEURISTIC_BOUND_FACTOR  = 3;
1059
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
1060
      const double GLOBAL_UPDATE_FACTOR = 3.0;
898 1061

	
1062
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
1063
        (_res_node_num + _sup_node_num * _sup_node_num));
1064
      int next_update_limit = global_update_freq;
1065
      
1066
      int relabel_cnt = 0;
1067
      
899 1068
      // Perform cost scaling phases
900
      IntVector pred_arc(_res_node_num);
901
      std::vector<int> path_nodes;
1069
      std::vector<int> path;
902 1070
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
903 1071
                                        1 : _epsilon / _alpha )
904 1072
      {
905
        // "Early Termination" heuristic: use Bellman-Ford algorithm
906
        // to check if the current flow is optimal
907
        if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
908
          _arc_vec.clear();
909
          _cost_vec.clear();
910
          for (int j = 0; j != _res_arc_num; ++j) {
911
            if (_res_cap[j] > 0) {
912
              _arc_vec.push_back(IntPair(_source[j], _target[j]));
913
              _cost_vec.push_back(_cost[j] + 1);
914
            }
915
          }
916
          _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
917

	
918
          BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map);
919
          bf.init(0);
920
          bool done = false;
921
          int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num));
922
          for (int i = 0; i < K && !done; ++i)
923
            done = bf.processNextWeakRound();
924
          if (done) break;
925
        }
926

	
927
        // Saturate arcs not satisfying the optimality condition
928
        for (int a = 0; a != _res_arc_num; ++a) {
929
          if (_res_cap[a] > 0 &&
930
              _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
931
            Value delta = _res_cap[a];
932
            _excess[_source[a]] -= delta;
933
            _excess[_target[a]] += delta;
934
            _res_cap[a] = 0;
935
            _res_cap[_reverse[a]] += delta;
936
          }
1073
        // Early termination heuristic
1074
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
1075
          if (earlyTermination()) break;
937 1076
        }
938 1077
        
939
        // Find active nodes (i.e. nodes with positive excess)
940
        for (int u = 0; u != _res_node_num; ++u) {
941
          if (_excess[u] > 0) _active_nodes.push_back(u);
942
        }
943

	
944
        // Initialize the next arcs
945
        for (int u = 0; u != _res_node_num; ++u) {
946
          _next_out[u] = _first_out[u];
947
        }
948

	
1078
        // Initialize current phase
1079
        initPhase();
1080
        
949 1081
        // Perform partial augment and relabel operations
950 1082
        while (true) {
951 1083
          // Select an active node (FIFO selection)
952 1084
          while (_active_nodes.size() > 0 &&
953 1085
                 _excess[_active_nodes.front()] <= 0) {
954 1086
            _active_nodes.pop_front();
955 1087
          }
956 1088
          if (_active_nodes.size() == 0) break;
957 1089
          int start = _active_nodes.front();
958
          path_nodes.clear();
959
          path_nodes.push_back(start);
960 1090

	
961 1091
          // Find an augmenting path from the start node
1092
          path.clear();
962 1093
          int tip = start;
963
          while (_excess[tip] >= 0 &&
964
                 int(path_nodes.size()) <= max_length) {
1094
          while (_excess[tip] >= 0 && int(path.size()) < max_length) {
965 1095
            int u;
966
            LargeCost min_red_cost, rc;
967
            int last_out = _sum_supply < 0 ?
968
              _first_out[tip+1] : _first_out[tip+1] - 1;
1096
            LargeCost min_red_cost, rc, pi_tip = _pi[tip];
1097
            int last_out = _first_out[tip+1];
969 1098
            for (int a = _next_out[tip]; a != last_out; ++a) {
970
              if (_res_cap[a] > 0 &&
971
                  _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
972
                u = _target[a];
973
                pred_arc[u] = a;
1099
              u = _target[a];
1100
              if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) {
1101
                path.push_back(a);
974 1102
                _next_out[tip] = a;
975 1103
                tip = u;
976
                path_nodes.push_back(tip);
977 1104
                goto next_step;
978 1105
              }
979 1106
            }
980 1107

	
981 1108
            // Relabel tip node
982
            min_red_cost = std::numeric_limits<LargeCost>::max() / 2;
1109
            min_red_cost = std::numeric_limits<LargeCost>::max();
1110
            if (tip != start) {
1111
              int ra = _reverse[path.back()];
1112
              min_red_cost = _cost[ra] + pi_tip - _pi[_target[ra]];
1113
            }
983 1114
            for (int a = _first_out[tip]; a != last_out; ++a) {
984
              rc = _cost[a] + _pi[_source[a]] - _pi[_target[a]];
1115
              rc = _cost[a] + pi_tip - _pi[_target[a]];
985 1116
              if (_res_cap[a] > 0 && rc < min_red_cost) {
986 1117
                min_red_cost = rc;
987 1118
              }
988 1119
            }
989 1120
            _pi[tip] -= min_red_cost + _epsilon;
990

	
991
            // Reset the next arc of tip
992 1121
            _next_out[tip] = _first_out[tip];
1122
            ++relabel_cnt;
993 1123

	
994 1124
            // Step back
995 1125
            if (tip != start) {
996
              path_nodes.pop_back();
997
              tip = path_nodes.back();
1126
              tip = _source[path.back()];
1127
              path.pop_back();
998 1128
            }
999 1129

	
1000 1130
          next_step: ;
1001 1131
          }
1002 1132

	
1003 1133
          // Augment along the found path (as much flow as possible)
1004 1134
          Value delta;
1005
          int u, v = path_nodes.front(), pa;
1006
          for (int i = 1; i < int(path_nodes.size()); ++i) {
1135
          int pa, u, v = start;
1136
          for (int i = 0; i != int(path.size()); ++i) {
1137
            pa = path[i];
1007 1138
            u = v;
1008
            v = path_nodes[i];
1009
            pa = pred_arc[v];
1139
            v = _target[pa];
1010 1140
            delta = std::min(_res_cap[pa], _excess[u]);
1011 1141
            _res_cap[pa] -= delta;
1012 1142
            _res_cap[_reverse[pa]] += delta;
1013 1143
            _excess[u] -= delta;
1014 1144
            _excess[v] += delta;
1015 1145
            if (_excess[v] > 0 && _excess[v] <= delta)
1016 1146
              _active_nodes.push_back(v);
1017 1147
          }
1148

	
1149
          // Global update heuristic
1150
          if (relabel_cnt >= next_update_limit) {
1151
            globalUpdate();
1152
            next_update_limit += global_update_freq;
1153
          }
1018 1154
        }
1019 1155
      }
1020 1156
    }
1021 1157

	
1022 1158
    /// Execute the algorithm performing push and relabel operations
1023 1159
    void startPush() {
1024 1160
      // Paramters for heuristics
1025
      const int BF_HEURISTIC_EPSILON_BOUND = 1000;
1026
      const int BF_HEURISTIC_BOUND_FACTOR  = 3;
1161
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
1162
      const double GLOBAL_UPDATE_FACTOR = 2.0;
1027 1163

	
1164
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
1165
        (_res_node_num + _sup_node_num * _sup_node_num));
1166
      int next_update_limit = global_update_freq;
1167

	
1168
      int relabel_cnt = 0;
1169
      
1028 1170
      // Perform cost scaling phases
1029 1171
      BoolVector hyper(_res_node_num, false);
1172
      LargeCostVector hyper_cost(_res_node_num);
1030 1173
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
1031 1174
                                        1 : _epsilon / _alpha )
1032 1175
      {
1033
        // "Early Termination" heuristic: use Bellman-Ford algorithm
1034
        // to check if the current flow is optimal
1035
        if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) {
1036
          _arc_vec.clear();
1037
          _cost_vec.clear();
1038
          for (int j = 0; j != _res_arc_num; ++j) {
1039
            if (_res_cap[j] > 0) {
1040
              _arc_vec.push_back(IntPair(_source[j], _target[j]));
1041
              _cost_vec.push_back(_cost[j] + 1);
1042
            }
1043
          }
1044
          _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
1045

	
1046
          BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map);
1047
          bf.init(0);
1048
          bool done = false;
1049
          int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num));
1050
          for (int i = 0; i < K && !done; ++i)
1051
            done = bf.processNextWeakRound();
1052
          if (done) break;
1176
        // Early termination heuristic
1177
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
1178
          if (earlyTermination()) break;
1053 1179
        }
1054

	
1055
        // Saturate arcs not satisfying the optimality condition
1056
        for (int a = 0; a != _res_arc_num; ++a) {
1057
          if (_res_cap[a] > 0 &&
1058
              _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
1059
            Value delta = _res_cap[a];
1060
            _excess[_source[a]] -= delta;
1061
            _excess[_target[a]] += delta;
1062
            _res_cap[a] = 0;
1063
            _res_cap[_reverse[a]] += delta;
1064
          }
1065
        }
1066

	
1067
        // Find active nodes (i.e. nodes with positive excess)
1068
        for (int u = 0; u != _res_node_num; ++u) {
1069
          if (_excess[u] > 0) _active_nodes.push_back(u);
1070
        }
1071

	
1072
        // Initialize the next arcs
1073
        for (int u = 0; u != _res_node_num; ++u) {
1074
          _next_out[u] = _first_out[u];
1075
        }
1180
        
1181
        // Initialize current phase
1182
        initPhase();
1076 1183

	
1077 1184
        // Perform push and relabel operations
1078 1185
        while (_active_nodes.size() > 0) {
1079
          LargeCost min_red_cost, rc;
1186
          LargeCost min_red_cost, rc, pi_n;
1080 1187
          Value delta;
1081 1188
          int n, t, a, last_out = _res_arc_num;
1082 1189

	
1190
        next_node:
1083 1191
          // Select an active node (FIFO selection)
1084
        next_node:
1085 1192
          n = _active_nodes.front();
1086
          last_out = _sum_supply < 0 ?
1087
            _first_out[n+1] : _first_out[n+1] - 1;
1088

	
1193
          last_out = _first_out[n+1];
1194
          pi_n = _pi[n];
1195
          
1089 1196
          // Perform push operations if there are admissible arcs
1090 1197
          if (_excess[n] > 0) {
1091 1198
            for (a = _next_out[n]; a != last_out; ++a) {
1092 1199
              if (_res_cap[a] > 0 &&
1093
                  _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) {
1200
                  _cost[a] + pi_n - _pi[_target[a]] < 0) {
1094 1201
                delta = std::min(_res_cap[a], _excess[n]);
1095 1202
                t = _target[a];
1096 1203

	
1097 1204
                // Push-look-ahead heuristic
1098 1205
                Value ahead = -_excess[t];
1099
                int last_out_t = _sum_supply < 0 ?
1100
                  _first_out[t+1] : _first_out[t+1] - 1;
1206
                int last_out_t = _first_out[t+1];
1207
                LargeCost pi_t = _pi[t];
1101 1208
                for (int ta = _next_out[t]; ta != last_out_t; ++ta) {
1102 1209
                  if (_res_cap[ta] > 0 && 
1103
                      _cost[ta] + _pi[_source[ta]] - _pi[_target[ta]] < 0)
1210
                      _cost[ta] + pi_t - _pi[_target[ta]] < 0)
1104 1211
                    ahead += _res_cap[ta];
1105 1212
                  if (ahead >= delta) break;
1106 1213
                }
1107 1214
                if (ahead < 0) ahead = 0;
1108 1215

	
1109 1216
                // Push flow along the arc
1110
                if (ahead < delta) {
1217
                if (ahead < delta && !hyper[t]) {
1111 1218
                  _res_cap[a] -= ahead;
1112 1219
                  _res_cap[_reverse[a]] += ahead;
1113 1220
                  _excess[n] -= ahead;
1114 1221
                  _excess[t] += ahead;
1115 1222
                  _active_nodes.push_front(t);
1116 1223
                  hyper[t] = true;
1224
                  hyper_cost[t] = _cost[a] + pi_n - pi_t;
1117 1225
                  _next_out[n] = a;
1118 1226
                  goto next_node;
1119 1227
                } else {
1120 1228
                  _res_cap[a] -= delta;
1121 1229
                  _res_cap[_reverse[a]] += delta;
1122 1230
                  _excess[n] -= delta;
1123 1231
                  _excess[t] += delta;
1124 1232
                  if (_excess[t] > 0 && _excess[t] <= delta)
1125 1233
                    _active_nodes.push_back(t);
1126 1234
                }
1127 1235

	
1128 1236
                if (_excess[n] == 0) {
1129 1237
                  _next_out[n] = a;
1130 1238
                  goto remove_nodes;
1131 1239
                }
1132 1240
              }
1133 1241
            }
1134 1242
            _next_out[n] = a;
1135 1243
          }
1136 1244

	
1137 1245
          // Relabel the node if it is still active (or hyper)
1138 1246
          if (_excess[n] > 0 || hyper[n]) {
1139
            min_red_cost = std::numeric_limits<LargeCost>::max() / 2;
1247
             min_red_cost = hyper[n] ? -hyper_cost[n] :
1248
               std::numeric_limits<LargeCost>::max();
1140 1249
            for (int a = _first_out[n]; a != last_out; ++a) {
1141
              rc = _cost[a] + _pi[_source[a]] - _pi[_target[a]];
1250
              rc = _cost[a] + pi_n - _pi[_target[a]];
1142 1251
              if (_res_cap[a] > 0 && rc < min_red_cost) {
1143 1252
                min_red_cost = rc;
1144 1253
              }
1145 1254
            }
1146 1255
            _pi[n] -= min_red_cost + _epsilon;
1256
            _next_out[n] = _first_out[n];
1147 1257
            hyper[n] = false;
1148

	
1149
            // Reset the next arc
1150
            _next_out[n] = _first_out[n];
1258
            ++relabel_cnt;
1151 1259
          }
1152 1260
        
1153 1261
          // Remove nodes that are not active nor hyper
1154 1262
        remove_nodes:
1155 1263
          while ( _active_nodes.size() > 0 &&
1156 1264
                  _excess[_active_nodes.front()] <= 0 &&
1157 1265
                  !hyper[_active_nodes.front()] ) {
1158 1266
            _active_nodes.pop_front();
1159 1267
          }
1268
          
1269
          // Global update heuristic
1270
          if (relabel_cnt >= next_update_limit) {
1271
            globalUpdate();
1272
            for (int u = 0; u != _res_node_num; ++u)
1273
              hyper[u] = false;
1274
            next_update_limit += global_update_freq;
1275
          }
1160 1276
        }
1161 1277
      }
1162 1278
    }
1163 1279

	
1164 1280
  }; //class CostScaling
1165 1281

	
1166 1282
  ///@}
1167 1283

	
1168 1284
} //namespace lemon
1169 1285

	
1170 1286
#endif //LEMON_COST_SCALING_H
Ignore white space 24 line context
... ...
@@ -135,28 +135,29 @@
135 135
      /// improved version of the previous method
136 136
      /// \ref goldberg89cyclecanceling.
137 137
      /// It is faster both in theory and in practice, its running time
138 138
      /// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)).
139 139
      CANCEL_AND_TIGHTEN
140 140
    };
141 141

	
142 142
  private:
143 143

	
144 144
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
145 145
    
146 146
    typedef std::vector<int> IntVector;
147
    typedef std::vector<char> CharVector;
148 147
    typedef std::vector<double> DoubleVector;
149 148
    typedef std::vector<Value> ValueVector;
150 149
    typedef std::vector<Cost> CostVector;
150
    typedef std::vector<char> BoolVector;
151
    // Note: vector<char> is used instead of vector<bool> for efficiency reasons
151 152

	
152 153
  private:
153 154
  
154 155
    template <typename KT, typename VT>
155 156
    class StaticVectorMap {
156 157
    public:
157 158
      typedef KT Key;
158 159
      typedef VT Value;
159 160
      
160 161
      StaticVectorMap(std::vector<Value>& v) : _v(v) {}
161 162
      
162 163
      const Value& operator[](const Key& key) const {
... ...
@@ -189,25 +190,25 @@
189 190
    int _res_arc_num;
190 191
    int _root;
191 192

	
192 193
    // Parameters of the problem
193 194
    bool _have_lower;
194 195
    Value _sum_supply;
195 196

	
196 197
    // Data structures for storing the digraph
197 198
    IntNodeMap _node_id;
198 199
    IntArcMap _arc_idf;
199 200
    IntArcMap _arc_idb;
200 201
    IntVector _first_out;
201
    CharVector _forward;
202
    BoolVector _forward;
202 203
    IntVector _source;
203 204
    IntVector _target;
204 205
    IntVector _reverse;
205 206

	
206 207
    // Node and arc data
207 208
    ValueVector _lower;
208 209
    ValueVector _upper;
209 210
    CostVector _cost;
210 211
    ValueVector _supply;
211 212

	
212 213
    ValueVector _res_cap;
213 214
    CostVector _pi;
... ...
@@ -924,26 +925,26 @@
924 925
      }
925 926
    }
926 927

	
927 928
    // Execute the "Cancel And Tighten" method
928 929
    void startCancelAndTighten() {
929 930
      // Constants for the min mean cycle computations
930 931
      const double LIMIT_FACTOR = 1.0;
931 932
      const int MIN_LIMIT = 5;
932 933

	
933 934
      // Contruct auxiliary data vectors
934 935
      DoubleVector pi(_res_node_num, 0.0);
935 936
      IntVector level(_res_node_num);
936
      CharVector reached(_res_node_num);
937
      CharVector processed(_res_node_num);
937
      BoolVector reached(_res_node_num);
938
      BoolVector processed(_res_node_num);
938 939
      IntVector pred_node(_res_node_num);
939 940
      IntVector pred_arc(_res_node_num);
940 941
      std::vector<int> stack(_res_node_num);
941 942
      std::vector<int> proc_vector(_res_node_num);
942 943

	
943 944
      // Initialize epsilon
944 945
      double epsilon = 0;
945 946
      for (int a = 0; a != _res_arc_num; ++a) {
946 947
        if (_res_cap[a] > 0 && -_cost[a] > epsilon)
947 948
          epsilon = -_cost[a];
948 949
      }
949 950

	
Ignore white space 24 line context
... ...
@@ -155,27 +155,28 @@
155 155
      /// The \e Altering \e Candidate \e List pivot rule.
156 156
      /// It is a modified version of the Candidate List method.
157 157
      /// It keeps only the several best eligible arcs from the former
158 158
      /// candidate list and extends this list in every iteration.
159 159
      ALTERING_LIST
160 160
    };
161 161
    
162 162
  private:
163 163

	
164 164
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
165 165

	
166 166
    typedef std::vector<int> IntVector;
167
    typedef std::vector<char> CharVector;
168 167
    typedef std::vector<Value> ValueVector;
169 168
    typedef std::vector<Cost> CostVector;
169
    typedef std::vector<char> BoolVector;
170
    // Note: vector<char> is used instead of vector<bool> for efficiency reasons
170 171

	
171 172
    // State constants for arcs
172 173
    enum ArcStateEnum {
173 174
      STATE_UPPER = -1,
174 175
      STATE_TREE  =  0,
175 176
      STATE_LOWER =  1
176 177
    };
177 178

	
178 179
  private:
179 180

	
180 181
    // Data related to the underlying digraph
181 182
    const GR &_graph;
... ...
@@ -203,26 +204,26 @@
203 204
    ValueVector _supply;
204 205
    ValueVector _flow;
205 206
    CostVector _pi;
206 207

	
207 208
    // Data for storing the spanning tree structure
208 209
    IntVector _parent;
209 210
    IntVector _pred;
210 211
    IntVector _thread;
211 212
    IntVector _rev_thread;
212 213
    IntVector _succ_num;
213 214
    IntVector _last_succ;
214 215
    IntVector _dirty_revs;
215
    CharVector _forward;
216
    CharVector _state;
216
    BoolVector _forward;
217
    BoolVector _state;
217 218
    int _root;
218 219

	
219 220
    // Temporary data used in the current pivot iteration
220 221
    int in_arc, join, u_in, v_in, u_out, v_out;
221 222
    int first, second, right, last;
222 223
    int stem, par_stem, new_stem;
223 224
    Value delta;
224 225
    
225 226
    const Value MAX;
226 227

	
227 228
  public:
228 229
  
... ...
@@ -235,159 +236,159 @@
235 236

	
236 237
  private:
237 238

	
238 239
    // Implementation of the First Eligible pivot rule
239 240
    class FirstEligiblePivotRule
240 241
    {
241 242
    private:
242 243

	
243 244
      // References to the NetworkSimplex class
244 245
      const IntVector  &_source;
245 246
      const IntVector  &_target;
246 247
      const CostVector &_cost;
247
      const CharVector &_state;
248
      const BoolVector &_state;
248 249
      const CostVector &_pi;
249 250
      int &_in_arc;
250 251
      int _search_arc_num;
251 252

	
252 253
      // Pivot rule data
253 254
      int _next_arc;
254 255

	
255 256
    public:
256 257

	
257 258
      // Constructor
258 259
      FirstEligiblePivotRule(NetworkSimplex &ns) :
259 260
        _source(ns._source), _target(ns._target),
260 261
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
261 262
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num),
262 263
        _next_arc(0)
263 264
      {}
264 265

	
265 266
      // Find next entering arc
266 267
      bool findEnteringArc() {
267 268
        Cost c;
268
        for (int e = _next_arc; e < _search_arc_num; ++e) {
269
        for (int e = _next_arc; e != _search_arc_num; ++e) {
269 270
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
270 271
          if (c < 0) {
271 272
            _in_arc = e;
272 273
            _next_arc = e + 1;
273 274
            return true;
274 275
          }
275 276
        }
276
        for (int e = 0; e < _next_arc; ++e) {
277
        for (int e = 0; e != _next_arc; ++e) {
277 278
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
278 279
          if (c < 0) {
279 280
            _in_arc = e;
280 281
            _next_arc = e + 1;
281 282
            return true;
282 283
          }
283 284
        }
284 285
        return false;
285 286
      }
286 287

	
287 288
    }; //class FirstEligiblePivotRule
288 289

	
289 290

	
290 291
    // Implementation of the Best Eligible pivot rule
291 292
    class BestEligiblePivotRule
292 293
    {
293 294
    private:
294 295

	
295 296
      // References to the NetworkSimplex class
296 297
      const IntVector  &_source;
297 298
      const IntVector  &_target;
298 299
      const CostVector &_cost;
299
      const CharVector &_state;
300
      const BoolVector &_state;
300 301
      const CostVector &_pi;
301 302
      int &_in_arc;
302 303
      int _search_arc_num;
303 304

	
304 305
    public:
305 306

	
306 307
      // Constructor
307 308
      BestEligiblePivotRule(NetworkSimplex &ns) :
308 309
        _source(ns._source), _target(ns._target),
309 310
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
310 311
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num)
311 312
      {}
312 313

	
313 314
      // Find next entering arc
314 315
      bool findEnteringArc() {
315 316
        Cost c, min = 0;
316
        for (int e = 0; e < _search_arc_num; ++e) {
317
        for (int e = 0; e != _search_arc_num; ++e) {
317 318
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
318 319
          if (c < min) {
319 320
            min = c;
320 321
            _in_arc = e;
321 322
          }
322 323
        }
323 324
        return min < 0;
324 325
      }
325 326

	
326 327
    }; //class BestEligiblePivotRule
327 328

	
328 329

	
329 330
    // Implementation of the Block Search pivot rule
330 331
    class BlockSearchPivotRule
331 332
    {
332 333
    private:
333 334

	
334 335
      // References to the NetworkSimplex class
335 336
      const IntVector  &_source;
336 337
      const IntVector  &_target;
337 338
      const CostVector &_cost;
338
      const CharVector &_state;
339
      const BoolVector &_state;
339 340
      const CostVector &_pi;
340 341
      int &_in_arc;
341 342
      int _search_arc_num;
342 343

	
343 344
      // Pivot rule data
344 345
      int _block_size;
345 346
      int _next_arc;
346 347

	
347 348
    public:
348 349

	
349 350
      // Constructor
350 351
      BlockSearchPivotRule(NetworkSimplex &ns) :
351 352
        _source(ns._source), _target(ns._target),
352 353
        _cost(ns._cost), _state(ns._state), _pi(ns._pi),
353 354
        _in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num),
354 355
        _next_arc(0)
355 356
      {
356 357
        // The main parameters of the pivot rule
357
        const double BLOCK_SIZE_FACTOR = 0.5;
358
        const double BLOCK_SIZE_FACTOR = 1.0;
358 359
        const int MIN_BLOCK_SIZE = 10;
359 360

	
360 361
        _block_size = std::max( int(BLOCK_SIZE_FACTOR *
361 362
                                    std::sqrt(double(_search_arc_num))),
362 363
                                MIN_BLOCK_SIZE );
363 364
      }
364 365

	
365 366
      // Find next entering arc
366 367
      bool findEnteringArc() {
367 368
        Cost c, min = 0;
368 369
        int cnt = _block_size;
369 370
        int e;
370
        for (e = _next_arc; e < _search_arc_num; ++e) {
371
        for (e = _next_arc; e != _search_arc_num; ++e) {
371 372
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
372 373
          if (c < min) {
373 374
            min = c;
374 375
            _in_arc = e;
375 376
          }
376 377
          if (--cnt == 0) {
377 378
            if (min < 0) goto search_end;
378 379
            cnt = _block_size;
379 380
          }
380 381
        }
381
        for (e = 0; e < _next_arc; ++e) {
382
        for (e = 0; e != _next_arc; ++e) {
382 383
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
383 384
          if (c < min) {
384 385
            min = c;
385 386
            _in_arc = e;
386 387
          }
387 388
          if (--cnt == 0) {
388 389
            if (min < 0) goto search_end;
389 390
            cnt = _block_size;
390 391
          }
391 392
        }
392 393
        if (min >= 0) return false;
393 394

	
... ...
@@ -399,25 +400,25 @@
399 400
    }; //class BlockSearchPivotRule
400 401

	
401 402

	
402 403
    // Implementation of the Candidate List pivot rule
403 404
    class CandidateListPivotRule
404 405
    {
405 406
    private:
406 407

	
407 408
      // References to the NetworkSimplex class
408 409
      const IntVector  &_source;
409 410
      const IntVector  &_target;
410 411
      const CostVector &_cost;
411
      const CharVector &_state;
412
      const BoolVector &_state;
412 413
      const CostVector &_pi;
413 414
      int &_in_arc;
414 415
      int _search_arc_num;
415 416

	
416 417
      // Pivot rule data
417 418
      IntVector _candidates;
418 419
      int _list_length, _minor_limit;
419 420
      int _curr_length, _minor_count;
420 421
      int _next_arc;
421 422

	
422 423
    public:
423 424

	
... ...
@@ -460,36 +461,36 @@
460 461
              _in_arc = e;
461 462
            }
462 463
            else if (c >= 0) {
463 464
              _candidates[i--] = _candidates[--_curr_length];
464 465
            }
465 466
          }
466 467
          if (min < 0) return true;
467 468
        }
468 469

	
469 470
        // Major iteration: build a new candidate list
470 471
        min = 0;
471 472
        _curr_length = 0;
472
        for (e = _next_arc; e < _search_arc_num; ++e) {
473
        for (e = _next_arc; e != _search_arc_num; ++e) {
473 474
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
474 475
          if (c < 0) {
475 476
            _candidates[_curr_length++] = e;
476 477
            if (c < min) {
477 478
              min = c;
478 479
              _in_arc = e;
479 480
            }
480 481
            if (_curr_length == _list_length) goto search_end;
481 482
          }
482 483
        }
483
        for (e = 0; e < _next_arc; ++e) {
484
        for (e = 0; e != _next_arc; ++e) {
484 485
          c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
485 486
          if (c < 0) {
486 487
            _candidates[_curr_length++] = e;
487 488
            if (c < min) {
488 489
              min = c;
489 490
              _in_arc = e;
490 491
            }
491 492
            if (_curr_length == _list_length) goto search_end;
492 493
          }
493 494
        }
494 495
        if (_curr_length == 0) return false;
495 496
      
... ...
@@ -502,25 +503,25 @@
502 503
    }; //class CandidateListPivotRule
503 504

	
504 505

	
505 506
    // Implementation of the Altering Candidate List pivot rule
506 507
    class AlteringListPivotRule
507 508
    {
508 509
    private:
509 510

	
510 511
      // References to the NetworkSimplex class
511 512
      const IntVector  &_source;
512 513
      const IntVector  &_target;
513 514
      const CostVector &_cost;
514
      const CharVector &_state;
515
      const BoolVector &_state;
515 516
      const CostVector &_pi;
516 517
      int &_in_arc;
517 518
      int _search_arc_num;
518 519

	
519 520
      // Pivot rule data
520 521
      int _block_size, _head_length, _curr_length;
521 522
      int _next_arc;
522 523
      IntVector _candidates;
523 524
      CostVector _cand_cost;
524 525

	
525 526
      // Functor class to compare arcs during sort of the candidate list
526 527
      class SortFunc
... ...
@@ -555,50 +556,50 @@
555 556
                                    std::sqrt(double(_search_arc_num))),
556 557
                                MIN_BLOCK_SIZE );
557 558
        _head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size),
558 559
                                 MIN_HEAD_LENGTH );
559 560
        _candidates.resize(_head_length + _block_size);
560 561
        _curr_length = 0;
561 562
      }
562 563

	
563 564
      // Find next entering arc
564 565
      bool findEnteringArc() {
565 566
        // Check the current candidate list
566 567
        int e;
567
        for (int i = 0; i < _curr_length; ++i) {
568
        for (int i = 0; i != _curr_length; ++i) {
568 569
          e = _candidates[i];
569 570
          _cand_cost[e] = _state[e] *
570 571
            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
571 572
          if (_cand_cost[e] >= 0) {
572 573
            _candidates[i--] = _candidates[--_curr_length];
573 574
          }
574 575
        }
575 576

	
576 577
        // Extend the list
577 578
        int cnt = _block_size;
578 579
        int limit = _head_length;
579 580

	
580
        for (e = _next_arc; e < _search_arc_num; ++e) {
581
        for (e = _next_arc; e != _search_arc_num; ++e) {
581 582
          _cand_cost[e] = _state[e] *
582 583
            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
583 584
          if (_cand_cost[e] < 0) {
584 585
            _candidates[_curr_length++] = e;
585 586
          }
586 587
          if (--cnt == 0) {
587 588
            if (_curr_length > limit) goto search_end;
588 589
            limit = 0;
589 590
            cnt = _block_size;
590 591
          }
591 592
        }
592
        for (e = 0; e < _next_arc; ++e) {
593
        for (e = 0; e != _next_arc; ++e) {
593 594
          _cand_cost[e] = _state[e] *
594 595
            (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
595 596
          if (_cand_cost[e] < 0) {
596 597
            _candidates[_curr_length++] = e;
597 598
          }
598 599
          if (--cnt == 0) {
599 600
            if (_curr_length > limit) goto search_end;
600 601
            limit = 0;
601 602
            cnt = _block_size;
602 603
          }
603 604
        }
604 605
        if (_curr_length == 0) return false;
... ...
@@ -1319,25 +1320,25 @@
1319 1320
      _rev_thread[last] = u;
1320 1321
      _last_succ[u_out] = u;
1321 1322

	
1322 1323
      // Remove the subtree of u_out from the thread list except for
1323 1324
      // the case when old_rev_thread equals to v_in
1324 1325
      // (it also means that join and v_out coincide)
1325 1326
      if (old_rev_thread != v_in) {
1326 1327
        _thread[old_rev_thread] = right;
1327 1328
        _rev_thread[right] = old_rev_thread;
1328 1329
      }
1329 1330

	
1330 1331
      // Update _rev_thread using the new _thread values
1331
      for (int i = 0; i < int(_dirty_revs.size()); ++i) {
1332
      for (int i = 0; i != int(_dirty_revs.size()); ++i) {
1332 1333
        u = _dirty_revs[i];
1333 1334
        _rev_thread[_thread[u]] = u;
1334 1335
      }
1335 1336

	
1336 1337
      // Update _pred, _forward, _last_succ and _succ_num for the
1337 1338
      // stem nodes from u_out to u_in
1338 1339
      int tmp_sc = 0, tmp_ls = _last_succ[u_out];
1339 1340
      u = u_out;
1340 1341
      while (u != u_in) {
1341 1342
        w = _parent[u];
1342 1343
        _pred[u] = _pred[w];
1343 1344
        _forward[u] = !_forward[w];
... ...
@@ -1391,46 +1392,143 @@
1391 1392
    // Update potentials
1392 1393
    void updatePotential() {
1393 1394
      Cost sigma = _forward[u_in] ?
1394 1395
        _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] :
1395 1396
        _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]];
1396 1397
      // Update potentials in the subtree, which has been moved
1397 1398
      int end = _thread[_last_succ[u_in]];
1398 1399
      for (int u = u_in; u != end; u = _thread[u]) {
1399 1400
        _pi[u] += sigma;
1400 1401
      }
1401 1402
    }
1402 1403

	
1404
    // Heuristic initial pivots
1405
    bool initialPivots() {
1406
      Value curr, total = 0;
1407
      std::vector<Node> supply_nodes, demand_nodes;
1408
      for (NodeIt u(_graph); u != INVALID; ++u) {
1409
        curr = _supply[_node_id[u]];
1410
        if (curr > 0) {
1411
          total += curr;
1412
          supply_nodes.push_back(u);
1413
        }
1414
        else if (curr < 0) {
1415
          demand_nodes.push_back(u);
1416
        }
1417
      }
1418
      if (_sum_supply > 0) total -= _sum_supply;
1419
      if (total <= 0) return true;
1420

	
1421
      IntVector arc_vector;
1422
      if (_sum_supply >= 0) {
1423
        if (supply_nodes.size() == 1 && demand_nodes.size() == 1) {
1424
          // Perform a reverse graph search from the sink to the source
1425
          typename GR::template NodeMap<bool> reached(_graph, false);
1426
          Node s = supply_nodes[0], t = demand_nodes[0];
1427
          std::vector<Node> stack;
1428
          reached[t] = true;
1429
          stack.push_back(t);
1430
          while (!stack.empty()) {
1431
            Node u, v = stack.back();
1432
            stack.pop_back();
1433
            if (v == s) break;
1434
            for (InArcIt a(_graph, v); a != INVALID; ++a) {
1435
              if (reached[u = _graph.source(a)]) continue;
1436
              int j = _arc_id[a];
1437
              if (_cap[j] >= total) {
1438
                arc_vector.push_back(j);
1439
                reached[u] = true;
1440
                stack.push_back(u);
1441
              }
1442
            }
1443
          }
1444
        } else {
1445
          // Find the min. cost incomming arc for each demand node
1446
          for (int i = 0; i != int(demand_nodes.size()); ++i) {
1447
            Node v = demand_nodes[i];
1448
            Cost c, min_cost = std::numeric_limits<Cost>::max();
1449
            Arc min_arc = INVALID;
1450
            for (InArcIt a(_graph, v); a != INVALID; ++a) {
1451
              c = _cost[_arc_id[a]];
1452
              if (c < min_cost) {
1453
                min_cost = c;
1454
                min_arc = a;
1455
              }
1456
            }
1457
            if (min_arc != INVALID) {
1458
              arc_vector.push_back(_arc_id[min_arc]);
1459
            }
1460
          }
1461
        }
1462
      } else {
1463
        // Find the min. cost outgoing arc for each supply node
1464
        for (int i = 0; i != int(supply_nodes.size()); ++i) {
1465
          Node u = supply_nodes[i];
1466
          Cost c, min_cost = std::numeric_limits<Cost>::max();
1467
          Arc min_arc = INVALID;
1468
          for (OutArcIt a(_graph, u); a != INVALID; ++a) {
1469
            c = _cost[_arc_id[a]];
1470
            if (c < min_cost) {
1471
              min_cost = c;
1472
              min_arc = a;
1473
            }
1474
          }
1475
          if (min_arc != INVALID) {
1476
            arc_vector.push_back(_arc_id[min_arc]);
1477
          }
1478
        }
1479
      }
1480

	
1481
      // Perform heuristic initial pivots
1482
      for (int i = 0; i != int(arc_vector.size()); ++i) {
1483
        in_arc = arc_vector[i];
1484
        if (_state[in_arc] * (_cost[in_arc] + _pi[_source[in_arc]] -
1485
            _pi[_target[in_arc]]) >= 0) continue;
1486
        findJoinNode();
1487
        bool change = findLeavingArc();
1488
        if (delta >= MAX) return false;
1489
        changeFlow(change);
1490
        if (change) {
1491
          updateTreeStructure();
1492
          updatePotential();
1493
        }
1494
      }
1495
      return true;
1496
    }
1497

	
1403 1498
    // Execute the algorithm
1404 1499
    ProblemType start(PivotRule pivot_rule) {
1405 1500
      // Select the pivot rule implementation
1406 1501
      switch (pivot_rule) {
1407 1502
        case FIRST_ELIGIBLE:
1408 1503
          return start<FirstEligiblePivotRule>();
1409 1504
        case BEST_ELIGIBLE:
1410 1505
          return start<BestEligiblePivotRule>();
1411 1506
        case BLOCK_SEARCH:
1412 1507
          return start<BlockSearchPivotRule>();
1413 1508
        case CANDIDATE_LIST:
1414 1509
          return start<CandidateListPivotRule>();
1415 1510
        case ALTERING_LIST:
1416 1511
          return start<AlteringListPivotRule>();
1417 1512
      }
1418 1513
      return INFEASIBLE; // avoid warning
1419 1514
    }
1420 1515

	
1421 1516
    template <typename PivotRuleImpl>
1422 1517
    ProblemType start() {
1423 1518
      PivotRuleImpl pivot(*this);
1424 1519

	
1520
      // Perform heuristic initial pivots
1521
      if (!initialPivots()) return UNBOUNDED;
1522

	
1425 1523
      // Execute the Network Simplex algorithm
1426 1524
      while (pivot.findEnteringArc()) {
1427 1525
        findJoinNode();
1428 1526
        bool change = findLeavingArc();
1429 1527
        if (delta >= MAX) return UNBOUNDED;
1430 1528
        changeFlow(change);
1431 1529
        if (change) {
1432 1530
          updateTreeStructure();
1433 1531
          updatePotential();
1434 1532
        }
1435 1533
      }
1436 1534
      
0 comments (0 inline)