Method: Ai4r::GeneticAlgorithm::GeneticSearch#run
- Defined in:
- lib/ai4r/genetic_algorithm/genetic_algorithm.rb
#run ⇒ Object
-
Choose initial population
2. Evaluate the fitness of each individual in the population 3. Repeat 1. Select best-ranking individuals to reproduce 2. Breed new generation through crossover and mutation (genetic operations) and give birth to offspring 3. Evaluate the individual fitnesses of the offspring 4. Replace worst ranked part of population with offspring 4. Until termination 5. Return the best chromosome
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ai4r/genetic_algorithm/genetic_algorithm.rb', line 64 def run generate_initial_population # Generate initial population best = best_chromosome best_fitness = best.fitness stagnation = 0 @on_generation&.call(@generation, best_fitness) @max_generation.times do @generation += 1 selected_to_breed = selection # Evaluates current population offsprings = reproduction selected_to_breed # Generate the population for this new generation replace_worst_ranked offsprings current_best = best_chromosome if current_best.fitness > best_fitness best_fitness = current_best.fitness best = current_best stagnation = 0 else stagnation += 1 end @on_generation&.call(@generation, best_fitness) break if (@fitness_threshold && best_fitness >= @fitness_threshold) || (@max_stagnation && stagnation >= @max_stagnation) end best end |