【问题标题】:Chess: Bug in Alpha-Beta国际象棋:Alpha-Beta 中的错误
【发布时间】:2016-07-03 20:06:36
【问题描述】:

我正在实现一个国际象棋引擎,并且我编写了一个相当复杂的 alpha-beta 搜索例程,其中包含静止搜索和转置表。但是,我观察到一个奇怪的错误。

评估函数使用的是方格表,例如棋子:

static int ptable_pawn[64] = {  
   0,  0,  0,  0,  0,  0,  0,  0,
  30, 35, 35, 40, 40, 35, 35, 30,
  20, 25, 25, 30, 30, 25, 25, 20,
  10, 20, 20, 20, 20, 20, 20, 10,
   3,  0, 14, 15, 15, 14,  0,  3,
   0,  5,  3, 10, 10,  3,  5,  0,
   5,  5,  5,  5,  5,  5,  5,  5,
   0,  0,  0,  0,  0,  0,  0,  0
};

当轮到黑方时,桌子在 x 轴上反射。具体来说,如果你很好奇,查找是这样发生的,其中 A-H 列映射到 0-7,行是从白色一侧开始的 0-7:

int ptable_index_for_white(int col, int row) {
    return col+56-(row*8);
}

int ptable_index_for_black(int col, int row) {
    return col+(row*8);
}

因此,在 h4(坐标 7、3)上的棋子对于白色来说价值 3 分(厘兵),而在 f6(坐标 5、5)上的棋子对于黑色来说价值 3 分。

目前整个评价函数都是片方表和材料。

在更大的搜索深度,我的引擎选择了一些真正可怕的动作。考虑这个从起始位置生成的输出:

Iterative Deepening Analysis Results (including cached analysis)
Searching at depth 1... d1 [+0.10]: 1.b1c3 
    (4 new nodes, 39 new qnodes, 0 qnode aborts, 0ms), 162kN/s
Searching at depth 2... d2 [+0.00]: 1.e2e4 d7d5 
    (34 new nodes, 78 new qnodes, 0 qnode aborts, 1ms), 135kN/s
Searching at depth 3... d3 [+0.30]: 1.d2d4 d7d5 2.c1f4 
    (179 new nodes, 1310 new qnodes, 0 qnode aborts, 4ms), 337kN/s
Searching at depth 4... d4 [+0.00]: 1.g1f3 b8c6 2.e2e4 d7d5 
    (728 new nodes, 2222 new qnodes, 0 qnode aborts, 14ms), 213kN/s
Searching at depth 5... d5 [+0.20]: 1.b1a3 g8f6 2.d2d4 h8g8 3.c1f4 
    (3508 new nodes, 27635 new qnodes, 0 qnode aborts, 103ms), 302kN/s
Searching at depth 6... d6 [-0.08]: 1.d2d4 a7a5 2.c1f4 b7b6 3.f4c1 c8b7 
    (21033 new nodes, 112915 new qnodes, 0 qnode aborts, 654ms), 205kN/s
Searching at depth 7... d7 [+0.20]: 1.b1a3 g8f6 2.a1b1 h8g8 3.d2d4 g8h8 4.c1f4 
    (39763 new nodes, 330837 new qnodes, 0 qnode aborts, 1438ms), 258kN/s
Searching at depth 8... d8 [-0.05]: 1.e2e4 a7a6 2.e4e5 a6a5 3.h2h4 d7d6 4.e5d6 c7d6 
    (251338 new nodes, 2054526 new qnodes, 0 qnode aborts, 12098ms), 191kN/s

在深度 8 处,请注意黑方以“... a7a6 ... a6a5”的移动开局,根据棋盘桌,这很可怕。此外,“h2h4”对于白人来说是一个可怕的举动。为什么我的搜索功能会选择如此奇怪的动作?值得注意的是,这只会在更深的地方开始发生(深度 3 的移动看起来很好)。

此外,搜索经常会出错!考虑以下位置:

引擎推荐了一个可怕的错误(3... f5h3),不知何故错过了明显的回复(4.g2h3):

Searching at depth 7... d7 [+0.17]: 3...f5h3 4.e3e4 h3g4 5.f2f3 g8f6 6.e4d5 f6d5 
    (156240 new nodes, 3473795 new qnodes, 0 qnode aborts, 17715ms), 205kN/s

