def child_finder(self, node, montecarlo): if node.state == 0: node.add_children([Node(1), Node(-1)]) else: for i in range(2): modifier = (100 if i == 1 else 200) * (-1 if node.state < 0 else 1) node.add_child(Node(node.state + modifier))
def add_child_to_parent(self, parent, move): child = Node(deepcopy(parent.state)) child.state.move(move) child.player_number = child.state.whose_turn() parent.add_child(child) return child
def build_montecarlo(self): root_node = Node(deepcopy(self.game)) root_node.player_number = self.game.whose_turn() montecarlo = MonteCarlo(root_node) montecarlo.child_finder = self.montecarlo_child_finder return montecarlo
def build_children(self, node): children = [] for i in range(2): child = Node(node.state or (1 if i == 1 else -1)) child.policy_value = .90 if i == 1 else 0.10 children.append(child) node.update_win_value(0) return children
def test_choice_is_correct(self): montecarlo = MonteCarlo(Node(0)) montecarlo.child_finder = self.child_finder montecarlo.simulate(50) chosen_node = montecarlo.make_choice() self.assertIs(chosen_node.state, 1) exploratory_node = montecarlo.make_exploratory_choice() self.assertTrue(chosen_node != None)
def child_finder(node): for move in node.state.getMoves(): child = Node(deepcopy(node.state)) child.state.move(*move) node.add_child(child)
def init_board(state, currentPlayer): self.board = board.Board(state, currentPlayer) self.root = Node(board.Board(board.getState(), currentPlayer)) santoriniSim.player_number = 2 montecarlo = MonteCarlo(root) montecarlo.simulate(self.num_sims)