tt and mate scoring bug

Discussion of chess software programming and technical issues.

Moderator: Ras

ericlangedijk
Posts: 56
Joined: Thu Aug 08, 2013 5:13 pm

tt and mate scoring bug

Post by ericlangedijk »

Since the beginning my engine had a matescoring bug.
I hunted the source of that problem: using a tt score as an eval inside the search instead of the static evaluation.
I got that idea from (many) other chess engines.
This eval is then used for the logic of razoring, reversed futility pruning, nullmove pruning.
Something like this:

Code: Select all

            // use tt score as a better eval.
            if (ttHit
                && (ttEntry.bound == EXACT
                    || (ttEntry.bound == LOWER_BOUND && ttData.score >= stack->eval)
                    || (ttEntry.bound == UPPER_BOUND && ttData.score <= stack->eval)))
                stack->eval = ttEntry.score;
When I use this logic in my engine sometimes with deep mates it reports a pv with a ridiculous mate score far beyond the max searchdepth.

Any wizard around here who knows why, what and how to solve this?
(I saw 1 or 2 engines during my play-tests having the same bug).
User avatar
hgm
Posts: 28481
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: tt and mate scoring bug

Post by hgm »

Mate scores beyond the search depth are a normal phenomenon (known as 'grafting') when you allow overdeep hash hits to satisfy search requests.

E.g. in the following position
[d]8/8/8/3k4/R7/2K5/8/8 w - - 0 1
Fairy-Max gives the following analysis:

28 #13 156.8M 1:09.35 a4a5 d5d6 c3d4 d6e6 a5d5 e6f6 d5e5 f6g6 d4e4 g6f6 e4f4 f6g6 e5f5 g6h6 f5g5 h6h7 g5g2 h7h6
27 #13 100.0M 0:44.62 a4a5 d5d6 c3d4 d6e6 a5d5 e6f6 d5e5 f6g6 d4e4 g6f6 e4f4 f6g6 e5e6 g6g7 f4g5 g7f7 e6e5
26 #13 75.4M 0:33.59 a4a5 d5d6 c3d4 d6e6 a5d5 e6f6 d5e5 f6g6 d4e4 g6f6 e4f4 f6g6 e5e6 g6g7 f4g5 g7f7 e6e5
25 #14 37.8M 0:17.57 a4a5 d5e6 c3d4 e6d6 a5c5 d6e6 c5d5
24 #15 19.7M 0:09.25 a4a5 d5d6 c3d4 d6e6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4a5 e5f4 a5c5 f4f3 c5c4 f3f2 c4f4 f2g2 d3e4 g2g3 e4e3 g3h3
15 +4.54 551486 0:00.21 c3d3 d5e5 a4a5 e5f4 a5c5 f4f3 c5c4 f3f2 d3e4 f2g2 c4c3 g2f2 c3b3
14 +4.54 359309 0:00.14 c3d3 d5e5 a4a5 e5f4 a5c5 f4f3 c5c4 f3f2 d3e4 f2g2 c4c3 g2f2
13 +4.52 209668 0:00.07 c3d3 d5e5 a4a5 e5f4 a5b5 f4f3 b5b4 f3f2 d3e4 f2g3 e4e3
12 +4.52 149534 0:00.06 c3d3 d5e5 a4a5 e5f4 a5b5 f4f3 b5b4 f3f2 d3e4 f2g3 e4e3
11 +4.51 93373 0:00.03 c3d3 d5e5 a4a5 e5f4 a5b5 f4g3 d3e4 g3g4 b5c5 g4g3 e4e5
10 +4.51 52561 0:00.01 c3d3 d5e5 a4a5 e5f4 a5b5 f4g3 d3e4 g3g4 b5c5 g4g3
9 +4.50 37186 0:00.01 c3d3 d5e5 a4a5 e5f4 a5b5 f4g3 d3e4 g3g4 e4e5
8 +4.48 18682 0:00.00 c3d3 d5e5 a4a5 e5f4 d3d4 f4f3 d4e5 f3e3
7 +4.49 7438 0:00.00 c3d3 d5e5 a4a5 e5f4 d3d4 f4f3 d4e5
6 +4.48 4338 0:00.00 c3d3 d5e5 a4a5 e5f4 d3d4 f4f3
5 +4.47 1853 0:00.00 c3d3 d5e5 d3e3 e5f5 e3d4
4 +4.47 421 0:00.00 c3d3 d5e5 d3e3 e5f5
3 +4.47 191 0:00.00 c3d3 d5e5 d3e3
2 +4.46 14 0:00.00 c3d3 d5e5
1 +4.47 7 0:00.00 c3d3
0 #

So it already finds a way to force mate in 28 at a search depth of 20 ply, while King capture for such a mate would occur only on ply 57. So the grafting nearly triples the search depth.
ericlangedijk
Posts: 56
Joined: Thu Aug 08, 2013 5:13 pm

Re: tt and mate scoring bug

Post by ericlangedijk »

Ok that is kinda understandable. My maximum searchdepth is 128. And I get scores beyond that, sometimes larger than mate in 200 ply.
So I am not sure if that is hurting or a bug. What I do know is that when the engine starts spawning these huge mates, a mate is always there and it gets smaller soon until the real mate in X.
I disabled the "use tt score as eval" for now and I did only notice an increase of around +15 ELO. And it seems more stable.
Aleks Peshkov
Posts: 990
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia
Full name: Aleks Peshkov

Re: tt and mate scoring bug

Post by Aleks Peshkov »

There are too distinct kinds of evaluation: 1) positional -- traditionally measured in centipawns and 2) mate scores counted in plies (halfmoves). Encoding two distinct value types into one number is source of confusion. Traditional static evaluation returns former (centipawn score), various search heuristicts like futility pruning also make sense only with former. Using positional evaluation when side to move king is in check is source of problems. Adding plies to centipawns and adding centipawns to mate scores is undefined operation and nonsense in strict sense.

Code: Select all

if (!inCheck() && ttScore.isEval()) {
    if (ttBound == ExactScore
        || (ttBound == FailHigh && eval <= ttScore)
        || (ttBound == FailLow && ttScore <= eval)
    ) {
        eval = ttScore;
    }
}
ericlangedijk
Posts: 56
Joined: Thu Aug 08, 2013 5:13 pm

Re: tt and mate scoring bug

Post by ericlangedijk »

Yes. Matescores are a range of its own.
syzygy
Posts: 5948
Joined: Tue Feb 28, 2012 11:56 pm

Re: tt and mate scoring bug

Post by syzygy »

ericlangedijk wrote: Wed Apr 01, 2026 5:03 pm I disabled the "use tt score as eval" for now and I did only notice an increase of around +15 ELO. And it seems more stable.
+15 ELO sounds like an acceptable trade-off ;-)