Exemplo n.º 1
0
    def test_tensor_spec_from_gym_space_dict(self):
        dict_space = gym.spaces.Dict([
            ('spec_2', gym.spaces.Box(-1.0, 1.0, (3, 4))),
            ('spec_1', gym.spaces.Discrete(2)),
        ])

        spec = alf_gym_wrapper.tensor_spec_from_gym_space(dict_space)

        keys = list(spec.keys())
        self.assertEqual('spec_1', keys[1])
        self.assertEqual(2, len(spec))
        self.assertEqual((), spec['spec_1'].shape)
        self.assertEqual(torch.int64, spec['spec_1'].dtype)
        self.assertEqual(0, spec['spec_1'].minimum)
        self.assertEqual(1, spec['spec_1'].maximum)

        self.assertEqual('spec_2', keys[0])
        self.assertEqual((3, 4), spec['spec_2'].shape)
        self.assertEqual(torch.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,
        )
Exemplo n.º 2
0
    def test_tensor_spec_from_gym_space_discrete(self):
        discrete_space = gym.spaces.Discrete(3)
        spec = alf_gym_wrapper.tensor_spec_from_gym_space(discrete_space)

        self.assertEqual((), spec.shape)
        self.assertEqual(torch.int64, spec.dtype)
        self.assertEqual(0, spec.minimum)
        self.assertEqual(2, spec.maximum)
Exemplo n.º 3
0
    def test_tensor_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 = alf_gym_wrapper.tensor_spec_from_gym_space(tuple_space)

        self.assertEqual(4, len(spec))
        # Test Discrete
        self.assertEqual((), spec[0].shape)
        self.assertEqual(torch.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(torch.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(torch.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(torch.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(torch.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(torch.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(torch.int64, tuple_in_dict[1].dtype)
        self.assertEqual(0, tuple_in_dict[1].minimum)
        self.assertEqual(2, tuple_in_dict[1].maximum)
Exemplo n.º 4
0
    def test_tensor_spec_from_gym_space_box_scalars(self):
        for dtype in (np.float32, np.float64):
            box_space = gym.spaces.Box(-1.0, 1.0, (3, 4), dtype=dtype)
            spec = alf_gym_wrapper.tensor_spec_from_gym_space(box_space)

            torch_dtype = getattr(torch, np.dtype(dtype).name)
            self.assertEqual((3, 4), spec.shape)
            self.assertEqual(torch_dtype, spec.dtype)
            np.testing.assert_array_equal(-np.ones((3, 4)), spec.minimum)
            np.testing.assert_array_equal(np.ones((3, 4)), spec.maximum)
Exemplo n.º 5
0
    def test_tensor_spec_from_gym_space_multi_binary(self):
        multi_binary_space = gym.spaces.MultiBinary(4)
        spec = alf_gym_wrapper.tensor_spec_from_gym_space(multi_binary_space)

        self.assertEqual((4, ), spec.shape)
        self.assertEqual(torch.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)
Exemplo n.º 6
0
    def test_tensor_spec_from_gym_space_multi_discrete(self):
        multi_discrete_space = gym.spaces.MultiDiscrete([1, 2, 3, 4])
        spec = alf_gym_wrapper.tensor_spec_from_gym_space(multi_discrete_space)

        self.assertEqual((4, ), spec.shape)
        self.assertEqual(torch.int64, 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)
Exemplo n.º 7
0
    def test_tensor_spec_from_gym_space_box_scalars_simplify_bounds(self):
        box_space = gym.spaces.Box(-1.0, 1.0, (3, 4))
        spec = alf_gym_wrapper.tensor_spec_from_gym_space(
            box_space, simplify_box_bounds=True)

        self.assertEqual((3, 4), spec.shape)
        self.assertEqual(torch.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)
Exemplo n.º 8
0
    def test_tensor_spec_from_gym_space_box_array(self):
        for dtype in (np.float32, np.float64):
            box_space = gym.spaces.Box(np.array([-1.0, -2.0]),
                                       np.array([2.0, 4.0]),
                                       dtype=dtype)
            spec = alf_gym_wrapper.tensor_spec_from_gym_space(box_space)

            torch_dtype = getattr(torch, np.dtype(dtype).name)
            self.assertEqual((2, ), spec.shape)
            self.assertEqual(torch_dtype, spec.dtype)
            np.testing.assert_array_equal(np.array([-1.0, -2.0]), spec.minimum)
            np.testing.assert_array_equal(np.array([2.0, 4.0]), spec.maximum)
Exemplo n.º 9
0
    def test_tensor_spec_from_gym_space_tuple(self):
        tuple_space = gym.spaces.Tuple(
            (gym.spaces.Discrete(2), gym.spaces.Discrete(3)))
        spec = alf_gym_wrapper.tensor_spec_from_gym_space(tuple_space)

        self.assertEqual(2, len(spec))
        self.assertEqual((), spec[0].shape)
        self.assertEqual(torch.int64, spec[0].dtype)
        self.assertEqual(0, spec[0].minimum)
        self.assertEqual(1, spec[0].maximum)

        self.assertEqual((), spec[1].shape)
        self.assertEqual(torch.int64, spec[1].dtype)
        self.assertEqual(0, spec[1].minimum)
        self.assertEqual(2, spec[1].maximum)
Exemplo n.º 10
0
    def test_tensor_spec_from_gym_space_when_simplify_box_bounds_false(self):
        # testing on gym.spaces.Dict which makes recursive calls to
        # _tensor_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 = alf_gym_wrapper.tensor_spec_from_gym_space(
            dict_space, simplify_box_bounds=False)

        self.assertEqual((2, ), spec['box1'].shape)
        self.assertEqual((2, ), spec['box2'].shape)
        self.assertEqual(torch.float32, spec['box1'].dtype)
        self.assertEqual(torch.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)