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
def think(board, state): """ Performs MCTS by sampling games and calling the appropriate functions to construct the game tree. Args: board: The game setup. state: The state of the game. Returns: The action to be taken. """ identity_of_bot = board.current_player(state) # start at root root_node = MCTSNode(parent=None, parent_action=None) node = root_node root_node.untried_actions = fun_board.legal_actions(state) for step in range(num_nodes): sampled_game = state # Start at root node = root_node node.state = sampled_game node = traverse_nodes(node, sampled_game, identity_of_bot) leaf_node = expand_leaf(node, sampled_game) sampled_game = rollout(leaf_node.state) won = board.win_values(sampled_game) if won is None: won = False elif won[identity_of_bot] == 1: won = True else: won = False backpropagate(leaf_node, won) best_action = None best_ratio = 0 for action in root_node.child_nodes.keys(): child_node = root_node.child_nodes[action] ratio = child_node.wins / child_node.visits if ratio >= best_ratio: best_ratio = ratio best_action = action # global root_node # root_node = root_node.child_nodes[best_action] if best_action is None: print(node) print(best_action) return best_action
def expand_leaf(node, state): """ 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. """ try: action = node.untried_actions.pop() except IndexError: return node new_node = MCTSNode(parent=node, parent_action=action) new_node.state = fun_board.next_state(node.state, action) new_node.untried_actions = fun_board.legal_actions(new_node.state) node.child_nodes[action] = new_node return new_node