Legality Check on TT move

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jordanbray
Posts: 52
Joined: Mon Aug 11, 2014 3:01 am

Legality Check on TT move

Post 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?
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: Legality Check on TT move

Post 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.
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Legality Check on TT move

Post 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.
jordanbray
Posts: 52
Joined: Mon Aug 11, 2014 3:01 am

Re: Legality Check on TT move

Post 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.
jordanbray
Posts: 52
Joined: Mon Aug 11, 2014 3:01 am

Re: Legality Check on TT move

Post 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.
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Legality Check on TT move

Post 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.
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: Legality Check on TT move

Post 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.
jordanbray
Posts: 52
Joined: Mon Aug 11, 2014 3:01 am

Re: Legality Check on TT move

Post 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.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Legality Check on TT move

Post 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.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Legality Check on TT move

Post 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...