mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-16 19:49:13 +00:00
* typing in spaces.Box and spaces.Discrete * adds typing to dict and tuple spaces * Typecheck all spaces * Explicit regex to include all files under space folder * Style: use native types and __future__ annotations * Allow only specific strings for Box.is_bounded args * Add typing to changes from #2517 * Remove Literal as it's not supported by py3.7 * Use more recent version of pyright * Avoid name clash for type checker * Revert "Avoid name clash for type checker" This reverts commit 1aaf3e0e0328171623a17a997b65fe734bc0afb1. * Ignore the error. It's reported as probable bug at https://github.com/microsoft/pyright/issues/2852 * rebase and add typing for `_short_repr`
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
from typing import Optional
|
|
|
|
import numpy as np
|
|
from .space import Space
|
|
|
|
|
|
class Discrete(Space[int]):
|
|
r"""A discrete space in :math:`\{ 0, 1, \\dots, n-1 \}`.
|
|
|
|
A start value can be optionally specified to shift the range
|
|
to :math:`\{ a, a+1, \\dots, a+n-1 \}`.
|
|
|
|
Example::
|
|
|
|
>>> Discrete(2)
|
|
>>> Discrete(3, start=-1) # {-1, 0, 1}
|
|
|
|
"""
|
|
|
|
def __init__(self, n: int, seed: Optional[int] = None, start: int = 0):
|
|
assert n > 0, "n (counts) have to be positive"
|
|
assert isinstance(start, (int, np.integer))
|
|
self.n = int(n)
|
|
self.start = int(start)
|
|
super().__init__((), np.int64, seed)
|
|
|
|
def sample(self) -> int:
|
|
return self.start + self.np_random.randint(self.n)
|
|
|
|
def contains(self, x) -> bool:
|
|
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) # type: ignore
|
|
else:
|
|
return False
|
|
return self.start <= as_int < self.start + self.n
|
|
|
|
def __repr__(self) -> str:
|
|
if self.start != 0:
|
|
return "Discrete(%d, start=%d)" % (self.n, self.start)
|
|
return "Discrete(%d)" % self.n
|
|
|
|
def __eq__(self, other) -> bool:
|
|
return (
|
|
isinstance(other, Discrete)
|
|
and self.n == other.n
|
|
and self.start == other.start
|
|
)
|