diff --git a/models/concerns/vrp_result.rb b/models/concerns/vrp_result.rb index eeca40303..3160ddf9e 100644 --- a/models/concerns/vrp_result.rb +++ b/models/concerns/vrp_result.rb @@ -35,7 +35,7 @@ def vrp_result(options = nil) include_root_in_json end - hash = serializable_hash(options).vrp_result + hash = serializable_hash(options).vrp_result(options) if root root = model_name.element if root == true { root => hash } diff --git a/models/solution/parsers/step_parser.rb b/models/solution/parsers/step_parser.rb index 8d2f1d01a..53da278af 100644 --- a/models/solution/parsers/step_parser.rb +++ b/models/solution/parsers/step_parser.rb @@ -20,13 +20,15 @@ module Parsers class ServiceParser def self.parse(service, options) + activity = options[:index] && service.activities[options[:index]] || service.activity activity_hash = Models::Activity.field_names.map{ |key| - activity = options[:index] && service.activities[options[:index]] || service.activity + next if key == :point_id + [key, activity.send(key)] - }.to_h + }.compact.to_h dup_activity = Models::Activity.new(activity_hash) - dup_activity[:simplified_setup_duration] = service.activity[:simplified_setup_duration] + dup_activity[:simplified_setup_duration] = activity[:simplified_setup_duration] if activity[:simplified_setup_duration] { id: service.original_id, diff --git a/models/solution/route.rb b/models/solution/route.rb index 8c4f619cc..e3a9f0ccc 100644 --- a/models/solution/route.rb +++ b/models/solution/route.rb @@ -53,9 +53,9 @@ def count_services steps.count(&:service_id) end - def insert_step(vrp, step_object, index) + def insert_step(vrp, step_object, index, idle_time = 0) steps.insert(index, step_object) - shift_route_times(step_object.activity.duration, index) + shift_route_times(idle_time + step_object.activity.duration, index) end def shift_route_times(shift_amount, shift_start_index = 0) diff --git a/models/solution/solution.rb b/models/solution/solution.rb index bb8a591de..d8ff5d6f6 100644 --- a/models/solution/solution.rb +++ b/models/solution/solution.rb @@ -97,8 +97,8 @@ def update_costs self.cost -= (previous_total - cost_info.total).round end - def insert_step(vrp, route, step_object, index) - route.insert_step(vrp, step_object, index) + def insert_step(vrp, route, step_object, index, idle_time = 0) + route.insert_step(vrp, step_object, index, idle_time) Parsers::SolutionParser.parse(self, vrp) end end diff --git a/test/wrapper_test.rb b/test/wrapper_test.rb index e9434184f..c2f3a1af8 100644 --- a/test/wrapper_test.rb +++ b/test/wrapper_test.rb @@ -3038,8 +3038,7 @@ def test_empty_result_when_no_vehicle assert_equal 0, corresponding_in_route.activity.timewindows.first.start assert_equal 10, corresponding_in_route.activity.timewindows.first.end - # Services which can be served have their timewindows interpreted and the day index is removed - assert_nil corresponding_in_route.activity.timewindows.first.day_index + assert_equal 0, corresponding_in_route.activity.timewindows.first.day_index corresponding_unassigned = solutions[0].unassigned.find{ |un| un.id == vrp[:services][1][:id] } assert_equal 30, corresponding_unassigned.activity.timewindows.first.start diff --git a/wrappers/wrapper.rb b/wrappers/wrapper.rb index cf863b845..c1eb1eba7 100644 --- a/wrappers/wrapper.rb +++ b/wrappers/wrapper.rb @@ -1254,13 +1254,13 @@ def simplify_vehicle_pause(vrp, result = nil, options = { mode: :simplify }) end else # there is a clear position to insert - activity_after_rest = route.steps[insert_rest_at] + step_after_rest = route.steps[insert_rest_at] - rest_start = activity_after_rest.info.begin_time + rest_start = step_after_rest.info.begin_time # if this the first service of this location then we need to consider the setup_duration - rest_start -= activity_after_rest.activity.setup_duration.to_i if activity_after_rest.info.travel_time > 0 + rest_start -= step_after_rest.activity.setup_duration.to_i if step_after_rest.info.travel_time > 0 if rest.timewindows&.last&.end && rest_start > rest.timewindows.last.end - rest_start -= activity_after_rest.info.travel_time + rest_start -= step_after_rest.info.travel_time rest_start = [rest_start, rest.timewindows&.first&.start.to_i].max # don't induce idle_time if within travel_time end @@ -1283,18 +1283,16 @@ def simplify_vehicle_pause(vrp, result = nil, options = { mode: :simplify }) end times = { begin_time: rest_start, end_time: rest_start + rest.duration, departure_time: rest_start + rest.duration } rest_step = Models::Solution::Step.new(rest, info: Models::Solution::Step::Info.new(times)) - result.insert_step(vrp, route, rest_step, insert_rest_at) - + result.insert_step(vrp, route, rest_step, insert_rest_at, idle_time_created_by_inserted_pause) # shift_route_times(route, idle_time_created_by_inserted_pause + rest.duration, insert_rest_at + 1) next if no_cost cost_increase = vehicle.cost_time_multiplier.to_f * rest.duration + vehicle.cost_waiting_time_multiplier.to_f * idle_time_created_by_inserted_pause - - route[:cost_info]&.time += cost_increase - result[:cost_info]&.time += cost_increase - result[:cost] += cost_increase # totals are not calculated yet + route.cost_info&.time += cost_increase + result.cost_info&.time += cost_increase + result.cost += cost_increase # totals are not calculated yet } } else