Minimax 工作正常但 Alpha-beta 剪枝不行
Minimax works fine but Alpha-beta prunning doesn't
我正在尝试让 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;
}
}
两者都使用相同的评估,不确定这里有什么问题。感谢您的帮助。
不清楚您是否正在尝试实施 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;
}
}
我正在尝试让 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;
}
}
两者都使用相同的评估,不确定这里有什么问题。感谢您的帮助。
不清楚您是否正在尝试实施 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;
}
}