Skip to content

Commit

Permalink
Added point mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Diehl committed Nov 8, 2008
1 parent 7426f68 commit 629d424
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
4 changes: 2 additions & 2 deletions examples/increment_cubed.rb
Expand Up @@ -25,9 +25,9 @@ def cases(num, value)
:generations_limit => 500,
:fitness_cases => cases(6, 1),
:error_function => lambda{|cases| cases.inject{|x, y| x.abs + y.abs } },
:elitism_percent => 0.3,
:elitism_percent => 0.4,
:crossover_percent => 0.5,
:mutation_percent => 0.0
:mutation_percent => 0.1
})

population.evolve!
Expand Down
6 changes: 3 additions & 3 deletions examples/polynomial.rb
Expand Up @@ -25,9 +25,9 @@ def cases(num, value)
:generations_limit => 500,
:fitness_cases => cases(6, 1),
:error_function => lambda{|cases| cases.inject{|x, y| x.abs + y.abs } },
:elitism_percent => 0.3,
:crossover_percent => 0.6,
:mutation_percent => 0.0
:elitism_percent => 0.4,
:crossover_percent => 0.5,
:mutation_percent => 0.1
})

population.evolve!
Expand Down
2 changes: 1 addition & 1 deletion lib/population.rb
Expand Up @@ -56,7 +56,7 @@ def evolve_generation!
elsif number_of_crossovers > 0 && number_of_crossovers -= 1
select_program.crossover(select_program)
elsif number_of_mutations > 0 && number_of_mutations -= 1
select_program.mutate(Program.randomized(rand(size_limit).next, instructions))
select_program.mutate(instructions, size_limit)
elsif number_of_reproductions > 0 && number_of_reproductions -= 1
select_program.reproduce
else
Expand Down
16 changes: 5 additions & 11 deletions lib/program.rb
Expand Up @@ -34,10 +34,12 @@ def crossover(mate)
self.random_subset + mate.random_subset
end

def mutate(mutation)
def mutate(instructions, size_limit)
result = self.dup
result[rand(result.size)] = mutation
result.flatten
result.each_with_index do |inst, index|
result[index] = instructions[rand(instructions.size)] if rand < (1.5 / (size_limit*0.5))
end
result
end

def +(program)
Expand All @@ -51,13 +53,5 @@ def random_slice
def random_subset
self.slice( rand(self.size), rand(self.size) + 1 )
end

def slice_left
self.slice( 0, rand(self.size).next )
end

def slice_right
self.slice( -rand(self.size).next, self.size )
end
end
end
10 changes: 5 additions & 5 deletions spec/program/mutate_spec.rb
Expand Up @@ -5,20 +5,20 @@ module Revolve
describe Program, "#mutate" do
before do
@parent = Program.new(1, 2, 3, 4, 5)
@mutation = Program.new("one", "two", "three", "four", "five")
@child = @parent.mutate(@mutation)
@instructions = [1337]
@child = @parent.mutate(@instructions, 20)
end

it "should produce a new Program" do
@child.should be_kind_of(Program)
end

it "should be of length equal to the sum of the program - 1 and its mutation" do
@child.length.should == 9
it "should be of length equal to the original program" do
@child.length.should == 5
end

it "should only contain instructions from either program or mutation" do
@child.each{|instruction| (@parent + @mutation).should include(instruction) }
@child.each{|instruction| (@parent + @instructions).should include(instruction) }
end
end

Expand Down

0 comments on commit 629d424

Please sign in to comment.