2023-11-07 13:27:25 +00:00
|
|
|
"""Test suite for Autoreset wrapper."""
|
2022-03-25 12:20:02 -05:00
|
|
|
import numpy as np
|
|
|
|
|
2022-09-16 23:41:27 +01:00
|
|
|
import gymnasium as gym
|
2023-11-07 13:27:25 +00:00
|
|
|
from gymnasium.wrappers import Autoreset
|
|
|
|
from tests.testing_env import GenericTestEnv
|
2022-03-25 12:20:02 -05:00
|
|
|
|
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
def autoreset_reset_func(self: gym.Env, seed=None, options=None):
|
|
|
|
self.count = 0
|
|
|
|
return np.array([self.count]), {"count": self.count}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-04-08 09:54:49 -05:00
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
def autoreset_step_func(self: gym.Env, action: int):
|
|
|
|
self.count += 1
|
|
|
|
return (
|
|
|
|
np.array([self.count]), # Obs
|
|
|
|
self.count > 2, # Reward
|
|
|
|
self.count > 2, # Terminated
|
|
|
|
False, # Truncated
|
|
|
|
{"count": self.count}, # Info
|
|
|
|
)
|
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."""
|
2023-11-07 13:27:25 +00:00
|
|
|
env = GenericTestEnv(reset_func=autoreset_reset_func, step_func=autoreset_step_func)
|
|
|
|
env = Autoreset(env)
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-08-23 11:09:54 -04:00
|
|
|
obs, info = env.reset()
|
2022-03-25 12:20:02 -05:00
|
|
|
assert obs == np.array([0])
|
|
|
|
assert info == {"count": 0}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
|
|
|
action = 0
|
2022-08-30 19:41:59 +05:30
|
|
|
obs, reward, terminated, truncated, info = env.step(action)
|
2022-03-25 12:20:02 -05:00
|
|
|
assert obs == np.array([1])
|
|
|
|
assert reward == 0
|
2022-08-30 19:41:59 +05:30
|
|
|
assert (terminated or truncated) is False
|
2022-03-25 12:20:02 -05:00
|
|
|
assert info == {"count": 1}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-08-30 19:41:59 +05:30
|
|
|
obs, reward, terminated, truncated, info = env.step(action)
|
2022-03-25 12:20:02 -05:00
|
|
|
assert obs == np.array([2])
|
2022-08-30 19:41:59 +05:30
|
|
|
assert (terminated or truncated) 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-08-30 19:41:59 +05:30
|
|
|
obs, reward, terminated, truncated, info = env.step(action)
|
2022-03-25 12:20:02 -05:00
|
|
|
assert obs == np.array([0])
|
2022-08-30 19:41:59 +05:30
|
|
|
assert (terminated or truncated) is True
|
2022-03-25 12:20:02 -05:00
|
|
|
assert reward == 1
|
|
|
|
assert info == {
|
|
|
|
"count": 0,
|
2022-07-10 02:18:06 +05:30
|
|
|
"final_observation": np.array([3]),
|
|
|
|
"final_info": {"count": 3},
|
2022-03-25 12:20:02 -05:00
|
|
|
}
|
2022-05-10 15:35:45 +01:00
|
|
|
|
2022-08-30 19:41:59 +05:30
|
|
|
obs, reward, terminated, truncated, info = env.step(action)
|
2022-03-25 12:20:02 -05:00
|
|
|
assert obs == np.array([1])
|
|
|
|
assert reward == 0
|
2022-08-30 19:41:59 +05:30
|
|
|
assert (terminated or truncated) 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()
|