Two fold or three fold repetition?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

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 »

QS and FP are unrelated to your repetition detection issue. I suggest to unconfuse your thoughts ... 😉 Just fixing the bugs according to the input you got here should help.
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 only check for two fold repetitions now. Three fold repetition checks does not work in my implementation. Too difficult to find out what goes wrong. So I resign. (also waste of time)

Somehow P1(2) is stored in TTable when it did not occur twice in the game history and not present in the history from root.

So I now check only if a position occurs once or is not present in the game history and not present in the history from root.
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

O wait that's wrong. Must be:

if position present in game history or present in the history from root then it is 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 »

Henk wrote: Wed Aug 25, 2021 12:03 pm Somehow P1(2) is stored in TTable when it did not occur twice in the game history and not present in the history from root.
Why should it be a problem that P1(2) is stored in TT? See my "section B)" ... That *does not matter* for repetition detection since you *must not* read the TT before having ensured that the current position is not repeated ...

Should I introduce a counter for the number of times that I posted this? :roll:
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 am only talking nonsense. This is too complcated for me and will be waste of time.

I know it picks up somehow a wrong value from TT. You say that is impossible but my computer shows that it is possible.
But my source code giving no clue.
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Two fold or three fold repetition?

Post by amanjpro »

Henk wrote: Wed Aug 25, 2021 2:38 pm I am only talking nonsense. This is too complcated for me and will be waste of time.

I know it picks up somehow a wrong value from TT. You say that is impossible but my computer shows that it is possible.
But my source code giving no clue.
It is clear by now this thread cannot solve your issue Henk, is your code available somewhere that we can look at and give feedback?
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

Not much magic. This code returns true if it thinks it is a draw:

Code: Select all

           var count = historyFromRoot.Get(keyRep);
            if (count >= 1)
            {
                return true;
            }

            var countGameHistory = gameHistory.Get(keyRep);

            int nOccurences = threeFoldRep ? 2 : 1;
            if (countGameHistory >= nOccurences)
            {
                return true;
            }
What can go wrong? Maybe zobristkey representation wrong value??

If I set threeFoldRep to false. It all works fine. No bad repetition draws. Misery starts when threeFoldRep == true;




Here it is called at start of PVS.

Code: Select all

     if (depth >= 0 && plyCount > 0 )
            {
                if (Board.IsaDraw(true, GamePhase, key.Rep, HistoryFromRoot, PositionHistory))
                {
                    return 0;
                }
            }

What can go wrong. Maybe remove plyCount > 0 ??
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

Only had to play one game to get a repetition draw. With threeFoldRep = true;

[pgn]
[Event "Computer Chess Game"]
[Site "LAPTOP-1FK7MTIP"]
[Date "2021.08.25"]
[Round "-"]
[White "Skipper_8_17"]
[Black "Skipper_8_16"]
[Result "1/2-1/2"]
[TimeControl "60"]
[Annotator "1. +0.26 1... -0.05"]

1. d4 {+0.26/8} Nf6 {-0.05/8 1.0} 2. Nh3 {+0.44/8 1.0} e6 {+0.23/9 1.0} 3.
Nc3 {+0.06/8 1.0} Nc6 {+0.16/8 1.0} 4. e4 {+0.22/9 1.0} Bb4 {+0.01/7 1.0}
5. e5 {+0.31/8 1.0} Nd5 {+0.35/8 1.0} 6. Bd2 {+0.90/8 0.9} O-O
{+0.17/7 0.9} 7. Nxd5 {+1.35/9 0.9} Bxd2+ {+0.06/9 0.9} 8. Qxd2
{+1.06/9 0.9} exd5 {+0.09/9 0.9} 9. c4 {+1.03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+ {+0.00/13 0.4} 65. Kc3 {+5.62/9 0.4} Rg3+
{+0.00/14 0.4}
{XBoard adjudication: repetition draw} 1/2-1/2
[/pgn]

Skipper_8_16 sees the draw but 8_17 does not.
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Two fold or three fold repetition?

Post by amanjpro »

Henk wrote: Wed Aug 25, 2021 3:44 pm Not much magic. This code returns true if it thinks it is a draw:

Code: Select all

           var count = historyFromRoot.Get(keyRep);
            if (count >= 1)
            {
                return true;
            }

            var countGameHistory = gameHistory.Get(keyRep);

            int nOccurences = threeFoldRep ? 2 : 1;
            if (countGameHistory >= nOccurences)
            {
                return true;
            }
What can go wrong? Maybe zobristkey representation wrong value??

If I set threeFoldRep to false. It all works fine. No bad repetition draws. Misery starts when threeFoldRep == true;




Here it is called at start of PVS.

Code: Select all

     if (depth >= 0 && plyCount > 0 )
            {
                if (Board.IsaDraw(true, GamePhase, key.Rep, HistoryFromRoot, PositionHistory))
                {
                    return 0;
                }
            }

What can go wrong. Maybe remove plyCount > 0 ??
Sorry for asking the obvious, but do you ever update gameHistory counts? Can you show that part of the code?
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: Two fold or three fold repetition?

Post by Henk »

positionHistory and gameHistory are aliases.

Btw. Occupied(move.End.BitBoard) could be replaced by captureLocation != null

Code: Select all

  
	public void Next(IMoveBase move, IPositionHistory positionHistory)
        {
            Assert(move != null);

            var movingPieceKind = PieceKind(move.Start.BitBoard);
            var captureLocation = move.GetCaptureLocation(this);
            var captureKind = captureLocation == null ? None_Kind : PieceKind(captureLocation.BitBoard);

            if (movingPieceKind == Pawn || Occupied(move.End.BitBoard) || move is LongCastling || move is ShortCastling)
            {         
                if (positionHistory != null)
                    positionHistory.Clear();
            }


            if (captureLocation == null)
            {
                NonCaptureMove(move, this.CurPlayer);
            }
            else
            {
                Capture(move, this.CurPlayer, captureKind);
            }

            if (positionHistory != null)
            {
                var key = new ZobristKey(this, ZobristTable.Instance);
                positionHistory.Add(key.Rep);
            }

        }
  

Code: Select all

    public class PositionHistory :  IPositionHistory 
    {
        readonly Dictionary<ulong, int> rep;



   	public void Add(ulong key)
        {
            if (rep.ContainsKey(key))
            {
                rep[key]++;
            }
            else
            {
                rep.Add(key, 1);
            }
        }
Last edited by Henk on Wed Aug 25, 2021 4:50 pm, edited 1 time in total.