def test_register_happy(self): of = obs.ObservationFactory(self.client) with self.subTest('single obs'): test_obs = CompliantObs(None) of.register_observation(test_obs) self.assertEqual(len(of._observations), 1) self.assertEqual(of._observations[0], test_obs) self.assertEqual(of.get_observation_space(), CompliantObs.observation_space) with self.subTest('multiple_obs'): test_obs2 = CompliantObs(2) of.register_observation(test_obs2) self.assertEqual(len(of._observations), 2) self.assertNotEqual(of._observations[0], test_obs2) self.assertEqual(of._observations[1], test_obs2) with self.subTest('Cached observation space'): self.assertEqual(of.get_observation_space(), spaces.Box(low=0, high=3, shape=(2,))) with self.subTest('Fresh observation space'): self.assertEqual(of.get_observation_space(generate=True), spaces.Box(low=0, high=3, shape=(4,)))
def test_register_mismatch(self): of = obs.ObservationFactory(self.client) test_obs = CompliantObs(None) with self.subTest('obs_space mismatch'): test_obs.labels = ['1', '2', '3'] with self.assertRaises(ValueError): of.register_observation(test_obs) with self.subTest('observation mismatch'): test_obs.observation_space = spaces.Box(low=0, high=3, shape=(3,)) with self.assertRaises(ValueError): of.register_observation(test_obs)
def test_get_obs_multiple_observations(self): of = obs.ObservationFactory(self.client) of.register_observation(CompliantObs(None)) test_obs = CompliantObs(None) test_obs.compute = lambda: np.array([5, 6]) test_obs.labels = ['5', '6'] of.register_observation(test_obs) observations, labels = of.get_obs() np.testing.assert_array_equal(observations, np.array([1, 2, 5, 6])) self.assertListEqual(labels, ['1', '2', '5', '6'])
def test_get_observation_space_normalized(self): of = obs.ObservationFactory(self.client, normalize=True) of.register_observation(CompliantObs(None)) test_obs = CompliantObs(None) test_obs.observation_space = spaces.Box(low=np.array([5., 6.]), high=np.array([5., 6.])) of.register_observation(test_obs) self.assertEqual( of.get_observation_space(), spaces.Box(low=np.array([-1., -1., -1., -1.]), high=np.array([1., 1., 1., 1.])))
def test_actions(self): no_op = np.zeros(self.env.action_space.shape[0]) self.env.obs_factory.register_observation(CompliantObs(None)) self.env.termination_factory.register_termination( DummyTermination(0, True)) # Let the robot stabilize first for i in range(1000): self.env.step(no_op) position, orientation = p.getBasePositionAndOrientation(self.env.robot) with self.subTest('no action'): for i in range(10): self.env.step(no_op) new_pos, new_or = p.getBasePositionAndOrientation(self.env.robot) np.testing.assert_array_almost_equal(position, new_pos) np.testing.assert_array_almost_equal(orientation, new_or) with self.subTest('with action'): action = np.array([5.] * self.env.action_space.shape[0]) for i in range(10): self.env.step(action) new_pos, new_or = p.getBasePositionAndOrientation(self.env.robot) self.assert_array_not_almost_equal(position, new_pos) self.assert_array_not_almost_equal(orientation, new_or)
def test_step_simple_reward(self): self.env.obs_factory.register_observation(CompliantObs(None)) self.env.termination_factory.register_termination( DummyTermination(0, True)) obs, reward, done, info = self.env.step(self.env.action_space.sample()) self.assertEqual(reward, 1)
def test_action_normalization(self): joint_cnt = 12 config = solo_env.Solo8VanillaConfig() config.max_motor_rotation = 10 env = solo_env.Solo8VanillaEnv(config=config, normalize_actions=True) env.obs_factory.register_observation(CompliantObs(None)) env.termination_factory.register_termination(DummyTermination(0, True)) env.reward_factory.register_reward(1, SimpleReward()) mock_client = mock.MagicMock() env.client = mock_client with self.subTest('min bounds'): env.step([-1.] * joint_cnt) _, kwargs = mock_client.setJointMotorControlArray.call_args_list[-1] np.testing.assert_array_equal( kwargs['targetPositions'], np.array([-config.max_motor_rotation] * joint_cnt)) with self.subTest('max bounds'): env.step([1.] * joint_cnt) _, kwargs = mock_client.setJointMotorControlArray.call_args_list[-1] np.testing.assert_array_equal( kwargs['targetPositions'], np.array([config.max_motor_rotation] * joint_cnt)) with self.subTest('at 0'): env.step([0.] * joint_cnt) _, kwargs = mock_client.setJointMotorControlArray.call_args_list[-1] np.testing.assert_array_equal( kwargs['targetPositions'], np.array([0] * joint_cnt))
def test_get_obs_single_observation(self): of = obs.ObservationFactory(self.client) of.register_observation(CompliantObs(None)) observations, labels = of.get_obs() np.testing.assert_array_equal(observations, np.array([1, 2])) self.assertListEqual(labels, ['1', '2'])
def test_get_observation_space_multiple_observations(self): of = obs.ObservationFactory(self.client) of.register_observation(CompliantObs(None)) of.get_observation_space() test_obs = CompliantObs(None) test_obs.observation_space = spaces.Box(low=np.array([5., 6.]), high=np.array([5., 6.])) of.register_observation(test_obs) with self.subTest('from cache'): self.assertEqual(of.get_observation_space(), CompliantObs.observation_space) with self.subTest('regenerate cache'): self.assertEqual(of.get_observation_space(generate=True), spaces.Box(low=np.array([0., 0., 5., 6.]), high=np.array([3., 3., 5., 6.])))
def test_realtime(self, mock_time): env = solo_env.Solo8VanillaEnv(config=solo_env.Solo8VanillaConfig(), realtime=True) env.reward_factory.register_reward(1, SimpleReward()) env.obs_factory.register_observation(CompliantObs(None)) env.termination_factory.register_termination(DummyTermination(0, True)) env.step(env.action_space.sample()) self.assertTrue(mock_time.called)
def test_get_obs_normalized(self): of = obs.ObservationFactory(self.client, normalize=True) test_obs1 = CompliantObs(None) test_obs1.compute = lambda: np.array([1.5, 1.5]) of.register_observation(test_obs1) test_obs2 = CompliantObs(None) test_obs2.compute = lambda: np.array([0., 0.]) of.register_observation(test_obs2) test_obs3 = CompliantObs(None) test_obs3.compute = lambda: np.array([3., 3.]) of.register_observation(test_obs3) observations, _ = of.get_obs() np.testing.assert_array_equal(observations, np.array([0., 0., -1., -1., 1., 1.]))
def test_reset(self): self.env.obs_factory.register_observation(CompliantObs(None)) self.env.termination_factory.register_termination(DummyTermination(0, True)) base_pos, base_or = p.getBasePositionAndOrientation(self.env.robot) action = np.array([5.] * self.env.action_space.shape[0]) for _ in range(100): self.env.step(action) self.assertEqual( self.env.termination_factory._terminations[0].reset_counter, 1) new_pos, new_or = p.getBasePositionAndOrientation(self.env.robot) self.assert_array_not_almost_equal(base_pos, new_pos) self.assert_array_not_almost_equal(base_or, new_or) self.env.reset() self.assertEqual( self.env.termination_factory._terminations[0].reset_counter, 2) new_pos, new_or = p.getBasePositionAndOrientation(self.env.robot) np.testing.assert_array_almost_equal(base_pos, new_pos) np.testing.assert_array_almost_equal(base_or, new_or)