-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakebackprop.py
266 lines (209 loc) · 7.21 KB
/
snakebackprop.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
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
import numpy as np
import sys
from random import randint, uniform as randfloat
np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
mutation_rate = 0.005
layers = 10 # Including input and output
grid = [2, 2]
dim = grid[0] * grid[1]
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def slope(x):
return x * (1 - x)
def error(y, f):
return (1 / 2) * (y - f) ** 2
class SnakeGame:
def __init__(self):
self.clear = 0
self.head = 1
self.bod = 2
self.food = 3
self.data = [self.clear] * dim
self.body = []
self.headpos = 0
self.data[self.headpos] = self.head
self.set_food_pos()
self.score = 0
self.increase_tail = 0 # For when something was just eaten
# 1 - Down, 2 - Left, 3 - Right, 4 - Up
self.facing = 1
self.ended = False
def update_body(self):
if len(self.body) != 0:
if self.increase_tail == 0:
self.data[self.body[len(self.body) - 1]] = self.clear
self.increase_tail = 0
self.body = [self.headpos] + self.body[:-1]
self.data[self.headpos] = self.bod
else:
self.data[self.headpos] = self.clear
def check_for_collision(self):
if self.data[self.headpos] == self.bod:
self.ended = True
def update_position(self):
if self.facing == 1: # Down
if self.headpos >= (grid[0] - 1)*grid[1]:
self.ended = True
else:
self.headpos += grid[0]
elif self.facing == 2: # Left
if self.headpos % grid[1] == 0:
self.ended = True
else:
self.headpos -= 1
elif self.facing == 3: # Right
if (self.headpos + 1) % grid[1] == 0:
self.ended = True
else:
self.headpos += 1
elif self.facing == 4: # Up
if self.headpos < grid[1]:
self.ended = True
else:
self.headpos -= grid[0]
self.check_for_collision()
self.data[self.headpos] = self.head
def set_food_pos(self):
if len(self.body) + 1 != dim:
self.foodpos = randint(0, (grid[0] * grid[1]) - 1)
while self.data[self.foodpos] != 0:
self.foodpos = randint(0, (grid[0] * grid[1]) - 1)
self.data[self.foodpos] = self.food
def increase_body(self):
if len(self.body) == 0:
self.body.append(self.headpos)
else:
self.body.append(self.body[0])
self.set_food_pos()
self.increase_tail = 1
def update_score(self):
self.score += 1
def play(self, face=0):
if face == 0:
face = self.facing
self.facing = face
self.update_body()
self.update_position()
if self.headpos == self.foodpos:
self.update_score()
self.increase_body()
def output(self):
if self.ended is False:
return np.asarray(self.data)
return False
def restart(self):
self.data = [self.clear] * dim
self.body = []
self.headpos = 0
self.data[self.headpos] = self.head
self.set_food_pos()
self.score = 0
self.increase_tail = 0 # For when something was just eaten
# 1 - Down, 2 - Left, 3 - Right, 4 - Up
self.facing = 1
self.ended = False
class SnakeNeuralNetwork:
def __init__(self, weights=None):
self.game = SnakeGame()
self.INPUT = []
self.bias_term = 1
self.played = []
self.weights = []
if weights is None:
for i in range(layers-1):
self.weights.append(np.random.rand(dim, dim+1))
self.weights.append(np.random.rand(4, dim+1))
else:
self.weights = weights
self.forward_weights = []
self.facing = 1
self.output = 0
self.alive = 0
self.fitness = 0
def feed_forward(self):
self.INPUT = self.game.output()
self.bias_term = 1
self.INPUT = np.append(self.INPUT, self.bias_term)
M1 = self.INPUT
self.forward_weights = []
for i in range(len(self.weights)):
M2 = self.weights[i]
# If not the last layer, then add the bias term
if i != layers-1:
M1 = np.append(sigmoid(np.dot(M2, M1.T)), self.bias_term)
self.forward_weights.append(M1)
else:
M1 = sigmoid(np.dot(M2, M1.T))
self.output = M1
def face(self):
output = self.output.tolist()
facing = output.index(max(output)) + 1
self.facing = facing
#self.played.append(facing)
#self.played.append({facing: list(self.INPUT)})
output = [round(x, 3) for x in output]
self.played.append({facing: output})
def get_fitness(self):
self.fitness = self.game.score
return self.fitness
def make_move(self):
self.feed_forward()
self.face()
self.game.play(self.facing)
self.get_fitness()
def restart(self):
self.game.restart()
self.INPUT = []
self.played = []
self.facing = 1
self.output = 0
self.alive = 0
self.fitness = 0
def change_weights(self, mutate):
for weight in self.weights:
shape = weight.shape
for r in range(shape[0]):
for c in range(shape[1]):
prob = randfloat(0, 1)
change = 0
if prob+mutation_rate >= 1:
change = randfloat(-mutate, mutate)
weight[r][c] += change
def derivative_net_weight(self, weight):
M1 = self.INPUT
new_weights = list(self.weights)
new_weights = new_weights[:weight-1] + new_weights[weight:]
for i in range(len(new_weights)):
M2 = new_weights[i]
# If not the last layer, then add the bias term
M1 = np.append(sigmoid(np.dot(M2, M1.T)), self.bias_term)
return M1
def backprop(self):
print("In backprop")
expected = np.copy(self.output)
output_list = self.output.tolist()
expected[output_list.index(max(output_list))] = 0
dE_dOutput = (self.output - expected)
dOuput_dNet = slope(self.output)
delta = dE_dOutput * dOuput_dNet
delta = delta.reshape(4, 1)
new_weights = []
for i in range(len(self.weights)):
dNet_dWeightx = self.derivative_net_weight(i+1)
dNet_dWeightx = dNet_dWeightx.reshape(1, 5)
new_weights.append(self.weights[i] - np.dot(delta, dNet_dWeightx))
self.weights = new_weights
def train(self):
moves = grid[0] * grid[1] + 1
prevscore = self.game.score
while self.game.score < dim:
self.make_move()
moves -= 1
if self.game.ended is True or moves <= 0:
self.backprop()
print(self.fitness, self.played)
self.restart()
prevscore = self.game.score
moves = grid[0] * grid[1] + 1
Snake = SnakeNeuralNetwork()
Snake.train()