Page 1 of 4

Legality Check on TT move

Posted: Sun Jan 11, 2015 10:05 pm
by jordanbray
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?

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 10:10 pm
by syzygy
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 10:14 pm
by hgm
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 10:15 pm
by jordanbray
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 10:18 pm
by jordanbray
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 10:19 pm
by hgm
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 10:49 pm
by cdani
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 11:04 pm
by jordanbray
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 11:14 pm
by bob
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.

Re: Legality Check on TT move

Posted: Sun Jan 11, 2015 11:15 pm
by bob
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...