-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneticAlgorithm.js
More file actions
78 lines (64 loc) · 2.29 KB
/
geneticAlgorithm.js
File metadata and controls
78 lines (64 loc) · 2.29 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
// Generate Initial Population using shuffle
function genPop() {
for (let i = 0; i < p.popSize; i++) {
p.population[i] = p.cities.slice();
p.population[i] = fyShuffle(p.population[i]);
}
}
// Calculate Fitness
function calcFitness() {
let currGenBestRoute = {};
currGenBestRoute.dist = Infinity;
for (let i = 0; i < p.population.length; i++) { // using a for loop here bc need an index to match with fitness[]
let d = calcDist(p.population[i]);
p.fitness[i] = 1 / (p.pow(d, 3) + 1);
if (d < currGenBestRoute.dist) {
currGenBestRoute.dist = d; // change these variable declarations to an object property assignment thing.
currGenBestRoute.order = p.population[i];
currGenBestRoute.perm = p.permCount; // number of permutations to date to get to this
p.currGenShortestDistOrder = currGenBestRoute.order;
}
p.permCount++; // possible optimization here of moving this line outside of for loop and doing permCount += p.population.length
}
if (currGenBestRoute.dist < p.shortestDist) {
setNewBestRoute(currGenBestRoute);
p.shortestDist = currGenBestRoute.dist;
}
}
function normFitness() {
let sum = 0;
p.fitness.forEach(function(elt) { // keeping forEach instead of map() bc forEach is faster.
sum += elt;
});
for (let i = 0; i < p.fitness.length; i++) {
p.fitness[i] = p.fitness[i] / sum;
}
}
function nextGen() {
let newPop = [];
for (let i = 0; i < p.population.length; i++) {
let newOrder = pickOne(p.population, p.fitness); // order is a p5.vector array. What's relationship between population and fitness array again?()
mutateSwap(newOrder, p.mSwapRate);
newPop[i] = newOrder;
}
p.population = newPop;
}
function pickOne(popArr, probs) {
let index = 0;
let r = Math.random();
while (r > 0) {
r = r - probs[index];
index++;
}
index--;
return popArr[index].slice();
}
function mutateSwap(cArr, mRate) {
let numSwaps = Math.ceil(mRate * cArr.length);
for (let i = 0; i < numSwaps; i++) {
let indexA = Math.floor(p.random(cArr.length));
let indexB = Math.floor(p.random(cArr.length));
swap(cArr, indexA, indexB);
}
return cArr;
}