def _space2vt()

in gym3/interop.py [0:0]


def _space2vt(space: "gym.spaces.Space"):
    from gym import spaces

    if isinstance(space, spaces.Box):
        if space.dtype.name in INTEGER_DTYPE_NAMES:
            high = space.high.flat[0]
            assert (space.low == 0).all() and (
                space.high == high
            ).all(), "only identical high/low bounds across all dimensions are supported, and low must be 0 for integer types, please wrap your environment to adjust your Box space bounds, split into a Dict space, or use float32 as the dtype"
            return TensorType(
                shape=space.shape,
                eltype=Discrete(n=high + 1, dtype_name=space.dtype.name),
            )
        else:
            assert (
                space.dtype.name in FLOAT_DTYPE_NAMES
            ), f"only {FLOAT_DTYPE_NAMES} is supported for real values, please wrap your environment so that a valid dtype is used"
            return TensorType(shape=space.shape, eltype=Real(dtype_name=space.dtype.name))
    elif isinstance(space, spaces.Discrete):
        return TensorType(
            shape=(), eltype=Discrete(space.n, dtype_name=space.dtype.name)
        )
    elif isinstance(space, spaces.MultiDiscrete):
        assert misc.allsame(
            space.nvec
        ), f"only multidiscrete with identical values of n is allowed, please wrap your environment so that it has a Dict space with individual Discrete spaces for each dimension instead"
        return TensorType(
            shape=(len(space.nvec),),
            eltype=Discrete(space.nvec[0], dtype_name=space.dtype.name),
        )
    elif isinstance(space, spaces.MultiBinary):
        return TensorType(
            shape=(space.n,), eltype=Discrete(2, dtype_name=space.dtype.name)
        )
    elif isinstance(space, spaces.Dict):
        return DictType(
            **{name: _space2vt(subspace) for (name, subspace) in space.spaces.items()}
        )
    elif isinstance(space, spaces.Tuple):
        assert (
            False
        ), "tuple space not supported, please wrap your environment so that it has a Dict space instead of a Tuple space"
    else:
        raise NotImplementedError