Files
Gymnasium/gym/spaces/discrete.py
Alok Singh 6332d4f113 rm unnecessary __contains__ duplicate code (#1147)
`contains` really should not exist when it does exactly what the builtin
magic method `__contains__` was meant for, but that would break backward
compatibility.
2018-08-28 10:51:28 -07:00

29 lines
769 B
Python

import numpy as np
import gym
class Discrete(gym.Space):
"""
{0,1,...,n-1}
Example usage:
self.observation_space = spaces.Discrete(2)
"""
def __init__(self, n):
self.n = n
gym.Space.__init__(self, (), np.int64)
def sample(self):
return gym.spaces.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