Created
December 10, 2022 12:42
-
-
Save deleteman/f9539a677c11f39798f99a46e58a90ed to your computer and use it in GitHub Desktop.
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
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