-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuralNetworkWithBreeding.py
More file actions
427 lines (299 loc) · 10.7 KB
/
neuralNetworkWithBreeding.py
File metadata and controls
427 lines (299 loc) · 10.7 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# This script creates a basic neural network outline
# 12/10/2017
# Daniel Couturier
# ========================
import random;
import math;
import numpy as np;
# ========================
# ========================
# GLOBAL VARS
# ========================
# activationThreshold = random.uniform(0,1)
# seed = 1
# ========================
def updateSeed():
seed = seed + 1
# ========================
# create a "nodes" container
class Nodes(object):
"""docstring for word"""
def __init__(self, emptyArray=[]):
super(Nodes, self).__init__()
self.nodes = emptyArray
# ========================
# create a "weight" container
class Weights(object):
"""docstring for word"""
def __init__(self, emptyArray=[]):
super(Weights, self).__init__()
self.weights = emptyArray
# ========================
# creates a node
class Node(object):
"""docstring for word"""
def __init__(self):
super(Node, self).__init__()
self.output = int(0)
self.activationSum = int(0)
self.bias = random.random()
self.activationThreshold = random.uniform(0,1)
# ========================
# create a "Network" container
class Network(object):
"""docstring for network"""
def __init__(self, emptyArray=[]):
super(Network, self).__init__()
self.nodeLayers = []
self.weightLayers = []
# ========================
# modifies a node
def runNode(node):
node.output = 0;
finalSum = node.activationSum + node.bias
if (finalSum >= node.activationThreshold):
node.output = 1;
else:
node.output = 0;
node.activationSum = 0
# ========================
# adds a value to the activation Sum
def addActivationSum(node,num):
originalSum = node.activationSum
node.activationSum = originalSum + num
# ========================
# runs the first layer of the neural network.
def firstLayer(numInputNodes):
# FIRST LAYER ===========================
# initialInputs are ONLY used in the very first layer of inputs. This could be the distance from a wall, or a 0 or 1 value.
# Basically need to take this value and multiply it by the first layer of weights to get your first set of inputs to your first layer of nodes.
initialInputs = Weights([]);
# random.seed(seed)
# seed = seed + 1
for i in range(0, numInputNodes):
initialInputs.weights.append(random.random())
inputNodes = Nodes([]);
for i in range(0, numInputNodes):
inputNodes.nodes.append(Node())
weights = Weights([])
for x in range (0, numInputNodes):
weights.weights.append(1)
for i in range(0,numInputNodes):
# NOTE: MIGHT NOT EVEN NEED THE INPUTS VARIABLE
# inputNodes.nodes[i].input = initialInputs.weights[i] * weights.weights[i]
addActivationSum(inputNodes.nodes[i], initialInputs.weights[i] * weights.weights[i])
# once the layer nodes are all added up, need to see if they are activated
for i in range(0, numInputNodes):
runNode(inputNodes.nodes[i])
return [inputNodes, initialInputs]
def calculateNormalizeWeights(weights):
# normalize weights
weightsSum = sum(weights)
# create an array or normalized scores
normWeights = [];
for i in range(0, len(weights)):
normWeight = float(weights[i] / weightsSum)
normWeights.append(normWeight)
return normWeights
# ========================
# runs the Nth layer of the neural network.
def nLayer(numOutputNodes, prevLayer):
# OUTPUT LAYER ======================
outputNodes = Nodes([]);
numPrevLayer = len(prevLayer.nodes)
for i in range(0, numOutputNodes):
outputNodes.nodes.append(Node())
outputWeights = Weights([])
for i in range(0, numPrevLayer*numOutputNodes):
outputWeights.weights.append(random.random())
outputWeights.weights = calculateNormalizeWeights(outputWeights.weights)
# iterate through previous layer nodes
for x in range(0, len(prevLayer.nodes)):
inputNodeOutputAtX = prevLayer.nodes[x].output
# iterate through current layer
for y in range(0, numOutputNodes):
value = outputWeights.weights[(x*numOutputNodes) + y]
num = inputNodeOutputAtX * value
addActivationSum(outputNodes.nodes[y], num)
for i in range(0, numOutputNodes):
runNode(outputNodes.nodes[i])
return [outputNodes, outputWeights]
def scoreLayer(nodeLayer, target):
# score fitness of a word
score = 0;
for i in range(0, len(nodeLayer.nodes)):
if nodeLayer.nodes[i].output == target[i]:
score = score + 1;
# this is to make the better words stick out more
score = score * score;
return float(score);
def createNetwork(lastLayerNodesNum):
# CREATE A NETWORK ==================================
# INPUT LAYER
# define Network
network = Network([])
numInputNodes = 100
inputNodes = Nodes([])
initialWeights = Weights([])
[inputNodes, initialWeights] = firstLayer(numInputNodes)
network.nodeLayers.append(inputNodes)
network.weightLayers.append(initialWeights)
prevLayer = Nodes([])
prevLayer = inputNodes
nextLayerNodesNum = [60, 20, lastLayerNodesNum]
for i in range(0, len(nextLayerNodesNum)):
# NEXT LAYER(S)
numNextLayerNodes = nextLayerNodesNum[i];
nextLayer = Nodes([])
outputWeights = Weights([])
[nextLayer, outputWeights] = nLayer(numNextLayerNodes, prevLayer)
network.nodeLayers.append(nextLayer)
network.weightLayers.append(outputWeights)
prevLayer.nodes.clear()
prevLayer = Nodes([])
prevLayer = nextLayer
return network
# ====================================================
def calculateScores(pop, targetOutputs):
#find fitness of each word in the population
scores = [];
for i in range(0, len(pop)):
wordIndex = pop[i];
score = scoreLayer(pop[i].nodeLayers[len(pop[i].nodeLayers)-1], targetOutputs)
scores.append(score)
return scores
def calculateNormalizeScores(scores):
# normalize scores
scoresSum = sum(scores)
# create an array or normalized scores
normScores = [];
for i in range(0, len(scores)):
normScore = float(scores[i] / scoresSum)
normScores.append(normScore)
return normScores
def createPopulation(lastLayerNodesNum):
# create initial population
pop = []
popSize = 100
for i in range(0, popSize):
network = createNetwork(lastLayerNodesNum);
pop.append(network)
return pop
def createChild(indexA, indexB, pop, target, mutationRate):
#create child from parents
parentA = Network([])
parentB = Network([])
parentA = pop[indexA]
parentB = pop[indexB]
child = Network([]);
# GO THROUGH NODES =======
# go through all node layers
for i in range(0, len(parentA.nodeLayers)):
child.nodeLayers.append(Nodes([]))
# go through all nodes within this layer
for x in range(0, len(parentA.nodeLayers[i].nodes)):
dart = random.random();
if (dart < 0.5):
child.nodeLayers[i].nodes.append(parentA.nodeLayers[i].nodes[x])
else:
child.nodeLayers[i].nodes.append(parentB.nodeLayers[i].nodes[x])
# mutate here
rand = random.random();
if(rand < mutationRate):
newNum = random.random()
child.nodeLayers[i].nodes[x].activationThreshold = newNum
# GO THROUGH WEIGHTS
# go through all weights layers
for y in range(0, len(parentA.weightLayers)):
child.weightLayers.append(Weights([]))
# go through all weights within this layer
for z in range(0, len(parentA.weightLayers[y].weights)):
dart = random.random()
if(dart < 0.5):
child.weightLayers[y].weights.append(parentA.weightLayers[y].weights[z])
else:
child.weightLayers[y].weights.append(parentB.weightLayers[y].weights[z])
# mutate here
rand = random.random();
if(rand < mutationRate):
newNum = random.random()
child.weightLayers[y].weights[z] = newNum
child.weightLayers[y].weights = calculateNormalizeWeights(child.weightLayers[y].weights)
return child;
def createChildren(normScores, pop, target, mutationRate, lastLayerNodesNum):
#create children for entire next population
popPortion = math.floor(len(pop)/10)
pop2 = []
for i in range (0, len(pop)-popPortion):
# select parents a & b randomly "from a bag"
indexA = selectParent(normScores);
indexB = selectParent(normScores);
#create child from given parents
child = createChild(indexA, indexB, pop, target, mutationRate)
#replace child into population
pop2.append(child)
for i in range(0, popPortion):
pop2.append(createNetwork(lastLayerNodesNum))
return pop2;
def selectParent(normScores):
# select an index from the population of words based on it's probability from normalized score
# this number is always between 0 and 1
dart = random.random();
index = 0;
while (dart > 0):
dart = dart - normScores[index];
index = index + 1;
index = index - 1;
return index;
def getMaxScore(scores, pop, generation):
# get top scorer
maxScore = 0;
maxScoreIndex = 0;
for i in range(0, len(scores)):
currScore = scores[i];
if(currScore > maxScore):
maxScore = currScore;
maxScoreIndex = i;
print ("Generation:", generation, ", max score: ", maxScore)
return maxScore
def setup():
targetOutputs = [1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1]
mutationRate = 0.1
lastLayerNodesNum = len(targetOutputs)
generation = 0
return [targetOutputs, mutationRate, lastLayerNodesNum, generation]
def findTargetScore(pop, targetOutputs, generation):
scores = []
scores = calculateScores(pop, targetOutputs);
maxScore = getMaxScore(scores, pop, generation);
targetLayer = Nodes([])
for i in range(0, len(targetOutputs)):
targetLayer.nodes.append(Node())
targetLayer.nodes[i].output = targetOutputs[i]
targetScore = scoreLayer(targetLayer, targetOutputs)
normScores = calculateNormalizeScores(scores);
return [maxScore, targetScore, normScores]
# ========================
def main():
# setup the basic requirements
[targetOutputs, mutationRate, lastLayerNodesNum, generation] = setup()
# CREATE INITIAL POPULATION
pop = createPopulation(lastLayerNodesNum)
# setup teh basic target score
scores = []
[maxScore, targetScore, normScores] = findTargetScore(pop, targetOutputs, generation)
while maxScore < targetScore:
generation = generation + 1
# ===== CROSS BREEDING ===================================
#create children for entire next population
pop = createChildren(normScores, pop, targetOutputs, mutationRate, lastLayerNodesNum);
# ==== SCORING ==========================================
# calculate scores for each word
scores.clear()
scores = calculateScores(pop, targetOutputs);
# normalize word scores
normScores = calculateNormalizeScores(scores);
# print high score
maxScore = getMaxScore(scores, pop, generation);
# =========================
main();