コード例 #1
0
    def test_find_element(self):
        xml_str = """
    <root>
      <option name='option_name'>
        <content></content>
      </option>
      <world name='world_name'>
        <geom name='geom_name'/>
      </world>
    </root>"""
        tree = xml_tools.parse(six.StringIO(xml_str))
        world = xml_tools.find_element(root=tree,
                                       tag='world',
                                       name='world_name')
        self.assertEqual(world.tag, 'world')
        self.assertEqual(world.attrib['name'], 'world_name')

        geom = xml_tools.find_element(root=tree, tag='geom', name='geom_name')
        self.assertEqual(geom.tag, 'geom')
        self.assertEqual(geom.attrib['name'], 'geom_name')

        with self.assertRaisesRegexp(ValueError, 'Element with tag'):
            xml_tools.find_element(root=tree,
                                   tag='does_not_exist',
                                   name='name')

        with self.assertRaisesRegexp(ValueError, 'Element with tag'):
            xml_tools.find_element(root=tree,
                                   tag='world',
                                   name='does_not_exist')
コード例 #2
0
 def test_tostring(self):
   xml_str = """
   <root>
     <head>
       <content></content>
     </head>
   </root>"""
   tree = xml_tools.parse(io.StringIO(xml_str))
   self.assertEqual(b'<root>\n  <head>\n    <content/>\n  </head>\n</root>\n',
                    etree.tostring(tree, pretty_print=True))
コード例 #3
0
ファイル: lqr.py プロジェクト: tsinghua-rll/dm_control
def _make_model(n_bodies,
                n_actuators,
                random,
                stiffness_range=(15, 25),
                damping_range=(0, 0)):
    """Returns an MJCF XML string defining a model of springs and dampers.

  Args:
    n_bodies: An integer, the number of bodies (DoFs) in the system.
    n_actuators: An integer, the number of actuated bodies.
    random: A `numpy.random.RandomState` instance.
    stiffness_range: A tuple containing minimum and maximum stiffness. Each
      joint's stiffness is sampled uniformly from this interval.
    damping_range: A tuple containing minimum and maximum damping. Each joint's
      damping is sampled uniformly from this interval.

  Returns:
    An MJCF string describing the linear system.

  Raises:
    ValueError: If the number of bodies or actuators is erronous.
  """
    if n_bodies < 1 or n_actuators < 1:
        raise ValueError('At least 1 body and 1 actuator required.')
    if n_actuators > n_bodies:
        raise ValueError('At most 1 actuator per body.')

    file_path = os.path.join(os.path.dirname(__file__), 'lqr.xml')
    with resources.GetResourceAsFile(file_path) as xml_file:
        mjcf = xml_tools.parse(xml_file)
    parent = mjcf.find('./worldbody')
    actuator = etree.SubElement(mjcf.getroot(), 'actuator')
    tendon = etree.SubElement(mjcf.getroot(), 'tendon')

    for body in range(n_bodies):
        # Inserting body.
        child = _make_body(body, stiffness_range, damping_range, random)
        site_name = 'site_{}'.format(body)
        child.append(etree.Element('site', name=site_name))

        if body == 0:
            child.set('pos', '.25 0 .1')
        # Add actuators to the first n_actuators bodies.
        if body < n_actuators:
            # Adding actuator.
            joint_name = 'joint_{}'.format(body)
            motor_name = 'motor_{}'.format(body)
            child.find('joint').set('name', joint_name)
            actuator.append(
                etree.Element('motor', name=motor_name, joint=joint_name))

        # Add a tendon between consecutive bodies (for visualisation purposes only).
        if body < n_bodies - 1:
            child_site_name = 'site_{}'.format(body + 1)
            tendon_name = 'tendon_{}'.format(body)
            spatial = etree.SubElement(tendon, 'spatial', name=tendon_name)
            spatial.append(etree.Element('site', site=site_name))
            spatial.append(etree.Element('site', site=child_site_name))
        parent.append(child)
        parent = child

    return etree.tostring(mjcf, pretty_print=True)