gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix and improve refine methods in CostScaling (#417)
0 1 0
default
1 file changed with 46 insertions and 25 deletions:
↑ Collapse diff ↑
Show white space 768 line context
... ...
@@ -722,611 +722,632 @@
722 722
    /// map. The \c Value type of the algorithm must be convertible to
723 723
    /// the \c Value type of the map.
724 724
    ///
725 725
    /// \pre \ref run() must be called before using this function.
726 726
    template <typename FlowMap>
727 727
    void flowMap(FlowMap &map) const {
728 728
      for (ArcIt a(_graph); a != INVALID; ++a) {
729 729
        map.set(a, _res_cap[_arc_idb[a]]);
730 730
      }
731 731
    }
732 732

	
733 733
    /// \brief Return the potential (dual value) of the given node.
734 734
    ///
735 735
    /// This function returns the potential (dual value) of the
736 736
    /// given node.
737 737
    ///
738 738
    /// \pre \ref run() must be called before using this function.
739 739
    Cost potential(const Node& n) const {
740 740
      return static_cast<Cost>(_pi[_node_id[n]]);
741 741
    }
742 742

	
743 743
    /// \brief Return the potential map (the dual solution).
744 744
    ///
745 745
    /// This function copies the potential (dual value) of each node
746 746
    /// into the given map.
747 747
    /// The \c Cost type of the algorithm must be convertible to the
748 748
    /// \c Value type of the map.
749 749
    ///
750 750
    /// \pre \ref run() must be called before using this function.
751 751
    template <typename PotentialMap>
752 752
    void potentialMap(PotentialMap &map) const {
753 753
      for (NodeIt n(_graph); n != INVALID; ++n) {
754 754
        map.set(n, static_cast<Cost>(_pi[_node_id[n]]));
755 755
      }
756 756
    }
757 757

	
758 758
    /// @}
759 759

	
760 760
  private:
761 761

	
762 762
    // Initialize the algorithm
763 763
    ProblemType init() {
764 764
      if (_res_node_num <= 1) return INFEASIBLE;
765 765

	
766 766
      // Check the sum of supply values
767 767
      _sum_supply = 0;
768 768
      for (int i = 0; i != _root; ++i) {
769 769
        _sum_supply += _supply[i];
770 770
      }
771 771
      if (_sum_supply > 0) return INFEASIBLE;
772 772

	
773 773

	
774 774
      // Initialize vectors
775 775
      for (int i = 0; i != _res_node_num; ++i) {
776 776
        _pi[i] = 0;
777 777
        _excess[i] = _supply[i];
778 778
      }
779 779

	
780 780
      // Remove infinite upper bounds and check negative arcs
781 781
      const Value MAX = std::numeric_limits<Value>::max();
782 782
      int last_out;
783 783
      if (_have_lower) {
784 784
        for (int i = 0; i != _root; ++i) {
785 785
          last_out = _first_out[i+1];
786 786
          for (int j = _first_out[i]; j != last_out; ++j) {
787 787
            if (_forward[j]) {
788 788
              Value c = _scost[j] < 0 ? _upper[j] : _lower[j];
789 789
              if (c >= MAX) return UNBOUNDED;
790 790
              _excess[i] -= c;
791 791
              _excess[_target[j]] += c;
792 792
            }
793 793
          }
794 794
        }
795 795
      } else {
796 796
        for (int i = 0; i != _root; ++i) {
797 797
          last_out = _first_out[i+1];
798 798
          for (int j = _first_out[i]; j != last_out; ++j) {
799 799
            if (_forward[j] && _scost[j] < 0) {
800 800
              Value c = _upper[j];
801 801
              if (c >= MAX) return UNBOUNDED;
802 802
              _excess[i] -= c;
803 803
              _excess[_target[j]] += c;
804 804
            }
805 805
          }
806 806
        }
807 807
      }
808 808
      Value ex, max_cap = 0;
809 809
      for (int i = 0; i != _res_node_num; ++i) {
810 810
        ex = _excess[i];
811 811
        _excess[i] = 0;
812 812
        if (ex < 0) max_cap -= ex;
813 813
      }
814 814
      for (int j = 0; j != _res_arc_num; ++j) {
815 815
        if (_upper[j] >= MAX) _upper[j] = max_cap;
816 816
      }
817 817

	
818 818
      // Initialize the large cost vector and the epsilon parameter
819 819
      _epsilon = 0;
820 820
      LargeCost lc;
821 821
      for (int i = 0; i != _root; ++i) {
822 822
        last_out = _first_out[i+1];
823 823
        for (int j = _first_out[i]; j != last_out; ++j) {
824 824
          lc = static_cast<LargeCost>(_scost[j]) * _res_node_num * _alpha;
825 825
          _cost[j] = lc;
826 826
          if (lc > _epsilon) _epsilon = lc;
827 827
        }
828 828
      }
829 829
      _epsilon /= _alpha;
830 830

	
831 831
      // Initialize maps for Circulation and remove non-zero lower bounds
832 832
      ConstMap<Arc, Value> low(0);
833 833
      typedef typename Digraph::template ArcMap<Value> ValueArcMap;
834 834
      typedef typename Digraph::template NodeMap<Value> ValueNodeMap;
835 835
      ValueArcMap cap(_graph), flow(_graph);
836 836
      ValueNodeMap sup(_graph);
837 837
      for (NodeIt n(_graph); n != INVALID; ++n) {
838 838
        sup[n] = _supply[_node_id[n]];
839 839
      }
840 840
      if (_have_lower) {
841 841
        for (ArcIt a(_graph); a != INVALID; ++a) {
842 842
          int j = _arc_idf[a];
843 843
          Value c = _lower[j];
844 844
          cap[a] = _upper[j] - c;
845 845
          sup[_graph.source(a)] -= c;
846 846
          sup[_graph.target(a)] += c;
847 847
        }
848 848
      } else {
849 849
        for (ArcIt a(_graph); a != INVALID; ++a) {
850 850
          cap[a] = _upper[_arc_idf[a]];
851 851
        }
852 852
      }
853 853

	
854 854
      _sup_node_num = 0;
855 855
      for (NodeIt n(_graph); n != INVALID; ++n) {
856 856
        if (sup[n] > 0) ++_sup_node_num;
857 857
      }
858 858

	
859 859
      // Find a feasible flow using Circulation
860 860
      Circulation<Digraph, ConstMap<Arc, Value>, ValueArcMap, ValueNodeMap>
861 861
        circ(_graph, low, cap, sup);
862 862
      if (!circ.flowMap(flow).run()) return INFEASIBLE;
863 863

	
864 864
      // Set residual capacities and handle GEQ supply type
865 865
      if (_sum_supply < 0) {
866 866
        for (ArcIt a(_graph); a != INVALID; ++a) {
867 867
          Value fa = flow[a];
868 868
          _res_cap[_arc_idf[a]] = cap[a] - fa;
869 869
          _res_cap[_arc_idb[a]] = fa;
870 870
          sup[_graph.source(a)] -= fa;
871 871
          sup[_graph.target(a)] += fa;
872 872
        }
873 873
        for (NodeIt n(_graph); n != INVALID; ++n) {
874 874
          _excess[_node_id[n]] = sup[n];
875 875
        }
876 876
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
877 877
          int u = _target[a];
878 878
          int ra = _reverse[a];
879 879
          _res_cap[a] = -_sum_supply + 1;
880 880
          _res_cap[ra] = -_excess[u];
881 881
          _cost[a] = 0;
882 882
          _cost[ra] = 0;
883 883
          _excess[u] = 0;
884 884
        }
885 885
      } else {
886 886
        for (ArcIt a(_graph); a != INVALID; ++a) {
887 887
          Value fa = flow[a];
888 888
          _res_cap[_arc_idf[a]] = cap[a] - fa;
889 889
          _res_cap[_arc_idb[a]] = fa;
890 890
        }
891 891
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
892 892
          int ra = _reverse[a];
893 893
          _res_cap[a] = 0;
894 894
          _res_cap[ra] = 0;
895 895
          _cost[a] = 0;
896 896
          _cost[ra] = 0;
897 897
        }
898 898
      }
899 899

	
900 900
      // Initialize data structures for buckets
901 901
      _max_rank = _alpha * _res_node_num;
902 902
      _buckets.resize(_max_rank);
903 903
      _bucket_next.resize(_res_node_num + 1);
904 904
      _bucket_prev.resize(_res_node_num + 1);
905 905
      _rank.resize(_res_node_num + 1);
906 906

	
907 907
      return OPTIMAL;
908 908
    }
909 909

	
910 910
    // Execute the algorithm and transform the results
911 911
    void start(Method method) {
912 912
      const int MAX_PARTIAL_PATH_LENGTH = 4;
913 913

	
914 914
      switch (method) {
915 915
        case PUSH:
916 916
          startPush();
917 917
          break;
918 918
        case AUGMENT:
919 919
          startAugment(_res_node_num - 1);
920 920
          break;
921 921
        case PARTIAL_AUGMENT:
922 922
          startAugment(MAX_PARTIAL_PATH_LENGTH);
923 923
          break;
924 924
      }
925 925

	
926 926
      // Compute node potentials for the original costs
927 927
      _arc_vec.clear();
928 928
      _cost_vec.clear();
929 929
      for (int j = 0; j != _res_arc_num; ++j) {
930 930
        if (_res_cap[j] > 0) {
931 931
          _arc_vec.push_back(IntPair(_source[j], _target[j]));
932 932
          _cost_vec.push_back(_scost[j]);
933 933
        }
934 934
      }
935 935
      _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
936 936

	
937 937
      typename BellmanFord<StaticDigraph, LargeCostArcMap>
938 938
        ::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map);
