-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeClass.py
422 lines (361 loc) · 16.5 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import os
import pygame
import argparse
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from random import randint
from tensorflow.keras.utils import to_categorical
import random
import statistics
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers.core import Dense, Dropout
import random
import numpy as np
import pandas as pd
from operator import add
import collections
class DQNAgent(object):
def __init__(self, params):
self.reward = 0
self.gamma = 0.9
self.dataframe = pd.DataFrame()
self.short_memory = np.array([])
self.agent_target = 1
self.agent_predict = 0
self.learning_rate = params['learning_rate']
self.epsilon = 1
self.actual = []
self.first_layer = params['first_layer_size']
self.second_layer = params['second_layer_size']
self.third_layer = params['third_layer_size']
self.memory = collections.deque(maxlen=params['memory_size'])
self.weights = params['weights_path']
self.load_weights = params['load_weights']
self.model = self.network()
def network(self):
model = Sequential()
model.add(Dense(self.first_layer, activation='relu', input_dim=11))
model.add(Dense(self.second_layer, activation='relu'))
model.add(Dense(self.third_layer, activation='relu'))
model.add(Dense(3, activation='softmax'))
opt = Adam(self.learning_rate)
model.compile(loss='mse', optimizer=opt)
if self.load_weights:
model.load_weights(self.weights)
return model
def get_state(self, game, player, food):
state = [
(player.x_change == 20 and player.y_change == 0 and ((list(map(add, player.position[-1], [20, 0])) in player.position) or
player.position[-1][0] + 20 >= (game.game_width - 20))) or (player.x_change == -20 and player.y_change == 0 and ((list(map(add, player.position[-1], [-20, 0])) in player.position) or
player.position[-1][0] - 20 < 20)) or (player.x_change == 0 and player.y_change == -20 and ((list(map(add, player.position[-1], [0, -20])) in player.position) or
player.position[-1][-1] - 20 < 20)) or (player.x_change == 0 and player.y_change == 20 and ((list(map(add, player.position[-1], [0, 20])) in player.position) or
player.position[-1][-1] + 20 >= (game.game_height-20))), # danger straight
(player.x_change == 0 and player.y_change == -20 and ((list(map(add,player.position[-1],[20, 0])) in player.position) or
player.position[ -1][0] + 20 > (game.game_width-20))) or (player.x_change == 0 and player.y_change == 20 and ((list(map(add,player.position[-1],
[-20,0])) in player.position) or player.position[-1][0] - 20 < 20)) or (player.x_change == -20 and player.y_change == 0 and ((list(map(
add,player.position[-1],[0,-20])) in player.position) or player.position[-1][-1] - 20 < 20)) or (player.x_change == 20 and player.y_change == 0 and (
(list(map(add,player.position[-1],[0,20])) in player.position) or player.position[-1][
-1] + 20 >= (game.game_height-20))), # danger right
(player.x_change == 0 and player.y_change == 20 and ((list(map(add,player.position[-1],[20,0])) in player.position) or
player.position[-1][0] + 20 > (game.game_width-20))) or (player.x_change == 0 and player.y_change == -20 and ((list(map(
add, player.position[-1],[-20,0])) in player.position) or player.position[-1][0] - 20 < 20)) or (player.x_change == 20 and player.y_change == 0 and (
(list(map(add,player.position[-1],[0,-20])) in player.position) or player.position[-1][-1] - 20 < 20)) or (
player.x_change == -20 and player.y_change == 0 and ((list(map(add,player.position[-1],[0,20])) in player.position) or
player.position[-1][-1] + 20 >= (game.game_height-20))), #danger left
player.x_change == -20, # move left
player.x_change == 20, # move right
player.y_change == -20, # move up
player.y_change == 20, # move down
food.x_food < player.x, # food left
food.x_food > player.x, # food right
food.y_food < player.y, # food up
food.y_food > player.y # food down
]
for i in range(len(state)):
if state[i]:
state[i]=1
else:
state[i]=0
return np.asarray(state)
def set_reward(self, player, crash):
self.reward = 0
if crash:
self.reward = -10
return self.reward
if player.eaten:
self.reward = 10
return self.reward
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def replay_new(self, memory, batch_size):
if len(memory) > batch_size:
minibatch = random.sample(memory, batch_size)
else:
minibatch = memory
for state, action, reward, next_state, done in minibatch:
target = reward
if not done:
target = reward + self.gamma * np.amax(self.model.predict(np.array([next_state]))[0])
target_f = self.model.predict(np.array([state]))
target_f[0][np.argmax(action)] = target
self.model.fit(np.array([state]), target_f, epochs=1, verbose=0)
def train_short_memory(self, state, action, reward, next_state, done):
target = reward
if not done:
target = reward + self.gamma * np.amax(self.model.predict(next_state.reshape((1, 11)))[0])
target_f = self.model.predict(state.reshape((1, 11)))
target_f[0][np.argmax(action)] = target
self.model.fit(state.reshape((1, 11)), target_f, epochs=1, verbose=0)
#################################
# Define parameters manually #
#################################
def define_parameters():
params = dict()
# Neural Network
params['epsilon_decay_linear'] = 1/75
params['learning_rate'] = 0.0005
params['first_layer_size'] = 50 # neurons in the first layer
params['second_layer_size'] = 300 # neurons in the second layer
params['third_layer_size'] = 50 # neurons in the third layer
params['episodes'] = 150
params['memory_size'] = 2500
params['batch_size'] = 1000
# Settings
params['weights_path'] = r'C:\Users\HP\Downloads\snake-ga-tf-master\weights\weights3.hdf5'
params['load_weights'] = False
params['train'] = True
params['plot_score'] = True
return params
class Game:
def __init__(self, game_width, game_height):
pygame.display.set_caption('Sapola')
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
"Start frm here"
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(r'C:\Users\HP\Downloads\snake-ga-tf-master\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(r'C:\Users\HP\Downloads\snake-ga-tf-master\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))
if train==False:
fit_reg = False
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.ylim(0,65)
plt.show()
def get_mean_stdev(array):
return statistics.mean(array), statistics.stdev(array)
def test(display_option, speed, params):
params['load_weights'] = True
params['train'] = False
score, mean, stdev = run(display_option, speed, params)
return score, mean, stdev
def run(display_option, speed, params):
pygame.init()
agent = DQNAgent(params)
weights_filepath = params['weights_path']
if params['load_weights']:
agent.model.load_weights(weights_filepath)
print("weights loaded")
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 display_option:
display(player1, food1, game, record)
while not game.crash:
if not params['train']:
agent.epsilon = 0.00
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 = to_categorical(randint(0, 2), num_classes=3)
else:
# predict action based on the old state
prediction = agent.model.predict(state_old.reshape((1, 11)))
final_move = to_categorical(np.argmax(prediction[0]), num_classes=3)
# 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 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 display_option:
display(player1, food1, game, record)
pygame.time.wait(speed)
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']:
agent.model.save_weights(params['weights_path'])
total_score, mean, stdev = test(display_option, speed, params)
if params['plot_score']:
plot_seaborn(counter_plot, score_plot, params['train'])
print('Total score: {} Mean: {} Std dev: {}'.format(total_score, mean, stdev))
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", type=bool, default=True)
parser.add_argument("--speed", type=int, default=500)
args = parser.parse_args()
params['bayesian_optimization'] = False # Use bayesOpt.py for Bayesian Optimization
run(args.display, args.speed, params)