2021-12-08 22:14:15 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-09-17 18:02:59 -04:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
import pytest
|
2021-09-17 18:02:59 -04:00
|
|
|
|
|
|
|
from gym import core, spaces
|
2022-03-31 12:50:38 -07:00
|
|
|
from gym.wrappers import OrderEnforcing, TimeLimit
|
2016-05-27 12:16:35 -07:00
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2016-05-27 12:16:35 -07:00
|
|
|
class ArgumentEnv(core.Env):
|
|
|
|
calls = 0
|
|
|
|
|
|
|
|
def __init__(self, arg):
|
|
|
|
self.calls += 1
|
|
|
|
self.arg = arg
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2021-09-17 18:02:59 -04:00
|
|
|
class UnittestEnv(core.Env):
|
|
|
|
observation_space = spaces.Box(low=0, high=255, shape=(64, 64, 3), dtype=np.uint8)
|
|
|
|
action_space = spaces.Discrete(3)
|
|
|
|
|
2022-01-19 23:28:59 +01:00
|
|
|
def reset(self, *, seed: Optional[int] = None, options: Optional[dict] = None):
|
2021-12-08 22:14:15 +01:00
|
|
|
super().reset(seed=seed)
|
2021-09-17 18:02:59 -04:00
|
|
|
return self.observation_space.sample() # Dummy observation
|
|
|
|
|
|
|
|
def step(self, action):
|
|
|
|
observation = self.observation_space.sample() # Dummy observation
|
|
|
|
return (observation, 0.0, False, {})
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownSpacesEnv(core.Env):
|
|
|
|
"""This environment defines its observation & action spaces only
|
|
|
|
after the first call to reset. Although this pattern is sometimes
|
|
|
|
necessary when implementing a new environment (e.g. if it depends
|
|
|
|
on external resources), it is not encouraged.
|
|
|
|
"""
|
|
|
|
|
2022-02-06 17:28:27 -06:00
|
|
|
def reset(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
seed: Optional[int] = None,
|
|
|
|
return_info: bool = False,
|
|
|
|
options: Optional[dict] = None
|
|
|
|
):
|
2021-12-08 22:14:15 +01:00
|
|
|
super().reset(seed=seed)
|
2021-09-17 18:02:59 -04:00
|
|
|
self.observation_space = spaces.Box(
|
|
|
|
low=0, high=255, shape=(64, 64, 3), dtype=np.uint8
|
|
|
|
)
|
|
|
|
self.action_space = spaces.Discrete(3)
|
2022-02-06 17:28:27 -06:00
|
|
|
if not return_info:
|
|
|
|
return self.observation_space.sample() # Dummy observation
|
|
|
|
else:
|
|
|
|
return self.observation_space.sample(), {} # Dummy observation with info
|
2021-09-17 18:02:59 -04:00
|
|
|
|
|
|
|
def step(self, action):
|
|
|
|
observation = self.observation_space.sample() # Dummy observation
|
|
|
|
return (observation, 0.0, False, {})
|
|
|
|
|
|
|
|
|
2022-01-19 23:28:59 +01:00
|
|
|
class OldStyleEnv(core.Env):
|
|
|
|
"""This environment doesn't accept any arguments in reset, ideally we want to support this too (for now)"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
super().reset()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def step(self, action):
|
|
|
|
return 0, 0, False, {}
|
|
|
|
|
|
|
|
|
2021-09-17 18:02:59 -04:00
|
|
|
class NewPropertyWrapper(core.Wrapper):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
env,
|
|
|
|
observation_space=None,
|
|
|
|
action_space=None,
|
|
|
|
reward_range=None,
|
|
|
|
metadata=None,
|
|
|
|
):
|
|
|
|
super().__init__(env)
|
|
|
|
if observation_space is not None:
|
|
|
|
# Only set the observation space if not None to test property forwarding
|
|
|
|
self.observation_space = observation_space
|
|
|
|
if action_space is not None:
|
|
|
|
self.action_space = action_space
|
|
|
|
if reward_range is not None:
|
|
|
|
self.reward_range = reward_range
|
|
|
|
if metadata is not None:
|
|
|
|
self.metadata = metadata
|
|
|
|
|
|
|
|
|
2016-05-27 12:16:35 -07:00
|
|
|
def test_env_instantiation():
|
|
|
|
# This looks like a pretty trivial, but given our usage of
|
|
|
|
# __new__, it's worth having.
|
2021-07-29 02:26:34 +02:00
|
|
|
env = ArgumentEnv("arg")
|
|
|
|
assert env.arg == "arg"
|
2016-05-27 12:16:35 -07:00
|
|
|
assert env.calls == 1
|
2021-09-17 18:02:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
properties = [
|
|
|
|
{
|
|
|
|
"observation_space": spaces.Box(
|
|
|
|
low=0.0, high=1.0, shape=(64, 64, 3), dtype=np.float32
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"action_space": spaces.Discrete(2)},
|
|
|
|
{"reward_range": (-1.0, 1.0)},
|
2022-02-28 15:54:03 -05:00
|
|
|
{"metadata": {"render_modes": ["human", "rgb_array"]}},
|
2021-09-17 18:02:59 -04:00
|
|
|
{
|
|
|
|
"observation_space": spaces.Box(
|
|
|
|
low=0.0, high=1.0, shape=(64, 64, 3), dtype=np.float32
|
|
|
|
),
|
|
|
|
"action_space": spaces.Discrete(2),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("class_", [UnittestEnv, UnknownSpacesEnv])
|
|
|
|
@pytest.mark.parametrize("props", properties)
|
|
|
|
def test_wrapper_property_forwarding(class_, props):
|
|
|
|
env = class_()
|
|
|
|
env = NewPropertyWrapper(env, **props)
|
|
|
|
|
|
|
|
# If UnknownSpacesEnv, then call reset to define the spaces
|
|
|
|
if isinstance(env.unwrapped, UnknownSpacesEnv):
|
|
|
|
_ = env.reset()
|
|
|
|
|
|
|
|
# Test the properties set by the wrapper
|
|
|
|
for key, value in props.items():
|
|
|
|
assert getattr(env, key) == value
|
|
|
|
|
|
|
|
# Otherwise, test if the properties are forwarded
|
|
|
|
all_properties = {"observation_space", "action_space", "reward_range", "metadata"}
|
|
|
|
for key in all_properties - props.keys():
|
|
|
|
assert getattr(env, key) == getattr(env.unwrapped, key)
|
2022-01-19 23:28:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_compatibility_with_old_style_env():
|
|
|
|
env = OldStyleEnv()
|
|
|
|
env = OrderEnforcing(env)
|
|
|
|
env = TimeLimit(env)
|
|
|
|
obs = env.reset()
|
|
|
|
assert obs == 0
|