mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-22 15:11:51 +00:00
fix circular import (py2) by not importing spaces (#858)
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import numpy as np
|
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.
|
A box in R^n.
|
||||||
I.e., each coordinate is bounded.
|
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)
|
logger.warn("gym.spaces.Box autodetected dtype as %s. Please provide explicit dtype." % dtype)
|
||||||
self.low = low.astype(dtype)
|
self.low = low.astype(dtype)
|
||||||
self.high = high.astype(dtype)
|
self.high = high.astype(dtype)
|
||||||
Space.__init__(self, shape, dtype)
|
gym.Space.__init__(self, shape, dtype)
|
||||||
|
|
||||||
def sample(self):
|
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):
|
def contains(self, x):
|
||||||
return x.shape == self.shape and (x >= self.low).all() and (x <= self.high).all()
|
return x.shape == self.shape and (x >= self.low).all() and (x <= self.high).all()
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
from gym import Space
|
import gym
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
class Dict(Space):
|
class Dict(gym.Space):
|
||||||
"""
|
"""
|
||||||
A dictionary of simpler spaces.
|
A dictionary of simpler spaces.
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ class Dict(Space):
|
|||||||
if isinstance(spaces, list):
|
if isinstance(spaces, list):
|
||||||
spaces = OrderedDict(spaces)
|
spaces = OrderedDict(spaces)
|
||||||
self.spaces = 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):
|
def sample(self):
|
||||||
return OrderedDict([(k, space.sample()) for k, space in self.spaces.items()])
|
return OrderedDict([(k, space.sample()) for k, space in self.spaces.items()])
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from gym import Space, spaces
|
import gym
|
||||||
|
|
||||||
class Discrete(Space):
|
class Discrete(gym.Space):
|
||||||
"""
|
"""
|
||||||
{0,1,...,n-1}
|
{0,1,...,n-1}
|
||||||
|
|
||||||
@@ -10,9 +10,9 @@ class Discrete(Space):
|
|||||||
"""
|
"""
|
||||||
def __init__(self, n):
|
def __init__(self, n):
|
||||||
self.n = n
|
self.n = n
|
||||||
Space.__init__(self, (), np.int64)
|
gym.Space.__init__(self, (), np.int64)
|
||||||
def sample(self):
|
def sample(self):
|
||||||
return spaces.np_random.randint(self.n)
|
return gym.spaces.np_random.randint(self.n)
|
||||||
def contains(self, x):
|
def contains(self, x):
|
||||||
if isinstance(x, int):
|
if isinstance(x, int):
|
||||||
as_int = x
|
as_int = x
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
from gym import spaces, Space
|
import gym
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
class MultiBinary(Space):
|
class MultiBinary(gym.Space):
|
||||||
def __init__(self, n):
|
def __init__(self, n):
|
||||||
self.n = n
|
self.n = n
|
||||||
Space.__init__(self, (self.n,), np.int8)
|
gym.Space.__init__(self, (self.n,), np.int8)
|
||||||
def sample(self):
|
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):
|
def contains(self, x):
|
||||||
return ((x==0) | (x==1)).all()
|
return ((x==0) | (x==1)).all()
|
||||||
def to_jsonable(self, sample_n):
|
def to_jsonable(self, sample_n):
|
||||||
|
@@ -1,17 +1,16 @@
|
|||||||
import gym
|
import gym
|
||||||
from gym import spaces, Space
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
class MultiDiscrete(Space):
|
class MultiDiscrete(gym.Space):
|
||||||
def __init__(self, nvec):
|
def __init__(self, nvec):
|
||||||
"""
|
"""
|
||||||
nvec: vector of counts of each categorical variable
|
nvec: vector of counts of each categorical variable
|
||||||
"""
|
"""
|
||||||
self.nvec = np.asarray(nvec, dtype=np.int32)
|
self.nvec = np.asarray(nvec, dtype=np.int32)
|
||||||
assert self.nvec.ndim == 1, 'nvec should be a 1d array (or list) of ints'
|
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):
|
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):
|
def contains(self, x):
|
||||||
return (x < self.nvec).all() and x.dtype.kind in 'ui'
|
return (x < self.nvec).all() and x.dtype.kind in 'ui'
|
||||||
def to_jsonable(self, sample_n):
|
def to_jsonable(self, sample_n):
|
||||||
|
@@ -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
|
A tuple (i.e., product) of simpler spaces
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ class Tuple(Space):
|
|||||||
"""
|
"""
|
||||||
def __init__(self, spaces):
|
def __init__(self, spaces):
|
||||||
self.spaces = spaces
|
self.spaces = spaces
|
||||||
Space.__init__(self, None, None)
|
gym.Space.__init__(self, None, None)
|
||||||
|
|
||||||
def sample(self):
|
def sample(self):
|
||||||
return tuple([space.sample() for space in self.spaces])
|
return tuple([space.sample() for space in self.spaces])
|
||||||
|
Reference in New Issue
Block a user