diff --git a/optimizer_wrapper.rb b/optimizer_wrapper.rb index 81d610afb..74550fb83 100644 --- a/optimizer_wrapper.rb +++ b/optimizer_wrapper.rb @@ -661,6 +661,26 @@ def self.zip_cluster(vrp, cluster_threshold, force_cluster) new_services = Array.new(new_size) clusterer.clusters.each_with_index do |cluster, i| new_services[i] = vrp.services[cluster.data_items[0][0]] + cluster_ids = cluster.data_items.map{ |arr| vrp.services[arr[0]].id } + route_index = vrp.routes.index{ |route| route.mission_ids & cluster_ids } + if route_index + ref_id = vrp.routes[route_index].mission_ids.find{ |mission_id| + cluster_ids.include?(mission_id) + } + vrp.routes.each{ |route| + route.mission_ids.delete_if{ |mission_id| + cluster_ids.include?(mission_id) unless mission_id == ref_id + } + } + vrp.routes[route_index].mission_ids.map!{ |mission_id| + if cluster_ids.include?(mission_id) + new_services[i].id + else + mission_id + end + } + end + new_services[i].activity.duration = cluster.data_items.map{ |di| vrp.services[di[0]].activity.duration }.reduce(&:+) next unless force_cluster diff --git a/test/fixtures/pud_initial_routes.json b/test/fixtures/pud_initial_routes.json index 4112f10d9..5b2422edd 100644 --- a/test/fixtures/pud_initial_routes.json +++ b/test/fixtures/pud_initial_routes.json @@ -656,10 +656,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_1", @@ -677,10 +674,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_2", @@ -698,10 +692,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_3", @@ -719,10 +710,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_4", @@ -740,10 +728,7 @@ "point_id": "point_2", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_5", @@ -761,10 +746,7 @@ "point_id": "point_2", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_6", @@ -782,10 +764,7 @@ "point_id": "point_0", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_7", @@ -803,10 +782,7 @@ "point_id": "point_0", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_8", @@ -824,10 +800,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_9", @@ -845,10 +818,7 @@ "point_id": "point_0", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_10", @@ -866,10 +836,7 @@ "point_id": "point_2", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_11", @@ -887,10 +854,7 @@ "point_id": "point_2", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_12", @@ -908,10 +872,7 @@ "point_id": "point_2", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_13", @@ -929,10 +890,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_14", @@ -950,10 +908,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_15", @@ -971,10 +926,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_16", @@ -992,10 +944,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_17", @@ -1013,10 +962,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_18", @@ -1034,10 +980,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_19", @@ -1055,10 +998,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_20", @@ -1076,10 +1016,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_21", @@ -1097,10 +1034,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_22", @@ -1118,10 +1052,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_23", @@ -1139,10 +1070,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_24", @@ -1160,10 +1088,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_25", @@ -1181,10 +1106,7 @@ "point_id": "point_13", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_26", @@ -1202,10 +1124,7 @@ "point_id": "point_6", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_27", @@ -1223,10 +1142,7 @@ "point_id": "point_6", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_28", @@ -1244,10 +1160,7 @@ "point_id": "point_6", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_29", @@ -1265,10 +1178,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 }, { "id": "shipment_30", @@ -1286,10 +1196,7 @@ "point_id": "point_7", "setup_duration": 2700 }, - "maximum_inroute_duration": 6300, - "skills": [ - "external" - ] + "maximum_inroute_duration": 6300 } ], "relations": [], diff --git a/test/wrapper_test.rb b/test/wrapper_test.rb index 935c93481..9d203b36d 100644 --- a/test/wrapper_test.rb +++ b/test/wrapper_test.rb @@ -30,6 +30,19 @@ def test_zip_cluster assert_equal 2, OptimizerWrapper.send(:zip_cluster, TestHelper.create(problem), 5, false).size end + def test_zip_cluster_with_routes + problem = VRP.basic_threshold + problem[:services].each{ |s| + s[:activity][:timewindows] = [{ + start: 1, + end: 2 + }] + s[:skills] = ['A'] + } + problem[:routes] = [{ mission_ids: (1..4).map{ |id| "service_#{id}" }}] + assert_equal 2, OptimizerWrapper.send(:zip_cluster, TestHelper.create(problem), 5, false).size + end + def test_no_zip_cluster size = 5 problem = { diff --git a/wrappers/wrapper.rb b/wrappers/wrapper.rb index 6754957cd..cff1e0a6f 100644 --- a/wrappers/wrapper.rb +++ b/wrappers/wrapper.rb @@ -479,6 +479,9 @@ def add_unassigned(unfeasible, vrp, service, reason) add_unassigned(unfeasible, vrp, service_in, "In a relation with an unfeasible service: #{service.id}") } } + vrp.routes.each{ |route| + route.mission_ids.delete_if{ |mission_id| mission_id == service.id } + } end unfeasible