Files
Gymnasium/gym/spaces/discrete.py
Greg Brockman a20719c8e3 Revert "Feature/len (#68)"
This reverts commit 705d024993.
2016-05-09 18:34:44 -07:00

24 lines
649 B
Python

import numpy as np
from gym import Space
class Discrete(Space):
"""
{0,1,...,n-1}
"""
def __init__(self, n):
self.n = n
def sample(self):
return 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.kind 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 self.n == other.n