|
| 1 | +class GeneticAlgorithmTSP { |
| 2 | + constructor(cities, populationSize, mutationRate, generations) { |
| 3 | + this.cities = cities; |
| 4 | + this.populationSize = populationSize; |
| 5 | + this.mutationRate = mutationRate; |
| 6 | + this.generations = generations; |
| 7 | + this.population = []; |
| 8 | + } |
| 9 | + |
| 10 | + // Initialize population with random routes |
| 11 | + initializePopulation() { |
| 12 | + for (let i = 0; i < this.populationSize; i++) { |
| 13 | + let route = [...this.cities].sort(() => Math.random() - 0.5); |
| 14 | + this.population.push(route); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + // Calculate fitness based on total route distance |
| 19 | + calculateFitness(route) { |
| 20 | + let distance = 0; |
| 21 | + for (let i = 0; i < route.length - 1; i++) { |
| 22 | + distance += this.distance(route[i], route[i + 1]); |
| 23 | + } |
| 24 | + distance += this.distance(route[route.length - 1], route[0]); |
| 25 | + return 1 / distance; |
| 26 | + } |
| 27 | + |
| 28 | + // Calculate distance between two cities (Pythagorean theorem) |
| 29 | + distance(city1, city2) { |
| 30 | + const dx = city1.x - city2.x; |
| 31 | + const dy = city1.y - city2.y; |
| 32 | + return Math.sqrt(dx * dx + dy * dy); |
| 33 | + } |
| 34 | + |
| 35 | + // Selection based on fitness (roulette wheel selection) |
| 36 | + selectParents() { |
| 37 | + const fitnesses = this.population.map(route => this.calculateFitness(route)); |
| 38 | + const totalFitness = fitnesses.reduce((acc, fitness) => acc + fitness, 0); |
| 39 | + const probabilities = fitnesses.map(fitness => fitness / totalFitness); |
| 40 | + |
| 41 | + let parent1 = this.population[this.rouletteWheelSelection(probabilities)]; |
| 42 | + let parent2 = this.population[this.rouletteWheelSelection(probabilities)]; |
| 43 | + return [parent1, parent2]; |
| 44 | + } |
| 45 | + |
| 46 | + rouletteWheelSelection(probabilities) { |
| 47 | + let r = Math.random(); |
| 48 | + let sum = 0; |
| 49 | + for (let i = 0; i < probabilities.length; i++) { |
| 50 | + sum += probabilities[i]; |
| 51 | + if (r <= sum) return i; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Crossover (Ordered Crossover) |
| 56 | + crossover(parent1, parent2) { |
| 57 | + const start = Math.floor(Math.random() * parent1.length); |
| 58 | + const end = start + Math.floor(Math.random() * (parent1.length - start)); |
| 59 | + const child = new Array(parent1.length).fill(null); |
| 60 | + |
| 61 | + for (let i = start; i < end; i++) { |
| 62 | + child[i] = parent1[i]; |
| 63 | + } |
| 64 | + |
| 65 | + let parent2Index = 0; |
| 66 | + for (let i = 0; i < child.length; i++) { |
| 67 | + if (child[i] === null) { |
| 68 | + while (child.includes(parent2[parent2Index])) { |
| 69 | + parent2Index++; |
| 70 | + } |
| 71 | + child[i] = parent2[parent2Index]; |
| 72 | + } |
| 73 | + } |
| 74 | + return child; |
| 75 | + } |
| 76 | + |
| 77 | + // Mutation (swap mutation) |
| 78 | + mutate(route) { |
| 79 | + if (Math.random() < this.mutationRate) { |
| 80 | + const index1 = Math.floor(Math.random() * route.length); |
| 81 | + const index2 = (index1 + 1 + Math.floor(Math.random() * (route.length - 1))) % route.length; |
| 82 | + [route[index1], route[index2]] = [route[index2], route[index1]]; |
| 83 | + } |
| 84 | + return route; |
| 85 | + } |
| 86 | + |
| 87 | + // Evolve population |
| 88 | + evolve() { |
| 89 | + const newPopulation = []; |
| 90 | + for (let i = 0; i < this.populationSize; i++) { |
| 91 | + const [parent1, parent2] = this.selectParents(); |
| 92 | + let child = this.crossover(parent1, parent2); |
| 93 | + child = this.mutate(child); |
| 94 | + newPopulation.push(child); |
| 95 | + } |
| 96 | + this.population = newPopulation; |
| 97 | + } |
| 98 | + |
| 99 | + // Run the genetic algorithm |
| 100 | + run() { |
| 101 | + this.initializePopulation(); |
| 102 | + for (let i = 0; i < this.generations; i++) { |
| 103 | + this.evolve(); |
| 104 | + } |
| 105 | + return this.getBestRoute(); |
| 106 | + } |
| 107 | + |
| 108 | + // Get the best route in the current population |
| 109 | + getBestRoute() { |
| 110 | + return this.population.reduce((bestRoute, route) => { |
| 111 | + return this.calculateFitness(route) > this.calculateFitness(bestRoute) ? route : bestRoute; |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Example usage: |
| 117 | +const cities = [ |
| 118 | + { x: 0, y: 0 }, |
| 119 | + { x: 1, y: 2 }, |
| 120 | + { x: 4, y: 3 }, |
| 121 | + { x: 5, y: 1 }, |
| 122 | + { x: 3, y: 5 } |
| 123 | +]; |
| 124 | + |
| 125 | +const gaTSP = new GeneticAlgorithmTSP(cities, 50, 0.01, 1000); |
| 126 | +const bestRoute = gaTSP.run(); |
| 127 | +console.log('Best Route:', bestRoute); |
0 commit comments