Skip to content

Instantly share code, notes, and snippets.

@Yuemashi
Created July 8, 2016 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yuemashi/b5908a1ded4949709d7ae55c412fba88 to your computer and use it in GitHub Desktop.
Save Yuemashi/b5908a1ded4949709d7ae55c412fba88 to your computer and use it in GitHub Desktop.
nextGeneration = -> population {
population = selection[population]
elite = population.take(1).flatten
nextPop = []
(0..population.length / 2 - 1)
.each{crossover[
[] << population[rand(population.length - 1)] << population[rand(population.length - 1)],
rand(population[1].length - 1)]
.map{|g| nextPop << g}}
nextPop = nextPop
.map{|n| (0..n.length - 1)
.map{|p| rand(0.0..1.0) < 0.05 ? Genotype[1] : n[p] }.flatten }
nextPop.sort_by { |n| -fitness[n]}.take(nextPop.length - 1) << elite }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment