-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.cs
executable file
·226 lines (192 loc) · 6.69 KB
/
NeuralNetwork.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
using System.Collections.Generic;
using System;
/// <summary>
/// Neural Network C# (Unsupervised)
/// </summary>
public class NeuralNetwork : IComparable<NeuralNetwork>
{
private int[] layers; //layers
private float[][] neurons; //neuron matix
private float[][][] weights; //weight matrix
private float cost; //cost of the network
/// <summary>
/// Initilizes and neural network with random weights
/// </summary>
/// <param name="layers">layers to the neural network</param>
public NeuralNetwork(int[] layers)
{
//deep copy of layers of this network
this.layers = new int[layers.Length];
for (int i = 0; i < layers.Length; i++)
{
this.layers[i] = layers[i];
}
//generate matrix
InitNeurons();
InitWeights();
}
/// <summary>
/// Deep copy constructor
/// </summary>
/// <param name="copyNetwork">Network to deep copy</param>
public NeuralNetwork(NeuralNetwork copyNetwork)
{
this.layers = new int[copyNetwork.layers.Length];
for (int i = 0; i < copyNetwork.layers.Length; i++)
{
this.layers[i] = copyNetwork.layers[i];
}
InitNeurons();
InitWeights();
CopyWeights(copyNetwork.weights);
}
private void CopyWeights(float[][][] copyWeights)
{
for (int i = 0; i < weights.Length; i++)
{
for (int j = 0; j < weights[i].Length; j++)
{
for (int k = 0; k < weights[i][j].Length; k++)
{
weights[i][j][k] = copyWeights[i][j][k];
}
}
}
}
/// <summary>
/// Create neuron matrix
/// </summary>
private void InitNeurons()
{
//Neuron Initilization
List<float[]> neuronsList = new List<float[]>();
for (int i = 0; i < layers.Length; i++) //run through all layers
{
neuronsList.Add(new float[layers[i]]); //add layer to neuron list
}
neurons = neuronsList.ToArray(); //convert list to array
}
/// <summary>
/// Create weights matrix.
/// </summary>
private void InitWeights()
{
List<float[][]> weightsList = new List<float[][]>(); //weights list which will later will converted into a weights 3D array
//itterate over all neurons that have a weight connection
for (int i = 1; i < layers.Length; i++)
{
List<float[]> layerWeightsList = new List<float[]>(); //layer weight list for this current layer (will be converted to 2D array)
int neuronsInPreviousLayer = layers[i - 1];
//itterate over all neurons in this current layer
for (int j = 0; j < neurons[i].Length; j++)
{
float[] neuronWeights = new float[neuronsInPreviousLayer]; //neruons weights
//itterate over all neurons in the previous layer and set the weights randomly between 0.5f and -0.5
for (int k = 0; k < neuronsInPreviousLayer; k++)
{
//give random weights to neuron weights
neuronWeights[k] = UnityEngine.Random.Range(-0.5f,0.5f);
}
layerWeightsList.Add(neuronWeights); //add neuron weights of this current layer to layer weights
}
weightsList.Add(layerWeightsList.ToArray()); //add this layers weights converted into 2D array into weights list
}
weights = weightsList.ToArray(); //convert to 3D array
}
/// <summary>
/// Feed forward this neural network with a given input array
/// </summary>
/// <param name="inputs">Inputs to network</param>
/// <returns></returns>
public float[] FeedForward(float[] inputs)
{
//Add inputs to the neuron matrix
for (int i = 0; i < inputs.Length; i++)
{
neurons[0][i] = inputs[i];
}
//itterate over all neurons and compute feedforward values
for (int i = 1; i < layers.Length; i++)
{
for (int j = 0; j < neurons[i].Length; j++)
{
float value = 0f;
for (int k = 0; k < neurons[i-1].Length; k++)
{
value += weights[i - 1][j][k] * neurons[i - 1][k]; //sum off all weights connections of this neuron weight their values in previous layer
}
neurons[i][j] = (float)Math.Tanh(value); //Hyperbolic tangent activation
}
}
return neurons[neurons.Length-1]; //return output layer
}
/// <summary>
/// Mutate neural network weights
/// </summary>
public void Mutate()
{
for (int i = 0; i < weights.Length; i++)
{
for (int j = 0; j < weights[i].Length; j++)
{
for (int k = 0; k < weights[i][j].Length; k++)
{
float weight = weights[i][j][k];
//mutate weight value
float randomNumber = UnityEngine.Random.Range(0f,100f);
if (randomNumber <= 2f)
{ //if 1
//flip sign of weight
weight *= -1f;
}
else if (randomNumber <= 4f)
{ //if 2
//pick random weight between -1 and 1
weight = UnityEngine.Random.Range(-0.5f, 0.5f);
}
else if (randomNumber <= 6f)
{ //if 3
//randomly increase by 0% to 100%
float factor = UnityEngine.Random.Range(0f, 1f) + 1f;
weight *= factor;
}
else if (randomNumber <= 8f)
{ //if 4
//randomly decrease by 0% to 100%
float factor = UnityEngine.Random.Range(0f, 1f);
weight *= factor;
}
weights[i][j][k] = weight;
}
}
}
}
public void AddCost(float fit)
{
cost += fit;
}
public void SetCost(float fit)
{
cost = fit;
}
public float GetCost()
{
return cost;
}
/// <summary>
/// Compare two neural networks and sort based on cost
/// </summary>
/// <param name="other">Network to be compared to</param>
/// <returns></returns>
public int CompareTo(NeuralNetwork other)
{
if (other == null) return 1;
if (cost < other.cost)
return 1;
else if (cost > other.cost)
return -1;
else
return 0;
}
}