2017-09-05 08:49:43 -07:00
|
|
|
from collections import OrderedDict
|
2019-01-30 22:39:55 +01:00
|
|
|
from .space import Space
|
2017-09-05 08:49:43 -07:00
|
|
|
|
2019-01-30 22:39:55 +01:00
|
|
|
|
|
|
|
class Dict(Space):
|
2017-09-05 08:49:43 -07:00
|
|
|
"""
|
2017-11-05 21:16:46 +03:00
|
|
|
A dictionary of simpler spaces.
|
2017-09-05 08:49:43 -07:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
self.observation_space = spaces.Dict({"position": spaces.Discrete(2), "velocity": spaces.Discrete(3)})
|
2017-11-05 21:16:46 +03:00
|
|
|
|
|
|
|
Example usage [nested]:
|
|
|
|
self.nested_observation_space = spaces.Dict({
|
|
|
|
'sensors': spaces.Dict({
|
2018-08-13 11:53:23 -07:00
|
|
|
'position': spaces.Box(low=-100, high=100, shape=(3,)),
|
|
|
|
'velocity': spaces.Box(low=-1, high=1, shape=(3,)),
|
2017-11-05 21:16:46 +03:00
|
|
|
'front_cam': spaces.Tuple((
|
|
|
|
spaces.Box(low=0, high=1, shape=(10, 10, 3)),
|
|
|
|
spaces.Box(low=0, high=1, shape=(10, 10, 3))
|
|
|
|
)),
|
|
|
|
'rear_cam': spaces.Box(low=0, high=1, shape=(10, 10, 3)),
|
|
|
|
}),
|
2019-03-01 14:36:47 -08:00
|
|
|
'ext_controller': spaces.MultiDiscrete((5, 2, 2)),
|
2017-11-05 21:16:46 +03:00
|
|
|
'inner_state':spaces.Dict({
|
|
|
|
'charge': spaces.Discrete(100),
|
|
|
|
'system_checks': spaces.MultiBinary(10),
|
|
|
|
'job_status': spaces.Dict({
|
|
|
|
'task': spaces.Discrete(5),
|
|
|
|
'progress': spaces.Box(low=0, high=100, shape=()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-09-05 08:49:43 -07:00
|
|
|
"""
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2018-12-19 17:53:08 -08:00
|
|
|
def __init__(self, spaces=None, **spaces_kwargs):
|
2021-07-29 12:42:48 -04:00
|
|
|
assert (spaces is None) or (not spaces_kwargs), "Use either Dict(spaces=dict(...)) or Dict(foo=x, bar=z)"
|
2018-12-19 17:53:08 -08:00
|
|
|
if spaces is None:
|
|
|
|
spaces = spaces_kwargs
|
2018-08-27 19:28:32 +02:00
|
|
|
if isinstance(spaces, dict) and not isinstance(spaces, OrderedDict):
|
2017-09-05 08:49:43 -07:00
|
|
|
spaces = OrderedDict(sorted(list(spaces.items())))
|
|
|
|
if isinstance(spaces, list):
|
2017-09-06 19:52:26 -07:00
|
|
|
spaces = OrderedDict(spaces)
|
2017-09-05 08:49:43 -07:00
|
|
|
self.spaces = spaces
|
2019-03-25 02:06:55 +01:00
|
|
|
for space in spaces.values():
|
2021-07-29 12:42:48 -04:00
|
|
|
assert isinstance(space, Space), "Values of the dict should be instances of gym.Space"
|
|
|
|
super(Dict, self).__init__(None, None) # None for shape and dtype, since it'll require special handling
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2019-05-24 18:16:07 -07:00
|
|
|
def seed(self, seed=None):
|
2019-01-30 22:39:55 +01:00
|
|
|
[space.seed(seed) for space in self.spaces.values()]
|
2017-09-05 08:49:43 -07:00
|
|
|
|
|
|
|
def sample(self):
|
|
|
|
return OrderedDict([(k, space.sample()) for k, space in self.spaces.items()])
|
|
|
|
|
|
|
|
def contains(self, x):
|
|
|
|
if not isinstance(x, dict) or len(x) != len(self.spaces):
|
|
|
|
return False
|
|
|
|
for k, space in self.spaces.items():
|
|
|
|
if k not in x:
|
|
|
|
return False
|
|
|
|
if not space.contains(x[k]):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-04-19 23:19:07 +02:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.spaces[key]
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2020-07-11 01:39:21 +02:00
|
|
|
def __iter__(self):
|
|
|
|
for key in self.spaces:
|
|
|
|
yield key
|
2019-04-19 23:19:07 +02:00
|
|
|
|
2017-09-05 08:49:43 -07:00
|
|
|
def __repr__(self):
|
2021-07-29 12:42:48 -04:00
|
|
|
return "Dict(" + ", ".join([str(k) + ":" + str(s) for k, s in self.spaces.items()]) + ")"
|
2017-09-05 08:49:43 -07:00
|
|
|
|
|
|
|
def to_jsonable(self, sample_n):
|
|
|
|
# serialize as dict-repr of vectors
|
2021-07-29 12:42:48 -04:00
|
|
|
return {key: space.to_jsonable([sample[key] for sample in sample_n]) for key, space in self.spaces.items()}
|
2017-09-05 08:49:43 -07:00
|
|
|
|
|
|
|
def from_jsonable(self, sample_n):
|
|
|
|
dict_of_list = {}
|
|
|
|
for key, space in self.spaces.items():
|
|
|
|
dict_of_list[key] = space.from_jsonable(sample_n[key])
|
|
|
|
ret = []
|
|
|
|
for i, _ in enumerate(dict_of_list[key]):
|
|
|
|
entry = {}
|
|
|
|
for key, value in dict_of_list.items():
|
|
|
|
entry[key] = value[i]
|
|
|
|
ret.append(entry)
|
|
|
|
return ret
|
2018-02-08 12:53:47 -08:00
|
|
|
|
2018-09-24 20:11:03 +02:00
|
|
|
def __eq__(self, other):
|
2019-03-23 23:18:19 -07:00
|
|
|
return isinstance(other, Dict) and self.spaces == other.spaces
|