不涉及静止搜索,因为错误发生在第 1 层 (!!)。

这是我的搜索功能的代码。很抱歉它太长了:我尽我所能简化了,但我不知道哪些部分与错误无关。我认为我的算法在某种程度上是错误的。

该实现几乎完全基于维基百科的this one。 (更新:我已经大大简化了搜索,我的错误仍然存​​在。)

// Unified alpha-beta and quiescence search
int abq(board *b, int alpha, int beta, int ply) {
    pthread_testcancel(); // To allow search worker thread termination
    bool quiescence = (ply <= 0);

    // Generate all possible moves for the quiscence search or normal search, and compute the
    // static evaluation if applicable.
    move *moves = NULL;
    int num_available_moves = 0;
    if (quiescence) moves = board_moves(b, &num_available_moves, true); // Generate only captures
    else moves = board_moves(b, &num_available_moves, false); // Generate all moves
    if (quiescence && !useqsearch) return relative_evaluation(b); // If qsearch is turned off

    // Abort if the quiescence search is too deep (currently 45 plies)
    if (ply < -quiesce_ply_cutoff) { 
        sstats.qnode_aborts++;
        return relative_evaluation(b);
    }

    // Allow the quiescence search to generate cutoffs
    if (quiescence) {
        int score = relative_evaluation(b);
        alpha = max(alpha, score);
        if (alpha >= beta) return score;
    }

    // Update search stats
    if (quiescence) sstats.qnodes_searched++;
    else sstats.nodes_searched++;

    // Search hueristic: sort exchanges using MVV-LVA
    if (quiescence && mvvlva) nlopt_qsort_r(moves, num_available_moves, sizeof(move), b, &capture_move_comparator);

    move best_move_yet = no_move;
    int best_score_yet = NEG_INFINITY;
    int num_moves_actually_examined = 0; // We might end up in checkmate
    for (int i = num_available_moves - 1; i >= 0; i--) { // Iterate backwards to match MVV-LVA sort order
        apply(b, moves[i]);
        // never move into check
        coord king_loc = b->black_to_move ? b->white_king : b->black_king; // for side that just moved
        if (in_check(b, king_loc.col, king_loc.row, !(b->black_to_move))) {
            unapply(b, moves[i]);
            continue;
        }
        int score = -abq(b, -beta, -alpha, ply - 1);
        num_moves_actually_examined++;
        unapply(b, moves[i]);
        if (score >= best_score_yet) {
            best_score_yet = score;
            best_move_yet = moves[i];
        }
        alpha = max(alpha, best_score_yet);
        if (alpha >= beta) break;
    }

    // We have no available moves (or captures) that don't leave us in check
    // This means checkmate or stalemate in normal search
    // It might mean no captures are available in quiescence search
    if (num_moves_actually_examined == 0) {
        if (quiescence) return relative_evaluation(b); // TODO: qsearch doesn't understand stalemate or checkmate
        coord king_loc = b->black_to_move ? b->black_king : b->white_king;
        if (in_check(b, king_loc.col, king_loc.row, b->black_to_move)) return NEG_INFINITY; // checkmate
        else return 0; // stalemate
    }

    // record the selected move in the transposition table
    evaltype type = (quiescence) ? qexact : exact;
    evaluation eval = {.best = best_move_yet, .score = best_score_yet, .type = type, .depth = ply};
    tt_put(b, eval);
    return best_score_yet;
}

/* 
 * Returns a relative evaluation of the board position from the perspective of the side about to move.
 */
int relative_evaluation(board *b) {
    int evaluation = evaluate(b);
    if (b->black_to_move) evaluation = -evaluation;
    return evaluation;
}

我正在像这样调用搜索:

int result = abq(b, NEG_INFINITY, POS_INFINITY, ply);

编辑:即使我简化了搜索例程,该错误仍然存​​在。引擎只是把碎片弄错了。您可以通过在 XBoard(或任何其他与 UCI 兼容的 GUI)中加载它并在强大的引擎上运行它来轻松地看到这一点。应manlio的要求,我上传了代码:

这里是 GitHub 存储库(链接已删除;问题出在上面的 sn-p 中)。它将在 OS X 或任何 *nix 系统上使用“make”构建。

