Skip to content

Maze Generation

Jose Jimenez edited this page Jul 28, 2023 · 93 revisions

Randomly generating a maze, used to spawn new micromouse environments.

Source of Help

If you are familiar with Kruskal's Algorithm in creating a minimum spanning tree then this maze generation algorithm is a recap of Kruskal's Algorithm in a special use-case. Otherwise, if you are not familiar the following document does not assume so. Enjoy.


Definition

To define our maze, let any arbitrary pair of cells have a path to each other. For now, let us define that the arbitrary pair of cells only have one unique path to each other.

The maze can be represented by a graph where each node is a cell in the maze and an undirected edge between adjacent nodes represent an open path. Also, note by our definition the graph has no cycles because an "arbitrary pair of cells only have one unique path to each other".

Algorithm Approach

By the maze definition, every cell in the maze can be visited by running a depth-first search (DFS) on any starting cell. In other words, you can start at any cell in the maze and be guaranteed that you can walk to every other cell in the maze without having to jump over walls. In graph theory, this is a tree.

The problem that started off as creating a random maze is now boiled down to creating a random tree from a set of nodes. The approach we did on creating a random tree is known as Kruskal's algorithm. The pseudocode is as follows:

Example: G and G with no edges respectively

Algorithm Visualization

Visualization Concept
  1. Start off with a forest of nodes
  2. Make each node be its own set

  1. Pick a random edge: (u, v)
  2. If u and v are not in the same set, legitimize the edge and union disjoint sets.

  1. Repeat the last two steps...

...

Done.

Realistic Micromouse Environment

The problem with the approach above for a micromouse simulation is that there exists only one unique path from start cell to goal cell due to the tree structure of the maze. In this current environment, the micromouse is not tested to find the most optimal path in the maze.

To mitigate this issue, complexity is added to the maze by introducing cycles. This can be done by simply adding an arbitrary amount of complementary edges that are not present in the maze. Now in this new environment, the micromouse will be tested in a realistic emulation where a set of solutions exists and the micromouse will then have to find the shortest solution path.

Author: Jose Jimenez-Olivas
Date: January 9, 2020