Program synthesis for game-playing: evolve interpretable rule-based programs to play Snake.
SynthSnake searches over a tiny DSL of nested IF/ELSE rules with boolean sensors (danger ahead/left/right, relative food direction) and produces a readable policy program that plays the game. The entire pipeline is end-to-end and deterministic with seeds.
pip install -e .
python experiments/run_synthesis.py
python scripts/plot_results.py
python experiments/demo_best.pyArtifacts are written to artifacts/ and results to results/. The demo prints an ASCII animation in the terminal.
IF danger_right
IF food_ahead
IF danger_ahead
RIGHT
ELSE
IF food_dx_pos
FORWARD
ELSE
FORWARD
ELSE
IF danger_left
FORWARD
ELSE
RIGHT
ELSE
IF food_ahead
IF danger_ahead
IF food_ahead
RIGHT
ELSE
FORWARD
ELSE
FORWARD
ELSE
IF danger_right
IF food_right
RIGHT
ELSE
LEFT
ELSE
IF food_ahead
FORWARD
ELSE
LEFT
Sample run (10x10 grid, 60 generations, population 60, 3 episodes per eval, max steps 150).
| Metric | Value |
|---|---|
| Best score (reward) | 15.50 |
| Best food eaten | 17.0 |
| Generations | 60 |
| Population | 60 |
Plot of best and mean score by generation (from results/sample_results.csv):
Baseline: random policy (uniformly random action each step) typically achieves near-zero reward due to early collisions.
Synthesized search: evolutionary program synthesis steadily discovers safer rules and food-seeking behavior, improving both survival and food collection. The resulting policies are interpretable, auditable, and can be modified by hand.
src/synthsnake/env.py: Snake environmentsrc/synthsnake/dsl.py: DSL AST + serializationsrc/synthsnake/interpreter.py: Policy interpretersrc/synthsnake/search.py: Evolutionary searchsrc/synthsnake/eval.py: Evaluation utilitiessrc/synthsnake/render.py: ASCII rendererexperiments/run_synthesis.py: training scriptexperiments/demo_best.py: visual demoscripts/plot_results.py: plot resultstests/: pytest suite
All scripts accept --seed and use deterministic RNG for training and evaluation.
