Skip to content

schola.core.spaces.dict.DictSpace

Class Definition

class schola.core.spaces.dict.DictSpace(space_dict=None)

Bases: Dict

A Space representing a dictionary of spaces.

See also:

  • gymnasium.spaces.Dict – The gym space object that this class is analogous to
  • proto_spaces.DictSpace – The protobuf representation of this space

Parameters

space_dict

Type: Dict[str, gymnasium.spaces.Space]
The dictionary of spaces to be represented.

Attributes

spaces

Type: Dict[str, gymnasium.spaces.Space]
The dictionary of spaces represented by this object.

has_only_one_fundamental_type

Type: bool (property)
Check if all the subspaces in the dictionary space are of the same fundamental type.

is_np_flattenable

Checks whether this space can be flattened to aspaces.Box.

np_random

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

shape

Return the shape of the space as an immutable property.

shapes

Type: Dict[str, Tuple[int]] (property)
Get the shapes of the subspaces in the dictionary space.

Methods

__init__

__init__(space_dict=None)

Constructor of Dict space.

This space can be instantiated in one of two ways: Either you pass a dictionary of spaces to __init__() via the spaces argument, or you pass the spaces as separate keyword arguments (where you will need to avoid the keys spaces and seed)

Parameters:

  • spaces – A dictionary of spaces. This specifies the structure of the Dict space
  • seed – Optionally, you can use this argument to seed the RNGs of the spaces that make up the Dict space
  • **spaces_kwargs – If spaces is None, you need to pass the constituent spaces as keyword arguments, as described above

contains

contains(x)

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

fill_proto

fill_proto(msg, action)

Parameters:

  • msg (DictPoint)

from_jsonable

from_jsonable(sample_n)

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

from_proto

@classmethod
from_proto(message)

get

get(k, d=None)

items

items()

keys

keys()

Returns the keys of the Dict.

process_data

process_data(msg)

Parameters:

  • msg (DictPoint)

sample

sample(mask=None)

Generates a single random sample from this space.

seed

seed(seed=None)

Seed the PRNG of this space and all subspaces.

simplify

simplify()

Simplify the dictionary space by merging subspaces of the same fundamental type, if possible.

Returns: The simplified space

Return type: gymnasium.spaces.Space

Examples:

>>> space = DictSpace({"a": BoxSpace([0,0],[2,2]), "b": BoxSpace([0,0],[2,2])})
>>> space.simplify()
Box(0.0, 2.0, (4,), float32)
>>> space = DictSpace({"a": DiscreteSpace(4), "b": BoxSpace([0,0],[2,2])})
>>> space.simplify()
Dict('a': Discrete(4), 'b': Box(0.0, 2.0, (2,), float32))
>>> space = DictSpace({"a": DiscreteSpace(4)})
>>> space.simplify()
Discrete(4)

to_jsonable

to_jsonable(sample_n)

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

to_normalized

to_normalized()

Normalize this dictionary space by normalizing all of the subspaces in this dictionary space.

Returns: The normalized dictionary space. A modified version of the space this method is called on

Return type: DictSpace

Example:

>>> space = DictSpace({"a": BoxSpace([0,0],[2,2]), "b": DiscreteSpace(3)})
>>> space.to_normalized()
Dict('a': Box(0.0, 2.0, (2,), float32), 'b': Discrete(3))

values

values()

Properties Documentation

has_only_one_fundamental_type

Check if all the subspaces in the dictionary space are of the same fundamental type.

Returns: True if all the subspaces are of the same fundamental type, False otherwise

Return type: bool

Examples:

>>> space = DictSpace({"a": BoxSpace([0,0],[2,2]), "b": DiscreteSpace(3)})
>>> space.has_only_one_fundamental_type
False
>>> space = DictSpace({"a": BoxSpace([0,0],[2,2]), "b": BoxSpace([0,0],[2,2])})
>>> space.has_only_one_fundamental_type
True
>>> space = DictSpace({"a": DiscreteSpace(3), "b": MultiDiscreteSpace([3,3])})
>>> space.has_only_one_fundamental_type
True

shapes

Get the shapes of the subspaces in the dictionary space.

Returns: A dictionary of the shapes of the subspaces in the dictionary space

Return type: Dict[str, Tuple[int]]

Example:

>>> space = DictSpace({"a": BoxSpace(0, 1, shape=(2,)), "b": DiscreteSpace(3)})
>>> space.shapes
{'a': 2, 'b': 1}