Skip to content

Instantly share code, notes, and snippets.

@deleteman
Created December 10, 2022 13:31
Show Gist options
  • Save deleteman/b160db0f0e4bacdbc9f3c290985a2d6f to your computer and use it in GitHub Desktop.
Save deleteman/b160db0f0e4bacdbc9f3c290985a2d6f to your computer and use it in GitHub Desktop.
// 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