Extended Gymnasium spaces with state tracking and constraint support


Discrete

Discrete(
    n: int,
    init_value: int | None = None,
    constrain_fn: Callable[[int], bool] | None = None,
    **kwargs: Any,
)

Bases: Discrete

Extended discrete space with state tracking and constraint support.

Parameters:

  • n (int) –

    Number of elements in the space.

  • init_value (int | None, default: None ) –

    Initial value for the space.

  • constrain_fn (Callable[[int], bool] | None, default: None ) –

    Optional predicate function for rejection sampling.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium.spaces.Discrete.

sample

sample(
    mask: Any | None = None,
    max_tries: int = 1000,
    warn_after_s: float | None = 5.0,
    set_value: bool = True,
    **kwargs: Any,
) -> int

Sample a random value using rejection sampling for constraints.

Parameters:

  • mask (Any | None, default: None ) –

    Optional mask for sampling.

  • max_tries (int, default: 1000 ) –

    Maximum number of rejection sampling attempts.

  • warn_after_s (float | None, default: 5.0 ) –

    Log a warning if sampling takes longer than this.

  • set_value (bool, default: True ) –

    Whether to update the current value with the sample.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium sample.

Returns:

  • int

    A randomly sampled value satisfying constraints.

Raises:

  • RuntimeError

    If no valid sample is found within max_tries.

contains

contains(x: Any) -> bool

Check if value is valid and satisfies constraints.

Parameters:

  • x (Any) –

    Value to check.

Returns:

  • bool

    True if value is valid and satisfies constraints, False otherwise.

reset

reset() -> None

Reset the space value to its initial value.

MultiDiscrete

MultiDiscrete(
    nvec: Any,
    init_value: Any | None = None,
    constrain_fn: Callable[[Any], bool] | None = None,
    **kwargs: Any,
)

Bases: MultiDiscrete

Extended multi-discrete space with state tracking and constraint support.

Parameters:

  • nvec (Any) –

    Vector of number of elements for each dimension.

  • init_value (Any | None, default: None ) –

    Initial values for the space.

  • constrain_fn (Callable[[Any], bool] | None, default: None ) –

    Optional predicate function for rejection sampling.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium.spaces.MultiDiscrete.

sample

sample(
    mask: Any | None = None,
    max_tries: int = 1000,
    warn_after_s: float | None = 5.0,
    set_value: bool = True,
    **kwargs: Any,
) -> Any

Sample random values using rejection sampling for constraints.

Parameters:

  • mask (Any | None, default: None ) –

    Optional mask for sampling.

  • max_tries (int, default: 1000 ) –

    Maximum number of rejection sampling attempts.

  • warn_after_s (float | None, default: 5.0 ) –

    Log a warning if sampling takes longer than this.

  • set_value (bool, default: True ) –

    Whether to update the current value with the sample.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium sample.

Returns:

  • Any

    Randomly sampled values satisfying constraints.

Raises:

  • RuntimeError

    If no valid sample is found within max_tries.

contains

contains(x: Any) -> bool

Check if values are valid and satisfy constraints.

Parameters:

  • x (Any) –

    Values to check.

Returns:

  • bool

    True if values are valid and satisfy constraints, False otherwise.

reset

reset() -> None

Reset the space values to their initial values.

Box

Box(
    low: Any,
    high: Any,
    shape: Iterable[int] | None = None,
    init_value: Any | None = None,
    constrain_fn: Callable[[Any], bool] | None = None,
    **kwargs: Any,
)

Bases: Box

Extended continuous box space with state tracking and constraint support.

Parameters:

  • low (Any) –

    Lower bounds of the space.

  • high (Any) –

    Upper bounds of the space.

  • shape (Iterable[int] | None, default: None ) –

    Optional shape of the space.

  • init_value (Any | None, default: None ) –

    Initial values for the space.

  • constrain_fn (Callable[[Any], bool] | None, default: None ) –

    Optional predicate function for rejection sampling.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium.spaces.Box.

sample

sample(
    mask: Any | None = None,
    max_tries: int = 1000,
    warn_after_s: float | None = 5.0,
    set_value: bool = True,
    **kwargs: Any,
) -> Any

Sample a random value using rejection sampling for constraints.

Parameters:

  • mask (Any | None, default: None ) –

    Optional mask for sampling.

  • max_tries (int, default: 1000 ) –

    Maximum number of rejection sampling attempts.

  • warn_after_s (float | None, default: 5.0 ) –

    Log a warning if sampling takes longer than this.

  • set_value (bool, default: True ) –

    Whether to update the current value with the sample.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium sample.

Returns:

  • Any

    A randomly sampled value satisfying constraints.

Raises:

  • RuntimeError

    If no valid sample is found within max_tries.

contains

contains(x: Any) -> bool

Check if value is valid and satisfies constraints.

Parameters:

  • x (Any) –

    Value to check.

Returns:

  • bool

    True if value is valid and satisfies constraints, False otherwise.

reset

reset() -> None

Reset the space value to its initial value.

RGBBox

RGBBox(
    shape: Iterable[int] = (3,),
    init_value: Any | None = None,
    **kwargs: Any,
)

Bases: Box

Specialized box space for RGB image data.

Parameters:

  • shape (Iterable[int], default: (3,) ) –

    Shape of the image (must have a channel of size 3).

  • init_value (Any | None, default: None ) –

    Initial value for the space.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to Box.

Raises:

  • ValueError

    If shape does not have a channel of size 3.

Dict

Dict(
    spaces_dict: dict[Any, Space] | None = None,
    init_value: dict | None = None,
    constrain_fn: Callable[[dict], bool] | None = None,
    sampling_order: list[str] | None = None,
    **kwargs: Any,
)

Bases: Dict

Extended dictionary space with ordered sampling and nested support.

Parameters:

  • spaces_dict (dict[Any, Space] | None, default: None ) –

    Dictionary mapping keys to Gymnasium spaces.

  • init_value (dict | None, default: None ) –

    Initial values for the contained spaces.

  • constrain_fn (Callable[[dict], bool] | None, default: None ) –

    Optional predicate function for rejection sampling.

  • sampling_order (list[str] | None, default: None ) –

    Explicit order for sampling keys.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to gymnasium.spaces.Dict.

sample

sample(
    mask: Any | None = None,
    max_tries: int = 1000,
    warn_after_s: float | None = 5.0,
    set_value: bool = True,
    **kwargs: Any,
) -> dict

Sample a random element from the Dict space.

Parameters:

  • mask (Any | None, default: None ) –

    Optional mask for sampling.

  • max_tries (int, default: 1000 ) –

    Maximum number of rejection sampling attempts.

  • warn_after_s (float | None, default: 5.0 ) –

    Log a warning if sampling takes longer than this.

  • set_value (bool, default: True ) –

    Whether to update the current value with the sample.

  • **kwargs (Any, default: {} ) –

    Additional arguments passed to sample.

Returns:

  • dict

    A randomly sampled dictionary satisfying constraints.

Raises:

  • RuntimeError

    If no valid sample is found within max_tries.

contains

contains(x: Any) -> bool

Check if value is a valid member of this space.

Parameters:

  • x (Any) –

    Value to check.

Returns:

  • bool

    True if value is valid, False otherwise.

reset

reset() -> None

Reset all contained spaces to their initial values.

update

update(keys: Iterable[str]) -> None

Update specific keys in the Dict space by resampling them.

Parameters:

Raises:

  • ValueError

    If a key is not found in the Dict space.

names

names() -> list[str]

Return all space keys including nested ones.