-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminds.py
110 lines (99 loc) · 3.73 KB
/
minds.py
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
"""
Name: minds.py
Author: Oliver Giles & Max Potter
Date: June 2017
Description:
- Contains creatures' neural networks
"""
from keras.models import Sequential
from keras.layers import Dense
import random
import numpy as np
import math
from bitstring import BitArray
import constants as const
inputCount = const.INPUT_COUNT
neuronsPerLayer = const.NEURONS_PER_LAYER
numHiddenLayers = const.NUM_HIDDEN_LAYERS
outputNeurons = const.OUTPUT_NEURONS
model = Sequential()
model.add(Dense(neuronsPerLayer, input_shape = (inputCount,), activation='relu'))
for i in range(numHiddenLayers):
model.add(Dense(neuronsPerLayer, activation='relu'))
model.add(Dense(outputNeurons, activation='relu'))
def get_weights(firstGeneration=False, DNA=''):
if firstGeneration:
#Generate random DNA for the first generation
DNAlength = (inputCount * neuronsPerLayer + neuronsPerLayer +
((neuronsPerLayer * neuronsPerLayer) * numHiddenLayers) + neuronsPerLayer *
outputNeurons + neuronsPerLayer * numHiddenLayers + outputNeurons)
DNAbin = []
for i in range(0, DNAlength * 12):
DNAbin.append(str(random.randint(0,1)))
DNAbin = ''.join(DNAbin)
b = BitArray(int=0, length=12)
DNA = []
for i in range(0, len(DNAbin), 12):
b.bin = DNAbin[i:i+12]
DNA.append(float(b.int))
else:
DNAbin = DNA
DNA = []
b = BitArray(int=0, length=12)
for i in range(0, len(DNAbin), 12):
b.bin = DNAbin[i:i+12]
DNA.append(float(b.int))
#Convert DNA to list of weights
weights = []
count = 0
#Normalise our converted DNA to floats between -0.5 and 0.5
DNA = 2*(DNA-np.float32(-2048))/(np.float32(2047)-np.float32(-2048)) - 1
#Convert list of weights into shaped arrays for each layer
#Input weights: inputCount x neuronsPerLayer matrix
l = np.array(DNA[count:count + inputCount*neuronsPerLayer])
count += inputCount*neuronsPerLayer
l = l.reshape(inputCount, neuronsPerLayer)
weights.append(l)
#Input activation weights: neuronsPerLayer array
l = np.array(DNA[count:count + neuronsPerLayer])
count += neuronsPerLayer
l = l.reshape(neuronsPerLayer)
weights.append(l)
for i in range(numHiddenLayers):
#Hidden layer weights: neuronsPerLayer x neuronsPerLayer matrix
l = np.array(DNA[count:count + neuronsPerLayer*neuronsPerLayer])
count += neuronsPerLayer*neuronsPerLayer
l = l.reshape(neuronsPerLayer, neuronsPerLayer)
weights.append(l)
#Hidden activation weights: neuronsPerLayer array
l = np.array(DNA[count:count + neuronsPerLayer])
count += neuronsPerLayer
l = l.reshape(neuronsPerLayer)
weights.append(l)
#Output layer weights: neuronsPerLayer x outputNeruons matrix
l = np.array(DNA[count:count + outputNeurons*neuronsPerLayer])
l = l.reshape(neuronsPerLayer, outputNeurons)
weights.append(l)
count += neuronsPerLayer * outputNeurons
#Hidden activation weights: neuronsPerLayer array
l = np.array(DNA[count:count + neuronsPerLayer])
count += neuronsPerLayer
l = l.reshape(outputNeurons)
weights.append(l)
return weights, DNAbin
def think(weights, vision, x, y, angle):
#Use height, width and tilesize to let the creature know how far it is from the centre
height, width = const.HEIGHT*const.TILESIZE, const.WIDTH*const.TILESIZE
x, y = abs(width/2-x)/float(width/2), abs(height/2-y)/float(height/2)
#Use the creatures Keras model and vision to determine direction/speed
impulse = np.float32(random.uniform(-1, 1))
tempList = []
tempList.append(impulse)
vision.append(impulse)
#vision.append(angle)
vision.append(x)
vision.append(y)
vision2 = np.array([vision]) #Flatten array
model.set_weights(weights)
actions = model.predict(vision2)
return actions