Created
October 23, 2020 15:16
-
-
Save masouduut94/c11fe2ad951fc9c39905fa75d04a11c4 to your computer and use it in GitHub Desktop.
selection phase of mcts on the game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def select_node(self) -> tuple: | |
""" | |
Select a node in the tree to preform a single simulation from. | |
""" | |
node = self.root | |
state = deepcopy(self.root_state) | |
# stop if we find reach a leaf node | |
while len(node.children) != 0: | |
# descend to the maximum value node, break ties at random | |
children = node.children.values() | |
max_value = max(children, key=lambda n: n.value).value | |
max_nodes = [n for n in node.children.values() | |
if n.value == max_value] | |
node = choice(max_nodes) | |
state.play(node.move) | |
# if some child node has not been explored select it before expanding | |
# other children | |
if node.N == 0: | |
return node, state | |
# if we reach a leaf node generate its children and return one of them | |
# if the node is terminal, just return the terminal node | |
if self.expand(node, state): | |
node = choice(list(node.children.values())) | |
state.play(node.move) | |
return node, state | |
@staticmethod | |
def expand(parent: Node, state: GameState) -> bool: | |
""" | |
Generate the children of the passed "parent" node based on the available | |
moves in the passed gamestate and add them to the tree. | |
Returns: | |
bool: returns false If node is leaf (the game has ended). | |
""" | |
children = [] | |
if state.winner != GameMeta.PLAYERS['none']: | |
# game is over at this node so nothing to expand | |
return False | |
for move in state.moves(): | |
children.append(Node(move, parent)) | |
parent.add_children(children) | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment