How is the alpha value in alpha-beta pruning algorithm used and updated?

我的梦境 提交于 2019-12-24 13:17:08

问题


I was looking at the post Strange behaviour in a function while implementing the alpha-beta pruning algorithm and the accepted answer, where it is stated: "Your rootAlphaBeta doesn't update the alpha value". I was wondering what the necessary addition to the code was.


回答1:


For alpha-beta pruning to work, the alpha value needs to get propagated up to the top level of the depth first search. This can be achieved by initializing a variable to store alpha outside of the loop over the potential moves, storing the result of the call to alphaBeta() in it, and then using it as an argument to alphaBeta(). In code that will look like:

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


来源:https://stackoverflow.com/questions/19963555/how-is-the-alpha-value-in-alpha-beta-pruning-algorithm-used-and-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!