-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathGenome.pde
75 lines (66 loc) · 1.87 KB
/
Genome.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
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
class Gen {
boolean source_hidden_layer;
int id_source_neuron;
int id_target_neuron;
float weight;
Gen(){
source_hidden_layer = (random(1) < 0.5);
id_source_neuron = (int)random(0, 7);
if (source_hidden_layer){
id_target_neuron = (int)random(0, 7);
} else {
id_target_neuron = (int)random(0, 2);
}
weight = random(-1, 1);
}
}
class Genome {
int length;
ArrayList<Gen> genes;
float[] hidden_layer_bias;
float[] output_layer_bias;
Genome(){
length = 16;
genes = new ArrayList<Gen>();
for (int i = 0; i < length; i++){
genes.add(new Gen());
}
hidden_layer_bias = random_vector(7);
output_layer_bias = random_vector(2);
}
Genome copy(){
Genome copy = new Genome();
for (int i = 0; i < length; i++){
copy.genes.set(i, genes.get(i));
}
for (int i = 0; i < 7; i++){
copy.hidden_layer_bias[i] = hidden_layer_bias[i];
}
for (int i = 0; i < 2; i++){
copy.output_layer_bias[i] = output_layer_bias[i];
}
return copy;
}
Genome mutate() {
// Start with a copy of the genome (this one, copy)
Genome mutated_genome = copy();
// But change some of the genes for new random ones
int amount_of_mutations = (int)random(1, 5);
for (int i = 0; i < amount_of_mutations; i++){
int index = (int)random(0, length);
mutated_genome.genes.set(index, new Gen());
}
return mutated_genome;
}
Genome crossover(Genome anotherGenome) {
// Start with a copy of the genome
Genome crossed_genome = copy();
// But change some of the genes for some of the other genome's genes
int amount_of_crossovers = (int)random(1, 5);
for (int i = 0; i < amount_of_crossovers; i++){
int index = (int)random(0, length);
crossed_genome.genes.set(index, anotherGenome.genes.get(index));
}
return crossed_genome;
}
}