Legality Check on TT move

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
jordanbray
Posts: 51
Joined: Mon Aug 11, 2014 1:01 am

Legality Check on TT move

Post by jordanbray » Sun Jan 11, 2015 9:05 pm

Do you guys check the legality of the TT move once it's been extracted? I've been debugging some issues today and ended up with a hash collision (on a 64-bit hash!).

I'm gonna be debugging my zobrist to see if I can find the source of that bug, but for now I've added in a legality check on the move extracted from the TT. What are your thoughts on leaving that legality check in, assuming I find this zobrist bug?

syzygy
Posts: 4455
Joined: Tue Feb 28, 2012 10:56 pm

Re: Legality Check on TT move

Post by syzygy » Sun Jan 11, 2015 9:10 pm

You can't do without a legality test, unless your program cannot crash on an illegal move. Collisions aren't that rare with 64-bit hash keys.

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

Re: Legality Check on TT move

Post by hgm » Sun Jan 11, 2015 9:14 pm

I don't check legality, but I designed the engine such that illegal moves can do no damage. E.g. after a castling I not only save and restore the piece captured by the King, but also that by the Rook. And because absense of a King would crash the check test, I test whether the King is captured, even though normally this is not supposed to happed.

jordanbray
Posts: 51
Joined: Mon Aug 11, 2014 1:01 am

Re: Legality Check on TT move

Post by jordanbray » Sun Jan 11, 2015 9:15 pm

Is it, then worthwhile to do a legality check any time I take a score from the TT? It seems like I'm gonna end up with a bad search if I take the score without much fuss, given that hash collisions do seem to happen.

I've sort of taken it for granted that it would never happen, I guess.

jordanbray
Posts: 51
Joined: Mon Aug 11, 2014 1:01 am

Re: Legality Check on TT move

Post by jordanbray » Sun Jan 11, 2015 9:18 pm

My bug was kinda strange. It was a pawn that jumped over another pawn. The pawn was then captured en-passant, and two pieces (white pawns) were removed from the board. There's gotta be lots of gotchas in dealing with that.

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

Re: Legality Check on TT move

Post by hgm » Sun Jan 11, 2015 9:19 pm

It is always safe to take the score; this will never crash the engine. Testing the hash move for legality doesn't solve the problem that colissions can give you wrong scores, as the hash move could still be legal in the position you collide with, or there could be no hash move at all.

User avatar
cdani
Posts: 2104
Joined: Sat Jan 18, 2014 9:24 am
Location: Andorra
Contact:

Re: Legality Check on TT move

Post by cdani » Sun Jan 11, 2015 9:49 pm

Someone has an idea of how often happen the hash collisions? I did some tries and I'm not able to catch an illegal move coming from hash. Anyway I keep checking them for legality.

jordanbray
Posts: 51
Joined: Mon Aug 11, 2014 1:01 am

Re: Legality Check on TT move

Post by jordanbray » Sun Jan 11, 2015 10:04 pm

My hash collision was a false positive, so I don't know.

Turns out my legality checker was broken, which caused some killer moves to be passed back to the search. When those moves (which were illegal) were unmade, the hash got restored to the old value. This meant that two positions were in the TT with the same hash value.

However, per the advice here, I'm gonna leave the legality check in place.

bob
Posts: 20555
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Legality Check on TT move

Post by bob » Sun Jan 11, 2015 10:14 pm

jordanbray wrote:Is it, then worthwhile to do a legality check any time I take a score from the TT? It seems like I'm gonna end up with a bad search if I take the score without much fuss, given that hash collisions do seem to happen.

I've sort of taken it for granted that it would never happen, I guess.
A bad score has almost zero probability of changing anything, from a lot of testing Cozzie and I did a few years ago. The only issue is "will an illegal move cause you to crash?" If you do a copy/make program, it is unlikely, but if you do a simpler make/unmake (as I do in Crafty) making an illegal move can corrupt the board and there is no way to uncorrupt it, leaving things broken. As a result, I always check the tt move before playing it, but I don't check anything else.

bob
Posts: 20555
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Legality Check on TT move

Post by bob » Sun Jan 11, 2015 10:15 pm

cdani wrote:Someone has an idea of how often happen the hash collisions? I did some tries and I'm not able to catch an illegal move coming from hash. Anyway I keep checking them for legality.
It is rare. If I play games, I might see one every 20-30 games, maybe. It is REALLY rare with 64 bit signatures...

Post Reply