2023-02-18 21:08:08 +00:00
|
|
|
"""Tests the vector wrappers work as expected."""
|
2023-02-12 07:49:37 -05:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import gymnasium as gym
|
|
|
|
from gymnasium.experimental.vector import VectorWrapper
|
|
|
|
|
|
|
|
|
2023-02-18 21:08:08 +00:00
|
|
|
class DummyVectorWrapper(VectorWrapper):
|
|
|
|
"""Dummy Vector wrapper that contains a counter function to logging the number of times that reset is called."""
|
|
|
|
|
2023-02-12 07:49:37 -05:00
|
|
|
def __init__(self, env):
|
2023-02-18 21:08:08 +00:00
|
|
|
"""Initialises the wrapper with the environment creating a counter variable."""
|
2023-02-12 07:49:37 -05:00
|
|
|
super().__init__(env)
|
|
|
|
self.env = env
|
|
|
|
self.counter = 0
|
|
|
|
|
|
|
|
def reset(self, **kwargs):
|
2023-02-18 21:08:08 +00:00
|
|
|
"""Updates the ``counter`` each time at ``reset`` is called."""
|
2023-02-12 07:49:37 -05:00
|
|
|
super().reset()
|
|
|
|
self.counter += 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_vector_env_wrapper_inheritance():
|
2023-02-18 21:08:08 +00:00
|
|
|
"""Test vector environment wrapper inheritance."""
|
2023-02-12 07:49:37 -05:00
|
|
|
env = gym.make_vec("FrozenLake-v1", vectorization_mode="async")
|
2023-02-18 21:08:08 +00:00
|
|
|
wrapped = DummyVectorWrapper(env)
|
2023-02-12 07:49:37 -05:00
|
|
|
wrapped.reset()
|
|
|
|
assert wrapped.counter == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_vector_env_wrapper_attributes():
|
|
|
|
"""Test if `set_attr`, `call` methods for VecEnvWrapper get correctly forwarded to the vector env it is wrapping."""
|
|
|
|
env = gym.make_vec("CartPole-v1", num_envs=3)
|
2023-02-18 21:08:08 +00:00
|
|
|
wrapped = DummyVectorWrapper(gym.make_vec("CartPole-v1", num_envs=3))
|
2023-02-12 07:49:37 -05:00
|
|
|
|
|
|
|
assert np.allclose(wrapped.call("gravity"), env.call("gravity"))
|
|
|
|
env.set_attr("gravity", [20.0, 20.0, 20.0])
|
|
|
|
wrapped.set_attr("gravity", [20.0, 20.0, 20.0])
|
|
|
|
assert np.allclose(wrapped.get_attr("gravity"), env.get_attr("gravity"))
|