Source code for rofunc.learning.RofuncRL.tasks.isaacgymenv.ase.humanoid_amp_getup

# Copyright (c) 2018-2022, NVIDIA Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from isaacgym import gymtorch
from isaacgym.torch_utils import *

from rofunc.learning.RofuncRL.tasks.isaacgymenv.ase.humanoid_amp import HumanoidAMP


[docs]class HumanoidAMPGetupTask(HumanoidAMP): def __init__(self, cfg, rl_device, sim_device, graphics_device_id, headless, virtual_screen_capture, force_render): self.cfg = cfg self._recovery_episode_prob = cfg["env"]["recoveryEpisodeProb"] self._recovery_steps = cfg["env"]["recoverySteps"] self._fall_init_prob = cfg["env"]["fallInitProb"] self._reset_fall_env_ids = [] super().__init__(cfg=self.cfg, rl_device=rl_device, sim_device=sim_device, graphics_device_id=graphics_device_id, headless=headless, virtual_screen_capture=virtual_screen_capture, force_render=force_render) self._recovery_counter = torch.zeros(self.num_envs, device=self.device, dtype=torch.int) self._generate_fall_states()
[docs] def pre_physics_step(self, actions): super().pre_physics_step(actions) self._update_recovery_count()
def _generate_fall_states(self): max_steps = 150 env_ids = to_torch(np.arange(self.num_envs), device=self.device, dtype=torch.long) root_states = self._initial_humanoid_root_states[env_ids].clone() root_states[..., 3:7] = torch.randn_like(root_states[..., 3:7]) root_states[..., 3:7] = torch.nn.functional.normalize(root_states[..., 3:7], dim=-1) self._humanoid_root_states[env_ids] = root_states env_ids_int32 = self._humanoid_actor_ids[env_ids] self.gym.set_actor_root_state_tensor_indexed(self.sim, gymtorch.unwrap_tensor(self._root_states), gymtorch.unwrap_tensor(env_ids_int32), len(env_ids_int32)) self.gym.set_dof_state_tensor_indexed(self.sim, gymtorch.unwrap_tensor(self._dof_state), gymtorch.unwrap_tensor(env_ids_int32), len(env_ids_int32)) rand_actions = np.random.uniform(-0.5, 0.5, size=[self.num_envs, self.get_action_size()]) rand_actions = to_torch(rand_actions, device=self.device) self.pre_physics_step(rand_actions) # step physics and render each frame for i in range(max_steps): self.render() self.gym.simulate(self.sim) self._refresh_sim_tensors() self._fall_root_states = self._humanoid_root_states.clone() self._fall_root_states[:, 7:13] = 0 self._fall_dof_pos = self._dof_pos.clone() self._fall_dof_vel = torch.zeros_like(self._dof_vel, device=self.device, dtype=torch.float) def _reset_actors(self, env_ids): num_envs = env_ids.shape[0] recovery_probs = to_torch(np.array([self._recovery_episode_prob] * num_envs), device=self.device) recovery_mask = torch.bernoulli(recovery_probs) == 1.0 terminated_mask = (self._terminate_buf[env_ids] == 1) recovery_mask = torch.logical_and(recovery_mask, terminated_mask) recovery_ids = env_ids[recovery_mask] if len(recovery_ids) > 0: self._reset_recovery_episode(recovery_ids) nonrecovery_ids = env_ids[torch.logical_not(recovery_mask)] fall_probs = to_torch(np.array([self._fall_init_prob] * nonrecovery_ids.shape[0]), device=self.device) fall_mask = torch.bernoulli(fall_probs) == 1.0 fall_ids = nonrecovery_ids[fall_mask] if len(fall_ids) > 0: self._reset_fall_episode(fall_ids) nonfall_ids = nonrecovery_ids[torch.logical_not(fall_mask)] if len(nonfall_ids) > 0: super()._reset_actors(nonfall_ids) self._recovery_counter[nonfall_ids] = 0 def _reset_recovery_episode(self, env_ids): self._recovery_counter[env_ids] = self._recovery_steps def _reset_fall_episode(self, env_ids): fall_state_ids = torch.randint_like(env_ids, low=0, high=self._fall_root_states.shape[0]) self._humanoid_root_states[env_ids] = self._fall_root_states[fall_state_ids] self._dof_pos[env_ids] = self._fall_dof_pos[fall_state_ids] self._dof_vel[env_ids] = self._fall_dof_vel[fall_state_ids] self._recovery_counter[env_ids] = self._recovery_steps self._reset_fall_env_ids = env_ids
[docs] def reset_idx(self, env_ids): self._reset_fall_env_ids = [] super().reset_idx(env_ids)
def _init_amp_obs(self, env_ids): super()._init_amp_obs(env_ids) if len(self._reset_fall_env_ids) > 0: self._init_amp_obs_default(self._reset_fall_env_ids) def _update_recovery_count(self): self._recovery_counter -= 1 self._recovery_counter = torch.clamp_min(self._recovery_counter, 0) def _compute_reset(self): super()._compute_reset() is_recovery = self._recovery_counter > 0 self.reset_buf[is_recovery] = 0 self._terminate_buf[is_recovery] = 0