Discrete
- class Discrete(*args, init_value=None, constrain_fn=None, **kwargs)[source]
Bases:
DiscreteExtended discrete space with state tracking and constraint support.
This class extends
gymnasium.spaces.Discreteto add state management and optional constraint validation. Unlike the standard discrete space, this version maintains a current value and supports rejection sampling via a custom constraint function.- Variables:
init_value (int) – The initial value for the space.
value (int) – The current value of the space.
constrain_fn (callable) – Optional function that returns True if a value satisfies custom constraints.
Example
Create a discrete space that only accepts even numbers:
space = Discrete(n=10, init_value=0, constrain_fn=lambda x: x % 2 == 0) value = space.sample() # Samples even number and updates space.value space.reset() # Resets space.value back to 0 (init_value)Note
The
sample()method uses rejection sampling when a constraint function is provided, which may impact performance for difficult constraints.- __init__(*args, init_value=None, constrain_fn=None, **kwargs)[source]
Initialize a Discrete space with state tracking.
- Parameters:
*args – Positional arguments passed to gymnasium.spaces.Discrete.
init_value (int, optional) – Initial value for the space. Defaults to None.
constrain_fn (callable, optional) – Function that takes an int and returns True if the value satisfies custom constraints. Defaults to None.
**kwargs – Keyword arguments passed to gymnasium.spaces.Discrete.
- check()[source]
Validate the current space value.
Checks if the current value is within the space bounds and satisfies the constraint function. Logs a warning if the constraint fails.
- Returns:
True if the current value is valid, False otherwise.
- Return type:
bool
- contains(x)[source]
Check if value is valid and satisfies constraints.
- Parameters:
x (int) – The value to check.
- Returns:
- True if x is within bounds and satisfies the constraint
function, False otherwise.
- Return type:
bool
- reset()[source]
Reset the space value to its initial value.
Sets the current value back to the init_value specified during initialization.
- sample(*args, max_tries=1000, warn_after_s=5.0, set_value=True, **kwargs)[source]
Sample a random value using rejection sampling for constraints.
Repeatedly samples values until one satisfies the constraint function or max_tries is reached. Optionally updates the space’s current value.
- Parameters:
*args – Positional arguments passed to gymnasium.spaces.Discrete.sample().
max_tries (int, optional) – Maximum number of sampling attempts before raising an error. Defaults to 1000.
warn_after_s (float, optional) – Time threshold in seconds after which to log a warning about slow sampling. Set to None to disable. Defaults to 5.0.
set_value (bool, optional) – Whether to update the space’s current value with the sampled value. Defaults to True.
**kwargs – Keyword arguments passed to gymnasium.spaces.Discrete.sample().
- Returns:
A sampled value that satisfies the constraint function.
- Return type:
int
- Raises:
RuntimeError – If no valid sample is found after max_tries attempts.