Skip to content

Instantly share code, notes, and snippets.

@rebeccapeltz
Created May 29, 2017 17:29
Show Gist options
  • Save rebeccapeltz/3bbdba118d26f4243b4bdbb48453a0bf to your computer and use it in GitHub Desktop.
Save rebeccapeltz/3bbdba118d26f4243b4bdbb48453a0bf to your computer and use it in GitHub Desktop.
Genetic Algorithm Reproduction transcribed to JavaScript
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