939 939
      bf.distMap(_pi_map);
940 940
      bf.init(0);
941 941
      bf.start();
942 942

	
943 943
      // Handle non-zero lower bounds
944 944
      if (_have_lower) {
945 945
        int limit = _first_out[_root];
946 946
        for (int j = 0; j != limit; ++j) {
947 947
          if (!_forward[j]) _res_cap[j] += _lower[j];
948 948
        }
949 949
      }
950 950
    }
951 951

	
952 952
    // Initialize a cost scaling phase
953 953
    void initPhase() {
954 954
      // Saturate arcs not satisfying the optimality condition
955 955
      for (int u = 0; u != _res_node_num; ++u) {
956 956
        int last_out = _first_out[u+1];
957 957
        LargeCost pi_u = _pi[u];
958 958
        for (int a = _first_out[u]; a != last_out; ++a) {
959 959
          Value delta = _res_cap[a];
960 960
          if (delta > 0) {
961 961
            int v = _target[a];
962 962
            if (_cost[a] + pi_u - _pi[v] < 0) {
963 963
              _excess[u] -= delta;
964 964
              _excess[v] += delta;
965 965
              _res_cap[a] = 0;
966 966
              _res_cap[_reverse[a]] += delta;
967 967
            }
968 968
          }
969 969
        }
970 970
      }
971 971

	
972 972
      // Find active nodes (i.e. nodes with positive excess)
973 973
      for (int u = 0; u != _res_node_num; ++u) {
974 974
        if (_excess[u] > 0) _active_nodes.push_back(u);
975 975
      }
976 976

	
977 977
      // Initialize the next arcs
978 978
      for (int u = 0; u != _res_node_num; ++u) {
979 979
        _next_out[u] = _first_out[u];
980 980
      }
981 981
    }
982 982

	
983 983
    // Early termination heuristic
984 984
    bool earlyTermination() {
985 985
      const double EARLY_TERM_FACTOR = 3.0;
986 986

	
987 987
      // Build a static residual graph
988 988
      _arc_vec.clear();
989 989
      _cost_vec.clear();
990 990
      for (int j = 0; j != _res_arc_num; ++j) {
991 991
        if (_res_cap[j] > 0) {
992 992
          _arc_vec.push_back(IntPair(_source[j], _target[j]));
993 993
          _cost_vec.push_back(_cost[j] + 1);
994 994
        }
995 995
      }
996 996
      _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
997 997

	
998 998
      // Run Bellman-Ford algorithm to check if the current flow is optimal
999 999
      BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map);
1000 1000
      bf.init(0);
1001 1001
      bool done = false;
1002 1002
      int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num)));
1003 1003
      for (int i = 0; i < K && !done; ++i) {
1004 1004
        done = bf.processNextWeakRound();
1005 1005
      }
1006 1006
      return done;
1007 1007
    }
1008 1008

	
1009 1009
    // Global potential update heuristic
1010 1010
    void globalUpdate() {
1011 1011
      const int bucket_end = _root + 1;
1012 1012

	
1013 1013
      // Initialize buckets
1014 1014
      for (int r = 0; r != _max_rank; ++r) {
1015 1015
        _buckets[r] = bucket_end;
1016 1016
      }
1017 1017
      Value total_excess = 0;
1018 1018
      int b0 = bucket_end;
1019 1019
      for (int i = 0; i != _res_node_num; ++i) {
1020 1020
        if (_excess[i] < 0) {
1021 1021
          _rank[i] = 0;
1022 1022
          _bucket_next[i] = b0;
1023 1023
          _bucket_prev[b0] = i;
1024 1024
          b0 = i;
1025 1025
        } else {
1026 1026
          total_excess += _excess[i];
1027 1027
          _rank[i] = _max_rank;
1028 1028
        }
1029 1029
      }
1030 1030
      if (total_excess == 0) return;
1031 1031
      _buckets[0] = b0;
1032 1032

	
1033 1033
      // Search the buckets
1034 1034
      int r = 0;
1035 1035
      for ( ; r != _max_rank; ++r) {
1036 1036
        while (_buckets[r] != bucket_end) {
1037 1037
          // Remove the first node from the current bucket
1038 1038
          int u = _buckets[r];
1039 1039
          _buckets[r] = _bucket_next[u];
1040 1040

	
1041 1041
          // Search the incomming arcs of u
1042 1042
          LargeCost pi_u = _pi[u];
1043 1043
          int last_out = _first_out[u+1];
1044 1044
          for (int a = _first_out[u]; a != last_out; ++a) {
1045 1045
            int ra = _reverse[a];
1046 1046
            if (_res_cap[ra] > 0) {
1047 1047
              int v = _source[ra];
1048 1048
              int old_rank_v = _rank[v];
1049 1049
              if (r < old_rank_v) {
1050 1050
                // Compute the new rank of v
1051 1051
                LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon;
1052 1052
                int new_rank_v = old_rank_v;
1053 1053
                if (nrc < LargeCost(_max_rank)) {
1054 1054
                  new_rank_v = r + 1 + static_cast<int>(nrc);
1055 1055
                }
1056 1056

	
1057 1057
                // Change the rank of v
1058 1058
                if (new_rank_v < old_rank_v) {
1059 1059
                  _rank[v] = new_rank_v;
1060 1060
                  _next_out[v] = _first_out[v];
1061 1061

	
1062 1062
                  // Remove v from its old bucket
1063 1063
                  if (old_rank_v < _max_rank) {
1064 1064
                    if (_buckets[old_rank_v] == v) {
1065 1065
                      _buckets[old_rank_v] = _bucket_next[v];
1066 1066
                    } else {
1067 1067
                      int pv = _bucket_prev[v], nv = _bucket_next[v];
1068 1068
                      _bucket_next[pv] = nv;
1069 1069
                      _bucket_prev[nv] = pv;
1070 1070
                    }
1071 1071
                  }
1072 1072

	
1073 1073
                  // Insert v into its new bucket
1074 1074
                  int nv = _buckets[new_rank_v];
1075 1075
                  _bucket_next[v] = nv;
1076 1076
                  _bucket_prev[nv] = v;
1077 1077
                  _buckets[new_rank_v] = v;
1078 1078
                }
1079 1079
              }
1080 1080
            }
1081 1081
          }
1082 1082

	
1083 1083
          // Finish search if there are no more active nodes
1084 1084
          if (_excess[u] > 0) {
1085 1085
            total_excess -= _excess[u];
1086 1086
            if (total_excess <= 0) break;
1087 1087
          }
1088 1088
        }
1089 1089
        if (total_excess <= 0) break;
1090 1090
      }
1091 1091

	
1092 1092
      // Relabel nodes
1093 1093
      for (int u = 0; u != _res_node_num; ++u) {
1094 1094
        int k = std::min(_rank[u], r);
1095 1095
        if (k > 0) {
1096 1096
          _pi[u] -= _epsilon * k;
1097 1097
          _next_out[u] = _first_out[u];
1098 1098
        }
1099 1099
      }
1100 1100
    }