【问题讨论】:

  • stackoverflow 上有很多用户。其中有多少国际象棋引擎实现者?我不知道,但如果你没有得到这个答案,我不会感到惊讶。 :)
  • 长而有趣的问题,但是我认为如果您将其发布在专门的国际象棋编程论坛上,您会得到更好的答案。其中之一是Computer Chess Club: Programming and Technical Discussions。那里有很多真正的专家。
  • 如果还没有尝试过,我首先建议在没有转置表的情况下在这个位置上测试引擎(例如用if(false)替换if (stored != NULL &amp;&amp; stored-&gt;depth &gt;= ply))以确保错误不是由于 TT 代码不一致。
  • @Arnauld 谢谢,但这并没有修复错误。
  • 另外,您是否确认搜索后的电路板结构与搜索前的状态完全相同?我知道这听起来像是一个相当愚蠢的检查,但是 make/unmake 例程中的一些错误可能只会在某些情况下触发(如castling或en-passant capture)并完全破坏分析。

标签: algorithm artificial-intelligence chess minimax alpha-beta-pruning


【解决方案1】:
if (score >= best_score_yet) {

应该是:

if (score > best_score_yet) {

或者你会考虑不好的举动。第一个best_move_yet 是正确的(因为best_score_yet = NEG_INFINITY),但score == best_score_yet 的其他动作不一定更好。

更改该行:

起始位置

Iterative Deepening Analysis Results (including cached analysis)
Searching at depth 1... d1 [+0.10]: 1.e2e4 
    (1 new nodes, 4 new qnodes, 0 qnode aborts, 0ms, 65kN/s)
    (ttable: 1/27777778 = 0.00% load, 0 hits, 0 misses, 1 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 2... d2 [+0.00]: 1.e2e4 g8f6 
    (21 new nodes, 41 new qnodes, 0 qnode aborts, 0ms, 132kN/s)
    (ttable: 26/27777778 = 0.00% load, 0 hits, 0 misses, 25 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 3... d3 [+0.30]: 1.d2d4 g8f6 2.c1f4 
    (118 new nodes, 247 new qnodes, 0 qnode aborts, 5ms, 73kN/s)
    (ttable: 187/27777778 = 0.00% load, 0 hits, 0 misses, 161 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 4... d4 [+0.00]: 1.e2e4 g8f6 2.f1d3 b8c6 
    (1519 new nodes, 3044 new qnodes, 0 qnode aborts, 38ms, 119kN/s)
    (ttable: 2622/27777778 = 0.01% load, 0 hits, 0 misses, 2435 inserts (with 0 overwrites), 1 insert failures)
Searching at depth 5... d5 [+0.10]: 1.g2g3 g8f6 2.f1g2 b8c6 3.g2f3 
    (10895 new nodes, 35137 new qnodes, 0 qnode aborts, 251ms, 184kN/s)
    (ttable: 30441/27777778 = 0.11% load, 0 hits, 0 misses, 27819 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 6... d6 [-0.08]: 1.d2d4 g8f6 2.c1g5 b8c6 3.g5f6 g7f6 
    (88027 new nodes, 249718 new qnodes, 0 qnode aborts, 1281ms, 264kN/s)
    (ttable: 252536/27777778 = 0.91% load, 0 hits, 0 misses, 222095 inserts (with 0 overwrites), 27 insert failures)
Searching at depth 7... d7 [+0.15]: 1.e2e4 g8f6 2.d2d4 b8c6 3.d4d5 c6b4 4.g1f3 
    (417896 new nodes, 1966379 new qnodes, 0 qnode aborts, 8485ms, 281kN/s)
    (ttable: 1957490/27777778 = 7.05% load, 0 hits, 0 misses, 1704954 inserts (with 0 overwrites), 817 insert failures)

在测试位置时:

Calculating...
Iterative Deepening Analysis Results (including cached analysis)
Searching at depth 1... d1 [+2.25]: 3...g8h6 4.(q)c3d5 (q)d8d5 
    (1 new nodes, 3 new qnodes, 0 qnode aborts, 0ms, 23kN/s)
    (ttable: 3/27777778 = 0.00% load, 0 hits, 0 misses, 3 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 2... d2 [-0.13]: 3...f5e4 4.c3e4 (q)d5e4 
    (32 new nodes, 443 new qnodes, 0 qnode aborts, 3ms, 144kN/s)
    (ttable: 369/27777778 = 0.00% load, 0 hits, 0 misses, 366 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 3... d3 [+0.25]: 3...g8h6 4.c3e2 h6g4 
    (230 new nodes, 2664 new qnodes, 0 qnode aborts, 24ms, 122kN/s)
    (ttable: 2526/27777778 = 0.01% load, 0 hits, 0 misses, 2157 inserts (with 0 overwrites), 0 insert failures)
Searching at depth 4... d4 [-0.10]: 3...g8f6 4.e3e4 f5e6 5.f1b5 
    (2084 new nodes, 13998 new qnodes, 0 qnode aborts, 100ms, 162kN/s)
    (ttable: 15663/27777778 = 0.06% load, 0 hits, 0 misses, 13137 inserts (with 0 overwrites), 2 insert failures)
Searching at depth 5... d5 [+0.15]: 3...g8f6 4.f1e2 h8g8 5.g2g4 f5e4 6.(q)c3e4 (q)f6e4 
   (38987 new nodes, 1004867 new qnodes, 0 qnode aborts, 2765ms, 378kN/s)
   (ttable: 855045/27777778 = 3.08% load, 0 hits, 0 misses, 839382 inserts (with 0 overwrites), 302 insert failures)

【讨论】:

    【解决方案2】:

    我很乐意查看实际的 repo,但我在执行类似的游戏算法时多次遇到这个确切的问题。我会告诉你是什么导致了我的问题,你可以检查你是否犯了同样的错误。这些是按我认为最有可能解决您的问题的顺序列出的。

    层不是移动,每次迭代移动应该增加 2(这就是层)

    这个错误几乎总是表现在第一个玩家几乎每一步都做出错误的选择,因为他们永远看不到做出错误的举动的后果。避免这种情况的方法是增加 2 步(或者更一般地增加游戏中玩家的数量,但你使用的是 minmax,所以它是 2)。这可确保每个玩家始终在下一轮寻找后果。

    必须始终从当前玩家的角度进行评估

    这听起来很明显,但我发誓我每次实现评估函数时都会搞砸。在设计评估时,我们总是从第一个玩的玩家的角度来设计它,而我们应该做的是设计它以返回当前玩家的评估。我们可以知道轮到哪个玩家了,因为我们有全板状态,所以不需要传进去。

    如果您的评估调用不是您的 minmax 函数中的第一次调用,那么这尤其难以调试,但您已经以这种方式实现了它,所以这不是问题。

    评估函数必须是对称的

    当它发生时,这是一个特别讨厌的错误。这个想法是,同一个玩家会根据他们是赢还是输来以不同的方式评估相同的位置。

    以国际象棋为例,作为获胜者,您希望赢得最少的步数,但如果您要输,您希望输掉的步数最多。一个典型的解决方案是说,如果你要赢,则为在较少的移动中获胜添加奖金,但如果你要输,则为更长的序列添加奖金。这导致根据情况以相反的原因添加奖励,并从评估中删除对称性,使得玩家 A 不等于 - 玩家 B。当你失去这种对称性时,你不能再将值传递回游戏树,您必须在每一步重新评估它们。

    但诀窍在于,像这样进行调整总是错误的。通过深入的静态评估,如果它找到了有保证的胜利,它只会提前切断。通过迭代深化解决方案,它仍然会首先找到较早的胜利。除非对手犯错,否则 5 中的队友永远不会成为 4 中的队友,因此永远不需要这样的调整。

    仔细检查您的转置表没有发生冲突

    我看不到您的转置表的实现,但如果您处理的状态多于分配存储的状态,那么您必须确保它是相同的位置,然后才能信任该值。我怀疑这是一个问题,因为看起来您只查看了几百万个节点,但仔细检查总是好的。此外,请确保您的哈希函数足够随机以避免经常发生冲突。

    mtd_f不应该查阅转置表

    mtd_f 是一个传递函数,它将在第一次调用 negamax 时正确处理转置表。您正在不正确地使用它现在实现的值,但只需删除该代码即可清理实现并正确处理它。此外,您应该在每次迭代时将评估传递给 mtd_f 函数,而不是每次都尝试加载它。

    【讨论】:

    • 这是一个老问题,是的,但如果我理解正确,你的第一点肯定是不正确的。您不必在每次迭代时将深度增加 2 层。通过实现某种静态评估函数(即使只进行偶数迭代也应该实现),可以在奇数迭代中准确计算。
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多