简体   繁体   English

Minimax 工作正常,但 Alpha-beta 修剪不能

[英]Minimax works fine but Alpha-beta prunning doesn't

I'm trying to get Alpha-beta pruning to work but it's giving me completely wrong moves compared to my Minimax function.我试图让 Alpha-beta 修剪工作,但与我的 Minimax function 相比,它给了我完全错误的动作。 Here is my Minimax function which is working perfectly right now.这是我的 Minimax function,它现在运行良好。

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

and here is my Alphabeta pruning function这是我的 Alphabeta 修剪 function

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

Both are using same evaluations, not sure what's wrong here.两者都使用相同的评估,不确定这里有什么问题。 Thank you for your help.谢谢您的帮助。

It's unclear whether you are trying to implement the Fail-hard or Fail-soft methods , but I think that it's the later.目前尚不清楚您是在尝试实现Fail-hard 还是 Fail-soft 方法,但我认为是后者。 If so, then while i cannot test this for you, I think that this is what you want:如果是这样,那么虽然我无法为您测试,但我认为这就是您想要的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Alpha-Beta修剪的Minimax; 类变量还是通过递归发送它们? - Minimax with Alpha-Beta pruning; class variables or sending them through the recursion? Minimax Alpha Beta Pruning 不起作用,但 Minimax 单独起作用 - Minimax Alpha Beta Pruning not working, but Minimax alone does 改进井字游戏的评估功能-alpha-beta算法 - Improving evaluation function in tic-tac-toe game - alpha-beta algorithm 如何在C#中使用Alpha Beta修剪从Minimax取得最佳进展? - How to get best move from Minimax with Alpha Beta Pruning in C#? 尽管过程正常,但为什么参数化查询不起作用 - Why parameterized query doesn't work although the procedure works fine WebApplication 在 IIS Express 上运行良好,但在本地 IIS 上不起作用 - WebApplication works fine on IIS Express but it doesn't on Local IIS Call to Process可与Debug一起正常使用,但在已安装的应用程序中不起作用 - Call to Process works fine with Debug, but it doesn't work in the installed application 我的WebApi在Visual Studio上运行良好,但在IIS上不起作用 - My WebApi works fine on Visual Studio but doesn't work on IIS PutAsync不向web api发送请求,但fiddler工作正常 - PutAsync doesn't send request to web api, but fiddler works fine Aero Glass在Windows 7上可以正常工作,但在Vista上则不能工作 - Aero Glass works fine on Windows 7, but doesn't work on Vista
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM