예제 #1
0
    def run_against_random(self, num_games=25, game_num=0):
        wrong_moves = 0
        total_moves = 0
        games_won = 0
        for _ in range(num_games):
            game = Hex(5, 1)
            while len(game.get_moves()) > 0:
                if game.player == Hex.PLAYER_TOP:  # model player
                    next_move = ANET.predict(get_feature(
                        game.get_state(), game.player == Hex.PLAYER_LEFT),
                                             model=self.model)
                    next_move = (
                        next_move[1], next_move[0]
                    ) if game.player == Hex.PLAYER_LEFT else next_move
                    if next_move not in game.get_moves():
                        wrong_moves += 1
                        next_move = choice(game.get_moves())
                else:
                    next_move = choice(game.get_moves())
                game.do_move(next_move)
                total_moves += 1

            res_model = game.get_result(Hex.PLAYER_TOP)
            res_random = game.get_result(Hex.PLAYER_LEFT)
            if res_model == 0 and res_random == 0:
                print("Draw")
                continue
            games_won += res_model

        win_rate = games_won / num_games
        wrong_moves_rate = wrong_moves / total_moves
        pp.pprint("Game {}    : {}".format(game_num, win_rate))
        pp.pprint("Wrong moves: {}".format(wrong_moves_rate))
        return win_rate, wrong_moves_rate
예제 #2
0
    def run_tournament(self, path="topp"):
        self.fetch_game_models(path)
        games = self.get_games()
        wrong_moves = 0
        total_moves = 0
        for p1, p2 in games:
            for i in range(self.num_games):
                game = Hex(5, 1)
                while len(game.get_moves()) > 0:
                    if self.random and game.player == Hex.PLAYER_LEFT:
                        next_move = choice(game.get_moves())
                    else:
                        model = self.models[p1 if game.player ==
                                            Hex.PLAYER_TOP else p2]
                        next_move = ANET.predict(get_feature(
                            game.get_state(), game.player == Hex.PLAYER_LEFT),
                                                 model=model)
                        next_move = (
                            next_move[1], next_move[0]
                        ) if game.player == Hex.PLAYER_LEFT else next_move
                        if next_move not in game.get_moves():
                            wrong_moves += 1
                            next_move = choice(game.get_moves())
                    total_moves += 1
                    game.do_move(next_move)
                res_p1 = game.get_result(Hex.PLAYER_TOP)
                res_p2 = game.get_result(Hex.PLAYER_LEFT)
                if res_p1 == 0 and res_p2 == 0:
                    raise Exception
                if res_p1 == 1 and res_p2 == 1:
                    raise Exception

                winning_model = p1 if res_p1 == 1 else p2
                loosing_model = p2 if res_p2 == 0 else p1
                if self.results.get(
                    (winning_model, loosing_model)) is not None:
                    self.results[(winning_model, loosing_model)] += 1
                else:
                    self.results[(winning_model, loosing_model)] = 1
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(self.results)
        if total_moves > 0:
            pp.pprint("Wrong moves (%): " + str(wrong_moves / total_moves))
예제 #3
0
def main():
    # n, num_games, verbose, starting_player, max_rollouts = setup_game()
    n, num_games, verbose, starting_player, max_rollouts = 5, 200, False, 1, 0.5
    results = []
    game_num = 1
    viewer = None

    run_tournament = True
    with_training = True
    num_games_tournament = 25
    if run_tournament:
        save_path = "short_topp"
    else:
        save_path = "long_topp"

    ##### CONFIG #####

    buffer_size = 40
    train_interval = 40
    saving_interval = 10
    moves_done = 0
    epochs = 300

    ##################

    buffer = ReplayBuffer(vfrac=0.1, tfrac=0.1, size=buffer_size)
    anet = init_anet(n, buffer)

    if with_training:
        anet.save_to_file(save_path + "/model_step_{0}.h5".format(0))
    game = Hex(n, starting_player)
    ROOT_NODE = Node(game=game)
    while with_training and num_games >= game_num:
        game = Hex(n, starting_player)
        next_root = ROOT_NODE
        # viewer = Board(game)
        print("Game number {}".format(game_num))
        while game.get_moves():
            mc = MonteCarlo(game, max_rollouts, next_root)
            mc.run(lambda _input: ANET.predict(_input, model=anet.model))
            case = mc.get_training_case()
            buffer.push(case)
            next_root = mc.get_best_move()
            game.do_move(next_root.move)
            moves_done += 1

            if viewer:
                viewer.do_move(next_root.move, game.player)
            if moves_done % train_interval == 0:
                buffer.update()
                anet.train_model(epochs)
                anet.run_against_random(num_games=50, game_num=game_num)
        if saving_interval > 0 and game_num % saving_interval == 0:
            anet.save_to_file(save_path +
                              "/model_step_{0}.h5".format(game_num))
            buffer.size += 20
            # train_interval += 5
            # anet.optimizer.lr /= 2
        if game.get_result(game.player) == 1:
            results.append(game.player)
        game_num += 1

    if viewer:
        viewer.persist()

    if run_tournament:
        tournament = Tournament(num_games_tournament)
        tournament.run_tournament(save_path)

    else:
        anet.save_to_file("best_topp/model_2.h5")