in gym3/libenv.py [0:0]
def _get_space(self, c_name: Any) -> Tuple[types.DictType, List[Spec]]:
"""
Get a c space and convert to a gym space
"""
count = self._c_lib.libenv_get_tensortypes(self._c_env, c_name, self._ffi.NULL)
if count == 0:
return types.DictType(), []
c_tensortypes = self._ffi.new("struct libenv_tensortype[%d]" % count)
self._c_lib.libenv_get_tensortypes(self._c_env, c_name, c_tensortypes)
# convert to gym3 types
name_to_tensortype = {}
specs = []
for i in range(count):
c_tt = c_tensortypes[i]
name = self._ffi.string(c_tt.name).decode("utf8")
shape = tuple(c_tt.shape[j] for j in range(c_tt.ndim))
if c_tt.scalar_type == self._c_lib.LIBENV_SCALAR_TYPE_REAL:
if c_tt.dtype == self._c_lib.LIBENV_DTYPE_FLOAT32:
dtype = np.dtype("float32")
low = c_tt.low.float32
high = c_tt.high.float32
else:
assert False, "unrecognized dtype for real"
tensortype = types.TensorType(eltype=types.Real(), shape=shape)
elif c_tt.scalar_type == self._c_lib.LIBENV_SCALAR_TYPE_DISCRETE:
if c_tt.dtype == self._c_lib.LIBENV_DTYPE_UINT8:
dtype = np.dtype("uint8")
low = c_tt.low.uint8
high = c_tt.high.uint8
elif c_tt.dtype == self._c_lib.LIBENV_DTYPE_INT32:
dtype = np.dtype("int32")
low = c_tt.low.int32
high = c_tt.high.int32
else:
assert False, "unrecognized dtype for discrete"
assert low == 0 and high >= 0, "discrete low/high bounds are incorrect"
tensortype = types.TensorType(
eltype=types.Discrete(n=high + 1, dtype_name=dtype.name),
shape=shape,
)
else:
assert False, "unknown space type"
name_to_tensortype[name] = tensortype
specs.append(Spec(name=name, shape=tensortype.shape, dtype=dtype))
return types.DictType(**name_to_tensortype), specs