(self, physics, random_state, ignore_contacts_with_entities=None)
Sets initial prop poses.

    Args:
      physics: An `mjcf.Physics` instance.
      random_state: a `np.random.RandomState` instance.
      ignore_contacts_with_entities: a list of `composer.Entity` instances
        to ignore when detecting collisions. This can be used to ignore props
        that are not being placed by this initializer, but are known to be
        colliding in the current state of the simulation (for example if they
        are going to be placed by a different initializer that will be called
        subsequently).

    Raises:
      RuntimeError: If `ignore_collisions == False` and a non-colliding prop
        pose could not be found within `max_attempts_per_prop`.
Sets initial prop poses.
Sets initial prop poses .
def __call__(self, physics, random_state, ignore_contacts_with_entities=None):
    """Sets initial prop poses.

    Args:
      physics: An `mjcf.Physics` instance.
      random_state: a `np.random.RandomState` instance.
      ignore_contacts_with_entities: a list of `composer.Entity` instances
        to ignore when detecting collisions. This can be used to ignore props
        that are not being placed by this initializer, but are known to be
        colliding in the current state of the simulation (for example if they
        are going to be placed by a different initializer that will be called
        subsequently).

    Raises:
      RuntimeError: If `ignore_collisions == False` and a non-colliding prop
        pose could not be found within `max_attempts_per_prop`.
    """
    if ignore_contacts_with_entities is None:
      ignore_contacts_with_entities = []
    # Temporarily disable contacts for all geoms that belong to props which
    # haven't yet been placed in order to free up space in the contact buffer.
    cached_contact_params = self._disable_and_cache_contact_parameters(
        physics, self._props + ignore_contacts_with_entities)

    try:
      physics.forward()
    except control.PhysicsError as cause:
      effect = control.PhysicsError(
          'Despite disabling contact for all props in this initializer, '
          '`physics.forward()` resulted in a `PhysicsError`')
      raise effect from cause

    def place_props():
      for prop in self._props:
        # Restore the original contact parameters for all geoms in the prop
        # we're about to place, so that we can detect if the new pose results in
        # collisions.
        self._restore_contact_parameters(physics, prop, cached_contact_params)

        success = False
        initial_position, initial_quaternion = prop.get_pose(physics)
        next_position, next_quaternion = initial_position, initial_quaternion
        for _ in range(self._max_attempts_per_prop):
          next_position = variation.evaluate(self._position,
                                             initial_value=initial_position,
                                             current_value=next_position,
                                             random_state=random_state)
          next_quaternion = variation.evaluate(self._quaternion,
                                               initial_value=initial_quaternion,
                                               current_value=next_quaternion,
                                               random_state=random_state)
          prop.set_pose(physics, next_position, next_quaternion)
          try:
            # If this pose results in collisions then there's a chance we'll
            # encounter a PhysicsError error here due to a full contact buffer,
            # in which case reject this pose and sample another.
            physics.forward()
          except control.PhysicsError:
            continue

          if (self._ignore_collisions
              or not self._has_collisions_with_prop(physics, prop)):
            success = True
            break

        if not success:
          raise RuntimeError(_REJECTION_SAMPLING_FAILED.format(
              model_name=prop.mjcf_model.model,
              max_attempts=self._max_attempts_per_prop))

      for prop in ignore_contacts_with_entities:
        self._restore_contact_parameters(physics, prop, cached_contact_params)

    # Place the props and settle the physics. If settling was requested and it
    # it fails, re-place the props.
    def place_and_settle():
      for _ in range(self._max_settle_physics_attempts):
        place_props()

        # Step physics and check prop states.
        original_time = physics.data.time
        props_isolator = utils.JointStaticIsolator(physics, self._prop_joints)
        prop_joints_mj = physics.bind(self._prop_joints)
        while physics.data.time - original_time < self._max_settle_physics_time:
          with props_isolator:
            physics.step()
          max_qvel = np.max(np.abs(prop_joints_mj.qvel))
          max_qacc = np.max(np.abs(prop_joints_mj.qacc))
          if (max_qvel < _SETTLE_QVEL_TOL and max_qacc < _SETTLE_QACC_TOL):
            return True
        physics.data.time = original_time

      if self._raise_exception_on_settle_failure:
        raise RuntimeError(
            _SETTLING_PHYSICS_FAILED.format(
                max_attempts=self._max_settle_physics_attempts,
                max_time=self._max_settle_physics_time,
                max_qvel=max_qvel,
                max_qacc=max_qacc,
            ))
      else:
        log_str = _SETTLING_PHYSICS_FAILED.format(
            max_attempts='%s',
            max_time='%s',
            max_qvel='%s',
            max_qacc='%s',
        )
        logging.warning(log_str, self._max_settle_physics_attempts,
                        self._max_settle_physics_time, max_qvel, max_qacc)

      return False

    if self._settle_physics:
      place_and_settle()
    else:
      place_props()
