예제 #1
0
def RunAgent(game_count=50,
             visible=True,
             waitForEyes=False,
             gamma=0.4,
             layer3=False,
             dropout=False,
             dropoutrate=0.5,
             path="model.h5",
             nepochs=2,
             eps=1):
    agent = Agent(gamma=gamma,
                  ifDropout=dropout,
                  dropoutrate=dropoutrate,
                  path_saved_weights=path,
                  layer3=layer3,
                  nepochs=nepochs,
                  epsilon=eps)

    with open('SnakeLogs.csv', 'w', newline='') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=' ',
                                quotechar='|',
                                quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(['Attempt', 'Length', 'iterations_count'])

    main_game = Game(visible, waitForEyes)
    for i in range(game_count):
        main_game.rerun()
        next_state = main_game.send_state()
        reward = 0
        while main_game.running:
            state = next_state
            (next_state,
             reward) = main_game.run(agent.get_action(state, reward))
            if not main_game.running:
                with open('SnakeLogs.csv', 'a', newline='') as csvfile:
                    spamwriter = csv.writer(csvfile)
                    spamwriter.writerow([
                        str(i),
                        str(main_game.get_snake_size()),
                        str(main_game.iterations_count)
                    ])
                agent.get_action(next_state, reward, game_over=True)