From b8794697a81016c1dbec447d24df49b2f1899ad3 Mon Sep 17 00:00:00 2001 From: Kallinteris Andreas <30759571+Kallinteris-Andreas@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:21:29 +0200 Subject: [PATCH] MuJoco-v5 environments replace `ndarray.flat.copy()` with `ndarray.flatten()` (#815) --- gymnasium/envs/mujoco/ant_v5.py | 6 +++--- gymnasium/envs/mujoco/half_cheetah_v5.py | 4 ++-- gymnasium/envs/mujoco/hopper_v5.py | 4 ++-- gymnasium/envs/mujoco/humanoid_v5.py | 12 ++++++------ gymnasium/envs/mujoco/humanoidstandup_v5.py | 12 ++++++------ gymnasium/envs/mujoco/pusher_v5.py | 4 ++-- gymnasium/envs/mujoco/reacher_v5.py | 6 +++--- gymnasium/envs/mujoco/swimmer_v5.py | 4 ++-- gymnasium/envs/mujoco/walker2d_v5.py | 4 ++-- 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/gymnasium/envs/mujoco/ant_v5.py b/gymnasium/envs/mujoco/ant_v5.py index 4efd7ced5..5f15ad2cd 100644 --- a/gymnasium/envs/mujoco/ant_v5.py +++ b/gymnasium/envs/mujoco/ant_v5.py @@ -404,14 +404,14 @@ def step(self, action): return observation, reward, terminated, False, info def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = self.data.qvel.flat.copy() + position = self.data.qpos.flatten() + velocity = self.data.qvel.flatten() if self._exclude_current_positions_from_observation: position = position[2:] if self._include_cfrc_ext_in_observation: - contact_force = self.contact_forces[1:].flat.copy() + contact_force = self.contact_forces[1:].flatten() return np.concatenate((position, velocity, contact_force)) else: return np.concatenate((position, velocity)) diff --git a/gymnasium/envs/mujoco/half_cheetah_v5.py b/gymnasium/envs/mujoco/half_cheetah_v5.py index a5989f773..6499ca7fb 100644 --- a/gymnasium/envs/mujoco/half_cheetah_v5.py +++ b/gymnasium/envs/mujoco/half_cheetah_v5.py @@ -263,8 +263,8 @@ def step(self, action): return observation, reward, False, False, info def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = self.data.qvel.flat.copy() + position = self.data.qpos.flatten() + velocity = self.data.qvel.flatten() if self._exclude_current_positions_from_observation: position = position[1:] diff --git a/gymnasium/envs/mujoco/hopper_v5.py b/gymnasium/envs/mujoco/hopper_v5.py index 093199262..a1f1086fc 100644 --- a/gymnasium/envs/mujoco/hopper_v5.py +++ b/gymnasium/envs/mujoco/hopper_v5.py @@ -301,8 +301,8 @@ def terminated(self): return terminated def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = np.clip(self.data.qvel.flat.copy(), -10, 10) + position = self.data.qpos.flatten() + velocity = np.clip(self.data.qvel.flatten(), -10, 10) if self._exclude_current_positions_from_observation: position = position[1:] diff --git a/gymnasium/envs/mujoco/humanoid_v5.py b/gymnasium/envs/mujoco/humanoid_v5.py index c3afc977a..b0f078ec0 100644 --- a/gymnasium/envs/mujoco/humanoid_v5.py +++ b/gymnasium/envs/mujoco/humanoid_v5.py @@ -454,24 +454,24 @@ def terminated(self): return terminated def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = self.data.qvel.flat.copy() + position = self.data.qpos.flatten() + velocity = self.data.qvel.flatten() if self._include_cinert_in_observation is True: - com_inertia = self.data.cinert[1:].flat.copy() + com_inertia = self.data.cinert[1:].flatten() else: com_inertia = np.array([]) if self._include_cvel_in_observation is True: - com_velocity = self.data.cvel[1:].flat.copy() + com_velocity = self.data.cvel[1:].flatten() else: com_velocity = np.array([]) if self._include_qfrc_actuator_in_observation is True: - actuator_forces = self.data.qfrc_actuator[6:].flat.copy() + actuator_forces = self.data.qfrc_actuator[6:].flatten() else: actuator_forces = np.array([]) if self._include_cfrc_ext_in_observation is True: - external_contact_forces = self.data.cfrc_ext[1:].flat.copy() + external_contact_forces = self.data.cfrc_ext[1:].flatten() else: external_contact_forces = np.array([]) diff --git a/gymnasium/envs/mujoco/humanoidstandup_v5.py b/gymnasium/envs/mujoco/humanoidstandup_v5.py index ce6ab01c0..46e591ac8 100644 --- a/gymnasium/envs/mujoco/humanoidstandup_v5.py +++ b/gymnasium/envs/mujoco/humanoidstandup_v5.py @@ -405,24 +405,24 @@ def __init__( } def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = self.data.qvel.flat.copy() + position = self.data.qpos.flatten() + velocity = self.data.qvel.flatten() if self._include_cinert_in_observation is True: - com_inertia = self.data.cinert[1:].flat.copy() + com_inertia = self.data.cinert[1:].flatten() else: com_inertia = np.array([]) if self._include_cvel_in_observation is True: - com_velocity = self.data.cvel[1:].flat.copy() + com_velocity = self.data.cvel[1:].flatten() else: com_velocity = np.array([]) if self._include_qfrc_actuator_in_observation is True: - actuator_forces = self.data.qfrc_actuator[6:].flat.copy() + actuator_forces = self.data.qfrc_actuator[6:].flatten() else: actuator_forces = np.array([]) if self._include_cfrc_ext_in_observation is True: - external_contact_forces = self.data.cfrc_ext[1:].flat.copy() + external_contact_forces = self.data.cfrc_ext[1:].flatten() else: external_contact_forces = np.array([]) diff --git a/gymnasium/envs/mujoco/pusher_v5.py b/gymnasium/envs/mujoco/pusher_v5.py index d0e7e8b32..99d4eabf0 100644 --- a/gymnasium/envs/mujoco/pusher_v5.py +++ b/gymnasium/envs/mujoco/pusher_v5.py @@ -266,8 +266,8 @@ def reset_model(self): def _get_obs(self): return np.concatenate( [ - self.data.qpos.flat[:7], - self.data.qvel.flat[:7], + self.data.qpos.flatten()[:7], + self.data.qvel.flatten()[:7], self.get_body_com("tips_arm"), self.get_body_com("object"), self.get_body_com("goal"), diff --git a/gymnasium/envs/mujoco/reacher_v5.py b/gymnasium/envs/mujoco/reacher_v5.py index 3005480a5..e8ba867d1 100644 --- a/gymnasium/envs/mujoco/reacher_v5.py +++ b/gymnasium/envs/mujoco/reacher_v5.py @@ -231,13 +231,13 @@ def reset_model(self): return self._get_obs() def _get_obs(self): - theta = self.data.qpos.flat[:2] + theta = self.data.qpos.flatten()[:2] return np.concatenate( [ np.cos(theta), np.sin(theta), - self.data.qpos.flat[2:], - self.data.qvel.flat[:2], + self.data.qpos.flatten()[2:], + self.data.qvel.flatten()[:2], (self.get_body_com("fingertip") - self.get_body_com("target"))[:2], ] ) diff --git a/gymnasium/envs/mujoco/swimmer_v5.py b/gymnasium/envs/mujoco/swimmer_v5.py index 8d8166e1c..c49267bce 100644 --- a/gymnasium/envs/mujoco/swimmer_v5.py +++ b/gymnasium/envs/mujoco/swimmer_v5.py @@ -256,8 +256,8 @@ def step(self, action): return observation, reward, False, False, info def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = self.data.qvel.flat.copy() + position = self.data.qpos.flatten() + velocity = self.data.qvel.flatten() if self._exclude_current_positions_from_observation: position = position[2:] diff --git a/gymnasium/envs/mujoco/walker2d_v5.py b/gymnasium/envs/mujoco/walker2d_v5.py index ee623850a..dde3e9b03 100644 --- a/gymnasium/envs/mujoco/walker2d_v5.py +++ b/gymnasium/envs/mujoco/walker2d_v5.py @@ -295,8 +295,8 @@ def terminated(self): return terminated def _get_obs(self): - position = self.data.qpos.flat.copy() - velocity = np.clip(self.data.qvel.flat.copy(), -10, 10) + position = self.data.qpos.flatten() + velocity = np.clip(self.data.qvel.flatten(), -10, 10) if self._exclude_current_positions_from_observation: position = position[1:]