An implementation of AlphaZero for the board game Tak. TakZero is arguably one of the strongest engines in Tak along with Topaz. For the network weights reach out to me directly.
This is my second time implementing AlphaZero for Tak. My first was WilemBot.
This repository is a bit of a mess because I wrote it during my Bachelor's thesis, so there is a lot of experimental and unused code.
If you wish to use the engine, you only have to build the tei binary (i.e. only run cargo build --release -p tei).
The repository contains several libraries and binaries:
takzerois the main library which implements MCTS and the neural networksselfplayis used during training to generate replays and exploitation targetsreanalyzecomputes fresh targets from old replayslearntakes targets fromselfplayandreanalyzeto train new modelsevaluationpits models against each otherpuzzleruns the puzzle benchmarkanalysisincludes interactive game analysisgraphcomputes the ratio of unique states seen throughout trainingteia TEI implementationeeeis a collection of binaries to run Epistemic uncertainty Estimation Experiments (EEE)generalizationtrains a hash-based uncertainty estimatorrndis the same asgeneralization, but specifically forrndseen_ratioanalyzes the ratio of seen states according to a filled hash-setensembletrains an ensemble networkutilsutility functions for running experiments
visualize_searchcreates a visualization of the search tree used by an agentvisualize_replay_buffercreates a visualization of the overlap of different replay buffers, as well as the number of seen states at different depthspythoncontains miscellaneous Python scriptsaction_spacecomputes the action space for different board sizesanalyze_searchanalyzes search data to figure out which bandit algorithm optimizes best for explorationelocomputes Bayesian Elo from match results (fromevaluation) and creates a graphextract_from_logsgraphs various data from logsconcat_outconcatenates log outputgenerate_openingsgenerates random opening positions (for example to use as an opening book for a tournament)get_match_resultsextract match results from evaluation logsimproved_policycompares different improved policy formulasnovelty_per_depthplots the novelty per depthplot_eeeplots the results of EEEplot_elo_dataplots the Elo datareplay_buffer_uniquenessplots the replay buffer uniqueness
You will need the C++ Pytorch library (LibTorch). See tch-rs for installation instructions.
It's very likely that a newer version of LibTorch has been released
since I have updated the repository last. My recommendation is to
download the newest and update the tch version in Cargo.toml
to match.
Make sure to set the following environment variables (This assumes Linux):
export LIBTORCH=/absolute/path/to/your/libtorch
export LIBTORCH_INCLUDE=$LIBTORCH
export LD_LIBRARY_PATH=$LIBTORCH/lib:$LD_LIBRARY_PATH
export LD_PRELOAD=$LIBTORCH/lib/libtorch_cpu.so:$LIBTORCH/lib/libtorch.soYou may also need to set LIBTORCH_BYPASS_VERSION_CHECK to 1 if the latest
tch-rs version is a little behind your version of LibTorch.
I managed to build this repository on my Raspberry Pi. Here are the steps I followed:
- install
uvand runuv initto setup an environment uv add torch numpy setuptoolsto add the required dependenciessource .venv/bin/activateto activate the virtual environment- install
rustupand get the default toolchain for your system git clonethis repository- update
tchinCargo.tomlto the latest version - install the mold linker so that I can link on ARM
- add a
.cargo/config.tomlwith
[target.aarch64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-C", "link-arg=--ld-path=/usr/bin/mold", "-C", "target-cpu=native"]- export a bunch of environment variables (put this in your .bashrc or similar)
export LIBTORCH_USE_PYTORCH=1
export LIBTORCH_BYPASS_VERSION_CHECK=1
export LIBTORCH=~/.venv/lib/python3.10/site-packages/torch # Replace with your own path to torch that was installed by uv
export LIBTORCH_INCLUDE=$LIBTORCH
export LD_LIBRARY_PATH=$LIBTORCH/lib:$LD_LIBRARY_PATH
export LD_PRELOAD=$LIBTORCH/lib/libtorch_cpu.so:$LIBTORCH/lib/libtorch.socargo build --release -p tei- wait like 15 minutes for it to build
To generate the local novelty per depth graph follow these steps:
- Edit
eee/src/seen_ratio.rswith the path to a trained model, and adjust the imports based on whether it is a SimHash or LCGHash model. - Run
cargo run -p eee -r --bin seen_ratiofor each agent. - Take the output and place it into
python/novelty_per_depth.py. - Run
python python/novelty_per_depth.py.
- Acquire a replay buffer by running an undirected agent. (See elo graph instructions.)
- Edit the import in
eee/src/generalization.rsfor the model that you want to test. - Run
cargo run -p eee -r --bin generalizationfor each agent, rename the output fileeee_data.csvfor each. - Edit
plot_eee.pyto plot hashes and runpython python/plot_eee.py
- Acquire a replay buffer by running an undirected agent. (See elo graph instructions.)
- Run
cargo run -p eee -r --bin rnd - Edit
plot_eee.pyto plot RND and runpython python/plot_eee.py
To generate the elo ratings for agents throughout training follow these steps:
- Edit
selfplay/src/main.rs,reanalyze/src/main.rs, andlearn/src/main.rsfor the agent and value of beta that is desired. - Compile using
cargo build -r -p selfplay -p reanalyze -p learn. If exploration is desired, append--features explorationto the command. - Deploy the agent on a cluster, 1 learn process, 10 selfplay processes, and 10 reanalyze processes.
- Once you have generated checkpoints for all agents, compile the evaluation using
cargo build -r -p evaluation. - Evaluate agents against each other by deploying evaluation processes.
- Extract the match results out of logs using
python/get_match_results.py. - Place the match results into
match_results/and runpython python/elo.pyto plot the elo. - For an easier to edit plot, copy the bayeselo output from
elo.pyintoplot_elo_data.pyin the expected format.
To generate the replay uniqueness graphs follow these steps:
- Train agents using steps 1-3 from the elo graph instructions.
- Edit
graph/main.rswith paths to the replay files. - Run
cargo run -r -p graphand see the generated graph ingraph.html. - For an easier to edit plot, copy the output into
replay_buffer_uniqueness.pyand run withpython python/replay_buffer_uniqueness.py.




