mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-19 13:32:03 +00:00
Type cast in spaces
families (#2491)
* Type cast for `spaces.Dict` * Type cast for `spaces.Tuple` * Type cast for `spaces.Discrete` * Type cast for `spaces.MultiDiscrete` * Type cast for `spaces.MultiBinary`
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from collections import OrderedDict
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Mapping, Sequence
|
||||
import numpy as np
|
||||
from .space import Space
|
||||
|
||||
@@ -42,9 +42,15 @@ class Dict(Space, Mapping):
|
||||
if spaces is None:
|
||||
spaces = spaces_kwargs
|
||||
if isinstance(spaces, dict) and not isinstance(spaces, OrderedDict):
|
||||
spaces = OrderedDict(sorted(list(spaces.items())))
|
||||
if isinstance(spaces, list):
|
||||
try:
|
||||
spaces = OrderedDict(sorted(spaces.items()))
|
||||
except TypeError: # raise when sort by different types of keys
|
||||
spaces = OrderedDict(spaces.items())
|
||||
if isinstance(spaces, Sequence):
|
||||
spaces = OrderedDict(spaces)
|
||||
|
||||
assert isinstance(spaces, OrderedDict), "spaces must be a dictionary"
|
||||
|
||||
self.spaces = spaces
|
||||
for space in spaces.values():
|
||||
assert isinstance(
|
||||
|
@@ -16,8 +16,9 @@ class Discrete(Space):
|
||||
"""
|
||||
|
||||
def __init__(self, n, seed=None, start=0):
|
||||
assert n >= 0 and isinstance(start, (int, np.integer))
|
||||
self.n = n
|
||||
assert n > 0, "n (counts) have to be positive"
|
||||
assert isinstance(start, (int, np.integer))
|
||||
self.n = int(n)
|
||||
self.start = int(start)
|
||||
super().__init__((), np.int64, seed)
|
||||
|
||||
|
@@ -1,3 +1,4 @@
|
||||
from collections.abc import Sequence
|
||||
import numpy as np
|
||||
from .space import Space
|
||||
|
||||
@@ -27,18 +28,21 @@ class MultiBinary(Space):
|
||||
"""
|
||||
|
||||
def __init__(self, n, seed=None):
|
||||
self.n = n
|
||||
if type(n) in [tuple, list, np.ndarray]:
|
||||
input_n = n
|
||||
if isinstance(n, (Sequence, np.ndarray)):
|
||||
self.n = input_n = tuple(int(i) for i in n)
|
||||
else:
|
||||
self.n = n = int(n)
|
||||
input_n = (n,)
|
||||
|
||||
assert (np.asarray(input_n) > 0).all(), "n (counts) have to be positive"
|
||||
|
||||
super().__init__(input_n, np.int8, seed)
|
||||
|
||||
def sample(self):
|
||||
return self.np_random.integers(low=0, high=2, size=self.n, dtype=self.dtype)
|
||||
|
||||
def contains(self, x):
|
||||
if isinstance(x, list) or isinstance(x, tuple):
|
||||
if isinstance(x, Sequence):
|
||||
x = np.array(x) # Promote list to array for contains check
|
||||
if self.shape != x.shape:
|
||||
return False
|
||||
|
@@ -1,3 +1,4 @@
|
||||
from collections.abc import Sequence
|
||||
import numpy as np
|
||||
from gym import logger
|
||||
from .space import Space
|
||||
@@ -29,8 +30,8 @@ class MultiDiscrete(Space):
|
||||
"""
|
||||
nvec: vector of counts of each categorical variable
|
||||
"""
|
||||
assert (np.array(nvec) > 0).all(), "nvec (counts) have to be positive"
|
||||
self.nvec = np.asarray(nvec, dtype=dtype)
|
||||
self.nvec = np.array(nvec, dtype=dtype, copy=True)
|
||||
assert (self.nvec > 0).all(), "nvec (counts) have to be positive"
|
||||
|
||||
super().__init__(self.nvec.shape, dtype, seed)
|
||||
|
||||
@@ -38,7 +39,7 @@ class MultiDiscrete(Space):
|
||||
return (self.np_random.random(self.nvec.shape) * self.nvec).astype(self.dtype)
|
||||
|
||||
def contains(self, x):
|
||||
if isinstance(x, list):
|
||||
if isinstance(x, Sequence):
|
||||
x = np.array(x) # Promote list to array for contains check
|
||||
# if nvec is uint32 and space dtype is uint32, then 0 <= x < self.nvec guarantees that x
|
||||
# is within correct bounds for space dtype (even though x does not have to be unsigned)
|
||||
|
@@ -11,6 +11,7 @@ class Tuple(Space):
|
||||
"""
|
||||
|
||||
def __init__(self, spaces, seed=None):
|
||||
spaces = tuple(spaces)
|
||||
self.spaces = spaces
|
||||
for space in spaces:
|
||||
assert isinstance(
|
||||
@@ -53,8 +54,8 @@ class Tuple(Space):
|
||||
return tuple(space.sample() for space in self.spaces)
|
||||
|
||||
def contains(self, x):
|
||||
if isinstance(x, list):
|
||||
x = tuple(x) # Promote list to tuple for contains check
|
||||
if isinstance(x, (list, np.ndarray)):
|
||||
x = tuple(x) # Promote list and ndarray to tuple for contains check
|
||||
return (
|
||||
isinstance(x, tuple)
|
||||
and len(x) == len(self.spaces)
|
||||
|
Reference in New Issue
Block a user