Skip to content

Inexpli/Pufferfish

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

87 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Pufferfish โ™Ÿ๏ธ

Python License

A hybrid chess engine combining the minimax algorithm with alpha-beta pruning, supported by a neural network for best move prediction.


image
Pufferfish (Depth 5) vs Cicada 0.1 (Depth 9)
Pufferfish as White, Cicada 0.1 as Black

๐ŸŽฏ Features

  • Hybrid Architecture: Minimax algorithm with alpha-beta pruning assisted by a neural network
  • Advanced Search Techniques: Quiescence search and move ordering for better performance
  • Transposition Table: Storage of calculated positions for faster move calculation
  • Position Evaluation: Piece value heuristics and positional values for each piece
  • Neural Network: PyTorch model predicting the best moves based on the position
  • UCI Compliance: Full implementation of the Universal Chess Interface protocol
  • Tablebases: Support for Gaviota tablebases
  • Opening Books: Integration with Polyglot opening books
  • Cross-platform: Runs on Windows, Linux, and macOS

๐Ÿ“‹ Requirements

  • Python 3.8 or newer
  • PyTorch
  • Additional dependencies listed in requirements.txt

๐Ÿš€ Installation

Step 1: Clone the repository

git clone [https://github.com/Inexpli/Pufferfish.git](https://github.com/Inexpli/Pufferfish.git)
cd Pufferfish/

Step 2: Install dependencies

pip install -r requirements.txt

Step 3: Build the executable

To create a standalone executable engine:

pyinstaller pufferfish.spec

Note: You may expect a large number of warnings during the executable creation; do not worry, as this is normal for PyInstaller.

Step 4: Export the engine

After compilation is complete, you will find the executable engine in the dist/uci_wrapper/ folder.

๐ŸŽฎ Usage

UCI Mode (with Graphical Interface)

Pufferfish can be used with any GUI supporting the UCI protocol, such as:

  • Arena Chess GUI
  • Cute Chess
  • ChessBase
  • Lichess (via Lichess-Bot)
  • Chess.com

In the GUI settings, add the engine pointing to:

  • Source file:: pufferfish.py (Python)
  • Executable: dist/pufferfish/pufferfish.exe (Windows) or dist/pufferfish/pufferfish (Linux/Mac)

CLI Mode (Command Line)

python pufferfish.py

Basic UCI commands:

uci                # Engine information
isready            # Check readiness
ucinewgame         # New game
position startpos  # Start position
go movetime 3000   # Search for 3 seconds
quit               # Exit

๐Ÿ“ Project Structure

Pufferfish/
โ”œโ”€โ”€ charts/                        # Data and charts from the training process
โ”‚   โ”œโ”€โ”€ policy_network/
โ”‚   |   โ”œโ”€โ”€ [model_name].csv       # Metrics for each model (loss, accuracy, etc.)
โ”‚   |   โ””โ”€โ”€ read_chart.ipynb       # Jupyter notebook for reading data
|   โ””โ”€โ”€ value_network/
|       โ””โ”€โ”€ [model_name].csv       # Metric for the model (loss, accuracy, etc.)
โ”œโ”€โ”€ core/                          
โ”‚   โ”œโ”€โ”€ evaluation.py              # Position evaluation functions
โ”‚   โ”œโ”€โ”€ minimax.py                 # Minimax algorithm with alpha-beta pruning and QS
โ”‚   โ”œโ”€โ”€ transposition_table.py     # Transposition table for search optimization
โ”‚   โ”œโ”€โ”€ heuristics.py              # Evaluation heuristics (material, position, etc.)
โ”‚   โ”œโ”€โ”€ model.py                   # Integration of ML models with the engine
โ”‚   โ”œโ”€โ”€ gaviota.py                 # Handling Gaviota tablebases
โ”‚   โ”œโ”€โ”€ polyglot.py                # Handling Polyglot opening books
โ”‚   โ”œโ”€โ”€ syzygy.py                  # Handling Syzygy tablebases
โ”‚   โ””โ”€โ”€ utils.py                   # Utility functions
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ policy_network/
โ”‚   |   โ””โ”€โ”€ [model_name].onnx      # Neural network model for move prediction
|   โ””โ”€โ”€ value_network/
|       โ””โ”€โ”€ [model_name].pth       # Neural network model for position evaluation
โ”œโ”€โ”€ tablebases/
โ”‚   โ”œโ”€โ”€ gaviota/                   # Gaviota tablebases
โ”‚   โ””โ”€โ”€ polyglot/                  # Polyglot opening books
โ”œโ”€โ”€ tests/                         
โ”‚   โ”œโ”€โ”€ methods.ipynb              # Performance tests of various minimax implementations
โ”‚   โ”œโ”€โ”€ minimax_opt.ipynb          # Minimax algorithm optimization
โ”‚   โ”œโ”€โ”€ nodes.ipynb                # Analysis of searched nodes
โ”‚   โ”œโ”€โ”€ gaviota.ipynb              # Integration tests with Gaviota bases
โ”‚   โ”œโ”€โ”€ polyglot.ipynb             # Integration tests with opening books
โ”‚   โ””โ”€โ”€ syzygy.ipynb               # Integration tests with Syzygy bases
โ”œโ”€โ”€ training/                      
โ”‚   โ”œโ”€โ”€ policy_network/            # Training policy network (move prediction)
โ”‚   โ”‚   โ”œโ”€โ”€ data_manager.py        # Training data management
|   |   โ”œโ”€โ”€ data_parser.ipynb      # Processing PGN files for model learning
โ”‚   โ”‚   โ”œโ”€โ”€ dataset.py             # Policy network dataset
|   |   โ”œโ”€โ”€ lmdb_dataset.py        # Database configuration for games
โ”‚   โ”‚   โ”œโ”€โ”€ model.py               # Policy network architecture
โ”‚   โ”‚   โ”œโ”€โ”€ test_model.ipynb       # Policy model tests
โ”‚   โ”‚   โ””โ”€โ”€ train_model.ipynb      # Policy network training notebook
โ”‚   โ””โ”€โ”€ value_network/             # Training value network (position evaluation)
โ”‚       โ”œโ”€โ”€ data_manager.py        # Training data management
โ”‚       โ”œโ”€โ”€ dataset.py             # Value network dataset
โ”‚       โ”œโ”€โ”€ model.py               # Value network architecture
โ”‚       โ”œโ”€โ”€ test_minimax.ipynb     # Value network integration tests with minimax
โ”‚       โ”œโ”€โ”€ test_model.ipynb       # Value model tests
โ”‚       โ””โ”€โ”€ train_model.ipynb      # Value network training notebook
โ”œโ”€โ”€ .gitignore                     # Files ignored by Git
โ”œโ”€โ”€ README.md                      # You are reading this file
โ”œโ”€โ”€ export_onnx.ipynb              # PyTorch model conversion to ONNX
โ”œโ”€โ”€ pufferfish.py                  # Main UCI file
โ”œโ”€โ”€ pufferfish.spec                # Specification for PyInstaller
โ””โ”€โ”€ requirements.txt               # Python dependencies

Key Components

  • engine.py - The heart of the project combining traditional chess techniques (minimax, alpha-beta pruning, transposition tables) with neural network predictions, implementing a hybrid approach to position evaluation.
  • core/ - Engine logic modules using the python-chess library for game representation, with custom implementations of search algorithms, evaluation, and integration with opening and endgame databases.
  • training/ - Complete pipeline for training two types of networks: policy network (predicting best moves) and value network (position evaluation).
  • charts/ - Training process visualizations enabling convergence monitoring and issue identification.
  • tests/ - Notebooks with optimization experiments, performance tests of various implementations, and integration with chess databases.

๐Ÿง  Architecture

Pufferfish uses a hybrid approach combining:

Search Algorithm

  1. Minimax with alpha-beta pruning - efficient game tree search with elimination of irrelevant branches.
  2. Quiescence search - additional search in "unquiet" positions (captures, checks).
  3. Move ordering - intelligent move analysis order for better pruning.
  4. Transposition table - cache of calculated positions for faster recalculation of repeating positions.

Position Evaluation

  • Piece value heuristics - material valuation (pawn=100, knight=320, bishop=330, rook=500, queen=900).
  • Positional values - bonuses/penalties for the position of each piece on the board (piece-square tables).

Neural Network

  • PyTorch model trained to predict the best moves
  • Input: Representation of the current board position
  • Output: Probabilities for possible moves
  • Integration: The network assists the classic algorithm in selecting the best variations

Databases

  • Gaviota tablebases - optimal play in endgames (up to 5 pieces)
  • Polyglot opening books - proven opening variations

โš™๏ธ Configuration

The engine can be configured via standard UCI options. Available parameters depend on implementation and can be set in the GUI or via the setoption command.

๐Ÿค Contributing

Contribution to the project's development is welcome! If you want to help:

  1. Fork the repository
  2. Create a branch for your feature (git checkout -b feature/FeatureName)
  3. Commit your changes (git commit -m 'Add new feature')
  4. Push to the branch (git push origin feature/FeatureName)
  5. Open a Pull Request

๐Ÿ“ License

The entire project is under the MIT license.

๐Ÿ“ง Contact

๐Ÿ™ Acknowledgments

  • Internet creators and YouTubers for inspiration and publishing materials regarding chess engine architecture
  • The chess programming community for documentation and advice
  • The PyTorch team for the deep learning framework

Note: Some features may change in the future.

About

A hybrid, UCI-compatible chess engine blending classical search (Minimax, Alpha-Beta, QS) with a Neural Network Policy Head for move prediction.

Topics

Resources

License

Stars

Watchers

Forks

Contributors