def test_spec_from_gym_space_dtype_map(self): tuple_space = gym.spaces.Tuple(( gym.spaces.Discrete(2), gym.spaces.Box(0, 1, (3, 4)), gym.spaces.Tuple((gym.spaces.Discrete(2), gym.spaces.Discrete(3))), gym.spaces.Dict({ 'spec_1': gym.spaces.Discrete(2), 'spec_2': gym.spaces.Tuple(( gym.spaces.Discrete(2), gym.spaces.Box(0, 1, (3, 4)), )), }), )) dtype_map = {gym.spaces.Discrete: np.uint8, gym.spaces.Box: np.uint16} spec = gym_wrapper._spec_from_gym_space(tuple_space, dtype_map=dtype_map) self.assertEqual(np.uint8, spec[0].dtype) self.assertEqual(np.uint16, spec[1].dtype) self.assertEqual(np.uint8, spec[2][0].dtype) self.assertEqual(np.uint8, spec[2][1].dtype) self.assertEqual(np.uint8, spec[3]['spec_1'].dtype) self.assertEqual(np.uint8, spec[3]['spec_2'][0].dtype) self.assertEqual(np.uint16, spec[3]['spec_2'][1].dtype)
def test_spec_from_gym_space_dtype_map(self): class Box(gym.spaces.Box): """Box space without the dtype property.""" def __init__(self, *args, **kwargs): super(Box, self).__init__(*args, **kwargs) del self.dtype tuple_space = gym.spaces.Tuple(( gym.spaces.Discrete(2), Box(0, 1, (3, 4)), gym.spaces.Tuple((gym.spaces.Discrete(2), gym.spaces.Discrete(3))), gym.spaces.Dict({ 'spec_1': gym.spaces.Discrete(2), 'spec_2': gym.spaces.Tuple(( gym.spaces.Discrete(2), Box(0, 1, (3, 4)), )), }), )) dtype_map = {gym.spaces.Discrete: np.uint8, gym.spaces.Box: np.uint16} spec = gym_wrapper._spec_from_gym_space(tuple_space, dtype_map=dtype_map) self.assertEqual(np.uint8, spec[0].dtype) self.assertEqual(np.uint16, spec[1].dtype) self.assertEqual(np.uint8, spec[2][0].dtype) self.assertEqual(np.uint8, spec[2][1].dtype) self.assertEqual(np.uint8, spec[3]['spec_1'].dtype) self.assertEqual(np.uint8, spec[3]['spec_2'][0].dtype) self.assertEqual(np.uint16, spec[3]['spec_2'][1].dtype)
def test_spec_from_gym_space_dict(self): dict_space = gym.spaces.Dict({ 'spec_1': gym.spaces.Discrete(2), 'spec_2': gym.spaces.Box(-1.0, 1.0, (3, 4)), }) spec = gym_wrapper._spec_from_gym_space(dict_space) self.assertEqual(2, len(spec)) self.assertEqual((), spec['spec_1'].shape) self.assertEqual(np.int64, spec['spec_1'].dtype) self.assertEqual(0, spec['spec_1'].minimum) self.assertEqual(1, spec['spec_1'].maximum) self.assertEqual((3, 4), spec['spec_2'].shape) self.assertEqual(np.float32, spec['spec_2'].dtype) np.testing.assert_array_almost_equal( -np.ones((3, 4)), spec['spec_2'].minimum, ) np.testing.assert_array_almost_equal( np.ones((3, 4)), spec['spec_2'].maximum, )
def test_spec_from_gym_space_box_scalars(self): box_space = gym.spaces.Box(-1.0, 1.0, (3, 4)) spec = gym_wrapper._spec_from_gym_space(box_space) self.assertEqual((3, 4), spec.shape) self.assertEqual(np.float32, spec.dtype) np.testing.assert_array_almost_equal(-np.ones((3, 4)), spec.minimum) np.testing.assert_array_almost_equal(np.ones((3, 4)), spec.maximum)
def test_spec_from_gym_space_discrete(self): discrete_space = gym.spaces.Discrete(3) spec = gym_wrapper._spec_from_gym_space(discrete_space) self.assertEqual((), spec.shape) self.assertEqual(np.int64, spec.dtype) self.assertEqual(0, spec.minimum) self.assertEqual(2, spec.maximum)
def test_spec_from_gym_space_box_scalars_simplify_bounds(self): box_space = gym.spaces.Box(-1.0, 1.0, (3, 4)) spec = gym_wrapper._spec_from_gym_space(box_space, simplify_box_bounds=True) self.assertEqual((3, 4), spec.shape) self.assertEqual(np.float32, spec.dtype) np.testing.assert_array_equal(np.array([-1], dtype=np.int), spec.minimum) np.testing.assert_array_equal(np.array([1], dtype=np.int), spec.maximum)
def test_spec_from_gym_space_multi_binary(self): multi_binary_space = gym.spaces.MultiBinary(4) spec = gym_wrapper._spec_from_gym_space(multi_binary_space) self.assertEqual((4,), spec.shape) self.assertEqual(np.int8, spec.dtype) np.testing.assert_array_equal(np.array([0], dtype=np.int), spec.minimum) np.testing.assert_array_equal(np.array([1], dtype=np.int), spec.maximum)
def test_spec_from_gym_space_box_array(self): box_space = gym.spaces.Box(np.array([-1.0, -2.0]), np.array([2.0, 4.0])) spec = gym_wrapper._spec_from_gym_space(box_space) self.assertEqual((2,), spec.shape) self.assertEqual(np.float32, spec.dtype) np.testing.assert_array_almost_equal(np.array([-1.0, -2.0]), spec.minimum) np.testing.assert_array_almost_equal(np.array([2.0, 4.0]), spec.maximum)
def test_spec_from_gym_space_tuple_mixed(self): tuple_space = gym.spaces.Tuple(( gym.spaces.Discrete(2), gym.spaces.Box(-1.0, 1.0, (3, 4)), gym.spaces.Tuple((gym.spaces.Discrete(2), gym.spaces.Discrete(3))), gym.spaces.Dict({ 'spec_1': gym.spaces.Discrete(2), 'spec_2': gym.spaces.Tuple( (gym.spaces.Discrete(2), gym.spaces.Discrete(3))), }), )) spec = gym_wrapper._spec_from_gym_space(tuple_space) self.assertEqual(4, len(spec)) # Test Discrete self.assertEqual((), spec[0].shape) self.assertEqual(np.int64, spec[0].dtype) self.assertEqual(0, spec[0].minimum) self.assertEqual(1, spec[0].maximum) # Test Box self.assertEqual((3, 4), spec[1].shape) self.assertEqual(np.float32, spec[1].dtype) np.testing.assert_array_almost_equal(-np.ones((3, 4)), spec[1].minimum) np.testing.assert_array_almost_equal(np.ones((3, 4)), spec[1].maximum) # Test Tuple self.assertEqual(2, len(spec[2])) self.assertEqual((), spec[2][0].shape) self.assertEqual(np.int64, spec[2][0].dtype) self.assertEqual(0, spec[2][0].minimum) self.assertEqual(1, spec[2][0].maximum) self.assertEqual((), spec[2][1].shape) self.assertEqual(np.int64, spec[2][1].dtype) self.assertEqual(0, spec[2][1].minimum) self.assertEqual(2, spec[2][1].maximum) # Test Dict # Test Discrete in Dict discrete_in_dict = spec[3]['spec_1'] self.assertEqual((), discrete_in_dict.shape) self.assertEqual(np.int64, discrete_in_dict.dtype) self.assertEqual(0, discrete_in_dict.minimum) self.assertEqual(1, discrete_in_dict.maximum) # Test Tuple in Dict tuple_in_dict = spec[3]['spec_2'] self.assertEqual(2, len(tuple_in_dict)) self.assertEqual((), tuple_in_dict[0].shape) self.assertEqual(np.int64, tuple_in_dict[0].dtype) self.assertEqual(0, tuple_in_dict[0].minimum) self.assertEqual(1, tuple_in_dict[0].maximum) self.assertEqual((), tuple_in_dict[1].shape) self.assertEqual(np.int64, tuple_in_dict[1].dtype) self.assertEqual(0, tuple_in_dict[1].minimum) self.assertEqual(2, tuple_in_dict[1].maximum)
def test_spec_from_gym_space_multi_discrete(self): multi_discrete_space = gym.spaces.MultiDiscrete([1, 2, 3, 4]) spec = gym_wrapper._spec_from_gym_space(multi_discrete_space) self.assertEqual((4,), spec.shape) self.assertEqual(np.int32, spec.dtype) np.testing.assert_array_equal(np.array([0], dtype=np.int), spec.minimum) np.testing.assert_array_equal( np.array([0, 1, 2, 3], dtype=np.int), spec.maximum)
def test_spec_name_nested(self): dict_space = gym.spaces.Tuple((gym.spaces.Dict({ 'spec_0': gym.spaces.Dict({ 'spec_1': gym.spaces.Discrete(2), 'spec_2': gym.spaces.Discrete(2), }), }), gym.spaces.Discrete(2))) spec = gym_wrapper._spec_from_gym_space(dict_space, name='observation') self.assertEqual('observation/tuple_0/spec_0/spec_1', spec[0]['spec_0']['spec_1'].name) self.assertEqual('observation/tuple_0/spec_0/spec_2', spec[0]['spec_0']['spec_2'].name) self.assertEqual('observation/tuple_1', spec[1].name)
def test_spec_from_gym_space_tuple(self): tuple_space = gym.spaces.Tuple( (gym.spaces.Discrete(2), gym.spaces.Discrete(3))) spec = gym_wrapper._spec_from_gym_space(tuple_space) self.assertEqual(2, len(spec)) self.assertEqual((), spec[0].shape) self.assertEqual(np.int64, spec[0].dtype) self.assertEqual(0, spec[0].minimum) self.assertEqual(1, spec[0].maximum) self.assertEqual((), spec[1].shape) self.assertEqual(np.int64, spec[1].dtype) self.assertEqual(0, spec[1].minimum) self.assertEqual(2, spec[1].maximum)
def test_spec_from_gym_space_when_simplify_box_bounds_false(self): # testing on gym.spaces.Dict which makes recursive calls to # _spec_from_gym_space box_space = gym.spaces.Box(-1.0, 1.0, (2, )) dict_space = gym.spaces.Dict({'box1': box_space, 'box2': box_space}) spec = gym_wrapper._spec_from_gym_space(dict_space, simplify_box_bounds=False) self.assertEqual((2, ), spec['box1'].shape) self.assertEqual((2, ), spec['box2'].shape) self.assertEqual(np.float32, spec['box1'].dtype) self.assertEqual(np.float32, spec['box2'].dtype) np.testing.assert_array_equal(np.array([-1, -1], dtype=np.int), spec['box1'].minimum) np.testing.assert_array_equal(np.array([1, 1], dtype=np.int), spec['box1'].maximum) np.testing.assert_array_equal(np.array([-1, -1], dtype=np.int), spec['box2'].minimum) np.testing.assert_array_equal(np.array([1, 1], dtype=np.int), spec['box2'].maximum)
def test_spec_name(self): box_space = gym.spaces.Box( np.array([-1.0, -2.0]), np.array([2.0, 4.0]), dtype=np.float32) spec = gym_wrapper._spec_from_gym_space(box_space, name='observation') self.assertEqual('observation', spec.name)