Skip to content

Commit

Permalink
small refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsecadeline committed Jun 7, 2021
1 parent 22aa7e1 commit d728545
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 62 deletions.
11 changes: 6 additions & 5 deletions lib/heuristics/concerns/scheduling_data_initialisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ module SchedulingDataInitialization
def generate_route_structure(vrp)
vrp.vehicles.each{ |vehicle|
capacity = compute_capacities(vehicle[:capacities], true)
vrp.units.reject{ |unit| capacity.has_key?(unit[:id]) }.each{ |unit| capacity[unit[:id]] = 0.0 }
vrp.units.reject{ |unit| capacity.key?(unit[:id]) }.each{ |unit| capacity[unit[:id]] = 0.0 }
@candidate_routes[vehicle.original_id][vehicle.global_day_index] = {
vehicle_id: vehicle.id,
global_day_index: vehicle.global_day_index,
tw_start: (vehicle.timewindow.start < 84600) ? vehicle.timewindow.start : vehicle.timewindow.start - vehicle.global_day_index * 86400,
tw_end: (vehicle.timewindow.end < 84600) ? vehicle.timewindow.end : vehicle.timewindow.end - vehicle.global_day_index * 86400,
# vehicle: vehicle # it is costly to use this
vehicle_original_id: vehicle.original_id,
day: vehicle.global_day_index,
tw_start: vehicle.timewindow.start % 86400,
tw_end: vehicle.timewindow.end % 86400,
start_point_id: vehicle.start_point&.id,
end_point_id: vehicle.end_point&.id,
duration: vehicle.duration || (vehicle.timewindow.end - vehicle.timewindow.start),
Expand Down
11 changes: 7 additions & 4 deletions lib/heuristics/concerns/scheduling_end_phase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def refine_solution(&block)
add_missing_visits
end

unless @services_data.all?{ |_id, data| (data[:raw].exclusion_cost || 0).zero? }
unless @services_data.all?{ |_id, data| data[:raw].exclusion_cost.to_f.zero? }
block&.call(nil, nil, nil, 'scheduling heuristic - correcting underfilled routes', nil, nil, nil)
correct_underfilled_routes
end
Expand Down Expand Up @@ -239,7 +239,9 @@ def empty_underfilled

all_empty = false

next if route_data[:stops].sum{ |stop| @services_data[stop[:id]][:raw].exclusion_cost || 0 } >= route_data[:cost_fixed]
next if route_data[:stops].sum{ |stop|
@services_data[stop[:id]][:raw].exclusion_cost.to_f
} >= route_data[:cost_fixed]

smth_removed = true
locally_removed = route_data[:stops].collect{ |stop|
Expand Down Expand Up @@ -409,7 +411,8 @@ def generate_new_routes(still_removed)
if insertion_costs.flatten.empty?
keep_inserting = false
empty_routes.delete_if{ |tab| tab[:vehicle_id] == best_route[:vehicle_id] && tab[:day] == best_route[:day] }
if route_data[:stops].empty? || route_data[:stops].sum{ |stop| @services_data[stop[:id]][:raw].exclusion_cost || 0 } < route_data[:cost_fixed]
if route_data[:stops].empty? ||
route_data[:stops].sum{ |stop| @services_data[stop[:id]][:raw].exclusion_cost.to_f } < route_data[:cost_fixed]
route_data[:stops] = []
previous_vehicle_filled_info = {
stores: best_route[:stores],
Expand All @@ -420,7 +423,7 @@ def generate_new_routes(still_removed)
route_data[:stops].each{ |stop|
@ids_to_renumber |= [stop[:id]]
still_removed.delete(still_removed.find{ |removed| removed.first == stop[:id] })
@output_tool&.add_single_visit(route_data[:global_day_index], @services_data[stop[:id]][:used_days], stop[:id], @services_data[stop[:id]][:raw].visits_number)
@output_tool&.add_single_visit(route_data[:day], @services_data[stop[:id]][:used_days], stop[:id], @services_data[stop[:id]][:raw].visits_number)
@uninserted.delete(@uninserted.find{ |_id, data| data[:original_id] == stop[:id] }[0])
}
end
Expand Down
Loading

0 comments on commit d728545

Please sign in to comment.