-
Notifications
You must be signed in to change notification settings - Fork 1
/
Population.js
167 lines (147 loc) · 5 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const compareFitnessInc = (s1, s2) => {
const a = s1.fitness;
const b = s2.fitness;
if (a < b)
return -1;
else if (a == b)
return 0;
else
return 1;
}
const compareFitnessDec = (s1, s2) => {
const a = s1.fitness;
const b = s2.fitness;
if (a < b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
class Population {
// Private
#fitsum // sum of fitness for all individuals in this population
constructor() {
this.parentPop = []; // Main population - invariant : always sorted, best indiv on the front
this.matingPool = []; // Individuals chosen as parents are temporarily stored here
this.childPop = []; // Child population for step 3 - 'produceOffspring'
this.#fitsum = 0;
// Init parentPop with new random individuals
for (let i = 0; i < popsize; i++) {
this.parentPop[i] = new Song();
}
}
// Step 1. Calculate fitness of each individual (Song) in the population
calPopulationFitness() {
this.#fitsum = 0;
for (let i = 0; i < this.parentPop.length; i++) {
let song = this.parentPop[i];
song.calFitness(fitnessChoice);
this.#fitsum += song.fitness;
}
// parentPop Invariant
if (minFitness) this.parentPop.sort(compareFitnessDec);
else this.parentPop.sort(compareFitnessInc);
}
// Step 2. Select parents to form mating pool
selectParents = {
// Implemented the algorithm given in the lecture note.
roulette: function () {
// Roulette + FPS
let cumProb = []; // cumulative probability, proportional to fitness
let p = mapFitnessToProb(this.parentPop[0].fitness, this.#fitsum);
cumProb[0] = p;
for (let i = 1; i < popsize; i++) {
p = mapFitnessToProb(this.parentPop[i].fitness, this.#fitsum);
cumProb[i] = cumProb[i - 1] + p;
}
this.matingPool = []
for (let i = 0; i < popsize; i++) {
let rand = random();
let j = 0;
while (cumProb[j] < rand) {
j++;
}
this.matingPool[i] = this.parentPop[j];
}
},
tournament: function (K) {
// tournament - select best among randomly chosen K
for (let i = 0; i < popsize; i++) {
let tmp = []
for (let j = 0; j < K; j++) {
tmp.push(random(this.parentPop));
}
this.matingPool[i] = tmp.reduce((prev, cur) => {
if (minFitness)
return prev.fitness < cur.fitness ? prev : cur;
else
return prev.fitness < cur.fitness ? cur : prev;
})
}
}
}
// Step 3. Produce offspring population - crossover and mutation
produceOffspring() {
this.childPop = [];
for (let i = 0; i < this.parentPop.length; i++) {
let parent1 = random(this.matingPool);
let parent2 = random(this.matingPool);
let child;
switch (crossoverChoice) {
case 0:
child = crossover.singlePoint(parent1, parent2);
break;
case 1:
child = crossover.uniform(parent1, parent2);
break;
case 2:
child = crossover.average(parent1, parent2);
}
switch (mutationChoice) {
case 0:
mutation.randomFlip(child);
break;
case 1:
mutation.addVal(child);
}
child.calFitness(fitnessChoice);
this.childPop[i] = child;
}
}
// Step 4. With parent and offspring population, get next generation population
formNextGeneration = {
// Gradual replacement
// if replaceCnt == this.parentPop.length, whole parent population will be replaced with offspring population
gradual_replacement: function (replaceCnt = this.parentPop.length) {
if (minFitness) this.childPop.sort(compareFitnessInc);
else this.childPop.sort(compareFitnessDec);
// Replace 'replaceCnt' individuals with lowest fitness from parent with same amount of best offsprings
for (let i = 0; i < replaceCnt; i++) {
this.parentPop[i] = this.childPop[i];
}
},
// Elitism
// Keep 'keepCnt' best individuals from the parent population
elitism: function (keepCnt) {
const N = this.parentPop.length;
for (let i = keepCnt; i < N; i++) {
this.parentPop[i] = random(this.childPop); // randomly select offspring for the rest of the population
}
}
}
// Find individual with best fitness
findBest() {
if (minFitness) this.parentPop.sort(compareFitnessInc);
else this.parentPop.sort(compareFitnessDec);
bestSong = this.parentPop[0];
}
// Calculate average fitness of the whole population
getAverageFitness() {
return this.#fitsum / popsize;
}
}
// Used in roulette selection
function mapFitnessToProb(fit, fitsum) {
return map(fit, 0, fitsum, 0, 1)
}