def run(self): if self.mct == None: # if new tree then initialize that it came from the opposite player self.mct = Tree(switch_turns(self.turn)) else: # if the tree exists look for the node if it contains that children = self.mct.get_root().get_children() if len(children) != 0: contained = False node = children[0] for n in children: if n.get_move() == self._cloned_board.get_previous_move(): contained = True node = n if contained: self.mct.set_root(node) else: # otherwise initialize new tree self.mct = Tree(switch_turns(self.turn)) else: # otherwise initialize new tree self.mct = Tree(switch_turns(self.turn)) start_time = current_milli_time() while current_milli_time() - start_time < self.timeout - self.before: self._cloned_board = clone( UltimateTicTacToe(board=self.board, last_turn=self.last_turn)) self.roll_out(self.expansion(self.selection(self.mct.get_root()))) return self.choose_best_next_move()
def roll_out(self, node): # first approach, random sample to see current_simulation_turn = switch_turns(node.get_turn()) while not self._cloned_board.is_game_done(): moves = self._cloned_board.get_free_moves() move = choice(moves) self.play_cloned_board(move, current_simulation_turn) current_simulation_turn = switch_turns(current_simulation_turn) if self._cloned_board.get_winner() == None: self.backpropogate(GameState.DRAW, node) else: self.backpropogate( GameState.LOSE if node.get_turn() == current_simulation_turn else GameState.WIN, node)
def add_child(self, move): child = Node(move=move, parent=self, turn=switch_turns(self._turn)) self._children.append(child) return child