2021-07-26 17:07:45 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import gym
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import alr_envs # noqa
|
2021-08-19 09:30:54 +02:00
|
|
|
from alr_envs.utils.make_env_helpers import make
|
2021-07-26 17:07:45 +02:00
|
|
|
|
|
|
|
ALL_SPECS = list(spec for spec in gym.envs.registry.all() if "alr_envs" in spec.entry_point)
|
|
|
|
SEED = 1
|
|
|
|
|
|
|
|
|
2021-08-25 17:16:59 +02:00
|
|
|
class TestMPEnvironments(unittest.TestCase):
|
2021-07-26 17:07:45 +02:00
|
|
|
|
|
|
|
def _run_env(self, env_id, iterations=None, seed=SEED, render=False):
|
|
|
|
"""
|
|
|
|
Example for running a DMC based env in the step based setting.
|
|
|
|
The env_id has to be specified as `domain_name-task_name` or
|
|
|
|
for manipulation tasks as `manipulation-environment_name`
|
|
|
|
|
|
|
|
Args:
|
|
|
|
env_id: Either `domain_name-task_name` or `manipulation-environment_name`
|
|
|
|
iterations: Number of rollout steps to run
|
|
|
|
seed= random seeding
|
|
|
|
render: Render the episode
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-08-19 09:30:54 +02:00
|
|
|
env: gym.Env = make(env_id, seed=seed)
|
2021-07-26 17:07:45 +02:00
|
|
|
rewards = []
|
|
|
|
observations = []
|
|
|
|
dones = []
|
|
|
|
obs = env.reset()
|
|
|
|
self._verify_observations(obs, env.observation_space, "reset()")
|
|
|
|
|
2022-07-07 10:47:04 +02:00
|
|
|
iterations = iterations or (env.spec.max_episode_steps or 1)
|
2021-07-26 17:07:45 +02:00
|
|
|
|
|
|
|
# number of samples(multiple environment steps)
|
|
|
|
for i in range(iterations):
|
|
|
|
observations.append(obs)
|
|
|
|
|
2022-07-11 16:18:18 +02:00
|
|
|
actions = env.action_space.sample()
|
2021-07-26 17:07:45 +02:00
|
|
|
# ac = np.random.uniform(env.action_space.low, env.action_space.high, env.action_space.shape)
|
2022-07-11 16:18:18 +02:00
|
|
|
obs, reward, done, info = env.step(actions)
|
2021-07-26 17:07:45 +02:00
|
|
|
|
|
|
|
self._verify_observations(obs, env.observation_space, "step()")
|
|
|
|
self._verify_reward(reward)
|
|
|
|
self._verify_done(done)
|
|
|
|
|
|
|
|
rewards.append(reward)
|
|
|
|
dones.append(done)
|
|
|
|
|
|
|
|
if render:
|
|
|
|
env.render("human")
|
|
|
|
|
|
|
|
if done:
|
2022-07-11 16:18:18 +02:00
|
|
|
break
|
2021-07-26 17:07:45 +02:00
|
|
|
|
2022-07-11 16:18:18 +02:00
|
|
|
assert done, "Done flag is not True after end of episode."
|
2021-07-26 17:07:45 +02:00
|
|
|
observations.append(obs)
|
|
|
|
env.close()
|
|
|
|
del env
|
2022-07-11 16:18:18 +02:00
|
|
|
return np.array(observations), np.array(rewards), np.array(dones), np.array(actions)
|
2021-07-26 17:07:45 +02:00
|
|
|
|
2021-08-25 17:16:59 +02:00
|
|
|
def _run_env_determinism(self, ids):
|
|
|
|
seed = 0
|
|
|
|
for env_id in ids:
|
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
traj1 = self._run_env(env_id, seed=seed)
|
|
|
|
traj2 = self._run_env(env_id, seed=seed)
|
|
|
|
for i, time_step in enumerate(zip(*traj1, *traj2)):
|
2022-07-11 16:18:18 +02:00
|
|
|
obs1, rwd1, done1, ac1, obs2, rwd2, done2, ac2 = time_step
|
|
|
|
self.assertTrue(np.array_equal(ac1, ac2), f"Actions [{i}] delta {ac1 - ac2} is not zero.")
|
|
|
|
self.assertTrue(np.array_equal(obs1, obs2), f"Observations [{i}] delta {obs1 - obs2} is not zero.")
|
2021-08-25 17:16:59 +02:00
|
|
|
self.assertEqual(rwd1, rwd2, f"Rewards [{i}] {rwd1} and {rwd2} do not match.")
|
|
|
|
self.assertEqual(done1, done2, f"Dones [{i}] {done1} and {done2} do not match.")
|
|
|
|
|
2021-07-26 17:07:45 +02:00
|
|
|
def _verify_observations(self, obs, observation_space, obs_type="reset()"):
|
|
|
|
self.assertTrue(observation_space.contains(obs),
|
|
|
|
f"Observation {obs} received from {obs_type} "
|
|
|
|
f"not contained in observation space {observation_space}.")
|
|
|
|
|
|
|
|
def _verify_reward(self, reward):
|
2022-07-11 16:18:18 +02:00
|
|
|
self.assertIsInstance(reward, (float, int), f"Returned type {type(reward)} as reward, expected float or int.")
|
2021-07-26 17:07:45 +02:00
|
|
|
|
|
|
|
def _verify_done(self, done):
|
|
|
|
self.assertIsInstance(done, bool, f"Returned {done} as done flag, expected bool.")
|
|
|
|
|
2021-08-25 17:16:59 +02:00
|
|
|
def test_alr_environment_functionality(self):
|
|
|
|
"""Tests that environments runs without errors using random actions for ALR MP envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
|
|
|
for env_id in alr_envs.ALL_ALR_MOTION_PRIMITIVE_ENVIRONMENTS['DMP']:
|
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
|
|
|
for env_id in alr_envs.ALL_ALR_MOTION_PRIMITIVE_ENVIRONMENTS['ProMP']:
|
2021-08-25 17:16:59 +02:00
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
|
|
|
def test_openai_environment_functionality(self):
|
|
|
|
"""Tests that environments runs without errors using random actions for OpenAI gym MP envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
|
|
|
for env_id in alr_envs.ALL_GYM_MOTION_PRIMITIVE_ENVIRONMENTS['DMP']:
|
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
|
|
|
for env_id in alr_envs.ALL_GYM_MOTION_PRIMITIVE_ENVIRONMENTS['ProMP']:
|
2021-08-25 17:16:59 +02:00
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
|
|
|
def test_dmc_environment_functionality(self):
|
|
|
|
"""Tests that environments runs without errors using random actions for DMC MP envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
2022-07-11 16:18:18 +02:00
|
|
|
for env_id in alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS['DMP']:
|
2021-08-25 17:16:59 +02:00
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
2022-07-11 16:18:18 +02:00
|
|
|
for env_id in alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS['ProMP']:
|
2021-08-25 17:16:59 +02:00
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
|
|
|
def test_metaworld_environment_functionality(self):
|
|
|
|
"""Tests that environments runs without errors using random actions for Metaworld MP envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
|
|
|
for env_id in alr_envs.ALL_METAWORLD_MOTION_PRIMITIVE_ENVIRONMENTS['DMP']:
|
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
|
|
|
for env_id in alr_envs.ALL_METAWORLD_MOTION_PRIMITIVE_ENVIRONMENTS['ProMP']:
|
2021-08-25 17:16:59 +02:00
|
|
|
with self.subTest(msg=env_id):
|
|
|
|
self._run_env(env_id)
|
|
|
|
|
|
|
|
def test_alr_environment_determinism(self):
|
|
|
|
"""Tests that identical seeds produce identical trajectories for ALR MP Envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
|
|
|
self._run_env_determinism(alr_envs.ALL_ALR_MOTION_PRIMITIVE_ENVIRONMENTS["DMP"])
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
|
|
|
self._run_env_determinism(alr_envs.ALL_ALR_MOTION_PRIMITIVE_ENVIRONMENTS["ProMP"])
|
2021-08-25 17:16:59 +02:00
|
|
|
|
|
|
|
def test_openai_environment_determinism(self):
|
|
|
|
"""Tests that identical seeds produce identical trajectories for OpenAI gym MP Envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
|
|
|
self._run_env_determinism(alr_envs.ALL_GYM_MOTION_PRIMITIVE_ENVIRONMENTS["DMP"])
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
|
|
|
self._run_env_determinism(alr_envs.ALL_GYM_MOTION_PRIMITIVE_ENVIRONMENTS["ProMP"])
|
2021-08-25 17:16:59 +02:00
|
|
|
|
|
|
|
def test_dmc_environment_determinism(self):
|
|
|
|
"""Tests that identical seeds produce identical trajectories for DMC MP Envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
2022-07-11 16:18:18 +02:00
|
|
|
self._run_env_determinism(alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS["DMP"])
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
2022-07-11 16:18:18 +02:00
|
|
|
self._run_env_determinism(alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS["ProMP"])
|
2021-08-25 17:16:59 +02:00
|
|
|
|
|
|
|
def test_metaworld_environment_determinism(self):
|
|
|
|
"""Tests that identical seeds produce identical trajectories for Metaworld MP Envs."""
|
|
|
|
with self.subTest(msg="DMP"):
|
|
|
|
self._run_env_determinism(alr_envs.ALL_METAWORLD_MOTION_PRIMITIVE_ENVIRONMENTS["DMP"])
|
2021-11-30 16:11:32 +01:00
|
|
|
with self.subTest(msg="ProMP"):
|
|
|
|
self._run_env_determinism(alr_envs.ALL_METAWORLD_MOTION_PRIMITIVE_ENVIRONMENTS["ProMP"])
|
2021-08-25 11:43:32 +02:00
|
|
|
|
2021-07-26 17:07:45 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|