Skip to content

Instantly share code, notes, and snippets.

@deleteman
Created December 10, 2022 12:42
Show Gist options
  • Save deleteman/f9539a677c11f39798f99a46e58a90ed to your computer and use it in GitHub Desktop.
Save deleteman/f9539a677c11f39798f99a46e58a90ed to your computer and use it in GitHub Desktop.
const evolve = (population) => {
// select individuals for the next generation
let nextGeneration = selection(population);
// create the new generation by performing crossover and mutation
for (let i = 0; i < POPULATION_SIZE; i++) {
// with probability CROSSOVER_RATE, perform crossover
if (Math.random() < CROSSOVER_RATE) {
let individual1 = nextGeneration[i];
let individual2 = nextGeneration[Math.floor(Math.random() * POPULATION_SIZE)];
nextGeneration[i] = crossover(individual1, individual2);
}
// perform mutation on the individual
nextGeneration[i] = mutation(nextGeneration[i]);
}
// return the new generation
return nextGeneration;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment