repetition draw bug

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Some minor ambiguities concerning game termination

Post by hgm »

Volker Annuss wrote:Checkmate, stalemate and insufficient material terminate the game immediately by rule whereas one has to claim a draw by fifty move rule or repetition else the game will continue. So insufficient material should have a higher priority than fifty move rule and repetition.
That was my first thought too. Until I realized that when there is both a 50-move and an insufficient material draw, the material must have already been insufficient for 50 moves...
User avatar
stevemulligan
Posts: 117
Joined: Wed Jul 20, 2011 2:54 pm
Location: Ottawa, Canada

Re: Some minor ambiguities concerning game termination

Post by stevemulligan »

sje wrote: The priority ranking is:

1) Checkmate
2) Stalemate
3) Fifty move rule
4) Repetition rule
5) Insufficient material rule

My results still don't match up after accounting for the priority and matching your insufficient material function as best I could.

Code: Select all

rgstat 10000
 Checkmate: 2815 (0.2815)
 FiftyMoves: 1048 (0.1048)
 Insufficient: 5858 (0.5858)
 Repetition: 96 (0.0096)
 Stalemate: 183 (0.0183)
Elapsed Time 82.636s (121.0126)

I'm assuming checkmate/stalemate code is working because my perft scores are matching in a large test suite I have.

For 50 move rule, I manage the half move clock like this (reset on pawn move or capture)

Code: Select all

if ((pieceKind[mdest] == PieceType.Pawn) || (CapturedPieceType != PieceType.All) || (LastMoveType == MoveTypeAI.Promotion))
                DrawCount = 0;
            else
                DrawCount++;

For repetition I use zobrist hashes in a dictionary so I know if a position has occurred 3 times in the game.

The only place I think I might have made an error is insufficient material, yet that # is the closet match

Code: Select all

public bool NewSufficientMaterial()
{
   return (NewSufficientMaterialByColor(PlayerColor.White) || NewSufficientMaterialByColor(PlayerColor.Black));
}

public bool NewSufficientMaterialByColor(PlayerColor color)
{
    if ((GetBB(color, PieceType.Pawn) > 0) || (GetBB(color, PieceType.Queen) > 0) || (GetBB(color, PieceType.Rook) > 0))
                return true;

    List<ulong> k, b;

    k = Util.SplitBB&#40;GetBB&#40;color, PieceType.Knight&#41;);
    b = Util.SplitBB&#40;GetBB&#40;color, PieceType.Bishop&#41;);
            
    if (&#40;b.Count >= 2&#41; || &#40;k.Count >= 3&#41;)
                return true;

    if (&#40;b.Count > 0&#41; && &#40;k.Count > 0&#41;)
                return true;

    return false;
&#125;
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Some minor ambiguities concerning game termination

Post by sje »

stevemulligan wrote:My results still don't match up after accounting for the priority and matching your insufficient material function as best I could.

Code: Select all

rgstat 10000
 Checkmate&#58; 2815 &#40;0.2815&#41;
 FiftyMoves&#58; 1048 &#40;0.1048&#41;
 Insufficient&#58; 5858 &#40;0.5858&#41;
 Repetition&#58; 96 &#40;0.0096&#41;
 Stalemate&#58; 183 &#40;0.0183&#41;
Elapsed Time 82.636s &#40;121.0126&#41;
The checkmate count is nearly double the 15.2% expected. I have FEN files of 100,000 checkmates and 100,000 stalemates and I can send them to you if you think they might be helpful.
User avatar
stevemulligan
Posts: 117
Joined: Wed Jul 20, 2011 2:54 pm
Location: Ottawa, Canada

Re: Some minor ambiguities concerning game termination

Post by stevemulligan »

sje wrote: The checkmate count is nearly double the 15.2% expected. I have FEN files of 100,000 checkmates and 100,000 stalemates and I can send them to you if you think they might be helpful.
Thx for sending them. All the checkmates & stalemates match up for me.

I'm going to try saving the fen of all the so-called checkmates from random games and verify them using this same method.
User avatar
stevemulligan
Posts: 117
Joined: Wed Jul 20, 2011 2:54 pm
Location: Ottawa, Canada

Re: Some minor ambiguities concerning game termination

Post by stevemulligan »

sje wrote: The checkmate count is nearly double the 15.2% expected.
Could differences in the prng be an issue? I can't find any problems in my draw conditions, perft & mate tests all pass.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Some minor ambiguities concerning game termination

Post by sje »

stevemulligan wrote:
sje wrote: The checkmate count is nearly double the 15.2% expected.
Could differences in the prng be an issue?
Only if your PRNG is seriously broken. My guess is that there's a bug with the repetition code.
User avatar
stevemulligan
Posts: 117
Joined: Wed Jul 20, 2011 2:54 pm
Location: Ottawa, Canada

Re: Some minor ambiguities concerning game termination

Post by stevemulligan »

Ok, making progress. How close do I have to be to your #'s with a million samples to be confident I have gotten rid of any repetition/draw bugs?

Code: Select all

rgstat 1000000
Summary&#58;
  Checkmate 153,174 &#40;0.153174&#41;
  FiftyMoves 193,469 &#40;0.193469&#41;
  Insufficient 567,328 &#40;0.567328&#41;
  Repetition 25,273 &#40;0.025273&#41;
  Stalemate 60,756 &#40;0.060756&#41;
Count&#58; 1,000,000   Elapsed&#58; 3485.771  &#40;286.88 Hz / 0.003485771 s&#41;
It's my first time reading LISP so I'm not sure on these two points:
  • 1. I'm matching your insufficient material criteria as best I can. To get similar results this criteria needs to match exactly right?

    2. In the CIL.lisp file you sent me it looks like the priority is 50 move, insufficient, repetition based on defun pos->pdgt. Am I reading that right? Asking because you said in this thread you checked repetition before material. (That's how I'm doing it, and I get closer to the #'s you posted when I do that - I just wanted to check if I was reading the LISP code correctly)
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Some minor ambiguities concerning game termination

Post by sje »

stevemulligan wrote:Ok, making progress. How close do I have to be to your #'s with a million samples to be confident I have gotten rid of any repetition/draw bugs?

It's my first time reading LISP so I'm not sure on these two points:
  • 1. I'm matching your insufficient material criteria as best I can. To get similar results this criteria needs to match exactly right?

    2. In the CIL.lisp file you sent me it looks like the priority is 50 move, insufficient, repetition based on defun pos->pdgt. Am I reading that right? Asking because you said in this thread you checked repetition before material. (That's how I'm doing it, and I get closer to the #'s you posted when I do that - I just wanted to check if I was reading the LISP code correctly)
I'd try for at least a million games to give at least three significant digits of agreement.

Assuming that the program can reliably detect game termination, the priority order won't matter much, maybe less than one in several thousand games.

I think you've got the insufficient material code working okay.

Maybe someone needs to publish a repetition detection test suite. This would be a collection of PGN data with games with know game termination including repetition draws.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Some minor ambiguities concerning game termination

Post by sje »

Here is the game termination assignment code from Bozochess:

Code: Select all

function poscalcgt&#40;var pos&#58; postype&#41;&#58; gttype;
var
  result&#58; gttype;
begin
  with pos do
    begin
      if posisnomoves&#40;pos&#41; then
        if inch then result &#58;= gtcheckmate else result &#58;= gtstalemate
      else
        if posisfiftymovesdraw&#40;pos&#41; then
          result &#58;= gtfiftymoves
        else
          if posisinsufficientdraw&#40;pos&#41; then
            result &#58;= gtinsufficient
          else
            if posisrepetitiondraw&#40;pos&#41; then
              result &#58;= gtrepetition
            else
              result &#58;= gtunterminated;
      poscalcgt &#58;= result;
    end;
end; &#123; poscalcgt &#125;
User avatar
stevemulligan
Posts: 117
Joined: Wed Jul 20, 2011 2:54 pm
Location: Ottawa, Canada

Re: Some minor ambiguities concerning game termination

Post by stevemulligan »

sje wrote:Maybe someone needs to publish a repetition detection test suite. This would be a collection of PGN data with games with know game termination including repetition draws.
Are there any compiled linux apps that I will play a random game and save to PGN format? I could run that and collect a big library of repetitions/50 move pgn's. If left to my own devices I'm gonna fire up Perl again :p