GithubHelp home page GithubHelp logo

timospal / nnue-bitboard-chess-engine Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 3.9 MB

Bitboard chess engine that uses an NNUE model for node evaluation inside a PVS search. Move tables and plain magic bitboards are used for move generation

C++ 97.27% CMake 2.73%
bitboards chess chess-engine nnue pvs

nnue-bitboard-chess-engine's Introduction

NNUE-Bitboard-Chess-Engine

This is a bitboard chess engine that uses an NNUE model for node evaluation inside a PVS search. For more information on this current project and other techniques concerning chess engines see my bsc dissertation (written in greek).

Build info

Compiled with: cmake CMakeLists -> make

NNUE file should be at the same folder with the executable. If the program crashes due to unknown cpu instructions the appropriate intrisics should be disabled within the CMakeLists file.

Possible options: USE_SSE41 USE_SSE3 USE_SSE2 USE_SSE USE_AVX2. The more instructions supported the better the speed (search nps)

Bitboards

Bitboards (bitmaps) are used to represent various states and piece positions within the chess board. They utilise the fact that a uint_64 has excactly as many bits as we need to represent an 8x8 chess board. This method offers great computational speed when run in 64 bit architectures where 64 bit registers can be used. When we want to add remove ot check anything within our set all we have to do is use binary operators.

eg: We can enable the i-th position of board A by doing A |= (1L << i). These basic operations are abstracted within a bitboard class.

Move generation

We differentiate between two move categories. Pseudo moves and legal moves. A pseudo move is a move that is legal in isolation but might not protect against a check or let the king be checked. We firstly generate Pseudo moves and filter them afterwards.

We differentiate between two piece categories. Leaper pieces and sliding pieces.

  • Leaper pieces : pawns , king , knights
  • Sliding pieces : queen , rook , bishop

Attack / move tables are pre-generated at startup. This way we can get all the possible pseudo moves for a given position without needing to calculate them on the fly. For slider pieces pre-calculated magic numbers are used (magic bitboards technique)

Eg: In the following example we create a custom mask for the bishop rays at the position e5. We then filter the relevant tiles from the occupancy board from said position to find the blocker pieces. By multiplying by a magic number we can flatten the diagonal to be used as a key to a precalculated move table.

Sublime's custom image

Exception: pawn pushes are calculated on the spot due to their simplicity and strong correlation to the occupancy bitboards when calculating double pushes on the 2nd or 7th ranks. A pseudo move is considered legal if, after being applied, it leaves no checks. This assume we apply the move to a temporary copy of the current state which is later discarded.

Move search

To find the optimal move a PVS implementation is used. The following optimization are included:

  • null move prunning
  • futility pruning
  • move ordering
  • transpotion tables with zobrist keys
  • ab pruning
  • late move pruning
  • static null move pruning
  • check extension

UCI

The engine supports the basic UCI commands so it can be used inside GUI apps.

  • ucinewgame // Starts a new game (required by the protocol)
  • position fen [fen string] // Sets a [fen] position
  • position default // Sets the default position
  • go depth [n] // Searches for the best move with depth [n].
  • quit // Quits the program

More info on the UCI protocol can be read here http://wbec-ridderkerk.nl/html/UCIProtocol.html

Fen strings

Fen strings describe a board state, whose turn it is, if en passant is available, castling rights, piece positions and half/full move counters.

Default fen string: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

Dependencies

This engine uses the NNUE-probe library as a submodule. Furthermore the model file is required.

Note: the library only works with older model types. File nn-62ef826d1a6d.nnue was tested. Various model files can be downloaded from https://tests.stockfishchess.org/nns

nnue-bitboard-chess-engine's People

Contributors

timospal avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

nnue-bitboard-chess-engine's Issues

Improvement - remarks and tips

Recently i discovered your NNUE engine, and i like it !
I managed to compile its source (on Linux) and downloaded the concerning NNUE file.
The engine runs fine in CuteChess (GUI), i did several test games and now i have some remarks and questions.

The engine often makes a move within one second !?
And calculation is always done upto depth 8, never deeper ..
I propose better time management, and thus (?) a stronger engine - i guess the current rating (using nn-62ef826d1a6d.nnue) is about 2500.
eg. when i let engines play a 15 minute game, your engine still moves within 1 second and ends up with more time on the clock !

It often makes a draw by 3-fold repetition !?
I attach many (35) bullet games (1m+2s bullet) against several engines : some-bullet.zip : see those repetitions.
You really should solve this issue, it's a pitty losing winning positions this way ..

Can the NNUE file be changed ? which model type is used, and can you point to (some of those) alternatives ? i guess these are the 20 Mb ones, but i have no clue ..
btw. i could change the name of the NNUE file in the code and then compile again, but having an UCI option would be preferred ..

The output (in CuteChess GUI) is minimal : we only see the eval cp value of bestmove, no best moves at each depth, no PV line, no display of the thinking time ..

The cmake & make procedure differs slightly from "Build info" in README .. here's what i did :

cmake .
make

At first i had one compile error, but i solved it by adding the line #include <string> on top of /src/miscellaneous/Timer.h .. then compiling went well, although some warnings are still shown .. i managed to solve these also (just to learn), i mainly changed [=] to [=,this] in /src/representation/Board.cpp ..

Finally, i wonder about MCTS : i see a few source files concerning this, but your README doesn't mention anything about it .. does the engine really use MCTS ? Maybe create an UCI option to en/disable ..

[ i'm on Xubuntu 22.04 ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.