-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
370 lines (309 loc) · 10.9 KB
/
game.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
# ================
# Import libraries
# ================
import pygame
import random
from enum import Enum
from collections import namedtuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as fun
import torch.optim as optim
import os
import matplotlib.pyplot as plt
from IPython import display
# ================
# End import
# ================
# ======================================================================================================================
# GAME CODE
# ======================================================================================================================
pygame.init()
font = pygame.font.Font("comfortaa.ttf", 15)
# ================
# Navigation
# ================
class Direction(Enum):
NORTH = 1
SOUTH = 2
WEST = 3
EAST = 4
coordinate = namedtuple("Turn", ["x", "y"])
# ================
# End navigation
# ================
# ================
# Game parameters
# ================
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
CELL = 20 # Size of each cell
SPEED = 30 # Speed of the snake
# ================
# End game parameters
# ================
# ================
# Game window
# ================
class Game:
# ================
# GUI settings
# ================
def gui_settings(self):
self.display.fill(WHITE)
# Render snake
for border in self.snake:
pygame.draw.rect(self.display, GREEN, pygame.Rect(border.x, border.y, CELL, CELL))
pygame.draw.rect(self.display, YELLOW, pygame.Rect(border.x + 2, border.y + 2, 6, 6))
# Render food
pygame.draw.rect(self.display,
RED,
pygame.Rect(self.food.x,
self.food.y,
CELL,
CELL)
)
# Render score
text = font.render(f"Score: {str(self.score)}", True, BLACK)
self.display.blit(text, [0, 0])
pygame.display.flip()
def __init__(self, width=720, height=720):
pygame.display.set_caption("Snake Arena")
self.width = width
self.height = height
self.display = pygame.display.set_mode((width, height))
self.clock = pygame.time.Clock()
self.reset()
# Initialize components
# self.snake = [0]
# self.food = None
# self.head = None
# self.score = None
# self.refresh = None
# self.direction = None
# ================
# End GUI settings
# ================
# ================
# Snake navigation
# ================
def navigate(self, action):
right = [Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.NORTH]
position = right.index(self.direction)
if np.array_equal(action, np.array([1, 0, 0])):
change_position = right[position]
elif np.array_equal(action, np.array([0, 1, 0])):
new_position = ((position + 1) % 4)
change_position = right[new_position]
else:
new_position = ((position - 1) % 4)
change_position = right[new_position]
self.direction = change_position
x = self.head.x
y = self.head.y
if self.direction == Direction.EAST:
x += CELL
elif self.direction == Direction.WEST:
x -= CELL
elif self.direction == Direction.SOUTH:
y += CELL
elif self.direction == Direction.NORTH:
y -= CELL
self.head = coordinate(x, y)
# ================
# End Snake navigation
# ================
# ================
# Game reset
# ================
def reset(self):
self.direction = Direction.EAST
self.head = coordinate(self.width // 2, self.height // 2)
self.snake = [self.head, coordinate(self.head.x - CELL, self.head.y),
coordinate(self.head.x - (2 * CELL), self.head.y)]
self.score = 0
self.food = None
self.food_gen()
self.refresh = 0
# ================
# End reset
# ================
# ================
# Check for collision
# ================
def collision(self, collide=None):
if collide is None:
collide = self.head
# Quit if the snake head hits the border
if collide.x > self.width - CELL or collide.x < 0 or collide.y > self.height - CELL or collide.y < 0:
return True
# Quit if the snake head hits itself
if collide in self.snake[1:]:
return True
return False
# ================
# End collision check
# ================
# ================
# Game actions
# ================
def next_action(self, action):
self.refresh += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Insert new head at the beginning towards the direction of the movement
self.navigate(action)
self.snake.insert(0, self.head)
"""
Initialize the reward variable as 0
We will check for collisions
If the snake hits the border:
i) Game will be reset
ii) Reward will be set to (-1)
Negative rewards will result in removal of the snake generation
"""
# Begin collision check
reward = 0
end = False
if self.collision() or self.refresh > 100 * len(self.snake):
end = True
reward = -1
return reward, end, self.score
# End collision check
"""
We consider the snake eating the food as a positive step
The more food it eats, the more reward it gets
Increase rewards lets the snake generation pass the test
"""
# Update score
if self.head == self.food:
self.score += 1
reward = 1
self.food_gen()
else:
self.snake.pop()
# End update score
# Update the arena
self.gui_settings()
self.clock.tick(SPEED)
# End update
# WASTED!
return reward, end, self.score
# ================
# End game actions
# ================
# ================
# Render food
# ================
def food_gen(self):
x = random.randint(0, (self.width - CELL) // CELL) * CELL
y = random.randint(0, (self.height - CELL) // CELL) * CELL
self.food = coordinate(x, y)
if self.food in self.snake:
self.food_gen()
# ================
# End rendering food
# ================
# ================
# End game window
# ================
# ======================================================================================================================
# END GAME CODE
# ======================================================================================================================
# ======================================================================================================================
# TRAINER CODE
# ======================================================================================================================
# ================
# Learning code
# ================
"""
We will use quality learning to make our snake model evolve
The save data will be stored in 'data/savedata.pth'
The model will gain reward points based on eating food and gaining rewards
The models with higher rewards get accepted and the ones wih lower rewards gets removed
"""
class QLearning(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.initial = nn.Linear(input_size, hidden_size)
self.final = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = fun.relu(self.initial(x))
x = self.final(x)
return x
def save(self, filename="savedata.pth"):
path_to_data = "./data"
if not os.path.exists(path_to_data):
os.makedirs(path_to_data)
filename = os.path.join(path_to_data, filename)
torch.save(self.state_dict(), filename)
# ================
# End learning code
# ================
# ================
# Training code
# ================
class QTraining:
def __init__(self, trainer, learning_rate, gamma):
self.learning_rate = learning_rate
self.trainer = trainer
self.gamma = gamma
self.optimizer = optim.Adam(trainer.parameters(), lr=self.learning_rate) # Preferred optimizer
self.criterion = nn.MSELoss()
def train_step(self, state, action, reward, new_state, done):
state = torch.tensor(state, dtype=torch.float)
new_state = torch.tensor(new_state, dtype=torch.float)
action = torch.tensor(action, dtype=torch.long)
reward = torch.tensor(reward, dtype=torch.float)
if len(state.shape) == 1:
state = torch.unsqueeze(state, 0)
new_state = torch.unsqueeze(new_state, 0)
action = torch.unsqueeze(action, 0)
reward = torch.unsqueeze(reward, 0)
done = (done,)
prediction = self.trainer(state)
target = prediction.clone()
for index in range(len(done)):
q_new = reward[index]
if not done[index]:
q_new = reward[index] + self.gamma * torch.max(self.trainer(new_state[index]))
target[index][torch.argmax(action).item()] = q_new
self.optimizer.zero_grad()
loss = self.criterion(target, prediction)
loss.backward()
self.optimizer.step()
# ================
# End training code
# ================
# ======================================================================================================================
# END TRAINER CODE
# ======================================================================================================================
# ======================================================================================================================
# DATA PLOTTING CODE
# ======================================================================================================================
plt.ion()
def plot(score, mean):
display.clear_output(wait=True)
display.display(plt.gcf())
window = plt.gcf()
window.canvas.set_window_title("Training data")
plt.clf()
plt.title("Training data")
plt.xlabel("Generation")
plt.ylabel("Score")
plt.plot(score)
plt.plot(mean)
plt.ylim(ymin=0)
plt.text(len(score) - 1, score[-1], str(score[-1]))
plt.text(len(mean) - 1, mean[-1], str(mean[-1]))
plt.show(block=False)
plt.pause(1)
# ======================================================================================================================
# END PLOTTING
# ======================================================================================================================