Utils#

maze_world.utils.maze_dijkstra_solver(impassable_array, motions, start_idx, goal_idx)[source]#

Solves a maze using Dijkstra’s algorithm.

Parameters:
  • impassable_array (array_like) – Array representing impassable cells in the maze.

  • motions (array_like) – List of possible motions in the maze.

  • start_idx (tuple) – Initial position of the agent (x, y).

  • goal_idx (tuple) – Position of the goal cell (x, y).

Returns:

List of actions to reach the goal from the start position.

Return type:

list

Example:
>>> import gymnasium as gym
>>> env = gym.make("maze_world:RandomMaze-11x11-v0")
>>> observation, info = env.reset(seed=0, options={})
>>> episode_score = 0.0
>>>
>>> optimal_actions = maze_dijkstra_solver(
>>>     env.unwrapped.maze_map.astype(bool),
>>>     env.unwrapped._action_to_direction.values(),
>>>     info["agent"],
>>>     info["target"],
>>> )
>>> for action in optimal_actions:
>>>     observation, reward, terminated, truncated, info = env.step(action)
>>>     episode_score += reward
>>>     if terminated or truncated:
>>>         break
>>> env.close()
>>> print("Success" if all(info["agent"] == info["target"]) else "Failure")
Success
class maze_world.utils.WilsonMazeGenerator[source]#

Maze Generator utilizing Wilson’s Loop Erased Random Walk Algorithm.

Source: https://github.com/CaptainFl1nt/WilsonMazeGenerator

__init__(height, width)[source]#

Initializes a maze generator with the specified width and height.

Parameters:
  • height (int) – Height of the generated mazes.

  • width (int) – Width of the generated mazes.

get_grid()[source]#

Retrieve the maze grid.

Returns:

The maze grid.

Return type:

list

get_solution()[source]#

Returns the solution to the maze as a list of tuples.

Returns:

The solution to the maze.

Return type:

list

show_solution(show)[source]#

Sets whether the __str__() method outputs the solution or not.

Parameters:

show (bool) – Boolean value indicating whether to show the solution or not.

generate_maze()[source]#

Generates the maze according to the Wilson Loop Erased Random Walk Algorithm.

The algorithm works as follows:
  1. Reset the grid before generation.

  2. Choose the first cell to put in the visited list.

  3. Loop until all cells have been visited:
    1. Choose a random cell to start the walk.

    2. Loop until the random walk reaches a visited cell.

    3. Loop until the end of the path is reached:
      • Add the cell to visited and cut into the maze.

      • Follow the direction to the next cell.

Returns:

None

solve_maze()[source]#

Solves the maze according to the Wilson Loop Erased Random Walk Algorithm

Returns:

None