From ce6734e25824bb64a9d27044835667e63022d6fc Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Mon, 25 Nov 2024 23:11:27 -0500 Subject: [PATCH 1/8] update Fetch and Robotic environments to fix reset issue introduced in v1.3.0 --- gymnasium_robotics/__init__.py | 10 +++++----- gymnasium_robotics/envs/fetch/fetch_env.py | 6 ++++++ gymnasium_robotics/envs/robot_env.py | 8 ++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/gymnasium_robotics/__init__.py b/gymnasium_robotics/__init__.py index feb748a6..c1481ddc 100644 --- a/gymnasium_robotics/__init__.py +++ b/gymnasium_robotics/__init__.py @@ -30,7 +30,7 @@ def _merge(a, b): ) register( - id=f"FetchSlide{suffix}-v3", + id=f"FetchSlide{suffix}-v4", entry_point="gymnasium_robotics.envs.fetch.slide:MujocoFetchSlideEnv", kwargs=kwargs, max_episode_steps=50, @@ -44,7 +44,7 @@ def _merge(a, b): ) register( - id=f"FetchPickAndPlace{suffix}-v3", + id=f"FetchPickAndPlace{suffix}-v4", entry_point="gymnasium_robotics.envs.fetch.pick_and_place:MujocoFetchPickAndPlaceEnv", kwargs=kwargs, max_episode_steps=50, @@ -58,7 +58,7 @@ def _merge(a, b): ) register( - id=f"FetchReach{suffix}-v3", + id=f"FetchReach{suffix}-v4", entry_point="gymnasium_robotics.envs.fetch.reach:MujocoFetchReachEnv", kwargs=kwargs, max_episode_steps=50, @@ -72,7 +72,7 @@ def _merge(a, b): ) register( - id=f"FetchPush{suffix}-v3", + id=f"FetchPush{suffix}-v4", entry_point="gymnasium_robotics.envs.fetch.push:MujocoFetchPushEnv", kwargs=kwargs, max_episode_steps=50, @@ -87,7 +87,7 @@ def _merge(a, b): ) register( - id=f"HandReach{suffix}-v2", + id=f"HandReach{suffix}-v3", entry_point="gymnasium_robotics.envs.shadow_dexterous_hand.reach:MujocoHandReachEnv", kwargs=kwargs, max_episode_steps=50, diff --git a/gymnasium_robotics/envs/fetch/fetch_env.py b/gymnasium_robotics/envs/fetch/fetch_env.py index 1e90984c..5b59c2c6 100644 --- a/gymnasium_robotics/envs/fetch/fetch_env.py +++ b/gymnasium_robotics/envs/fetch/fetch_env.py @@ -376,6 +376,12 @@ def _reset_sim(self): # Reset buffers for joint states, actuators, warm-start, control buffers etc. self._mujoco.mj_resetData(self.model, self.data) + self.data.time = self.initial_time + self.data.qpos[:] = np.copy(self.initial_qpos) + self.data.qvel[:] = np.copy(self.initial_qvel) + if self.model.na != 0: + self.data.act[:] = None + # Randomize start position of object. if self.has_object: object_xpos = self.initial_gripper_xpos[:2] diff --git a/gymnasium_robotics/envs/robot_env.py b/gymnasium_robotics/envs/robot_env.py index e482715d..9353873a 100644 --- a/gymnasium_robotics/envs/robot_env.py +++ b/gymnasium_robotics/envs/robot_env.py @@ -305,6 +305,14 @@ def _initialize_simulation(self): def _reset_sim(self): # Reset buffers for joint states, warm-start, control buffers etc. mujoco.mj_resetData(self.model, self.data) + + self.data.time = self.initial_time + self.data.qpos[:] = np.copy(self.initial_qpos) + self.data.qvel[:] = np.copy(self.initial_qvel) + if self.model.na != 0: + self.data.act[:] = None + + self._mujoco.mj_forward(self.model, self.data) return super()._reset_sim() def render(self): From 0d0662e5cd54236a2b77a41c5cf26fbda0c38418 Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Sat, 30 Nov 2024 12:55:04 -0500 Subject: [PATCH 2/8] update test_envs --- tests/envs/hand/test_reach.py | 2 +- tests/test_envs.py | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/envs/hand/test_reach.py b/tests/envs/hand/test_reach.py index 1b91afd9..52080b4d 100644 --- a/tests/envs/hand/test_reach.py +++ b/tests/envs/hand/test_reach.py @@ -8,7 +8,7 @@ def test_serialize_deserialize(): - env1 = gym.make("HandReach-v2", distance_threshold=1e-6) + env1 = gym.make("HandReach-v3", distance_threshold=1e-6) env1.reset() env2 = pickle.loads(pickle.dumps(env1)) diff --git a/tests/test_envs.py b/tests/test_envs.py index f880b297..6e966998 100644 --- a/tests/test_envs.py +++ b/tests/test_envs.py @@ -1,6 +1,8 @@ import pickle import warnings +import numpy as np + import gymnasium as gym import pytest from gymnasium.envs.mujoco.utils import check_mujoco_reset_state @@ -163,3 +165,47 @@ def test_pickle_env(env_spec): data_equivalence(env.step(action), pickled_env.step(action)) env.close() pickled_env.close() + +_test_robot_env_reset_list = ['Fetch', 'HandReach'] + +@pytest.mark.parametrize( + "spec", + [spec for spec in non_mujoco_py_env_specs if np.any([tar in spec.id for tar in _test_robot_env_reset_list])], + ids=[spec.id for spec in non_mujoco_py_env_specs if np.any([tar in spec.id for tar in _test_robot_env_reset_list])] +) +def test_robot_env_reset(spec): + """Checks initial state of robotic environment, i.e. Fetch and Shadow Dexterous Hand Reach, + whether their initial states align with the document.""" + + def _test_initial_states(env, seed=None): + diag_dict = {} + + init_obs = env.reset(seed = seed) + + if isinstance(init_obs[0], dict): + diag_dict.update(**init_obs[0]) + elif isinstance(init_obs[0], np.ndarray): + diag_dict.update({'observation': init_obs[0]}) + diag_dict.update({ + 'qpos': env.unwrapped.data.qpos, + 'qvel': env.unwrapped.data.qvel, + 'init_qpos': env.unwrapped.initial_qpos, + 'init_qvel': env.unwrapped.initial_qvel + }) + + # exclude object location from environments + # if spec.id[:-3] in ['FetchPush', 'FetchPickAndPlace', 'FetchSlide',]: + if np.any([tar in spec.id for tar in ['FetchPush', 'FetchPickAndPlace', 'FetchSlide',]]): + diag_dict['qpos'] = np.delete(diag_dict['qpos'], np.s_[-7:-5]) + diag_dict['init_qpos'] = np.delete(diag_dict['init_qpos'], np.s_[-7:-5]) + + # testing + assert np.allclose(diag_dict['qpos'], diag_dict['init_qpos']) + assert np.allclose(diag_dict['qvel'], diag_dict['init_qvel']) + return diag_dict + + cur_env: gym.Env = spec.make() + + _test_initial_states(cur_env, seed=24) + _test_initial_states(cur_env, seed=10) + return From 2e6010f8abd74c45a6f6853a19c20024bcd08918 Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Sat, 30 Nov 2024 13:23:13 -0500 Subject: [PATCH 3/8] delete commented unused code --- tests/test_envs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_envs.py b/tests/test_envs.py index 6e966998..e27524ae 100644 --- a/tests/test_envs.py +++ b/tests/test_envs.py @@ -194,7 +194,6 @@ def _test_initial_states(env, seed=None): }) # exclude object location from environments - # if spec.id[:-3] in ['FetchPush', 'FetchPickAndPlace', 'FetchSlide',]: if np.any([tar in spec.id for tar in ['FetchPush', 'FetchPickAndPlace', 'FetchSlide',]]): diag_dict['qpos'] = np.delete(diag_dict['qpos'], np.s_[-7:-5]) diag_dict['init_qpos'] = np.delete(diag_dict['init_qpos'], np.s_[-7:-5]) From ed9c7372a2c6bec0ab152004e6ddf3ed2f4925fe Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Wed, 11 Dec 2024 12:45:37 -0500 Subject: [PATCH 4/8] update changelog for Fetch environment and HandReach of Shadow Dexterous Hand --- gymnasium_robotics/envs/fetch/pick_and_place.py | 1 + gymnasium_robotics/envs/fetch/push.py | 2 ++ gymnasium_robotics/envs/fetch/reach.py | 2 ++ gymnasium_robotics/envs/fetch/slide.py | 2 ++ gymnasium_robotics/envs/shadow_dexterous_hand/reach.py | 1 + 5 files changed, 8 insertions(+) diff --git a/gymnasium_robotics/envs/fetch/pick_and_place.py b/gymnasium_robotics/envs/fetch/pick_and_place.py index 37f9a75b..32124700 100644 --- a/gymnasium_robotics/envs/fetch/pick_and_place.py +++ b/gymnasium_robotics/envs/fetch/pick_and_place.py @@ -130,6 +130,7 @@ class MujocoFetchPickAndPlaceEnv(MujocoFetchEnv, EzPickle): ## Version History + * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/fetch/push.py b/gymnasium_robotics/envs/fetch/push.py index 5420be0b..994cce95 100644 --- a/gymnasium_robotics/envs/fetch/push.py +++ b/gymnasium_robotics/envs/fetch/push.py @@ -157,6 +157,8 @@ class MujocoFetchPushEnv(MujocoFetchEnv, EzPickle): ``` ## Version History + + * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/fetch/reach.py b/gymnasium_robotics/envs/fetch/reach.py index cb227f9f..0a778592 100644 --- a/gymnasium_robotics/envs/fetch/reach.py +++ b/gymnasium_robotics/envs/fetch/reach.py @@ -115,6 +115,8 @@ class MujocoFetchReachEnv(MujocoFetchEnv, EzPickle): ``` ## Version History + + * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/fetch/slide.py b/gymnasium_robotics/envs/fetch/slide.py index d4898aed..87df28b4 100644 --- a/gymnasium_robotics/envs/fetch/slide.py +++ b/gymnasium_robotics/envs/fetch/slide.py @@ -156,6 +156,8 @@ class MujocoFetchSlideEnv(MujocoFetchEnv, EzPickle): ``` ## Version History + + * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py b/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py index 19665424..aafaa3c2 100644 --- a/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py +++ b/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py @@ -387,6 +387,7 @@ class MujocoHandReachEnv(get_base_hand_reanch_env(MujocoHandEnv)): ``` ## Version History + * v3: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) * v2: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v1: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v0: the environment depends on `mujoco_py` which is no longer maintained. From 35baa79fedf5b7bed6056a5c87e81d0645877ca7 Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Wed, 11 Dec 2024 12:47:10 -0500 Subject: [PATCH 5/8] correct HandReach changelog description --- gymnasium_robotics/envs/shadow_dexterous_hand/reach.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py b/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py index aafaa3c2..de338213 100644 --- a/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py +++ b/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py @@ -387,7 +387,7 @@ class MujocoHandReachEnv(get_base_hand_reanch_env(MujocoHandEnv)): ``` ## Version History - * v3: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) + * v3: Fixed bug: the initial state not matching the initial state description in the documentation. Hand Reach environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) * v2: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v1: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v0: the environment depends on `mujoco_py` which is no longer maintained. From 83b4a5f7039a011438df526ba0d46522563ea26d Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Fri, 13 Dec 2024 17:14:22 -0500 Subject: [PATCH 6/8] update changelog description and fix test_envs.py formatting --- .../envs/fetch/pick_and_place.py | 2 +- gymnasium_robotics/envs/fetch/push.py | 4 +- gymnasium_robotics/envs/fetch/reach.py | 4 +- gymnasium_robotics/envs/fetch/slide.py | 4 +- .../envs/shadow_dexterous_hand/reach.py | 3 +- tests/test_envs.py | 64 ++++++++++++------- 6 files changed, 51 insertions(+), 30 deletions(-) diff --git a/gymnasium_robotics/envs/fetch/pick_and_place.py b/gymnasium_robotics/envs/fetch/pick_and_place.py index 32124700..187e3451 100644 --- a/gymnasium_robotics/envs/fetch/pick_and_place.py +++ b/gymnasium_robotics/envs/fetch/pick_and_place.py @@ -130,7 +130,7 @@ class MujocoFetchPickAndPlaceEnv(MujocoFetchEnv, EzPickle): ## Version History - * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) + * v4: Fixed bug where initial state did not match initial state description in documentation. Fetch environments' initial states after reset now match the documentation (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)). * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/fetch/push.py b/gymnasium_robotics/envs/fetch/push.py index 994cce95..8ef1a0d8 100644 --- a/gymnasium_robotics/envs/fetch/push.py +++ b/gymnasium_robotics/envs/fetch/push.py @@ -157,8 +157,8 @@ class MujocoFetchPushEnv(MujocoFetchEnv, EzPickle): ``` ## Version History - - * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) + + * v4: Fixed bug where initial state did not match initial state description in documentation. Fetch environments' initial states after reset now match the documentation (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)). * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/fetch/reach.py b/gymnasium_robotics/envs/fetch/reach.py index 0a778592..7a95146e 100644 --- a/gymnasium_robotics/envs/fetch/reach.py +++ b/gymnasium_robotics/envs/fetch/reach.py @@ -115,8 +115,8 @@ class MujocoFetchReachEnv(MujocoFetchEnv, EzPickle): ``` ## Version History - - * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) + + * v4: Fixed bug where initial state did not match initial state description in documentation. Fetch environments' initial states after reset now match the documentation (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)). * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/fetch/slide.py b/gymnasium_robotics/envs/fetch/slide.py index 87df28b4..686b854c 100644 --- a/gymnasium_robotics/envs/fetch/slide.py +++ b/gymnasium_robotics/envs/fetch/slide.py @@ -156,8 +156,8 @@ class MujocoFetchSlideEnv(MujocoFetchEnv, EzPickle): ``` ## Version History - - * v4: Fixed bug: the initial state not matching the initial state description in the documentation. Fetch environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) + + * v4: Fixed bug where initial state did not match initial state description in documentation. Fetch environments' initial states after reset now match the documentation (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)). * v3: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v2: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v1: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py b/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py index de338213..8d99d5b6 100644 --- a/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py +++ b/gymnasium_robotics/envs/shadow_dexterous_hand/reach.py @@ -387,7 +387,8 @@ class MujocoHandReachEnv(get_base_hand_reanch_env(MujocoHandEnv)): ``` ## Version History - * v3: Fixed bug: the initial state not matching the initial state description in the documentation. Hand Reach environments' initial states after reset now align with the documentation.(related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)) + + * v3: Fixed bug where initial state did not match initial state description in documentation. Hand Reach environments' initial states after reset now match the documentation (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/251)). * v2: Fixed bug: `env.reset()` not properly resetting the internal state. Fetch environments now properly reset their state (related [GitHub issue](https://github.com/Farama-Foundation/Gymnasium-Robotics/issues/207)). * v1: the environment depends on the newest [mujoco python bindings](https://mujoco.readthedocs.io/en/latest/python.html) maintained by the MuJoCo team in Deepmind. * v0: the environment depends on `mujoco_py` which is no longer maintained. diff --git a/tests/test_envs.py b/tests/test_envs.py index e27524ae..ccc0bf32 100644 --- a/tests/test_envs.py +++ b/tests/test_envs.py @@ -1,9 +1,8 @@ import pickle import warnings -import numpy as np - import gymnasium as gym +import numpy as np import pytest from gymnasium.envs.mujoco.utils import check_mujoco_reset_state from gymnasium.envs.registration import EnvSpec @@ -166,43 +165,64 @@ def test_pickle_env(env_spec): env.close() pickled_env.close() -_test_robot_env_reset_list = ['Fetch', 'HandReach'] + +_test_robot_env_reset_list = ["Fetch", "HandReach"] + @pytest.mark.parametrize( - "spec", - [spec for spec in non_mujoco_py_env_specs if np.any([tar in spec.id for tar in _test_robot_env_reset_list])], - ids=[spec.id for spec in non_mujoco_py_env_specs if np.any([tar in spec.id for tar in _test_robot_env_reset_list])] + "spec", + [ + spec + for spec in non_mujoco_py_env_specs + if np.any([tar in spec.id for tar in _test_robot_env_reset_list]) + ], + ids=[ + spec.id + for spec in non_mujoco_py_env_specs + if np.any([tar in spec.id for tar in _test_robot_env_reset_list]) + ], ) def test_robot_env_reset(spec): - """Checks initial state of robotic environment, i.e. Fetch and Shadow Dexterous Hand Reach, - whether their initial states align with the document.""" + """Check initial state of robotic environment, i.e. Fetch and Shadow Dexterous Hand Reach, + whether their initial states match the description in the documentation.""" def _test_initial_states(env, seed=None): diag_dict = {} - init_obs = env.reset(seed = seed) + init_obs = env.reset(seed=seed) if isinstance(init_obs[0], dict): diag_dict.update(**init_obs[0]) elif isinstance(init_obs[0], np.ndarray): - diag_dict.update({'observation': init_obs[0]}) - diag_dict.update({ - 'qpos': env.unwrapped.data.qpos, - 'qvel': env.unwrapped.data.qvel, - 'init_qpos': env.unwrapped.initial_qpos, - 'init_qvel': env.unwrapped.initial_qvel - }) + diag_dict.update({"observation": init_obs[0]}) + diag_dict.update( + { + "qpos": env.unwrapped.data.qpos, + "qvel": env.unwrapped.data.qvel, + "init_qpos": env.unwrapped.initial_qpos, + "init_qvel": env.unwrapped.initial_qvel, + } + ) # exclude object location from environments - if np.any([tar in spec.id for tar in ['FetchPush', 'FetchPickAndPlace', 'FetchSlide',]]): - diag_dict['qpos'] = np.delete(diag_dict['qpos'], np.s_[-7:-5]) - diag_dict['init_qpos'] = np.delete(diag_dict['init_qpos'], np.s_[-7:-5]) + if np.any( + [ + tar in spec.id + for tar in [ + "FetchPush", + "FetchPickAndPlace", + "FetchSlide", + ] + ] + ): + diag_dict["qpos"] = np.delete(diag_dict["qpos"], np.s_[-7:-5]) + diag_dict["init_qpos"] = np.delete(diag_dict["init_qpos"], np.s_[-7:-5]) # testing - assert np.allclose(diag_dict['qpos'], diag_dict['init_qpos']) - assert np.allclose(diag_dict['qvel'], diag_dict['init_qvel']) + assert np.allclose(diag_dict["qpos"], diag_dict["init_qpos"]) + assert np.allclose(diag_dict["qvel"], diag_dict["init_qvel"]) return diag_dict - + cur_env: gym.Env = spec.make() _test_initial_states(cur_env, seed=24) From fba389283ff8f79c1448f28bbd35fc23b0acebf0 Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Sat, 14 Dec 2024 01:20:10 -0500 Subject: [PATCH 7/8] update test_envs.py --- tests/test_envs.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/test_envs.py b/tests/test_envs.py index ccc0bf32..64466c03 100644 --- a/tests/test_envs.py +++ b/tests/test_envs.py @@ -189,12 +189,8 @@ def test_robot_env_reset(spec): def _test_initial_states(env, seed=None): diag_dict = {} - init_obs = env.reset(seed=seed) + env.reset(seed=seed) - if isinstance(init_obs[0], dict): - diag_dict.update(**init_obs[0]) - elif isinstance(init_obs[0], np.ndarray): - diag_dict.update({"observation": init_obs[0]}) diag_dict.update( { "qpos": env.unwrapped.data.qpos, @@ -227,4 +223,3 @@ def _test_initial_states(env, seed=None): _test_initial_states(cur_env, seed=24) _test_initial_states(cur_env, seed=10) - return From 7a3b71af7c346f5575f11344992450a10ef54129 Mon Sep 17 00:00:00 2001 From: wjxgeorge <12130815d@connect.polyu.hk> Date: Tue, 17 Dec 2024 12:58:03 -0500 Subject: [PATCH 8/8] update assertion to equal instead of allclose --- tests/test_envs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_envs.py b/tests/test_envs.py index 64466c03..397aa8a5 100644 --- a/tests/test_envs.py +++ b/tests/test_envs.py @@ -215,8 +215,8 @@ def _test_initial_states(env, seed=None): diag_dict["init_qpos"] = np.delete(diag_dict["init_qpos"], np.s_[-7:-5]) # testing - assert np.allclose(diag_dict["qpos"], diag_dict["init_qpos"]) - assert np.allclose(diag_dict["qvel"], diag_dict["init_qvel"]) + assert np.all(diag_dict["qpos"] == diag_dict["init_qpos"]) + assert np.all(diag_dict["qvel"] == diag_dict["init_qvel"]) return diag_dict cur_env: gym.Env = spec.make()