fix circular import (py2) by not importing spaces (#858)

This commit is contained in:
John Schulman
2018-02-02 23:01:45 -08:00
committed by GitHub
parent 21cc13a625
commit c21f09fb28
6 changed files with 22 additions and 22 deletions

View File

@@ -1,7 +1,8 @@
import numpy as np
from gym import Space, spaces, logger
import gym
from gym import logger
class Box(Space):
class Box(gym.Space):
"""
A box in R^n.
I.e., each coordinate is bounded.
@@ -30,10 +31,10 @@ class Box(Space):
logger.warn("gym.spaces.Box autodetected dtype as %s. Please provide explicit dtype." % dtype)
self.low = low.astype(dtype)
self.high = high.astype(dtype)
Space.__init__(self, shape, dtype)
gym.Space.__init__(self, shape, dtype)
def sample(self):
return spaces.np_random.uniform(low=self.low, high=self.high + (0 if self.dtype.kind == 'f' else 1), size=self.low.shape).astype(self.dtype)
return gym.spaces.np_random.uniform(low=self.low, high=self.high + (0 if self.dtype.kind == 'f' else 1), size=self.low.shape).astype(self.dtype)
def contains(self, x):
return x.shape == self.shape and (x >= self.low).all() and (x <= self.high).all()

View File

@@ -1,7 +1,7 @@
from gym import Space
import gym
from collections import OrderedDict
class Dict(Space):
class Dict(gym.Space):
"""
A dictionary of simpler spaces.
@@ -36,7 +36,7 @@ class Dict(Space):
if isinstance(spaces, list):
spaces = OrderedDict(spaces)
self.spaces = spaces
Space.__init__(self, None, None) # None for shape and dtype, since it'll require special handling
gym.Space.__init__(self, None, None) # None for shape and dtype, since it'll require special handling
def sample(self):
return OrderedDict([(k, space.sample()) for k, space in self.spaces.items()])

View File

@@ -1,7 +1,7 @@
import numpy as np
from gym import Space, spaces
import gym
class Discrete(Space):
class Discrete(gym.Space):
"""
{0,1,...,n-1}
@@ -10,9 +10,9 @@ class Discrete(Space):
"""
def __init__(self, n):
self.n = n
Space.__init__(self, (), np.int64)
gym.Space.__init__(self, (), np.int64)
def sample(self):
return spaces.np_random.randint(self.n)
return gym.spaces.np_random.randint(self.n)
def contains(self, x):
if isinstance(x, int):
as_int = x

View File

@@ -1,12 +1,12 @@
from gym import spaces, Space
import gym
import numpy as np
class MultiBinary(Space):
class MultiBinary(gym.Space):
def __init__(self, n):
self.n = n
Space.__init__(self, (self.n,), np.int8)
gym.Space.__init__(self, (self.n,), np.int8)
def sample(self):
return spaces.np_random.randint(low=0, high=2, size=self.n).astype(self.dtype)
return gym.spaces.np_random.randint(low=0, high=2, size=self.n).astype(self.dtype)
def contains(self, x):
return ((x==0) | (x==1)).all()
def to_jsonable(self, sample_n):

View File

@@ -1,17 +1,16 @@
import gym
from gym import spaces, Space
import numpy as np
class MultiDiscrete(Space):
class MultiDiscrete(gym.Space):
def __init__(self, nvec):
"""
nvec: vector of counts of each categorical variable
"""
self.nvec = np.asarray(nvec, dtype=np.int32)
assert self.nvec.ndim == 1, 'nvec should be a 1d array (or list) of ints'
Space.__init__(self, (self.nvec.size,), np.int8)
gym.Space.__init__(self, (self.nvec.size,), np.int8)
def sample(self):
return (spaces.np_random.rand(self.nvec.size) * self.nvec).astype(self.dtype)
return (gym.spaces.np_random.rand(self.nvec.size) * self.nvec).astype(self.dtype)
def contains(self, x):
return (x < self.nvec).all() and x.dtype.kind in 'ui'
def to_jsonable(self, sample_n):

View File

@@ -1,6 +1,6 @@
from gym import Space
import gym
class Tuple(Space):
class Tuple(gym.Space):
"""
A tuple (i.e., product) of simpler spaces
@@ -9,7 +9,7 @@ class Tuple(Space):
"""
def __init__(self, spaces):
self.spaces = spaces
Space.__init__(self, None, None)
gym.Space.__init__(self, None, None)
def sample(self):
return tuple([space.sample() for space in self.spaces])