def test_concat(self): d1 = Dict( collections.OrderedDict([('position', Box(0, 10, (2, ))), ('velocity', Box(0, 10, (3, )))])) d2 = Dict( collections.OrderedDict([('position', Box(0, 10, (2, ))), ('gravity', Box(0, 10, (3, )))])) concat_d = d1.concat(d2) assert (sorted(concat_d.spaces.keys()) == sorted( ['position', 'velocity', 'gravity']))
def test_convert_tf(self): d = Dict({'position': Discrete(2), 'velocity': Discrete(3)}) tensor_dict = d.to_tf_placeholder('test', 1) assert isinstance(tensor_dict, Dict) assert all( [isinstance(c, tf.Tensor) for c in tensor_dict.spaces.values()]) assert all([v.dtype == tf.int64 for v in tensor_dict.spaces.values()])
def test_unflatten(self): d = Dict( collections.OrderedDict([('position', Box(0, 10, (2, ))), ('velocity', Box(0, 10, (3, )))])) f = np.array([1., 2., 3., 4., 5.]) # Keys are intentionally in the "wrong" order. s = collections.OrderedDict([('velocity', np.array([3., 4., 5.])), ('position', np.array([1., 2.]))]) assert all((s[k] == v).all() for k, v in d.unflatten(f).items())
def test_flatten(self): d = Dict( collections.OrderedDict([('position', Box(0, 10, (2, ))), ('velocity', Box(0, 10, (3, )))])) f = np.array([1., 2., 3., 4., 5.]) # Keys are intentionally in the "wrong" order. s = collections.OrderedDict([('velocity', np.array([3., 4., 5.])), ('position', np.array([1., 2.]))]) assert (d.flatten(s) == f).all()
def test_convert_theano(self): d = Dict({'position': Discrete(2), 'velocity': Discrete(3)}) tensor_dict = d.to_theano_tensor('test', 1) assert isinstance(tensor_dict, Dict) assert all([ isinstance(c, theano.tensor.TensorVariable) for c in tensor_dict.spaces.values() ]) assert all( [space.dtype == 'int64' for space in tensor_dict.spaces.values()])
def test_pickleable(self): motion_dict = {'position': Discrete(2), 'velocity': Discrete(3)} sample = { 'position': 1, 'velocity': 2, } d = Dict(motion_dict) round_trip = pickle.loads(pickle.dumps(d)) assert d.contains(sample) assert round_trip assert round_trip.contains(sample)
def test_unflatten_n(self): d = Dict( collections.OrderedDict([('position', Box(0, 10, (2, ))), ('velocity', Box(0, 10, (3, )))])) f = np.array([[1., 2., 3., 4., 5.], [6., 7., 8., 9., 0.]]) # Keys are intentionally in the "wrong" order. s = [ collections.OrderedDict([('velocity', np.array([3., 4., 5.])), ('position', np.array([1., 2.]))]), collections.OrderedDict([('velocity', np.array([8., 9., 0.])), ('position', np.array([6., 7.]))]) ] for i, fi in enumerate(d.unflatten_n(f)): assert all((s[i][k] == v).all() for k, v in fi.items())
def _to_akro_space(self, space): """ Converts a gym.space into an akro.space. Args: space (gym.spaces) Returns: space (akro.spaces) """ if isinstance(space, GymBox): return Box(low=space.low, high=space.high, dtype=space.dtype) elif isinstance(space, GymDict): return Dict(space.spaces) elif isinstance(space, GymDiscrete): return Discrete(space.n) elif isinstance(space, GymTuple): return Tuple(list(map(self._to_akro_space, space.spaces))) else: raise NotImplementedError
def test_flat_dim_with_keys(self): d = Dict( collections.OrderedDict([('position', Box(0, 10, (2, ))), ('velocity', Box(0, 10, (3, )))])) assert d.flat_dim_with_keys(['position']) == 2
def test_flat_dim(self): d = Dict( collections.OrderedDict(position=Box(0, 10, (2, )), velocity=Box(0, 10, (3, )))) assert d.flat_dim == 5