Exemple #1
0
def run(config=None):
    if config == None:
        config = load_config(file_name=root_data_file + 'resnet_6_6_4.model',
                             only_load_param=True)
    try:
        board = Board(width=config.board_width,
                      height=config.board_height,
                      n_in_row=config.n_in_row)
        game = Game(board)

        # --------------- human VS AI ----------------
        best_policy = PolicyValueNet(
            config.board_width,
            config.board_height,
            Network=config.network,
            net_params=config.policy_param
        )  # setup which Network to use based on the net_params

        mcts_player = AlphaZeroPlayer(
            best_policy.predict,
            c_puct=config.c_puct,
            nplays=100,
            add_noise=True)  # set larger nplays for better performance

        # uncomment the following line to play with pure MCTS
        # mcts_player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct)

        # human player, input your move in the format: 2,3
        human = HumanPlayer()

        # set who_first=0 for human first
        game.start_game(human, mcts_player, who_first=1, is_shown=1)

    except KeyboardInterrupt:
        print('\n\rquit')
def contest(directory_name, n_games=20):
    """
    比较directory下各个模型间棋力最强的模型
    :param directory_name:
    :param n_games: 模型间对弈次数
    :return: 最强模型对应的文件名
    """

    filenames = os.listdir(directory_name)

    cur_players = []
    for filename in filenames:
        if filename.endswith("pkl"):
            player = load_current_best_player(
                os.path.join(directory_name, filename))
            player.filename = filename
            cur_players.append(player)

    config = load_config(os.path.join(directory_name, filenames[0]))
    game = config.game
    player1, player2 = None, None
    round = 0
    while len(cur_players) >= 2:
        next_round_players = []
        for player in cur_players:
            if player1 is None:
                player1 = player
            else:
                player2 = player
                win_cnt = defaultdict(int)
                for i in range(n_games):
                    print("evaluate game %d" % i)
                    winner = game.start_game(player1,
                                             player2,
                                             who_first=i % 2,
                                             is_shown=0)
                    win_cnt[winner] += 1
                win_ratio = 1.0 * (win_cnt[1] + 0.5 * win_cnt[-1]) / n_games
                info = player1.filename + " vs " + player2.filename + " : win %d lose %d tie %d round %d" % (
                    win_cnt[1], win_cnt[2], win_cnt[-1], round)
                logger.info(info)
                if win_ratio > 0.5:
                    next_round_players.append(player1)
                elif win_ratio == 0.5:
                    next_round_players.append(player1)
                    next_round_players.append(player2)
                else:
                    next_round_players.append(player2)
                player1, player2 = None, None
        else:
            if player1:
                next_round_players.append(player1)
                player1 = None
        cur_players = next_round_players
        round += 1
    logger.info(cur_players[0].filename + " is final winner!")
    return cur_players[0].filename
Exemple #3
0
def draw_loss(filename=root_data_file + 'epochs-1500-resnet2.pkl'):
    config = load_config(file_name=filename, only_load_param=False)
    print (config.loss_records)

    combined_loss_list = [loss['combined_loss']for loss in config.loss_records]
    policy_loss_list = [loss['policy_loss'] for loss in config.loss_records]
    value_loss_list = [loss['value_loss'] for loss in config.loss_records]
    entropy_list = [loss['entropy'] for loss in config.loss_records]

    plt.plot(combined_loss_list, color='blue', label='combined_loss')
    plt.plot(policy_loss_list, color='red', label='policy_loss')
    plt.plot(value_loss_list, color='green', label='value_loss')
    plt.plot(entropy_list, color='black', label='entropy')
    plt.legend()
    plt.show()
Exemple #4
0
def run(config=None):
    if config == None:
        config = load_config(file_name=root_data_file + 'resnet_6_6_4.model',
                             only_load_param=True)
    try:
        board = Board(width=config.board_width,
                      height=config.board_height,
                      n_in_row=config.n_in_row)

        #--------------------1.set player:alphazero VS human---------------------#
        best_policy = PolicyValueNet(
            config.board_width,
            config.board_height,
            Network=config.network,
            net_params=config.policy_param
        )  # setup which Network to use based on the net_params

        player1 = AlphaZeroPlayer(
            best_policy.predict, c_puct=config.c_puct,
            nplays=1000)  #set larger nplays for better performance

        # uncomment the following line to play with pure MCTS
        #player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct)
        player2 = HumanPlayer()
        # --------------------2.set order---------------------#
        who_first = 0  # 0 means player1 first, otherwise player2 first

        # --------------------3.start game--------------------#
        game = Game(board, is_visualize=True)
        t = threading.Thread(target=game.start_game,
                             args=(player1, player2, who_first))
        t.start()
        game.show()

    except:
        print('\n\rquit')
Exemple #5
0
            config.board_width,
            config.board_height,
            Network=config.network,
            net_params=config.policy_param
        )  # setup which Network to use based on the net_params

        mcts_player = AlphaZeroPlayer(
            best_policy.predict,
            c_puct=config.c_puct,
            nplays=100,
            add_noise=True)  # set larger nplays for better performance

        # uncomment the following line to play with pure MCTS
        # mcts_player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct)

        # human player, input your move in the format: 2,3
        human = HumanPlayer()

        # set who_first=0 for human first
        game.start_game(human, mcts_player, who_first=1, is_shown=1)

    except KeyboardInterrupt:
        print('\n\rquit')


if __name__ == '__main__':
    config = load_config(
        file_name="./data/epochs-1500-opponent-AlphaZero-win-0.70.pkl",
        only_load_param=False)
    run(config)
Exemple #6
0
            config.board_height,
            Network=config.network,
            net_params=config.policy_param
        )  # setup which Network to use based on the net_params

        player1 = AlphaZeroPlayer(
            best_policy.predict, c_puct=config.c_puct,
            nplays=1000)  #set larger nplays for better performance

        # uncomment the following line to play with pure MCTS
        #player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct)
        player2 = HumanPlayer()
        # --------------------2.set order---------------------#
        who_first = 0  # 0 means player1 first, otherwise player2 first

        # --------------------3.start game--------------------#
        game = Game(board, is_visualize=True)
        t = threading.Thread(target=game.start_game,
                             args=(player1, player2, who_first))
        t.start()
        game.show()

    except:
        print('\n\rquit')


if __name__ == '__main__':
    config = load_config(file_name=root_data_file + 'epochs-1100-resnet2.pkl',
                         only_load_param=False)
    run(config)
Exemple #7
0
            config.board_width,
            config.board_height,
            Network=config.network,
            net_params=config.policy_param
        )  # setup which Network to use based on the net_params

        mcts_player = AlphaZeroPlayer(
            best_policy.predict, c_puct=config.c_puct,
            nplays=1000)  #set larger nplays for better performance

        # uncomment the following line to play with pure MCTS
        # mcts_player2 = RolloutPlayer(nplays=1000, c_puct=config.c_puct)

        # human player, input your move in the format: 2,3
        human = HumanPlayer()

        # set who_first=0 for human first
        game.start_game(human, mcts_player, who_first=1, is_shown=1)

    except KeyboardInterrupt:
        print('\n\rquit')


if __name__ == '__main__':
    config = load_config(file_name=tmp_data_file +
                         'epochs-1080-6_6_4_best_resnet.pkl',
                         only_load_param=False)
    #run(config)
    setattr(config, "episode_records", [])
    print(config.episode_records)