-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of evolving a constant number with integers 0-9 and …
…addition, subtraction, multiplication, and protected division.
- Loading branch information
Larry Diehl
committed
Nov 6, 2008
1 parent
4e0a72c
commit e1a76c7
Showing
4 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require File.join(File.dirname(__FILE__), "..", "revolve") | ||
|
||
class Integer | ||
def protected_division(divisor) | ||
self / divisor rescue 1 | ||
end | ||
end | ||
|
||
population = Revolve::Population.initialized( 200, { | ||
:program_size => 10, | ||
:instructions => [1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
Revolve::Method.new(:+), Revolve::Method.new(:-), | ||
Revolve::Method.new(:*), Revolve::Method.new(:protected_division)], | ||
:max_generations => 500, | ||
:fitness_cases => [ lambda{|program| program.run.to_i - 120 } ], | ||
:fitness_combinator => lambda{|cases| cases.first.abs }, | ||
:crossover_chance => 0.6, | ||
:mutation_chance => 0.3 | ||
}) | ||
|
||
population.evolve! | ||
|
||
puts "Generations: #{population.generation}" | ||
puts "Fitness: #{population.fitness(population.fittest_program)}" | ||
puts "Program:\n#{population.fittest_program.inspect}" | ||
puts "Output: #{population.fittest_program.run}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require File.join(File.dirname(__FILE__), "..", "spec_helper") | ||
|
||
module Revolve | ||
|
||
describe Method, "#inspect" do | ||
before { @method = Method.new(:+) } | ||
|
||
it "should be the name wrapped in 'method'" do | ||
@method.inspect.should == "(:+ method)" | ||
end | ||
end | ||
|
||
end |