def test_model(self, model): steps_arr = [] scores_arr = [] for _ in range(self.test_games): steps = 0 game_memory = [] game = Game() _, score, snake, apple = game.start() prev_observation = self.generate_observation(snake, apple) 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, apple = game.step(game_action) game_memory.append([prev_observation, action]) if done: print('-----') print(steps) print(snake) print(apple) print(prev_observation) print(predictions) break else: prev_observation = self.generate_observation(snake, apple) 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 initial_population(self): training_data = [] for _ in range(self.initial_games): display = pygame.display.set_mode( (Config['game']['width'], Config['game']['height'])) pygame.display.set_caption(Config['game']['caption']) game = Game(display) _, prev_score, snake, apple = game.start() prev_observation = self.generate_observation(snake, apple) prev_apple_distance = self.get_apple_distance(snake, apple) for _ in range(self.goal_steps): action, game_action = self.generate_action(snake) done, score, snake, apple = game.step(game_action) if done: training_data.append([ self.add_action_to_observation(prev_observation, action), -1 ]) break else: apple_distance = self.get_apple_distance(snake, apple) if score > prev_score or apple_distance < prev_apple_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, apple) prev_apple_distance = apple_distance return training_data
#!/usr/bin/python # -*- encoding: utf-8 -*- __author__ = 'Oliver Banse' # IDEA # Import and Initialization import pygame from classes.Game import Game from classes.Screen import Screen pygame.init() # Display configuration screen = Screen('Hit the Mole!', 800, 600) # Entities game = Game(screen) # Run Game game.start() # if __name__ == '__main__': # game.start()
def build(self): game = Game() game.start() timer = Clock.schedule_interval(game.timer, 1) return game