Docs


Maze Solver

A genetic algorithm evolves a fixed plan of moves that threads a fresh perfect maze from the start corner to the exit.

Last updated June 3, 2026

Go to the live page Open the Maze Solver demo

The Maze Solver demo builds a real maze in front of you and asks the genetic algorithm to find a way through it. There is exactly one path from the start corner to the exit corner. The GA does not feel its way along step by step. Instead it evolves a plan: a fixed list of moves (up, right, down, left) that a walker follows in order, the whole route decided in advance.

The maze

Every run draws a brand new maze. It is a perfect maze, which means there are no loops and no closed-off rooms. Between any two cells there is one and only one path. That makes it a fair, solvable puzzle that still looks different each time. Press Reset and you get a different maze and a fresh population of plans.

How a run looks

Click Start. Fifteen walkers set off at the same time, each following its own candidate plan. The best plan of the generation, the one that gets closest to the exit, is highlighted and traces its route as it goes. The other fourteen show up as fainter markers stepping through their own moves. A move that runs into a wall is wasted, so the walker stays put, which is exactly the pressure that teaches the population where the open corridors are.

When the animation finishes, the platform evolves the population. The fittest plans become parents, their moves are recombined and nudged by mutation, and the next generation arrives a moment later to try again. The history strip under the canvas keeps the best plan's path from every generation so you can scroll back and watch the route reach deeper into the maze.

What fitness rewards

A plan earns more for getting closer to the exit, measured as how far it still has to go through the corridors. Reaching the exit is a big jump up, and once a plan can reach the exit, shorter plans score higher than longer ones. So the population first learns to escape at all, then learns to escape efficiently.

The loop at a glance

Your browser owns the maze and the scoring. The server only evolves the plans.

sequenceDiagram participant B as Your browser participant S as The server B->>B: build a fresh maze, measure distance to the exit S->>B: a generation of candidate plans loop each generation B->>B: walk each plan, score how close it gets to the exit B->>S: the scores S->>B: the next, fitter generation end Note over B: Reset builds a brand new maze and starts over

Settings to try

The Settings panel lets you change the maze size, from a quick six-by-six up to a sixteen-by-sixteen. Bigger mazes need longer plans and more generations to solve. You can also tune the genetic algorithm itself: mutation probability and range, tournament size, how many elites carry over, and the maximum number of generations. Each change applies on the next Reset.

For more on how the demos catalog works, see What the demos do.

Thanks for the feedback!