Two fold or three fold repetition?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

Henk wrote: Sat Aug 21, 2021 7:04 pm I mean len = number of moves played from start of the game or last capture, castling etc.

If there was a repetition draw possible than many entries in TT poluted for they used the wrong value for that position when calculating their value.
Same holds for best move stored in these entries being the wrong best move.

I mean one wrong value may infect the whole tree.
O wait len must not be from last capture or last pawn move etc for this may change and then all depths of entries in TTable are wrong that is set too deep.

By the way only need to check when game history contains positions repeated twice.
Last edited by Henk on Sun Aug 22, 2021 1:26 pm, edited 1 time in total.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Two fold or three fold repetition?

Post by Sven »

I don't understand your "pollution" argument. If you never probe the TT in a position that is repeated (following the repetition definition that has now been mentioned several times in this thread) then you also never store a TT entry if the corresponding position is repeated. So where is the "pollution"?

I also don't understand the benefit you see in storing an additional "game length ..." integer. Where and how would you use it? Only for repetition detection? But a single integer does not tell you anything about the path to that position, it can still be a transposition of a node from a completely different subtree or even a different search. Repetition detection definitely requires iterating through the game history (with some small optimizations, like walking backwards in steps of two plies or starting at current ply - 4). And when doing so, you get that "game length ..." information for free as an intrinsic part of the loop. So I see absolutely no benefit in storing such an integer in the TT.

If you still think your idea about the handling of repetitions is right then I am curious to see some pseudo code ...
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

If a value of a position is wrong then values of all ancesters might be wrong for they used that value which used to be correct but now is incorrect for two moves has been played that made the value of that position a draw.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Two fold or three fold repetition?

Post by Sven »

You insist in not understanding what I write ...
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

What I understand is that you don't store a value of a move giving two fold repetition but you still give it back to its ancesters for its not yet three fold repetition. That means ancesters are polluted.
Last edited by Henk on Sun Aug 22, 2021 3:55 pm, edited 1 time in total.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Two fold or three fold repetition?

Post by Sven »

I don't. Where did I state that? I think I stated the opposite several times ... No TT probing when repetition was detected means neither returning a TT score nor storing one.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

Sven wrote: Sun Aug 22, 2021 3:54 pm I don't. Where did I state that? I think I stated the opposite several times ...
if not then you only do two fold repetition checks and I thought you were doing three fold repetition checks.

By the way I don't understand what the word probe means. Is that a get or a put?
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Two fold or three fold repetition?

Post by Sven »

Back to your last example:

[d]1k3r2/1P5R/6p1/4p3/6PP/8/P4b2/1R5K b - - 1 1

Some definitions:

After 1...e4 position P1 is reached for the first time: P1(1).
After 2.Rb5 Ba7 3.Rb1 Bf2, position P1 is reached for the second time: P1(2).
After playing 4. Rb5 Ba7 5.Rb1 Bf2, position P1 is reached for the third time: P1(3).

A)
P1(3) is a draw by FIDE rules. When reached during the game, both the GUI and the engine's game playing code should detect it as a three-fold repetition and handle it according to the rules (usually the game will end there).

B)
Now let's look at the case where the root of your search is somewhere on the path between P1(1) and P1(2), including the former but excluding the latter. At some point the search will reach P1(2). Since this is neither a two-fold rep during search nor a three-fold rep during the game, a normal subtree search will be performed, eventually returning a score which is then stored in the TT. That may be subject of your trouble but it should not be, see below!

If that search of P1(2) is deep enough it will also visit P1(3), detect it as first repetition during search (and second during game as well), and score it as a draw without further search. Repetition detection always goes first, so TT is not used here, i.e. no probing (reading) and also nothing to store. That draw score may or may not affect the search score of P1(2) (see previous paragraph of case B) in general. In the given position White should be winning, though, so P1(3) should not affect the PV of P1(2).

C)
Last case is when the root of your search is somewhere on the path between P1(2) and P1(3), again including the former but excluding the latter. At some point the search will reach P1(3). What happens? Well, it is as simple as the other cases: you always do the repetition detection first, no TT probing yet. So you find out that the current position does not repeat a tree position but it repeats a game position that had already been repeated => three-fold rep detected, return draw score. TT unused again.

Here is my repetition detection pseudo code:

Code: Select all

bool isRepetition(bool isRoot)
{
    int ply = currentPly();
    int stop = std::max<int>(ply - fifty(), 0);
    for (int i = ply - 4; i >= stop; i -= 2) {
        if (position[i].hashKey == currentHashKey()) {
            position[ply].nRepetitions = position[i].nRepetitions + 1;
            return position[ply].nRepetitions >= 2 || !isRoot;
        }
    }
}
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

I see in my code I don't store rootPosition in historyFromRoot. So that might have been the bug.

[
But then it's still possible for instance in hyperbullet games or timetrouble that search of P1(2) is not deep enough and it won't visit P1(3).
So it thinks P1(2) is safe to play but causing a three fold.

Or maybe it did not visit P1(3) because of futility pruning or some other (bad) pruning.
]
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

Whether you do futility pruning or not when not in check quiescence search skips moves causing three fold repetition at depth 0 for they are no captures.

Pollution starts at quiescence search. But humans can't play chess so they won't notice.

O wait maybe I should repair that too. Looking for quiet moves at depth 0 causing three fold repetition.