Reinforcement Learning
stable-baselines3
English
Portuguese
board-game
self-play
maskable-ppo
sb3-contrib
ankh
Instructions to use heitorvcm/ankh-ppo with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- stable-baselines3
How to use heitorvcm/ankh-ppo with stable-baselines3:
from huggingface_sb3 import load_from_hub checkpoint = load_from_hub( repo_id="heitorvcm/ankh-ppo", filename="{MODEL FILENAME}.zip", ) - Notebooks
- Google Colab
- Kaggle
File size: 7,560 Bytes
12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 12c9315 8fadc05 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | ---
license: mit
library_name: stable-baselines3
tags:
- reinforcement-learning
- board-game
- self-play
- maskable-ppo
- sb3-contrib
- ankh
language:
- en
- pt
---
# Ankh: Gods of Egypt — MaskablePPO agent (2–5 players)
A single neural network that plays the board game **Ankh: Gods of Egypt** with 2 to 5 players, trained with
`MaskablePPO` (sb3-contrib) through self-play against earlier snapshots of itself. The repository contains the model
and **only the code needed to play against it and to continue training it**.
> **Fan project.** Not affiliated with, endorsed by, or connected to CMON, the game's designers or publishers.
> The game rules were re-implemented in Python from the public rulebook for research and learning. This repository
> contains **no** rulebook, artwork or other game assets.
**Resumo em português:** agente de PPO com máscara de ações que joga Ankh de 2 a 5 jogadores, treinado em self-play
(16 milhões de passos, 40 rounds). O repositório tem o modelo, as regras, o treino (`train_local.py`) e um jogo local
(`play.py`) para jogar contra a IA no navegador, com 1 a 5 humanos. Os resultados abaixo são fracos como medida de
força absoluta: veja "Limitations".
## Model
| | |
|---|---|
| Algorithm | `MaskablePPO` (sb3-contrib), `MlpPolicy`, net_arch `[512, 512]` |
| Checkpoint | `agent.zip` (round 40, ≈16.0M timesteps) |
| Observation | 6786 floats in [0, 1], always from the point of view of the deciding seat (me + up to 4 opponents, zero-padded) |
| Actions | 332 discrete actions, with an action mask for every decision |
| Players | one network for 2, 3, 4 and 5 players |
| Hidden information | the encoder never reads other players' pending battle cards or Plague bids |
| Versions used | stable-baselines3 2.9.2, PyTorch 2.15, Gymnasium 1.4.0 |
Hyperparameters: `n_steps=128`, `n_epochs=4`, `gamma=0.995`, `gae_lambda=0.95`, `ent_coef=0.01`,
`learning_rate=3e-4`, batch size = `n_steps * n_envs / 4`.
## Install
```bash
pip install -r requirements.txt # plus PyTorch (with CUDA if you want to train on a GPU)
```
Playing without a model needs only Python and numpy; the AI seats then move at random.
## Play
```bash
python play.py --models agent.zip # opens http://127.0.0.1:8765
python play.py --models agent.zip --lan # also accepts other devices on your local network
```
Up to 5 human players: in the "New game" dialog each seat is Human or AI (all-human games use no AI). Humans can share
one screen (a hand-over screen hides secret choices) or each use their own device with `--lan` (each opens
`http://IP:8765/?seat=N`; there is no password, so use it only on a trusted network). Moves of the other players are
replayed step by step.
Text replay of a game between copies of the model (shows hidden information; debugging tool):
```bash
python replay.py --scenario basic_4p --seed 7 --models agent.zip,agent.zip,agent.zip,agent.zip
```
## Use the model in your own code
```python
from huggingface_hub import hf_hub_download
from sb3_contrib import MaskablePPO
path = hf_hub_download("heitorvcm/ankh-ppo", "agent.zip")
model = MaskablePPO.load(path, device="cpu")
# obs: np.ndarray (6786,) from ankh_obs.Encoder(game).encode(decision)
# mask: np.ndarray (332,) from decision.mask
action, _ = model.predict(obs, action_masks=mask, deterministic=False)
```
The observation encoder and the rules live in this repository (`ankh_obs.py`, `ankh_game.py`); **the checkpoint is
useless without them**, and checkpoints from other versions of the encoder will not load.
## Train or continue training
```bash
python train_local.py --dir my_run # from scratch
python train_local.py --dir my_run2 --init-from agent.zip # new run starting from these weights
python train_local.py --dir my_run # run it again to resume (Ctrl+C saves a checkpoint)
python train_local.py --help
```
Useful options: `--players 2,3` and `--scenarios basic_2p,basic_4p` (what to train on), `--god-pool 0,4` (which gods),
`--round-steps`, `--total-rounds`, `--zero-sum` (symmetric zero-sum rewards), `--merged-reward zero` (no reward for
seats of a merged god). By default every game draws a random scenario (2–5 players), a random seat for the agent and
distinct random gods. CPU worker processes run the rules and the GPU runs the network in batches.
Each round writes to `--dir`: `checkpoints/`, `snapshots/` (the self-play opponents), `tb/` (TensorBoard),
`metrics.csv`, `report.jsonl`, `relatorio.md` (a detailed report in Portuguese) and `curvas.png` when matplotlib is
installed.
**Opponents:** random until the first snapshot exists; then about 50% of the time the latest snapshot and otherwise an
earlier one (mostly the 4 most recent).
**Reward:** +1 win, −1/(P−1) loss, −1 if every god is forgotten (0 with `--zero-sum`); small dense terms: +0.02 per
Devotion step gained, −0.01 × mean opponent steps, +0.005 per warrior added on the board, −0.0025 × mean opponent
change. The god merge itself gives and takes no reward (accumulated Devotion is used).
**Compute used for this checkpoint:** 40 rounds × 400k steps, ≈445 minutes on a single consumer PC.
## Results (read the caveats)
All numbers come from the training log of the last round (4,348 games against the round's snapshot opponent). Chance
is `1/players`.
| Players | Games | Agent win rate | Chance (1/P) |
|---|---|---|---|
| 2 | 1052 | 57% | 50% |
| 3 | 1081 | 48% | 33% |
| 4 | 1123 | 30% | 25% |
| 5 | 1092 | 24% | 20% |
Overall 39% vs. roughly 32% expected by chance with this mix of player counts. Against its previous snapshot in the
evaluation step (160 games, deterministic agent vs. sampling opponent) it won 54%.
## Limitations
- The only opponents were earlier copies of the same model. There is **no** evaluation against human players or
an independent baseline, and the evaluation against random opponents was not recorded for this run.
- The training win rate has been flat at ≈39–46% since round 29: no clear sign of continued improvement.
- Strong **seat effects**: in 2-player games the agent wins 85% as seat 0 and 27% as seat 1; with 4 players seat 0 wins
60% and the others 17–22%; with 5 players seat 0 is the *worst* (12%). The cause is unknown (turn order, event timing or
scenario setup).
- Being in a god merge happened in 40% of 3+ player games (about 52% expected for a random seat), so there is no sign
that the agent seeks the merge. This is a statistic over one run, not a guarantee.
## Intended use
Research, education and casual play against a bot. Not a strength benchmark for the real board game.
## Security note
Stable-Baselines3 checkpoints are zip files that use Python pickle. Only load checkpoints from sources you trust.
## Files
| File | What it is |
|---|---|
| `agent.zip` | the trained model |
| `ankh_game.py`, `ankh_map.py` | rules and board/scenario data |
| `ankh_obs.py` | observation encoder |
| `ankh_env.py`, `ankh_worker.py`, `ankh_vec.py` | training environments (single seat, CPU worker, batched VecEnv) |
| `ankh_eval.py`, `ankh_report.py` | evaluation and the per-run report written by the trainer |
| `train_local.py` | training script |
| `replay.py` | text replay of games |
| `play.py`, `play.html` | local web game against the AI (1–5 humans) |
| `requirements.txt` | Python dependencies |
| `LICENSE` | license of the code |
## License
Code and model: mit. The game itself, its name and artwork belong to their respective owners. |