-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpongEvolution.py
375 lines (317 loc) · 11.9 KB
/
pongEvolution.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
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
import pygame
import numpy as np
import random
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,180,0)
BLUE = (50,200,255)
FILL = BLACK
TEXT = WHITE
pygame.init()
#Here you can specify the structure of the neural network. This includes the input layer and output layer.
#e.g 3 inputs, 5 node hidden layer, 4 outputs would be [3, 5, 4]
#Be sure to update this if you add inputs
layer_structure = [4, 3]
#Initializing the display window
size = (800,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("pong")
testCoefs = [np.array([[0.38238344, 0.7515745 , 0.29565119, 0.35490288, 0.97040034],
[0.33545982, 0.0973694 , 0.41539856, 0.76129553, 0.93089118],
[0.85154809, 0.0240888 , 0.74555908, 0.34759429, 0.37355357],
[0.95104127, 0.29077331, 0.21244898, 0.78876218, 0.35243364]]), np.array([[0.25498077, 0.03853811, 0.76089995],
[0.36535132, 0.60519588, 0.08365677],
[0.12852428, 0.0156597 , 0.03317768],
[0.1276382 , 0.13700435, 0.6786845 ],
[0.71931642, 0.8930938 , 0.24983195]])]
#Paddle class
class Paddle:
def __init__(self, x = 400, xspeed = 0, coefs = 0, intercepts = 0):
self.x = x
self.xlast = x-xspeed
self.xspeed = xspeed
self.alive = True
self.score = 0
self.command = 2
self.winner = False
if coefs == 0:
self.coefs = self.generateCoefs(layer_structure)
else:
self.coefs = coefs
if intercepts == 0:
self.intercepts = self.generateIntercepts(layer_structure)
else:
self.intercepts = intercepts
#Creates random coefficients for the neural network
def generateCoefs(self, layer_structure):
coefs = []
for i in range(len(layer_structure)-1):
coefs.append(np.random.rand(layer_structure[i], layer_structure[i+1])*2-1)
return coefs
#Creates random intercepts for the neural network
def generateIntercepts(self, layer_structure):
intercepts = []
for i in range(len(layer_structure)-1):
intercepts.append(np.random.rand(layer_structure[i+1])*2-1)
return intercepts
#Returns mutated coefs
def mutateCoefs(self):
newCoefs = self.coefs.copy()
for i in range(len(newCoefs)):
for row in range(len(newCoefs[i])):
for col in range(len(newCoefs[i][row])):
newCoefs[i][row][col] = np.random.normal(newCoefs[i][row][col], 1)
return newCoefs
#Returns mutated intercepts
def mutateIntercepts(self):
newIntercepts = self.intercepts.copy()
for i in range(len(newIntercepts)):
for row in range(len(newIntercepts[i])):
newIntercepts[i][row] = np.random.normal(newIntercepts[i][row], 1)
return newIntercepts
#Returns a paddle with mutated coefs and intercepts
def mutate(self):
return Paddle(coefs = self.mutateCoefs(), intercepts = self.mutateIntercepts())
#Reset score, speed and position
def reset(self):
self.x = 400
self.xlast = 400
self.xspeed = 0
self.alive = True
self.score = 0
#Update position based on speed
def update(self):
self.xlast = self.x
self.x += self.xspeed
if self.x < 0:
self.x = 0
elif self.x > size[0]-100:
self.x=size[0]-100
self.xlast = self.x
#Draw the paddle to the screen
def draw(self):
if self.winner == False:
pygame.draw.rect(screen,BLACK,[self.x,size[1]-20,100,20])
pygame.draw.rect(screen,RED,[self.x+2,size[1]-18,100-4,20-4])
else:
pygame.draw.rect(screen,BLACK,[self.x,size[1]-20,100,20])
pygame.draw.rect(screen,BLUE,[self.x+2,size[1]-18,100-4,20-4])
#Ball class
class Ball:
def __init__(self, x = 50, y = 50, xspeed = 5, yspeed = 5):
self.x = x
self.y = y
self.xlast = x-xspeed
self.ylast = y-yspeed
self.xspeed = xspeed
self.yspeed = yspeed
self.alive = True
#Update position based on speed
def update(self, paddle):
self.xlast = self.x
self.ylast = self.y
self.x += self.xspeed
self.y += self.yspeed
#Accounts for bouncing off walls and paddle
if self.x<0:
self.x=0
self.xspeed = self.xspeed * -1
elif self.x>size[0]-15:
self.x=size[0]-15
self.xspeed = self.xspeed * -1
elif self.y<0:
self.y=0
self.yspeed = self.yspeed * -1
elif self.x>paddle.x and self.x<paddle.x+100 and self.ylast<size[1]-35 and self.y>=size[1]-35:
self.yspeed = self.yspeed * -1
paddle.score = paddle.score + 1
elif self.y>size[1]:
self.yspeed = self.yspeed * -1
paddle.alive = False
paddle.score -= round(abs((paddle.x+50)-self.x)/100,2)
#Draw ball to screen
def draw(self):
pygame.draw.rect(screen,WHITE,[self.x,self.y,15,15])
#Predicts the output for a given input given an array of coefficients and an array of intercepts
def calculateOutput(input, layer_structure, coefs, intercepts, g="identity"):
#The values of the neurons for each layer will be stores in "layers", so here the input layer is added to start
#(Stuff is transposed since we need columns for matrix multiplication)
layers = [np.transpose(input)]
#The current layer will be affected by the previous layer, so here we define the starting previousLayer as the input
previousLayer = np.transpose(input)
reduced_layer_structure = layer_structure[1:]
#Loops through the all the layers except the input
for k in range(len(reduced_layer_structure)):
#creates an empty array of the correct size
currentLayer = np.empty((reduced_layer_structure[k],1))
#The resulting layer is a matrix multiplication of the previousLayer and the coefficients, plus the intercepts
result = np.matmul(np.transpose(coefs[k]),previousLayer) + np.transpose(np.array([intercepts[k]]))
#The value of each neuron is then put through a function g()
for i in range(len(currentLayer)):
if g == "identity":
currentLayer[i] = result[i]
elif g == "relu":
currentLayer[i] = max(0, result[i])
elif g == "tanh":
currentLayer[i] = tanh(result[i])
elif g == "logistic":
try:
currentLayer[i] = 1 / (1 + exp(-1*result[i]))
except OverflowError:
currentLayer[i] = 0
#The current layer is then added to the layers list, and the previousLayer variable is updated
layers.append(currentLayer)
previousLayer = currentLayer.copy()
#Returns the index of the highest value neuron in the output layer (aka layers[-1])
#E.g. if the 7th neuron has the highest value, returns 7
return(layers[-1].tolist().index(max(layers[-1].tolist())))
#Returns a set of coefficients which are a mutation of the input
def mutateCoefs(coefs):
newCoefs = []
for array in coefs:
newCoefs.append(np.copy(array))
for i in range(len(newCoefs)):
for row in range(len(newCoefs[i])):
for col in range(len(newCoefs[i][row])):
newCoefs[i][row][col] = np.random.normal(newCoefs[i][row][col], 1)
return newCoefs
#Returns a set of intercepts which are a mutation of the input
def mutateIntercepts(intercepts):
newIntercepts = []
for array in intercepts:
newIntercepts.append(np.copy(array))
for i in range(len(newIntercepts)):
for row in range(len(newIntercepts[i])):
newIntercepts[i][row] = np.random.normal(newIntercepts[i][row], 1)
return newIntercepts
#Displays the nodes of a network, along with weighted lines showing the coefficient influences
def displayNetwork(layer_sctructure, coefs = testCoefs, command = 0):
#Stores the larges coefficient, so we can scale the thicknesses accordingly.
max_coef = np.max(coefs[0])
#Determines how much space this visual network will take up
height = 300
width = 300
inputs = ["paddle x", "ball x", "ball y", "ball Xspeed", "ball Yspeed"]
outputs = ["left", "right", "stop"]
layerCount = len(layer_structure)
#This will store the positions of all the nodes (organized with sub-lists of each layer)
circle_positions = []
#Label inputs
for i in range(layer_structure[0]):
font= pygame.font.SysFont('Calibri', 30, False, False)
text = font.render(inputs[i], True, TEXT)
screen.blit(text,[0,(i+1)* int(height/(layer_structure[0]+2))])
#Label outputs
for i in range(layer_structure[-1]):
font= pygame.font.SysFont('Calibri', 30, False, False)
text = font.render(str(outputs[i]), True, TEXT)
screen.blit(text,[width+50,(i+1)* int(height/(layer_structure[-1]+2))])
#Calculates an appropriate spacing of the layers
xspacing = int( width/(layerCount))
#Determine the location of the neurons for each layer, stores that in a list, and stores those lists in circle_positions
for i in range(layerCount):
layer_circle_positions = []
yspacing = int( height/(layer_structure[i]+2))
for j in range(layer_structure[i]):
layer_circle_positions.append(((i+1)*xspacing, (j+1)*yspacing))
circle_positions.append(layer_circle_positions)
#Draws a line between every node in one layer and every node in the next layer
for i in range(len(circle_positions)-1):
for j, circle_pos in enumerate(circle_positions[i]):
for k, circle_pos2 in enumerate(circle_positions[i+1]):
thickness = int(coefs[i][j,k]/max_coef*8)
if thickness > 0:
pygame.draw.lines(screen, BLUE, False, [circle_pos, circle_pos2], thickness)
else:
pygame.draw.lines(screen, RED, False, [circle_pos, circle_pos2], -thickness)
#Draws circles in the positions of the nodes (over the lines)
for layer in circle_positions:
for circle_pos in layer:
pygame.draw.circle(screen, BLACK, circle_pos, 20, 0)
pygame.draw.circle(screen, GREEN, circle_pos, 16, 0)
done = False
score = 0
command = "stop"
clock=pygame.time.Clock()
COUNT = 100
#create sprites
paddles = []
balls = []
for i in range(100):
paddles.append(Paddle())
balls.append(Ball())
#The first winner is arbitrarily chosen to be the last one (just so the user has a network to watch on screen)
winner = paddles[-1]
paddles[-1].winner = True
#game's main loop
generation = 1
while not done:
screen.fill(FILL)
#Track the number of paddles still alive in this generation
still_alive = 0
#Track the high score and the index of the highest scoring paddle
high_score = -9e99
high_score_index = -1
#Allow user to exit at any time
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#Loop through all the paddles
for i, paddle in enumerate(paddles):
#If you change the number of inputs, be sure to change the layer_structure at the top and the input text in displayNetwork
input = np.array([[paddle.x, balls[i].x, balls[i].y, balls[i].xspeed]])
paddle.command = calculateOutput(input, layer_structure, paddle.coefs, paddle.intercepts)
#0=left, 1=right, 2=stop
if paddle.command == 0:
paddle.xspeed = -5
elif paddle.command == 1:
paddle.xspeed = 5
elif paddle.command == 2:
paddle.xspeed = 0
#Update position of all living paddles
if paddle.alive == True:
paddle.update()
balls[i].update(paddle)
still_alive += 1
#Update high_score and high_scorer
if paddle.score > high_score:
high_score = paddle.score
high_score_index = i
winner = paddles[i]
winner.winner = True
#Draw everything but the winner
if paddle.alive and paddle != winner:
paddle.draw()
balls[i].draw()
paddle.winner = False
#draw the winner last (so that it is not hidden behind other paddles)
paddles[high_score_index].draw()
balls[high_score_index].draw()
#If all the paddles are dead, reproduce the most fit one
if still_alive == 0:
generation += 1
winner.reset()
print(high_score_index)
#clear the generation
paddles = []
balls = []
#Fill it with mutations of the winner
for i in range(COUNT-1):
paddles.append(Paddle(coefs = mutateCoefs(winner.coefs), intercepts = mutateIntercepts(winner.intercepts)))
balls.append(Ball())
#add the winner itself
paddles.append(winner)
balls.append(Ball())
#score board
font= pygame.font.SysFont('Calibri', 50, False, False)
text = font.render("Score = " + str(high_score), True, TEXT)
screen.blit(text,[size[0]-300,30])
text2 = font.render("Still alive = " + str(still_alive), True, TEXT)
screen.blit(text2,[size[0]-300, 90])
text2 = font.render("Generation = " + str(generation), True, TEXT)
screen.blit(text2,[size[0]-300, 150])
displayNetwork(layer_structure, coefs = winner.coefs)
pygame.display.flip()
clock.tick(60)
pygame.quit()