1101 1101

	
1102 1102
    /// Execute the algorithm performing augment and relabel operations
1103 1103
    void startAugment(int max_length) {
1104 1104
      // Paramters for heuristics
1105 1105
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
1106
      const double GLOBAL_UPDATE_FACTOR = 3.0;
1107

	
1108
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
1106
      const double GLOBAL_UPDATE_FACTOR = 1.0;
1107
      const int global_update_skip = static_cast<int>(GLOBAL_UPDATE_FACTOR *
1109 1108
        (_res_node_num + _sup_node_num * _sup_node_num));
1110
      int next_update_limit = global_update_freq;
1111

	
1112
      int relabel_cnt = 0;
1109
      int next_global_update_limit = global_update_skip;
1113 1110

	
1114 1111
      // Perform cost scaling phases
1115
      std::vector<int> path;
1112
      IntVector path;
1113
      BoolVector path_arc(_res_arc_num, false);
1114
      int relabel_cnt = 0;
1116 1115
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
1117 1116
                                        1 : _epsilon / _alpha )
1118 1117
      {
1119 1118
        // Early termination heuristic
1120 1119
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
1121 1120
          if (earlyTermination()) break;
1122 1121
        }
1123 1122

	
1124 1123
        // Initialize current phase
1125 1124
        initPhase();
1126 1125

	
1127 1126
        // Perform partial augment and relabel operations
