English | 简体中文
A small Python maze demo that trains a Q-learning agent and visualizes both the training loop and the final greedy-path replay with pygame.
Q-Learning Maze is a compact reinforcement learning demo built in Python. It generates a solvable maze, trains an agent with Q-learning, renders the learning process with pygame, and replays the greedy path discovered after training.
The project is intentionally small enough to read end-to-end while still showing the full loop of environment design, value updates, epsilon-greedy exploration, and visual feedback.
- Random solvable maze generation with walls and traps
- Visualized Q-learning training loop rendered with
pygame - Greedy path replay after training completes
- Runtime tuning through
MAZE_EPISODESandMAZE_TRAINING_DELAY_MS - Automated tests for the maze environment and Q-learning agent
Create a virtual environment, activate it, and install the dependencies:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtStart the desktop demo with:
python main.pyIf your shell exposes only python3, use:
python3 main.pyFor non-GUI environments, run the demo with SDL's dummy video driver:
SDL_VIDEODRIVER=dummy MAZE_TRAINING_DELAY_MS=0 MAZE_EPISODES=5 python main.pyThis is useful for CI, remote shells, or quick runtime verification after environment changes.
Run the full test suite with:
python -m pytestmain.py: Training loop, runtime configuration,pygamerendering, and greedy-path replaymaze_env.py: Maze generation, solvability checks, state transitions, rewards, and drawing helpersrl_brain.py: Q-learning agent, epsilon-greedy action selection, value updates, and path extractiontests/: Unit tests for environment behavior, Q-learning updates, and runtime configuration
Python virtual environments store absolute paths internally. If the project is moved to a new directory, the existing .venv may become stale and activation can fall back to the system interpreter.
If that happens, recreate the environment and reinstall dependencies:
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt