def test_simulate(): tree = MonteCarlo(player) tree.make_node(initial_state, player) key = hash(initial_state.tostring()) + hash(player) root = tree.nodes[key] for _ in range(1000): outcome = tree.simulate(root) assert isinstance(outcome, BoardPiece) or isinstance( outcome, GameState)
def test_backpropagate(): tree = MonteCarlo(player) tree.make_node(initial_state, player) key = hash(initial_state.tostring()) + hash(player) root = tree.nodes[key] n_test_sims = 100 for m in root.legal_moves: wins = 0 child = tree.expand(root) for i in range(n_test_sims): outcome = tree.simulate(child) tree.backpropagate(child, outcome) if outcome == player: wins += 1 assert child.n_wins == wins assert child.n_plays == n_test_sims assert root.n_plays == sum([c.n_plays for c in root.children.values()]) assert root.n_wins == sum([c.n_wins for c in root.children.values()])