Created
December 10, 2022 13:31
-
-
Save deleteman/b160db0f0e4bacdbc9f3c290985a2d6f 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
// define a function to perform mutation on an individual | |
const mutation = (individual) => { | |
// iterate over the cities in the individual | |
for (let i = 0; i < NUM_CITIES; i++) { | |
// with probability MUTATION_RATE, swap this city with another city | |
if (Math.random() < MUTATION_RATE) { | |
let j = Math.floor(Math.random() * NUM_CITIES); | |
let temp = individual[i]; | |
individual[i] = individual[j]; | |
individual[j] = temp; | |
} | |
} | |
// return the mutated individual | |
return individual; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment