gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Bug fix in CostScaling (#417)
0 1 0
default
1 file changed with 2 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -862,97 +862,97 @@
862 862
        for (NodeIt n(_graph); n != INVALID; ++n) {
863 863
          _excess[_node_id[n]] = sup[n];
864 864
        }
865 865
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
866 866
          int u = _target[a];
867 867
          int ra = _reverse[a];
868 868
          _res_cap[a] = -_sum_supply + 1;
869 869
          _res_cap[ra] = -_excess[u];
870 870
          _cost[a] = 0;
871 871
          _cost[ra] = 0;
872 872
          _excess[u] = 0;
873 873
        }
874 874
      } else {
875 875
        for (ArcIt a(_graph); a != INVALID; ++a) {
876 876
          Value fa = flow[a];
877 877
          _res_cap[_arc_idf[a]] = cap[a] - fa;
878 878
          _res_cap[_arc_idb[a]] = fa;
879 879
        }
880 880
        for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
881 881
          int ra = _reverse[a];
882 882
          _res_cap[a] = 0;
883 883
          _res_cap[ra] = 0;
884 884
          _cost[a] = 0;
885 885
          _cost[ra] = 0;
886 886
        }
887 887
      }
888 888

	
889 889
      return OPTIMAL;
890 890
    }
891 891

	
892 892
    // Execute the algorithm and transform the results
893 893
    void start(Method method) {
894 894
      // Maximum path length for partial augment
895 895
      const int MAX_PATH_LENGTH = 4;
896 896

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

	
904 904
      // Execute the algorithm
905 905
      switch (method) {
906 906
        case PUSH:
907 907
          startPush();
908 908
          break;
909 909
        case AUGMENT:
910
          startAugment();
910
          startAugment(_res_node_num - 1);
911 911
          break;
912 912
        case PARTIAL_AUGMENT:
913 913
          startAugment(MAX_PATH_LENGTH);
914 914
          break;
915 915
      }
916 916

	
917 917
      // Compute node potentials for the original costs
918 918
      _arc_vec.clear();
919 919
      _cost_vec.clear();
920 920
      for (int j = 0; j != _res_arc_num; ++j) {
921 921
        if (_res_cap[j] > 0) {
922 922
          _arc_vec.push_back(IntPair(_source[j], _target[j]));
923 923
          _cost_vec.push_back(_scost[j]);
924 924
        }
925 925
      }
926 926
      _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end());
927 927

	
928 928
      typename BellmanFord<StaticDigraph, LargeCostArcMap>
929 929
        ::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map);
930 930
      bf.distMap(_pi_map);
931 931
      bf.init(0);
932 932
      bf.start();
933 933

	
934 934
      // Handle non-zero lower bounds
935 935
      if (_have_lower) {
936 936
        int limit = _first_out[_root];
937 937
        for (int j = 0; j != limit; ++j) {
938 938
          if (!_forward[j]) _res_cap[j] += _lower[j];
939 939
        }
940 940
      }
941 941
    }
942 942

	
943 943
    // Initialize a cost scaling phase
944 944
    void initPhase() {
945 945
      // Saturate arcs not satisfying the optimality condition
946 946
      for (int u = 0; u != _res_node_num; ++u) {
947 947
        int last_out = _first_out[u+1];
948 948
        LargeCost pi_u = _pi[u];
949 949
        for (int a = _first_out[u]; a != last_out; ++a) {
950 950
          int v = _target[a];
951 951
          if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) {
952 952
            Value delta = _res_cap[a];
953 953
            _excess[u] -= delta;
954 954
            _excess[v] += delta;
955 955
            _res_cap[a] = 0;
956 956
            _res_cap[_reverse[a]] += delta;
957 957
          }
958 958
        }
... ...
@@ -1039,97 +1039,97 @@
1039 1039
                int new_rank_v = old_rank_v;
1040 1040
                if (nrc < LargeCost(_max_rank))
1041 1041
                  new_rank_v = r + 1 + int(nrc);
1042 1042

	
1043 1043
                // Change the rank of v
1044 1044
                if (new_rank_v < old_rank_v) {
1045 1045
                  _rank[v] = new_rank_v;
1046 1046
                  _next_out[v] = _first_out[v];
1047 1047

	
1048 1048
                  // Remove v from its old bucket
1049 1049
                  if (old_rank_v < _max_rank) {
1050 1050
                    if (_buckets[old_rank_v] == v) {
1051 1051
                      _buckets[old_rank_v] = _bucket_next[v];
1052 1052
                    } else {
1053 1053
                      _bucket_next[_bucket_prev[v]] = _bucket_next[v];
1054 1054
                      _bucket_prev[_bucket_next[v]] = _bucket_prev[v];
1055 1055
                    }
1056 1056
                  }
1057 1057

	
1058 1058
                  // Insert v to its new bucket
1059 1059
                  _bucket_next[v] = _buckets[new_rank_v];
1060 1060
                  _bucket_prev[_buckets[new_rank_v]] = v;
1061 1061
                  _buckets[new_rank_v] = v;
1062 1062
                }
1063 1063
              }
