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 10, 2021
1 parent 46369ca commit cc891fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 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
if shipment_relations.size != shipment_relations.uniq.size
raise OptimizerWrapper::DiscordantProblemError, '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 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 cc891fe

Please sign in to comment.