Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/move generation optimization #10

Merged
merged 5 commits into from Feb 18, 2021

Conversation

ekmadsen
Copy link
Owner

@ekmadsen ekmadsen commented Feb 18, 2021

Improved detection of pieces pinned to own king by sliding attackers. Previous implementation only found potentially pinned pieces (because the pieces were on the same file, rank, or diagonal as the sliding attacker). The new implementation finds all actually pinned pieces.

This speeds up resolution of pseudo-legal moves to legal moves by eliminating unnecessary calls to Board.IsSquareAttacked(kingSquare) in Board.IsMoveLegal method. Its benefit is limited though because many pseudo-legal moves never are examined for legality because a beta cutoff occurs before the move is searched.

Eliminated unnecessary call to Board.PlayNullMove and Board.UndoMove in Board.IsMoveLegal method. Instead of actually making a null move, the code flips side-to-move, sets a few other properties of the position, calls IsSquareAttacked(kingSquare), then restores original property values.

I experimented with eliminating the remaining call to Board.PlayMove and Board.UndoMove in Board.IsMoveLegal method to determine if this speeds up resolution of pseudo-legal moves to legal moves. I did this by intersecting pre-calculated moves from the destination (To) square to the opponent king. And by detecting pieces pinned to the opponent king by own sliding attackers. Moving such pieces in a direction other than the attacking ray creates a discovered check. Unfortunately, this code was slower than simply moving the piece and calling Board.IsSquareAttacked. Board.IsSquareAttacked uses pre-calculated moves (magic bitboards) to determine if own king is attacked after moving the piece, constituting an illegal move. Did not include in this PR.

Also, I experimented combining legality checking with playing a move in a Board.PlayMoveIfLegal method. Unfortunately this causes numerous complications with futility-pruned moves. MadChess never prunes moves that deliver check, moves aren't known to deliver check until they're played, so this must be detected after the fact (which complicates undoing the move and restoring board state) or detected prior to playing the move (which already has been proven slower). Ugly and prone to bugs. Did not include in this PR.

Also adjusted how nodes are counted- affecting Node Per Second (NPS) metric- to a more honest measurement. A node is counted only in...

  1. Board.PlayMove method
  2. Board.PlayNullMove method (because this changes side-to-move plus a few other properties and advances position index)

2562 +/- 16 ELO at bullet chess.

…nstead of finding potentially pinned pieces. Solves 286 of 300 WAC positions at 3 sec per move.
…estPositions command runs in 302 seconds when moving pieces and 334 seconds using bitboards. Commented out test code.
@ekmadsen ekmadsen merged commit 22002dc into master Feb 18, 2021
@ekmadsen ekmadsen deleted the feature/move-generation-optimization branch February 18, 2021 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant