gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Bug fix in NetworkSimplex (#234)
0 1 0
default
1 file changed with 7 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -1099,154 +1099,155 @@
1099 1099
      // Initialize node related data
1100 1100
      bool valid_supply = true;
1101 1101
      Flow sum_supply = 0;
1102 1102
      if (!_pstsup && !_psupply) {
1103 1103
        _pstsup = true;
1104 1104
        _psource = _ptarget = NodeIt(_graph);
1105 1105
        _pstflow = 0;
1106 1106
      }
1107 1107
      if (_psupply) {
1108 1108
        int i = 0;
1109 1109
        for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
1110 1110
          _node_id[n] = i;
1111 1111
          _supply[i] = (*_psupply)[n];
1112 1112
          sum_supply += _supply[i];
1113 1113
        }
1114 1114
        valid_supply = (_ptype == GEQ && sum_supply <= 0) ||
1115 1115
                       (_ptype == LEQ && sum_supply >= 0);
1116 1116
      } else {
1117 1117
        int i = 0;
1118 1118
        for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
1119 1119
          _node_id[n] = i;
1120 1120
          _supply[i] = 0;
1121 1121
        }
1122 1122
        _supply[_node_id[_psource]] =  _pstflow;
1123 1123
        _supply[_node_id[_ptarget]] = -_pstflow;
1124 1124
      }
1125 1125
      if (!valid_supply) return false;
1126 1126

	
1127 1127
      // Infinite capacity value
1128 1128
      Flow inf_cap =
1129 1129
        std::numeric_limits<Flow>::has_infinity ?
1130 1130
        std::numeric_limits<Flow>::infinity() :
1131 1131
        std::numeric_limits<Flow>::max();
1132 1132

	
1133 1133
      // Initialize artifical cost
1134 1134
      Cost art_cost;
1135 1135
      if (std::numeric_limits<Cost>::is_exact) {
1136 1136
        art_cost = std::numeric_limits<Cost>::max() / 4 + 1;
1137 1137
      } else {
1138 1138
        art_cost = std::numeric_limits<Cost>::min();
1139 1139
        for (int i = 0; i != _arc_num; ++i) {
1140 1140
          if (_cost[i] > art_cost) art_cost = _cost[i];
1141 1141
        }
1142 1142
        art_cost = (art_cost + 1) * _node_num;
1143 1143
      }
1144 1144

	
1145 1145
      // Run Circulation to check if a feasible solution exists
1146 1146
      typedef ConstMap<Arc, Flow> ConstArcMap;
1147
      ConstArcMap zero_arc_map(0), inf_arc_map(inf_cap);
1147 1148
      FlowNodeMap *csup = NULL;
1148 1149
      bool local_csup = false;
1149 1150
      if (_psupply) {
1150 1151
        csup = _psupply;
1151 1152
      } else {
1152 1153
        csup = new FlowNodeMap(_graph, 0);
1153 1154
        (*csup)[_psource] =  _pstflow;
1154 1155
        (*csup)[_ptarget] = -_pstflow;
1155 1156
        local_csup = true;
1156 1157
      }
1157 1158
      bool circ_result = false;
