Beta search truncating my principal variation

I have implemented an alpha-beta search that adds its results to a transposition table. Then, I am extracting the principal variation from the transposition table.

This appears to work alright for analysis at shallow depths. However, when I ask for analysis at a depth of 7 plies, I get this:

7 [+1.00] 1.b1c3 a7a6 2.g1f3 a6a5 3.a6a5 

At the end, a move is repeated. This final move is placed in the table as a result of pruning, but it is not even a legal move for white. Obviously, there are less than 7 plies printed.

Is this a misunderstanding in my alpha-beta search code?

int ab_max(board *b, int alpha, int beta, int ply) {
    if (ply == 0) return evaluate(b);
    int num_children;
    move chosen_move = no_move;
    move *moves = board_moves(b, &num_children);
    assert(num_children > 0);
    for (int i = 0; i < num_children; i++) {
        apply(b, moves[i]);
        int score = ab_min(b, alpha, beta, ply - 1);
        if (score >= beta) {
            tt_put(b, (evaluation){moves[i], score, at_least, ply});
            unapply(b, moves[i]);
            free(moves);
            return beta; // fail-hard
        }
        if (score > alpha) {
            alpha = score;
            chosen_move = moves[i];
        }
        unapply(b, moves[i]);
    }
    tt_put(b, (evaluation){chosen_move, alpha, exact, ply});
    free(moves);
    return alpha;
}

int ab_min(board *b, int alpha, int beta, int ply) {
    if (ply == 0) return evaluate(b);
    int num_children;
    move chosen_move = no_move;
    move *moves = board_moves(b, &num_children);
    assert(num_children > 0);
    for (int i = 0; i < num_children; i++) {
        apply(b, moves[i]);
        int score = ab_max(b, alpha, beta, ply - 1);
        if (score <= alpha) {
            tt_put(b, (evaluation){moves[i], score, at_most, ply});
            unapply(b, moves[i]);
            free(moves);
            return alpha; // fail-hard
        }
        if (score < beta) {
            beta = score;
            chosen_move = moves[i];
        }
        unapply(b, moves[i]);
    }
    tt_put(b, (evaluation){chosen_move, beta, exact, ply});
    free(moves);
    return beta;
}

This is the interesting part of my evaluation printing function:

do {
        if (!b->black_to_move) printf("%d.", moveno++);
        char move[6];
        printf("%s ", move_to_string(eval->best, move));
        apply(b, eval->best);
        eval = tt_get(b);
    } while (eval != NULL && depth-- > 0);

No move in my principal variation should ever be pruned, right?


I am answering my own question to save future chess authors several hours of grief.

There is a trivial bug in which, when a cutoff occurs, I unapply the move too late.

However, the more interesting problem is described here: Chess: Extracting the principal variation from the transposition table

链接地址: http://www.djcxy.com/p/56334.html

上一篇: 国际象棋:从转位表中提取主要差异

下一篇: Beta搜索截断了我的主要变体