1128 1127
        while (true) {
1129 1128
          // Select an active node (FIFO selection)
1130 1129
          while (_active_nodes.size() > 0 &&
1131 1130
                 _excess[_active_nodes.front()] <= 0) {
1132 1131
            _active_nodes.pop_front();
1133 1132
          }
1134 1133
          if (_active_nodes.size() == 0) break;
1135 1134
          int start = _active_nodes.front();
1136 1135

	
1137 1136
          // Find an augmenting path from the start node
1138
          path.clear();
1139 1137
          int tip = start;
1140
          while (_excess[tip] >= 0 && int(path.size()) < max_length) {
1138
          while (int(path.size()) < max_length && _excess[tip] >= 0) {
1141 1139
            int u;
1142
            LargeCost min_red_cost, rc, pi_tip = _pi[tip];
1140
            LargeCost rc, min_red_cost = std::numeric_limits<LargeCost>::max();
1141
            LargeCost pi_tip = _pi[tip];
1143 1142
            int last_out = _first_out[tip+1];
1144 1143
            for (int a = _next_out[tip]; a != last_out; ++a) {
1144
              if (_res_cap[a] > 0) {
1145 1145
              u = _target[a];
1146
              if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) {
1146
                rc = _cost[a] + pi_tip - _pi[u];
1147
                if (rc < 0) {
1147 1148
                path.push_back(a);
1148 1149
                _next_out[tip] = a;
1150
                  if (path_arc[a]) {
1151
                    goto augment;   // a cycle is found, stop path search
1152
                  }
1149 1153
                tip = u;
1154
                  path_arc[a] = true;
1150 1155
                goto next_step;
1151 1156
              }
1157
                else if (rc < min_red_cost) {
1158
                  min_red_cost = rc;
1159
                }
1160
              }
1152 1161
            }
1153 1162

	
1154 1163
            // Relabel tip node
1155
            min_red_cost = std::numeric_limits<LargeCost>::max();
1156 1164
            if (tip != start) {
1157 1165
              int ra = _reverse[path.back()];
1158
              min_red_cost = _cost[ra] + pi_tip - _pi[_target[ra]];
1166
              min_red_cost =
1167
                std::min(min_red_cost, _cost[ra] + pi_tip - _pi[_target[ra]]);
1159 1168
            }
1169
            last_out = _next_out[tip];
1160 1170
            for (int a = _first_out[tip]; a != last_out; ++a) {
1171
              if (_res_cap[a] > 0) {
1161 1172
              rc = _cost[a] + pi_tip - _pi[_target[a]];
1162
              if (_res_cap[a] > 0 && rc < min_red_cost) {
1173
                if (rc < min_red_cost) {
1163 1174
                min_red_cost = rc;
1164 1175
              }
1165 1176
            }
1177
            }
1166 1178
            _pi[tip] -= min_red_cost + _epsilon;
1167 1179
            _next_out[tip] = _first_out[tip];
1168 1180
            ++relabel_cnt;
1169 1181

	
1170 1182
            // Step back
1171 1183
            if (tip != start) {
1172
              tip = _source[path.back()];
1184
              int pa = path.back();
1185
              path_arc[pa] = false;
1186
              tip = _source[pa];
1173 1187
              path.pop_back();
1174 1188
            }
1175 1189

	
1176 1190
          next_step: ;
1177 1191
          }
1178 1192

	
1179 1193
          // Augment along the found path (as much flow as possible)
1194
        augment:
1180 1195
          Value delta;
1181 1196
          int pa, u, v = start;
1182 1197
          for (int i = 0; i != int(path.size()); ++i) {
1183 1198
            pa = path[i];
1184 1199
            u = v;
1185 1200
            v = _target[pa];
1201
            path_arc[pa] = false;
1186 1202
            delta = std::min(_res_cap[pa], _excess[u]);
1187 1203
            _res_cap[pa] -= delta;
1188 1204
            _res_cap[_reverse[pa]] += delta;
1189 1205
            _excess[u] -= delta;
1190 1206
            _excess[v] += delta;
1191
            if (_excess[v] > 0 && _excess[v] <= delta)
1207
            if (_excess[v] > 0 && _excess[v] <= delta) {
1192 1208
              _active_nodes.push_back(v);
1193 1209
          }
1210
          }
1211
          path.clear();
1194 1212

	
1195 1213
          // Global update heuristic
1196
          if (relabel_cnt >= next_update_limit) {
1214
          if (relabel_cnt >= next_global_update_limit) {
1197 1215
            globalUpdate();
1198
            next_update_limit += global_update_freq;
1216
            next_global_update_limit += global_update_skip;
1199 1217
          }
1200 1218
        }
1219

	
1201 1220
      }
1221

	
1202 1222
    }
1203 1223

	
1204 1224
    /// Execute the algorithm performing push and relabel operations
1205 1225
    void startPush() {
1206 1226
      // Paramters for heuristics
1207 1227
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
1208 1228
      const double GLOBAL_UPDATE_FACTOR = 2.0;
1209 1229

	
1210
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
1230
      const int global_update_skip = static_cast<int>(GLOBAL_UPDATE_FACTOR *
1211 1231
        (_res_node_num + _sup_node_num * _sup_node_num));
1212
      int next_update_limit = global_update_freq;
1213

	
1214
      int relabel_cnt = 0;
1232
      int next_global_update_limit = global_update_skip;
1215 1233

	
1216 1234
      // Perform cost scaling phases
1217 1235
      BoolVector hyper(_res_node_num, false);
1218 1236
      LargeCostVector hyper_cost(_res_node_num);
1237
      int relabel_cnt = 0;
1219 1238
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
1220 1239
                                        1 : _epsilon / _alpha )
1221 1240
      {
1222 1241
        // Early termination heuristic
1223 1242
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
1224 1243
          if (earlyTermination()) break;
1225 1244
        }
1226 1245

	
1227 1246
        // Initialize current phase
1228 1247
        initPhase();
1229 1248

	
1230 1249
        // Perform push and relabel operations
1231 1250
        while (_active_nodes.size() > 0) {
1232 1251
          LargeCost min_red_cost, rc, pi_n;
1233 1252
          Value delta;
1234 1253
          int n, t, a, last_out = _res_arc_num;
1235 1254

	
1236 1255
        next_node:
1237 1256
          // Select an active node (FIFO selection)
1238 1257
          n = _active_nodes.front();
1239 1258
          last_out = _first_out[n+1];
1240 1259
          pi_n = _pi[n];
1241 1260

	
1242 1261
          // Perform push operations if there are admissible arcs
1243 1262
          if (_excess[n] > 0) {
1244 1263
            for (a = _next_out[n]; a != last_out; ++a) {
1245 1264
              if (_res_cap[a] > 0 &&
1246 1265
                  _cost[a] + pi_n - _pi[_target[a]] < 0) {
1247 1266
                delta = std::min(_res_cap[a], _excess[n]);
1248 1267
                t = _target[a];
1249 1268

	
1250 1269
                // Push-look-ahead heuristic
1251 1270
                Value ahead = -_excess[t];
1252 1271
                int last_out_t = _first_out[t+1];
1253 1272
                LargeCost pi_t = _pi[t];
1254 1273
                for (int ta = _next_out[t]; ta != last_out_t; ++ta) {
1255 1274
                  if (_res_cap[ta] > 0 &&
1256 1275
                      _cost[ta] + pi_t - _pi[_target[ta]] < 0)
1257 1276
                    ahead += _res_cap[ta];
1258 1277
                  if (ahead >= delta) break;
1259 1278
                }
1260 1279
                if (ahead < 0) ahead = 0;
1261 1280

	
1262 1281
                // Push flow along the arc
1263 1282
                if (ahead < delta && !hyper[t]) {
1264 1283
                  _res_cap[a] -= ahead;
1265 1284
                  _res_cap[_reverse[a]] += ahead;
1266 1285
                  _excess[n] -= ahead;
1267 1286
                  _excess[t] += ahead;
1268 1287
                  _active_nodes.push_front(t);
1269 1288
                  hyper[t] = true;
1270 1289
                  hyper_cost[t] = _cost[a] + pi_n - pi_t;
1271 1290
                  _next_out[n] = a;
1272 1291
                  goto next_node;
1273 1292
                } else {
1274 1293
                  _res_cap[a] -= delta;
1275 1294
                  _res_cap[_reverse[a]] += delta;
1276 1295
                  _excess[n] -= delta;
1277 1296
                  _excess[t] += delta;
1278 1297
                  if (_excess[t] > 0 && _excess[t] <= delta)
1279 1298
                    _active_nodes.push_back(t);
1280 1299
                }
1281 1300

	
1282 1301
                if (_excess[n] == 0) {
1283 1302
                  _next_out[n] = a;
1284 1303
                  goto remove_nodes;
1285 1304
                }
1286 1305
              }
1287 1306
            }
1288 1307
            _next_out[n] = a;
1289 1308
          }
1290 1309

	
1291 1310
          // Relabel the node if it is still active (or hyper)
1292 1311
          if (_excess[n] > 0 || hyper[n]) {
1293 1312
             min_red_cost = hyper[n] ? -hyper_cost[n] :
1294 1313
               std::numeric_limits<LargeCost>::max();
1295 1314
            for (int a = _first_out[n]; a != last_out; ++a) {
1315
              if (_res_cap[a] > 0) {
1296 1316
              rc = _cost[a] + pi_n - _pi[_target[a]];
1297
              if (_res_cap[a] > 0 && rc < min_red_cost) {
1317
                if (rc < min_red_cost) {
1298 1318
                min_red_cost = rc;
1299 1319
              }
1300 1320
            }
1321
            }
1301 1322
            _pi[n] -= min_red_cost + _epsilon;
1302 1323
            _next_out[n] = _first_out[n];
1303 1324
            hyper[n] = false;
1304 1325
            ++relabel_cnt;
1305 1326
          }
1306 1327

	
1307 1328
          // Remove nodes that are not active nor hyper
1308 1329
        remove_nodes:
1309 1330
          while ( _active_nodes.size() > 0 &&
1310 1331
                  _excess[_active_nodes.front()] <= 0 &&
1311 1332
                  !hyper[_active_nodes.front()] ) {
1312 1333
            _active_nodes.pop_front();
1313 1334
          }
1314 1335

	
1315 1336
          // Global update heuristic
1316
          if (relabel_cnt >= next_update_limit) {
1337
          if (relabel_cnt >= next_global_update_limit) {
1317 1338
            globalUpdate();
1318 1339
            for (int u = 0; u != _res_node_num; ++u)
1319 1340
              hyper[u] = false;
1320
            next_update_limit += global_update_freq;
1341
            next_global_update_limit += global_update_skip;
1321 1342
          }
1322 1343
        }
1323 1344
      }
1324 1345
    }
1325 1346

	
1326 1347
  }; //class CostScaling
1327 1348

	
1328 1349
  ///@}
1329 1350

	
1330 1351
} //namespace lemon
1331 1352

	
1332 1353
#endif //LEMON_COST_SCALING_H
0 comments (0 inline)