-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.js
33 lines (30 loc) · 922 Bytes
/
Route.js
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
class Route {
constructor(city) {
this.city = city
this.distance = 0
this.fitness
}
calculateDistance() {
let x = 0
for (let i = 0; i < this.city.length-1; i++) {
x += this.city[i].calculateDistance(this.city[i+1])
}
x += this.city[this.city.length-1].calculateDistance(this.city[0])
this.distance = x
}
calculateFitness(val) {
this.fitness = parseFloat(val/this.distance)
}
mutate(mutationRate) {
for (let i = 0; i < this.city.length; i++) {
if(Math.random(1) < mutationRate) {
let randomIndex = Math.floor(Math.random(this.city.length))
let a = this.city[randomIndex]
let b = this.city[i]
this.city[i] = a
this.city[randomIndex] = b
// console.log("Gatcha")
}
}
}
}