How mapel0x/snake-ai-rl works
This is a Snake game-playing and training application: src/main.py runs training or greedy play, while the DQN learns from environment transitions and can be resumed from saved weights. The graph covers game rules, learning, visualization, and logged results. Training data and episode histories are written to CSV; plots are generated from histories. Headless training is supported. The README documents these capabilities, but some main.py calls are in omitted excerpts, so those relationships are cited to the README rather than inferred from module co-imports.
Main entry points: main.py Train / play · agent.py DQN agent · dataset_logger.py CSV logging · environment.py State and rewards
Components 18
Run orchestration
- Train / play
src/main.py - Game and RL config
src/config.py
Snake environment
- State and rewards
src/environment.py - Snake movement
src/snake.py - Food spawning
src/food.py - Pygame renderer
src/game.py
DQN learning
- DQN agent
src/agent.py - Q networks
src/model.py - Experience replay
src/replay_memory.py - Q-learning optimizer
src/trainer.py - Model weights
models/snake_model.pth
Logs and evaluation
- CSV logging
src/dataset_logger.py - Step dataset
dataset/snake_dataset.csv - Reward history
dataset/reward_history.csv - Score history
dataset/score_history.csv - Loss history
dataset/loss_history.csv - Training plots
graphs
Other components
- User
Connections 24
- User → Train / play: starts
README.md - Train / play → State and rewards: runs episodes
src/main.py - Train / play → DQN agent: selects actions
src/main.py - Train / play → Pygame renderer: renders play
src/main.py - Train / play → CSV logging: logs steps
src/main.py - Train / play → Game and RL config: uses parameters
src/main.py - State and rewards → Snake movement: moves and checks
src/environment.py - State and rewards → Food spawning: spawns and reads
src/environment.py - Pygame renderer → Snake movement: draws
src/game.py - Pygame renderer → Food spawning: draws
src/game.py - DQN agent → Q networks: predicts with
src/agent.py - DQN agent → Experience replay: stores transitions
src/agent.py - DQN agent → Q-learning optimizer: delegates learning
src/agent.py - Q-learning optimizer → Experience replay: samples batches
src/trainer.py - Q-learning optimizer → Q networks: optimizes policy
src/trainer.py - DQN agent → Model weights: saves and loads
src/agent.py - CSV logging → Step dataset: appends transitions
src/dataset_logger.py - CSV logging → Reward history: appends summaries
src/dataset_logger.py - CSV logging → Score history: appends summaries
src/dataset_logger.py - Train / play → Reward history: reads for plots
src/main.py - Train / play → Score history: reads for plots
src/main.py - Train / play → Loss history: reads for plots
src/main.py - Train / play → Training plots: writes charts
src/main.py - Train / play → Model weights: resumes training
README.md