Skip to content

Commit

Permalink
Fix make_vec for sync or async and modifying make arguments (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudo-rnd-thoughts authored Apr 17, 2024
1 parent d958991 commit fb5112f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion gymnasium/envs/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def make_vec(
We refer to the Vector environment as the vectorizor while the environment being vectorized is the base or vectorized environment (``vectorizor(vectorized env)``).
Args:
id: Name of the environment. Optionally, a module to import can be included, eg. 'module:Env-v0'
id: Name of the environment. Optionally, a module to import can be included, e.g. 'module:Env-v0'
num_envs: Number of environments to create
vectorization_mode: The vectorization method used, defaults to ``None`` such that if env id' spec has a ``vector_entry_point`` (not ``None``),
this is first used otherwise defaults to ``sync`` to use the :class:`gymnasium.vector.SyncVectorEnv`.
Expand Down Expand Up @@ -874,6 +874,8 @@ def make_vec(

env_spec = copy.deepcopy(env_spec)
env_spec_kwargs = env_spec.kwargs
# for sync or async, these parameters should be passed in `make(..., **kwargs)` rather than in the env spec kwargs, therefore, we `reset` the kwargs
env_spec.kwargs = dict()

num_envs = env_spec_kwargs.pop("num_envs", num_envs)
vectorization_mode = env_spec_kwargs.pop("vectorization_mode", vectorization_mode)
Expand Down
3 changes: 2 additions & 1 deletion gymnasium/vector/sync_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def set_attr(self, name: str, values: list[Any] | tuple[Any, ...] | Any):

def close_extras(self, **kwargs: Any):
"""Close the environments."""
[env.close() for env in self.envs]
if hasattr(self, "envs"):
[env.close() for env in self.envs]

def _check_spaces(self) -> bool:
"""Check that each of the environments obs and action spaces are equivalent to the single obs and action space."""
Expand Down
12 changes: 12 additions & 0 deletions tests/envs/registration/test_make_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def test_make_vec_wrappers():
},
),
("CartPole-v1", {"render_mode": "rgb_array"}),
("CartPole-v1", {"vectorization_mode": "sync", "max_episode_steps": 5}),
("CartPole-v1", {"sutton_barto_reward": True}),
("CartPole-v1", {"vectorization_mode": "sync", "sutton_barto_reward": True}),
(gym.spec("CartPole-v1"), {}),
(gym.spec("CartPole-v1"), {"num_envs": 3}),
(gym.spec("CartPole-v1"), {"vectorization_mode": "sync"}),
Expand All @@ -199,6 +202,15 @@ def test_make_vec_wrappers():
},
),
(gym.spec("CartPole-v1"), {"render_mode": "rgb_array"}),
(
gym.spec("CartPole-v1"),
{"vectorization_mode": "sync", "max_episode_steps": 5},
),
(gym.spec("CartPole-v1"), {"sutton_barto_reward": True}),
(
gym.spec("CartPole-v1"),
{"vectorization_mode": "sync", "sutton_barto_reward": True},
),
),
)
def test_make_vec_with_spec(env_id: str, kwargs: dict):
Expand Down

0 comments on commit fb5112f

Please sign in to comment.