How to test 3rd repetition rule?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: How to test 3rd repetition rule?

Post by stegemma »

This a position from one of my games against an IM (it was a simultaneous against many players, I was black):

[D]3Q4/kp4p1/2p1r3/8/P2P4/2PP1PK1/4r2P/8 b - a3 0 40

I've played Rg6+ and after Kf4 Rf6+ Kg3 Rg6+ it was a draw.

The position is interesting because is a real position and white has 3 pawns more than black, if we suppose Q = RR.

PS: here's the whole game:

[pgn]
[Event "Simultanea Accademia Virgiliana"]
[Site "Mantova ITALY"]
[Date "2000.09.30"]
[Round "-"]
[White "Bruno Belotti (IM)"]
[Black "Stefano Gemma (2N)"]
[Result "1/2-1/2"]
1. e4 e5 2. f4 exf4 3. Nf3 d5 4. exd5 Qxd5 5. Nc3 Qf5 6. d4 Bb4 7. Bd3
Bxc3+ 8. bxc3 Qe6+ 9. Kf2 Nf6 10. Re1 Ng4+ 11. Kg1 Ne3 12. Bxe3 fxe3 13.
Qc1 O-O 14. Bxh7+ Kh8 15. Rxe3 Qf6 16. Bd3 Bf5 17. Ne5 Bxd3 18. cxd3 Kg8
19. Rf3 Qe7 20. Qf4 Nd7 21. Nxd7 Qxd7 22. Rh3 Qe7 23. Qf3 c6 24. Qh5 f6 25.
Qh7+ Kf7 26. Rg3 Rg8 27. Rf1 Rae8 28. Qg6+ Kf8 29. Rxf6+ Qxf6 30. Rf3 Qxf3
31. gxf3 Ke7 32. Qg5+ Kd7 33. Kf2 Re7 34. Qf5+ Kc7 35. Qc5 Rge8 36. Qxa7
Re2+ 37. Kg3 R8e6 38. Qa5+ Kb8 39. Qd8+ Ka7 40. a4 Rg6+ 41. Kf4 Rf6+ 42.
Kg3 Rg6+
1/2-1/2
[/pgn]
Author of Drago, Raffaela, Freccia, Satana, Sabrina.
http://www.linformatica.com
Stan Arts
Posts: 179
Joined: Fri Feb 14, 2014 10:53 pm
Location: the Netherlands

Re: How to test 3rd repetition rule?

Post by Stan Arts »

pkumar wrote: Do you consider castling rights in evaluation followed by stand-pat in Qsearch?
No. I evaluate the king's position and castling rights objectively so that "manual" castling is also encouraged when rights are lost but the resulting position is scored the same. A bonus for both rooks being over to the middle from the king and the king feeling quite unsafe in the middle makes it really want to castle and prevent castling rights being lost when possible.

Long ago this was probably problematic as with very shallow searches you can have for example a kf1 show up when you can't see deep enough that you can castle a few moves later. But nowadays it seems to work well also depending on weights a bit..Nemeton gets it much better than Neurosis that did kf1 things a little too often..
pkumar
Posts: 100
Joined: Tue Oct 15, 2013 5:45 pm

Re: How to test 3rd repetition rule?

Post by pkumar »

No. I evaluate the king's position and castling rights objectively so that "manual" castling is also encouraged when rights are lost but the resulting position is scored the same. A bonus for both rooks being over to the middle from the king and the king feeling quite unsafe in the middle makes it really want to castle and prevent castling rights being lost when possible.
I am thinking of two leaf positions with king at the centre which look identical but have diferent castling rights. The king is unsafe but more so without castling rights as "manual" castling takes more moves. So scoring should be different. It seems easier with explicit use of castling rights in scoring - perhaps you are already doing so.
flok

Re: How to test 3rd repetition rule?

Post by flok »

Luis Babboni wrote:I´m working in add the 3rd repetition rule recognition by my engine.
I did a pair of tests I could imagine and seems to work, but I´m still not happy, I need more tests to feel sure it works.
There are some place where to find positions to test if your engine recognizes the 3rd repetition rule?
Wich is the usual way to test this?
Thanks!
What is the way to prevent this at all?

I have an std::map<uint64_t, int> in which I keep track of the zobrist-hashes in the current pv I'm working at. The int is a counter and when that reaches 3 at some point in the search-tree, then I use as a score (instead of calling search()).
Still tons (at least half of the games) of games end in a draw due to move repetition.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

This should be in the wiki

Post by sje »

This should be in the wiki, if it's not already there.

The easiest way to test game termination detection is to add a chunk of code to generate lots of games, each made from randomly selected moves, and keep track of the counts of the different game termination kinds.

Generate a million such games, and the counts should be fairly close to:

Code: Select all

&#91;&#93; rg 1000000
   Checkmate   152806 0.152806
  FiftyMoves   223350 0.22335
Insufficient   536842 0.536842
  Repetition    25374 0.025374
   Stalemate    61628 0.061628
Average ply length&#58; 341.998
Maximum ply length&#58; 922
Which shows that repetition draws should occur about 2.54% of the time.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

And some examples

Post by sje »

And some examples:

To assist with testing, I've produced a PGN file of a thousand random games, of which 24 are repetition draws.

In each game, note the Termination tag pair which indicates the game termination kind.

https://dl.dropboxusercontent.com/u/316 ... 000.pgn.gz
jswaff
Posts: 105
Joined: Mon Jun 09, 2014 12:22 am
Full name: James Swafford

Re: How to test 3rd repetition rule?

Post by jswaff »

In my Java engine chess4j, I pulled this logic out into a separate class and wrote a unit test to ensure that it works.

https://sourceforge.net/p/chess4j/code/ ... .java#l244

See 'testDrawRep()' at the bottom. It basically just starts with the opening position and plays through some moves, checking after each move whether it's a draw by rep or not.

I don't think I have anything that checks for a draw by rep within the context of a search but that satisfies me that the logic works, if I can manage to plug it in correctly.
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: This should be in the wiki

Post by Luis Babboni »

sje wrote:This should be in the wiki, if it's not already there.

The easiest way to test game termination detection is to add a chunk of code to generate lots of games, each made from randomly selected moves, and keep track of the counts of the different game termination kinds.

Generate a million such games, and the counts should be fairly close to:

Code: Select all

&#91;&#93; rg 1000000
   Checkmate   152806 0.152806
  FiftyMoves   223350 0.22335
Insufficient   536842 0.536842
  Repetition    25374 0.025374
   Stalemate    61628 0.061628
Average ply length&#58; 341.998
Maximum ply length&#58; 922
Which shows that repetition draws should occur about 2.54% of the time.
Nice idea!!
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Ten million

Post by sje »

Code: Select all

&#91;&#93; rg 10000000
   Checkmate  1530488 0.153049
  FiftyMoves  2240233 0.224023
Insufficient  5358465 0.535847
  Repetition   258098 0.0258098
   Stalemate   612716 0.0612716
Average ply length&#58; 342.005
Maximum ply length&#58; 1017
User avatar
Werner Taelemans
Posts: 119
Joined: Mon Feb 03, 2014 11:57 am
Location: Belgium
Full name: Werner Taelemans

Re: How to test 3rd repetition rule?

Post by Werner Taelemans »

flok wrote:The int is a counter and when that reaches 3 at some point in the search-tree...
It is OK to use the draw score when the counter reaches 2, as explained in this thread:
http://www.talkchess.com/forum/viewtopic.php?t=51000