Skip to content

Commit

Permalink
refactoring extra/missing box error
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaguery committed May 19, 2012
1 parent a841d7c commit 54902ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 12 additions & 6 deletions features/cleanup_distance.feature
Expand Up @@ -20,12 +20,6 @@ Feature: Cleanup distance
When I calculate the cleanup distance for box 2 of stack 1
Then the score for that box should be 2

Scenario: +100 if target box is totally missing
Given the target is [[:r]]
And the observed is [[]]
When I calculate the cleanup distance for box 1 of stack 1
Then the score for that box should be 100

Scenario: count boxes moved when retrieving replacement
Given the target is [[:r], [:b,:b]]
And the observed is [[],[:r,:b,:b]]
Expand Down Expand Up @@ -135,6 +129,18 @@ Feature: Cleanup distance
When I calculate the cleanup distance
Then the score should be 37

Scenario: the presence of extra boxes should be penalized
Given the target is [[]]
And the observed is [[:r, :r]]
When I calculate the cleanup distance
Then the score should be 203

Scenario: the lack of needed boxes should be penalized
Given the target is [[:r, :r]]
And the observed is [[]]
When I calculate the cleanup distance
Then the score should be 200


Scenario: it should provide a numeric answer for arbitrary rearrangements
Given I have 100 arbitrary target:objective pairs with 5 stacks and 5 crates each
Expand Down
9 changes: 7 additions & 2 deletions lib/cargobot.rb
Expand Up @@ -187,7 +187,7 @@ def crate_cleanup_error(target, stack_number, box_number)
return 0
else
possible_replacements = topmost_matches(expected_box)
return 100 if possible_replacements.empty?
return 0 if possible_replacements.empty?

this_stack_replacement = @stacks[stack].rindex(expected_box).nil? ?
nil :
Expand Down Expand Up @@ -226,11 +226,16 @@ def stack_cleanup_error(target, stack_number)


def cleanup_error(target)
target.stacks.each_with_index.inject(0) do |sum,(stack,idx)|
stack_error = target.stacks.each_with_index.inject(0) do |sum,(stack,idx)|
sum + stack_cleanup_error(target,idx+1)
end

extra_boxes_error = (target.stacks.flatten.length - @stacks.flatten.length).abs*100

stack_error + extra_boxes_error
end


def topmost_matches(crate)
@stacks.collect {|s| (s.length - s.rindex(crate)) unless s.rindex(crate).nil?}.compact
end
Expand Down

0 comments on commit 54902ae

Please sign in to comment.