mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-16 11:39:13 +00:00
* Make the seed in default initialization controllable Since seed() is being called in default initialization of Space, it should be controllable for reproducibility. * Updated derived classes of Space to have their seeds controllable at initialization. * Allow Tuple's spaces to each have their own seed * Added dict based seeding for Dict space; test cases for Tuple and Dict seeding * Update discrete.py * Update test_spaces.py * Add seed to __init__() * blacked * Fix black * Fix failing tests
38 lines
885 B
Python
38 lines
885 B
Python
import numpy as np
|
|
from .space import Space
|
|
|
|
|
|
class Discrete(Space):
|
|
r"""A discrete space in :math:`\{ 0, 1, \\dots, n-1 \}`.
|
|
|
|
Example::
|
|
|
|
>>> Discrete(2)
|
|
|
|
"""
|
|
|
|
def __init__(self, n, seed=None):
|
|
assert n >= 0
|
|
self.n = n
|
|
super(Discrete, self).__init__((), np.int64, seed)
|
|
|
|
def sample(self):
|
|
return self.np_random.randint(self.n)
|
|
|
|
def contains(self, x):
|
|
if isinstance(x, int):
|
|
as_int = x
|
|
elif isinstance(x, (np.generic, np.ndarray)) and (
|
|
x.dtype.char in np.typecodes["AllInteger"] and x.shape == ()
|
|
):
|
|
as_int = int(x)
|
|
else:
|
|
return False
|
|
return as_int >= 0 and as_int < self.n
|
|
|
|
def __repr__(self):
|
|
return "Discrete(%d)" % self.n
|
|
|
|
def __eq__(self, other):
|
|
return isinstance(other, Discrete) and self.n == other.n
|