Quick-Start#

In the section, we understand minimal and sufficient usage of maze-world framework. We begin by importing the packages

[4]:
import gymnasium as gym
import numpy as np

Basic Usage#

[5]:
env = gym.make("maze_world:RandomMaze-11x11-v0")
terminated, truncated = False, False
observation, info = env.reset(seed=0, options={})
episode_score = 0.0

while not (terminated or truncated):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)
    episode_score += reward
env.close()

Creating custom random maze size#

[6]:
gym.envs.register(
    id="RandomMaze-7x7-v0",
    entry_point="maze_world.envs:RandomMazeEnv",
    max_episode_steps=200,
    kwargs={
        "maze_width": 7,
        "maze_height": 7,
        "maze_complexity": 1,
        "maze_density": 1,
    },
)
[8]:
env = gym.make("maze_world:RandomMaze-7x7-v0")
terminated, truncated = False, False
observation, info = env.reset(seed=0, options={})
episode_score = 0.0

while not (terminated or truncated):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)
    episode_score += reward
env.close()

Creating maze with pre-specified map#

[9]:
def _generate_maze_fn():
    # This function would be called on every reset

    maze_map = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        ]
    )
    agent_loc = np.array([1, 1])
    target_loc = np.array([7, 1])
    return maze_map, agent_loc, target_loc


gym.envs.register(
    id="UMaze-v0",
    entry_point="maze_world.envs:MazeEnv",
    max_episode_steps=200,
    kwargs={
        "generate_maze_fn": _generate_maze_fn,
        "maze_height": 9,
        "maze_width": 10,
    },
)
/Users/anuragkoul/.virtualenv/maze-world/lib/python3.9/site-packages/gymnasium/envs/registration.py:694: UserWarning: WARN: Overriding environment UMaze-v0 already in registry.
  logger.warn(f"Overriding environment {new_spec.id} already in registry.")
[10]:
env = gym.make("maze_world:UMaze-v0")
terminated, truncated = False, False
observation, info = env.reset(seed=0, options={})
episode_score = 0.0

while not (terminated or truncated):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)
    episode_score += reward
env.close()