Created
December 10, 2022 12:36
-
-
Save deleteman/abdd5bff5b58386690aa59bdfdb6e883 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
// 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