Class: GeneticAlgorithms::Engine
- Inherits:
-
Object
- Object
- GeneticAlgorithms::Engine
- Defined in:
- lib/genetic_algorithms/engine.rb
Class Attribute Summary collapse
-
.chromosome_length ⇒ Object
Returns the value of attribute chromosome_length.
-
.num_generations ⇒ Object
Returns the value of attribute num_generations.
-
.population_size ⇒ Object
Returns the value of attribute population_size.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Engine
constructor
A new instance of Engine.
- #start(best_possible_score, &fitness_function) ⇒ Object
Constructor Details
#initialize ⇒ Engine
16 17 18 19 20 |
# File 'lib/genetic_algorithms/engine.rb', line 16 def initialize Engine.configure chromosomes = Population.random_chromosomes Engine.population_size, Engine.chromosome_length @population = Population.new chromosomes end |
Class Attribute Details
.chromosome_length ⇒ Object
Returns the value of attribute chromosome_length.
5 6 7 |
# File 'lib/genetic_algorithms/engine.rb', line 5 def chromosome_length @chromosome_length end |
.num_generations ⇒ Object
Returns the value of attribute num_generations.
5 6 7 |
# File 'lib/genetic_algorithms/engine.rb', line 5 def num_generations @num_generations end |
.population_size ⇒ Object
Returns the value of attribute population_size.
5 6 7 |
# File 'lib/genetic_algorithms/engine.rb', line 5 def population_size @population_size end |
Class Method Details
.configure(&block) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/genetic_algorithms/engine.rb', line 8 def self.configure(&block) block.call(self) if block_given? Engine.population_size = 10 unless Engine.population_size Engine.chromosome_length = 10 unless Engine.chromosome_length Engine.num_generations = 5 unless Engine.num_generations end |
Instance Method Details
#start(best_possible_score, &fitness_function) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/genetic_algorithms/engine.rb', line 22 def start(best_possible_score, &fitness_function) highest_score, best_gen = 0, nil (0...Engine.num_generations).inject(@population) do |newest_population, i| next_gen = newest_population.evolve(&fitness_function) if newest_population.highest_score > highest_score highest_score, best_gen = newest_population.highest_score, newest_population break if highest_score == best_possible_score end next_gen end {best_gen.best_solution => best_gen.highest_score} end |