rlberry.spaces.Dict

class rlberry.spaces.Dict(spaces=None, **spaces_kwargs)[source]

Bases: Dict

Inherited from gymnasium.spaces.Dict for compatibility with gym.

rlberry wraps gym.spaces to make sure the seeding mechanism is unified in the library (rlberry.seeding)

Attributes:
rngnumpy.random._generator.Generator

random number generator provided by rlberry.seeding

Methods

reseed()

get new random number generator

contains(x: Any) bool[source]

Return boolean specifying if x is a valid member of this space.

from_jsonable(sample_n: dict[str, list[Any]]) list[dict[str, Any]][source]

Convert a JSONable data type to a batch of samples from this space.

get(k[, d]) D[k] if k in D, else d.  d defaults to None.
property is_np_flattenable

Checks whether this space can be flattened to a spaces.Box.

items() a set-like object providing a view on D's items
keys() KeysView[source]

Returns the keys of the Dict.

property np_random: Generator

Lazily seed the PRNG since this is expensive and only needed if sampling from this space.

As seed() is not guaranteed to set the _np_random for particular seeds. We add a check after seed() to set a new random number generator.

sample(mask: dict[str, Any] | None = None) dict[str, Any][source]

Generates a single random sample from this space.

The sample is an ordered dictionary of independent samples from the constituent spaces.

Args:

mask: An optional mask for each of the subspaces, expects the same keys as the space

Returns:

A dictionary with the same key and sampled values from self.spaces

seed(seed: int | dict[str, Any] | None = None) dict[str, int][source]

Seed the PRNG of this space and all subspaces.

Depending on the type of seed, the subspaces will be seeded differently

  • None - All the subspaces will use a random initial seed

  • Int - The integer is used to seed the Dict space that is used to generate seed values for each of the subspaces. Warning, this does not guarantee unique seeds for all subspaces, though is very unlikely.

  • Dict - A dictionary of seeds for each subspace, requires a seed key for every subspace. This supports seeding of multiple composite subspaces (Dict["space": Dict[...], ...] with {"space": {...}, ...}).

Args:

seed: An optional int or dictionary of subspace keys to int to seed each PRNG. See above for more details.

Returns:

A dictionary for the seed values of the subspaces

property shape: tuple[int, ...] | None

Return the shape of the space as an immutable property.

to_jsonable(sample_n: Sequence[dict[str, Any]]) dict[str, list[Any]][source]

Convert a batch of samples from this space to a JSONable data type.

values() an object providing a view on D's values