2022-01-24 23:22:11 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Optional, Union, Sequence
|
2017-02-04 23:36:32 -08:00
|
|
|
import numpy as np
|
2019-01-30 22:39:55 +01:00
|
|
|
from .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]):
|
2021-07-29 02:26:34 +02:00
|
|
|
"""
|
|
|
|
An n-shape binary space.
|
2020-01-24 21:42:41 -08:00
|
|
|
|
2020-09-26 06:43:15 +08:00
|
|
|
The argument to MultiBinary defines n, which could be a number or a `list` of numbers.
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2020-01-24 21:42:41 -08:00
|
|
|
Example Usage:
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2020-01-24 21:42:41 -08:00
|
|
|
>> self.observation_space = spaces.MultiBinary(5)
|
|
|
|
|
|
|
|
>> self.observation_space.sample()
|
|
|
|
|
2021-12-16 13:45:37 +08:00
|
|
|
array([0, 1, 0, 1, 0], dtype=int8)
|
2020-01-24 21:42:41 -08:00
|
|
|
|
2021-12-16 13:45:37 +08:00
|
|
|
>> self.observation_space = spaces.MultiBinary([3, 2])
|
2020-09-26 06:43:15 +08:00
|
|
|
|
|
|
|
>> self.observation_space.sample()
|
|
|
|
|
|
|
|
array([[0, 0],
|
2021-07-29 02:26:34 +02:00
|
|
|
[0, 1],
|
2020-09-26 06:43:15 +08:00
|
|
|
[1, 1]], dtype=int8)
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
"""
|
|
|
|
|
2022-01-24 23:22:11 +01:00
|
|
|
def __init__(
|
|
|
|
self, n: Union[np.ndarray, Sequence[int], int], seed: Optional[int] = None
|
|
|
|
):
|
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)
|
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,)
|
2021-12-16 13:45:37 +08:00
|
|
|
|
|
|
|
assert (np.asarray(input_n) > 0).all(), "n (counts) have to be positive"
|
|
|
|
|
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
|
|
|
|
def shape(self) -> tuple[int, ...]:
|
|
|
|
"""Has stricter type than gym.Space - never None."""
|
|
|
|
return self._shape # type: ignore
|
|
|
|
|
|
|
|
def sample(self) -> np.ndarray:
|
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:
|
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:
|
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:
|
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:
|
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:
|
2019-03-23 23:18:19 -07:00
|
|
|
return isinstance(other, MultiBinary) and self.n == other.n
|