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