forked from SerialForBreakfast/neuralEvolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNA.pde
More file actions
56 lines (48 loc) · 1.17 KB
/
DNA.pde
File metadata and controls
56 lines (48 loc) · 1.17 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
class DNA {
//The genes include: Neural Network Weights, Mass
float[] genes;
//Construction, with random genes
DNA(){
genes = new float[40];
for (int i = 0; i < 32; ++i) { //Weights genes
genes[i] = random(-1, 1);
}
for (int i = 32; i < 33; ++i) { //Mass Gene
genes[i] = random(10, 70);
}
for (int i = 33; i < 40; ++i) { //Others Genes - not used
genes[i] = random(0, 50);
}
}
//Construction, with defined genes
DNA( float[] f ){
genes = f;
}
//Return the exact copy of the dna
DNA copy() {
float[] newgenes = new float[genes.length];
arraycopy(genes,newgenes);
return new DNA(newgenes);
}
//Mutate the genes
void mutate( float mutationRate ) {
for (int i = 0; i < 32; i++) {
if (random(1) < mutationRate) {
println("Mutation!");
genes[i] = random(0, 1);
}
}
for (int i = 32; i < 33; i++) {
if (random(1) < mutationRate) {
println("Mutation!");
genes[i] = random(50, 150);
}
}
for (int i = 33; i < 40; i++) {
if (random(1) < mutationRate) {
println("Mutation!");
genes[i] = random(10, 50);
}
}
}
}