-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaying-god-of-pong.py
328 lines (271 loc) · 9.1 KB
/
playing-god-of-pong.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
import random
import sys
import pygame
import datetime
import numpy as np
n = int(sys.argv[1])
n_fittests = int(sys.argv[2])
n_sons = int(sys.argv[1])
n_gen = int(sys.argv[3])
def init_n_players_1_layer(n):
with open("population.dat","w") as file:
# Write generation 1
file.write("1")
file.write("\n")
for player in range(n):
# layer 1
for neuron_3 in range(3):
for p in range(7):
r = round(2*random.random()-1,2)
file.write('{},'.format(r))
if player < n-1:
file.write("\n")
file.close()
def move_player(player,v,min_lim,max_lim):
y = player[1] + v
y = max(y,min_lim)
y = min(y,max_lim)
return [player[0],y]
def v_pc_player(pc_center,ball,v_limits):
if pc_center < ball[1]:
return v_limits[1]
elif pc_center > ball[1]:
return -v_limits[0]
else:
return 0
def move_ball(posx,posy,vx,vy):
return (posx+vx,posy+vy)
def neural_move(input,layer1):
output = layer1.dot(input)
decision = np.where(np.array(output)==max(output))[0][0]
return decision
def pong(layer1,show_game=False):
win_width = 400
win_height = 400
p_width = 20
p_height = 90
ball_radius = 5
ball_v_limit = 3
limit_pv = 3
limit_p2_up = 3
limit_p2_down = 2
if show_game:
pygame.init()
window = pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("Hi!")
p1 = [p_width/2,win_height/2-p_height/2]
p2 = [win_width-3*p_width/2,win_height/2-p_height/2]
p1_v = 0
p2_v = 0
ball = [int(win_width/4),int(win_height/2)]
ball_vx = 1
#ball_vy = ball_init_dir
ball_vy = random.sample([-1,0,1],1)[0]
n_balls_touch = 0
n_walks = 0
zero_v_plays = 0
winner = False
run = True
while run:
input = np.array([1,p1[1],ball[0],ball[1],ball_vx,ball_vy,p2[1]])
#player_vertical_position = p1[1]
#ball_horizontal_posiiton = ball[0]
#ball_vertical_position = ball[1]
#ball_horizontal_velocity = ball_vx
#ball_vartical_velocity = ball_vy
#oponent_vertical_position = p2[1]
move = neural_move(input,layer1)
if move == 1:
p1_v = -limit_pv
elif move == 2:
p1_v = limit_pv
# Conta numero de passos do player 1
n_walks += abs(p1_v)
p1 = move_player(p1,p1_v,0,win_height-p_height)
# p2 acompanha a bola se ela está na metade dele da tela
if ball[0] >= win_width/2:
p2_v = v_pc_player(p2[1]+p_height/2,ball,(limit_p2_up,limit_p2_down))
else:
p2_v = 0
p2 = move_player(p2,p2_v,0,win_height-p_height)
ball = move_ball(ball[0],ball[1],ball_vx,ball_vy)
# Bola bate no player 1
if ball[0] <= p1[0] + p_width:
# Contar vezes que os players jogaram parados
if p1_v == p2_v == 0:
zero_v_plays += 1
if p1[1] <= ball[1] <= p1[1]+p_height:
ball_vx = -ball_vx
# Mudar a velocidade vertical da bola
if p1_v != 0:
iball_vy = ball_vy+abs(p1_v)/p1_v
if iball_vy >= 0:
ball_vy = int(min(iball_vy,ball_v_limit))
else:
ball_vy = int(max(iball_vy,-ball_v_limit))
#ball_vy = min(ball_vy+p1_v,ball_v_limit)
n_balls_touch += 1
else:
run = False
# Bola bate no player 2
elif ball[0] >= p2[0]:
if p2[1] <= ball[1] <= p2[1]+p_height:
ball_vx = -ball_vx
# Mudar a velocidade vertical da bola
if p2_v != 0:
iball_vy = ball_vy+abs(p2_v)/p2_v
if iball_vy >= 0:
ball_vy = int(min(iball_vy,ball_v_limit))
else:
ball_vy = int(max(iball_vy,-ball_v_limit))
#ball_vy = min(ball_vy+p1_v,ball_v_limit)
else:
run = False
winner = True
# Bola bater nas paredes verticais
if ball[1] >= win_height or ball[1] <= 0:
ball_vy = -ball_vy
# Punir jogos longos parados
if n_balls_touch >= 3:
if zero_v_plays == n_balls_touch:
run = False
n_balls_touch = 0
p1[1] = win_height*2
# Impedir jogos inifinitos
if n_balls_touch >= 50:
run = False
winner = True
if show_game:
window.fill((0,0,0))
pygame.draw.circle(window,(255,0,0),ball,ball_radius)
pygame.draw.rect(window,(200,64,32),(p1[0],p1[1],p_width,p_height))
pygame.draw.rect(window,(21,62,86),(p2[0],p2[1],p_width,p_height))
pygame.display.update()
pygame.time.delay(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return []
keys = pygame.key.get_pressed()
if keys[pygame.K_q]:
run = False
return []
# Punir os doido atrás de bola
if winner:
score = np.exp(-n_walks/1000)
else:
score = n_balls_touch
# winner, score, i_distance_to_ball
winn = 1 if winner else 0
i_distance_to_ball = 0 if winner else (win_height - abs((p1[1]+p_height/2)-ball[1]))
if show_game:
pygame.quit()
return [winn, score, i_distance_to_ball]
def generate_son(dad,mom):
son = [0 for i in dad]
nmutations = 0
for i in range(len(dad)):
dad_gen = dad[i]
mom_gen = mom[i]
if dad_gen == mom_gen:
son[i] = dad_gen
else:
son_gen = dad_gen if random.random() >= 0.5 else mom_gen
if random.random() <= 0.1:
d = 2*(random.random()-1)
nmutations += 1
son_gen += d
son[i] = round(son_gen,2)
return son, nmutations
def train(n_best=0,show_game=False):
if n_best == 0:
input_file = "population.dat"
else:
input_file = "pop_by_fit.dat"
with open(input_file,"r") as file:
reader = file.read().split('\n')
generation = int(reader[0])
players = reader[1:len(reader)]
file.close()
round_results = []
print("Generation: ",generation)
if n_best == 0:
n_best = len(players)
output_file = "pop_by_fit.dat"
else:
output_file = "champion_of_pong.dat"
for ip in range(n_best):
p = players[ip]
all = p.split(',')
list1 = [float(f) for f in all[0:21]]
# 1 layers, 4 neurons each
layer1 = np.ndarray(shape=(3,7))
layer1[0,:] = list1[0:7]
layer1[1,:] = list1[7:14]
layer1[2,:] = list1[14:21]
r = pong(layer1,show_game)
if len(r) > 0:
round_results.append([r[0],r[1],r[2],p])
else:
print("Interrupt warning! The pong game was interrupted.")
return "broke"
print("Player {}/{}...Result: {}".format(ip+1,n_best,r))
round_results.sort(reverse=True)
with open(output_file,"w") as file:
file.write(str(generation))
file.write("\n")
for r in round_results:
file.write(r[3])
file.write("\n")
file.close()
def evolve(n_fittests,n_sons):
with open("pop_by_fit.dat","r") as file:
all = file.read().split("\n")
generation = all[0]
reproducers = [line.split(',') for line in all[1:n_fittests+1]]
reproducers = [[float(j) for j in i[:-1]] for i in reproducers]
print("Reproducing generation",generation)
sons = []
idad = 0
imom = 0
nmutat = 0
while len(sons) < n_sons:
imom += 1
if imom == len(reproducers):
idad += 1
imom = 0
if idad == len(reproducers):
idad = 0
imom = 0
if imom != idad:
dad = reproducers[idad]
mom = reproducers[imom]
son,imutat = generate_son(dad,mom)
sons.append(son)
nmutat += imutat
file.close()
with open("population.dat","w") as file:
file.write(str(int(generation)+1))
file.write("\n")
for ison in range(len(sons)):
son = sons[ison]
for gen in son:
file.write(str(gen))
file.write(',')
if ison < len(sons)-1:
file.write("\n")
file.close()
print("Reproducing of {} new players complete. {} mutations happened!".format(n_sons,nmutat))
init_n_players_1_layer(n)
broke_run = False
for generation in range(n_gen-1):
rnd = train()
if not(rnd == "broke"):
evolve(n_fittests,n_sons)
else:
broke_run = True
break
if not broke_run:
last_round = train()
if not(last_round == "broke"):
evolve(n_fittests,n_sons)
train(1,show_game=True)