Skip to content

Commit

Permalink
added another evaluation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaguery committed May 20, 2012
1 parent af660ea commit ccd45fd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 15 deletions.
32 changes: 17 additions & 15 deletions examples/hillclimbing.rb
Expand Up @@ -33,9 +33,9 @@


# # Mirror (medium)
# setup = [[:y, :y, :y, :y],[:g, :g],[:g],[:g],[:g, :g],[]]
# target = [[],[:g, :g],[:g],[:g],[:g, :g],[:y, :y, :y, :y]]
# claw_position = 1
setup = [[:y, :y, :y, :y],[:g, :g],[:g],[:g],[:g, :g],[]]
target = [[],[:g, :g],[:g],[:g],[:g, :g],[:y, :y, :y, :y]]
claw_position = 1
# Successful programs:
# R_y claw_none claw_g R claw L call1 R call2 R R R L R_any claw
# R_y claw_none claw_g R claw L call1 R call2 R R R L R_any claw_r
Expand Down Expand Up @@ -90,9 +90,9 @@


# Walking Piles (easy)
target = [[],[],[],[],[:b,:b,:b,:b],[:b,:b,:b,:b],[:b,:b,:b,:b]]
setup = [[:b,:b,:b,:b],[:b,:b,:b,:b],[:b,:b,:b,:b],[],[],[],[]]
claw_position = 1
# target = [[],[],[],[],[:b,:b,:b,:b],[:b,:b,:b,:b],[:b,:b,:b,:b]]
# setup = [[:b,:b,:b,:b],[:b,:b,:b,:b],[:b,:b,:b,:b],[],[],[],[]]
# claw_position = 1
# Successful programs:
# call3 prog_3 R R claw L prog_3 L_none L claw_none R call3
# call3 call3 prog_3 R R claw L prog_3 L_none L claw_none R call3
Expand Down Expand Up @@ -198,12 +198,13 @@ def mutate_token_array(array, max_length = 32)
claw_position:claw_position,
step_limit:200)
wildtype.activate
wildtype_err = CrateStacks.new(wildtype.stacks).teardown_distance(CrateStacks.new target)

results_file.puts "1,#{wildtype_err},#{wildtype.steps},#{wildtype.moves},#{wildtype.crashes},#{wildtype.topples},#{wildtype.script.inspect}"
wildtype_err = CrateStacks.new(wildtype.stacks).rebuild_distance(CrateStacks.new target)
counter = 1
results_file.puts "#{counter},1,#{wildtype_err},#{wildtype.steps},#{wildtype.moves},#{wildtype.crashes},#{wildtype.topples},#{wildtype.script.inspect}"

bests = {wildtype.script => [wildtype_err,wildtype.crashes]}
until bests.values.count([0,0]) > 20 do
until bests.values.count([0,0,0]) > 20 do
counter += 1
mutant_tokens = mutate_token_array(wildtype_tokens)
mutant = CargoBot.new(
mutant_tokens.join(" "),
Expand All @@ -212,17 +213,18 @@ def mutate_token_array(array, max_length = 32)
claw_position:claw_position,
step_limit:200)
mutant.activate
mutant_err = CrateStacks.new(mutant.stacks).teardown_distance(CrateStacks.new target)
mutant_err = CrateStacks.new(mutant.stacks).rebuild_distance(CrateStacks.new target)

if (mutant_err <= wildtype_err) &&
(mutant.crashes <= wildtype.crashes) &&
(mutant.crashes <= wildtype.crashes) &&
(mutant.topples <= wildtype.topples) &&
(bests[mutant.script].nil?)
bests[mutant.script] = [mutant_err,mutant.crashes]
bests[mutant.script] = [mutant_err,mutant.crashes,mutant.topples]
wildtype_tokens = mutant_tokens
wildtype = mutant
wildtype_err = mutant_err
results_file.puts "#{bests.length},#{mutant_err},#{mutant.steps},#{mutant.moves},#{mutant.crashes},#{mutant.topples}, #{mutant.script.inspect}"
puts "#{bests.length},#{mutant_err},#{mutant.steps},#{mutant.moves},#{mutant.crashes},#{mutant.topples}"
results_file.puts "#{counter},#{bests.length},#{mutant_err},#{mutant.steps},#{mutant.moves},#{mutant.crashes},#{mutant.topples}, #{mutant.script.inspect}"
puts "#{counter},#{bests.length},#{mutant_err},#{mutant.steps},#{mutant.moves},#{mutant.crashes},#{mutant.topples}"
end
end

Expand Down
52 changes: 52 additions & 0 deletions features/rebuild_distance.feature
@@ -0,0 +1,52 @@
Feature: Rebuild distance
In order to evaluate a stack of crates resulting from sorting
As a Cargo-Bot simulator
I want to measure the distance blocks are from where they're wanted

Scenario: rebuild distance is number of blocks for correct solution
Given the target is [[:r, :g, :b]]
And the observed is [[:r, :g, :b]]
When I calculate the rebuild distance
Then the score should be 0

Scenario: rd counts 'extra reaching' needed to reconstruct target from observed
Given the target is [[:r, :r, :r, :g, :b]]
And the observed is [[:r, :b, :r, :r, :g]]
When I calculate the rebuild distance
Then the score should be 3

Scenario: rd counts 'extra reaching' needed to reconstruct target from observed
Given the target is [[:r, :r, :g, :b], []]
And the observed is [[:r, :r], [:g, :b]]
When I calculate the rebuild distance
Then the score should be 2

Scenario: rd counts 'extra reaching' needed to reconstruct target from observed
Given the target is [[], [:r, :r, :g, :b]]
And the observed is [[:r, :r], [:g, :b]]
When I calculate the rebuild distance
Then the score should be 2

Scenario: rd counts 'extra reaching' needed to reconstruct target from observed
Given the target is [[:g],[:g],[:g],[:g],[:g],[:g]]
And the observed is [[:g,:g,:g,:g,:g,:g],[],[],[],[],[]]
When I calculate the rebuild distance
Then the score should be 15

Scenario: rd should rebuild using leftmost available blocks
Given the target is [[:r, :r, :r], [:g, :g, :g], [:b, :b, :b]]
And the observed is [[:r, :g, :b], [:r, :g, :g], [:b, :r, :b]]
When I calculate the rebuild distance
Then the score should be 7

Scenario: rd should count 100 points for every missing block
Given the target is [[:r, :r, :r]]
And the observed is [[:r, :r]]
When I calculate the rebuild distance
Then the score should be 100

Scenario: rd should count 100 points for every extra block
Given the target is [[:r]]
And the observed is [[:r, :g, :b]]
When I calculate the rebuild distance
Then the score should be 200
7 changes: 7 additions & 0 deletions features/step_definitions/rebuild_distance_steps.rb
@@ -0,0 +1,7 @@
When /^I calculate the rebuild distance$/ do
@distance = @observed.rebuild_distance(@target)
end

Then /^the score should be twice the teardown distance$/ do
@distance.should == @observed.teardown_distance(@target)*2
end
21 changes: 21 additions & 0 deletions lib/cargobot.rb
Expand Up @@ -235,6 +235,27 @@ def cleanup_error(target)
stack_error + extra_boxes_error
end


def rebuild_distance(target)
raw_materials = @stacks.collect {|stack| stack.clone}
score = 0

target.stacks.each_with_index do |target_stack, stack_idx|
target_stack.each do |wanted_item|
source_stack = raw_materials.find_index {|s| s.include? wanted_item}
if source_stack.nil?
score += 100
else
height = raw_materials[source_stack].find_index(wanted_item)
raw_materials[source_stack].delete_at height
score += (source_stack-stack_idx).abs + height
end
end
end
return score + 100*raw_materials.flatten.length
end


def teardown_distance(target)
stackwise_error = target.stacks.each_with_index.inject(0) do |sum,(stack,idx)|
my_ht, target_ht = @stacks[idx].length, stack.length
Expand Down

0 comments on commit ccd45fd

Please sign in to comment.