2016-04-27 08:00:58 -07:00
|
|
|
import numpy as np
|
|
|
|
|
2016-06-14 18:57:47 -04:00
|
|
|
import gym, time
|
2016-05-30 18:07:59 -07:00
|
|
|
from gym.spaces import prng
|
|
|
|
|
|
|
|
class Discrete(gym.Space):
|
2016-04-27 08:00:58 -07:00
|
|
|
"""
|
|
|
|
{0,1,...,n-1}
|
2016-06-11 23:10:58 -07:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
self.observation_space = spaces.Discrete(2)
|
2016-04-27 08:00:58 -07:00
|
|
|
"""
|
2016-05-30 18:07:59 -07:00
|
|
|
def __init__(self, n):
|
2016-04-27 08:00:58 -07:00
|
|
|
self.n = n
|
|
|
|
def sample(self):
|
2016-05-30 18:07:59 -07:00
|
|
|
return prng.np_random.randint(self.n)
|
2016-04-27 08:00:58 -07:00
|
|
|
def contains(self, x):
|
2016-04-27 18:31:32 -07:00
|
|
|
if isinstance(x, int):
|
|
|
|
as_int = x
|
2016-05-03 22:27:06 -04:00
|
|
|
elif isinstance(x, (np.generic, np.ndarray)) and (x.dtype.kind in np.typecodes['AllInteger'] and x.shape == ()):
|
2016-04-27 18:31:32 -07:00
|
|
|
as_int = int(x)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return as_int >= 0 and as_int < self.n
|
2017-08-11 14:03:12 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def shape(self):
|
2017-11-05 21:16:46 +03:00
|
|
|
return (self.n,)
|
2016-04-27 08:00:58 -07:00
|
|
|
def __repr__(self):
|
|
|
|
return "Discrete(%d)" % self.n
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.n == other.n
|