def test_product(): box = Box(-1.0, 1.0, shape=(2, 3), dtype=np.float32) discrete = Discrete(5) product = Product([discrete, box]) assert product.dtype is None assert product.shape is None assert isinstance(product.spaces, tuple) assert len(product.spaces) == 2 s1 = product.sample() assert isinstance(s1, tuple) assert len(s1) == 2 assert isinstance(s1[0], int) assert isinstance(s1[1], np.ndarray) assert s1[1].shape == (2, 3) assert s1 in product assert product.flat_dim == 2 * 3 + 5 assert isinstance(product.flatten(s1), np.ndarray) assert product.flatten(s1).shape == (11, ) s2 = product.unflatten(product.flatten(s1)) assert isinstance(s2, tuple) assert len(s2) == 2 assert isinstance(s2[0], int) assert isinstance(s2[1], np.ndarray) assert s2[1].shape == (2, 3)
def test_dict(self): with pytest.raises(AssertionError): Dict([Discrete(10), Box(-1, 1, np.float32, shape=(3, ))]) sensor_space = Dict({ 'position': Box(-100, 100, np.float32, shape=(3, )), 'velocity': Box(-1, 1, np.float32, shape=(3, )) }) assert len(sensor_space.spaces) == 2 assert 'position' in sensor_space.spaces and 'velocity' in sensor_space.spaces assert sensor_space.spaces['position'] == Box(-100, 100, np.float32, shape=(3, )) assert sensor_space.spaces['velocity'] == Box(-1, 1, np.float32, shape=(3, )) space = Dict({'sensors': sensor_space, 'score': Discrete(100)}) assert len(space.spaces) == 2 assert 'sensors' in space.spaces and 'score' in space.spaces assert space.spaces['sensors'] == sensor_space assert space.spaces['score'] == Discrete(100) sample = space.sample() assert isinstance(sample, dict) and len(sample) == 2 assert isinstance(sample['sensors'], dict) and len( sample['sensors']) == 2 assert sample['sensors'] in sensor_space assert sample['score'] in Discrete(100) assert space.flat_dim == 3 + 3 + 100 assert space.flatten(sample).shape == (106, ) sample2 = space.unflatten(space.flatten(sample)) assert sample['score'] == sample2['score'] assert np.allclose(sample['sensors']['position'], sample2['sensors']['position']) assert np.allclose(sample['sensors']['velocity'], sample2['sensors']['velocity']) assert sample in space
def __init__(self, cycle): self.cycle = cycle self.x = 0.01 self.r = 0.0 self.count = 0 self._observation_space = Box(0.0, float(self.cycle), shape=[ 1, ], dtype=np.float32) self._action_space = Discrete(2)
def __init__(self, env, keys): super().__init__(env) self.keys = keys spaces = self.env.observation_space.spaces assert all([isinstance(space, Box) for space in spaces.values()]) # enforce all Box spaces # Calculate dimensionality shape = (int(np.sum([spaces[key].flat_dim for key in self.keys])), ) self._observation_space = Box(low=-np.inf, high=np.inf, shape=shape, dtype=np.float32)
def _convert_gym_space(self, space): """ Enforce the space dtype to lagom spaces Args: space (gym Space): gym version of spaces Returns: converted space (lagom Space) """ if isinstance(space, gym.spaces.Box): return Box(low=space.low, high=space.high, dtype=space.dtype) # Don't give shape elif isinstance(space, gym.spaces.Dict): return Dict(dict([(key, self._convert_gym_space(space)) for key, space in space.spaces.items()])) elif isinstance(space, gym.spaces.Discrete): return Discrete(n=space.n) elif isinstance(space, gym.spaces.Tuple): return Product([self._convert_gym_space(s) for s in space.spaces]) else: raise TypeError('Currently only Box, Dict, Discrete and Tuple spaces are supported. ')
def check(box): assert all([ dtype == np.float32 for dtype in [box.dtype, box.low.dtype, box.high.dtype] ]) assert all([ s == (2, 3) for s in [box.shape, box.low.shape, box.high.shape] ]) assert np.allclose(box.low, np.full([2, 3], -1.0)) assert np.allclose(box.high, np.full([2, 3], 1.0)) sample = box.sample() assert sample.shape == (2, 3) and sample.dtype == np.float32 assert box.flat_dim == 6 and isinstance(box.flat_dim, int) assert box.flatten(sample).shape == (6, ) assert np.allclose(sample, box.unflatten(box.flatten(sample))) assert sample in box assert str(box) == 'Box(2, 3)' assert box == Box(-1.0, 1.0, np.float32, shape=[2, 3]) del box, sample
def __init__(self, env, num_stack): r"""Initialize the wrapper. Args: env (Env): environment object num_stack (int): number of stacks """ super().__init__(env) self.num_stack = num_stack assert isinstance(self.env.observation_space, Box), 'must be Box type' # Create a new observation space low = np.repeat(self.env.observation_space.low[..., np.newaxis], self.num_stack, axis=-1) high = np.repeat(self.env.observation_space.high[..., np.newaxis], self.num_stack, axis=-1) dtype = self.env.observation_space.dtype self._observation_space = Box(low=low, high=high, dtype=dtype) # Initialize the buffer for stacked observation self.stack_buffer = np.zeros(self._observation_space.shape, dtype=dtype)
def seed(self, seed): self.last_seed = seed self.current_player = 1 self.random_state = np.random.RandomState(seed) self.game = game.make_game(seed) self.game.its_showtime() self.actual_observation_space = Tuple( (Box(0, 255, dtype=np.uint8, shape=(game.MAP_HEIGHT, game.MAP_WIDTH)), Box(0, 255, dtype=np.uint8, shape=(3, )), Box(0, 255, dtype=np.uint8, shape=(3, )), Box(0, 1, dtype=np.uint8, shape=(game.MAP_HEIGHT, game.MAP_WIDTH)), Box(0, 1, dtype=np.uint8, shape=(game.MAP_HEIGHT, game.MAP_WIDTH)), Box(0, 1, dtype=np.uint8, shape=(game.GOAL_LEN, )), Box(0, 1, dtype=np.uint8, shape=(game.GOAL_LEN, ))))
def __init__(self, env, keys): r"""Initialize the wrapper. Args: env (Env): environment object keys (list): a list of selected keys to flatten the observation. """ super().__init__(env) self.keys = keys # Sanity check that all subspaces should be Box spaces = self.env.observation_space.spaces assert all([isinstance(space, Box) for space in spaces.values()]) # Calculate dimensionality shape = (int(np.sum([spaces[key].flat_dim for key in self.keys])), ) # Create new observation space self._observation_space = Box(low=-np.inf, high=np.inf, shape=shape, dtype=np.float32)
def test_tuple(self): with pytest.raises(AssertionError): Tuple(Discrete(10)) space = Tuple([ Discrete(5), Box(-1.0, 1.0, np.float32, shape=(2, 3)), Dict({ 'success': Discrete(2), 'velocity': Box(-1, 1, np.float32, shape=(1, 3)) }) ]) assert len(space.spaces) == 3 assert space.spaces[0] == Discrete(5) assert space.spaces[1] == Box(-1.0, 1.0, np.float32, shape=(2, 3)) assert space.spaces[2] == Dict({ 'success': Discrete(2), 'velocity': Box(-1, 1, np.float32, shape=(1, 3)) }) sample = space.sample() assert isinstance(sample, tuple) and len(sample) == 3 assert sample[0] in Discrete(5) assert sample[1] in Box(-1.0, 1.0, np.float32, shape=(2, 3)) assert sample[2] in Dict({ 'success': Discrete(2), 'velocity': Box(-1, 1, np.float32, shape=(1, 3)) }) assert space.flat_dim == 5 + 2 * 3 + 2 + 3 assert space.flatten(sample).shape == (16, ) sample2 = space.unflatten(space.flatten(sample)) assert sample[0] == sample2[0] assert np.allclose(sample[1], sample2[1]) assert sample[2]['success'] == sample2[2]['success'] assert np.allclose(sample[2]['velocity'], sample2[2]['velocity']) assert sample in space
def test_box(self): with pytest.raises(AssertionError): Box(-1.0, 1.0, dtype=None) with pytest.raises(AssertionError): Box(-1.0, [1.0, 2.0], np.float32, shape=(2, )) with pytest.raises(AssertionError): Box(np.array([-1.0, -2.0]), np.array([3.0, 4.0, 5.0]), np.float32) with pytest.raises(AttributeError): Box(np.array([-1.0, -2.0]), [3.0, 4.0], np.float32) def check(box): assert all([ dtype == np.float32 for dtype in [box.dtype, box.low.dtype, box.high.dtype] ]) assert all([ s == (2, 3) for s in [box.shape, box.low.shape, box.high.shape] ]) assert np.allclose(box.low, np.full([2, 3], -1.0)) assert np.allclose(box.high, np.full([2, 3], 1.0)) sample = box.sample() assert sample.shape == (2, 3) and sample.dtype == np.float32 assert box.flat_dim == 6 and isinstance(box.flat_dim, int) assert box.flatten(sample).shape == (6, ) assert np.allclose(sample, box.unflatten(box.flatten(sample))) assert sample in box assert str(box) == 'Box(2, 3)' assert box == Box(-1.0, 1.0, np.float32, shape=[2, 3]) del box, sample box1 = Box(-1.0, 1.0, np.float32, shape=[2, 3]) check(box1) x = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) box2 = Box(low=-x, high=x, dtype=np.float32) check(box2) assert box1 == box2
def observation_space(self): low = np.append(self.env.observation_space.low, 0.0) high = np.append(self.env.observation_space.high, np.inf) return Box(low, high, np.float32)
def observation_space(self): return Box(low=0, high=1, shape=self.env.observation_space.shape, dtype=np.float32)
def test_dict(): # Simple use space = Dict({ 'position': Discrete(2), 'velocity': Box(low=-1.0, high=1.0, shape=(1, 2), dtype=np.float32) }) assert space.dtype is None assert space.shape is None assert isinstance(space.spaces, dict) assert len(space.spaces) == 2 assert 'position' in space.spaces assert 'velocity' in space.spaces assert isinstance(space.spaces['position'], Discrete) assert isinstance(space.spaces['velocity'], Box) assert space.spaces['position'].n == 2 assert space.spaces['velocity'].shape == (1, 2) s1 = space.sample() assert s1 in space assert isinstance(s1, OrderedDict) assert len(s1) == 2 assert 'position' in s1 assert 'velocity' in s1 assert isinstance(s1['position'], int) assert isinstance(s1['velocity'], np.ndarray) assert s1['position'] in space.spaces['position'] assert s1['velocity'] in space.spaces['velocity'] assert space.flat_dim == 2 + 1 * 2 flat_s1 = space.flatten(s1) assert isinstance(flat_s1, np.ndarray) assert flat_s1.dtype == np.float32 assert flat_s1.shape == (4, ) assert flat_s1[:2].sum() == 1 assert flat_s1.max() <= 1.0 assert flat_s1.min() >= -1.0 unflat_s1 = space.unflatten(flat_s1) assert isinstance(unflat_s1, OrderedDict) assert len(unflat_s1) == 2 assert 'position' in unflat_s1 assert 'velocity' in unflat_s1 assert unflat_s1['position'] == s1['position'] assert np.allclose(unflat_s1['velocity'], s1['velocity']) assert unflat_s1 in space del space del s1 del flat_s1 del unflat_s1 # Nested space = Dict({ 'sensors': Dict({ 'position': Box(-100, 100, shape=(3, ), dtype=np.float32), 'velocity': Box(-1, 1, shape=(3, ), dtype=np.float32) }), 'score': Discrete(100) }) # Do not do redundant tests assert space.flat_dim == 3 + 3 + 100 s1 = space.sample() assert s1 in space assert 'score' in s1 assert 'sensors' in s1 assert 'position' in s1['sensors'] assert 'velocity' in s1['sensors'] flat_s1 = space.flatten(s1) assert flat_s1.shape == (3 + 3 + 100, ) unflat_s1 = space.unflatten(flat_s1) assert unflat_s1 in space
def observation_space(self): obs_shape = list(self.env.observation_space.shape) obs_shape[0] = self.size obs_shape[1] = self.size return Box(low=0, high=255, shape=obs_shape, dtype=np.uint8)
def test_box(): with pytest.raises(AssertionError): Box(-1.0, 1.0, dtype=None) box1 = Box(-1.0, 1.0, shape=[2, 3], dtype=np.float32) assert box1.dtype == np.float32 assert box1.shape == (2, 3) assert box1.low.shape == (2, 3) assert box1.high.shape == (2, 3) s1 = box1.sample() assert s1.shape == (2, 3) assert s1 in box1 assert box1.flat_dim == 6 assert box1.flatten(s1).shape == (6, ) assert np.allclose(s1, box1.unflatten(box1.flatten(s1))) low = np.array([[-1.0, -1.0, -1.0], [-1.0, -1.0, -1.0]]) box2 = Box(low=low, high=-low, dtype=np.float32) assert box2.dtype == np.float32 assert box2.shape == (2, 3) assert box2.low.shape == (2, 3) assert box2.high.shape == (2, 3) s2 = box2.sample() assert s2.shape == (2, 3) assert s2 in box2 assert box1 == box2 assert box2.flat_dim == 6 assert box2.flatten(s2).shape == (6, ) assert np.allclose(s2, box2.unflatten(box2.flatten(s2)))
def observation_space(self): obs_shape = self.env.observation_space.shape[:2] if self.keep_dim: return Box(low=0, high=255, shape=(*obs_shape, 1), dtype=np.uint8) else: return Box(low=0, high=255, shape=obs_shape, dtype=np.uint8)
def observation_space(self): flat_dim = self.actual_observation_space.flat_dim return Box(0, 255, dtype=np.uint8, shape=(flat_dim, ))