Esempio n. 1
0
    def action(self, game: GameState):
        if game.game_over():
            return None

        possibleMoves = game.legal_moves()
        boards = game.children()

        win_h = self.win_heuristic(boards)
        if not win_h == -1:
            return possibleMoves[win_h], 1

        evals = self.heuristic.hs(boards)
        evals = self.default_heuristic(boards, evals)

        if self.variance:
            if not game.turn():
                evals = [1 - e for e in evals]
            total = sum(evals)
            evals = [e / total for e in evals]
            choice = random.random()
            s = 0
            for idx, e in enumerate(evals):
                s += e
                if choice <= s:
                    bestIdx = idx
                    break

        else:
            if game.turn():
                bestIdx = np.argmax(evals)
            else:
                bestIdx = np.argmin(evals)

        bestMove = possibleMoves[bestIdx]

        return bestMove, evals[bestIdx]
Esempio n. 2
0
 def _expand(self, node: GameState):
     "Update the `children` dict with the children of `node`"
     if node in self.children:
         return  # already expanded
     self.children[node] = set(node.children())