def test_multiple_joints_of_same_type(self): physics = engine.Physics.from_xml_string("""<mujoco> <worldbody> <body> <geom type="box" size="1 1 1"/> <joint name="hinge_1" type="hinge"/> <joint name="hinge_2" type="hinge"/> <joint name="hinge_3" type="hinge"/> </body> </worldbody> </mujoco>""") randomizers.randomize_limited_and_rotational_joints(physics, self.rand) self.assertNotEqual(0., physics.named.data.qpos['hinge_1']) self.assertNotEqual(0., physics.named.data.qpos['hinge_2']) self.assertNotEqual(0., physics.named.data.qpos['hinge_3']) self.assertNotEqual(physics.named.data.qpos['hinge_1'], physics.named.data.qpos['hinge_2']) self.assertNotEqual(physics.named.data.qpos['hinge_2'], physics.named.data.qpos['hinge_3']) self.assertNotEqual(physics.named.data.qpos['hinge_1'], physics.named.data.qpos['hinge_3'])
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode. Initializes the swimmer orientation to [-pi, pi) and the relative joint angle of each joint uniformly within its range. Args: physics: An instance of `Physics`. """ # Random joint angles: randomizers.randomize_limited_and_rotational_joints( physics, self.random) # Random target position. close_target = self.random.rand( ) < .2 # Probability of a close target. target_box = .3 if close_target else 2 xpos, ypos = self.random.uniform(-target_box, target_box, size=2) physics.named.model.geom_pos['target', 'x'] = xpos #physics.named.model.body_pos['target_body', 'x'] = xpos #physics.named.data.geom_xpos['target', 'x'] = xpos physics.named.model.geom_pos['target', 'y'] = ypos #physics.named.model.body_pos['target_body', 'y'] = ypos #physics.named.data.geom_xpos['target', 'y'] = ypos physics.named.model.light_pos['target_light', 'x'] = xpos physics.named.model.light_pos['target_light', 'y'] = ypos super(Swimmer, self).initialize_episode(physics)
def initialize_episode(self, physics, difficulty=None): """Sets the state of the environment at the start of each episode.""" # Sometime don't reset if self.dontreset: return # Reset based on difficulty if self.difficulty is None: randomizers.randomize_limited_and_rotational_joints( physics, self.random) elif self.difficulty == 'e': physics.data.qpos[0] = self.random.uniform(0.15, 0.27) elif self.difficulty == 'm': physics.data.qpos[0] = self.random.uniform(-0.15, 0.15) elif self.difficulty == 'h': physics.data.qpos[0] = self.random.uniform(-0.27, -0.15) physics.data.qpos[1] = self.random.uniform(-0.27, 0.27) # Randomize wal positions w1 = self.random.uniform(-0.2, 0.2) w2 = self.random.uniform(-0.2, 0.2) physics.named.model.geom_pos['wall1A', 'y'] = 0.25 + w1 physics.named.model.geom_pos['wall1B', 'y'] = -0.25 + w1 physics.named.model.geom_pos['wall2A', 'y'] = 0.25 + w2 physics.named.model.geom_pos['wall2B', 'y'] = -0.25 + w2 # Randomize target position physics.named.model.geom_pos['target', 'x'] = self.random.uniform(0.2, 0.28) physics.named.model.geom_pos['target', 'y'] = self.random.uniform(-0.28, 0.28)
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode. If _randomize_gains is True, the relationship between the controls and the joints is randomized, so that each control actuates a random linear combination of joints. Args: physics: An instance of `mujoco.Physics`. """ physics.named.model.geom_pos['target', 'x'] = self._goal[0] physics.named.model.geom_pos['target', 'y'] = self._goal[1] physics.named.model.geom_pos['pointmass', 'x'] = 0 physics.named.model.geom_pos['pointmass', 'y'] = 0 if self._randomize_joints: randomizers.randomize_limited_and_rotational_joints( physics, self.random) if self._randomize_gains: dir1 = self.random.randn(2) dir1 /= np.linalg.norm(dir1) # Find another actuation direction that is not 'too parallel' to dir1. parallel = True while parallel: dir2 = self.random.randn(2) dir2 /= np.linalg.norm(dir2) parallel = abs(np.dot(dir1, dir2)) > 0.9 physics.model.wrap_prm[[0, 1]] = dir1 physics.model.wrap_prm[[2, 3]] = dir2
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode.""" physics.named.model.geom_size['target', 0] = self._target_size randomizers.randomize_limited_and_rotational_joints(physics, self.random) # randomize target position angle = self.random.uniform(0, 2 * np.pi) radius = self.random.uniform(.05, .20) physics.named.model.geom_pos['target', 'x'] = radius * np.sin(angle) physics.named.model.geom_pos['target', 'y'] = radius * np.cos(angle)
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode. In 'standing' mode, use initial orientation and small velocities. In 'random' mode, randomize joint angles and let fall to the floor. Args: physics: An instance of `Physics`. """ randomizers.randomize_limited_and_rotational_joints( physics, self.random) super(PlanarWalker, self).initialize_episode(physics)
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode.""" physics.named.model.geom_size["target", 0] = self._target_size randomizers.randomize_limited_and_rotational_joints(physics, self.random) # Randomize target position angle = self.random.uniform(0, 2 * np.pi) radius = self.random.uniform(0.05, 0.20) physics.named.model.geom_pos["target", "x"] = radius * np.sin(angle) physics.named.model.geom_pos["target", "y"] = radius * np.cos(angle) super(Reacher, self).initialize_episode(physics)
def _set_random_joint_angles(physics, random, max_attempts=1000): """Sets the joints to a random collision-free state.""" for _ in range(max_attempts): randomizers.randomize_limited_and_rotational_joints(physics, random) # Check for collisions. physics.after_reset() if physics.data.ncon == 0: break else: raise RuntimeError('Could not find a collision-free state ' 'after {} attempts'.format(max_attempts))
def test_unlimited_hinge_randomization_range(self): physics = mujoco.Physics.from_xml_string("""<mujoco> <worldbody> <body> <geom type="box" size="1 1 1"/> <joint name="hinge" type="hinge"/> </body> </worldbody> </mujoco>""") for _ in range(10): randomizers.randomize_limited_and_rotational_joints(physics, self.rand) self.assertBetween(physics.named.data.qpos['hinge'], -np.pi, np.pi)
def initialize_episode(self, physics): """Sets a random collision-free configuration at the start of each episode. Args: physics: An instance of `Physics`. """ penetrating = True while penetrating: randomizers.randomize_limited_and_rotational_joints( physics, self.random) # Check for collisions. physics.after_reset() penetrating = physics.data.ncon > 0
def initialize_episode(self, physics, task_params): """Sets the state of the environment at the start of each episode.""" physics.named.model.geom_size['target', 0] = _TARGET_SIZE randomizers.randomize_limited_and_rotational_joints( physics, self.random) # randomize target position x, y = task_params['x'], task_params['y'] physics.named.model.geom_pos['target', 'x'] = x physics.named.model.geom_pos['target', 'y'] = y self.prev_dist = physics.finger_to_target_dist() self.shaping_rew = 0.0
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode. Args: physics: An instance of `Physics`. """ # Find a collision-free random initial configuration. penetrating = True while penetrating: randomizers.randomize_limited_and_rotational_joints( physics, self.random) # Check for collisions. physics.after_reset() penetrating = physics.data.ncon > 0 super(Humanoid, self).initialize_episode(physics)
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode. In 'standing' mode, use initial orientation and small velocities. In 'random' mode, randomize joint angles and let fall to the floor. Args: physics: An instance of `Physics`. """ # Find a collision-free random initial configuration. penetrating = True while penetrating: randomizers.randomize_limited_and_rotational_joints( physics, self.random) # Check for collisions. physics.after_reset() penetrating = physics.data.ncon > 0
def test_single_joint_of_each_type(self): physics = engine.Physics.from_xml_string("""<mujoco> <default> <joint range="0 90" /> </default> <worldbody> <body> <geom type="box" size="1 1 1"/> <joint name="free" type="free"/> </body> <body> <geom type="box" size="1 1 1"/> <joint name="limited_hinge" type="hinge" limited="true"/> <joint name="slide" type="slide"/> <joint name="limited_slide" type="slide" limited="true"/> <joint name="hinge" type="hinge"/> </body> <body> <geom type="box" size="1 1 1"/> <joint name="ball" type="ball"/> </body> <body> <geom type="box" size="1 1 1"/> <joint name="limited_ball" type="ball" limited="true"/> </body> </worldbody> </mujoco>""") randomizers.randomize_limited_and_rotational_joints(physics, self.rand) self.assertNotEqual(0., physics.named.data.qpos['hinge']) self.assertNotEqual(0., physics.named.data.qpos['limited_hinge']) self.assertNotEqual(0., physics.named.data.qpos['limited_slide']) self.assertNotEqual(0., np.sum(physics.named.data.qpos['ball'])) self.assertNotEqual(0., np.sum(physics.named.data.qpos['limited_ball'])) self.assertNotEqual(0., np.sum(physics.named.data.qpos['free'][3:])) # Unlimited slide and the positional part of the free joint remains # uninitialized. self.assertEqual(0., physics.named.data.qpos['slide']) self.assertEqual(0., np.sum(physics.named.data.qpos['free'][:3]))
def test_limited_1d_joint_limits_are_respected(self): physics = mujoco.Physics.from_xml_string("""<mujoco> <default> <joint limited="true"/> </default> <worldbody> <body> <geom type="box" size="1 1 1"/> <joint name="hinge" type="hinge" range="0 10"/> <joint name="slide" type="slide" range="30 50"/> </body> </worldbody> </mujoco>""") for _ in range(10): randomizers.randomize_limited_and_rotational_joints(physics, self.rand) self.assertBetween(physics.named.data.qpos['hinge'], np.deg2rad(0), np.deg2rad(10)) self.assertBetween(physics.named.data.qpos['slide'], 30, 50)
def test_limited_ball_joint_are_respected(self): physics = mujoco.Physics.from_xml_string("""<mujoco> <worldbody> <body name="body" zaxis="1 0 0"> <geom type="box" size="1 1 1"/> <joint name="ball" type="ball" limited="true" range="0 60"/> </body> </worldbody> </mujoco>""") body_axis = np.array([1., 0., 0.]) joint_axis = np.zeros(3) for _ in range(10): randomizers.randomize_limited_and_rotational_joints(physics, self.rand) quat = physics.named.data.qpos['ball'] mjlib.mju_rotVecQuat(joint_axis, body_axis, quat) angle_cos = np.dot(body_axis, joint_axis) self.assertGreater(angle_cos, 0.5) # cos(60) = 0.5
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode.""" physics.named.model.geom_size['target', 0] = self._target_size randomizers.randomize_limited_and_rotational_joints(physics, self.random) # Randomize target position # angle = self.random.uniform(0, 2 * np.pi) # radius = self.random.uniform(.05, .20) angle = 1.75 * np.pi radius = 0.15 physics.named.model.geom_pos['target', 'x'] = radius * np.sin(angle) physics.named.model.geom_pos['target', 'y'] = radius * np.cos(angle) bdry = 0.1 physics.named.data.qpos['dis1x'] = np.random.uniform(-bdry, bdry) physics.named.data.qpos['dis1y'] = np.random.uniform(-bdry, bdry) physics.named.data.qpos['dis2x'] = np.random.uniform(-bdry, bdry) physics.named.data.qpos['dis2y'] = np.random.uniform(-bdry, bdry) super(Reacher, self).initialize_episode(physics)
def initialize_episode(self, physics): """Sets the state of the environment at the start of each episode.""" randomizers.randomize_limited_and_rotational_joints( physics, self.random) self._timeout_progress = 0 super(Hopper, self).initialize_episode(physics)