【问题标题】:Minimax works fine but Alpha-beta prunning doesn'tMinimax 工作正常,但 Alpha-beta 修剪不能
【发布时间】:2021-08-29 06:47:44
【问题描述】:

我试图让 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】:

    目前尚不清楚您是否尝试实现Fail-hard or Fail-soft methods,但我认为是后者。如果是这样,那么虽然我无法为您测试,但我认为这就是您想要的:

    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;
        }
    }
    

    【讨论】:

    • 谢谢!它现在似乎和 Minimax 一样工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多