1158 1159
      if (_ptype == GEQ || (_ptype == LEQ && sum_supply == 0)) {
1159 1160
        // GEQ problem type
1160 1161
        if (_plower) {
1161 1162
          if (_pupper) {
1162 1163
            Circulation<GR, FlowArcMap, FlowArcMap, FlowNodeMap>
1163 1164
              circ(_graph, *_plower, *_pupper, *csup);
1164 1165
            circ_result = circ.run();
1165 1166
          } else {
1166 1167
            Circulation<GR, FlowArcMap, ConstArcMap, FlowNodeMap>
1167
              circ(_graph, *_plower, ConstArcMap(inf_cap), *csup);
1168
              circ(_graph, *_plower, inf_arc_map, *csup);
1168 1169
            circ_result = circ.run();
1169 1170
          }
1170 1171
        } else {
1171 1172
          if (_pupper) {
1172 1173
            Circulation<GR, ConstArcMap, FlowArcMap, FlowNodeMap>
1173
              circ(_graph, ConstArcMap(0), *_pupper, *csup);
1174
              circ(_graph, zero_arc_map, *_pupper, *csup);
1174 1175
            circ_result = circ.run();
1175 1176
          } else {
1176 1177
            Circulation<GR, ConstArcMap, ConstArcMap, FlowNodeMap>
1177
              circ(_graph, ConstArcMap(0), ConstArcMap(inf_cap), *csup);
1178
              circ(_graph, zero_arc_map, inf_arc_map, *csup);
1178 1179
            circ_result = circ.run();
1179 1180
          }
1180 1181
        }
1181 1182
      } else {
1182 1183
        // LEQ problem type
1183 1184
        typedef ReverseDigraph<const GR> RevGraph;
1184 1185
        typedef NegMap<FlowNodeMap> NegNodeMap;
1185 1186
        RevGraph rgraph(_graph);
1186 1187
        NegNodeMap neg_csup(*csup);
1187 1188
        if (_plower) {
1188 1189
          if (_pupper) {
1189 1190
            Circulation<RevGraph, FlowArcMap, FlowArcMap, NegNodeMap>
1190 1191
              circ(rgraph, *_plower, *_pupper, neg_csup);
1191 1192
            circ_result = circ.run();
1192 1193
          } else {
1193 1194
            Circulation<RevGraph, FlowArcMap, ConstArcMap, NegNodeMap>
1194
              circ(rgraph, *_plower, ConstArcMap(inf_cap), neg_csup);
1195
              circ(rgraph, *_plower, inf_arc_map, neg_csup);
1195 1196
            circ_result = circ.run();
1196 1197
          }
1197 1198
        } else {
1198 1199
          if (_pupper) {
1199 1200
            Circulation<RevGraph, ConstArcMap, FlowArcMap, NegNodeMap>
1200
              circ(rgraph, ConstArcMap(0), *_pupper, neg_csup);
1201
              circ(rgraph, zero_arc_map, *_pupper, neg_csup);
1201 1202
            circ_result = circ.run();
1202 1203
          } else {
1203 1204
            Circulation<RevGraph, ConstArcMap, ConstArcMap, NegNodeMap>
1204
              circ(rgraph, ConstArcMap(0), ConstArcMap(inf_cap), neg_csup);
1205
              circ(rgraph, zero_arc_map, inf_arc_map, neg_csup);
1205 1206
            circ_result = circ.run();
1206 1207
          }
1207 1208
        }
1208 1209
      }
1209 1210
      if (local_csup) delete csup;
1210 1211
      if (!circ_result) return false;
1211 1212

	
1212 1213
      // Set data for the artificial root node
1213 1214
      _root = _node_num;
1214 1215
      _parent[_root] = -1;
1215 1216
      _pred[_root] = -1;
1216 1217
      _thread[_root] = 0;
1217 1218
      _rev_thread[0] = _root;
1218 1219
      _succ_num[_root] = all_node_num;
1219 1220
      _last_succ[_root] = _root - 1;
1220 1221
      _supply[_root] = -sum_supply;
1221 1222
      if (sum_supply < 0) {
1222 1223
        _pi[_root] = -art_cost;
1223 1224
      } else {
1224 1225
        _pi[_root] = art_cost;
1225 1226
      }
1226 1227

	
1227 1228
      // Store the arcs in a mixed order
1228 1229
      int k = std::max(int(sqrt(_arc_num)), 10);
1229 1230
      int i = 0;
1230 1231
      for (ArcIt e(_graph); e != INVALID; ++e) {
1231 1232
        _arc_ref[i] = e;
1232 1233
        if ((i += k) >= _arc_num) i = (i % k) + 1;
1233 1234
      }
1234 1235

	
1235 1236
      // Initialize arc maps
1236 1237
      if (_pupper && _pcost) {
1237 1238
        for (int i = 0; i != _arc_num; ++i) {
1238 1239
          Arc e = _arc_ref[i];
1239 1240
          _source[i] = _node_id[_graph.source(e)];
1240 1241
          _target[i] = _node_id[_graph.target(e)];
1241 1242
          _cap[i] = (*_pupper)[e];
1242 1243
          _cost[i] = (*_pcost)[e];
1243 1244
          _flow[i] = 0;
1244 1245
          _state[i] = STATE_LOWER;
1245 1246
        }
1246 1247
      } else {
1247 1248
        for (int i = 0; i != _arc_num; ++i) {
1248 1249
          Arc e = _arc_ref[i];
1249 1250
          _source[i] = _node_id[_graph.source(e)];
1250 1251
          _target[i] = _node_id[_graph.target(e)];
1251 1252
          _flow[i] = 0;
1252 1253
          _state[i] = STATE_LOWER;
0 comments (0 inline)