2022-05-10 17:18:06 +02:00
|
|
|
"""Implementation of the `Space` metaclass."""
|
2022-01-24 23:22:11 +01:00
|
|
|
|
2022-05-25 15:28:19 +01:00
|
|
|
from typing import (
|
|
|
|
Generic,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Mapping,
|
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
Tuple,
|
|
|
|
Type,
|
|
|
|
TypeVar,
|
|
|
|
Union,
|
|
|
|
)
|
2021-12-22 19:12:57 +01:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2019-05-25 00:57:29 +02:00
|
|
|
from gym.utils import seeding
|
|
|
|
|
2021-12-22 19:12:57 +01:00
|
|
|
T_cov = TypeVar("T_cov", covariant=True)
|
|
|
|
|
|
|
|
|
|
|
|
class Space(Generic[T_cov]):
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Superclass that is used to define observation and action spaces.
|
|
|
|
|
|
|
|
Spaces are crucially used in Gym to define the format of valid actions and observations.
|
|
|
|
They serve various purposes:
|
|
|
|
|
2022-05-25 14:46:41 +01:00
|
|
|
* They clearly define how to interact with environments, i.e. they specify what actions need to look like
|
|
|
|
and what observations will look like
|
|
|
|
* They allow us to work with highly structured data (e.g. in the form of elements of :class:`Dict` spaces)
|
|
|
|
and painlessly transform them into flat arrays that can be used in learning code
|
2022-05-10 17:18:06 +02:00
|
|
|
* They provide a method to sample random elements. This is especially useful for exploration and debugging.
|
|
|
|
|
|
|
|
Different spaces can be combined hierarchically via container spaces (:class:`Tuple` and :class:`Dict`) to build a
|
|
|
|
more expressive space
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
Custom observation & action spaces can inherit from the ``Space``
|
|
|
|
class. However, most use-cases should be covered by the existing space
|
|
|
|
classes (e.g. :class:`Box`, :class:`Discrete`, etc...), and container classes (:class`Tuple` &
|
|
|
|
:class:`Dict`). Note that parametrized probability distributions (through the
|
|
|
|
:meth:`Space.sample()` method), and batching functions (in :class:`gym.vector.VectorEnv`), are
|
|
|
|
only well-defined for instances of spaces provided in gym by default.
|
|
|
|
Moreover, some implementations of Reinforcement Learning algorithms might
|
|
|
|
not handle custom spaces properly. Use custom spaces with care.
|
2019-01-30 22:39:55 +01:00
|
|
|
"""
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
shape: Optional[Sequence[int]] = None,
|
2022-05-25 15:28:19 +01:00
|
|
|
dtype: Optional[Union[Type, str, np.dtype]] = None,
|
|
|
|
seed: Optional[Union[int, seeding.RandomNumberGenerator]] = None,
|
2022-01-24 23:22:11 +01:00
|
|
|
):
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Constructor of :class:`Space`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
shape (Optional[Sequence[int]]): If elements of the space are numpy arrays, this should specify their shape.
|
|
|
|
dtype (Optional[Type | str]): If elements of the space are numpy arrays, this should specify their dtype.
|
|
|
|
seed: Optionally, you can use this argument to seed the RNG that is used to sample from the space
|
|
|
|
"""
|
2021-09-11 19:07:02 +02:00
|
|
|
self._shape = None if shape is None else tuple(shape)
|
2019-01-30 22:39:55 +01:00
|
|
|
self.dtype = None if dtype is None else np.dtype(dtype)
|
2020-05-29 22:11:39 +01:00
|
|
|
self._np_random = None
|
2021-09-13 20:08:01 +02:00
|
|
|
if seed is not None:
|
2022-04-24 17:14:33 +01:00
|
|
|
if isinstance(seed, seeding.RandomNumberGenerator):
|
|
|
|
self._np_random = seed
|
|
|
|
else:
|
|
|
|
self.seed(seed)
|
2020-05-29 22:11:39 +01:00
|
|
|
|
|
|
|
@property
|
2022-01-24 23:22:11 +01:00
|
|
|
def np_random(self) -> seeding.RandomNumberGenerator:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Lazily seed the PRNG since this is expensive and only needed if sampling from this space."""
|
2020-05-29 22:11:39 +01:00
|
|
|
if self._np_random is None:
|
|
|
|
self.seed()
|
|
|
|
|
2021-12-22 19:12:57 +01:00
|
|
|
return self._np_random # type: ignore ## self.seed() call guarantees right type.
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2021-09-11 19:07:02 +02:00
|
|
|
@property
|
2022-05-25 15:28:19 +01:00
|
|
|
def shape(self) -> Optional[Tuple[int, ...]]:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Return the shape of the space as an immutable property."""
|
2021-09-11 19:07:02 +02:00
|
|
|
return self._shape
|
|
|
|
|
2021-12-22 19:12:57 +01:00
|
|
|
def sample(self) -> T_cov:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Randomly sample an element of this space. Can be uniform or non-uniform sampling based on boundedness of space."""
|
2019-01-30 22:39:55 +01:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def seed(self, seed: Optional[int] = None) -> list:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Seed the PRNG of this space and possibly the PRNGs of subspaces."""
|
2020-05-29 22:11:39 +01:00
|
|
|
self._np_random, seed = seeding.np_random(seed)
|
2019-05-25 00:57:29 +02:00
|
|
|
return [seed]
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2021-12-22 19:12:57 +01:00
|
|
|
def contains(self, x) -> bool:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Return boolean specifying if x is a valid member of this space."""
|
2019-01-30 22:39:55 +01:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-12-22 19:12:57 +01:00
|
|
|
def __contains__(self, x) -> bool:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Return boolean specifying if x is a valid member of this space."""
|
2019-01-30 22:39:55 +01:00
|
|
|
return self.contains(x)
|
|
|
|
|
2022-05-25 15:28:19 +01:00
|
|
|
def __setstate__(self, state: Union[Iterable, Mapping]):
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Used when loading a pickled space.
|
|
|
|
|
|
|
|
This method was implemented explicitly to allow for loading of legacy states.
|
2022-05-25 14:46:41 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
state: The updated state value
|
2022-05-10 17:18:06 +02:00
|
|
|
"""
|
2021-09-23 13:51:43 -06:00
|
|
|
# Don't mutate the original state
|
|
|
|
state = dict(state)
|
|
|
|
|
|
|
|
# Allow for loading of legacy states.
|
|
|
|
# See:
|
|
|
|
# https://github.com/openai/gym/pull/2397 -- shape
|
|
|
|
# https://github.com/openai/gym/pull/1913 -- np_random
|
|
|
|
#
|
|
|
|
if "shape" in state:
|
|
|
|
state["_shape"] = state["shape"]
|
|
|
|
del state["shape"]
|
|
|
|
if "np_random" in state:
|
|
|
|
state["_np_random"] = state["np_random"]
|
|
|
|
del state["np_random"]
|
|
|
|
|
|
|
|
# Update our state
|
|
|
|
self.__dict__.update(state)
|
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def to_jsonable(self, sample_n: Sequence[T_cov]) -> list:
|
2019-01-30 22:39:55 +01:00
|
|
|
"""Convert a batch of samples from this space to a JSONable data type."""
|
|
|
|
# By default, assume identity is JSONable
|
2022-01-24 23:22:11 +01:00
|
|
|
return list(sample_n)
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2022-05-25 15:28:19 +01:00
|
|
|
def from_jsonable(self, sample_n: list) -> List[T_cov]:
|
2019-01-30 22:39:55 +01:00
|
|
|
"""Convert a JSONable data type to a batch of samples from this space."""
|
|
|
|
# By default, assume identity is JSONable
|
|
|
|
return sample_n
|