-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopulation.js
More file actions
66 lines (46 loc) · 1.75 KB
/
Population.js
File metadata and controls
66 lines (46 loc) · 1.75 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
class Population {
constructor(mRate, popSize) {
this.generation = 0;
this.mutationRate = mRate;
this.population = [];
for (let i = 0; i < popSize; i++)
this.population.push(new Specimen());
this.dead = [];
this.matingPool = [];
}
naturalSelection() {
let maxFitness = this.dead[this.dead.length - 1].fitness;
this.matingPool = [];
let meanFitness = 0;
for (let i of this.dead) {
let fitness = map(i.fitness, 0, maxFitness, 0, 1);
meanFitness += i.fitness;
for (let j = 0; j < floor(fitness * 10); j++)
this.matingPool.push(i);
}
console.log(`Fitness médio: ${(meanFitness/this.dead.length).toFixed(3)}`);
console.log(`Fitness máximo: ${maxFitness.toFixed(3)}`);
this.dead = [];
}
generate() {
for (let i = 0; i < POP_SIZE; i++) {
let partner1_index = floor(random(this.matingPool.length - 1));
let partner2_index = floor(random(this.matingPool.length - 1));
let partner1 = this.matingPool[partner1_index];
let partner2 = this.matingPool[partner2_index];
// Não usando proteção
let child = partner1.crossover(partner2);
child.mutate(this.mutationRate);
this.population[i] = child;
}
this.generation += 1;
}
genocide() {
// Coloca o melhor no fim do array (NOT HAPPENING)
this.population.sort((a, b) => a.fitness > b.fitness);
while(this.population.length != 0){
this.population[0].die();
}
this.population = [];
}
}