Class: GeneticAlgorithms::Engine
- Inherits:
-
Object
- Object
- GeneticAlgorithms::Engine
- Defined in:
- lib/genetic_algorithms/engine.rb
Instance Method Summary collapse
-
#initialize(population_size = 10, chromosome_length = 10, num_generations = 5) ⇒ Engine
constructor
A new instance of Engine.
- #start(best_possible_score, &fitness_function) ⇒ Object
Constructor Details
#initialize(population_size = 10, chromosome_length = 10, num_generations = 5) ⇒ Engine
Returns a new instance of Engine.
4 5 6 7 8 9 10 11 |
# File 'lib/genetic_algorithms/engine.rb', line 4 def initialize(population_size=10, chromosome_length=10, num_generations=5) @population_size = population_size @chromosome_length = chromosome_length @num_generations = num_generations @chromosomes = Population.random_chromosomes @population_size, @chromosome_length @population = Population.new @chromosomes end |
Instance Method Details
#start(best_possible_score, &fitness_function) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/genetic_algorithms/engine.rb', line 13 def start(best_possible_score, &fitness_function) highest_score, best_gen = 0, nil (0...@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 |