我已经实现了一个带有 alpha-beta 修剪的 minimax 算法。为了得到最好的移动,我用rootAlphaBeta
函数调用了 alpha-beta 算法。但是,在rootAlphaBeta
函数中,我发现了一些非常奇怪的行为。当我rootAlphaBeta
使用 aply
为 4 调用该函数时,它会进行大约 20 000 次调用,但是当我alphaBeta
直接调用该函数时,它只会进行大约 2000 次调用。我似乎找不到问题所在,因为呼叫次数应该相同。
两种算法最终找到的移动应该是相同的,对吧?我想是的,至少这招的分数是一样的,alphaBeta
当我没有直接叫它时,我无法知道选择的招式rootAlphaBeta
。
def alphaBeta(self, board, rules, alpha, beta, ply, player):
"""Implements a minimax algorithm with alpha-beta pruning."""
if ply == 0:
return self.positionEvaluation(board, rules, player)
move_list = board.generateMoves(rules, player)
for move in move_list:
board.makeMove(move, player)
current_eval = -self.alphaBeta(board, rules, -beta, -alpha, ply - 1,
board.getOtherPlayer(player))
board.unmakeMove(move, player)
if current_eval >= beta:
return beta
if current_eval > alpha:
alpha = current_eval
return alpha
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)
for move in move_list:
board.makeMove(move, player)
current_eval = -self.alphaBeta(board, rules, float('-infinity'),
float('infinity'), ply - 1,
board.getOtherPlayer(player))
board.unmakeMove(move, player)
if current_eval > max_eval:
max_eval = current_eval
best_move = move
return best_move