|
| 1 | +from snake_game import SnakeGame |
| 2 | +from random import randint |
| 3 | +import numpy as np |
| 4 | +import tflearn |
| 5 | +import math |
| 6 | +from tflearn.layers.core import input_data, fully_connected |
| 7 | +from tflearn.layers.estimator import regression |
| 8 | +from statistics import mean |
| 9 | +from collections import Counter |
| 10 | + |
| 11 | +class SnakeNN: |
| 12 | + def __init__(self, initial_games = 10000, test_games = 1000, goal_steps = 2000, lr = 1e-2, filename = 'snake_nn_2.tflearn'): |
| 13 | + self.initial_games = initial_games |
| 14 | + self.test_games = test_games |
| 15 | + self.goal_steps = goal_steps |
| 16 | + self.lr = lr |
| 17 | + self.filename = filename |
| 18 | + self.vectors_and_keys = [ |
| 19 | + [[-1, 0], 0], |
| 20 | + [[0, 1], 1], |
| 21 | + [[1, 0], 2], |
| 22 | + [[0, -1], 3] |
| 23 | + ] |
| 24 | + |
| 25 | + def initial_population(self): |
| 26 | + training_data = [] |
| 27 | + for _ in range(self.initial_games): |
| 28 | + game = SnakeGame() |
| 29 | + _, prev_score, snake, food = game.start() |
| 30 | + prev_observation = self.generate_observation(snake, food) |
| 31 | + prev_food_distance = self.get_food_distance(snake, food) |
| 32 | + for _ in range(self.goal_steps): |
| 33 | + action, game_action = self.generate_action(snake) |
| 34 | + done, score, snake, food = game.step(game_action) |
| 35 | + if done: |
| 36 | + training_data.append([self.add_action_to_observation(prev_observation, action), -1]) |
| 37 | + break |
| 38 | + else: |
| 39 | + food_distance = self.get_food_distance(snake, food) |
| 40 | + if score > prev_score or food_distance < prev_food_distance: |
| 41 | + training_data.append([self.add_action_to_observation(prev_observation, action), 1]) |
| 42 | + else: |
| 43 | + training_data.append([self.add_action_to_observation(prev_observation, action), 0]) |
| 44 | + prev_observation = self.generate_observation(snake, food) |
| 45 | + prev_food_distance = food_distance |
| 46 | + return training_data |
| 47 | + |
| 48 | + def generate_action(self, snake): |
| 49 | + action = randint(0,2) - 1 |
| 50 | + return action, self.get_game_action(snake, action) |
| 51 | + |
| 52 | + def get_game_action(self, snake, action): |
| 53 | + snake_direction = self.get_snake_direction_vector(snake) |
| 54 | + new_direction = snake_direction |
| 55 | + if action == -1: |
| 56 | + new_direction = self.turn_vector_to_the_left(snake_direction) |
| 57 | + elif action == 1: |
| 58 | + new_direction = self.turn_vector_to_the_right(snake_direction) |
| 59 | + for pair in self.vectors_and_keys: |
| 60 | + if pair[0] == new_direction.tolist(): |
| 61 | + game_action = pair[1] |
| 62 | + return game_action |
| 63 | + |
| 64 | + def generate_observation(self, snake, food): |
| 65 | + snake_direction = self.get_snake_direction_vector(snake) |
| 66 | + food_direction = self.get_food_direction_vector(snake, food) |
| 67 | + barrier_left = self.is_direction_blocked(snake, self.turn_vector_to_the_left(snake_direction)) |
| 68 | + barrier_front = self.is_direction_blocked(snake, snake_direction) |
| 69 | + barrier_right = self.is_direction_blocked(snake, self.turn_vector_to_the_right(snake_direction)) |
| 70 | + angle = self.get_angle(snake_direction, food_direction) |
| 71 | + return np.array([int(barrier_left), int(barrier_front), int(barrier_right), angle]) |
| 72 | + |
| 73 | + def add_action_to_observation(self, observation, action): |
| 74 | + return np.append([action], observation) |
| 75 | + |
| 76 | + def get_snake_direction_vector(self, snake): |
| 77 | + return np.array(snake[0]) - np.array(snake[1]) |
| 78 | + |
| 79 | + def get_food_direction_vector(self, snake, food): |
| 80 | + return np.array(food) - np.array(snake[0]) |
| 81 | + |
| 82 | + def normalize_vector(self, vector): |
| 83 | + return vector / np.linalg.norm(vector) |
| 84 | + |
| 85 | + def get_food_distance(self, snake, food): |
| 86 | + return np.linalg.norm(self.get_food_direction_vector(snake, food)) |
| 87 | + |
| 88 | + def is_direction_blocked(self, snake, direction): |
| 89 | + point = np.array(snake[0]) + np.array(direction) |
| 90 | + return point.tolist() in snake[:-1] or point[0] == 0 or point[1] == 0 or point[0] == 21 or point[1] == 21 |
| 91 | + |
| 92 | + def turn_vector_to_the_left(self, vector): |
| 93 | + return np.array([-vector[1], vector[0]]) |
| 94 | + |
| 95 | + def turn_vector_to_the_right(self, vector): |
| 96 | + return np.array([vector[1], -vector[0]]) |
| 97 | + |
| 98 | + def get_angle(self, a, b): |
| 99 | + a = self.normalize_vector(a) |
| 100 | + b = self.normalize_vector(b) |
| 101 | + return math.atan2(a[0] * b[1] - a[1] * b[0], a[0] * b[0] + a[1] * b[1]) / math.pi |
| 102 | + |
| 103 | + def model(self): |
| 104 | + network = input_data(shape=[None, 5, 1], name='input') |
| 105 | + network = fully_connected(network, 25, activation='relu') |
| 106 | + network = fully_connected(network, 1, activation='linear') |
| 107 | + network = regression(network, optimizer='adam', learning_rate=self.lr, loss='mean_square', name='target') |
| 108 | + model = tflearn.DNN(network, tensorboard_dir='log') |
| 109 | + return model |
| 110 | + |
| 111 | + def train_model(self, training_data, model): |
| 112 | + X = np.array([i[0] for i in training_data]).reshape(-1, 5, 1) |
| 113 | + y = np.array([i[1] for i in training_data]).reshape(-1, 1) |
| 114 | + model.fit(X,y, n_epoch = 3, shuffle = True, run_id = self.filename) |
| 115 | + model.save(self.filename) |
| 116 | + return model |
| 117 | + |
| 118 | + def test_model(self, model): |
| 119 | + steps_arr = [] |
| 120 | + scores_arr = [] |
| 121 | + for _ in range(self.test_games): |
| 122 | + steps = 0 |
| 123 | + game_memory = [] |
| 124 | + game = SnakeGame() |
| 125 | + _, score, snake, food = game.start() |
| 126 | + prev_observation = self.generate_observation(snake, food) |
| 127 | + for _ in range(self.goal_steps): |
| 128 | + predictions = [] |
| 129 | + for action in range(-1, 2): |
| 130 | + predictions.append(model.predict(self.add_action_to_observation(prev_observation, action).reshape(-1, 5, 1))) |
| 131 | + action = np.argmax(np.array(predictions)) |
| 132 | + game_action = self.get_game_action(snake, action - 1) |
| 133 | + done, score, snake, food = game.step(game_action) |
| 134 | + game_memory.append([prev_observation, action]) |
| 135 | + if done: |
| 136 | + print('-----') |
| 137 | + print(steps) |
| 138 | + print(snake) |
| 139 | + print(food) |
| 140 | + print(prev_observation) |
| 141 | + print(predictions) |
| 142 | + break |
| 143 | + else: |
| 144 | + prev_observation = self.generate_observation(snake, food) |
| 145 | + steps += 1 |
| 146 | + steps_arr.append(steps) |
| 147 | + scores_arr.append(score) |
| 148 | + print('Average steps:',mean(steps_arr)) |
| 149 | + print(Counter(steps_arr)) |
| 150 | + print('Average score:',mean(scores_arr)) |
| 151 | + print(Counter(scores_arr)) |
| 152 | + |
| 153 | + def visualise_game(self, model): |
| 154 | + game = SnakeGame(gui = True) |
| 155 | + _, _, snake, food = game.start() |
| 156 | + prev_observation = self.generate_observation(snake, food) |
| 157 | + for _ in range(self.goal_steps): |
| 158 | + precictions = [] |
| 159 | + for action in range(-1, 2): |
| 160 | + precictions.append(model.predict(self.add_action_to_observation(prev_observation, action).reshape(-1, 5, 1))) |
| 161 | + action = np.argmax(np.array(precictions)) |
| 162 | + game_action = self.get_game_action(snake, action - 1) |
| 163 | + done, _, snake, food = game.step(game_action) |
| 164 | + if done: |
| 165 | + break |
| 166 | + else: |
| 167 | + prev_observation = self.generate_observation(snake, food) |
| 168 | + |
| 169 | + def train(self): |
| 170 | + training_data = self.initial_population() |
| 171 | + nn_model = self.model() |
| 172 | + nn_model = self.train_model(training_data, nn_model) |
| 173 | + self.test_model(nn_model) |
| 174 | + |
| 175 | + def visualise(self): |
| 176 | + nn_model = self.model() |
| 177 | + nn_model.load(self.filename) |
| 178 | + self.visualise_game(nn_model) |
| 179 | + |
| 180 | + def test(self): |
| 181 | + nn_model = self.model() |
| 182 | + nn_model.load(self.filename) |
| 183 | + self.test_model(nn_model) |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + SnakeNN().train() |
0 commit comments