Skip to content

Instantly share code, notes, and snippets.

@deleteman
Created December 10, 2022 12:36
Show Gist options
  • Save deleteman/abdd5bff5b58386690aa59bdfdb6e883 to your computer and use it in GitHub Desktop.
Save deleteman/abdd5bff5b58386690aa59bdfdb6e883 to your computer and use it in GitHub Desktop.
// create the initial population
let population = [];
for (let i = 0; i < POPULATION_SIZE; i++) {
// generate a random individual
let individual = [];
for (let j = 0; j < NUM_CITIES; j++) {
individual.push(j);
}
// shuffle the cities in the individual
for (let j = 0; j < NUM_CITIES; j++) {
let k = Math.floor(Math.random() * NUM_CITIES);
let temp = individual[j];
individual[j] = individual[k];
individual[k] = temp;
}
population.push(individual);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment