legal or pseudolegal move generator?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

tcusr
Posts: 323
Joined: Tue Aug 31, 2021 10:32 pm
Full name: tcusr

legal or pseudolegal move generator?

Post by tcusr »

hello everyone, what kind of move generator do you think is best for a chess engine?
i want to make clear that this is not to speed up perft but to use in an actual engine.
in my opinion the pseudolegal is far easier to write and generates moves quickly too, but i think that the branches to verify if the king is in check nullify this benefits
on the other hand though a legal generator is hard write because it has to cover all edge cases but in the end it will make search code cleaner and there will be no branches in search.
which one do you think is better?
R. Tomasi
Posts: 307
Joined: Wed Sep 01, 2021 4:08 pm
Location: Germany
Full name: Roland Tomasi

Re: legal or pseudolegal move generator?

Post by R. Tomasi »

tcusr wrote: Thu Sep 02, 2021 9:57 pm hello everyone, what kind of move generator do you think is best for a chess engine?
i want to make clear that this is not to speed up perft but to use in an actual engine.
in my opinion the pseudolegal is far easier to write and generates moves quickly too, but i think that the branches to verify if the king is in check nullify this benefits
on the other hand though a legal generator is hard write because it has to cover all edge cases but in the end it will make search code cleaner and there will be no branches in search.
which one do you think is better?
In Pygmalion I generate pseudo-legal moves. In my opinion this is better, because the move generation itself will be significantly faster. And I do not think that branching argument holds here: first of all, you will have the branches in any case. You just move them to an earlier stage when you do the respective checks during move generation. And then, more importantly: if I generate pseudo-legal moves only, I will not have to check the legality of all the generated moves. I can skip verifiying those that come after a fail-high. Only my two cents, of course.

[Edit: typos]
Chan Rasjid
Posts: 588
Joined: Thu Mar 09, 2006 4:47 pm
Location: Singapore

Re: legal or pseudolegal move generator?

Post by Chan Rasjid »

I think all top engines gen only pseudolegal moves for a reason.

If you use principle variation search(which every serious engine does), you would be searching a window of width= 1 (70% of all nodes, about?) except when searching pv-nodes. So there would be beta cutoff and most of the balanced moves are discarded.

Validate moves before a make().
tcusr
Posts: 323
Joined: Tue Aug 31, 2021 10:32 pm
Full name: tcusr

Re: legal or pseudolegal move generator?

Post by tcusr »

valid points, thanks
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: legal or pseudolegal move generator?

Post by bob »

One note. If you are in check, generating legal moves might be faster.

You have to measure two things and choose the fastest.

(1) make move, go to next ply, generate moves and capture opponent king, go back to previous ply, unmake move and ignore.

(2) generate legal moves with specific code (really not generating all and then excluding illegal moves, just generating legal moves only.).

For me, (2) was faster and is how Crafty does this. Might be different for your implementation. Note that generating legal moves only is actually pretty tricky. And. you still need to check for illegal positions in the search, since some moves are illegal and you are not in check when you get to this position. But in a position with 50 pseudo-legal moves and just 4 legal moves, the savings of legal-only for the in-check positions can add up quickly.
R. Tomasi
Posts: 307
Joined: Wed Sep 01, 2021 4:08 pm
Location: Germany
Full name: Roland Tomasi

Re: legal or pseudolegal move generator?

Post by R. Tomasi »

bob wrote: Mon Sep 06, 2021 2:14 am One note. If you are in check, generating legal moves might be faster.

You have to measure two things and choose the fastest.

(1) make move, go to next ply, generate moves and capture opponent king, go back to previous ply, unmake move and ignore.

(2) generate legal moves with specific code (really not generating all and then excluding illegal moves, just generating legal moves only.).

For me, (2) was faster and is how Crafty does this. Might be different for your implementation. Note that generating legal moves only is actually pretty tricky. And. you still need to check for illegal positions in the search, since some moves are illegal and you are not in check when you get to this position. But in a position with 50 pseudo-legal moves and just 4 legal moves, the savings of legal-only for the in-check positions can add up quickly.
Very valid points. I use a special move generator when in check, such that I do not get the 50 worthless pseudo-legal moves in that case. Yet, it's still a pseudo-legal movegenerator. So I have to still check if the generated moves are legal, only for much less moves. That way I can still save the trouble checking the legality of moves that may come after a fail-high. Tbh I never took a look at the Crafty sources, therefore I have no Idea how your legal move generator works and I have to admit that I do not have any idea on how to do this which isn't prohibitivly expensive. However I would think that for legal move generation to be superior in such cases it would have to be lightening fast...
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: legal or pseudolegal move generator?

Post by Ras »

R. Tomasi wrote: Mon Sep 06, 2021 8:34 pmI use a special move generator when in check, such that I do not get the 50 worthless pseudo-legal moves in that case. Yet, it's still a pseudo-legal movegenerator.
Same here (check evasions).
Rasmus Althoff
https://www.ct800.net
Mergi
Posts: 127
Joined: Sat Aug 21, 2021 9:55 pm
Full name: Jen

Re: legal or pseudolegal move generator?

Post by Mergi »

If you have a classic bitboard engine, checking for whether moving a piece will leave the king in check or not should be just a case of one extra & operation per piece + possibly a little bit of a setup when initializing the generator, depending on whether you already compute the piece giving the check anyway. In my opinion, there really is almost no reason not to do it. The only problems are king moves and enpassants, those can be expensive to verify.

I generate almost only legal moves (except enpassants and some king moves), and having to score and sort through less moves, even with staged move generation, makes a big difference ... at least for me.
tcusr
Posts: 323
Joined: Tue Aug 31, 2021 10:32 pm
Full name: tcusr

Re: legal or pseudolegal move generator?

Post by tcusr »

Mergi wrote: Tue Sep 07, 2021 2:09 am If you have a classic bitboard engine, checking for whether moving a piece will leave the king in check or not should be just a case of one extra & operation per piece + possibly a little bit of a setup when initializing the generator, depending on whether you already compute the piece giving the check anyway. In my opinion, there really is almost no reason not to do it. The only problems are king moves and enpassants, those can be expensive to verify.

I generate almost only legal moves (except enpassants and some king moves), and having to score and sort through less moves, even with staged move generation, makes a big difference ... at least for me.
do you verify the legality of those moves in your make_move function then?
Mergi
Posts: 127
Joined: Sat Aug 21, 2021 9:55 pm
Full name: Jen

Re: legal or pseudolegal move generator?

Post by Mergi »

tcusr wrote: Tue Sep 07, 2021 10:51 am do you verify the legality of those moves in your make_move function then?
Yes, enpassants and king moves I do verify in MakeMove function, but every other move is always strictly legal (regardless of king being in check or not).