问题描述
我正在尝试使 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[,true)) {
score = Minimax(board,true);
bestscore = Mathf.Min(score,bestscore);
}
}
return bestscore;
}
}
这是我的 Alphabeta 修剪函数
float ABPruning(char[,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[,false)) {
bestscore = ABPruning(board,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[,true)) {
bestscore = ABPruning(board,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[,true)) {
bestScore = Mathf.Min(bestScore,true));
beta = Mathf.Min(beta,bestScore);
if (bestScore<=alpha) {
break;
}
}
}
return bestScore;
}
}