1064 1064
            }
1065 1065
          }
1066 1066

	
1067 1067
          // Finish search if there are no more active nodes
1068 1068
          if (_excess[u] > 0) {
1069 1069
            total_excess -= _excess[u];
1070 1070
            if (total_excess <= 0) break;
1071 1071
          }
1072 1072
        }
1073 1073
        if (total_excess <= 0) break;
1074 1074
      }
1075 1075

	
1076 1076
      // Relabel nodes
1077 1077
      for (int u = 0; u != _res_node_num; ++u) {
1078 1078
        int k = std::min(_rank[u], r);
1079 1079
        if (k > 0) {
1080 1080
          _pi[u] -= _epsilon * k;
1081 1081
          _next_out[u] = _first_out[u];
1082 1082
        }
1083 1083
      }
1084 1084
    }
1085 1085

	
1086 1086
    /// Execute the algorithm performing augment and relabel operations
1087
    void startAugment(int max_length = std::numeric_limits<int>::max()) {
1087
    void startAugment(int max_length) {
1088 1088
      // Paramters for heuristics
1089 1089
      const int EARLY_TERM_EPSILON_LIMIT = 1000;
1090 1090
      const double GLOBAL_UPDATE_FACTOR = 3.0;
1091 1091

	
1092 1092
      const int global_update_freq = int(GLOBAL_UPDATE_FACTOR *
1093 1093
        (_res_node_num + _sup_node_num * _sup_node_num));
1094 1094
      int next_update_limit = global_update_freq;
1095 1095

	
1096 1096
      int relabel_cnt = 0;
1097 1097

	
1098 1098
      // Perform cost scaling phases
1099 1099
      std::vector<int> path;
1100 1100
      for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ?
1101 1101
                                        1 : _epsilon / _alpha )
1102 1102
      {
1103 1103
        // Early termination heuristic
1104 1104
        if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
1105 1105
          if (earlyTermination()) break;
1106 1106
        }
1107 1107

	
1108 1108
        // Initialize current phase
1109 1109
        initPhase();
1110 1110

	
1111 1111
        // Perform partial augment and relabel operations
1112 1112
        while (true) {
1113 1113
          // Select an active node (FIFO selection)
1114 1114
          while (_active_nodes.size() > 0 &&
1115 1115
                 _excess[_active_nodes.front()] <= 0) {
1116 1116
            _active_nodes.pop_front();
1117 1117
          }
1118 1118
          if (_active_nodes.size() == 0) break;
1119 1119
          int start = _active_nodes.front();
1120 1120

	
1121 1121
          // Find an augmenting path from the start node
1122 1122
          path.clear();
1123 1123
          int tip = start;
1124 1124
          while (_excess[tip] >= 0 && int(path.size()) < max_length) {
1125 1125
            int u;
1126 1126
            LargeCost min_red_cost, rc, pi_tip = _pi[tip];
1127 1127
            int last_out = _first_out[tip+1];
1128 1128
            for (int a = _next_out[tip]; a != last_out; ++a) {
1129 1129
              u = _target[a];
1130 1130
              if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) {
1131 1131
                path.push_back(a);
1132 1132
                _next_out[tip] = a;
1133 1133
                tip = u;
1134 1134
                goto next_step;
1135 1135
              }
0 comments (0 inline)