Beispiel #1
0
def test_root_wins_losses_propagation():
    tree = GameTree()
    tree.run(10)
    wins = sum(tree.root.children.wins)
    losses = sum(tree.root.children.losses)
    assert wins == tree.root.wins or wins + 1 == tree.root.wins
    assert losses == tree.root.losses or losses + 1 == tree.root.losses
Beispiel #2
0
class MCTSPlayer(Player):
    DEFAULT_NAME = 'MCTS'
    DEFAULT_ITERATIONS = 100

    def __init__(self, name: str = None, iterations: int = DEFAULT_ITERATIONS):
        super(MCTSPlayer, self).__init__(name)
        self.tree = None
        self.iterations = iterations

    def get_move(self, game: Game):
        if not self.tree:
            self.tree = GameTree(game_state=game)
        best_move = self.tree.run(self.iterations)
        return best_move

    def update_state(self, move):
        if self.tree:
            self.tree.play(move)

    @property
    def stats(self):
        return {
            'tree_height': self.tree.height,
            'tree_exploration': self.tree.exploration_rate,
            'tree_nodes': self.tree.nodes
        }

    def clone(self):
        return MCTSPlayer(self.name, self.iterations)
Beispiel #3
0
def test_node_wins_propagation():
    tree = GameTree()
    tree.run()

    assert tree.root.wins == tree.root.children[0].wins