-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuroNet.cs
321 lines (280 loc) · 7.96 KB
/
NeuroNet.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Linq;
namespace NeuralNetwork
{
public class NeuroNet
{
internal float[][] Neurons { get; private set; }
internal float[][][] Weights { get; private set; }
internal float[][] Biases { get; private set; }
public float[] Outputs
{
get
{
return this.Neurons[this.Neurons.Length - 1];
}
}
public event EventHandler<FeedForwardFinishedEventArgs> FeedForwardFinished;
private int[] layers;
public int LayerCount
{
get
{
return this.layers.Length;
}
}
private NeuroNet()
{
// default ctor
// used for creating empty networks while crossover
}
public NeuroNet(int[] layers)
{
this.layers = new int[layers.Length];
for (int i = 0; i < layers.Length; i++)
{
this.layers[i] = layers[i];
}
this.InitNeurons();
this.InitWeightsAndBiases();
}
public NeuroNet(NeuroNet copy)
{
this.layers = new int[copy.layers.Length];
for (int i = 0; i < this.layers.Length; i++)
{
this.layers[i] = copy.layers[i];
}
this.InitNeurons();
this.InitWeightsAndBiases(copy.Weights, copy.Biases);
}
public NeuroNet(Genome genome)
{
this.LoadFromGenome(genome);
}
private void InitNeurons()
{
List<float[]> neurons = new List<float[]>();
for (int i = 0; i < this.layers.Length; i++)
{
float[] layer = new float[this.layers[i]];
for (int j = 0; j < this.layers[i]; j++)
{
layer[j] = 0f;
}
neurons.Add(layer);
}
this.Neurons = neurons.ToArray();
}
private void InitWeightsAndBiases(float[][][] presetWeights = null, float[][] presetBiases = null)
{
List<float[][]> weights = new List<float[][]>();
List<float[]> biases = new List<float[]>();
for (int i = 1; i < this.layers.Length; i++)
{
List<float[]> layerWeights = new List<float[]>();
float[] layerBiases = new float[this.layers[i]];
for (int j = 0; j < this.layers[i]; j++)
{
float[] neuronWeights = new float[this.layers[i - 1]];
if (presetBiases != null && presetBiases.Length >= i && presetBiases[i - 1].Length >= j)
{
layerBiases[j] = presetBiases[i - 1][j];
}
else
{
layerBiases[j] = NeuroHelper.RandomNext();
}
for (int k = 0; k < neuronWeights.Length; k++)
{
if (presetWeights != null && presetWeights.Length >= i && presetWeights[i - 1].Length >= j && presetWeights[i - 1][j].Length >= k)
{
neuronWeights[k] = presetWeights[i - 1][j][k];
}
else
{
neuronWeights[k] = NeuroHelper.RandomNext();
}
}
layerWeights.Add(neuronWeights);
}
biases.Add(layerBiases);
weights.Add(layerWeights.ToArray());
}
this.Weights = weights.ToArray();
this.Biases = biases.ToArray();
}
public float[] FeedForward(float[] inputs)
{
for (int i = 0; i < inputs.Length; i++)
{
this.Neurons[0][i] = inputs[i];
}
for (int i = 1; i < this.LayerCount; i++)
{
for (int j = 0; j < this.Neurons[i].Length; j++)
{
float value = 0f;
for (int k = 0; k < this.Neurons[i - 1].Length; k++)
{
value += this.Weights[i - 1][j][k] * this.Neurons[i - 1][k];
}
this.Neurons[i][j] = NeuroHelper.Sigmoid(value);
}
}
this.FeedForwardFinished?.Invoke(this, new FeedForwardFinishedEventArgs(this.Outputs));
return this.Outputs;
}
public void Mutate()
{
for (int i = 1; i < this.LayerCount; i++)
{
if (NeuroHelper.RandomNext(0f, 1f) <= NeuroHelper.LayerMutationChance)
{
List<float[]> neurons = new List<float[]>();
for (int l = 0; l < this.layers.Length; l++)
{
float[] layer = new float[this.layers[l] + 1];
for (int j = 0; j < this.layers[l]; j++)
{
layer[j] = 0f;
}
neurons.Add(layer);
}
this.Neurons = neurons.ToArray();
this.layers[i]++; // increase the neuroncount of the layer
this.InitWeightsAndBiases(this.Weights, this.Biases);
}
if (NeuroHelper.RandomNext(0f, 1f) <= NeuroHelper.NeuronWeightMutationChance)
{
int neuronIndex = NeuroHelper.RandomNext(0, this.Neurons[i].Length);
int weightIndex = NeuroHelper.RandomNext(0, this.Weights[i - 1][neuronIndex].Length);
if (weightIndex < this.Weights.Length)
{
this.Weights[i - 1][neuronIndex][weightIndex] += NeuroHelper.RandomNext(-NeuroHelper.NeuronWeightMutationDefaultValue, NeuroHelper.NeuronWeightMutationDefaultValue);
}
}
if (NeuroHelper.RandomNext(0f, 1f) <= NeuroHelper.NeuronBiasMutationChance)
{
int neuronIndex = NeuroHelper.RandomNext(0, this.Neurons[i].Length);
this.Biases[i - 1][neuronIndex] += NeuroHelper.RandomNext(-NeuroHelper.NeuronBiasMutationDefaultValue, NeuroHelper.NeuronBiasMutationDefaultValue);
}
}
}
public static NeuroNet Crossover(NeuroNet mother, NeuroNet father)
{
NeuroNet child = InitChild(mother, father);
for (int i = 0; i < child.Weights.Length; i++)
{
for (int j = 0; j < child.Weights[i].Length; j++)
{
for (int k = 0; k < child.Weights[i][j].Length; k++)
{
if (!(k >= mother.Weights[i][j].Length && mother.Weights[i][j].Length < father.Weights[i][j].Length)
&& ((k >= father.Weights[i][j].Length && mother.Weights[i][j].Length > father.Weights[i][j].Length) || NeuroHelper.RandomNext(0, 100) > 50))
{
child.Weights[i][j][k] = mother.Weights[i][j][k];
}
else
{
child.Weights[i][j][k] = father.Weights[i][j][k];
}
}
if (!(j >= mother.Biases[i].Length && mother.Biases[i].Length < father.Biases[i].Length)
&& ((j >= father.Biases[i].Length && mother.Biases[i].Length > father.Biases[i].Length) || NeuroHelper.RandomNext(0, 100) > 50))
{
child.Biases[i][j] = mother.Biases[i][j];
}
else
{
child.Biases[i][j] = father.Biases[i][j];
}
}
}
return child;
}
private static NeuroNet InitChild(NeuroNet mother, NeuroNet father)
{
int[] fetchedLayers = new int[Math.Max(mother.layers.Length, father.layers.Length)];
for (int i = 0; i < fetchedLayers.Length; i++)
{
if (mother.layers[i] >= father.layers[i])
{
fetchedLayers[i] = mother.layers[i];
}
else
{
fetchedLayers[i] = father.layers[i];
}
}
NeuroNet child = new NeuroNet
{
layers = fetchedLayers
};
child.InitNeurons();
child.InitWeightsAndBiases();
return child;
}
public static void Selection(Dictionary<NeuroNet, int> scoredNeuroNets)
{
IEnumerable<KeyValuePair<NeuroNet, int>> sortedBrains = scoredNeuroNets.OrderBy(x => x.Value);
for (int i = 0; i < sortedBrains.Count(); i++)
{
if (i <= sortedBrains.Count() / 2 && NeuroHelper.RandomNext(0, sortedBrains.Count() * 2) < ((sortedBrains.Count() * 2 - 2) - i * 2))
{
if (scoredNeuroNets.Count >= 2)
{
scoredNeuroNets.Remove(sortedBrains.ElementAt(i).Key);
}
}
if (i > sortedBrains.Count() / 2 && NeuroHelper.RandomNext(0, sortedBrains.Count() * 2) < (sortedBrains.Count() * 2) - i * 2)
{
if (scoredNeuroNets.Count >= 2)
{
scoredNeuroNets.Remove(sortedBrains.ElementAt(i).Key);
}
}
}
}
private void LoadFromGenome(Genome genome)
{
this.layers = new int[genome.LayerCount];
for (int i = 0; i < this.layers.Length; i++)
{
this.layers[i] = genome.NeuronsPerLayer[i];
}
this.InitNeurons();
this.InitWeightsAndBiases();
int idx = 0;
for (int i = 0; i < this.LayerCount - 1; i++)
{
for (int j = 0; j < this.Weights[i].Length; j++)
{
for (int k = 0; k < this.Weights[i][j].Length; k++)
{
this.Weights[i][j][k] = genome.Weights[idx];
idx++;
}
}
}
idx = 0;
for (int i = 0; i < this.LayerCount - 1; i++)
{
for (int j = 0; j < this.Biases[i].Length; j++)
{
this.Biases[i][j] = genome.Biases[idx];
idx++;
}
}
}
public void Save(string fileName)
{
Genome.Save(this, fileName);
}
public void Load(string fileName)
{
this.LoadFromGenome(Genome.Load(fileName));
}
}
}