def __call__ ( self , physics , random_state , ignore_contacts_with_entities = None ) : if ignore_contacts_with_entities is None : ignore_contacts_with_entities = [ ] # Temporarily disable contacts for all geoms that belong to props which # haven't yet been placed in order to free up space in the contact buffer. cached_contact_params = self . _disable_and_cache_contact_parameters ( physics , self . _props + ignore_contacts_with_entities ) try : physics . forward ( ) except control . PhysicsError as cause : effect = control . PhysicsError ( 'Despite disabling contact for all props in this initializer, ' '`physics.forward()` resulted in a `PhysicsError`' ) raise effect from cause def place_props ( ) : for prop in self . _props : # Restore the original contact parameters for all geoms in the prop # we're about to place, so that we can detect if the new pose results in # collisions. self . _restore_contact_parameters ( physics , prop , cached_contact_params ) success = False initial_position , initial_quaternion = prop . get_pose ( physics ) next_position , next_quaternion = initial_position , initial_quaternion for _ in range ( self . _max_attempts_per_prop ) : next_position = variation . evaluate ( self . _position , initial_value = initial_position , current_value = next_position , random_state = random_state ) next_quaternion = variation . evaluate ( self . _quaternion , initial_value = initial_quaternion , current_value = next_quaternion , random_state = random_state ) prop . set_pose ( physics , next_position , next_quaternion ) try : # If this pose results in collisions then there's a chance we'll # encounter a PhysicsError error here due to a full contact buffer, # in which case reject this pose and sample another. physics . forward ( ) except control . PhysicsError : continue if ( self . _ignore_collisions or not self . _has_collisions_with_prop ( physics , prop ) ) : success = True break if not success : raise RuntimeError ( _REJECTION_SAMPLING_FAILED . format ( model_name = prop . mjcf_model . model , max_attempts = self . _max_attempts_per_prop ) ) for prop in ignore_contacts_with_entities : self . _restore_contact_parameters ( physics , prop , cached_contact_params ) # Place the props and settle the physics. If settling was requested and it # it fails, re-place the props. def place_and_settle ( ) : for _ in range ( self . _max_settle_physics_attempts ) : place_props ( ) # Step physics and check prop states. original_time = physics . data . time props_isolator = utils . JointStaticIsolator ( physics , self . _prop_joints ) prop_joints_mj = physics . bind ( self . _prop_joints ) while physics . data . time - original_time < self . _max_settle_physics_time : with props_isolator : physics . step ( ) max_qvel = np . max ( np . abs ( prop_joints_mj . qvel ) ) max_qacc = np . max ( np . abs ( prop_joints_mj . qacc ) ) if ( max_qvel < _SETTLE_QVEL_TOL and max_qacc < _SETTLE_QACC_TOL ) : return True physics . data . time = original_time if self . _raise_exception_on_settle_failure : raise RuntimeError ( _SETTLING_PHYSICS_FAILED . format ( max_attempts = self . _max_settle_physics_attempts , max_time = self . _max_settle_physics_time , max_qvel = max_qvel , max_qacc = max_qacc , ) ) else : log_str = _SETTLING_PHYSICS_FAILED . format ( max_attempts = '%s' , max_time = '%s' , max_qvel = '%s' , max_qacc = '%s' , ) logging . warning ( log_str , self . _max_settle_physics_attempts , self . _max_settle_physics_time , max_qvel , max_qacc ) return False if self . _settle_physics : place_and_settle ( ) else : place_props ( )