-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNet.pde
More file actions
189 lines (142 loc) · 5.87 KB
/
NeuralNet.pde
File metadata and controls
189 lines (142 loc) · 5.87 KB
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
class NeuralNet {
int iNodes;//No. of input nodes
int hNodes;//No. of hidden nodes
int oNodes;//No. of output nodes
Matrix whi;//matrix containing weights between the input nodes and the hidden nodes
Matrix whh;//matrix containing weights between the hidden nodes and the second layer hidden nodes
Matrix woh;//matrix containing weights between the second hidden layer nodes and the output nodes
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
NeuralNet(int inputs, int hiddenNo, int outputNo) {
//set dimensions from parameters
iNodes = inputs;
oNodes = outputNo;
hNodes = hiddenNo;
//create first layer weights
//included bias weight
whi = new Matrix(hNodes, iNodes +1);
//create second layer weights
//include bias weight
whh = new Matrix(hNodes, hNodes +1);
//create second layer weights
//include bias weight
woh = new Matrix(oNodes, hNodes +1);
//set the matricies to random values
whi.randomize();
whh.randomize();
woh.randomize();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//mutation function for genetic algorithm
void mutate(float mr) {
//mutates each weight matrix
whi.mutate(mr);
whh.mutate(mr);
woh.mutate(mr);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//calculate the output values by feeding forward through the neural network
float[] output(float[] inputsArr) {
//convert array to matrix
//Note woh has nothing to do with it its just a function in the Matrix class
Matrix inputs = woh.singleColumnMatrixFromArray(inputsArr);
//add bias
Matrix inputsBias = inputs.addBias();
//-----------------------calculate the guessed output
//apply layer one weights to the inputs
Matrix hiddenInputs = whi.dot(inputsBias);
//pass through activation function(sigmoid)
Matrix hiddenOutputs = hiddenInputs.activate();
//add bias
Matrix hiddenOutputsBias = hiddenOutputs.addBias();
//apply layer two weights
Matrix hiddenInputs2 = whh.dot(hiddenOutputsBias);
Matrix hiddenOutputs2 = hiddenInputs2.activate();
Matrix hiddenOutputsBias2 = hiddenOutputs2.addBias();
//apply level three weights
Matrix outputInputs = woh.dot(hiddenOutputsBias2);
//pass through activation function(sigmoid)
Matrix outputs = outputInputs.activate();
//convert to an array and return
return outputs.toArray();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//crossover function for genetic algorithm
NeuralNet crossover(NeuralNet partner) {
//creates a new child with layer matrices from both parents
NeuralNet child = new NeuralNet(iNodes, hNodes, oNodes);
child.whi = whi.crossover(partner.whi);
child.whh = whh.crossover(partner.whh);
child.woh = woh.crossover(partner.woh);
return child;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return a neural net which is a clone of this Neural net
NeuralNet clone() {
NeuralNet clone = new NeuralNet(iNodes, hNodes, oNodes);
clone.whi = whi.clone();
clone.whh = whh.clone();
clone.woh = woh.clone();
return clone;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//converts the weights matrices to a single table
//used for storing the snakes brain in a file
Table NetToTable() {
//create table
Table t = new Table();
//convert the matricies to an array
float[] whiArr = whi.toArray();
float[] whhArr = whh.toArray();
float[] wohArr = woh.toArray();
//set the amount of columns in the table
for (int i = 0; i< max(whiArr.length, whhArr.length, wohArr.length); i++) {
t.addColumn();
}
//set the first row as whi
TableRow tr = t.addRow();
for (int i = 0; i< whiArr.length; i++) {
tr.setFloat(i, whiArr[i]);
}
//set the second row as whh
tr = t.addRow();
for (int i = 0; i< whhArr.length; i++) {
tr.setFloat(i, whhArr[i]);
}
//set the third row as woh
tr = t.addRow();
for (int i = 0; i< wohArr.length; i++) {
tr.setFloat(i, wohArr[i]);
}
//return table
return t;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//takes in table as parameter and overwrites the matrices data for this neural network
//used to load snakes from file
void TableToNet(Table t) {
//create arrays to tempurarily store the data for each matrix
float[] whiArr = new float[whi.rows * whi.cols];
float[] whhArr = new float[whh.rows * whh.cols];
float[] wohArr = new float[woh.rows * woh.cols];
//set the whi array as the first row of the table
TableRow tr = t.getRow(0);
for (int i = 0; i< whiArr.length; i++) {
whiArr[i] = tr.getFloat(i);
}
//set the whh array as the second row of the table
tr = t.getRow(1);
for (int i = 0; i< whhArr.length; i++) {
whhArr[i] = tr.getFloat(i);
}
//set the woh array as the third row of the table
tr = t.getRow(2);
for (int i = 0; i< wohArr.length; i++) {
wohArr[i] = tr.getFloat(i);
}
//convert the arrays to matrices and set them as the layer matrices
whi.fromArray(whiArr);
whh.fromArray(whhArr);
woh.fromArray(wohArr);
}
}