-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation.js
79 lines (66 loc) · 1.83 KB
/
population.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
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
class Population {
constructor(popsize, mutationRate) {
this.mutationRate = mutationRate;
this.matingPool = [];
this.generation = 1;
this.population = [];
for (let i = 0; i < popsize; i++) {
this.population[i] = new Chromosome();
}
}
run() {
for (let i = 0; i < this.population.length; i++) {
this.population[i].move();
this.population[i].show();
}
}
calcFitness() {
for (let i = 0; i < this.population.length; i++) {
this.population[i].calcFitness();
}
}
naturalSelection() {
this.matingPool = [];
let maxFitness = this.getMaxFitness();
for (let i = 0; i < this.population.length; i++) {
let fitnessNormal = map(this.population[i].getFitness(), 0, maxFitness, 0, 1);
let n = floor(fitnessNormal * 100);
for (let j = 0; j < n; j++) {
this.matingPool.push(this.population[i]);
}
}
}
generate() {
let partnerA, partnerB, indexA, indexB;
for (let i = 0; i < this.population.length; i++) {
indexA = floor(random(this.matingPool.length));
indexB = floor(random(this.matingPool.length));
partnerA = this.matingPool[indexA].dna;
partnerB = this.matingPool[indexB].dna;
let child = partnerA.crossover(partnerB);
child.mutate(this.mutationRate);
this.population[i] = new Chromosome(child);
}
this.generation++;
}
// TOOLS
getMaxFitness() {
var record = 0;
for (var i = 0; i < this.population.length; i++) {
if (this.population[i].getFitness() > record) {
record = this.population[i].getFitness();
}
}
return record;
}
getGeneration() {
return this.generation;
}
getAverage() {
let total = 0;
for (let i = 0; i < this.population.length; i++) {
total += this.population[i].fitness;
}
return total / this.population.length;
}
}