This file contains 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 NUM_CITIES = 10; // number of cities in the sales route | |
const POPULATION_SIZE = 100; // number of individuals in the population | |
const MUTATION_RATE = 0.1; // probability of mutation | |
const CROSSOVER_RATE = 0.7; // probability of crossover | |
const MAX_ITERATIONS = 1000; // maximum number of iterations | |
// define the distance between each pair of cities | |
const distances = [ [0, 10, 15, 20, 25, 30, 35, 40, 45, 50], | |
[10, 0, 5, 10, 15, 20, 25, 30, 35, 40], | |
[15, 5, 0, 5, 10, 15, 20, 25, 30, 35], |
This file contains 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
[ | |
0, 1, 3, 4, 2, | |
7, 6, 9, 8, 5 | |
] |
This file contains 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; |
This file contains 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 crossover = (individual1, individual2) => { | |
// choose a random crossover point | |
let point = Math.floor(Math.random() * NUM_CITIES); | |
// create the offspring by combining the two individuals | |
let offspring = individual1.slice(0, point).concat(individual2.slice(point)); | |
// remove duplicate cities from the offspring | |
let uniqueCities = new Set(offspring); | |
offspring = [...uniqueCities]; | |
// if the offspring is too short, add cities from the other individual | |
if (offspring.length < NUM_CITIES) { |
This file contains 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 select individuals for the next generation | |
const selection = (population) => { | |
// calculate the fitness of each individual | |
let fitnesses = population.map(calculateFitness); | |
// normalize the fitness values | |
let totalFitness = fitnesses.reduce((a, b) => a + b, 0); | |
let probabilities = fitnesses.map((fitness) => fitness / totalFitness); | |
// select individuals for the next generation | |
let nextGeneration = []; |
This file contains 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); |
This file contains 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
// run the genetic algorithm for a specified number of iterations | |
for (let i = 0; i < MAX_ITERATIONS; i++) { | |
population = evolve(population); | |
} | |
// sort the population by fitness | |
population.sort((a, b) => calculateFitness(a) - calculateFitness(b)); | |
// print the best individual |
This file contains 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++) { |
This file contains 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 NUM_CITIES = 10; // number of cities in the sales route | |
const POPULATION_SIZE = 100; // number of individuals in the population | |
const MUTATION_RATE = 0.1; // probability of mutation | |
const CROSSOVER_RATE = 0.7; // probability of crossover | |
const MAX_ITERATIONS = 1000; // maximum number of iterations | |
// define the distance between each pair of cities | |
const distances = [ | |
[0, 10, 15, 20, 25, 30, 35, 40, 45, 50], //distance of city 0 to all other cities | |
[10, 0, 5, 10, 15, 20, 25, 30, 35, 40],//distance of city 1 to all other cities |
This file contains 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
let internvalID = 0; | |
const names = ["Fernando", "John", "Cate", "Mary", "Francis"] | |
function sayHi(list) { | |
let name = list[Math.round(Math.random() * 10) % 4] | |
console.log("Hello", name) | |
} | |
intervalID = setInterval(sayHi, 1000, names) |