Minimax 工作得很好,但 Alpha-beta 剪枝却不行

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

我正在尝试让 Alpha-beta 剪枝发挥作用,但与我的 Minimax 函数相比,它给了我完全错误的动作。这是我的 Minimax 函数,现在运行良好。

float Minimax(char[,] _board, int depth, bool isMax) {
    if (depth == 0 || isFull(_board)) {
        EvaluateBoard(_board);
    }

    if (isMax) {
        float bestScore = -Mathf.Infinity;
        float score = -Mathf.Infinity;

        for (int i = 0; i < 7; i++) {
            char[,] board = (char[, ]) _board.Clone();
            if (Play(board, i, false)) {
                score = Minimax(board, depth - 1, false);
                bestScore = Mathf.Max(score, bestScore);
            }
        }
        return bestScore;
    } else {
        float bestScore = Mathf.Infinity;
        float score = Mathf.Infinity;

        for (int i = 0; i < 7; i++) {
            char[,] board = (char[, ]) _board.Clone();
            if (Play(board, i, true)) {
                score = Minimax(board, depth - 1, true);
                bestScore = Mathf.Min(score, bestScore);
            }
        }
        return bestScore;
    }
}

这是我的 Alphabeta 剪枝函数

float ABPruning(char[,] _board, int depth, float alpha, float beta, bool isMax) {
    if (depth == 0 || isFull(_board)) {
        return EvaluateBoard(_board);
    }
    if (isMax) {
        float bestScore = -Mathf.Infinity;
        
        for (int i = 0; i < 7; i++) {
            char[, ] board = (char[,]) _board.Clone();
            if (Play(board, i, false)) {
                bestScore = ABPruning(board, depth - 1, alpha, beta, false);
                alpha = Mathf.Max(alpha, bestScore);
                if (beta<=alpha) {
                    return bestScore;
                }

            }
        }
        return bestScore;
    } else {
        float bestScore = Mathf.Infinity;

        for (int i = 0; i < 7; i++) {
            char[, ] board = (char[,]) _board.Clone();
            if (Play(board, i, true)) {
                bestScore = ABPruning(board, depth - 1, alpha, beta, true);
                beta = Mathf.Min(beta, bestScore);
                if (beta<=alpha) {
                    break;
                }

            }
        }
        return bestScore;
    }
}

两者都使用相同的评估,不确定这里出了什么问题。谢谢您的帮助。

c# algorithm minimax alpha-beta-pruning
1个回答
2
投票

目前还不清楚您是否正在尝试实现“Fail-hard”或“Fail-soft”方法,但我认为是后者。 如果是这样,那么虽然我无法为您测试这一点,但我认为这就是您想要的:

float ABPruning(char[,] _board, int depth, float alpha, float beta, bool isMax) { if (depth == 0 || isFull(_board)) { return EvaluateBoard(_board); } if (isMax) { float bestScore = -Mathf.Infinity; for (int i = 0; i < 7; i++) { char[, ] board = (char[,]) _board.Clone(); if (Play(board, i, false)) { bestScore = Mathf.Max(bestScore, ABPruning(board, depth - 1, alpha, beta, false)); alpha = Mathf.Max(alpha, bestScore); if (bestScore>=beta) { break; } } } return bestScore; } else { float bestScore = Mathf.Infinity; for (int i = 0; i < 7; i++) { char[, ] board = (char[,]) _board.Clone(); if (Play(board, i, true)) { bestScore = Mathf.Min(bestScore, ABPruning(board, depth - 1, alpha, beta, true)); beta = Mathf.Min(beta, bestScore); if (bestScore<=alpha) { break; } } } return bestScore; } }
    
© www.soinside.com 2019 - 2024. All rights reserved.