Stanford CS234 Reinforcement Learning: Class Project
We used the following dataset from Kaggle: 3.5 Million Chess Games, a text file export of ChessDB (docs).
Here's how to easily download the datasets with the Kaggle API:
kaggle datasets download -p data/chess-games --unzip milesh1/35-million-chess-games
There is a slight learning curve to understand the dataset.
- Each row of the
all_with_filtered_anotations_since1998.txt
file contains the data for one chess game. - There is a header (game information) and body (moves), separated by
###
. - The header contains information like game date, ELO of each player, and if an initial position was specified (e.g. Fischer Random Chess).
A chess board is an 8 x 8 grid where:
- Rows ("rank"): numbered 1 (bottom row) to 8 (top row).
- Cols ("file"): lettered a (left column) to h (right column).
Moves are written in Portable Game Notation, which uses Algebraic Notation (also known as standard algebraic notation (SAN) or standard notation) to describe each move as movetext.
Stated with regex-like notation,
movetext is governed by ab.(c?d?e|O-O|O-O-O|e=f)
:
a
: player, either whiteW
or blackB
.- Note: in Forsyth–Edwards Notation (FEN) notation,
used by the
chess
library, white pieces are designated with capital letters, and black pieces are designated with lowercase letters.
- Note: in Forsyth–Edwards Notation (FEN) notation,
used by the
b
: one-indexed turn number..
: separator.
The rest could be one of several options:
- Normal move:
c?
: piece, either pawn (no letter,P
in other contexts), kingK
, queenQ
, rookR
, bishopB
, or knightN
(S
in other contexts).d?
: capture, either no capture (no letter) or capturex
.e
: board destination. When 2+ pieces could have reached the destination, the piece's original rank (row), file (column), or both are included. For example,g5
means the piece moved tog5
,df8
means the piece moved fromd8
tof8
,1a3
means the piece moved froma1
toa3
, andh4e1
means the piece moved fromh4
toe1
.
- Kingside castle:
O-O
. - Queenside castle:
O-O-O
. - Pawn promotion:
e
: board destination (see above).=
: indicates promotion.- Note: 'underpromotion' is a term for promoting a pawn to a non-queen piece.
f
: exchanged piece.
And lastly if the move leads to a check +
or checkmate #
.
To help connect the dots, check this May 1783 blindfolded match's visualization.
We are using the open source chess engine stockfish
(source code).
To integrate with the stockfish
engine,
we use the Python wrapper stockfish
.
This project was developed using Python 3.10.
To install OS-level dependencies on macOS:
brew install stockfish
After, run whereis stockfish
to find
the engine was installed at /opt/homebrew/bin/stockfish
.
Here is how to create a virtual environment and install the core project dependencies:
python -m venv venv
source venv/bin/activate
python -m pip install -r requirements.txt
Here is how to also install code quality assurance tooling:
python -m pip install -r requirements-qa.txt
pre-commit install
I launched several AWS
Deep Learning AMI GPU PyTorch 1.13.1 (Ubuntu 20.04) 20230315's
with instance type t2.2xlarge
. Here's how they were configured:
> source activate pytorch
> python --version
Python 3.9.16
Whew, I almost used conda
, that was a close call.
Step 1: install and configure Python 3.10:
python3 --version # 3.8.10
sudo apt update && sudo apt upgrade -y
sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt install -y python3.10 python3.10-venv
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
python3 --version # 3.10.10
Step 2: git clone
and install requirements into a venv
:
git clone --recurse-submodules https://github.com/jamesbraza/cs234-dreamchess.git
cd cs234-dreamchess
python3 -m venv venv
source venv/bin/activate
python -m pip install -r requirements.txt --progress-bar off \
-r requirements-qa.txt \
torch==1.13.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
Note the torch==1.13.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
trick installs a CPU-only version of PyTorch 1.13.1
(since t2.2xlarge
has no GPU).
I didn't want to use PyTorch 2.0 since it was released this week, and likely has some bugs.
Step 3: kick off your azg_chess
script:
tmux
source venv/bin/activate
python -m azg_chess.script