-
Notifications
You must be signed in to change notification settings - Fork 1k
/
snakeClass.py
337 lines (289 loc) · 11.8 KB
/
snakeClass.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
import os
import pygame
import argparse
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from DQN import DQNAgent
from random import randint
import random
import statistics
import torch.optim as optim
import torch
from GPyOpt.methods import BayesianOptimization
from bayesOpt import *
import datetime
import distutils.util
DEVICE = 'cpu' # 'cuda' if torch.cuda.is_available() else 'cpu'
#################################
# Define parameters manually #
#################################
def define_parameters():
params = dict()
# Neural Network
params['epsilon_decay_linear'] = 1/100
params['learning_rate'] = 0.00013629
params['first_layer_size'] = 200 # neurons in the first layer
params['second_layer_size'] = 20 # neurons in the second layer
params['third_layer_size'] = 50 # neurons in the third layer
params['episodes'] = 250
params['memory_size'] = 2500
params['batch_size'] = 1000
# Settings
params['weights_path'] = 'weights/weights.h5'
params['train'] = False
params["test"] = True
params['plot_score'] = True
params['log_path'] = 'logs/scores_' + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) +'.txt'
return params
class Game:
""" Initialize PyGAME """
def __init__(self, game_width, game_height):
pygame.display.set_caption('SnakeGen')
self.game_width = game_width
self.game_height = game_height
self.gameDisplay = pygame.display.set_mode((game_width, game_height + 60))
self.bg = pygame.image.load("img/background.png")
self.crash = False
self.player = Player(self)
self.food = Food()
self.score = 0
class Player(object):
def __init__(self, game):
x = 0.45 * game.game_width
y = 0.5 * game.game_height
self.x = x - x % 20
self.y = y - y % 20
self.position = []
self.position.append([self.x, self.y])
self.food = 1
self.eaten = False
self.image = pygame.image.load('img/snakeBody.png')
self.x_change = 20
self.y_change = 0
def update_position(self, x, y):
if self.position[-1][0] != x or self.position[-1][1] != y:
if self.food > 1:
for i in range(0, self.food - 1):
self.position[i][0], self.position[i][1] = self.position[i + 1]
self.position[-1][0] = x
self.position[-1][1] = y
def do_move(self, move, x, y, game, food, agent):
move_array = [self.x_change, self.y_change]
if self.eaten:
self.position.append([self.x, self.y])
self.eaten = False
self.food = self.food + 1
if np.array_equal(move, [1, 0, 0]):
move_array = self.x_change, self.y_change
elif np.array_equal(move, [0, 1, 0]) and self.y_change == 0: # right - going horizontal
move_array = [0, self.x_change]
elif np.array_equal(move, [0, 1, 0]) and self.x_change == 0: # right - going vertical
move_array = [-self.y_change, 0]
elif np.array_equal(move, [0, 0, 1]) and self.y_change == 0: # left - going horizontal
move_array = [0, -self.x_change]
elif np.array_equal(move, [0, 0, 1]) and self.x_change == 0: # left - going vertical
move_array = [self.y_change, 0]
self.x_change, self.y_change = move_array
self.x = x + self.x_change
self.y = y + self.y_change
if self.x < 20 or self.x > game.game_width - 40 \
or self.y < 20 \
or self.y > game.game_height - 40 \
or [self.x, self.y] in self.position:
game.crash = True
eat(self, food, game)
self.update_position(self.x, self.y)
def display_player(self, x, y, food, game):
self.position[-1][0] = x
self.position[-1][1] = y
if game.crash == False:
for i in range(food):
x_temp, y_temp = self.position[len(self.position) - 1 - i]
game.gameDisplay.blit(self.image, (x_temp, y_temp))
update_screen()
else:
pygame.time.wait(300)
class Food(object):
def __init__(self):
self.x_food = 240
self.y_food = 200
self.image = pygame.image.load('img/food2.png')
def food_coord(self, game, player):
x_rand = randint(20, game.game_width - 40)
self.x_food = x_rand - x_rand % 20
y_rand = randint(20, game.game_height - 40)
self.y_food = y_rand - y_rand % 20
if [self.x_food, self.y_food] not in player.position:
return self.x_food, self.y_food
else:
self.food_coord(game, player)
def display_food(self, x, y, game):
game.gameDisplay.blit(self.image, (x, y))
update_screen()
def eat(player, food, game):
if player.x == food.x_food and player.y == food.y_food:
food.food_coord(game, player)
player.eaten = True
game.score = game.score + 1
def get_record(score, record):
if score >= record:
return score
else:
return record
def display_ui(game, score, record):
myfont = pygame.font.SysFont('Segoe UI', 20)
myfont_bold = pygame.font.SysFont('Segoe UI', 20, True)
text_score = myfont.render('SCORE: ', True, (0, 0, 0))
text_score_number = myfont.render(str(score), True, (0, 0, 0))
text_highest = myfont.render('HIGHEST SCORE: ', True, (0, 0, 0))
text_highest_number = myfont_bold.render(str(record), True, (0, 0, 0))
game.gameDisplay.blit(text_score, (45, 440))
game.gameDisplay.blit(text_score_number, (120, 440))
game.gameDisplay.blit(text_highest, (190, 440))
game.gameDisplay.blit(text_highest_number, (350, 440))
game.gameDisplay.blit(game.bg, (10, 10))
def display(player, food, game, record):
game.gameDisplay.fill((255, 255, 255))
display_ui(game, game.score, record)
player.display_player(player.position[-1][0], player.position[-1][1], player.food, game)
food.display_food(food.x_food, food.y_food, game)
def update_screen():
pygame.display.update()
def initialize_game(player, game, food, agent, batch_size):
state_init1 = agent.get_state(game, player, food) # [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0]
action = [1, 0, 0]
player.do_move(action, player.x, player.y, game, food, agent)
state_init2 = agent.get_state(game, player, food)
reward1 = agent.set_reward(player, game.crash)
agent.remember(state_init1, action, reward1, state_init2, game.crash)
agent.replay_new(agent.memory, batch_size)
def plot_seaborn(array_counter, array_score, train):
sns.set(color_codes=True, font_scale=1.5)
sns.set_style("white")
plt.figure(figsize=(13,8))
fit_reg = False if train== False else True
ax = sns.regplot(
np.array([array_counter])[0],
np.array([array_score])[0],
#color="#36688D",
x_jitter=.1,
scatter_kws={"color": "#36688D"},
label='Data',
fit_reg = fit_reg,
line_kws={"color": "#F49F05"}
)
# Plot the average line
y_mean = [np.mean(array_score)]*len(array_counter)
ax.plot(array_counter,y_mean, label='Mean', linestyle='--')
ax.legend(loc='upper right')
ax.set(xlabel='# games', ylabel='score')
plt.show()
def get_mean_stdev(array):
return statistics.mean(array), statistics.stdev(array)
def test(params):
params['load_weights'] = True
params['train'] = False
params["test"] = False
score, mean, stdev = run(params)
return score, mean, stdev
def run(params):
"""
Run the DQN algorithm, based on the parameters previously set.
"""
pygame.init()
agent = DQNAgent(params)
agent = agent.to(DEVICE)
agent.optimizer = optim.Adam(agent.parameters(), weight_decay=0, lr=params['learning_rate'])
counter_games = 0
score_plot = []
counter_plot = []
record = 0
total_score = 0
while counter_games < params['episodes']:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Initialize classes
game = Game(440, 440)
player1 = game.player
food1 = game.food
# Perform first move
initialize_game(player1, game, food1, agent, params['batch_size'])
if params['display']:
display(player1, food1, game, record)
steps = 0 # steps since the last positive reward
while (not game.crash) and (steps < 100):
if not params['train']:
agent.epsilon = 0.01
else:
# agent.epsilon is set to give randomness to actions
agent.epsilon = 1 - (counter_games * params['epsilon_decay_linear'])
# get old state
state_old = agent.get_state(game, player1, food1)
# perform random actions based on agent.epsilon, or choose the action
if random.uniform(0, 1) < agent.epsilon:
final_move = np.eye(3)[randint(0,2)]
else:
# predict action based on the old state
with torch.no_grad():
state_old_tensor = torch.tensor(state_old.reshape((1, 11)), dtype=torch.float32).to(DEVICE)
prediction = agent(state_old_tensor)
final_move = np.eye(3)[np.argmax(prediction.detach().cpu().numpy()[0])]
# perform new move and get new state
player1.do_move(final_move, player1.x, player1.y, game, food1, agent)
state_new = agent.get_state(game, player1, food1)
# set reward for the new state
reward = agent.set_reward(player1, game.crash)
# if food is eaten, steps is set to 0
if reward > 0:
steps = 0
if params['train']:
# train short memory base on the new action and state
agent.train_short_memory(state_old, final_move, reward, state_new, game.crash)
# store the new data into a long term memory
agent.remember(state_old, final_move, reward, state_new, game.crash)
record = get_record(game.score, record)
if params['display']:
display(player1, food1, game, record)
pygame.time.wait(params['speed'])
steps+=1
if params['train']:
agent.replay_new(agent.memory, params['batch_size'])
counter_games += 1
total_score += game.score
print(f'Game {counter_games} Score: {game.score}')
score_plot.append(game.score)
counter_plot.append(counter_games)
mean, stdev = get_mean_stdev(score_plot)
if params['train']:
model_weights = agent.state_dict()
torch.save(model_weights, params["weights_path"])
if params['plot_score']:
plot_seaborn(counter_plot, score_plot, params['train'])
return total_score, mean, stdev
if __name__ == '__main__':
# Set options to activate or deactivate the game view, and its speed
pygame.font.init()
parser = argparse.ArgumentParser()
params = define_parameters()
parser.add_argument("--display", nargs='?', type=distutils.util.strtobool, default=True)
parser.add_argument("--speed", nargs='?', type=int, default=50)
parser.add_argument("--bayesianopt", nargs='?', type=distutils.util.strtobool, default=False)
args = parser.parse_args()
print("Args", args)
params['display'] = args.display
params['speed'] = args.speed
if args.bayesianopt:
bayesOpt = BayesianOptimizer(params)
bayesOpt.optimize_RL()
if params['train']:
print("Training...")
params['load_weights'] = False # when training, the network is not pre-trained
run(params)
if params['test']:
print("Testing...")
params['train'] = False
params['load_weights'] = True
run(params)