Skip to content

Arcify/synth-snake

Repository files navigation

SynthSnake

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.

Quickstart

pip install -e .
python experiments/run_synthesis.py
python scripts/plot_results.py
python experiments/demo_best.py

Artifacts are written to artifacts/ and results to results/. The demo prints an ASCII animation in the terminal.

Example Synthesized Policy

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

Results

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):

Synthesis plot

Research framing

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.

Project layout

  • src/synthsnake/env.py: Snake environment
  • src/synthsnake/dsl.py: DSL AST + serialization
  • src/synthsnake/interpreter.py: Policy interpreter
  • src/synthsnake/search.py: Evolutionary search
  • src/synthsnake/eval.py: Evaluation utilities
  • src/synthsnake/render.py: ASCII renderer
  • experiments/run_synthesis.py: training script
  • experiments/demo_best.py: visual demo
  • scripts/plot_results.py: plot results
  • tests/: pytest suite

Reproducibility

All scripts accept --seed and use deterministic RNG for training and evaluation.

About

Snake game environment with multiple algorithms that synthesizes an interpretable policy program (rule-based DSL) that plays Snake. The “learning” is fully done by searching over programs (genetic programming / evolutionary search).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages