-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNA.pde
39 lines (33 loc) · 902 Bytes
/
DNA.pde
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
// Simulate DNA data
class DNA {
// The genetic sequence
float[] genes;
DNA(int len) {
// DNA is random floating point values between 0 and 1 (!!)
genes = new float[len];
for (int i = 0; i < genes.length; i++) {
genes[i] = random(0,1);
}
}
DNA(float[] newgenes) {
genes = newgenes;
}
DNA crossover(DNA partner) {
float[] child = new float[genes.length];
int crossover = int(random(genes.length));
for (int i = 0; i < genes.length; i++) {
if (i > crossover) child[i] = genes[i];
else child[i] = partner.genes[i];
}
DNA newgenes = new DNA(child);
return newgenes;
}
// Based on a mutation probability, picks a new random character in array spots
void mutate(float m) {
for (int i = 0; i < genes.length; i++) {
if (random(1) < m) {
genes[i] = random(0,1);
}
}
}
}