-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathBrain.pde
121 lines (112 loc) · 3.26 KB
/
Brain.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
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
class Brain {
float[] inputs;
float[] outputs = {1, 0};
float[][] hidden_layer_weights;
float[] hidden_layer_bias;
float[] hidden_outputs;
float[][] output_layer_weights;
float[] output_layer_bias;
Brain(Genome genome) {
// Initialize weights and biases from genome
hidden_layer_weights = zeroes_matrix(7, 7);
output_layer_weights = zeroes_matrix(2, 7);
for (Gen gen : genome.genes) {
if (gen.source_hidden_layer) {
hidden_layer_weights[gen.id_target_neuron][gen.id_source_neuron] = gen.weight;
} else {
output_layer_weights[gen.id_target_neuron][gen.id_source_neuron] = gen.weight;
}
}
hidden_layer_bias = genome.hidden_layer_bias;
output_layer_bias = genome.output_layer_bias;
}
void feed_forward(float[] input_layer_values) {
inputs = input_layer_values;
hidden_outputs = matrix_vector_multiplication(hidden_layer_weights, input_layer_values);
for (int i = 0; i < hidden_outputs.length; i++) {
hidden_outputs[i] += hidden_layer_bias[i];
hidden_outputs[i] = ReLU(hidden_outputs[i]);
}
outputs = matrix_vector_multiplication(output_layer_weights, hidden_outputs);
for (int i = 0; i < outputs.length; i++) {
outputs[i] += output_layer_bias[i];
outputs[i] = ReLU(outputs[i]);
}
}
float ReLU(float x) {
return max(0, x);
}
void set_neural_connection_stroke(float weight){
if (weight > 0){
stroke(0, 255, 0);
} else if (weight < 0) {
stroke(255, 0, 0);
} else {
stroke(200);
}
weight = abs(weight);
weight = map(weight, 0, 1, 0.5, 5);
strokeWeight(weight);
}
void print() {
fill(0);
textSize(16);
text("(obstacle) distance", 550, 67);
text("(obstacle) x", 598, 107);
text("(obstacle) y", 598, 147);
text("(obstacle) width", 568, 187);
text("(obstacle) height", 563, 227);
text("(dino) y", 625, 267);
text("(game) speed", 586, 307);
text("jump", 927, 168);
text("crouch", 925, 208);
for (int i = 0; i < 7; i++){
// Input layer to hidden layer lines
for (int j = 0; j < 7; j++){
float weight = hidden_layer_weights[i][j];
set_neural_connection_stroke(weight);
line(700 + 16, 64+i*40, 800 - 16, 64+j*40);
}
// Hidden layer to output layer lines
for (int j = 0; j < 2; j++){
float weight = output_layer_weights[j][i];
set_neural_connection_stroke(weight);
line(800 + 16, 64 + i * 40, 900 - 16, 165 + j * 40);
}
// Input layer circles
strokeWeight(1);
stroke(83);
fill(255);
circle(700, 64 + i * 40, 32);
// Hidden layer circles
stroke(0);
if (hidden_outputs[i] == 0){
fill(255);
} else {
fill(170);
}
circle(800, 64 + i * 40, 32);
// Input texts
textSize(11);
fill(0);
if (inputs[i] > 99){
text(inputs[i], 688, 68+i*40);
} else {
text(inputs[i], 688, 68+i*40);
}
}
// Output circles
if (outputs[0] == 0){
fill(255);
} else {
fill(170);
}
circle(900, 165, 32);
if (outputs[1] == 0){
fill(255);
} else {
fill(170);
}
circle(900, 205, 32);
}
}