### Install Minigrid from Source Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/installation.md Navigate to the cloned repository and install Minigrid using pip. ```bash cd Minigrid python3 -m pip install . ``` -------------------------------- ### Install Minigrid from Source Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/basic_usage.md Installs Minigrid from its source code, typically for development or contribution. This involves cloning the repository and performing an editable installation. ```bash git clone https://github.com/Farama-Foundation/Minigrid.git cd Minigrid python3 -m pip install -e . ``` -------------------------------- ### Install Dependencies and Minigrid Source: https://github.com/farama-foundation/minigrid/blob/main/docs/README.md Installs the necessary packages for documentation and the Minigrid project itself. ```bash pip install -r docs/requirements.txt pip install -e . ``` -------------------------------- ### Install Minigrid using pip Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/installation.md Use this command to install the Minigrid library directly from PyPI. ```bash pip install minigrid ``` -------------------------------- ### Initialize and Interact with Minigrid Environment Source: https://github.com/farama-foundation/minigrid/blob/main/docs/index.md Demonstrates how to initialize a Minigrid environment using the Gymnasium interface and interact with it through a policy. Ensure the 'minigrid' library is installed. ```python 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() ``` -------------------------------- ### Install Minigrid via Pip Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/basic_usage.md Installs the Minigrid package using pip for general use. ```bash python3 -m pip install minigrid ``` -------------------------------- ### Install Minigrid in Development Mode Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/installation.md Install Minigrid from source in development (editable) mode. Changes to the source code will take effect immediately without reinstallation. ```bash python3 -m pip install -e . ``` -------------------------------- ### Symbolic Observation Wrapper Example Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Shows how to use the SymbolicObsWrapper to get a fully observable grid with a symbolic state representation. The symbolic state is a triple (X, Y, IDX) representing coordinates and object ID. ```python import gymnasium as gym from minigrid.wrappers import SymbolicObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") obs, _ = env.reset() print(obs['image'].shape) env_obs = SymbolicObsWrapper(env) obs, _ = env_obs.reset() print(obs['image'].shape) ``` -------------------------------- ### Define a Simple Custom Environment Class Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Create a class that inherits from MiniGridEnv and initializes the environment with a custom mission space and starting parameters. ```python class SimpleEnv(MiniGridEnv): def __init__( self, size=8, agent_start_pos=(1, 1), agent_start_dir=0, max_steps: int | None = None, **kwargs, ): self.agent_start_pos = agent_start_pos self.agent_start_dir = agent_start_dir mission_space = MissionSpace(mission_func=self._gen_mission) super().__init__( mission_space=mission_space, grid_size=size, max_steps=256, **kwargs, ) ``` -------------------------------- ### Clone Minigrid Repository Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/installation.md Clone the Minigrid repository from GitHub to install from source. ```bash git clone https://github.com/Farama-Foundation/Minigrid.git ``` -------------------------------- ### Action Bonus Wrapper Example Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Demonstrates how the ActionBonus wrapper provides an exploration reward for visiting new (state, action) pairs. The original environment returns 0 reward, while the wrapped environment returns 1.0. ```python >>> import gymnasium as gym >>> from minigrid.wrappers import ActionBonus >>> env = gym.make("MiniGrid-Empty-5x5-v0") >>> _, _ = env.reset(seed=0) >>> _, reward, _, _, _ = env.step(1) >>> print(reward) 0 >>> _, reward, _, _, _ = env.step(1) >>> print(reward) 0 >>> env_bonus = ActionBonus(env) >>> _, _ = env_bonus.reset(seed=0) >>> _, reward, _, _, _ = env_bonus.step(1) >>> print(reward) 1.0 >>> _, reward, _, _, _ = env_bonus.step(1) >>> print(reward) 1.0 ``` -------------------------------- ### Position Bonus Wrapper Example Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Demonstrates how to use the PositionBonus wrapper to add a scaled exploration bonus based on visited grid positions. This wrapper was formerly known as StateBonus. ```python import gymnasium as gym from minigrid.wrappers import PositionBonus env = gym.make("MiniGrid-Empty-5x5-v0") _, _ = env.reset(seed=0) _, reward, _, _, _ = env.step(1) print(reward) _, reward, _, _, _ = env.step(1) print(reward) env_bonus = PositionBonus(env, scale=1) obs, _ = env_bonus.reset(seed=0) obs, reward, terminated, truncated, info = env_bonus.step(1) print(reward) obs, reward, terminated, truncated, info = env_bonus.step(1) print(reward) ``` -------------------------------- ### Dict Observation Space Wrapper Example Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Shows how DictObservationSpaceWrapper converts a textual mission string into a numerical array of word indices. This is useful for environments with textual instructions that need to be processed numerically. ```python >>> import gymnasium as gym >>> import matplotlib.pyplot as plt >>> from minigrid.wrappers import DictObservationSpaceWrapper >>> env = gym.make("MiniGrid-LavaCrossingS11N5-v0") >>> obs, _ = env.reset() >>> obs['mission'] 'avoid the lava and get to the green goal square' >>> env_obs = DictObservationSpaceWrapper(env) >>> obs, _ = env_obs.reset() >>> obs['mission'][:10] [19, 31, 17, 36, 20, 38, 31, 2, 15, 35] ``` -------------------------------- ### SimpleEnv Class Definition for MiniGrid Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Defines a custom MiniGrid environment named SimpleEnv. It sets up the grid size, agent starting position and direction, and generates the mission. This class is suitable for creating custom game levels. ```python from __future__ import annotations from minigrid.core.constants import COLOR_NAMES from minigrid.core.grid import Grid from minigrid.core.mission import MissionSpace from minigrid.core.world_object import Door, Goal, Key, Wall from minigrid.manual_control import ManualControl from minigrid.minigrid_env import MiniGridEnv class SimpleEnv(MiniGridEnv): def __init__( self, size=10, agent_start_pos=(1, 1), agent_start_dir=0, max_steps: int | None = None, **kwargs, ): self.agent_start_pos = agent_start_pos self.agent_start_dir = agent_start_dir mission_space = MissionSpace(mission_func=self._gen_mission) if max_steps is None: max_steps = 4 * size**2 super().__init__( mission_space=mission_space, grid_size=size, # Set this to True for maximum speed see_through_walls=True, max_steps=max_steps, **kwargs, ) @staticmethod def _gen_mission(): return "grand mission" def _gen_grid(self, width, height): # Create an empty grid self.grid = Grid(width, height) # Generate the surrounding walls self.grid.wall_rect(0, 0, width, height) # Generate vertical separation wall for i in range(0, height): self.grid.set(5, i, Wall()) # Place the door and key self.grid.set(5, 6, Door(COLOR_NAMES[0], is_locked=True)) self.grid.set(3, 6, Key(COLOR_NAMES[0])) # Place a goal square in the bottom-right corner self.put_obj(Goal(), width - 2, height - 2) # Place the agent if self.agent_start_pos is not None: self.agent_pos = self.agent_start_pos self.agent_dir = self.agent_start_dir else: self.place_agent() self.mission = "grand mission" ``` -------------------------------- ### Get Goal Direction Observation Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper adds the slope or angular direction to the goal to the observations. Use this when the agent needs to know its directional relationship to the goal. ```python import gymnasium as gym import matplotlib.pyplot as plt from minigrid.wrappers import DirectionObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") env_obs = DirectionObsWrapper(env, type="slope") obs, _ = env_obs.reset() obs['goal_direction'].item() ``` -------------------------------- ### OneHotPartialObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Wrapper to get a one-hot encoding of a partially observable agent view as observation. It modifies the observation space to provide a one-hot representation of the agent's view. ```APIDOC ## class minigrid.wrappers.OneHotPartialObsWrapper(env, tile_size=8) ### Description Wrapper to get a one-hot encoding of a partially observable agent view as observation. ### Parameters * **env**: The environment to wrap. * **tile_size**: The size of each tile in the one-hot encoding. Defaults to 8. ### Example ```python import gymnasium as gym from minigrid.wrappers import OneHotPartialObsWrapper env = gym.make("MiniGrid-Empty-5x5-v0") obs, _ = env.reset() env = OneHotPartialObsWrapper(env) obs, _ = env.reset() ``` ``` -------------------------------- ### Build Documentation Locally Source: https://github.com/farama-foundation/minigrid/blob/main/docs/README.md Builds the Minigrid documentation once by navigating to the docs directory and running the make command. ```bash cd docs make dirhtml ``` -------------------------------- ### Generate Environment Documentation Source: https://github.com/farama-foundation/minigrid/blob/main/docs/README.md Generates documentation files for the Minigrid environments using provided scripts. ```bash python docs/_scripts/gen_env_docs.py python docs/_scripts/gen_envs_display.py ``` -------------------------------- ### Rebuild Documentation Automatically Source: https://github.com/farama-foundation/minigrid/blob/main/docs/README.md Sets up automatic rebuilding of the Minigrid documentation whenever changes are detected. Requires navigating to the docs directory first. ```bash cd docs sphinx-autobuild -b dirhtml . _build ``` -------------------------------- ### Run Manual Control UI with Specific Environment Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/basic_usage.md Launches the manual control UI and specifies the environment ID to be used. ```bash ./minigrid/manual_control.py --env-id MiniGrid-Empty-8x8-v0 ``` -------------------------------- ### Run Manual Control UI Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/basic_usage.md Launches the interactive UI for manual agent control using arrow keys. ```bash ./minigrid/manual_control.py ``` -------------------------------- ### Apply RGB Image Observation Wrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Demonstrates how to wrap a Minigrid environment to use fully observable RGB images as observations. This is useful for agents that operate in pixel space. Requires gymnasium, matplotlib, and the specific wrapper. ```python import gymnasium as gym import matplotlib.pyplot as plt from minigrid.wrappers import RGBImgObsWrapper env = gym.make("MiniGrid-Empty-5x5-v0") obs, _ = env.reset() plt.imshow(obs['image']) ![NoWrapper](../figures/lavacrossing_NoWrapper.png) env = RGBImgObsWrapper(env) obs, _ = env.reset() plt.imshow(obs['image']) ![RGBImgObsWrapper](../figures/lavacrossing_RGBImgObsWrapper.png) ``` -------------------------------- ### Main Function to Run SimpleEnv with Manual Control Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Initializes and runs the custom SimpleEnv with human rendering enabled. It also sets up manual control for interactive testing of the environment. ```python def main(): env = SimpleEnv(render_mode="human") # enable manual control for testing manual_control = ManualControl(env, seed=42) manual_control.start() if __name__ == "__main__": main() ``` -------------------------------- ### Apply RGB Partial Image Observation Wrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Shows how to wrap a Minigrid environment for partially observable RGB image observations. This wrapper is also for agents operating in pixel space. It requires gymnasium, matplotlib, and both RGB image wrappers. ```python import gymnasium as gym import matplotlib.pyplot as plt from minigrid.wrappers import RGBImgObsWrapper, RGBImgPartialObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") obs, _ = env.reset() plt.imshow(obs["image"]) ![NoWrapper](../figures/lavacrossing_NoWrapper.png) env_obs = RGBImgObsWrapper(env) obs, _ = env_obs.reset() plt.imshow(obs["image"]) ![RGBImgObsWrapper](../figures/lavacrossing_RGBImgObsWrapper.png) env_obs = RGBImgPartialObsWrapper(env) obs, _ = env_obs.reset() plt.imshow(obs["image"]) ![RGBImgPartialObsWrapper](../figures/lavacrossing_RGBImgPartialObsWrapper.png) ``` -------------------------------- ### Environment Reset with Predefined Seeds Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper ensures the environment is reset with a consistent sequence of seeds, allowing for reproducible environment configurations. It cycles through a provided list of seeds. ```python import minigrid import gymnasium as gym from minigrid.wrappers import ReseedWrapper env = gym.make("MiniGrid-Empty-5x5-v0") _ = env.reset(seed=123) print([env.np_random.integers(10).item() for i in range(10)]) env = ReseedWrapper(env, seeds=[0, 1], seed_idx=0) _, _ = env.reset() print([env.np_random.integers(10).item() for i in range(10)]) _, _ = env.reset() print([env.np_random.integers(10).item() for i in range(10)]) _, _ = env.reset() print([env.np_random.integers(10).item() for i in range(10)]) _, _ = env.reset() print([env.np_random.integers(10).item() for i in range(10)]) ``` -------------------------------- ### Applying RGBImgPartialObsWrapper and ImgObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrapper.md This snippet demonstrates how to wrap a MiniGrid environment to obtain RGB pixel observations and remove the mission field. It shows the necessary imports and the order of wrapper application. ```python 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 ``` -------------------------------- ### Citation for BabyAI Environments Source: https://github.com/farama-foundation/minigrid/blob/main/README.md BibTeX entry for citing the BabyAI platform, used for studying grounded language learning. ```bibtex @article{chevalier2018babyai, title={Babyai: A platform to study the sample efficiency of grounded language learning}, author={Chevalier-Boisvert, Maxime and Bahdanau, Dzmitry and Lahlou, Salem and Willems, Lucas and Saharia, Chitwan and Nguyen, Thien Huu and Bengio, Yoshua}, journal={arXiv preprint arXiv:1810.08272}, year={2018} } ``` -------------------------------- ### Initialize Mission Space Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Instantiate MissionSpace by passing the mission generation function. ```python mission_space = MissionSpace(mission_func=self._gen_mission) ``` -------------------------------- ### Add Keys and Doors Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Import necessary classes for keys and doors, then use the grid.set method to place them at specific coordinates. Doors can be configured as locked. ```python from minigrid.core.constants import COLOR_NAMES from minigrid.core.world_object import Door, Key ``` ```python self.grid.set(5, 6, Door(COLOR_NAMES[0], is_locked=True)) self.grid.set(3, 6, Key(COLOR_NAMES[0])) ``` -------------------------------- ### Citation for Minigrid Environments Source: https://github.com/farama-foundation/minigrid/blob/main/README.md BibTeX entry for citing the Minigrid and Miniworld environments. ```bibtex @inproceedings{MinigridMiniworld23, author = {Maxime Chevalier{-}Boisvert and Bolun Dai and Mark Towers and Rodrigo Perez{-}Vicente and Lucas Willems and Salem Lahlou and Suman Pal and Pablo Samuel Castro and Jordan Terry}, title = {Minigrid {\&} Miniworld: Modular {\&} Customizable Reinforcement Learning Environments for Goal-Oriented Tasks}, booktitle = {Advances in Neural Information Processing Systems 36, New Orleans, LA, USA}, month = {December}, year = {2023}, } ``` -------------------------------- ### Train PPO Agent on Minigrid Environment Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/training.md Train a PPO agent on the MiniGrid-Empty-16x16-v0 environment using a custom feature extractor. The environment is wrapped with ImgObsWrapper to convert dictionary observations to image observations for the CnnPolicy. ```python import minigrid from minigrid.wrappers import ImgObsWrapper from stable_baselines3 import PPO policy_kwargs = dict( features_extractor_class=MinigridFeaturesExtractor, features_extractor_kwargs=dict(features_dim=128), ) env = gym.make("MiniGrid-Empty-16x16-v0", render_mode="rgb_array") env = ImgObsWrapper(env) model = PPO("CnnPolicy", env, policy_kwargs=policy_kwargs, verbose=1) model.learn(2e5) ``` -------------------------------- ### Fully Observable Grid Observation Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper provides a fully observable gridworld by using a compact grid encoding instead of the agent's limited view. Use this when the agent needs to perceive the entire environment state. ```python import gymnasium as gym import matplotlib.pyplot as plt from minigrid.wrappers import FullyObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") obs, _ = env.reset() obs['image'].shape ``` ```python env_obs = FullyObsWrapper(env) obs, _ = env_obs.reset() obs['image'].shape ``` -------------------------------- ### Generate the Grid Layout Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Override the _gen_grid method to define the structure of the environment. This involves creating an empty grid, adding walls, and placing the agent. ```python # MiniGridEnv._gen_grid @abstractmethod def _gen_grid(self, width, height): pass ``` ```python self.grid = Grid(width, height) ``` ```python self.grid.wall_rect(0, 0, width, height) ``` ```python if self.agent_start_pos is not None: self.agent_pos = self.agent_start_pos self.agent_dir = self.agent_start_dir else: self.place_agent() ``` -------------------------------- ### Create Separating Walls Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Add walls to divide the environment into sections by iterating through grid coordinates and setting them as walls. ```python for i in range(0, height): self.grid.set(5, i, Wall()) ``` -------------------------------- ### Customize Agent View Size with ViewSizeWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Demonstrates how to use ViewSizeWrapper to change the agent's field of view size. This wrapper modifies the observation image shape. ```python >>> import gymnasium as gym >>> from minigrid.wrappers import ViewSizeWrapper >>> env = gym.make("MiniGrid-LavaCrossingS11N5-v0") >>> obs, _ = env.reset() >>> obs['image'].shape (7, 7, 3) >>> env_obs = ViewSizeWrapper(env, agent_view_size=5) >>> obs, _ = env_obs.reset() >>> obs['image'].shape (5, 5, 3) ``` -------------------------------- ### ImgObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper modifies the environment to use only the image as the observation output, excluding language/mission information. ```APIDOC ## class minigrid.wrappers.ImgObsWrapper(env) ### Description Use the image as the only observation output, no language/mission. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python >>> import gymnasium as gym >>> from minigrid.wrappers import ImgObsWrapper >>> env = gym.make("MiniGrid-Empty-5x5-v0") >>> obs, _ = env.reset() >>> obs.keys() dict_keys(['image', 'direction', 'mission']) >>> env = ImgObsWrapper(env) >>> obs, _ = env.reset() >>> obs.shape (7, 7, 3) ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Use Image as Observation - ImgObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper modifies the environment to use only the image as the observation, removing language and mission components. It's useful when the agent only needs visual input. ```python >>> import gymnasium as gym >>> from minigrid.wrappers import ImgObsWrapper >>> env = gym.make("MiniGrid-Empty-5x5-v0") >>> obs, _ = env.reset() >>> obs.keys() dict_keys(['image', 'direction', 'mission']) >>> env = ImgObsWrapper(env) >>> obs, _ = env.reset() >>> obs.shape (7, 7, 3) ``` -------------------------------- ### Place Goal Object Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Use the put_obj method to place a Goal object at a specified position in the grid. ```python self.put_obj(Goal(), width - 2, height - 2) ``` -------------------------------- ### ViewSizeWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Initializes the ViewSizeWrapper. This wrapper customizes the agent's field of view size. It cannot be used in conjunction with fully observable wrappers. ```APIDOC ## class minigrid.wrappers.ViewSizeWrapper(env, agent_view_size=7) ### Description Wrapper to customize the agent field of view size. This cannot be used with fully observable wrappers. ### Parameters * **env**: The environment to wrap. * **agent_view_size**: The desired size of the agent's view. Defaults to 7. ### Example ```pycon >>> import gymnasium as gym >>> from minigrid.wrappers import ViewSizeWrapper >>> env = gym.make("MiniGrid-LavaCrossingS11N5-v0") >>> obs, _ = env.reset() >>> obs['image'].shape (7, 7, 3) >>> env_obs = ViewSizeWrapper(env, agent_view_size=5) >>> obs, _ = env_obs.reset() >>> obs['image'].shape (5, 5, 3) ``` ``` -------------------------------- ### ActionBonus Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Wrapper which adds an exploration bonus to encourage visiting less-visited (state, action) pairs. ```APIDOC ## class minigrid.wrappers.ActionBonus(env) ### Description Wrapper which adds an exploration bonus. This is a reward to encourage exploration of less visited (state,action) pairs. ### Example ```pycon >>> import gymnasium as gym >>> from minigrid.wrappers import ActionBonus >>> env = gym.make("MiniGrid-Empty-5x5-v0") >>> _, _ = env.reset(seed=0) >>> _, reward, _, _, _ = env.step(1) >>> print(reward) 0 >>> _, reward, _, _, _ = env.step(1) >>> print(reward) 0 >>> env_bonus = ActionBonus(env) >>> _, _ = env_bonus.reset(seed=0) >>> _, reward, _, _, _ = env_bonus.step(1) >>> print(reward) 1.0 >>> _, reward, _, _, _ = env_bonus.step(1) >>> print(reward) 1.0 ``` ``` -------------------------------- ### Flattened Observation Wrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper encodes mission strings and combines them with observed images into a single flat array. It is not applicable to BabyAI environments. ```python import gymnasium as gym import matplotlib.pyplot as plt from minigrid.wrappers import FlatObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") env_obs = FlatObsWrapper(env) obs, _ = env_obs.reset() obs.shape ``` -------------------------------- ### minigrid.wrappers.PositionBonus Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Adds a scaled exploration bonus based on which positions are visited on the grid. This wrapper was previously called StateBonus. ```APIDOC ## class minigrid.wrappers.PositionBonus(env, scale=1) ### Description Adds a scaled exploration bonus based on which positions are visited on the grid. ### Parameters * **env**: The environment to wrap. * **scale**: The scaling factor for the bonus. Defaults to 1. ### Example ```python >>> import gymnasium as gym >>> from minigrid.wrappers import PositionBonus >>> env = gym.make("MiniGrid-Empty-5x5-v0") >>> _, _ = env.reset(seed=0) >>> _, reward, _, _, _ = env.step(1) >>> print(reward) 0 >>> _, reward, _, _, _ = env.step(1) >>> print(reward) 0 >>> env_bonus = PositionBonus(env, scale=1) >>> obs, _ = env_bonus.reset(seed=0) >>> obs, reward, terminated, truncated, info = env_bonus.step(1) >>> print(reward) 1.0 >>> obs, reward, terminated, truncated, info = env_bonus.step(1) >>> print(reward) 0.7071067811865475 ``` ``` -------------------------------- ### minigrid.wrappers.SymbolicObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Fully observable grid with a symbolic state representation. The symbol is a triple of (X, Y, IDX), where X and Y are the coordinates on the grid, and IDX is the id of the object. ```APIDOC ## class minigrid.wrappers.SymbolicObsWrapper(env) ### Description Fully observable grid with a symbolic state representation. The symbol is a triple of (X, Y, IDX), where X and Y are the coordinates on the grid, and IDX is the id of the object. ### Parameters * **env**: The environment to wrap. ### Example ```python >>> import gymnasium as gym >>> from minigrid.wrappers import SymbolicObsWrapper >>> env = gym.make("MiniGrid-LavaCrossingS11N5-v0") >>> obs, _ = env.reset() >>> obs['image'].shape (7, 7, 3) >>> env_obs = SymbolicObsWrapper(env) >>> obs, _ = env_obs.reset() >>> obs['image'].shape (11, 11, 3) ``` ``` -------------------------------- ### FullyObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Provides a fully observable gridworld using a compact grid encoding instead of the agent's view. ```APIDOC ## class minigrid.wrappers.FullyObsWrapper(env) ### Description Fully observable gridworld using a compact grid encoding instead of the agent view. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **env**: The environment to wrap. ### Example ```python import gymnasium as gym from minigrid.wrappers import FullyObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") obs, _ = env.reset() print(obs['image'].shape) env_obs = FullyObsWrapper(env) obs, _ = env_obs.reset() print(obs['image'].shape) ``` ``` -------------------------------- ### Object Type Mappings Source: https://github.com/farama-foundation/minigrid/blob/main/docs/_modules/minigrid/core/constants.md Provides mappings between object names and their integer indices, and vice-versa. Essential for representing objects in the grid. ```python # Map of object type to integers OBJECT_TO_IDX = { "unseen": 0, "empty": 1, "wall": 2, "floor": 3, "door": 4, "key": 5, "ball": 6, "box": 7, "goal": 8, "lava": 9, "agent": 10, } IDX_TO_OBJECT = dict(zip(OBJECT_TO_IDX.values(), OBJECT_TO_IDX.keys())) ``` -------------------------------- ### State Mappings Source: https://github.com/farama-foundation/minigrid/blob/main/docs/_modules/minigrid/core/constants.md Defines integer indices for different object states, such as 'open', 'closed', and 'locked'. Used for interactive elements like doors. ```python # Map of state names to integers STATE_TO_IDX = { "open": 0, "closed": 1, "locked": 2, } ``` -------------------------------- ### DictObservationSpaceWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Transforms the observation space to a fully numerical one by replacing textual instructions with word indices. ```APIDOC ## class minigrid.wrappers.DictObservationSpaceWrapper(env, max_words_in_mission=50, word_dict=None) ### Description Transforms the observation space (that has a textual component) to a fully numerical observation space, where the textual instructions are replaced by arrays representing the indices of each word in a fixed vocabulary. This wrapper is not applicable to BabyAI environments, given that these have their own language component. ### Example ```pycon >>> import gymnasium as gym >>> import matplotlib.pyplot as plt >>> from minigrid.wrappers import DictObservationSpaceWrapper >>> env = gym.make("MiniGrid-LavaCrossingS11N5-v0") >>> obs, _ = env.reset() >>> obs['mission'] 'avoid the lava and get to the green goal square' >>> env_obs = DictObservationSpaceWrapper(env) >>> obs, _ = env_obs.reset() >>> obs['mission'][:10] [19, 31, 17, 36, 20, 38, 31, 2, 15, 35] ``` ``` -------------------------------- ### FlatObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Encodes mission strings using a one-hot scheme and combines them with observed images into one flat array. Not applicable to BabyAI environments. ```APIDOC ## class minigrid.wrappers.FlatObsWrapper(env, maxStrLen: int = 96) ### Description Encode mission strings using a one-hot scheme, and combine these with observed images into one flat array. This wrapper is not applicable to BabyAI environments, given that they have their own language component. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **env**: The environment to wrap. * **maxStrLen**: The maximum string length for mission encoding. Defaults to 96. ### Example ```python import gymnasium as gym from minigrid.wrappers import FlatObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") env_obs = FlatObsWrapper(env) obs, _ = env_obs.reset() print(obs.shape) ``` ``` -------------------------------- ### minigrid.wrappers.StochasticActionWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Adds stochasticity to the actions. If a random action is provided, it is returned with probability 1 - prob. Else, a random action is sampled from the action space. ```APIDOC ## class minigrid.wrappers.StochasticActionWrapper(env=None, prob=0.9, random_action=None) ### Description Adds stochasticity to the actions. If a random action is provided, it is returned with probability 1 - prob. Else, a random action is sampled from the action space. ### Parameters * **env**: The environment to wrap. * **prob**: The probability of taking the intended action. Defaults to 0.9. * **random_action**: The action to take with probability 1 - prob. If None, a random action is sampled from the action space. ``` -------------------------------- ### Define Mission Function Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/create_env_tutorial.md Define a static method that returns the mission string for the environment. This function is passed to the MissionSpace constructor. ```python @staticmethod def _gen_mission(): return "grand mission" ``` -------------------------------- ### One-Hot Encoding of Partial Observations Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper transforms the agent's partial view into a one-hot encoded representation. Use this when a detailed, categorical observation of the agent's surroundings is required. ```python import gymnasium as gym from minigrid.wrappers import OneHotPartialObsWrapper env = gym.make("MiniGrid-Empty-5x5-v0") obs, _ = env.reset() print(obs["image"][0, :, :]) env = OneHotPartialObsWrapper(env) obs, _ = env.reset() print(obs["image"][0, :, :]) ``` -------------------------------- ### ReseedWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Wrapper to always regenerate an environment with the same set of seeds. This can be used to force an environment to always keep the same configuration when reset. ```APIDOC ## class minigrid.wrappers.ReseedWrapper(env, seeds=(0,), seed_idx=0) ### Description Wrapper to always regenerate an environment with the same set of seeds. This can be used to force an environment to always keep the same configuration when reset. ### Parameters * **env**: The environment to wrap. * **seeds**: A tuple of seeds to cycle through. Defaults to (0,). * **seed_idx**: The index of the current seed to use. Defaults to 0. ### Example ```python import minigrid import gymnasium as gym from minigrid.wrappers import ReseedWrapper env = gym.make("MiniGrid-Empty-5x5-v0") _, _ = env.reset(seed=123) env = ReseedWrapper(env, seeds=[0, 1], seed_idx=0) _, _ = env.reset() ``` ``` -------------------------------- ### ObservationWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md A base wrapper class for modifying observations from environment reset and step functions using a custom observation function. ```APIDOC ## class minigrid.wrappers.ObservationWrapper(env: Env[ObsType, ActType]) ### Description Modify observations from `Env.reset()` and `Env.step()` using `observation()` function. If you would like to apply a function to only the observation before passing it to the learning code, you can simply inherit from [`ObservationWrapper`](#minigrid.wrappers.ObservationWrapper) and overwrite the method `observation()` to implement that transformation. The transformation defined in that method must be reflected by the `env` observation space. Otherwise, you need to specify the new observation space of the wrapper by setting `self.observation_space` in the `__init__()` method of your wrapper. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Color Mappings Source: https://github.com/farama-foundation/minigrid/blob/main/docs/_modules/minigrid/core/constants.md Defines a mapping from color names to their RGB numpy array representations and vice-versa. Used for visualizing objects. ```python from __future__ import annotations import numpy as np TILE_PIXELS = 32 # Map of color names to RGB values COLORS = { "red": np.array([255, 0, 0]), "green": np.array([0, 255, 0]), "blue": np.array([0, 0, 255]), "purple": np.array([112, 39, 195]), "yellow": np.array([255, 255, 0]), "grey": np.array([100, 100, 100]), } COLOR_NAMES = sorted(list(COLORS.keys())) # Used to map colors to integers COLOR_TO_IDX = {"red": 0, "green": 1, "blue": 2, "purple": 3, "yellow": 4, "grey": 5} IDX_TO_COLOR = dict(zip(COLOR_TO_IDX.values(), COLOR_TO_IDX.keys())) ``` -------------------------------- ### NoDeath Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper prevents the agent from dying in specific cells (e.g., lava cells) by applying a negative reward instead of terminating the episode. ```APIDOC ## class minigrid.wrappers.NoDeath(env, no_death_types: tuple[str, ...], death_cost: float = -1.0) ### Description Wrapper to prevent death in specific cells (e.g., lava cells). Instead of dying, the agent will receive a negative reward. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python >>> import gymnasium as gym >>> from minigrid.wrappers import NoDeath >>> >>> env = gym.make("MiniGrid-LavaCrossingS9N1-v0") >>> _, _ = env.reset(seed=2) >>> _, _, _, _, _ = env.step(1) >>> _, reward, term, *_ = env.step(2) >>> reward, term (0, True) >>> >>> env = NoDeath(env, no_death_types=("lava",), death_cost=-1.0) >>> _, _ = env.reset(seed=2) >>> _, _, _, _, _ = env.step(1) >>> _, reward, term, *_ = env.step(2) >>> reward, term (-1.0, False) >>> >>> >>> env = gym.make("MiniGrid-Dynamic-Obstacles-5x5-v0") >>> _, _ = env.reset(seed=2) >>> _, reward, term, *_ = env.step(2) >>> reward, term (-1, True) >>> >>> env = NoDeath(env, no_death_types=("ball",), death_cost=-1.0) >>> _, _ = env.reset(seed=2) >>> _, reward, term, *_ = env.step(2) >>> reward, term (-2.0, False) ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Prevent Death with Negative Reward - NoDeath Wrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md This wrapper prevents the agent from dying in specified cell types (e.g., lava, ball) by assigning a negative reward instead of terminating the episode. It's useful for scenarios where death should be penalized but not end the learning process. ```python >>> import gymnasium as gym >>> from minigrid.wrappers import NoDeath >>> >>> env = gym.make("MiniGrid-LavaCrossingS9N1-v0") >>> _, _ = env.reset(seed=2) >>> _, _, _, _, _ = env.step(1) >>> _, reward, term, *_ = env.step(2) >>> reward, term (0, True) >>> >>> env = NoDeath(env, no_death_types=("lava",), death_cost=-1.0) >>> _, _ = env.reset(seed=2) >>> _, _, _, _, _ = env.step(1) >>> _, reward, term, *_ = env.step(2) >>> reward, term (-1.0, False) >>> >>> >>> env = gym.make("MiniGrid-Dynamic-Obstacles-5x5-v0") >>> _, _ = env.reset(seed=2) >>> _, reward, term, *_ = env.step(2) >>> reward, term (-1, True) >>> >>> env = NoDeath(env, no_death_types=("ball",), death_cost=-1.0) >>> _, _ = env.reset(seed=2) >>> _, reward, term, *_ = env.step(2) >>> reward, term (-2.0, False) ``` -------------------------------- ### Direction to Vector Mappings Source: https://github.com/farama-foundation/minigrid/blob/main/docs/_modules/minigrid/core/constants.md Maps agent's directional indices to their corresponding 2D vector representations. Used for movement and orientation calculations. ```python # Map of agent direction indices to vectors DIR_TO_VEC = [ # Pointing right (positive X) np.array((1, 0)), # Down (positive Y) np.array((0, 1)), # Pointing left (negative X) np.array((-1, 0)), # Up (negative Y) np.array((0, -1)), ] ``` -------------------------------- ### Custom Minigrid Feature Extractor Source: https://github.com/farama-foundation/minigrid/blob/main/docs/content/training.md Implement a custom feature extractor for Minigrid environments compatible with StableBaselines3's CNN policies. This class inherits from BaseFeaturesExtractor and defines a CNN architecture tailored for Minigrid's observation space. ```python class MinigridFeaturesExtractor(BaseFeaturesExtractor): def __init__(self, observation_space: gym.Space, features_dim: int = 512, normalized_image: bool = False) -> None: super().__init__(observation_space, features_dim) n_input_channels = observation_space.shape[0] self.cnn = nn.Sequential( nn.Conv2d(n_input_channels, 16, (2, 2)), nn.ReLU(), nn.Conv2d(16, 32, (2, 2)), nn.ReLU(), nn.Conv2d(32, 64, (2, 2)), nn.ReLU(), nn.Flatten(), ) # Compute shape by doing one forward pass with torch.no_grad(): n_flatten = self.cnn(torch.as_tensor(observation_space.sample()[None]).float()).shape[1] self.linear = nn.Sequential(nn.Linear(n_flatten, features_dim), nn.ReLU()) def forward(self, observations: torch.Tensor) -> torch.Tensor: return self.linear(self.cnn(observations)) ``` -------------------------------- ### DirectionObsWrapper Source: https://github.com/farama-foundation/minigrid/blob/main/docs/api/wrappers.md Provides the slope/angular direction to the goal with the observations. The type can be set to 'slope' or 'angle'. ```APIDOC ## class minigrid.wrappers.DirectionObsWrapper(env, type='slope') ### Description Provides the slope/angular direction to the goal with the observations as modeled by (y2 - y2 )/( x2 - x1). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **env**: The environment to wrap. * **type**: The type of direction observation to provide. Can be 'slope' or 'angle'. Defaults to 'slope'. ### Example ```python import gymnasium as gym from minigrid.wrappers import DirectionObsWrapper env = gym.make("MiniGrid-LavaCrossingS11N5-v0") env_obs = DirectionObsWrapper(env, type="slope") obs, _ = env_obs.reset() print(obs['goal_direction'].item()) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.