2022-05-10 17:18:06 +02:00
|
|
|
"""Implementation of a space that consists of binary np.ndarrays of a fixed shape."""
|
2022-05-25 15:28:19 +01:00
|
|
|
from typing import Optional, Sequence, Tuple, Union
|
2022-03-31 12:50:38 -07:00
|
|
|
|
2017-02-04 23:36:32 -08:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
|
2022-09-08 10:10:07 +01:00
|
|
|
from gymnasium.spaces.space import Space
|
2017-02-04 23:36:32 -08:00
|
|
|
|
2019-01-30 22:39:55 +01:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
class MultiBinary(Space[np.ndarray]):
|
2022-05-10 17:18:06 +02:00
|
|
|
"""An n-shape binary space.
|
2020-01-24 21:42:41 -08:00
|
|
|
|
2022-05-10 17:18:06 +02:00
|
|
|
Elements of this space are binary arrays of a shape that is fixed during construction.
|
2022-04-06 20:12:55 +01:00
|
|
|
|
|
|
|
Example Usage::
|
|
|
|
|
2022-05-20 14:49:30 +01:00
|
|
|
>>> observation_space = MultiBinary(5)
|
|
|
|
>>> observation_space.sample()
|
2022-04-06 20:12:55 +01:00
|
|
|
array([0, 1, 0, 1, 0], dtype=int8)
|
2022-05-20 14:49:30 +01:00
|
|
|
>>> observation_space = MultiBinary([3, 2])
|
|
|
|
>>> observation_space.sample()
|
2022-04-06 20:12:55 +01:00
|
|
|
array([[0, 0],
|
|
|
|
[0, 1],
|
|
|
|
[1, 1]], dtype=int8)
|
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,
|
|
|
|
n: Union[np.ndarray, Sequence[int], int],
|
2022-08-22 09:20:28 -04:00
|
|
|
seed: Optional[Union[int, np.random.Generator]] = None,
|
2022-01-24 23:22:11 +01:00
|
|
|
):
|
2022-05-10 17:18:06 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2021-12-16 13:45:37 +08:00
|
|
|
if isinstance(n, (Sequence, np.ndarray)):
|
|
|
|
self.n = input_n = tuple(int(i) for i in n)
|
2022-04-11 13:27:23 -04:00
|
|
|
assert (np.asarray(input_n) > 0).all() # n (counts) have to be positive
|
2020-09-26 06:43:15 +08:00
|
|
|
else:
|
2021-12-16 13:45:37 +08:00
|
|
|
self.n = n = int(n)
|
2021-07-29 02:26:34 +02:00
|
|
|
input_n = (n,)
|
2022-04-11 13:27:23 -04:00
|
|
|
assert (np.asarray(input_n) > 0).all() # n (counts) have to be positive
|
2021-12-16 13:45:37 +08:00
|
|
|
|
2021-11-14 14:50:23 +01:00
|
|
|
super().__init__(input_n, np.int8, seed)
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
@property
|
2022-05-25 15:28:19 +01:00
|
|
|
def shape(self) -> Tuple[int, ...]:
|
2022-09-08 10:10:07 +01:00
|
|
|
"""Has stricter type than gymnasium.Space - never None."""
|
2022-01-24 23:22:11 +01:00
|
|
|
return self._shape # type: ignore
|
|
|
|
|
2022-08-15 17:11:32 +02:00
|
|
|
@property
|
|
|
|
def is_np_flattenable(self):
|
|
|
|
"""Checks whether this space can be flattened to a :class:`spaces.Box`."""
|
|
|
|
return True
|
|
|
|
|
2022-06-26 23:23:15 +01:00
|
|
|
def sample(self, mask: Optional[np.ndarray] = None) -> np.ndarray:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""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).
|
2022-05-25 14:46:41 +01:00
|
|
|
|
2022-06-26 23:23:15 +01:00
|
|
|
Args:
|
|
|
|
mask: An optional np.ndarray to mask samples with expected shape of ``space.shape``.
|
2022-09-03 22:56:29 +01:00
|
|
|
For mask == 0 then the samples will be 0 and mask == 1 then random samples will be generated.
|
|
|
|
The expected mask shape is the space shape and mask dtype is `np.int8`.
|
2022-06-26 23:23:15 +01:00
|
|
|
|
2022-05-25 14:46:41 +01:00
|
|
|
Returns:
|
|
|
|
Sampled values from space
|
2022-05-10 17:18:06 +02:00
|
|
|
"""
|
2022-06-26 23:23:15 +01:00
|
|
|
if mask is not None:
|
|
|
|
assert isinstance(
|
|
|
|
mask, np.ndarray
|
|
|
|
), f"The expected type of the mask is np.ndarray, actual type: {type(mask)}"
|
|
|
|
assert (
|
|
|
|
mask.dtype == np.int8
|
|
|
|
), f"The expected dtype of the mask is np.int8, actual dtype: {mask.dtype}"
|
|
|
|
assert (
|
|
|
|
mask.shape == self.shape
|
|
|
|
), f"The expected shape of the mask is {self.shape}, actual shape: {mask.shape}"
|
|
|
|
assert np.all(
|
2022-09-03 22:56:29 +01:00
|
|
|
(mask == 0) | (mask == 1) | (mask == 2)
|
|
|
|
), f"All values of a mask should be 0, 1 or 2, actual values: {mask}"
|
2022-06-26 23:23:15 +01:00
|
|
|
|
2022-09-03 22:56:29 +01:00
|
|
|
return np.where(
|
|
|
|
mask == 2,
|
|
|
|
self.np_random.integers(low=0, high=2, size=self.n, dtype=self.dtype),
|
|
|
|
mask.astype(self.dtype),
|
2022-06-26 23:23:15 +01:00
|
|
|
)
|
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
return self.np_random.integers(low=0, high=2, size=self.n, dtype=self.dtype)
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2022-01-24 23:22:11 +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."""
|
2021-12-16 13:45:37 +08:00
|
|
|
if isinstance(x, Sequence):
|
2019-04-19 14:09:44 -07:00
|
|
|
x = np.array(x) # Promote list to array for contains check
|
2022-09-03 22:56:29 +01:00
|
|
|
|
|
|
|
return bool(
|
|
|
|
isinstance(x, np.ndarray)
|
|
|
|
and self.shape == x.shape
|
|
|
|
and np.all((x == 0) | (x == 1))
|
|
|
|
)
|
2018-08-27 15:30:47 -07:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def to_jsonable(self, sample_n) -> list:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Convert a batch of samples from this space to a JSONable data type."""
|
2017-11-05 21:16:46 +03:00
|
|
|
return np.array(sample_n).tolist()
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def from_jsonable(self, sample_n) -> list:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Convert a JSONable data type to a batch of samples from this space."""
|
2022-09-03 22:56:29 +01:00
|
|
|
return [np.asarray(sample, self.dtype) for sample in sample_n]
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __repr__(self) -> str:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Gives a string representation of this space."""
|
2021-11-14 14:50:23 +01:00
|
|
|
return f"MultiBinary({self.n})"
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __eq__(self, other) -> bool:
|
2022-05-10 17:18:06 +02:00
|
|
|
"""Check whether `other` is equivalent to this instance."""
|
2019-03-23 23:18:19 -07:00
|
|
|
return isinstance(other, MultiBinary) and self.n == other.n
|