mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-07-31 22:04:31 +00:00
* Added pydocstyle to pre-commit * Added docstrings for tests and updated the tests for autoreset * Add pydocstyle exclude folder to allow slowly adding new docstrings * Add docstrings for setup.py and gym/__init__.py, core.py, error.py and logger.py * Check that all unwrapped environment are of a particular wrapper type * Reverted back to import gym.spaces.Space to gym.spaces * Fixed the __init__.py docstring * Fixed autoreset autoreset test * Updated gym __init__.py top docstring * Fix examples in docstrings * Add docstrings and type hints where known to all functions and classes in gym/utils and gym/vector * Remove unnecessary import * Removed "unused error" and make APIerror deprecated at gym 1.0 * Add pydocstyle description to CONTRIBUTING.md * Added docstrings section to CONTRIBUTING.md * Added :meth: and :attr: keywords to docstrings * Added :meth: and :attr: keywords to docstrings * Imported annotations from __future__ to fix python 3.7 * Add __future__ import annotations for python 3.7 * isort * Remove utils and vectors for this PR and spaces for previous PR * Update gym/envs/classic_control/acrobot.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/envs/classic_control/acrobot.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/envs/classic_control/acrobot.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/spaces/dict.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/env_checker.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/ezpickle.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/ezpickle.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Update gym/utils/play.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Pre-commit * Updated docstrings with :meth: * Updated docstrings with :meth: * Update gym/utils/play.py * Update gym/utils/play.py * Update gym/utils/play.py * Apply suggestions from code review Co-authored-by: Markus Krimmel <montcyril@gmail.com> * pre-commit * Update gym/utils/play.py Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Updated fps and zoom parameter docstring * Update play docstring * Apply suggestions from code review Added suggested corrections from @markus28 Co-authored-by: Markus Krimmel <montcyril@gmail.com> * Pre-commit magic * Update the `gym.make` docstring with a warning for `env_checker` * Updated and fixed vector docstrings * Update test names for reflect the project filename style Co-authored-by: Markus Krimmel <montcyril@gmail.com>
130 lines
4.7 KiB
Python
130 lines
4.7 KiB
Python
"""Implementation of a space that represents the cartesian product of other spaces."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Iterable, Optional, Sequence
|
|
|
|
import numpy as np
|
|
|
|
from gym.spaces.space import Space
|
|
from gym.utils import seeding
|
|
|
|
|
|
class Tuple(Space[tuple], Sequence):
|
|
"""A tuple (more precisely: the cartesian product) of :class:`Space` instances.
|
|
|
|
Elements of this space are tuples of elements of the constituent spaces.
|
|
|
|
Example usage::
|
|
|
|
>>> from gym.spaces import Box, Discrete
|
|
>>> observation_space = Tuple((Discrete(2), Box(-1, 1, shape=(2,))))
|
|
>>> observation_space.sample()
|
|
(0, array([0.03633198, 0.42370757], dtype=float32))
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
spaces: Iterable[Space],
|
|
seed: Optional[int | list[int] | seeding.RandomNumberGenerator] = None,
|
|
):
|
|
r"""Constructor of :class:`Tuple`` space.
|
|
|
|
The generated instance will represent the cartesian product :math:`\text{spaces}[0] \times ... \times \text{spaces}[-1]`.
|
|
|
|
Args:
|
|
spaces (Iterable[Space]): The spaces that are involved in the cartesian product.
|
|
seed: Optionally, you can use this argument to seed the RNGs of the ``spaces`` to ensure reproducible sampling.
|
|
"""
|
|
spaces = tuple(spaces)
|
|
self.spaces = spaces
|
|
for space in spaces:
|
|
assert isinstance(
|
|
space, Space
|
|
), "Elements of the tuple must be instances of gym.Space"
|
|
super().__init__(None, None, seed) # type: ignore
|
|
|
|
def seed(self, seed: Optional[int | list[int]] = None) -> list:
|
|
"""Seed the PRNG of this space and all subspaces."""
|
|
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
|
|
|
|
def sample(self) -> tuple:
|
|
"""Generates a single random sample inside this space.
|
|
|
|
This method draws independent samples from the subspaces.
|
|
"""
|
|
return tuple(space.sample() for space in self.spaces)
|
|
|
|
def contains(self, x) -> bool:
|
|
"""Return boolean specifying if x is a valid member of this space."""
|
|
if isinstance(x, (list, np.ndarray)):
|
|
x = tuple(x) # Promote list and ndarray to tuple for contains check
|
|
return (
|
|
isinstance(x, tuple)
|
|
and len(x) == len(self.spaces)
|
|
and all(space.contains(part) for (space, part) in zip(self.spaces, x))
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
"""Gives a string representation of this space."""
|
|
return "Tuple(" + ", ".join([str(s) for s in self.spaces]) + ")"
|
|
|
|
def to_jsonable(self, sample_n: Sequence) -> list:
|
|
"""Convert a batch of samples from this space to a JSONable data type."""
|
|
# serialize as list-repr of tuple of vectors
|
|
return [
|
|
space.to_jsonable([sample[i] for sample in sample_n])
|
|
for i, space in enumerate(self.spaces)
|
|
]
|
|
|
|
def from_jsonable(self, sample_n) -> list:
|
|
"""Convert a JSONable data type to a batch of samples from this space."""
|
|
return [
|
|
sample
|
|
for sample in zip(
|
|
*[
|
|
space.from_jsonable(sample_n[i])
|
|
for i, space in enumerate(self.spaces)
|
|
]
|
|
)
|
|
]
|
|
|
|
def __getitem__(self, index: int) -> Space:
|
|
"""Get the subspace at specific `index`."""
|
|
return self.spaces[index]
|
|
|
|
def __len__(self) -> int:
|
|
"""Get the number of subspaces that are involved in the cartesian product."""
|
|
return len(self.spaces)
|
|
|
|
def __eq__(self, other) -> bool:
|
|
"""Check whether ``other`` is equivalent to this instance."""
|
|
return isinstance(other, Tuple) and self.spaces == other.spaces
|