简体   繁体   English

NegaMax无法正常工作

[英]NegaMax doesn't work as expected

I have a problem with implementing a simple NegaMax in my chess programm. 我在国际象棋程序中实现一个简单的NegaMax时遇到问题。

According to several websites negamax should look as follows in my code: 根据几个网站,negamax在我的代码中应如下所示:

int Position::negaMax(int curr_depth, int depth) {
    cd = curr_depth-1;
    if (curr_depth==depth) return evaluate();

    int max = -500000;

    calc_moves(true);
    doBackup(cd);
    for (int i=0;i<mvSize[cd];i++) {
        move_figure(mvD[cd][i][0],mvD[cd][i][1],mvD[cd][i][2],mvI[cd][i][0],mvI[cd][i][1]);    
        int score = -negaMax(curr_depth+1,depth);
        cd--; undoMove(cd);

        if (curr_depth==1)
            cout << "Move: " << getMoveString(i) << ", Score: " << score << endl;        

        if (score>max)
            max=score;
   }
   return max;
}

But with this code I get this output: 但是使用此代码,我得到以下输出:

Move: a2a3, Score: 0
Move: a2a4, Score: 0
Move: b2b3, Score: 0
Move: b2b4, Score: 0
Move: c2c3, Score: 0
Move: c2c4, Score: 0
Move: d2d3, Score: 0
Move: d2d4, Score: 0
Move: e2e3, Score: 0
Move: e2e4, Score: 0
Move: f2f3, Score: 0
Move: f2f4, Score: 0
Move: g2g3, Score: 0
Move: g2g4, Score: 0
Move: h2h3, Score: 0
Move: h2h4, Score: 0
Move: b1a3, Score: 0
Move: b1c3, Score: 0
Move: g1h3, Score: 0
Move: g1f3, Score: 0
score: 0

That can't be right, if I negaMax for ply3 from the starting position. 如果我从起始位置开始将negaMax用于ply3,那可能是不对的。

If I remove the minus sign in front of the recursive function call, I get a more better result. 如果删除递归函数调用前面的减号,则可获得更好的结果。 But in my opinion that can't be right, because without the minus sign in the above code, I only maximize the score for one player, but not for the both. 但是我认为这是不对的,因为如果没有上面代码中的减号,我只会使一个球员的得分最大化,而不能使两个球员的得分最大化。

Move: a2a3, Score: 0
Move: a2a4, Score: 30
Move: b2b3, Score: 0
Move: b2b4, Score: 30
Move: c2c3, Score: 0
Move: c2c4, Score: 30
Move: d2d3, Score: 295
Move: d2d4, Score: 295
Move: e2e3, Score: 295
Move: e2e4, Score: 295
Move: f2f3, Score: 0
Move: f2f4, Score: 30
Move: g2g3, Score: 0
Move: g2g4, Score: 30
Move: h2h3, Score: 0
Move: h2h4, Score: 30
Move: b1a3, Score: 30
Move: b1c3, Score: 30
Move: g1h3, Score: 30
Move: g1f3, Score: 30
score: 295

I've tried to implement different versions of MinMax, NegaMax and AlphaBeta. 我尝试实现不同版本的MinMax,NegaMax和AlphaBeta。 But I always get a score of 0. I would be very thankful for any hints. 但是我总是得到0分。对于任何提示,我将非常感谢。

The actual skeleton of negamax appears to be implemented correctly. negamax的实际框架似乎已正确实施。 (However, I'm more used to seeing a single depth variable passed to the recursive function that is subtracted each ply - and returns the evaluated score when it equals 0). (但是,我更习惯于看到传递给递归函数的单个深度变量,该深度变量每层相减-等于0时返回评估分数)。 But it is hard to diagnose your problem as an outsider due to the large dependency on other code. 但是由于对其他代码的依赖性很大,因此很难以局外人的身份诊断您的问题。

Rather than fishing for you, I feel it would be better to teach you how to fish. 我觉得与其教您钓鱼,不如教您如何钓鱼。 I would suggest spending some time to build a routine that outputs the tree structure and cumulative score visually somehow. 我建议花一些时间来建立一个例程,以某种方式直观地输出树的结构和累积分数。 It seems as though you already have the building blocks of such a thing, too. 似乎您已经已经有了这种东西的基础。 It might be time consuming to do this initially, but in the long run will help greatly with debugging - and believe me, with a chess engine, trawling through this tree will be an unfortunately frequent reality, especially when you implement more obscure moves like en-passant - these can cause all sorts of troubles within the tree (I've been there). 最初执行此操作可能会很耗时,但是从长远来看,这将极大地帮助调试-并且相信我,使用国际象棋引擎,在这棵树上拖网将是一个不幸的现实,尤其是当您执行诸如-passant-这些会引起树内的各种麻烦(我去过那里)。

Try to output something like this: 尝试输出如下内容:

<move-white ---->
    <move-black ----><eval score>
    <move-black ----><eval score>
    <move-black ----><eval score>
    <best black score>
<move-white ---->
    <move-black ----><eval score>
    <move-black ----><eval score>
    <move-black ----><eval score>
    <move-black ----><eval score>
    <move-black ----><eval score>
    <move-black ----><eval score>
    <best black score>
<move-white ---->
    <move-black ----><eval score>
    <move-black ----><eval score>
    <best black score>
<best white score>

...where ---- denotes the move. ...其中----表示移动。

Obviously this is going to be much larger and deeper, but at least you can see what is happening in a more human friendly way. 显然,这将变得更大更深,但是至少您可以以更人性化的方式看到正在发生的事情。 Hopefully it will help you with other issues in the long run, too. 希望从长远来看,它也会帮助您解决其他问题。 Setting up a good debugging system with a chess engine is vital, as you will probably find. 您可能会发现,使用国际象棋引擎建立良好的调试系统至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM