Created
May 29, 2017 17:29
-
-
Save rebeccapeltz/3bbdba118d26f4243b4bdbb48453a0bf to your computer and use it in GitHub Desktop.
Genetic Algorithm Reproduction transcribed to JavaScript
This file contains hidden or 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
GA.evolvePopulation = function (generation) { | |
var newGeneration = new Population(generation.populationSize(), false); | |
var elitismOffset = 0; | |
//keep best offsrping from past generation in first position if elitism is enabled | |
if (GA.elitism) { | |
newGeneration.saveTour(0, pop.getFittest()); | |
elitismOffset = 1; | |
} | |
//choose parent and "mate" - create new offspring from current generation | |
for (var i = elitismOffset; i < newGeneration.populationSize(); i++) { | |
//select parents | |
var parent1 = GA.parentSelection(generation); | |
var parent2 = GA.parentSelection(generation); | |
var child = GA.crossover(parent1, parent2); | |
newGeneration.saveOffspring(i, child); | |
} | |
//mutation - don't mutate first offsprint if elitism enabled | |
for (var i = elitismOffset; i < newGeneration.populationSize(); i++) { | |
GA.mutate(newGeneration.getTour(i)); | |
} | |
return newGeneration; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment