关于AlphaBeta算法

发布于 2024-09-12 10:24:26 字数 1228 浏览 9 评论 0原文

我用我自己的旧国际象棋引擎体验过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2024-09-19 10:24:26

好的,您对 MinValue/MaxValue 的看法是正确的。

<罢工>
我对 NegaMax 和 AlphaBeta 有点生疏,但是当我看到

   if (eval >= beta)
   {
       return beta;
   }
   if (eval > alpha)
   {
   }

You're test >这两个限制时,这似乎不对。
编辑

:这似乎只是一个命名/理解问题。您的 AlphaBeta() 方法可以更准确地命名为 NegaMaxWithAlphaBeta()。由于 NegaMax 中 alpha 和 beta 的角色交替,这些参数的命名与 MiniMax 并不完全匹配。

我发现算法遇到了 beta 截止,但在我看来,这种情况永远不应该发生

,是的,它应该发生。这只是偶数层的贝塔截止值。在奇数级别,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

   if (eval >= beta)
   {
       return beta;
   }
   if (eval > alpha)
   {
   }

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 accurately NegaMaxWithAlphaBeta(). Because of the alternating roles of alpha and beta in NegaMax the naming of those parameters is not an exact match with MiniMax.

i see that algorithim encounter beta cut-offs but in my opinion, this should never occur

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.

if i don't use narrowed window.

I think you are using a narrowing alpha/beta window.

But maybe this answer can help you explain your problem better.

走野 2024-09-19 10:24:26

请注意,在递归中,您以相反的顺序传递参数:

eval = -AlphaBeta(-beta, -alpha, ply - 1, bestLine);

与声明它们的方式相反:

int AlphaBeta(int alpha, int beta, int ply, Stack bestLine)

因此它们在每一层都会改变角色。 Alpha 可以更新,这相当于在树的更深层次上减少 beta。

对我来说,这看起来像是 Alpha/Beta 和 Negamax 的奇怪组合,因为两个玩家都试图降低分数,而分数在每个级别都会被否定。

Notice that in your recursion you pass the parameters in reverse order:

eval = -AlphaBeta(-beta, -alpha, ply - 1, bestLine);

Reversed from the way they are declared:

int AlphaBeta(int alpha, int beta, int ply, Stack bestLine)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文