[英]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.