2022-05-10 15:35:45 +01:00
|
|
|
"""Tests the gym.wrapper.AutoResetWrapper operates as expected."""
|
|
|
|
|
|
|
|
from typing import Generator, Optional
|
2022-04-08 09:54:49 -05:00
|
|
|
from unittest.mock import MagicMock
|
2022-03-25 12:20:02 -05:00
|
|
|
|
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2022-03-25 12:20:02 -05:00
|
|
|
|
|
|
|
import gym
|
|
|
|
from gym.wrappers import AutoResetWrapper
|
2022-04-08 09:54:49 -05:00
|
|
|
from tests.envs.spec_list import spec_list
|
2022-03-25 12:20:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DummyResetEnv(gym.Env):
|
2022-05-10 15:35:45 +01:00
|
|
|
"""A dummy environment which returns ascending numbers starting at `0` when :meth:`self.step()` is called.
|
|
|
|
|
|
|
|
After the second call to :meth:`self.step()` done is true.
|
|
|
|
Info dicts are also returned containing the same number returned as an observation, accessible via the key "count".
|
|
|
|
This environment is provided for the purpose of testing the autoreset wrapper.
|
2022-03-25 12:20:02 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
metadata = {}
|
|
|
|
|
|
|
|
def __init__(self):
|
2022-05-10 15:35:45 +01:00
|
|
|
"""Initialise the DummyResetEnv."""
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
self.action_space = gym.spaces.Box(
|
2022-05-10 15:35:45 +01:00
|
|
|
low=np.array([0]), high=np.array([2]), dtype=np.int64
|
2022-03-25 12:20:02 -05:00
|
|
|
)
|
2022-05-10 15:35:45 +01:00
|
|
|
self.observation_space = gym.spaces.Discrete(2)
|
2022-03-25 12:20:02 -05:00
|
|
|
self.count = 0
|
|
|
|
|
2022-05-10 15:35:45 +01:00
|
|
|
def step(self, action: int):
|
|
|
|
"""Steps the DummyEnv with the incremented step, reward and done `if self.count > 1` and updated info."""
|
2022-03-25 12:20:02 -05:00
|
|
|
self.count += 1
|
|
|
|
return (
|
2022-05-10 15:35:45 +01:00
|
|
|
np.array([self.count]), # Obs
|
|
|
|
self.count > 2, # Reward
|
|
|
|
self.count > 2, # Done
|
|
|
|
{"count": self.count}, # Info
|
2022-03-25 12:20:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
def reset(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
seed: Optional[int] = None,
|
|
|
|
return_info: Optional[bool] = False,
|
|
|
|
options: Optional[dict] = None
|
|
|
|
):
|
2022-05-10 15:35:45 +01:00
|
|
|
"""Resets the DummyEnv to return the count array and info with count."""
|
2022-03-25 12:20:02 -05:00
|
|
|
self.count = 0
|
|
|
|
if not return_info:
|
|
|
|
return np.array([self.count])
|
|
|
|
else:
|
|
|
|
return np.array([self.count]), {"count": self.count}
|
|
|
|
|
|
|
|
|
2022-05-10 15:35:45 +01:00
|
|
|
def unwrap_env(env) -> Generator[gym.Wrapper, None, None]:
|
|
|
|
"""Unwraps an environment yielding all wrappers around environment."""
|
|
|
|
while isinstance(env, gym.Wrapper):
|
|
|
|
yield type(env)
|
|
|
|
env = env.env
|
2022-03-25 12:20:02 -05:00
|
|
|
|
|
|
|
|
2022-04-10 18:36:23 +01:00
|
|
|
@pytest.mark.parametrize("spec", spec_list, ids=[spec.id for spec in spec_list])
|
2022-04-08 09:54:49 -05:00
|
|
|
def test_make_autoreset_true(spec):
|
2022-05-10 15:35:45 +01:00
|
|
|
"""Tests gym.make with `autoreset=True`, and check that the reset actually happens.
|
|
|
|
|
|
|
|
Note: This test assumes that the outermost wrapper is AutoResetWrapper so if that
|
|
|
|
is being changed in the future, this test will break and need to be updated.
|
2022-04-08 09:54:49 -05:00
|
|
|
Note: This test assumes that all first-party environments will terminate in a finite
|
2022-05-10 15:35:45 +01:00
|
|
|
amount of time with random actions, which is true as of the time of adding this test.
|
2022-04-08 09:54:49 -05:00
|
|
|
"""
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
with pytest.warns(None):
|
2022-05-10 15:35:45 +01:00
|
|
|
env = gym.make(spec.id, autoreset=True)
|
|
|
|
assert AutoResetWrapper in unwrap_env(env)
|
2022-04-08 09:54:49 -05:00
|
|
|
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
env.reset(seed=0)
|
2022-04-08 09:54:49 -05:00
|
|
|
env.unwrapped.reset = MagicMock(side_effect=env.unwrapped.reset)
|
|
|
|
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
obs, reward, done, info = env.step(env.action_space.sample())
|
|
|
|
|
|
|
|
assert env.unwrapped.reset.called
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
env.close()
|
2022-04-08 09:54:49 -05:00
|
|
|
|
|
|
|
|
2022-04-10 18:36:23 +01:00
|
|
|
@pytest.mark.parametrize("spec", spec_list, ids=[spec.id for spec in spec_list])
|
2022-05-10 15:35:45 +01:00
|
|
|
def test_gym_make_autoreset(spec):
|
|
|
|
"""Tests that `gym.make` autoreset wrapper is applied only when `gym.make(..., autoreset=True)`."""
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
with pytest.warns(None):
|
2022-05-10 15:35:45 +01:00
|
|
|
env = gym.make(spec.id)
|
|
|
|
assert AutoResetWrapper not in unwrap_env(env)
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
env.close()
|
2022-04-08 09:54:49 -05:00
|
|
|
|
2022-05-10 15:35:45 +01:00
|
|
|
with pytest.warns(None):
|
|
|
|
env = gym.make(spec.id, autoreset=False)
|
|
|
|
assert AutoResetWrapper not in unwrap_env(env)
|
|
|
|
env.close()
|
2022-04-08 09:54:49 -05:00
|
|
|
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
with pytest.warns(None):
|
2022-05-10 15:35:45 +01:00
|
|
|
env = gym.make(spec.id, autoreset=True)
|
|
|
|
assert AutoResetWrapper in unwrap_env(env)
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
env.close()
|
2022-04-08 09:54:49 -05:00
|
|
|
|
|
|
|
|
2022-05-10 15:35:45 +01:00
|
|
|
def test_autoreset_wrapper_autoreset():
|
|
|
|
"""Tests the autoreset wrapper actually automatically resets correctly."""
|
2022-03-25 12:20:02 -05:00
|
|
|
env = DummyResetEnv()
|
|
|
|
env = AutoResetWrapper(env)
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-03-25 12:20:02 -05:00
|
|
|
obs, info = env.reset(return_info=True)
|
|
|
|
assert obs == np.array([0])
|
|
|
|
assert info == {"count": 0}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
|
|
|
action = 0
|
2022-03-25 12:20:02 -05:00
|
|
|
obs, reward, done, info = env.step(action)
|
|
|
|
assert obs == np.array([1])
|
|
|
|
assert reward == 0
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
assert done is False
|
2022-03-25 12:20:02 -05:00
|
|
|
assert info == {"count": 1}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-03-25 12:20:02 -05:00
|
|
|
obs, reward, done, info = env.step(action)
|
|
|
|
assert obs == np.array([2])
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
assert done is False
|
2022-03-25 12:20:02 -05:00
|
|
|
assert reward == 0
|
|
|
|
assert info == {"count": 2}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-03-25 12:20:02 -05:00
|
|
|
obs, reward, done, info = env.step(action)
|
|
|
|
assert obs == np.array([0])
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
assert done is True
|
2022-03-25 12:20:02 -05:00
|
|
|
assert reward == 1
|
|
|
|
assert info == {
|
|
|
|
"count": 0,
|
|
|
|
"terminal_observation": np.array([3]),
|
|
|
|
"terminal_info": {"count": 3},
|
|
|
|
}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-03-25 12:20:02 -05:00
|
|
|
obs, reward, done, info = env.step(action)
|
|
|
|
assert obs == np.array([1])
|
|
|
|
assert reward == 0
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
assert done is False
|
2022-03-25 12:20:02 -05:00
|
|
|
assert info == {"count": 1}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
Update the flake8 pre-commit ignores (#2778)
* Remove additional ignores from flake8
* Remove all unused imports
* Remove all unused imports
* Update flake8 and pyupgrade
* F841, removed unused variables
* E731, removed lambda assignment to variables
* Remove E731, F403, F405, F524
* Remove E722, bare exceptions
* Remove E712, compare variable == True or == False to is True or is False
* Remove E402, module level import not at top of file
* Added --pre-file-ignores
* Add --per-file-ignores removing E741, E302 and E704
* Add E741, do not use variables named ‘l’, ‘O’, or ‘I’ to ignore issues in classic control
* Fixed issues for pytest==6.2
* Remove unnecessary # noqa
* Edit comment with the removal of E302
* Added warnings and declared module, attr for pyright type hinting
* Remove unused import
* Removed flake8 E302
* Updated flake8 from 3.9.2 to 4.0.1
* Remove unused variable
2022-04-26 16:18:37 +01:00
|
|
|
env.close()
|