Skip to content

Commit

Permalink
Convert bindump hash result to solution model
Browse files Browse the repository at this point in the history
  • Loading branch information
Braktar committed Jul 6, 2021
1 parent 431a87a commit 32c06d7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/interpreters/split_clustering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ def self.remove_poorly_populated_routes(vrp, solution, limit)
vehicle.timewindow&.end && (vehicle.timewindow.end - vehicle.timewindow.start)
route_duration = route.detail.total_time ||
(route.activities.last.timing.begin_time - route.activities.first.timing.begin_time)

log "route #{route.vehicle.id} time: #{route_duration}/#{vehicle_worktime} percent: " \
"#{((route_duration / (vehicle_worktime || route_duration).to_f) * 100).to_i}%", level: :info

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion test/lib/interpreters/split_clustering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def test_max_split_poorly_populated_route_limit_result
result = Marshal.load(File.binread('test/fixtures/max_split_poorly_populated_route_limit_result.bindump')) # rubocop: disable Security/MarshalLoad
Interpreters::SplitClustering.remove_poor_routes(vrp, result)

assert_equal 0, result[:unassigned].size, 'remove_poor_routes should not remove any services from this result'
assert_equal 0, result.unassigned.size, 'remove_poor_routes should not remove any services from this result'
end

def test_max_split_functionality
Expand Down
56 changes: 56 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,62 @@ def self.load_vrps(test, options = {})
vrps
end

def self.convert_bin_result(vrp, filename)
result = Marshal.load(File.binread("test/fixtures/#{filename}.bindump"))
solution = hash_result_conversion(vrp, result) if result.is_a?(Hash)
bin_dump(filename, solution)
solution
end

def self.hash_result_conversion(vrp, result)
routes = result[:routes].map{ |route|
time_detail = Models::RouteDetail.new(start_time: route[:start_time],
end_time: route[:end_time],
total_time: route[:total_time],
total_travel_time: route[:total_travel_time],
total_distance: route[:total_distance])
loads = route[:initial_loads].map{ |c_load|
c_unit = Models::Unit.new(id: c_load[:unit_id])
Models::Load.new(current: c_load[:value],
quantity: Models::Quantity.new(unit: c_unit, value: 0))
}
vehicle = vrp.vehicles.find{ |v| v.id == route[:vehicle_id] }
activities = route[:activities].map.with_index{ |act, a_i|
times = Models::Timing.new(begin_time: act[:begin_time], end_time: act[:end_time] || act[:departure_time],
travel_time: act[:travel_time], travel_distance: act[:travel_distance])
loads = act[:detail][:quantities]&.map{ |c_load|
c_unit = Models::Unit.new(id: c_load[:unit])
Models::Load.new(current: c_load[:current_load],
quantity: Models::Quantity.new(unit: c_unit, value: c_load[:value]))
}

if act[:service_id]
service = vrp.services.find{ |s| s.id == act[:service_id] }
service.route_activity(loads: loads, timing: times)
elsif act[:rest_id]
c_rest = vrp.rests.find{ |rest| rest.id == act[:rest_id] }
c_rest.route_activity(loads: loads)
elsif a_i.zero?
vehicle.start_depot_activity(loads: loads, timing: times)
else
vehicle.end_depot_activity(loads: loads, timing: times)
end
}.compact

Models::SolutionRoute.new(activities: activities, vehicle: vehicle, initial_loads: loads, detail: time_detail)
}

unassigned = result[:unassigned].map{ |act|
service = vrp.services.find{ |s| s.id == act[:service_id] }
service.route_activity
}
Models::Solution.new(routes: routes, unassigned: unassigned)
end

def self.bin_dump(filename, content)
File.open("test/fixtures/#{filename}.bindump", 'wb') { |f| f.write(Marshal.dump(content)) }
end

def self.expand_vehicles(vrp)
periodic = Interpreters::PeriodicVisits.new(vrp)
periodic.generate_vehicles(vrp)
Expand Down

0 comments on commit 32c06d7

Please sign in to comment.