45b37f9357743afb4cbbf68a0548aadf5714690c
- Replace Manhattan distance with BFS distances (actual maze path length) - Switch from NN to direct sequence genome (what YouTube Shorts actually use) - Mutation-only GA (crossover breaks positional maze paths) - Two-rate mutation: low rate before best-step (preserve prefix), high after (explore tail) - Auto-seed selection finds maze with short BFS path - Default maze 15x15, cell_size=36, max_steps=150 - Typically converges in 5-15 generations
maze-ml
100 neural-network agents learn to navigate a maze using a Genetic Algorithm — animated in real time with pygame.
Inspired by the "AI learns to..." YouTube Shorts format.
Quick start
pip install -r requirements.txt
python main.py
Options
| Flag | Default | Description |
|---|---|---|
--fast |
off | Train without rendering, show result at end |
--generations |
200 | Number of GA generations |
--maze-size |
25 | Maze dimensions (NxN) |
--seed |
random | Maze seed for reproducibility |
--speed |
60 | FPS cap for visual mode |
Architecture
config.py— all hyperparameters as a dataclassmaze.py— iterative DFS maze generation (bitmask cells)agent.py— Agent + tiny NeuralNet (8→12→4, pure numpy)genetic.py— tournament selection, uniform crossover, gaussian mutationvisualizer.py— pygame renderer with trail decay, fitness-ranked colors, HUDmain.py— training loop + argparse
How it works
Each generation, 100 agents simultaneously traverse the maze controlled by small neural networks (8 inputs → 12 hidden → 4 outputs). Fitness rewards progress toward the goal and reaching it. The top 30% of agents are selected as parents for the next generation via tournament selection.
After ~20–50 generations, agents learn to reliably navigate the maze.
Description
100 neural-network agents learn to navigate a maze via Genetic Algorithm � animated with pygame
Languages
Python
100%