我正在使用 negamax 和 alpha beta 剪枝设置一个基本的国际象棋人工智能。如果我删除 alpha beta 修剪,那么它会按预期工作。添加修剪后,它的棋步会更差(但速度要快得多)。有时它会按预期发挥,直到在 1 中突然出现错误。我不知道为什么有时最好的动作会被修剪。有人能找出问题所在吗?
public Move Think(Board board, Timer timer)
{
int bestEval = int.MinValue;
Move[] moves = board.GetLegalMoves();
Move bestMove = moves[0];
foreach (Move move in moves)
{
board.MakeMove(move);
int eval = -NegaMax(board, MAX_DEPTH, -100000, 100000);
board.UndoMove(move);
if (eval > bestEval)
{
bestEval = eval;
bestMove = move;
}
}
return bestMove;
}
//Evaluates a move based on the resulting board position
private int NegaMax(Board board, int depth, int alpha, int beta)
{
if (board.IsDraw())
return 0;
if (depth == 0)
return EvaluatePosition(board);
Move[] moves = board.GetLegalMoves();
int bestEval = int.MinValue;
foreach (Move move in moves)
{
board.MakeMove(move);
bestEval = Math.Max(bestEval, -NegaMax(board, depth - 1, -beta, -alpha));
board.UndoMove(move); //Must restore the board to its original position before testing the next move
alpha = Math.Max(alpha, bestEval);
if (alpha >= beta)
break;
}
return bestEval;
}
我尝试过的一件事是,当我在 think() 函数中调用它时,使用 int.MinValue 和 int.MaxValue 作为原始的 alpha 和 beta 参数。然而,这会导致更意想不到的行为。我希望它能发挥相同的作用,但它会发挥非常不同的动作,尽管它们和以前一样糟糕。