4

我已经实现了一个带有 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
4

1 回答 1

4

rootAlphaBeta不会更新alpha值。当它可以缩小除第一个之外的所有子节点的范围时,它会使用 (-inf, inf) 的完整范围调用其所有子节点。这将防止修剪一些对最终分数没有影响的分支,并增加节点数。

于 2012-09-24T17:04:33.073 回答