Ejemplo n.º 1
0
def think(state):
    """ Performs MCTS by sampling games and calling the appropriate functions to construct the game tree.

    Args:
        state:  The state of the game.

    Returns:    The action to be taken.

    """
    #'''
    start = time()
    time_elapsed = 0
    #'''

    def get_result (sampled_game):
        reds, blues = sampled_game.score.get('red', 0), sampled_game.score.get('blue', 0)
        result = reds - blues if identity_of_bot == 'red' else blues - reds
        return result

    identity_of_bot = state.player_turn
    root_node = MCTSNode(parent=None, parent_action=None, action_list=state.legal_moves)

    while time_elapsed < 10:
    #for step in range(num_nodes):

        # Copy the game for sampling a playthrough
        sampled_game = state.copy()

        # Start at root
        node = root_node

        # Do MCTS - This is all you!
        node = traverse_nodes(node, sampled_game, identity_of_bot)
        node = expand_leaf(node, sampled_game)
        rollout(sampled_game)
        backpropagate(node, get_result(sampled_game))
        time_elapsed = time() - start

    # Make choice based on tree
    choice = make_choice(root_node, state, identity_of_bot)
    action = choice.parent_action

    # Write tree to file for time testing (Extra Credit Assignment)
    file = open('mcts_modified.out', 'a')
    file.write(root_node.tree_to_string(horizon=100, indent=1))
    return action

    # Return an action, typically the most frequently used action (from the root) or the action with the best
    # estimated win rate.
Ejemplo n.º 2
0
def think(state):
    """ Performs MCTS by sampling games and calling the appropriate functions to construct the game tree.

    Args:
        state:  The state of the game.

    Returns:    The action to be taken.

    """
    start = time()
    time_elapsed = 0
    def get_result (sampled_game):
        if sampled_game.winner == identity_of_bot: return 1
        if sampled_game.winner == 'tie': return 0.5
        if sampled_game.winner != identity_of_bot: return -1
        else: return -1

    identity_of_bot = state.player_turn
    root_node = MCTSNode(parent=None, parent_action=None, action_list=state.legal_moves)

    while time_elapsed < 10:
    #for step in range(num_nodes):
        # Copy the game for sampling a playthrough
        sampled_game = state.copy()

        # Start at root
        node = root_node

        # Do MCTS - This is all you!
        node = traverse_nodes(node, sampled_game, identity_of_bot)
        node = expand_leaf(node, sampled_game)
        rollout(sampled_game)
        backpropagate(node, get_result(sampled_game))
        time_elapsed = time() - start

    # Make choice based on tree
    choice = make_choice(root_node, state, identity_of_bot)
    action = choice.parent_action

    # Write tree to file for time testing (Extra Credit Assignment)
    file = open('mcts_vanilla.out', 'a')
    file.write(root_node.tree_to_string(horizon=100, indent=1))

    return action