in safety_gym/envs/engine.py [0:0]
def step(self, action):
''' Take a step and return observation, reward, done, and info '''
action = np.array(action, copy=False) # Cast to ndarray
assert not self.done, 'Environment must be reset before stepping'
info = {}
# Set action
action_range = self.model.actuator_ctrlrange
# action_scale = action_range[:,1] - action_range[:, 0]
self.data.ctrl[:] = np.clip(action, action_range[:,0], action_range[:,1]) #np.clip(action * 2 / action_scale, -1, 1)
if self.action_noise:
self.data.ctrl[:] += self.action_noise * self.rs.randn(self.model.nu)
# Simulate physics forward
exception = False
for _ in range(self.rs.binomial(self.frameskip_binom_n, self.frameskip_binom_p)):
try:
self.set_mocaps()
self.sim.step() # Physics simulation step
except MujocoException as me:
print('MujocoException', me)
exception = True
break
if exception:
self.done = True
reward = self.reward_exception
info['cost_exception'] = 1.0
else:
self.sim.forward() # Needed to get sensor readings correct!
# Reward processing
reward = self.reward()
# Constraint violations
info.update(self.cost())
# Button timer (used to delay button resampling)
self.buttons_timer_tick()
# Goal processing
if self.goal_met():
info['goal_met'] = True
reward += self.reward_goal
if self.continue_goal:
# Update the internal layout so we can correctly resample (given objects have moved)
self.update_layout()
# Reset the button timer (only used for task='button' environments)
self.buttons_timer = self.buttons_resampling_delay
# Try to build a new goal, end if we fail
if self.terminate_resample_failure:
try:
self.build_goal()
except ResamplingError as e:
# Normal end of episode
self.done = True
else:
# Try to make a goal, which could raise a ResamplingError exception
self.build_goal()
else:
self.done = True
# Timeout
self.steps += 1
if self.steps >= self.num_steps:
self.done = True # Maximum number of steps in an episode reached
return self.obs(), reward, self.done, info