如何使用和更新alpha-beta修剪算法中的alpha值?

Fra*_*pps 1 python artificial-intelligence alpha-beta-pruning

在实现alpha-beta修剪算法和接受的答案时,我正在查看函数中的Strange行为,其中声明:"您rootAlphaBeta不更新alpha值".我想知道代码的必要添加内容是什么.

sea*_*erd 6

要使alpha-beta修剪起作用,alpha值需要传播到深度优先搜索的顶层.这可以通过初始化变量以在循环外存储alpha以进行潜在移动,将调用结果存储alphaBeta()在其中,然后将其用作参数来实现alphaBeta().在代码中看起来像:

def rootAlphaBeta(self, board, rules, ply, player):
    """ Makes a call to the alphaBeta function. Returns the optimal move for a player at given ply. """
    best_move = None
    max_eval = float('-infinity')

    move_list = board.generateMoves(rules, player)
    alpha = float('infinity')
    for move in move_list:
        board.makeMove(move, player)
        alpha = -self.alphaBeta(board, rules, float('-infinity'), alpha, ply - 1, board.getOtherPlayer(player))
        board.unmakeMove(move, player)

        if alpha > max_eval:
            max_eval = alpha
            best_move = move

    return best_move
Run Code Online (Sandbox Code Playgroud)