Draw by 3-fold repetition?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Draw by 3-fold repetition?

Post by bob »

hgm wrote:He doesn't use the hash table. Just the hash key. Most engines store a small array of hash keys since the last irreversible move, through which they then do a linear search. You could make a small dedicated hash table for this too (which for games without irreversiblemoves is probably advisable)..
Some have used a hash table for repetitions. Bruce Moreland was the first I had heard this from. Idea is that when you enter a position, you look for a repetition match. If present, increment the included rep counter and do what you want (most call 2 reps a draw, but you can be more careful and only allow 3 reps near the root if you want (I do not like the idea, but some have used it.) If you do the probe and find nothing, you enter the signature and a count of one. When you return from this ply, you decrement the counter and if zero, remove the entry. I think he used a table of 2K entries.

I didn't like the idea because one has to copy all of that stuff in a parallel search so that each thread has its own repetition hash,... but it seems to work just fine. It eliminates the loop to look for repetitions, but it means that for each node you do a "probe" and a "delete" (Bruce called them "open" and "close" operations.) I don't think there is any difference in terms of speed.
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Draw by 3-fold repetition?

Post by hgm »

I did it exactly like that in Joker. It could make a difference in games without irreversible moves, like Crazyhouse, where you would always have to search back to the beginning of the game.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Draw by 3-fold repetition?

Post by bob »

hgm wrote:I did it exactly like that in Joker. It could make a difference in games without irreversible moves, like Crazyhouse, where you would always have to search back to the beginning of the game.
As a human player that has played thousands of bughouse/crazyhouse games, I just realized that we never even thought about the 50 move rule, although some games did end in perpetuals. Never even occurred to me, in fact. Ditto for repetitions except for those that are simply 3 straight repetitions. Is there actually a rule for this? After a drop and capture I suppose you can repeat with a previous position where the capturing piece moved to that same square when it was empty or contained another piece?

Not particularly important, but I had never even though about it as I played, and Bert Gower and I were pretty damned good at this game as communication is as important as chess skill (what do you need? or "give me a knight whatever it costs")
User avatar
trojanfoe
Posts: 65
Joined: Sun Jul 31, 2011 11:57 am
Location: Waterlooville, Hampshire, UK

Re: Draw by 3-fold repetition?

Post by trojanfoe »

bob wrote: Some have used a hash table for repetitions.
Actually I just use a C++ std::vector object to hold the hash keys from previous positions. When looking for 3-fold rep draws I simply search the vector and count how many times the current position has occurred. Note that my test program is a not a chess engine, just something that lets two engines plays chess and has to adjudicate game endings as they cannot do such things themselves.

Cheers,
Andy
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Draw by 3-fold repetition?

Post by sje »

bob wrote:Some have used a hash table for repetitions. Bruce Moreland was the first I had heard this from. Idea is that when you enter a position, you look for a repetition match. If present, increment the included rep counter and do what you want (most call 2 reps a draw, but you can be more careful and only allow 3 reps near the root if you want (I do not like the idea, but some have used it.) If you do the probe and find nothing, you enter the signature and a count of one. When you return from this ply, you decrement the counter and if zero, remove the entry. I think he used a table of 2K entries.
If I recall correctly, Ken Thompson long ago used the main and only transposition table in Belle to recognize repeated positions in the search. Upon entering a node for the first time, it was stored in the transposition table with a flag saying "current variation position". Upon exiting the node, the flag was reset. Upon entering the same node a second time by a deeper path the flag was checked and if set, the node was scored as a draw. The controlling program in the attached pdp-11 had the logic for detecting threefold at ply one. It all worked, although there was a slight risk that a table element overwrite could cause a false negative and a repetition would be missed.
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Draw by 3-fold repetition?

Post by hgm »

trojanfoe wrote:Actually I just use a C++ std::vector object to hold the hash keys from previous positions. When looking for 3-fold rep draws I simply search the vector and count how many times the current position has occurred. Note that my test program is a not a chess engine, just something that lets two engines plays chess and has to adjudicate game endings as they cannot do such things themselves.
Well, as long as you flawlessly incorporate all rights in the key, (e.g. no phantom e.p. rights) this should work. But that is of course exactly what you should test. Assuming things work properly because they were designed to work properly is no substitute for testing.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CookieCat's repetition detection

Post by sje »

Here is CookieCat's repetition detection code. It's fairly simple and I don't think it can be made to run much faster.

Code: Select all

  function PosCalcRepCount(var pos: postype; limit: Integer): Integer;
    var
      myresult: Integer;
      count: hmvctype;
      spevptr: spevptrtype;
  begin
    with pos do
      begin
        myresult := 0;
        count := 0;
        spevptr := usedspevlist.tail;
        while &#40;myresult < limit&#41; and &#40;count < hmvc&#41; and &#40;spevptr <> nil&#41; do
          begin
            if Odd&#40;count&#41; then
              if HashEq&#40;spevptr^.spevmphc, mphc&#41; then
                Inc&#40;myresult&#41;;
            Inc&#40;count&#41;;
            spevptr &#58;= spevptr^.prev
          end
      end;
    PosCalcRepCount &#58;= myresult
  end; &#123; PosCalcRepCount &#125;

  function PosIsRepeated&#40;var pos&#58; postype&#41;&#58; Boolean; inline;
    var
      myresult&#58; Boolean;
  begin
    if pos.hmvc < 4 then
      myresult &#58;= False
    else
      myresult &#58;= PosCalcRepCount&#40;pos, 1&#41; = 1;
    PosIsRepeated &#58;= myresult
  end; &#123; PosIsRepeated &#125;

  function PosIsDrawByRepetition&#40;var pos&#58; postype&#41;&#58; Boolean; inline;
    var
      myresult&#58; Boolean;
  begin
    if pos.hmvc < 8 then
      myresult &#58;= False
    else
      myresult &#58;= PosCalcRepCount&#40;pos, 2&#41; = 2;
    PosIsDrawByRepetition &#58;= myresult
  end; &#123; PosIsDrawByRepetition &#125;
User avatar
trojanfoe
Posts: 65
Joined: Sun Jul 31, 2011 11:57 am
Location: Waterlooville, Hampshire, UK

Re: Draw by 3-fold repetition?

Post by trojanfoe »

hgm wrote:Well, as long as you flawlessly incorporate all rights in the key, (e.g. no phantom e.p. rights) this should work. But that is of course exactly what you should test. Assuming things work properly because they were designed to work properly is no substitute for testing.
I played a 100-game tourney between komodo and stockfish and 22 of the games were adjudicated as draws by 3-fold repetition, which seems quite high to me!

I'm just about to go through the games to see if there is anything suspicious.

I've uploaded the PGN file to https://gist.github.com/1593933, if anyone's interested.
User avatar
natasha
Posts: 145
Joined: Tue Jan 25, 2011 3:10 pm

Re: Draw by 3-fold repetition?

Post by natasha »

trojanfoe wrote:
hgm wrote:Well, as long as you flawlessly incorporate all rights in the key, (e.g. no phantom e.p. rights) this should work. But that is of course exactly what you should test. Assuming things work properly because they were designed to work properly is no substitute for testing.
I played a 100-game tourney between komodo and stockfish and 22 of the games were adjudicated as draws by 3-fold repetition, which seems quite high to me!

I'm just about to go through the games to see if there is anything suspicious.

I've uploaded the PGN file to https://gist.github.com/1593933, if anyone's interested.[/quot


thank you were number of those drawn games not that many moves . under 50 moves :?:
Silent gratitude isn’t very much to anyone.
User avatar
natasha
Posts: 145
Joined: Tue Jan 25, 2011 3:10 pm

Re: Draw by 3-fold repetition?

Post by natasha »

natasha wrote:
trojanfoe wrote:
hgm wrote:Well, as long as you flawlessly incorporate all rights in the key, (e.g. no phantom e.p. rights) this should work. But that is of course exactly what you should test. Assuming things work properly because they were designed to work properly is no substitute for testing.
I played a 100-game tourney between komodo and stockfish and 22 of the games were adjudicated as draws by 3-fold repetition, which seems quite high to me!

I'm just about to go through the games to see if there is anything suspicious.

I've uploaded the PGN file to https://gist.github.com/1593933, if anyone's interested.

thank you were number of those3 fold drawn games not that many moves . under 50 moves :?:
Last edited by natasha on Wed Jan 11, 2012 11:05 am, edited 1 time in total.
Silent gratitude isn’t very much to anyone.