MCPcopy Create free account
hub / github.com/InternRobotics/UniHSI / apply_randomizations

Method apply_randomizations

unihsi/env/tasks/base_task.py:211–405  ·  view source on GitHub ↗
(self, dr_params)

Source from the content-addressed store, hash-verified

209
210 # Apply randomizations only on resets, due to current PhysX limitations
211 def apply_randomizations(self, dr_params):
212 # If we don't have a randomization frequency, randomize every step
213 rand_freq = dr_params.get("frequency", 1)
214
215 # First, determine what to randomize:
216 # - non-environment parameters when > frequency steps have passed since the last non-environment
217 # - physical environments in the reset buffer, which have exceeded the randomization frequency threshold
218 # - on the first call, randomize everything
219 self.last_step = self.gym.get_frame_count(self.sim)
220 if self.first_randomization:
221 do_nonenv_randomize = True
222 env_ids = list(range(self.num_envs))
223 else:
224 do_nonenv_randomize = (self.last_step - self.last_rand_step) >= rand_freq
225 rand_envs = torch.where(self.randomize_buf >= rand_freq, torch.ones_like(self.randomize_buf), torch.zeros_like(self.randomize_buf))
226 rand_envs = torch.logical_and(rand_envs, self.reset_buf)
227 env_ids = torch.nonzero(rand_envs, as_tuple=False).squeeze(-1).tolist()
228 self.randomize_buf[rand_envs] = 0
229
230 if do_nonenv_randomize:
231 self.last_rand_step = self.last_step
232
233 param_setters_map = get_property_setter_map(self.gym)
234 param_setter_defaults_map = get_default_setter_args(self.gym)
235 param_getters_map = get_property_getter_map(self.gym)
236
237 # On first iteration, check the number of buckets
238 if self.first_randomization:
239 check_buckets(self.gym, self.envs, dr_params)
240
241 for nonphysical_param in ["observations", "actions"]:
242 if nonphysical_param in dr_params and do_nonenv_randomize:
243 dist = dr_params[nonphysical_param]["distribution"]
244 op_type = dr_params[nonphysical_param]["operation"]
245 sched_type = dr_params[nonphysical_param]["schedule"] if "schedule" in dr_params[nonphysical_param] else None
246 sched_step = dr_params[nonphysical_param]["schedule_steps"] if "schedule" in dr_params[nonphysical_param] else None
247 op = operator.add if op_type == 'additive' else operator.mul
248
249 if sched_type == 'linear':
250 sched_scaling = 1.0 / sched_step * \
251 min(self.last_step, sched_step)
252 elif sched_type == 'constant':
253 sched_scaling = 0 if self.last_step < sched_step else 1
254 else:
255 sched_scaling = 1
256
257 if dist == 'gaussian':
258 mu, var = dr_params[nonphysical_param]["range"]
259 mu_corr, var_corr = dr_params[nonphysical_param].get("range_correlated", [0., 0.])
260
261 if op_type == 'additive':
262 mu *= sched_scaling
263 var *= sched_scaling
264 mu_corr *= sched_scaling
265 var_corr *= sched_scaling
266 elif op_type == 'scaling':
267 var = var * sched_scaling # scale up var over time
268 mu = mu * sched_scaling + 1.0 * \

Callers

nothing calls this directly

Calls 2

get_attr_val_from_sampleFunction · 0.85
sampleMethod · 0.80

Tested by

no test coverage detected