[英]Minimax Alpha Beta Pruning not working, but Minimax alone does
My problem is when applying Alpha/Beta Pruning to Minimax.我的问题是在将 Alpha/Beta Pruning 应用于 Minimax 时。 It does very wierd and bad moves.
它做了非常奇怪和糟糕的动作。 When I use Minimax without Alpha/Beta, it works fine.
当我使用没有 Alpha/Beta 的 Minimax 时,它可以正常工作。 The two functions look like this:
这两个函数如下所示:
Minimax With Alpha/Beta Pruning:带有 Alpha/Beta 修剪的 Minimax:
public int minimaxAB(Piece[,] board, int depth, int a, int b, bool maximizingPlayer, bool WhiteToPlay)
{
if (depth == 0)
{
return EvaluatePosition(board, WhiteToPlay);
}
var moves = GenerateMoves(board, WhiteToPlay);
if (maximizingPlayer)
{
int value = int.MinValue;
foreach (var move in moves)
{
int minmaxResult = minimaxAB(move, depth - 1, a, b, false, !WhiteToPlay);
value = Math.Max(value, minmaxResult);
a = Math.Max(a, value);
if (a >= b)
return a;
if (depth == depthB)
{
moveScores.Add(move, minmaxResult);
}
}
return value;
}
else
{
int value = int.MaxValue;
foreach (var move in moves)
{
int minmaxResult = minimaxAB(move, depth - 1, a, b, true, !WhiteToPlay);
value = Math.Min(value, minmaxResult);
b = Math.Min(b, value);
if (b <= a)
return b;
if (depth == depthB)
{
moveScores.Add(move, minmaxResult);
}
}
return value;
}
}
Minimax without A/B:无 A/B 的极小极大:
public int minimax(Piece[,] board, int depth, bool maximizingPlayer, bool WhiteToPlay)
{
if (depth == 0)
{
int result = EvaluatePosition(board, WhiteToPlay);
return result;
}
var moves = GenerateMoves(board, WhiteToPlay);
if (maximizingPlayer)
{
int value = int.MinValue;
foreach (var move in moves)
{
int minmaxResult = minimax(move, depth - 1, false, !WhiteToPlay);
value = Math.Max(value, minmaxResult);
if (depth == depthB)
{
moveScores.Add(move, minmaxResult);
}
}
return value;
}
else
{
int value = int.MaxValue;
foreach (var move in moves)
{
int minmaxResult = minimax(move, depth - 1, true, !WhiteToPlay);
value = Math.Min(value, minmaxResult);
if (depth == depthB)
{
moveScores.Add(move, minmaxResult);
}
}
return value;
}
}
My evaluation function:我的评价function:
public int EvaluatePosition(Piece[,] boardPos, bool ForWhite)
{
int eval_W = 0;
int eval_B = 0;
int eval = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (boardPos[i, j] != Piece.Empty)
{
if (IsPieceWhite(boardPos[i, j]))
{
eval_W += GetPieceWorth(boardPos[i, j]) + DistanceToCenter(i, j);
eval += GetPieceWorth(boardPos[i, j]);
}
else if (IsPieceBlack(boardPos[i, j]))
{
eval_B += GetPieceWorth(boardPos[i, j]) + DistanceToCenter(i, j);
eval -= GetPieceWorth(boardPos[i, j]);
}
}
}
}
if (ForWhite)
return eval_W - eval_B;
else
return eval_B - eval_W;
}
I call with: minimaxAB(CurrentBoard, depthB, int.MinValue, int.MaxValue, true, whiteToMove);
我打电话给:
minimaxAB(CurrentBoard, depthB, int.MinValue, int.MaxValue, true, whiteToMove);
I am aware that Minimax with AB is suppose to produce exactly the same result, but in my case it does not.我知道带有 AB 的 Minimax 应该会产生完全相同的结果,但在我的情况下它不会。 I hope someone is able to spot what I did wrong.
我希望有人能够发现我做错了什么。
I figured it out, I needed an alpha and beta for both white and black.我想通了,我需要一个白色和黑色的 alpha 和 beta。 The reason for this is that I call the minimaxAB function for both white and black moves.
原因是我将 minimaxAB function 称为黑白棋步。
Working method:工作方法:
public int minimaxAB(Piece[,] board, int depth, int alpha_White, int beta_White, int alpha_Black, int beta_Black, bool maximizingPlayer, bool WhiteToPlay)
{
if (depth == 0 || !HasKings(board))
{
return EvaluatePosition(board, WhiteToPlay);
}
var moves = GenerateMoves(board, WhiteToPlay);
if (maximizingPlayer)
{
int value = int.MinValue;
foreach (var move in moves)
{
int minmaxResult = minimaxAB(move, depth - 1, alpha_White, beta_White, alpha_Black, beta_Black, false, !WhiteToPlay);
value = Math.Max(value, minmaxResult);
if (WhiteToPlay)
{
alpha_White = Math.Max(alpha_White, value);
if (alpha_White >= beta_White)
return alpha_White;
}
else
{
alpha_Black = Math.Max(alpha_Black, value);
if (alpha_Black >= beta_Black)
return alpha_Black;
}
if (depth == depthB)
{
moveScores.Add(move, minmaxResult);
}
}
return value;
}
else
{
int value = int.MaxValue;
foreach (var move in moves)
{
int minmaxResult = minimaxAB(move, depth - 1, alpha_White, beta_White, alpha_Black, beta_Black, true, !WhiteToPlay);
value = Math.Min(value, minmaxResult);
if (WhiteToPlay)
{
beta_White = Math.Min(beta_White, value);
if (beta_White <= alpha_White)
return beta_White;
}
else
{
beta_Black = Math.Min(beta_Black, value);
if (beta_Black <= alpha_Black)
return beta_Black;
}
if (depth == depthB)
{
moveScores.Add(move, minmaxResult);
}
}
return value;
}
}
Called with:调用:
minimaxAB(CurrentBoard, depthB, int.MinValue, int.MaxValue, int.MinValue, int.MaxValue, true, whiteToMove);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.