Minigrid Documentation

repository·main·Indexed 25 days ago

https://github.com/farama-foundation/minigrid

A collection of lightweight, fast, and customizable discrete grid-world environments for Reinforcement Learning research. Following the Gymnasium API, Minigrid includes standard grid-world environments and BabyAI environments for grounded language learning. The library provides tools for creating custom environments by inheriting from MiniGridEnv and a variety of wrappers to transform observations, rewards, and action stochasticity.

Tokens
8.1K
Snippets
18
Records
64
Agent score
81%

What's inside minigrid

  1. Use Wave Function Collapse (WFC) Environments

    main

    Minigrid provides a suite of environments generated using the Wave Function Collapse (WFC) algorithm. These environments are designed to create diverse, procedurally generated layouts such as mazes, dungeons, rooms, and obstacle-filled spaces.

    Available WFC environment types include:

    • Mazes: WFCMazeEnv, WFCMazeSimpleEnv, WFCMazeKnotEnv, WFCMazeWallEnv, WFCMazeSpiralsEnv, WFCMazePathsEnv, WFCLikeEnv.
    • Dungeons: WFCDungeonEnv, WFCDungeonMazeScaledEnv, WFCDungeonRoomsEnv, WFCDungeonLessRoomsEnv, WFCDungeonSpiralsEnv.
    • Rooms: WFCRoomsFabricEnv, WFCRoomsOfficeEnv, WFCRoomsMagicOfficeEnv.
    • Obstacles/Skewed Layouts: WFCObstaclesBlackdotsEnv, WFCObstaclesAngularEnv, WFCObstaclesHogs3Env, WFCObstaclesHogs2Env, WFCSkew2Env, WFCSkewCaveEnv, WFCSkewLakeEnv.
    • General: WFCEnv.
  2. Explore Minigrid Environments

    main

    Minigrid provides a variety of grid-world environments implemented in the minigrid/envs directory. Each environment is registered with OpenAI Gym (or Gymnasium) and can be instantiated using standard environment IDs.

    Key features of these environments include:

    • Gym Registration: Environments are accessible via standard gym.make() calls using their specific IDs.
    • Programmatic Tuning: Environments are tunable in terms of size and complexity, allowing developers to adjust difficulty levels for tasks like curriculum learning or fine-tuning agent performance.
  3. Overview of BabyAI environments

    main

    BabyAI environments are imported from the BabyAI project and are designed for research on grounded language learning.

    Key characteristics include:

    • Foundation: They are derived from the standard Minigrid grid-world environments.
    • Language Instructions: They include functionality to generate synthetic, natural-looking instructions (e.g., “put the red ball next to the box on your left”).
    • Tasks: Agents must navigate the world and move objects to specified locations to fulfill these linguistic commands.
  4. Overview of Minigrid environments

    main

    Minigrid environments are discrete grid-world environments designed for Reinforcement Learning research. They follow the Gymnasium standard API.

    Key characteristics include:

    • Agent: A triangle-like agent with a discrete action space.
    • Map: A 2D map containing various obstacles such as Walls, Lava, or Dynamic obstacles.
    • Missions: Tasks are defined by a mission string returned in the agent's observation. Missions can include goal-oriented or hierarchical tasks like picking up boxes, opening doors with keys, or navigating mazes.
    • Customization: Environments are registered with Gymnasium configurations and are programmatically tunable in terms of size and complexity, making them suitable for curriculum learning.
  5. How to use MiniGrid Wrappers to transform environments

    main

    The Wrapper class is the base class for all environment transformations in MiniGrid. It allows you to modularly modify the step and reset methods of an environment without altering the original environment code.

    When creating a custom subclass of Wrapper, if you override the __init__ method, you must call super().__init__(env) to ensure the environment is correctly wrapped.

  6. Quickstart with Minigrid environments

    main

    Minigrid provides 2D grid-world environments with goal-oriented tasks using a Gymnasium interface. You can initialize and interact with environments using gym.make(). The agent uses a discrete action space and interacts with objects like doors, keys, or boxes within maze maps.

    To use Minigrid, ensure you have minigrid and gymnasium installed, then follow the standard Gymnasium loop pattern.

    import gymnasium as gym
    import minigrid
    
    env = gym.make("MiniGrid-Empty-5x5-v0", render_mode="human")
    observation, info = env.reset(seed=42)
    for _ in range(1000):
       action = policy(observation)  # User-defined policy function
       observation, reward, terminated, truncated, info = env.step(action)
    
       if terminated or truncated:
          observation, info = env.reset()
    env.close()
  7. Build the Minigrid documentation

    main

    To build the Minigrid documentation locally, you must first install the project in editable mode along with the documentation requirements. After that, you need to run the scripts that generate environment-specific documentation before building the HTML files.

    1. Install dependencies

    pip install -r docs/requirements.txt
    pip install -e .

    2. Generate environment documentation

    Run these scripts to populate the documentation with environment details:

    python docs/_scripts/gen_env_docs.py
    python docs/_scripts/gen_envs_display.py

    3. Build the HTML

    To perform a one-time build of the documentation using dirhtml format:

    cd docs
    make dirhtml
    pip install -r docs/requirements.txt
    pip install -e .
    python docs/_scripts/gen_env_docs.py
    python docs/_scripts/gen_envs_display.py
    cd docs
    make dirhtml
  8. Transform observation formats using MiniGrid wrappers

    main

    MiniGrid environments provide observations as dictionaries containing 'image', 'mission', and optionally 'direction'. To make these compatible with Reinforcement Learning (RL) algorithms that expect single tensors, you can use specific wrappers:

    • FlatObsWrapper: Flattens the observation into a single tensor.
    • ImgObsWrapper: Removes the 'mission' field, leaving only the 'image' field tensor.
    • RGBImgPartialObsWrapper: Converts the default compact encoding (which is not pixels) into an array of RGB pixels.

    Note that the default observation is a partially observable view using a compact encoding (7x7x3 values per observation) that is not composed of RGB pixels.

    import gymnasium as gym
    from minigrid.wrappers import RGBImgPartialObsWrapper, ImgObsWrapper
    
    env = gym.make('MiniGrid-Empty-8x8-v0')
    env = RGBImgPartialObsWrapper(env) # Get pixel observations
    env = ImgObsWrapper(env) # Get rid of the 'mission' field
    obs, _ = env.reset() # This now produces an RGB tensor only
  9. Train an agent in Minigrid

    main
    For examples and tested code on how to train Minigrid environments using Reinforcement Learning (RL) algorithms, refer to the rl-starter-files repository. The default hyperparameters in that repository are known to converge for these environments.
  10. Rebuild documentation automatically with sphinx-autobuild

    main

    If you are actively making changes to the documentation and want to see them reflected immediately, use sphinx-autobuild. This will watch for file changes and rebuild the documentation automatically.

    Run the following command from the docs directory:

    cd docs
    sphinx-autobuild -b dirhtml . _build
    cd docs
    sphinx-autobuild -b dirhtml . _build