This repository contains all of the implementation for the SPD Chess AI model. This is a project that I (Keon Roohparvar) led for Sigma Phi Delta, the Professional Engineering Fraternity at Cal Poly SLO; I used the medium of a Chess Engine to teach undergraduates about some basic Deep Learning/Neural Network fundamentals.
The majority of team members were new to programming, so this project was a learning opportunity to give these guys a chance to start interacting with a fun, non-trivial project!
The folder structure is...
.
├── data # Sample data examples of games
├── legacy # Old code that we don't use anymore
├── models # Different people's implementations of Neural Networks
└── source
├── board.py # Our implementation of a board
├── data_handler.py # This handles all the interactions with the data
├── eval/ # Folder that contains code for evaluating boards (AB pruning, minimax, etc.)
├── find_move.py # High level script that takes a board FEN and finds a move
├── play_chess.py # This has two hard-coded models play against eachother and prints board
├── train_model.py # This trains models on the data
└── ui # folder that contains some work on implementing GUI
The process for getting this code setup is by following the steps below (please hit me up if you have questions):
- Ensure you have Python installed. If you are not sure, type
$ python3 -Vin your terminal. - Clone this repository somewhere locally on your computer.
- Create a python environment using
$ python3 -m venv venv- this should create a folder namedvenv/in your directory - Activate this virtual environment by running
$ source venv/bin/activate - Install TensorFlow - This may be challenging, but this TensorFlow installation Link should help.
- Install packages in requirements.txt by running
$ pip install -r requirements.txt
That's it! To check this all works, type python into the terminal and type import tensorflow to ensure that you properly installed TensorFlow. If you did, congrats! You are (probably) done, attempt running any of the scripts to ensure that you have the installations correct.
We ultimately employ Alpha-Beta pruning in tandem with neural networks to evaluate a position.
We use AB pruning to navigate the search tree and immediately eliminate any moves that result in us losing material at a depth of 4 moves into the future.
We then handle ties by using a neural network that was trained on Stockfish evaluations to predict the outcomes for positions where we gain the same amount of material. The move with the best prediction from the neural network is ultimately the move that we choose.
Currently, the model takes anywhere from 5 to 180 seconds to make a move, so we need to put in a considerable amount of effort into trying to decrease the time required for each move.