Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assert quantity issues #300

Merged
merged 3 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions test/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,23 @@ def test_assert_applicable_for_vroom_if_initial_routes
assert_empty OptimizerWrapper.config[:services][:vroom].inapplicable_solve?(vrp)
end

def test_assert_can_handle_empty_quantities
problem = VRP.basic
problem[:relations] = [{
type: :shipment,
linked_ids: ['service_1', 'service_2']
}]
problem[:units] << { id: 'vol' }
problem[:vehicles].first[:capacities] = [{ unit_id: 'kg', limit: 2 }, { unit_id: 'vol', limit: 1 }]
problem[:services][0][:quantities] = [{ unit_id: 'kg', value: 1 }]
problem[:services][1][:quantities] = [{ unit_id: 'kg', value: -1 }]
problem[:services][2][:quantities] = [{ unit_id: 'vol', value: 1 }]
vrp = TestHelper.create(problem)

OptimizerWrapper.config[:services][:vroom].inapplicable_solve?(vrp)
OptimizerWrapper.config[:services][:ortools].inapplicable_solve?(vrp)
senhalil marked this conversation as resolved.
Show resolved Hide resolved
end

def test_assert_inapplicable_relations
problem = VRP.basic
problem[:relations] = [{
Expand Down Expand Up @@ -3069,6 +3086,8 @@ def test_assert_inapplicable_relations
}]

vrp = TestHelper.create(problem)
assert_includes OptimizerWrapper.config[:services][:vroom].inapplicable_solve?(vrp),
:assert_no_overall_duration
assert_includes OptimizerWrapper.config[:services][:vroom].inapplicable_solve?(vrp),
:assert_no_relations_except_simple_shipments
refute_includes OptimizerWrapper.config[:services][:ortools].inapplicable_solve?(vrp),
Expand Down
1 change: 1 addition & 0 deletions wrappers/vroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def solver_constraints
:assert_vehicles_no_late_multiplier_or_single_vehicle,
:assert_vehicles_no_overload_multiplier,
:assert_vehicles_start_or_end,
:assert_no_overall_duration,

# Mission constraints
:assert_no_activity_with_position,
Expand Down
6 changes: 3 additions & 3 deletions wrappers/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ def assert_one_sticky_at_most(vrp)

def assert_no_relations_except_simple_shipments(vrp)
vrp.relations.all?{ |r|
return true if r.linked_ids.empty? && r.linked_vehicle_ids.empty?
return false unless r.type == :shipment && r.linked_ids.size == 2
next true if r.linked_ids.empty? && r.linked_vehicle_ids.empty?
next false unless r.type == :shipment && r.linked_ids.size == 2

quantities = Hash.new {}
vrp.units.each{ |unit| quantities[unit.id] = [] }
r.linked_services.first.quantities.each{ |q| quantities[q.unit.id] << q.value }
r.linked_services.last.quantities.each{ |q| quantities[q.unit.id] << q.value }
quantities.all?{ |_unit, values|
[0, 2].include?(values.size) && values.first >= 0 && values.first == -values.last
values.empty? || (values.size == 2 && values.first >= 0 && values.first == -values.last)
}
}
end
Expand Down