2022-04-24 17:14:33 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Iterable, Optional, Sequence
|
2022-03-31 12:50:38 -07:00
|
|
|
|
2019-03-25 00:47:16 +01:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
|
2022-04-24 17:14:33 +01:00
|
|
|
from gym.spaces.space import Space
|
|
|
|
from gym.utils import seeding
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2022-03-04 16:25:19 +01:00
|
|
|
class Tuple(Space[tuple], Sequence):
|
2016-04-27 08:00:58 -07:00
|
|
|
"""
|
|
|
|
A tuple (i.e., product) of simpler spaces
|
2016-06-11 23:10:58 -07:00
|
|
|
|
2022-04-06 20:12:55 +01:00
|
|
|
Example usage::
|
|
|
|
|
|
|
|
self.observation_space = spaces.Tuple((spaces.Discrete(2), spaces.Discrete(3)))
|
2016-04-27 08:00:58 -07:00
|
|
|
"""
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __init__(
|
2022-04-24 17:14:33 +01:00
|
|
|
self,
|
|
|
|
spaces: Iterable[Space],
|
|
|
|
seed: Optional[int | list[int] | seeding.RandomNumberGenerator] = None,
|
2022-01-24 23:22:11 +01:00
|
|
|
):
|
2021-12-16 13:45:37 +08:00
|
|
|
spaces = tuple(spaces)
|
2016-04-27 08:00:58 -07:00
|
|
|
self.spaces = spaces
|
2019-03-23 23:18:19 -07:00
|
|
|
for space in spaces:
|
2021-07-29 15:39:42 -04:00
|
|
|
assert isinstance(
|
|
|
|
space, Space
|
|
|
|
), "Elements of the tuple must be instances of gym.Space"
|
2022-01-24 23:22:11 +01:00
|
|
|
super().__init__(None, None, seed) # type: ignore
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2022-04-24 17:14:33 +01:00
|
|
|
def seed(self, seed: Optional[int | list[int]] = None) -> list:
|
2021-09-13 20:08:01 +02:00
|
|
|
seeds = []
|
|
|
|
|
|
|
|
if isinstance(seed, list):
|
|
|
|
for i, space in enumerate(self.spaces):
|
|
|
|
seeds += space.seed(seed[i])
|
|
|
|
elif isinstance(seed, int):
|
|
|
|
seeds = super().seed(seed)
|
|
|
|
try:
|
|
|
|
subseeds = self.np_random.choice(
|
|
|
|
np.iinfo(int).max,
|
|
|
|
size=len(self.spaces),
|
|
|
|
replace=False, # unique subseed for each subspace
|
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
subseeds = self.np_random.choice(
|
|
|
|
np.iinfo(int).max,
|
|
|
|
size=len(self.spaces),
|
|
|
|
replace=True, # we get more than INT_MAX subspaces
|
|
|
|
)
|
|
|
|
|
|
|
|
for subspace, subseed in zip(self.spaces, subseeds):
|
|
|
|
seeds.append(subspace.seed(int(subseed))[0])
|
|
|
|
elif seed is None:
|
|
|
|
for space in self.spaces:
|
|
|
|
seeds += space.seed(seed)
|
|
|
|
else:
|
|
|
|
raise TypeError("Passed seed not of an expected type: list or int or None")
|
|
|
|
|
|
|
|
return seeds
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def sample(self) -> tuple:
|
2021-11-14 14:50:23 +01:00
|
|
|
return tuple(space.sample() for space in self.spaces)
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def contains(self, x) -> bool:
|
2021-12-16 13:45:37 +08:00
|
|
|
if isinstance(x, (list, np.ndarray)):
|
|
|
|
x = tuple(x) # Promote list and ndarray to tuple for contains check
|
2021-07-29 02:26:34 +02:00
|
|
|
return (
|
|
|
|
isinstance(x, tuple)
|
|
|
|
and len(x) == len(self.spaces)
|
|
|
|
and all(space.contains(part) for (space, part) in zip(self.spaces, x))
|
|
|
|
)
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __repr__(self) -> str:
|
2021-07-29 02:26:34 +02:00
|
|
|
return "Tuple(" + ", ".join([str(s) for s in self.spaces]) + ")"
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2022-04-08 03:19:52 +02:00
|
|
|
def to_jsonable(self, sample_n: Sequence) -> list:
|
2016-04-27 08:00:58 -07:00
|
|
|
# serialize as list-repr of tuple of vectors
|
2021-07-29 15:39:42 -04:00
|
|
|
return [
|
|
|
|
space.to_jsonable([sample[i] for sample in sample_n])
|
|
|
|
for i, space in enumerate(self.spaces)
|
|
|
|
]
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def from_jsonable(self, sample_n) -> list:
|
2021-07-29 15:39:42 -04:00
|
|
|
return [
|
|
|
|
sample
|
|
|
|
for sample in zip(
|
|
|
|
*[
|
|
|
|
space.from_jsonable(sample_n[i])
|
|
|
|
for i, space in enumerate(self.spaces)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __getitem__(self, index: int) -> Space:
|
2019-03-01 18:22:58 -05:00
|
|
|
return self.spaces[index]
|
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __len__(self) -> int:
|
2019-03-01 18:22:58 -05:00
|
|
|
return len(self.spaces)
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __eq__(self, other) -> bool:
|
2019-03-23 23:18:19 -07:00
|
|
|
return isinstance(other, Tuple) and self.spaces == other.spaces
|