Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用和更新alpha-beta修剪算法中的alpha值?_Python_Artificial Intelligence_Alpha Beta Pruning - Fatal编程技术网

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

Python 如何使用和更新alpha-beta修剪算法中的alpha值?,python,artificial-intelligence,alpha-beta-pruning,Python,Artificial Intelligence,Alpha Beta Pruning,我在看帖子和公认的答案,上面写着:“你的rootAlphaBeta不会更新alpha值”。我想知道对代码的必要添加是什么。要使alpha-beta修剪工作,alpha值需要传播到深度优先搜索的顶层。这可以通过初始化一个变量来实现,该变量将alpha存储在潜在移动的循环外部,将调用alphaBeta()的结果存储在其中,然后将其用作alphaBeta()的参数。在代码中,它看起来像: def rootAlphaBeta(self, board, rules, ply, player): "

我在看帖子和公认的答案,上面写着:“你的
rootAlphaBeta
不会更新alpha值”。我想知道对代码的必要添加是什么。

要使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

为了使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

你的代码中没有输入错误吗?看起来你在更新beta值而不是alpha。你是说我在更新beta而不是alpha?因为我正在更新for循环第二行的alpha。我相信所有的beta更新都发生在原始问题中的alphaBeta()函数中(这里我只是编写rootAlphaBeta()函数,因为这就是问题所在)。免责声明:我在5年前写过这篇文章,从那以后就没有使用过多少alpha-beta修剪,所以我完全有可能犯了错误。你的代码中没有拼写错误吗?看起来你在更新beta值而不是alpha。你是说我在更新beta而不是alpha?因为我正在更新for循环第二行的alpha。我相信所有的beta更新都发生在原始问题中的alphaBeta()函数中(这里我只是编写rootAlphaBeta()函数,因为这就是问题所在)。免责声明:我在5年前写过这篇文章,从那时起就没有使用过多少alpha-beta修剪,所以我完全有可能犯了错误。