【问题标题】:How do I increase the performance of my alpha beta pruning minimax algorithm with memory and iterative deepening如何通过内存和迭代深化来提高我的 alpha beta pruning minimax 算法的性能
【发布时间】:2021-12-09 22:47:03
【问题描述】:

我用 python 创建了一个游戏,比如井字游戏,我可以在其中选择棋盘的大小和连续获胜所需的棋子数量。我还创建了一个具有极小值、α-β 修剪、转置表和迭代深化的机器人。我正在使用 python,我的板表示只是一个二维数组。由于某种原因,当国际象棋引擎每秒有 100,000 多个节点时,我的算法只能计算每秒 600 个节点。我知道 python 很慢,并且板表示会影响速度,但我认为它不应该只计算每秒大约 600 个节点。我还有一个评估功能,可以循环整个棋盘并评估棋子的位置。我只是想弄清楚如何加速算法,因为它看起来慢得不合理。

我的算法:

def tt_minimax(board, plr, alpha, beta, depth):
    global tt_start_time
    if ITERATIVEDEEPENING and timeit.default_timer()-tt_start_time >= MAXTIME:
        return "UNFINISHED", 0

    alpha_org = alpha

    tt_code = tt_hash(board)
    pv_move = []
    if tt_code in TRANSPOSITION_TABLE:
        tt_entry = TRANSPOSITION_TABLE[tt_code]
        if tt_entry[3] >= depth:
            if tt_entry[2] == 0:  # checking for Exact
                return tt_entry[0], tt_entry[1]
            elif tt_entry[2] == -1:  # lower bound
                alpha = max(alpha, tt_entry[1])
            elif tt_entry[2] == 1:  # upper bound
                beta = min(beta, tt_entry[1])

            if alpha >= beta:
                return tt_entry[0], tt_entry[1]
        else:
            pv_move = tt_entry[0]

    global tt_node_count
    tt_node_count += 1

    moves = find_moves(board)
    #random.shuffle(moves)

    if pv_move:
        moves.insert(0, moves.pop(moves.index(pv_move)))

    # if drawn
    if len(moves) == 0:
        return "none", 0

    # maximizing
    if plr == 1:

        best_score = MIN
        best_move = []
        for movePair in moves:
            y, x = movePair[0], movePair[1]
            board[y][x] = plr

            wt_code = tt_hash(board)
            if wt_code in WIN_TABLE:
                test_result = WIN_TABLE[wt_code]
            else:
                test_result = test_for_win(board)
                WIN_TABLE[wt_code] = test_result

            if test_result == plr:
                evaluation = 1000 + TTD + depth
                if evaluation > best_score:
                    best_score = evaluation
                    best_move = movePair
            elif depth > 0:
                returned = tt_minimax(board, 2, alpha, beta, depth - 1)
                if returned[0] == "UNFINISHED":
                    return returned
                return_eval = returned[1]
                if return_eval > best_score:
                    best_score = return_eval
                    best_move = movePair
            else:
                evaluation = evaluate(board, 2)
                if evaluation > best_score:
                    best_score = evaluation
                    best_move = movePair

            board[y][x] = 0
            alpha = max(alpha, best_score)

            if beta <= alpha:
                break

        tt_store(TRANSPOSITION_TABLE, board, alpha_org, beta,
                 best_move, best_score, depth)

        return best_move, best_score

    # minimizing
    else:
        best_score = MAX
        best_move = []
        for movePair in moves:
            y, x = movePair[0], movePair[1]
            board[y][x] = plr

            wt_code = tt_hash(board)
            if wt_code in WIN_TABLE:
                test_result = WIN_TABLE[wt_code]
            else:
                test_result = test_for_win(board)
                WIN_TABLE[wt_code] = test_result

            if test_result == plr:
                evaluation = -1000 - depth
                if evaluation < best_score:
                    best_score = evaluation
                    best_move = movePair
            elif depth > 0:
                returned = tt_minimax(board, 1, alpha, beta, depth - 1)
                if returned[0] == "UNFINISHED":
                    return returned
                return_eval = returned[1]
                if return_eval < best_score:
                    best_score = return_eval
                    best_move = movePair
            else:
                evaluation = evaluate(board, 2)
                if evaluation < best_score:
                    best_score = evaluation
                    best_move = movePair

            board[y][x] = 0
            beta = min(beta, best_score)

            if beta <= alpha:
                break

        tt_store(TRANSPOSITION_TABLE, board, alpha_org, beta,
                 best_move, best_score, depth)

        return best_move, best_score

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python minimax


【解决方案1】:

C 比 Python 快一点。 C 比 Python 快很多。将其转换为 C 将是巨大的。然后你可以再问一次,因为用 C 编码的东西也是巨大的。以十倍的速度乘以十倍的速度,您的程序将在 C 语言中尖叫。

Python,好吧,只要说 Python 并不意味着快就足够了。在 Python 的初始设计中,速度并不是一个因素,现在一切都是对象......只要说 Python 变得越来越好,它就会变得越来越慢。我喜欢 Python 是因为它的目的,但 C 也有它的目的。您的程序是 C 语言用途的完美示例。使用 Python 作为包装器来玩游戏。使用 C 计算下一步。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多