Example #1
0
def train(display_on, speed, params):
    pygame.init()
    pygame.font.init()

    agent = DQNAgent(params)

    counter_games = 0
    high_score = 0
    score_plot = []
    counter_plot = []

    while counter_games < params['episodes']:
        game = Game(440, 440, high_score)

        if display_on:
            game.update_display()

        while not game.crash:
            if handle_game_event(game):
                return

            # agent.epsilon is set to give randomness to actions
            agent.epsilon = 1 - (counter_games *
                                 params['epsilon_decay_linear'])

            state = game.get_state()
            move = agent.get_move(state)
            game.do_move(move)

            new_state = game.get_state()
            reward = get_reward(game)

            # train short memory base on the new action and state
            agent.train_short_memory(state, move, reward, new_state,
                                     game.crash)

            agent.remember(state, move, reward, new_state, game.crash)

            if display_on:
                game.update_display()
                pygame.time.wait(speed)

        counter_games += 1
        print(f'Game {counter_games}      Score: {game.score}')
        high_score = game.high_score

        score_plot.append(game.score)
        counter_plot.append(counter_games)

        agent.replay_memory(params['batch_size'])

    agent.model.save_weights(params['weights_path'])
    pygame.quit()
    plot_seaborn(counter_plot, score_plot)
Example #2
0
def play(display_on, speed, params):
    pygame.init()
    pygame.font.init()

    agent = DQNAgent(params)
    agent.epsilon = 0

    counter_games = 0
    high_score = 0
    score_plot = []
    counter_plot = []

    while counter_games < params['episodes']:
        game = Game(440, 440, high_score)

        if display_on:
            game.update_display()

        while not game.crash:
            if handle_game_event(game):
                return

            state = game.get_state()
            move = agent.get_move(state)

            game.do_move(move)

            if display_on:
                game.update_display()
                pygame.time.wait(speed)

        counter_games += 1
        print(f'Game {counter_games}      Score: {game.score}')
        high_score = game.high_score

        score_plot.append(game.score)
        counter_plot.append(counter_games)

    pygame.quit()
    plot_seaborn(counter_plot, score_plot)