6

我正在使用 Python 为游戏 2048 编写 AI。它比我预期的要慢得多。我将深度限制设置为 5,但仍然需要几秒钟才能得到答案。起初我以为我所有功能的实现都是垃圾,但我想出了真正的原因。搜索树上的叶子比应该有的多得多。

这是一个典型的结果(我数了树叶、树枝和展开的数量):

111640 leaves, 543296 branches, 120936 expansions
Branching factor: 4.49242574585
Expected max leaves = 4.49242574585^5 = 1829.80385192 leaves

还有一个,很好的衡量标准:

99072 leaves, 488876 branches, 107292 expansions
Branching factor: 4.55650001864
Expected max leaves = 4.55650001864^5 = 1964.06963743 leaves

如您所见,搜索树上的叶子比我使用朴素极小极大时的叶子要多得多。这里发生了什么?我的算法发布在下面:

# Generate constants
import sys
posInfinity = sys.float_info.max
negInfinity = -sys.float_info.max

# Returns the direction of the best move given current state and depth limit
def bestMove(grid, depthLimit):
    global limit
    limit = depthLimit
    moveValues = {}
    # Match each move to its minimax value
    for move in Utils2048.validMoves(grid):
        gridCopy = [row[:] for row in grid]
        Utils2048.slide(gridCopy, move)
        moveValues[move] = minValue(grid, negInfinity, posInfinity, 1)
    # Return move that have maximum value
    return max(moveValues, key = moveValues.get)

# Returns the maximum utility when the player moves
def maxValue(grid, a, b, depth):
    successors = Utils2048.maxSuccessors(grid)
    if len(successors) == 0 or limit < depth:
        return Evaluator.evaluate(grid)
    value = negInfinity
    for successor in successors:
        value = max(value, minValue(successor, a, b, depth + 1))
        if value >= b:
            return value
        a = max(a, value)
    return value
# Returns the minimum utility when the computer moves
def minValue(grid, a, b, depth):
    successors = Utils2048.minSuccessors(grid)
    if len(successors) == 0 or limit < depth:
        return Evaluator.evaluate(grid)
    value = posInfinity
    for successor in successors:
        value = min(value, maxValue(successor, a, b, depth + 1))
        if value <= a:
            return value
        b = min(b, value)
    return value

有人请帮帮我。我多次查看此代码,但无法确定问题所在。

4

1 回答 1

0

显然,您正在比较value( bbeta) 和a(alpha)。您的代码中的比较如下:

def maxValue(grid, a, b, depth):
    .....
    .....
        if value >= b:
            return value
        a = max(a, value)
    return value

def minValue(grid, a, b, depth):
    .....
    .....
        if value <= a:
            return value
        b = min(b, value)
    return value

然而,alpha-beta 剪枝的条件是,只要 alpha 增长超过 beta,即 alpha > beta,我们就不需要遍历搜索树。

因此,它应该是:

def maxValue(grid, a, b, depth):
    ....
    .....
        a = max(a, value)
        if a > b:
            return value

    return value

def minValue(grid, a, b, depth):
    .....
    .....
        b = min(b, value)
        if b < a:
            return value

    return value

这是您缺少的边缘情况,因为a(alpha) 和b(beta) 不一定总是等于value

于 2019-07-13T11:23:07.430 回答