def test_discrete_space(n): sp = Discrete(n) for ii in range(n): assert sp.contains(ii) for ii in range(2 * n): assert sp.contains(sp.sample())
class ActionDictTestEnv(gym.Env): action_space = Dict({"position": Discrete(1), "velocity": Discrete(1)}) observation_space = Box(low=-1.0, high=2.0, shape=(3, ), dtype=np.float32) def step(self, action): observation = np.array([1.0, 1.5, 0.5]) reward = 1 done = True return observation, reward, done def reset(self): return np.array([1.0, 1.5, 0.5])
def test_tuple(): sp1 = Box(0.0, 1.0, shape=(3, 2)) sp2 = Discrete(2) sp = Tuple([sp1, sp2]) for _ in range(10): assert sp.contains(sp.sample()) sp.reseed()
def convert_space_from_gym(gym_space): if isinstance(gym_space, gym.spaces.Discrete): return Discrete(gym_space.n) # # elif isinstance(gym_space, gym.spaces.Box): return Box(gym_space.low, gym_space.high, gym_space.shape, gym_space.dtype) # # elif isinstance(gym_space, gym.spaces.Tuple): spaces = [] for sp in gym_space.spaces: spaces.append(convert_space_from_gym(sp)) return Tuple(spaces) # # elif isinstance(gym_space, gym.spaces.MultiDiscrete): return MultiDiscrete(gym_space.nvec) # # elif isinstance(gym_space, gym.spaces.MultiBinary): return MultiBinary(gym_space.n) # # elif isinstance(gym_space, gym.spaces.Dict): spaces = {} for key in gym_space.spaces: spaces[key] = convert_space_from_gym(gym_space[key]) return Dict(spaces) else: raise ValueError("Unknown space class: {}".format(type(gym_space)))
def test_dict(): nested_observation_space = Dict({ 'sensors': Dict({ 'position': Box(low=-100, high=100, shape=(3, )), 'velocity': Box(low=-1, high=1, shape=(3, )), 'front_cam': Tuple((Box(low=0, high=1, shape=(10, 10, 3)), Box(low=0, high=1, shape=(10, 10, 3)))), 'rear_cam': Box(low=0, high=1, shape=(10, 10, 3)), }), 'ext_controller': MultiDiscrete((5, 2, 2)), 'inner_state': Dict({ 'charge': Discrete(100), 'system_checks': MultiBinary(10), 'job_status': Dict({ 'task': Discrete(5), 'progress': Box(low=0, high=100, shape=()), }) }) }) sp = nested_observation_space for _ in range(10): assert sp.contains(sp.sample()) sp.reseed() sp2 = Dict(sp.spaces) for _ in range(10): assert sp.contains(sp2.sample()) sp2.reseed()
def test_dict(): nested_observation_space = Dict({ "sensors": Dict({ "position": Box(low=-100, high=100, shape=(3, )), "velocity": Box(low=-1, high=1, shape=(3, )), "front_cam": Tuple(( Box(low=0, high=1, shape=(10, 10, 3)), Box(low=0, high=1, shape=(10, 10, 3)), )), "rear_cam": Box(low=0, high=1, shape=(10, 10, 3)), }), "ext_controller": MultiDiscrete((5, 2, 2)), "inner_state": Dict({ "charge": Discrete(100), "system_checks": MultiBinary(10), "job_status": Dict({ "task": Discrete(5), "progress": Box(low=0, high=100, shape=()), }), }), }) sp = nested_observation_space for _ in range(10): assert sp.contains(sp.sample()) sp.reseed() sp2 = Dict(sp.spaces) for _ in range(10): assert sp.contains(sp2.sample()) sp2.reseed()