Skip to content

Commit

Permalink
Merge pull request #300 from senhalil/feat/fix_assert_quantity_issue
Browse files Browse the repository at this point in the history
Fix assert quantity issues
  • Loading branch information
fab-girard committed Oct 21, 2021
2 parents 04ace3c + 4b1b4e8 commit 769f572
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
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)
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

0 comments on commit 769f572

Please sign in to comment.