Skip to content

Commit

Permalink
Merge pull request 428 from braktar/dev
Browse files Browse the repository at this point in the history
Expose the effective setup_time
  • Loading branch information
senhalil authored Jun 27, 2022
2 parents 0fba578 + bf21b16 commit 29851a5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/v01/entities/vrp_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class VrpResultSolutionRouteActivities < Grape::Entity
expose :travel_time, documentation: { type: Integer, desc: 'Travel time from previous point (in s)' }
expose :travel_value, documentation: { type: Integer, desc: 'Travel value from previous point' }
expose :waiting_time, documentation: { type: Integer, desc: 'Idle time (in s)' }
expose :setup_time, documentation: { type: Integer, desc: 'Effective setup duration (in s)'}
expose :begin_time, documentation: { type: Integer, desc: 'Time visit starts' }
expose :end_time, documentation: { type: Integer, desc: 'Time visit ends' }
expose :departure_time, documentation: { type: Integer, desc: '' }
Expand Down
16 changes: 16 additions & 0 deletions models/solution/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
module Models
class Solution < Base
class Route < Base
MISSION_TYPES = [:service, :pickup, :delivery].freeze
field :geometry

has_many :stops, class_name: 'Models::Solution::Stop'
Expand All @@ -38,6 +39,7 @@ def vrp_result(options = {})
options[:vehicle] = vehicle
hash = super(options)
hash['activities'] = hash.delete('stops')
compute_setup_times(hash)
hash['cost_details'] = hash.delete('cost_info')
hash['detail'] = hash.delete('info')
hash.merge!(info.vrp_result(options))
Expand All @@ -51,6 +53,7 @@ def vrp_result(options = {})
stop = Models::Solution::Stop.new(stop) if stop.is_a? Hash
stop.info.router_mode ||= vehicle&.router_mode
stop.info.speed_multiplier ||= vehicle&.speed_multiplier
stop.compute_info_end_time(vehicle: vehicle)
stop
}
end
Expand All @@ -64,6 +67,19 @@ def insert_stop(_vrp, stop, index, idle_time = 0)
shift_route_times(idle_time + stop.activity.duration, index)
end

def compute_setup_times(hash)
previous_point_id = nil
stops.each.with_index{ |stop, stop_index|
hash['activities'][stop_index]['setup_time'] =
if MISSION_TYPES.include?(stop.type) && stop.activity.point_id != previous_point_id
stop.activity.setup_duration_on(vehicle)
else
0
end
previous_point_id = stop.activity.point_id if stop.activity.point_id
}
end

def shift_route_times(shift_amount, shift_start_index = 0)
return if shift_amount == 0

Expand Down
5 changes: 2 additions & 3 deletions models/solution/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def initialize(object, options = {})
raise 'A route stop cannot be nil' unless parsed_object

super(parsed_object)
set_info_end_time
end

def vrp_result(options = {})
Expand All @@ -79,8 +78,8 @@ def vrp_result(options = {})
hash
end

def set_info_end_time
info.end_time = info.begin_time + activity.duration
def compute_info_end_time(options)
info.end_time = info.begin_time + activity.duration_on(service_id ? options[:vehicle] : nil)
info.departure_time = info.end_time
end

Expand Down
56 changes: 56 additions & 0 deletions test/api/v01/output_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,62 @@ def test_basic_output
}
end

def test_setup_duration_timings
vrp = VRP.basic

vrp[:matrices].first[:time] = [
[0, 4, 0, 5],
[6, 0, 0, 5],
[1, 0, 0, 5],
[5, 5, 5, 0]
]

vrp[:services] << { id: 'service_4', activity: { point_id: 'point_1' } }
vrp[:services].each{ |service|
service[:activity][:setup_duration] = 1
service[:activity][:duration] = 2
}
vrp[:services].first[:activity][:timewindows] = [{ start: 50, end: 55 }]
vrp[:vehicles].first[:force_start] = true
vrp[:vehicles].first[:additional_setup] = 3
vrp[:vehicles].first[:additional_service] = 4

response = post '/0.1/vrp/submit', { api_key: 'solvers', vrp: vrp }.to_json, 'CONTENT_TYPE' => 'application/json'
result = JSON.parse(response.body, symbolize_names: true)

route = result[:solutions].first[:routes].first
travel_times, setup_times, waiting_times, begin_times, departure_times = [], [], [], [], []
expected_durations, expected_setup_durations = [], []

previous_point_id = nil
route[:activities].each{ |act|
travel_times << act[:travel_time]
setup_times << act[:setup_time]
waiting_times << act[:waiting_time]
begin_times << act[:begin_time]
departure_times << act[:departure_time]

expected_durations << act[:detail][:duration] +
(act[:service_id] ? vrp[:vehicles].first[:additional_service] : 0)
expected_setup_durations <<
if act[:service_id] && act[:point_id] != previous_point_id
act[:detail][:setup_duration] + vrp[:vehicles].first[:additional_setup]
else
0
end

previous_point_id = act[:point_id] unless act[:rest_id]
}

begin_times.each.with_index{ |b_t, index|
assert_equal setup_times[index], expected_setup_durations[index]
assert_equal departure_times[index], b_t + expected_durations[index]
next if index == 0

assert_equal b_t, departure_times[index - 1] + travel_times[index] + waiting_times[index] + setup_times[index]
}
end

def test_day_week_num_and_other_periodic_fields
vrp = VRP.periodic
vrp[:services].first[:visits_number] = 2
Expand Down
6 changes: 6 additions & 0 deletions wrappers/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,8 @@ def simplify_service_setup_duration_and_vehicle_setup_modifiers(vrp, solution =
overall_total_travel_time_correction = 0
solution.routes.each{ |route|
vehicle = vehicles_grouped_by_vehicle_id[route[:vehicle_id]].first
vehicle.coef_setup = vehicle[:simplified_coef_setup]
vehicle.additional_setup = vehicle[:simplified_additional_setup]
total_travel_time_correction = 0
previous_point_id = nil
route.stops.each{ |stop|
Expand All @@ -1602,6 +1604,10 @@ def simplify_service_setup_duration_and_vehicle_setup_modifiers(vrp, solution =

overall_total_travel_time_correction += total_travel_time_correction
route.info.total_travel_time -= total_travel_time_correction.round

# Solution patcher must let the problem untouched
vehicle.coef_setup = 1
vehicle.additional_setup = 0
}
solution.info.total_travel_time -= overall_total_travel_time_correction.round

Expand Down

0 comments on commit 29851a5

Please sign in to comment.