问题
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