Beispiel #1
0
    def testBuildIndexersForEdgeCases(self, xml_string):
        model = wrapper.MjModel.from_xml_string(xml_string)
        data = wrapper.MjData(model)

        size_to_axis_indexer = index.make_axis_indexers(model)

        index.struct_indexer(model, 'mjmodel', size_to_axis_indexer)
        index.struct_indexer(data, 'mjdata', size_to_axis_indexer)
Beispiel #2
0
    def _reload_from_model(self, model):
        """Initializes a new or existing `Physics` from a `wrapper.MjModel`.

    Creates a new `wrapper.MjData` instance, then delegates to
    `_reload_from_data`.

    Args:
      model: Instance of `wrapper.MjModel`.
    """
        data = wrapper.MjData(model)
        self._reload_from_data(data)
Beispiel #3
0
    def setUp(self):
        self._model = wrapper.MjModel.from_xml_string(MODEL)
        self._data = wrapper.MjData(self._model)

        self._size_to_axis_indexer = index.make_axis_indexers(self._model)

        self._model_indexers = index.struct_indexer(self._model, 'mjmodel',
                                                    self._size_to_axis_indexer)

        self._data_indexers = index.struct_indexer(self._data, 'mjdata',
                                                   self._size_to_axis_indexer)
Beispiel #4
0
def validate(xml_string):
    """Validates that an XML string is a valid MJCF.

  Validation is performed by constructing Mujoco model from the string.
  The construction process contains compilation and validation phases by Mujoco
  engine, the best validation tool we have access to.

  Args:
    xml_string: XML string to validate
  """

    mjmodel = wrapper.MjModel.from_xml_string(xml_string)
    wrapper.MjData(mjmodel)
Beispiel #5
0
    def copy(self, share_model=False):
        """Creates a copy of this `Physics` instance.

    Args:
      share_model: If True, the copy and the original will share a common
        MjModel instance. By default, both model and data will both be copied.

    Returns:
      A `Physics` instance.
    """
        if not share_model:
            new_model = self.model.copy()
        else:
            new_model = self.model
        new_data = wrapper.MjData(new_model)
        mjlib.mj_copyData(new_data.ptr, new_data.model.ptr, self.data.ptr)
        cls = self.__class__
        new_obj = cls.__new__(cls)
        new_obj._reload_from_data(new_data)  # pylint: disable=protected-access
        return new_obj
Beispiel #6
0
class AllFieldsTest(parameterized.TestCase):
    """Generic tests covering each FieldIndexer in model and data."""

    # NB: the class must hold references to the model and data instances or they
    # may be garbage-collected before any indexing is attempted.
    model = wrapper.MjModel.from_xml_string(MODEL)
    data = wrapper.MjData(model)

    # Iterates over ('field_name', FieldIndexer) pairs
    @parameterized.named_parameters(_iter_indexers(model, data))
    def testReadWrite_(self, field):
        # Read the contents of the FieldIndexer as a numpy array.
        old_contents = field[:]
        # Write unique values to the FieldIndexer and read them back again.
        # Don't write to non-float fields since these might contain pointers.
        if np.issubdtype(old_contents.dtype, float):
            new_contents = np.arange(old_contents.size,
                                     dtype=old_contents.dtype)
            new_contents.shape = old_contents.shape
            field[:] = new_contents
            np.testing.assert_array_equal(new_contents, field[:])
Beispiel #7
0
    def testIndexThirdOrderActuators(self, field_name, key, numeric_key):
        model = wrapper.MjModel.from_xml_string(MODEL_3RD_ORDER_ACTUATORS)
        data = wrapper.MjData(model)
        size_to_axis_indexer = index.make_axis_indexers(model)
        data_indexers = index.struct_indexer(data, 'mjdata',
                                             size_to_axis_indexer)

        indexer = getattr(data_indexers, field_name)
        field = getattr(data, field_name)

        # Explicit check that the converted key matches the numeric key.
        converted_key = indexer._convert_key(key)
        self.assertIndexExpressionEqual(numeric_key, converted_key)

        # This writes unique values to the underlying buffer to prevent false
        # negatives.
        field.flat[:] = np.arange(field.size)

        # Check that the result of named indexing matches the result of numeric
        # indexing.
        np.testing.assert_array_equal(field[numeric_key], indexer[key])
Beispiel #8
0
 def from_model(cls, model):
     """A named constructor from a `wrapper.MjModel` instance."""
     data = wrapper.MjData(model)
     return cls(data)