Chess-Ant Introduction

Simulator to solve chess problems with MCTS Solver and Genetic Programming.

chess_ant.py is based on the code of deap/examples/gp/ant.py.

Requirements

On Ubuntu:

sudo -H -s
apt install python3-pip
pip3 install -r requirements.txt
exit

Or:

pip3 install chess
pip3 install deap
pip3 install mcts
pip3 install mcts-solver

Or:

pip3 install chess-ant

General Usage

Sample chess problems are available in pgn/. Jerry is useful for pasting Forsyth-Edwards Notation (FEN).

cd chess-ant/chess-ant/
git checkout -b test-run
python3 chess_ant.py --help
python3 chess_ant.py --auto --fen "7k/1Q6/8/8/5N2/1B6/8/3K4 w - - 0 1"
python3 chess_ant.py -a -c -p "my-pgn" -l1 -n100 -g10 -f "7k/1Q6/8/8/5N2/1B6/8/3K4 w - - 0 1"

If you installed chess-ant from PyPI:

chess-ant --help
chess-ant -a -n100 -g5 -f "7r/8/8/8/7k/2q5/6P1/6NK b - - 0 1"

This command will output the wrong answer. It will take some time, but the following command will output correctly.

chess-ant -a -n1000 -g5 -f "7r/8/8/8/7k/2q5/6P1/6NK b - - 0 1"

Chess-Classification

Version 0.0.1 of genPgn.py contains the Walrus operator, so it only works with Python 3.8 or higher. Please install Pytorch before installing Simple Transformers.

sudo -H -s
pip3 install pandas
pip3 install simpletransformers
apt install stockfish
pip3 install chess-classification
exit
genPgn --help
genPgn -l 10 -t 1 -p "train-pgn" -f "3qkbnr/8/8/8/8/8/PPPPPPPP/RNBQKBNR w - - 0 1"
cat train-pgn/train-*.pgn >> train-pgn/1.pgn
rm train-pgn/train-*.pgn
genPgn -l 10 -t 1 -p "train-pgn" -f "rnbqkbnr/pppppppp/8/8/8/8/8/3QKBNR w - - 0 1"
cat train-pgn/train-*.pgn >> train-pgn/2.pgn
rm train-pgn/train-*.pgn
genPgn -l 10 -t 1 -p "train-pgn" -f "4k3/pppppppp/8/8/8/8/PPPPPPPP/4K3 w - - 0 1"
cat train-pgn/train-*.pgn >> train-pgn/3.pgn
rm train-pgn/train-*.pgn
importPgn -p "train-pgn"
genPgn -l 10 -t 1 -p "eval-pgn" -f "3qkbnr/8/8/8/8/8/PPPPPPPP/RNBQKBNR w - - 0 1"
cat eval-pgn/train-*.pgn >> eval-pgn/1.pgn
rm eval-pgn/train-*.pgn
genPgn -l 10 -t 1 -p "eval-pgn" -f "rnbqkbnr/pppppppp/8/8/8/8/8/3QKBNR w - - 0 1"
cat eval-pgn/train-*.pgn >> eval-pgn/2.pgn
rm eval-pgn/train-*.pgn
genPgn -l 10 -t 1 -p "eval-pgn" -f "4k3/pppppppp/8/8/8/8/PPPPPPPP/4K3 w - - 0 1"
cat eval-pgn/train-*.pgn >> eval-pgn/3.pgn
rm eval-pgn/train-*.pgn
importPgn -p "eval-pgn"
from chess_classification.chess_classification import ChessClassification
classification = ChessClassification()

Loading a local save:

classification = ChessClassification("local-path/your-outputs")

Train or retrain:

classification.train_and_eval("train-pgn/fen.json", "eval-pgn/fen.json")

Test:

my_fen = "7r/8/8/8/7k/2q5/6P1/6NK b - - 0 1"
classification.predict_fen(my_fen)

predictions

labels

1-0

2

0-1

1

1/2-1/2

0

With chess_ant.py:

chess-ant -d -n100 -g5 -f "6rk/4pppp/8/8/3Q4/8/RB2PPPP/R6K w - - 0 1"

Loading a local save:

chess-ant -d -n100 -g5 -f "6rk/4pppp/8/8/3Q4/8/RB2PPPP/R6K w - - 0 1" --model-outputs "local-path/your-outputs"

Todo

  • It’s too slow.

  • Low correct answer rate.

  • Parallelization.

  • Support for other board games like shogi.

  • Support for Universal Chess Interface (UCI).

  • Docstring.

  • Boil spaghetti code.