-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.pde
More file actions
370 lines (314 loc) · 12.6 KB
/
Network.pde
File metadata and controls
370 lines (314 loc) · 12.6 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
class Network {
ArrayList<Layer> layers = new ArrayList<Layer>();
ArrayList<Connection> connections = new ArrayList<Connection>();
Layer inputLayer, outputLayer;
Network(int inputs, int outputs) {
inputLayer = new Layer(this, inputs, 0);
outputLayer = new Layer(this, outputs, 1);
layers.add(inputLayer);
layers.add(outputLayer);
// Connect all/some starting nodes (NEAT)
for (Neuron outputNode : outputLayer) {
for (Neuron inputNode : inputLayer) {
if (random(1) < initConnectionsChance ||
inputNode.layer == 0 && inputNode.index == 0) {
// connect the bias node to every output node,
// otherwise random chance of connection being
// made
outputNode.connect(
inputNode,
random(-weightLimit, weightLimit)
);
}
}
}
connectRandom(); // ensure at least one connection
}
Network() {
}
void draw(float x, float y, float width, float height) {
// displays neural network
float baseNeuronSize = clamp(width / layers.size() / 2, 5, 25);
float horizontalInterval =
(width - baseNeuronSize) / (layers.size() - 1);
for (int iLayer = 0; iLayer < layers.size(); iLayer++) {
Layer layer = layers.get(iLayer);
int numNeurons = layer.neurons.size();
float verticalInterval = height / numNeurons;
float neuronSize =
min(baseNeuronSize, clamp(height / layer.neurons.size(), 5, 25)
); // the size of the neuron, so it can fit right on the screen
layer.neuronDisplaySize = neuronSize;
for (int iNeuron = 0; iNeuron < numNeurons; iNeuron++) {
Neuron neuron = layer.get(iNeuron);
stroke(neuron.output < 0 ? 0 : 255);
fill(abs(neuron.output) * 255);
float neuronX =
x + horizontalInterval * iLayer + baseNeuronSize / 2;
float neuronY =
y + verticalInterval * (iNeuron + 0.5) + baseNeuronSize / 2;
circle(neuronX, neuronY, neuronSize);
neuron.displayPos = new PVector(neuronX, neuronY);
for (Connection connection : neuron.connections) {
if (connection.enabled) {
float inNeuronSize =
layers.get(connection.neuronIn.layer)
.neuronDisplaySize;
PVector targetPos = connection.neuronIn.displayPos;
stroke(connection.weight < 0 ? 0 : 255, 64);
strokeWeight(abs(connection.weight) * 2);
line(
neuronX - baseNeuronSize / 2,
neuronY,
targetPos.x + inNeuronSize / 2,
targetPos.y
);
strokeWeight(1);
}
}
}
}
}
void think() {
for (int i = 1; i < layers.size(); i++) {
for (Neuron neuron : layers.get(i)) {
neuron.think();
}
}
}
void connectRandom() {
// create random connection
if (!fullyConnected()) {
int layer1;
int neuron1;
int layer2;
int neuron2;
do {
layer1 = floor(random(1, layers.size()));
neuron1 = floor(random(layers.get(layer1).neurons.size()));
layer2 = floor(random(0, layer1));
neuron2 = floor(random(layers.get(layer2).neurons.size()));
} while (layers.get(layer1).get(neuron1).connectedTo(
layers.get(layer2).get(neuron2)
)); // while the connection doesn't already exist
layers.get(layer1).get(neuron1).connect(
layers.get(layer2).get(neuron2),
random(-weightLimit, weightLimit)
);
}
}
boolean fullyConnected() {
// returns whether every node is connected to every other node (no new
// connections can be added)
int runningNeuronCount = 0;
for (Layer layer : layers) {
for (Neuron neuron : layer) {
if (neuron.connections.size() < runningNeuronCount) {
return false;
}
}
runningNeuronCount += layer.neurons.size();
}
return true;
}
void connect(int layer1, int neuron1, int layer2, int neuron2) {
layers.get(layer1).get(neuron1).connect(
layers.get(layer2).get(neuron2),
random(-weightLimit, weightLimit)
);
}
void addRandom() {
// Insert a node randomly between two connected nodes
Neuron neuron, neuronIn;
do {
neuron = layers.get(floor(random(1, layers.size()))).getRandom();
if (neuron.connections.size() == 1) {
neuronIn = neuron.connections.get(0).neuronIn;
} else {
neuronIn = neuron; // just to make the compiler happy
}
} while (neuron.connections.size() == 0 ||
(neuron.connections.size() == 1 && neuronIn.layer == 0 &&
neuronIn.index == 0)
); // while the node has a connection, and if it only has one, it cannot
// be the bias node
Connection connection;
do {
connection =
neuron.connections.get(floor(random(neuron.connections.size()))
);
neuronIn = connection.neuronIn;
} while (neuronIn.layer == 0 && neuronIn.index == 0
); // do not select the bias node
int layerNum = connection.neuronIn.layer + 1;
if (connection.neuronOut.layer - connection.neuronIn.layer == 1) {
addLayer(layerNum);
}
Layer layer = layers.get(layerNum);
Neuron newNeuron =
layer.add(new Neuron(this, layerNum, layer.neurons.size()));
newNeuron.connect(connection.neuronIn, connection.weight);
newNeuron.connect(layers.get(0).get(0), 0); // connect to bias
connection.neuronOut.connect(newNeuron, connection.weight);
connection.enabled = false;
}
void mutateWeights() {
for (Layer layer : layers) {
for (Neuron neuron : layer) {
neuron.mutateWeights();
}
}
}
Layer addLayer(int i) {
for (int j = i; j < layers.size(); j++) {
layers.get(j).shiftLayer();
}
Layer newLayer = new Layer(this, i);
layers.add(i, newLayer);
return newLayer;
}
NetworkDifference compare(Network target) {
ArrayList<Connection> disjoint = new ArrayList<Connection>(),
excess = new ArrayList<Connection>(),
matchingThis = new ArrayList<Connection>(),
matchingTarget = new ArrayList<Connection>();
float totalWeightDiff = 0;
float numSame = 0;
boolean connectionFound = false;
Comparator comparator =
Comparator.comparing(Connection::getInnovationNumber);
int minThis =
((Connection) Collections.min(this.connections, comparator))
.innovationNumber;
int maxThis =
((Connection) Collections.max(this.connections, comparator))
.innovationNumber;
int minTarget =
((Connection) Collections.min(target.connections, comparator))
.innovationNumber;
int maxTarget =
((Connection) Collections.max(target.connections, comparator))
.innovationNumber;
ArrayList<Connection> unmatchedConnections =
(ArrayList) this.connections.clone();
for (Connection targetConnection : target.connections) {
for (int i = 0; i < unmatchedConnections.size(); i++) {
Connection connection = unmatchedConnections.get(i);
if (connection.innovationNumber ==
targetConnection.innovationNumber) {
matchingThis.add(connection);
matchingTarget.add(targetConnection);
connectionFound = true;
numSame++;
totalWeightDiff +=
abs(connection.weight - targetConnection.weight);
unmatchedConnections.remove(connection);
i--;
}
}
if (!connectionFound) {
if (minThis < targetConnection.innovationNumber &&
maxThis > targetConnection.innovationNumber) {
disjoint.add(targetConnection);
} else {
excess.add(targetConnection);
}
}
connectionFound = false;
}
for (Connection connection : unmatchedConnections) {
if (minTarget < connection.innovationNumber &&
maxTarget > connection.innovationNumber) {
disjoint.add(connection);
} else {
excess.add(connection);
}
}
return new NetworkDifference(
disjoint,
excess,
matchingThis,
matchingTarget,
numSame != 0 ? totalWeightDiff / numSame : 0
);
}
Network mutate() {
if (random(1) < weightChance) {
mutateWeights();
}
if (random(1) < connectionChance) {
connectRandom();
}
if (random(1) < neuronChance) {
addRandom();
}
return this;
}
Network
createOffspring(Network parent2, float fitnessThis, float fitnessTarget) {
Network moreFit, lessFit, offSpring = new Network();
if (fitnessThis > fitnessTarget) {
moreFit = this;
lessFit = parent2;
} else {
moreFit = parent2;
lessFit = this;
}
NetworkDifference diff = moreFit.compare(lessFit);
for (Layer layer : moreFit.layers) {
Layer layerCopy = layer.copy(offSpring);
offSpring.layers.add(layerCopy);
for (Neuron neuron : layer.neurons) {
for (Connection connection : neuron.connections) {
int matchingIndex = diff.matching1.indexOf(connection);
float weight;
boolean enabled = true;
if (matchingIndex == -1) {
weight = connection.weight;
} else {
Connection connection2 =
diff.matching2.get(matchingIndex);
weight = random(1) < 0.5 ? connection.weight
: connection2.weight;
enabled = !connection.enabled || !connection2.enabled
? random(1) < 0.25
: true;
}
layerCopy.get(neuron.index)
.connect(
offSpring.layers.get(connection.neuronIn.layer)
.get(connection.neuronIn.index),
weight,
connection.innovationNumber,
enabled
);
}
}
}
offSpring.inputLayer = offSpring.layers.get(0);
offSpring.outputLayer =
offSpring.layers.get(offSpring.layers.size() - 1);
return offSpring;
}
Network copy() {
Network copy = new Network();
for (Layer layer : layers) {
Layer layerCopy = layer.copy(copy);
copy.layers.add(layerCopy);
for (Neuron neuron : layer.neurons) {
for (Connection connection : neuron.connections) {
layerCopy.get(neuron.index)
.connect(
copy.layers.get(connection.neuronIn.layer)
.get(connection.neuronIn.index),
connection.weight,
connection.innovationNumber
);
}
}
}
copy.inputLayer = copy.layers.get(0);
copy.outputLayer = copy.layers.get(copy.layers.size() - 1);
return copy;
}
}