Skip to content

Commit

Permalink
fix circular import (py2) by not importing spaces (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschu committed Feb 3, 2018
1 parent 21cc13a commit c21f09f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
9 changes: 5 additions & 4 deletions gym/spaces/box.py
@@ -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.
Expand Down Expand Up @@ -30,10 +31,10 @@ def __init__(self, low=None, high=None, shape=None, dtype=None):
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()

Expand Down
6 changes: 3 additions & 3 deletions gym/spaces/dict_space.py
@@ -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.
Expand Down Expand Up @@ -36,7 +36,7 @@ def __init__(self, spaces):
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()])
Expand Down
8 changes: 4 additions & 4 deletions gym/spaces/discrete.py
@@ -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}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions gym/spaces/multi_binary.py
@@ -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):
Expand Down
7 changes: 3 additions & 4 deletions gym/spaces/multi_discrete.py
@@ -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):
Expand Down
6 changes: 3 additions & 3 deletions gym/spaces/tuple_space.py
@@ -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
Expand All @@ -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])
Expand Down

0 comments on commit c21f09f

Please sign in to comment.