forked from slavadev/snake_nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_2.py
186 lines (164 loc) · 7.67 KB
/
nn_2.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
from snake_game import SnakeGame
from random import randint
import numpy as np
import tflearn
import math
from tflearn.layers.core import input_data, fully_connected
from tflearn.layers.estimator import regression
from statistics import mean
from collections import Counter
class SnakeNN:
def __init__(self, initial_games = 10000, test_games = 1000, goal_steps = 2000, lr = 1e-2, filename = 'snake_nn_2.tflearn'):
self.initial_games = initial_games
self.test_games = test_games
self.goal_steps = goal_steps
self.lr = lr
self.filename = filename
self.vectors_and_keys = [
[[-1, 0], 0],
[[0, 1], 1],
[[1, 0], 2],
[[0, -1], 3]
]
def initial_population(self):
training_data = []
for _ in range(self.initial_games):
game = SnakeGame()
_, prev_score, snake, food = game.start()
prev_observation = self.generate_observation(snake, food)
prev_food_distance = self.get_food_distance(snake, food)
for _ in range(self.goal_steps):
action, game_action = self.generate_action(snake)
done, score, snake, food = game.step(game_action)
if done:
training_data.append([self.add_action_to_observation(prev_observation, action), -1])
break
else:
food_distance = self.get_food_distance(snake, food)
if score > prev_score or food_distance < prev_food_distance:
training_data.append([self.add_action_to_observation(prev_observation, action), 1])
else:
training_data.append([self.add_action_to_observation(prev_observation, action), 0])
prev_observation = self.generate_observation(snake, food)
prev_food_distance = food_distance
return training_data
def generate_action(self, snake):
action = randint(0,2) - 1
return action, self.get_game_action(snake, action)
def get_game_action(self, snake, action):
snake_direction = self.get_snake_direction_vector(snake)
new_direction = snake_direction
if action == -1:
new_direction = self.turn_vector_to_the_left(snake_direction)
elif action == 1:
new_direction = self.turn_vector_to_the_right(snake_direction)
for pair in self.vectors_and_keys:
if pair[0] == new_direction.tolist():
game_action = pair[1]
return game_action
def generate_observation(self, snake, food):
snake_direction = self.get_snake_direction_vector(snake)
food_direction = self.get_food_direction_vector(snake, food)
barrier_left = self.is_direction_blocked(snake, self.turn_vector_to_the_left(snake_direction))
barrier_front = self.is_direction_blocked(snake, snake_direction)
barrier_right = self.is_direction_blocked(snake, self.turn_vector_to_the_right(snake_direction))
angle = self.get_angle(snake_direction, food_direction)
return np.array([int(barrier_left), int(barrier_front), int(barrier_right), angle])
def add_action_to_observation(self, observation, action):
return np.append([action], observation)
def get_snake_direction_vector(self, snake):
return np.array(snake[0]) - np.array(snake[1])
def get_food_direction_vector(self, snake, food):
return np.array(food) - np.array(snake[0])
def normalize_vector(self, vector):
return vector / np.linalg.norm(vector)
def get_food_distance(self, snake, food):
return np.linalg.norm(self.get_food_direction_vector(snake, food))
def is_direction_blocked(self, snake, direction):
point = np.array(snake[0]) + np.array(direction)
return point.tolist() in snake[:-1] or point[0] == 0 or point[1] == 0 or point[0] == 21 or point[1] == 21
def turn_vector_to_the_left(self, vector):
return np.array([-vector[1], vector[0]])
def turn_vector_to_the_right(self, vector):
return np.array([vector[1], -vector[0]])
def get_angle(self, a, b):
a = self.normalize_vector(a)
b = self.normalize_vector(b)
return math.atan2(a[0] * b[1] - a[1] * b[0], a[0] * b[0] + a[1] * b[1]) / math.pi
def model(self):
network = input_data(shape=[None, 5, 1], name='input')
network = fully_connected(network, 25, activation='relu')
network = fully_connected(network, 1, activation='linear')
network = regression(network, optimizer='adam', learning_rate=self.lr, loss='mean_square', name='target')
model = tflearn.DNN(network, tensorboard_dir='log')
return model
def train_model(self, training_data, model):
X = np.array([i[0] for i in training_data]).reshape(-1, 5, 1)
y = np.array([i[1] for i in training_data]).reshape(-1, 1)
model.fit(X,y, n_epoch = 3, shuffle = True, run_id = self.filename)
model.save(self.filename)
return model
def test_model(self, model):
steps_arr = []
scores_arr = []
for _ in range(self.test_games):
steps = 0
game_memory = []
game = SnakeGame()
_, score, snake, food = game.start()
prev_observation = self.generate_observation(snake, food)
for _ in range(self.goal_steps):
predictions = []
for action in range(-1, 2):
predictions.append(model.predict(self.add_action_to_observation(prev_observation, action).reshape(-1, 5, 1)))
action = np.argmax(np.array(predictions))
game_action = self.get_game_action(snake, action - 1)
done, score, snake, food = game.step(game_action)
game_memory.append([prev_observation, action])
if done:
print('-----')
print(steps)
print(snake)
print(food)
print(prev_observation)
print(predictions)
break
else:
prev_observation = self.generate_observation(snake, food)
steps += 1
steps_arr.append(steps)
scores_arr.append(score)
print('Average steps:',mean(steps_arr))
print(Counter(steps_arr))
print('Average score:',mean(scores_arr))
print(Counter(scores_arr))
def visualise_game(self, model):
game = SnakeGame(gui = True)
_, _, snake, food = game.start()
prev_observation = self.generate_observation(snake, food)
for _ in range(self.goal_steps):
precictions = []
for action in range(-1, 2):
precictions.append(model.predict(self.add_action_to_observation(prev_observation, action).reshape(-1, 5, 1)))
action = np.argmax(np.array(precictions))
game_action = self.get_game_action(snake, action - 1)
done, _, snake, food = game.step(game_action)
if done:
break
else:
prev_observation = self.generate_observation(snake, food)
def train(self):
training_data = self.initial_population()
nn_model = self.model()
nn_model = self.train_model(training_data, nn_model)
self.test_model(nn_model)
def visualise(self):
nn_model = self.model()
nn_model.load(self.filename)
self.visualise_game(nn_model)
def test(self):
nn_model = self.model()
nn_model.load(self.filename)
self.test_model(nn_model)
if __name__ == "__main__":
SnakeNN().train()