Farama-Foundation / Minigrid

Simple and easily configurable grid world environments for reinforcement learning
https://minigrid.farama.org/
Other
2.09k stars 604 forks source link

[Bug Report] Vectorizing minigrid environments with a MissionSpace via gymnasium.vector.SyncVectorEnv causes an attribute error #303

Closed jbloomAus closed 1 year ago

jbloomAus commented 1 year ago

If you are submitting a bug report, please fill in the following details and use the tag [bug].

Describe the bug Vectorizing minigrid environments with a MissionSpace via gymnasium.vector.SyncVectorEnv causes an attribute error.

Code example

import gymnasium as gym 
import minigrid

def env_maker(env_id, **kwargs):
    def env_func():
        env = gym.make(env_id, **kwargs)
        return env
    return env_func

num_envs = 4
env_id = "MiniGrid-ObstructedMaze-2Q"
kwargs = dict(num_rooms_visited=2)
envs = gym.vector.SyncVectorEnv([env_maker(env_id, **kwargs) for _ in range(num_envs)])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 13
     11 env_id = "MiniGrid-ObstructedMaze-2Q"
     12 kwargs = dict(num_rooms_visited=2)
---> 13 envs = gym.vector.SyncVectorEnv([env_maker(env_id, **kwargs) for _ in range(num_envs)])

File ~/miniforge3/envs/decision_transformer_interpretability/lib/python3.9/site-packages/gymnasium/vector/sync_vector_env.py:66, in SyncVectorEnv.__init__(self, env_fns, observation_space, action_space, copy)
     59     action_space = action_space or self.envs[0].action_space
     60 super().__init__(
     61     num_envs=len(self.envs),
     62     observation_space=observation_space,
     63     action_space=action_space,
     64 )
---> 66 self._check_spaces()
     67 self.observations = create_empty_array(
     68     self.single_observation_space, n=self.num_envs, fn=np.zeros
     69 )
     70 self._rewards = np.zeros((self.num_envs,), dtype=np.float64)

File ~/miniforge3/envs/decision_transformer_interpretability/lib/python3.9/site-packages/gymnasium/vector/sync_vector_env.py:223, in SyncVectorEnv._check_spaces(self)
    221 def _check_spaces(self) -> bool:
    222     for env in self.envs:
--> 223         if not (env.observation_space == self.single_observation_space):
    224             raise RuntimeError(
    225                 "Some environments have an observation space different from "
    226                 f"`{self.single_observation_space}`. In order to batch observations, "
    227                 "the observation spaces from all environments must be equal."
    228             )
    230         if not (env.action_space == self.single_action_space):

File ~/miniforge3/envs/decision_transformer_interpretability/lib/python3.9/site-packages/gymnasium/spaces/dict.py:218, in Dict.__eq__(self, other)
    213 def __eq__(self, other: Any) -> bool:
    214     """Check whether `other` is equivalent to this instance."""
    215     return (
    216         isinstance(other, Dict)
    217         # Comparison of `OrderedDict`s is order-sensitive
--> 218         and self.spaces == other.spaces  # OrderedDict.__eq__
    219     )

File ~/GithubRepositories/Minigrid/minigrid/core/mission.py:175, in MissionSpace.__eq__(self, other)
    170 if isinstance(other, MissionSpace):
    171 
    172     # Check that place holder lists are the same
    173     if self.ordered_placeholders is not None:
    174         # Check length
--> 175         if (len(self.order_placeholder) == len(other.order_placeholder)) and (
    176             all(
    177                 set(i) == set(j)
    178                 for i, j in zip(self.order_placeholder, other.order_placeholder)
    179             )
    180         ):
    181             # Check mission string is the same with dummy space placeholders
    182             test_placeholders = [""] * len(self.order_placeholder)
    183             mission = self.mission_func(*test_placeholders)

AttributeError: 'MissionSpace' object has no attribute 'order_placeholder'

System Info Describe the characteristic of your environment:

Additional context I've already got a fix and will submit a PR associated with this issue.

Checklist