naming convention and running tests
This commit is contained in:
+13
-12
@@ -40,9 +40,9 @@ class TestMPEnvironments(unittest.TestCase):
|
||||
for i in range(iterations):
|
||||
observations.append(obs)
|
||||
|
||||
ac = env.action_space.sample()
|
||||
actions = env.action_space.sample()
|
||||
# ac = np.random.uniform(env.action_space.low, env.action_space.high, env.action_space.shape)
|
||||
obs, reward, done, info = env.step(ac)
|
||||
obs, reward, done, info = env.step(actions)
|
||||
|
||||
self._verify_observations(obs, env.observation_space, "step()")
|
||||
self._verify_reward(reward)
|
||||
@@ -55,13 +55,13 @@ class TestMPEnvironments(unittest.TestCase):
|
||||
env.render("human")
|
||||
|
||||
if done:
|
||||
obs = env.reset()
|
||||
break
|
||||
|
||||
assert done, "Done flag is not True after max episode length."
|
||||
assert done, "Done flag is not True after end of episode."
|
||||
observations.append(obs)
|
||||
env.close()
|
||||
del env
|
||||
return np.array(observations), np.array(rewards), np.array(dones)
|
||||
return np.array(observations), np.array(rewards), np.array(dones), np.array(actions)
|
||||
|
||||
def _run_env_determinism(self, ids):
|
||||
seed = 0
|
||||
@@ -70,8 +70,9 @@ class TestMPEnvironments(unittest.TestCase):
|
||||
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)):
|
||||
obs1, rwd1, done1, obs2, rwd2, done2 = time_step
|
||||
self.assertTrue(np.allclose(obs1, obs2), f"Observations [{i}] {obs1} and {obs2} do not match.")
|
||||
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.")
|
||||
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.")
|
||||
|
||||
@@ -81,7 +82,7 @@ class TestMPEnvironments(unittest.TestCase):
|
||||
f"not contained in observation space {observation_space}.")
|
||||
|
||||
def _verify_reward(self, reward):
|
||||
self.assertIsInstance(reward, float, f"Returned {reward} as reward, expected float.")
|
||||
self.assertIsInstance(reward, (float, int), f"Returned type {type(reward)} as reward, expected float or int.")
|
||||
|
||||
def _verify_done(self, done):
|
||||
self.assertIsInstance(done, bool, f"Returned {done} as done flag, expected bool.")
|
||||
@@ -113,12 +114,12 @@ class TestMPEnvironments(unittest.TestCase):
|
||||
def test_dmc_environment_functionality(self):
|
||||
"""Tests that environments runs without errors using random actions for DMC MP envs."""
|
||||
with self.subTest(msg="DMP"):
|
||||
for env_id in alr_envs.ALL_DEEPMIND_MOTION_PRIMITIVE_ENVIRONMENTS['DMP']:
|
||||
for env_id in alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS['DMP']:
|
||||
with self.subTest(msg=env_id):
|
||||
self._run_env(env_id)
|
||||
|
||||
with self.subTest(msg="ProMP"):
|
||||
for env_id in alr_envs.ALL_DEEPMIND_MOTION_PRIMITIVE_ENVIRONMENTS['ProMP']:
|
||||
for env_id in alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS['ProMP']:
|
||||
with self.subTest(msg=env_id):
|
||||
self._run_env(env_id)
|
||||
|
||||
@@ -151,9 +152,9 @@ class TestMPEnvironments(unittest.TestCase):
|
||||
def test_dmc_environment_determinism(self):
|
||||
"""Tests that identical seeds produce identical trajectories for DMC MP Envs."""
|
||||
with self.subTest(msg="DMP"):
|
||||
self._run_env_determinism(alr_envs.ALL_DEEPMIND_MOTION_PRIMITIVE_ENVIRONMENTS["DMP"])
|
||||
self._run_env_determinism(alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS["DMP"])
|
||||
with self.subTest(msg="ProMP"):
|
||||
self._run_env_determinism(alr_envs.ALL_DEEPMIND_MOTION_PRIMITIVE_ENVIRONMENTS["ProMP"])
|
||||
self._run_env_determinism(alr_envs.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS["ProMP"])
|
||||
|
||||
def test_metaworld_environment_determinism(self):
|
||||
"""Tests that identical seeds produce identical trajectories for Metaworld MP Envs."""
|
||||
|
||||
+15
-11
@@ -7,8 +7,8 @@ from dm_control import suite, manipulation
|
||||
|
||||
from alr_envs import make
|
||||
|
||||
DMC_ENVS = [f'{env}-{task}' for env, task in suite.ALL_TASKS if env != "lqr"]
|
||||
MANIPULATION_SPECS = [f'manipulation-{task}' for task in manipulation.ALL if task.endswith('_features')]
|
||||
DMC_ENVS = [f'dmc:{env}-{task}' for env, task in suite.ALL_TASKS if env != "lqr"]
|
||||
MANIPULATION_SPECS = [f'dmc:manipulation-{task}' for task in manipulation.ALL if task.endswith('_features')]
|
||||
SEED = 1
|
||||
|
||||
|
||||
@@ -29,9 +29,11 @@ class TestStepDMCEnvironments(unittest.TestCase):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
print(env_id)
|
||||
env: gym.Env = make(env_id, seed=seed)
|
||||
rewards = []
|
||||
observations = []
|
||||
actions = []
|
||||
dones = []
|
||||
obs = env.reset()
|
||||
self._verify_observations(obs, env.observation_space, "reset()")
|
||||
@@ -43,6 +45,7 @@ class TestStepDMCEnvironments(unittest.TestCase):
|
||||
observations.append(obs)
|
||||
|
||||
ac = env.action_space.sample()
|
||||
actions.append(ac)
|
||||
# ac = np.random.uniform(env.action_space.low, env.action_space.high, env.action_space.shape)
|
||||
obs, reward, done, info = env.step(ac)
|
||||
|
||||
@@ -57,13 +60,13 @@ class TestStepDMCEnvironments(unittest.TestCase):
|
||||
env.render("human")
|
||||
|
||||
if done:
|
||||
obs = env.reset()
|
||||
break
|
||||
|
||||
assert done, "Done flag is not True after max episode length."
|
||||
assert done, "Done flag is not True after end of episode."
|
||||
observations.append(obs)
|
||||
env.close()
|
||||
del env
|
||||
return np.array(observations), np.array(rewards), np.array(dones)
|
||||
return np.array(observations), np.array(rewards), np.array(dones), np.array(actions)
|
||||
|
||||
def _verify_observations(self, obs, observation_space, obs_type="reset()"):
|
||||
self.assertTrue(observation_space.contains(obs),
|
||||
@@ -71,7 +74,7 @@ class TestStepDMCEnvironments(unittest.TestCase):
|
||||
f"not contained in observation space {observation_space}.")
|
||||
|
||||
def _verify_reward(self, reward):
|
||||
self.assertIsInstance(reward, float, f"Returned {reward} as reward, expected float.")
|
||||
self.assertIsInstance(reward, (float, int), f"Returned type {type(reward)} as reward, expected float or int.")
|
||||
|
||||
def _verify_done(self, done):
|
||||
self.assertIsInstance(done, bool, f"Returned {done} as done flag, expected bool.")
|
||||
@@ -91,8 +94,9 @@ class TestStepDMCEnvironments(unittest.TestCase):
|
||||
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)):
|
||||
obs1, rwd1, done1, obs2, rwd2, done2 = time_step
|
||||
self.assertTrue(np.array_equal(obs1, obs2), f"Observations [{i}] {obs1} and {obs2} do not match.")
|
||||
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.")
|
||||
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.")
|
||||
|
||||
@@ -111,11 +115,11 @@ class TestStepDMCEnvironments(unittest.TestCase):
|
||||
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)):
|
||||
obs1, rwd1, done1, obs2, rwd2, done2 = time_step
|
||||
self.assertTrue(np.array_equal(obs1, obs2), f"Observations [{i}] {obs1} and {obs2} do not match.")
|
||||
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.")
|
||||
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.")
|
||||
self.assertEqual(done1, done2, f"Dones [{i}] {done1} and {done2} do not match.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -6,7 +6,7 @@ import numpy as np
|
||||
from alr_envs import make
|
||||
from metaworld.envs import ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE
|
||||
|
||||
ALL_ENVS = [env.split("-goal-observable")[0] for env, _ in ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE.items()]
|
||||
ALL_ENVS = [f'metaworld:{env.split("-goal-observable")[0]}' for env, _ in ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE.items()]
|
||||
SEED = 1
|
||||
|
||||
|
||||
@@ -57,9 +57,9 @@ class TestStepMetaWorlEnvironments(unittest.TestCase):
|
||||
env.render("human")
|
||||
|
||||
if done:
|
||||
obs = env.reset()
|
||||
break
|
||||
|
||||
assert done, "Done flag is not True after max episode length."
|
||||
assert done, "Done flag is not True after end of episode."
|
||||
observations.append(obs)
|
||||
env.close()
|
||||
del env
|
||||
@@ -71,7 +71,7 @@ class TestStepMetaWorlEnvironments(unittest.TestCase):
|
||||
f"not contained in observation space {observation_space}.")
|
||||
|
||||
def _verify_reward(self, reward):
|
||||
self.assertIsInstance(reward, float, f"Returned {reward} as reward, expected float.")
|
||||
self.assertIsInstance(reward, (float, int), f"Returned type {type(reward)} as reward, expected float or int.")
|
||||
|
||||
def _verify_done(self, done):
|
||||
self.assertIsInstance(done, bool, f"Returned {done} as done flag, expected bool.")
|
||||
@@ -94,7 +94,7 @@ class TestStepMetaWorlEnvironments(unittest.TestCase):
|
||||
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.")
|
||||
self.assertAlmostEqual(rwd1, rwd2, f"Rewards [{i}] {rwd1} and {rwd2} do not match.")
|
||||
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.")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user