def test_unflatten_n_tuple(self): box = Box(0.0, 1.0, (3, 4)) obs = ((([1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]), ([1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]), ([1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]))) arr = box.unflatten_n(obs) assert arr.shape == (3, 3, 4)
def test_same_dtype(self): type1 = np.float32 box = Box(0, 255, (3, 4), type1) assert box.dtype == type1 type2 = np.uint8 box = Box(0, 255, (3, 4), type2) assert box.dtype == type2
def test_unflatten_n(self): box = Box(0.0, 1.0, (3, 4)) obs = np.asarray((([1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]), ([1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]), ([1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]))) arr = box.unflatten_n(obs) assert obs.shape == (3, 4, 3) assert arr.shape == (3, 3, 4)
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_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_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_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 test_concat(self): box1 = Box(0.0, 1.0, (3, 4)) box2 = Box(1.0, 2.0, (2, 3)) concat_box = box1.concat(box2) expected_shape = box1.flat_dim + box2.flat_dim assert concat_box.shape == expected_shape assert np.array_equal( concat_box.low, np.concatenate([np.zeros(box1.flat_dim), np.ones(box2.flat_dim)])) assert np.array_equal( concat_box.high, np.concatenate([np.ones(box1.flat_dim), np.full(box2.flat_dim, 2)]))
def test_pickleable(self): obj = Box(-1.0, 1.0, (3, 4)) round_trip = pickle.loads(pickle.dumps(obj)) assert round_trip assert round_trip.shape == obj.shape assert np.array_equal(round_trip.bounds[0], obj.bounds[0]) assert np.array_equal(round_trip.bounds[1], obj.bounds[1])
def test_convert_theano(self): tup = Tuple((Box(0.0, 1.0, (3, 4)), Discrete(2))) tensor_tup = tup.to_theano_tensor('test', 1) assert isinstance(tensor_tup, tuple) assert all( [isinstance(c, theano.tensor.TensorVariable) for c in tensor_tup]) assert [c.dtype for c in tensor_tup] == ['float32', 'int64']
def test_bounds(self): box = Box(0.0, 1.0, (3, 4)) low, high = box.bounds assert low.shape == (3, 4) assert high.shape == (3, 4) assert low.dtype == np.float32 assert high.dtype == np.float32
def test_convert_tf(self): tup = Tuple((Box(0.0, 1.0, (3, 4)), Discrete(2))) tensor_tup = tup.to_tf_placeholder('test', 1) assert isinstance(tensor_tup, tuple) assert all([isinstance(c, tf.Tensor) for c in tensor_tup]) assert [c.dtype for c in tensor_tup] == [tf.float32, tf.int64] assert [c.get_shape().as_list() for c in tensor_tup] == [[None, 3, 4], [None, 2]]
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_invalid_env(self): with self.assertRaises(ValueError): Box(0.0, 1.0) with self.assertRaises(Exception): Box(np.array([-1.0, -2.0]), np.array([1.0, 2.0]), (2, 2))
def test_hash(self): box1 = Box(0.0, 1.0, (3, 4)) box2 = Box(0.0, 1.0, (3, 4)) assert box1.__hash__() == box2.__hash__()
def test_concat(self): tup1 = Tuple((Box(0, 1, (5, )), Box(0, 1, (10, )))) tup2 = Tuple((Box(0, 1, (5, )), Box(0, 1, (10, )))) concat_tup = tup1.concat(tup2) assert concat_tup.flat_dim == 30
def test_flat_dim(self): d = Dict( collections.OrderedDict(position=Box(0, 10, (2, )), velocity=Box(0, 10, (3, )))) assert d.flat_dim == 5
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_convert_theano(self): box = Box(0.0, 1.0, (3, 4)) tensor = box.to_theano_tensor('test', 1) assert isinstance(tensor, theano.tensor.TensorVariable) assert box.dtype == np.float32 assert tensor.dtype == 'float32'
def test_convert_tf(self): box = Box(0.0, 1.0, (3, 4)) tensor = box.to_tf_placeholder('test', 1) assert isinstance(tensor, tf.Tensor) assert tensor.dtype == tf.float32 assert tensor.get_shape().as_list() == [None, 3, 4]
def test_unflatten(self): box = Box(0.0, 1.0, (3, 4)) arr = box.unflatten([(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]) assert arr.shape == (3, 4) assert arr.dtype == np.int64
def test_flatten(self): box = Box(0.0, 1.0, (3, 4)) arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] flat_arr = box.flatten(arr) assert flat_arr.shape == (12, ) assert flat_arr.dtype == np.int64
def test_pickleable(self): env_spec = EnvSpec(Box(-1, 1, (1)), Box(-2, 2, (2))) round_trip = pickle.loads(pickle.dumps(env_spec)) assert round_trip assert round_trip.action_space == env_spec.action_space assert round_trip.observation_space == env_spec.observation_space
def test_flat_dim(self): box = Box(0.0, 1.0, (3, 4)) assert box.flat_dim == 12
def test_invalid_env(self): with self.assertRaises(AttributeError): Box(0.0, 1.0) with self.assertRaises(AssertionError): Box(np.array([-1.0, -2.0]), np.array([1.0, 2.0]), (2, 2))
def test_default_float32_env(self): box = Box(0.0, 1.0, (3, 4)) assert box.dtype == np.float32 box = Box(np.array([-1.0, -2.0]), np.array([1.0, 2.0])) assert box.dtype == np.float32
def test_hash(self): box = Box(0.0, 1.0, (3, 4)) assert box.__hash__() == 1213972508617964782