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-04-24 17:14:33 +01:00
|
|
|
from gym.spaces.space import Space
|
|
|
|
from gym.utils import seeding
|
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-05-25 15:28:19 +01:00
|
|
|
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:`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-01-24 23:22:11 +01:00
|
|
|
"""Has stricter type than gym.Space - never None."""
|
|
|
|
return self._shape # type: ignore
|
|
|
|
|
|
|
|
def sample(self) -> 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
|
|
|
|
|
|
|
Returns:
|
|
|
|
Sampled values from space
|
2022-05-10 17:18:06 +02: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
|
2020-09-26 06:43:15 +08:00
|
|
|
if self.shape != x.shape:
|
|
|
|
return False
|
2021-07-29 02:26:34 +02:00
|
|
|
return ((x == 0) | (x == 1)).all()
|
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."""
|
2018-08-27 15:30:47 -07:00
|
|
|
return [np.asarray(sample) 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
|