我的Minimax算法出了什么问题?

问题描述 投票:0回答:1

我试图用 教程,

AI不行,每次都是在最底层的行上建立,报出列索引为 0, 1, 2, 3, 4, 5, 6 然后是下一行,以此类推。我在代码输出中发现了很多不同的地方,与我的和 教程工作版 是最小化算法的深度,随着它的发展。其中 这个 是工作中的深度,因为它的复发,和。这个 是我的一个深度(比这个长,但我的控制台没有保留全部)。我尝试了很多东西来让它工作,包括把棋盘系统改成一个列表里面有多个列表,还有很多其他的东西,比如删除一些代码的部分,重新修改分数,但似乎没有改变。

我的最小值函数是:。

def minimax(board, depth, alpha, beta, maximizingPlayer):
    print (depth)
    validLocations = getValidLocations(board)
    isTerminal = isTerminalNode(board)
    if depth == 0 or isTerminal:
        if isTerminal:
            if checkWin(board, computerDisc):
                return (None, math.inf)
            elif checkWin(board, playerDisc):
                return (None, -math.inf)
            else: # Game is over, no more spaces
                return (None, 0)
        else: # Depth is zero
            # print ("THIS IS DEPTH 0")
            return (None, scorePos(board, computerDisc))

    if maximizingPlayer:
        value = -math.inf
        column = random.choice(validLocations)
        for c in validLocations:
            boardCopy = copy.deepcopy(board)
            dropPiece(boardCopy, c, computerDisc)
            newScore = minimax(boardCopy, depth-1, alpha, beta, False)[1]
            if newScore > value:
                value = newScore
                column = c
            alpha = max(alpha, value)
            if alpha >= beta:
                break
        return (column, value)

    else: # Minimizing player
        value = math.inf
        column = random.choice(validLocations)
        for c in validLocations:
            boardCopy = copy.deepcopy(board)
            dropPiece(boardCopy, c, playerDisc)
            newScore = minimax(boardCopy, depth-1, alpha, beta, True)[1]
            if newScore < value:
                value = newScore
                column = c
            beta = min(beta, value)
            if alpha >= beta:
                break

        return (column, value)

教程的功能是:

def minimax(board, depth, alpha, beta, maximizingPlayer):
valid_locations = get_valid_locations(board)
is_terminal = is_terminal_node(board)
if depth == 0 or is_terminal:
    if is_terminal:
        if winning_move(board, AI_PIECE):
            return (None, 100000000000000)
        elif winning_move(board, PLAYER_PIECE):
            return (None, -10000000000000)
        else: # Game is over, no more valid moves
            return (None, 0)
    else: # Depth is zero
        return (None, score_position(board, AI_PIECE))
if maximizingPlayer:
    value = -math.inf
    column = random.choice(valid_locations)
    for col in valid_locations:
        row = get_next_open_row(board, col)
        b_copy = board.copy()
        drop_piece(b_copy, row, col, AI_PIECE)
        new_score = minimax(b_copy, depth-1, alpha, beta, False)[1]
        if new_score > value:
            value = new_score
            column = col
        alpha = max(alpha, value)
        if alpha >= beta:
            break
    return column, value

else: # Minimizing player
    value = math.inf
    column = random.choice(valid_locations)
    for col in valid_locations:
        row = get_next_open_row(board, col)
        b_copy = board.copy()
        drop_piece(b_copy, row, col, PLAYER_PIECE)
        new_score = minimax(b_copy, depth-1, alpha, beta, True)[1]
        if new_score < value:
            value = new_score
            column = col
        beta = min(beta, value)
        if alpha >= beta:
            break
    return column, value

当我的功能失效的时候,我试着让它尽可能地接近教程功能,但似乎还是不行。我需要怎么做才行?

完整的程序:我的。https:/repl.it@MyloBishopConnect -4。教程。https:/github.comKeithGalliConnect4-Pythonblobmasterconnect4_with_ai.py.

python artificial-intelligence minimax alpha-beta-pruning
1个回答
1
投票

板子是通过传递给你的minimax函数作为参数。节点但在函数内部,你使用 董事会.

参考程序:def minimax(board, depth, alpha, beta, maximizingPlayer)。

你的程序: def minimax(节点、深度、alpha、beta、最大化播放器)。)

你的 递归 因此,minimax函数没有达到预期的效果。

编辑:为了解决这个问题,将 董事会 在Minimax内部有 节点 (不要改变 节点 函数定义中的 董事会)

编辑2:还可以检查函数 scorePos - 你有一个硬编码的。电脑光盘 而不是使用函数参数。

编辑3:此外,其他辅助函数,如isBoardFull()和isSpaceFree()应该在板子副本上操作,而不是全局变量。

© www.soinside.com 2019 - 2024. All rights reserved.