forked from lastleon/EvolutionSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.pde
162 lines (132 loc) · 4.08 KB
/
NeuralNetwork.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
public class NeuralNetwork {
// erklärt in Video
Matrix inputLayer;
Matrix outputLayer;
Matrix[] weights;
Matrix[] hiddenLayer;
int iLLength = 9;
int oLLength = 7;
int hLAmount;
// Konstruktor bei 1. Generation an Kreaturen, Weights zufällig
NeuralNetwork(int hLLength, int hLAmount) {
this.hLAmount = hLAmount;
weights = new Matrix[hLAmount+1];
hiddenLayer = new Matrix[hLAmount];
// InputSchicht wird erstellt
inputLayer = new Matrix(iLLength, 1);
// weights werden erstellt
for (int i=0; i<hLAmount+1; i++) {
if (i==0) {
weights[i] = new Matrix(hLLength, iLLength);
weights[i].setRandom(-1/sqrt(iLLength), 1/sqrt(iLLength));
} else if (i==hLAmount) {
weights[i] = new Matrix(oLLength, hLLength);
weights[i].setRandom(-1/sqrt(oLLength), 1/sqrt(oLLength));
} else {
weights[i] = new Matrix(hLLength, hLLength);
weights[i].setRandom(-1/sqrt(hLLength), 1/sqrt(hLLength));
}
}
// HiddenSchichten werden erstellt
for (int i=0; i<hLAmount; i++) {
hiddenLayer[i] = new Matrix(hLLength, 1);
}
// OutputSchicht wird erstellt
outputLayer = new Matrix(oLLength, 1);
}
// bei weiteren Generationen an Kreaturen
NeuralNetwork(int hLLength, Matrix[] w) {
hLAmount = w.length-1;
weights = new Matrix[hLAmount+1];
hiddenLayer = new Matrix[hLAmount];
// InputSchicht wird erstellt
inputLayer = new Matrix(iLLength, 1);
// weights werden erstellt
for (int i=0; i<hLAmount+1; i++) {
if (i==0) {
weights[i] = new Matrix(hLLength, iLLength);
weights[i].set(w[i].m);
} else if (i==hLAmount) {
weights[i] = new Matrix(oLLength, hLLength);
weights[i].set(w[i].m);
} else {
weights[i] = new Matrix(hLLength, hLLength);
weights[i].set(w[i].m);
}
}
// HiddenSchichten werden erstellt
for (int i=0; i<hLAmount; i++) {
hiddenLayer[i] = new Matrix(hLLength, 1);
}
outputLayer = new Matrix(oLLength, 1);
}
public void update() {
for (int i=0; i<hLAmount; i++) {
if (i==0) {
hiddenLayer[i].mult(weights[i], inputLayer);
hiddenLayer[i].tanh();
} else {
hiddenLayer[i].mult(weights[i], hiddenLayer[i-1]);
hiddenLayer[i].tanh();
}
}
outputLayer.mult(weights[hLAmount], hiddenLayer[hLAmount-1]);
outputLayer.tanh();
}
//// setter
public void setInputNVelocity(float v) {
inputLayer.set(0, 0, v);
}
public void setInputNEnergy(float v) {
inputLayer.set(1, 0, v);
}
public void setInputNFieldType(float v) {
inputLayer.set(2, 0, v);
}
public void setInputNMemory(float v) {
inputLayer.set(3, 0, v);
}
//// Fühler
public void setInputNSensorEnemyEnergy(float v) {
inputLayer.set(4, 0, v);
}
public void setInputNSensorFieldEnergy(float v) {
inputLayer.set(5, 0, v);
}
public void setInputNSensorFieldType(float v) {
inputLayer.set(6, 0, v);
}
//// Nicht mehr Fühler
public void setInputNPartnerFitness(float v) {
inputLayer.set(7, 0, v);
}
public void setInputNMemory2(float v) {
inputLayer.set(8, 0, v);
}
//// OutputSchicht
public float getGeschwindigkeit(Creature c) {
return map(outputLayer.get(0, 0), -1, 1, 0, c.getMaxVelocity());
}
public float getRotation() {
return map(outputLayer.get(1, 0), -1, 1, -Creature.maxMovementRotationAngle/2, Creature.maxMovementRotationAngle/2);
}
public float getMemory() {
return outputLayer.get(2, 0);
}
public float getEatingWill() {
return map(outputLayer.get(3, 0), -1, 1, 0, 1);
}
public float getBirthWill() {
return map(outputLayer.get(4, 0), -1, 1, 0, 1);
}
public float getAttackWill() {
return map(outputLayer.get(5, 0), -1, 1, 0, 1);
}
public float getMemory2() {
return outputLayer.get(6, 0);
}
// andere getter
public Matrix[] getWeights() {
return weights;
}
}