示例#1
0
def main(n,
         generations_num,
         load_and_play,
         model_filename,
         elite_size,
         debug_mode=False):
    if load_and_play == 0:
        run_generations(n, generations_num, model_filename, elite_size)
    else:
        parent = SnakeNetwork()
        parent.model = load_saved_model(model_filename)
        game = SnakeGame(gui=True)
        x = game.start()
        prev_score = score = 0
        prev_j = 0
        x_array = []
        x1_array = []
        end = 0
        for j in range(400):
            [x, score, end] = game.step(parent.predict_action(x))
            if score > prev_score:
                prev_score = score
                prev_j = j
            if end > 0 or j - 100 > prev_j:
                break
            if debug_mode:
                x_array.append(x)
                x1_array.append(parent.model.predict(x))
        if end == 0: game.end_game()
        if debug_mode:
            print(x_array)
            print(x1_array)
示例#2
0
 def visualise(self):
     game = SnakeGame(gui=True)
     game.start()
     for _ in range(self.goal_steps):
         game_action = self.get_game_action(game)
         done, score, snake, food = game.step(game_action)
         if done:
             break
     game.end_game()
     print('-----')
     print('snake: ' + str(snake))
     print('food: ' + str(food))
     print('score: ' + str(score))
示例#3
0
文件: nn.py 项目: m-tosch/Snake-AI
 def visualise_game(self, model):
     game = SnakeGame(gui=True)
     _, score, snake, food = game.start()
     prev_observation = self.generate_observation(snake, food)
     for _ in range(self.max_steps):
         predictions = []
         for action in range(-1, 2):
             predictions.append(
                 model.predict(
                     self.add_action_to_observation(
                         action, prev_observation).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)
         if done:
             break
         else:
             prev_observation = self.generate_observation(snake, food)
     game.end_game()
     print('snake: ' + str(snake))
     print('food: ' + str(food))
     print('prev_obs: ' + str(prev_observation))
     print('score: ' + str(score))
示例#4
0
def play_game(z, n, snakes_population):
    fitness_array = np.zeros(n)
    best_score = 0
    for i, snake in enumerate(snakes_population):
        game = SnakeGame()
        x = game.start()
        prev_score = score = steps = 0
        prev_j = 0
        for j in range((score + 1) * 100):
            [x, score, end] = game.step(snake.predict_action(x))
            if score > prev_score:
                prev_score = score
                prev_j = j
                steps = 0
            steps += 1
            if end > 0 or j - (score + 1) * 50 > prev_j:
                if end == 0: game.end_game()
                break
        if score > best_score:
            best_score = score
        fitness_array[i] = calculate_fitness(score, steps)
        snake.set_fitness(fitness_array[i])
        # print_progress("Snake game", i, len(snakes_population), start)
    return [fitness_array, best_score]