mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-02 06:16:32 +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>
86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
"""Implementation of a space that consists of binary np.ndarrays of a fixed shape."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional, Sequence, Union
|
|
|
|
import numpy as np
|
|
|
|
from gym.spaces.space import Space
|
|
from gym.utils import seeding
|
|
|
|
|
|
class MultiBinary(Space[np.ndarray]):
|
|
"""An n-shape binary space.
|
|
|
|
Elements of this space are binary arrays of a shape that is fixed during construction.
|
|
|
|
Example Usage::
|
|
|
|
>>> observation_space = MultiBinary(5)
|
|
>>> observation_space.sample()
|
|
array([0, 1, 0, 1, 0], dtype=int8)
|
|
>>> observation_space = MultiBinary([3, 2])
|
|
>>> observation_space.sample()
|
|
array([[0, 0],
|
|
[0, 1],
|
|
[1, 1]], dtype=int8)
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
n: Union[np.ndarray, Sequence[int], int],
|
|
seed: Optional[int | seeding.RandomNumberGenerator] = None,
|
|
):
|
|
"""Constructor of :class:`MultiBinary` space.
|
|
|
|
Args:
|
|
n: This will fix the shape of elements of the space. It can either be an integer (if the space is flat)
|
|
or some sort of sequence (tuple, list or np.ndarray) if there are multiple axes.
|
|
seed: Optionally, you can use this argument to seed the RNG that is used to sample from the space.
|
|
"""
|
|
if isinstance(n, (Sequence, np.ndarray)):
|
|
self.n = input_n = tuple(int(i) for i in n)
|
|
assert (np.asarray(input_n) > 0).all() # n (counts) have to be positive
|
|
else:
|
|
self.n = n = int(n)
|
|
input_n = (n,)
|
|
assert (np.asarray(input_n) > 0).all() # n (counts) have to be positive
|
|
|
|
super().__init__(input_n, np.int8, seed)
|
|
|
|
@property
|
|
def shape(self) -> tuple[int, ...]:
|
|
"""Has stricter type than gym.Space - never None."""
|
|
return self._shape # type: ignore
|
|
|
|
def sample(self) -> np.ndarray:
|
|
"""Generates a single random sample from this space.
|
|
|
|
A sample is drawn by independent, fair coin tosses (one toss per binary variable of the space).
|
|
"""
|
|
return self.np_random.integers(low=0, high=2, size=self.n, dtype=self.dtype)
|
|
|
|
def contains(self, x) -> bool:
|
|
"""Return boolean specifying if x is a valid member of this space."""
|
|
if isinstance(x, Sequence):
|
|
x = np.array(x) # Promote list to array for contains check
|
|
if self.shape != x.shape:
|
|
return False
|
|
return ((x == 0) | (x == 1)).all()
|
|
|
|
def to_jsonable(self, sample_n) -> list:
|
|
"""Convert a batch of samples from this space to a JSONable data type."""
|
|
return np.array(sample_n).tolist()
|
|
|
|
def from_jsonable(self, sample_n) -> list:
|
|
"""Convert a JSONable data type to a batch of samples from this space."""
|
|
return [np.asarray(sample) for sample in sample_n]
|
|
|
|
def __repr__(self) -> str:
|
|
"""Gives a string representation of this space."""
|
|
return f"MultiBinary({self.n})"
|
|
|
|
def __eq__(self, other) -> bool:
|
|
"""Check whether `other` is equivalent to this instance."""
|
|
return isinstance(other, MultiBinary) and self.n == other.n
|