Ejemplo n.º 1
0
def expand_leaf(parent_node: MCTSNode, state, board):
    """ Adds a new leaf to the tree by creating a new child node for the given node.

    Args:
        node:   The node for which a child will be added.
        state:  The state of the game.

    Returns:    The added child node.

    """
    # The parent node can execute all the actions in the current state
    current_state = ravel_states(board, state, parent_node)
    parent_node.untried_actions = board.legal_actions(current_state)
    if len(parent_node.untried_actions) == 0:
        print("Cant expand leaf there are no possıble plays proceed.")
        parent_node.parent.wins = -inf
        parent_node.wins = -inf
        return None

    #select a random action that can be executed in that node
    #!!! Make this random. It is kinda random?
    p_action = parent_node.untried_actions.pop()

    #!!! action list might not be correct.
    #create a new node which would be the next state as a result of the chosen action
    new_node = MCTSNode(parent=parent_node,
                        parent_action=p_action,
                        action_list=parent_node.untried_actions)

    parent_node.child_nodes[p_action] = new_node
    return new_node
Ejemplo n.º 2
0
def backpropagate(added_node: MCTSNode, expectation):
    """ Navigates the tree from a leaf node to the root, updating the win and visit count of each node along the path.

    Args:
        node:   A leaf node.
        won:    An indicator of whether the bot won or lost the game.

    """
    added_node.wins = expectation
    while added_node.parent:
        if added_node.parent.wins < expectation:
            added_node.parent.wins = expectation
        added_node = added_node.parent