![](/img/trans.png)
[英]Minimax with Alpha-Beta pruning; class variables or sending them through the recursion?
[英]Minimax works fine but Alpha-beta prunning doesn't
我試圖讓 Alpha-beta 修剪工作,但與我的 Minimax function 相比,它給了我完全錯誤的動作。 這是我的 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;
}
}
這是我的 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;
}
}
兩者都使用相同的評估,不確定這里有什么問題。 謝謝您的幫助。
目前尚不清楚您是在嘗試實現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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.