Skip to content

Commit

Permalink
Service can appear in at most 1 shipment relation
Browse files Browse the repository at this point in the history
  • Loading branch information
senhalil committed Mar 15, 2021
1 parent befcc2a commit d5597a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 8 additions & 4 deletions models/vrp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ def self.create(hash, delete = true)
end

def self.check_consistency(hash)
# shipment relation consistency
shipment_relations = hash[:relations]&.select{ |r| r[:type] == :shipment }&.flat_map{ |r| r[:linked_ids] }.to_a
unless shipment_relations.uniq!.nil?
raise OptimizerWrapper::DiscordantProblemError.new('A service cannot appear in more than one shipment relation')
end

# vehicle time cost consistency
if hash[:vehicles]&.any?{ |v| v[:cost_waiting_time_multiplier].to_f > (v[:cost_time_multiplier] || 1) }
raise OptimizerWrapper::DiscordantProblemError, 'cost_waiting_time_multiplier cannot be greater than cost_time_multiplier'
Expand All @@ -147,11 +153,9 @@ def self.check_consistency(hash)
# TODO: Active Hash should be checking this
[:matrices, :units, :points, :rests, :zones, :timewindows,
:vehicles, :services, :shipments, :subtours].each{ |key|
next if hash[key].to_a.collect{ |v| v[:id] }.uniq!.nil?
next if hash[key]&.collect{ |v| v[:id] }&.uniq!.nil?

raise OptimizerWrapper::DiscordantProblemError.new(
"#{key} IDs should be unique"
)
raise OptimizerWrapper::DiscordantProblemError.new("#{key} IDs should be unique")
}

# matrix_id consistency
Expand Down
13 changes: 13 additions & 0 deletions test/models/vrp_consistency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,18 @@ def test_dates_cannot_be_mixed_with_indices
Models::Vrp.filter(vrp)
end
end

def test_services_cannot_appear_in_more_than_one_shipment_relation
vrp = VRP.basic
vrp[:relations] = [
{ type: :shipment, linked_ids: %w[service_1 service_2] },
{ type: :shipment, linked_ids: %w[service_1 service_3] }
]
error = assert_raises OptimizerWrapper::DiscordantProblemError do
Models::Vrp.check_consistency(TestHelper.coerce(vrp))
end
expected_msg = 'A service cannot appear in more than one shipment relation'
assert_equal expected_msg, error.message, 'Error message does not match'
end
end
end
6 changes: 3 additions & 3 deletions test/models/vrp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ def test_available_interval
def test_no_lapse_in_relation
vrp = VRP.basic
vrp[:relations] = [{
type: 'vehicle_group_duration_on_months',
type: :vehicle_group_duration_on_months,
linked_vehicle_ids: ['vehicle_0']
}]

Models::Vrp.filter(vrp)
assert_empty vrp[:relations] # reject relation because lapse is mandatory

vrp[:relations] = [{
type: 'vehicle_group_duration_on_months',
type: :vehicle_group_duration_on_months,
linked_vehicle_ids: ['vehicle_0'],
lapse: 2
}]
Expand All @@ -298,7 +298,7 @@ def test_no_lapse_in_relation

vrp = VRP.lat_lon_two_vehicles
vrp[:relations] = [{
type: 'vehicle_trips',
type: :vehicle_trips,
linked_vehicle_ids: vrp[:vehicles].collect{ |v| v[:id] }
}]
Models::Vrp.filter(vrp)
Expand Down

0 comments on commit d5597a4

Please sign in to comment.