rushhour-gym
Rush Hour as a Gymnasium environment.
The rules are not reimplemented here. They run in the Go program this repository is built around — the same code the human participants play — and are served to Python over a pipe. One action is one cell, which is exactly what one mouse click is for a participant, so an agent's trace and a participant's trace count the same events and can be compared directly.
If you have not used Gymnasium before, start with README-AI.md, which walks through the same ground slowly.
import gymnasium
import rushhour_gym # registers the environment ids
env = gymnasium.make("RushHour-Easy-v0")
obs, info = env.reset(seed=0)
terminated = truncated = False
while not (terminated or truncated):
legal = info["action_mask"].nonzero()[0]
obs, reward, terminated, truncated, info = env.step(legal[0])
print(info["puzzle"], "solved in", info["n_slides"], "moves;",
"the optimum is", info["min_moves"])
env.close()
Installing
pip install rushhour-gym # from PyPI
pip install -e python[dev] # from a checkout, for development
The Python package needs the rushhour-env binary. It is looked for in
$RUSHHOUR_ENV_BIN, then on PATH, then in the repository root, then built
from source when the checkout and a Go toolchain are at hand:
go build -o rushhour-env ./cmd/rushhour-env
Failing all of that -- the PyPI case -- it is fetched once from the GitHub
release whose tag is the package version (v + rushhour_gym.__version__),
verified against that release's SHA256SUMS, and kept in
~/.cache/rushhour-gym/<tag>/ ($XDG_CACHE_HOME is honoured). Releases carry
a binary for Linux x86-64, macOS arm64 and Windows x86-64; elsewhere, build it.
$RUSHHOUR_ENV_OFFLINE=1 forbids the download.
Because the package version names the release it fetches from, a release is
cut by bumping __version__ in src/rushhour_gym/__init__.py to the tag's
number and pushing the tag; the release workflow refuses a tag that does not
match, and publishes the package to PyPI after the archives.
The environment
| id | puzzles | step budget |
|---|---|---|
RushHour-v0 |
all 49 | 500 |
RushHour-Easy-v0 |
optimum ≤ 12 moves | 200 |
RushHourFixed-v0 |
p02, the classic board |
100 |
Action space — Discrete(32): action = slot * 2 + direction, direction 0
being left/up and 1 right/down. The vehicle moves exactly one cell, or nothing
happens.
Slot 0 is the red car on every board; slots 1 upward are the other vehicles in reading order of their starting position, frozen for the episode. Boards have between 7 and 15 vehicles, so the remaining slots are padding: their actions are always masked off and their observation rows are zero.
Observations — obs_mode picks the encoding, all built from the same wire
data:
| mode | space | for |
|---|---|---|
planes (default) |
Box(0, 1, (19, 6, 6)) |
one binary plane per slot, plus horizontal / vertical / exit-corridor planes |
grid |
Box(0, 16, (6, 6)) |
compact and readable; cell values are names, not quantities |
cars |
Box(0, 6, (16, 4)) |
row, col, length, orientation — for an MLP baseline |
dict |
Dict(grid, cars, action_mask) |
SB3's MultiInputPolicy |
Whichever you pick, slot identity is visible in the observation: without it an
agent cannot tell which vehicle action 2k refers to.
Masking — info["action_mask"] on every reset and step, and an
action_masks() method, which is the name sb3-contrib's MaskablePPO looks
for.
Rewards — reward_scheme:
step_penalty(default):-1per step. The return is minus the number of cells moved, so the optimal policy is the shortest solution in clicks — the quantity the human data measures.sparse:+1on solving,0otherwise.shaped: step penalty plus potential-based shaping on the optimal distance to go. Policy-invariant, but it runs a search per step; fine for teaching, far too slow for the hard end of the library.
Termination — terminated means solved, and nothing else: Rush Hour has no
dead ends, since every move is reversible. An unsolved episode therefore ends
only by TimeLimit truncation, which is why a bare RushHourEnv() never
truncates while the registered ids do.
Vector environments
RushHourVectorEnv runs every board in one child process and advances them all
in a single request:
envs = gymnasium.make_vec("RushHour-Easy-v0", num_envs=16)
The step cost here is the pipe round trip, not the game, so 16 sub-processes
would be 16× the overhead for no gain. SyncVectorEnv/AsyncVectorEnv over
plain RushHourEnv still work if you want them.
Curricula
The optimal move count of every puzzle arrives in the handshake, so selection needs no extra round trips:
env = RushHourEnv(min_moves_range=(3, 12)) # at construction
env.set_puzzle_filter(min_moves_range=(3, 20)) # between episodes
env.reset(options={"puzzle": "p07"}) # a specific board
env.reset(options={"spec": "BCCCoo BoooDo oAAEDo oooEoo FFoEoo ooGGGo"})
All randomness lives in reset, driven by self.np_random, so a seeded run is
reproducible.
Baselines
rushhour_gym.run_optimal plays a breadth-first optimal solution — the ceiling
any learning curve should be read against, and the generator for
behaviour-cloning data. run_random is the floor. Neither learns anything.
from rushhour_gym import RushHourEnv, run_optimal
with RushHourEnv(puzzle="p02") as env:
print(run_optimal(env))
Talking to the server directly
The protocol is one JSON object per line and is meant to be driveable by hand:
$ ./rushhour-env -board
{"id":1,"cmd":"hello"}
{"id":2,"cmd":"reset","puzzle":"p02"}
{"id":3,"cmd":"step","action":12}
Commands: hello, reset, step, state, reset_batch, step_batch,
solve, close. The server reports facts and never a reward — the reward
scheme, the termination rule and the observation tensor all live in Python, so
changing them never means rebuilding Go.
Notes for training
- 6×6 is too small for SB3's
NatureCNN, which wants at least 36×36. UseMlpPolicyon a flattened observation, or a small custom extractor with 3×3 convolutions and no downsampling. - A random walk on a hard board can wander for a very long time without ever
being stuck. Start on
RushHour-Easy-v0. - Measured throughput: 28k steps/s for a single environment (36 µs a step)
and 35k for
RushHourVectorEnv(16), on an Intel Core Ultra 7 165H. It is dominated by the pipe round trip and Python'sjson, not by the game. If that ever binds, batch first, then tryorjson.
Metadata
Release files for rushhour-gym 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rushhour_gym-0.4.0.tar.gz | 28.1 kB | With provenance | Details for rushhour_gym-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rushhour_gym-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for rushhour_gym-0.4.0-py3-none-any.whl |
Total release size: 52.2 kB
Release files / rushhour_gym-0.4.0.tar.gz
| Download URL | rushhour_gym-0.4.0.tar.gz |
|---|---|
| Size | 28.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e1d25e7ba357fca64d5bab19838800e275c999339e8a3bf89f7c7715e73d8781
|
|
BLAKE2b-256 checksum How to use checksums |
22aea2bb1922f528b7a6684ebdad02cf25253cebc3e6c6ac8d28100bc1148656
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.
Transparency logRelease files / rushhour_gym-0.4.0-py3-none-any.whl
| Download URL | rushhour_gym-0.4.0-py3-none-any.whl |
|---|---|
| Size | 24.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06ef3406664c6842584c9dc156cfd6c7062ff1387b3c5ab00ae7d95a0f762188
|
|
BLAKE2b-256 checksum How to use checksums |
08c0dd6c6384504c39a673b98a312b58e2a9a4c72893b3c010a504ac34bb4de0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.
Transparency logSecurity for rushhour-gym 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for rushhour_gym-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.