关于AlphaBeta算法
我用我自己的旧国际象棋引擎体验过 AlphaBeta 算法,现在我正在尝试编写新引擎,我看到算法遇到了 beta 截止,但在我看来,如果我不使用缩小的窗口,这种情况永远不会发生。我错了吗?我使用 int.MaxValue
作为 beta 版本,使用 -int.MaxValue
作为 alpha 版本,那么什么会导致 beta 截止呢?
编辑:
Full code is here. public Result Search(int maxDepth)
{
int alpha = -int.MaxValue, beta = int.MaxValue, ply = maxDepth;
var bestLine = new Stack<Move>();
var score = AlphaBeta(alpha, beta, ply, bestLine);
return new Result(score, bestLine);
}
int AlphaBeta(int alpha, int beta, int ply, Stack<Move> bestLine)
{
if (ply <= 0) return Evaluation.Evaluate(Board);
var moves = Board.GenerateMoves();
foreach (var move in moves)
{
Board.MakeMove(move);
eval = -AlphaBeta(-beta, -alpha, ply - 1, bestLine);
Board.TakeBackMove(move);
if (eval >= beta)
{
return beta;
}
if (eval > alpha)
{
alpha = eval;
if (ply == 1) bestLine.Clear();
bestLine.Push(move);
}
}
return alpha;
}
}
I have experienced AlphaBeta algorithm with my own old chess engine and now i am trying to write new engine and i see that algorithim encounter beta cut-offs but in my opinion, this should never be occur if i don't use narrowed window. Am i wrong ? I am using int.MaxValue
for beta and -int.MaxValue
for alpha so what can cause a beta cut-offs ?
Edit:
Full code is here.
public Result Search(int maxDepth)
{
int alpha = -int.MaxValue, beta = int.MaxValue, ply = maxDepth;
var bestLine = new Stack<Move>();
var score = AlphaBeta(alpha, beta, ply, bestLine);
return new Result(score, bestLine);
}
int AlphaBeta(int alpha, int beta, int ply, Stack<Move> bestLine)
{
if (ply <= 0) return Evaluation.Evaluate(Board);
var moves = Board.GenerateMoves();
foreach (var move in moves)
{
Board.MakeMove(move);
eval = -AlphaBeta(-beta, -alpha, ply - 1, bestLine);
Board.TakeBackMove(move);
if (eval >= beta)
{
return beta;
}
if (eval > alpha)
{
alpha = eval;
if (ply == 1) bestLine.Clear();
bestLine.Push(move);
}
}
return alpha;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,您对 MinValue/MaxValue 的看法是正确的。
<罢工>
我对 NegaMax 和 AlphaBeta 有点生疏,但是当我看到
You're test>
这两个限制时,这似乎不对。编辑
:这似乎只是一个命名/理解问题。您的
AlphaBeta()
方法可以更准确地命名为NegaMaxWithAlphaBeta()
。由于 NegaMax 中 alpha 和 beta 的角色交替,这些参数的命名与 MiniMax 并不完全匹配。,是的,它应该发生。这只是偶数层的贝塔截止值。在奇数级别,
if (eval >= beta)
测试 alpha 截止值。我认为您正在使用缩小的 alpha/beta 窗口。
但也许这个答案可以帮助您更好地解释您的问题。
OK, you are right on the MinValue/MaxValue thing.
I'm a bit rusty about NegaMax and AlphaBeta but when I see
You're testing
>
for both limits, that doesn't seem right.Edit: It seems just a naming/understanding issue of sorts. Your
AlphaBeta()
method could be named more accuratelyNegaMaxWithAlphaBeta()
. Because of the alternating roles of alpha and beta in NegaMax the naming of those parameters is not an exact match with MiniMax.Yes it should occur. And it's only a beta-cutoff at the even ply-levels. At the odd levels,
if (eval >= beta)
tests for an alpha-cutoff.I think you are using a narrowing alpha/beta window.
But maybe this answer can help you explain your problem better.
请注意,在递归中,您以相反的顺序传递参数:
与声明它们的方式相反:
因此它们在每一层都会改变角色。 Alpha 可以更新,这相当于在树的更深层次上减少 beta。
对我来说,这看起来像是 Alpha/Beta 和 Negamax 的奇怪组合,因为两个玩家都试图降低分数,而分数在每个级别都会被否定。
Notice that in your recursion you pass the parameters in reverse order:
Reversed from the way they are declared:
So they change roles at each ply. Alpha can be updated, and that's equivalent to a reduction in beta on deeper levels of the tree.
To me this looks like an odd mix of Alpha/Beta and Negamax since both players are trying to reduce the score, which is negated at each level.