Esempio n. 1
0
def main():
    parser = argparse.ArgumentParser(
        description=
        "Enter the size of board and number of games you want to simulate")
    parser.add_argument('board_config', default=8, type=accept_board_config)
    parser.add_argument('num_of_games', default=100, type=check_num_of_games)
    args = parser.parse_args()
    board_config = args.board_config
    num_of_games = args.num_of_games
    num_of_pawns = 0
    print(
        f"Board config selected:{board_config}\nNumber of games to be played: {num_of_games}"
    )
    while True:
        if (board_config == 8):
            print("Select Number of Pawns for the board")
            numOfPawns = int(input("12,\t9,\t6\n"))
            if numOfPawns == 6 or numOfPawns == 9 or numOfPawns == 12:
                print("num_of_pawns")
                num_of_pawns = numOfPawns
                break
        else:
            if board_config == 6:
                num_of_pawns = 6
            elif board_config == 10:
                num_of_pawns = 20
            break
    state = Board(board_config, num_of_pawns)
    node = Node(state, depth=0)
    moves = -1
    nodes_processed = 0
    games = 0
    moves_list = []
    scores = []
    nodes_processed_list_MCTS = []
    while games < num_of_games:
        state = Board(board_config, num_of_pawns)
        obstacles = state.set_obstacles(3)
        print(f"Obstacles added at {obstacles}")
        node = Node(state, depth=0)
        games += 1
        moves = -1
        bot = Bot()
        bot2 = Bot()
        while not state.check_game_status():
            moves += 1
            print(f"Game #: {games}/{num_of_games}\nMove #: {moves}")
            if moves % 2 == 0:
                print(node.state)
                print(
                    f"Moves since last capture: {state.moves_since_last_capture}"
                )
                print("AI's turn")
                nodes_processed = bot.tree_node_processed
                res = bot.mcts(node)
                if res is not None:
                    index, parent_state = res
                    node = parent_state.children()[index]
                nodes_processed_this_turn = bot.tree_node_processed - nodes_processed
                print(f"nodes_processed_this_turn {nodes_processed_this_turn}")
                if node is None:
                    break
            else:
                print(node.state)
                print(
                    f"Moves since last capture: {node.state.moves_since_last_capture}"
                )
                print("Baseline AI turn")
                nodes_processed = bot2.tree_node_processed
                node = bot2.base_line_AI(node)
                if node is None:
                    break
            state = node.state
        print(f"Total moves: {moves}")
        score = state.compute_score()
        if (len(state.p1_pawns) > len(state.p2_pawns)):
            print("MCTS AI Won")
            print(f"Score = {score}")
        elif len(state.p1_pawns) < len(state.p2_pawns):
            print("BASELINE AI Won")
            print(f"Score = {score * -1}")
        else:
            print("It's a draw")
            print(f"Score = {score}")
        print(
            f"total nodes processed = {bot.tree_node_processed + bot2.tree_node_processed}"
        )
        moves_list.append(moves)
        scores.append(score)
        nodes_processed_list_MCTS.append(bot.tree_node_processed)
    from pathlib import Path
    # TODO: `~` not working in Mac, Analyse and fix
    path = Path('~/../plots/')
    with open(
            path /
            f"Simulated_{board_config}x{board_config}_{num_of_games}_{num_of_pawns}.txt",
            'w') as f:
        f.write(
            f"Moves List: {moves_list}\nScores List: {scores}\nNodes Processed List MCTS: {nodes_processed_list_MCTS}"
        )
    print(moves_list)
    print(scores)
    print(nodes_processed_list_MCTS)
    generatePlots(
        nodes_processed_list_MCTS, "Range of Nodes processed",
        "Number of games", "Nodes processed for MCTS", path /
        f"NodesprocessedMCTS_{board_config}_{num_of_games}_{num_of_pawns}")