-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNA.js
45 lines (38 loc) · 1002 Bytes
/
DNA.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
class DNA {
constructor() {
this.genetics = []
this.fitness = 0
// fill the genes with random numbers
for (let i = 0; i < digits; i++) {
this.genetics[i] = floor(random(10))
}
}
calculateFitness() {
// the fitness score is how many genes match the ideal DNA
for (let i = 0; i < digits; i++) {
if (this.genetics[i] == pi[i]) {
this.fitness++
}
}
}
crossover(partner) {
// breed two cells by merging the genes from each parent at a certain midpoint
let midpoint = floor(random(this.genetics.length))
let child = new DNA()
for (let i = 0; i < digits; i++) {
if (i < midpoint)
child.genetics[i] = this.genetics[i]
else
child.genetics[i] = partner.genetics[i]
}
return child
}
mutate() {
// mutate the gene depending on the mutation rate
for (let i = 0; i < digits; i++) {
if (random(1) < 0.01) {
this.genetics[i] = floor(random(10))
}
}
}
}