Added metadata field to VectorEnv and VectorWrapper. Implements #1005. (#1006)

This commit is contained in:
Tim Schneider
2024-04-11 11:58:53 +02:00
committed by GitHub
parent aa566f9cf3
commit a152867a27
3 changed files with 26 additions and 0 deletions

View File

@@ -53,6 +53,10 @@ vector/utils
The ``EnvSpec`` of the environment normally set during :py:meth:`gymnasium.make_vec`
.. autoattribute:: gymnasium.vector.VectorEnv.metadata
The metadata of the environment containing rendering modes, rendering fps, etc
.. autoattribute:: gymnasium.vector.VectorEnv.render_mode
The render mode of the environment which should follow similar specifications to `Env.render_mode`.

View File

@@ -92,6 +92,9 @@ class VectorEnv(Generic[ObsType, ActType, ArrayType]):
:func:`make_vec` is the equivalent function to :func:`make` for vector environments.
"""
# Set this in SOME subclasses
metadata: dict[str, Any] = {"render_modes": []}
spec: EnvSpec | None = None
render_mode: str | None = None
closed: bool = False
@@ -446,6 +449,11 @@ class VectorWrapper(VectorEnv):
"""Returns the `render_mode` from the base environment."""
return self.env.render_mode
@property
def metadata(self) -> dict[str, Any]:
"""Returns the `metadata` from the base environment."""
return self.env.metadata
@property
def np_random(self) -> np.random.Generator:
"""Returns the environment's internal :attr:`_np_random` that if not set will initialise with a random seed.

View File

@@ -54,3 +54,17 @@ def test_vector_env_wrapper_attributes():
assert np.allclose(wrapped.env.get_attr("gravity"), env.get_attr("gravity"))
env.close()
def test_vector_env_metadata():
"""Test if `metadata` property for VectorWrapper correctly forwards to the vector env it is wrapping."""
env = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync")
wrapped = DummyVectorWrapper(
gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync")
)
assert env.metadata == wrapped.metadata
env.metadata = {"render_modes": ["rgb_array"]}
assert env.metadata != wrapped.metadata
env.close()