ActionMasker
python
ActionMasker(action_names: list[str], training_actions: list[str] | None, device: torch.device) -> NoneHandles action masking logic for training with a subset of actions. Consolidates mask creation, application, and random action sampling.
Initialize the ActionMasker.
Parameters:
action_names(list[str]) – List of all action names from the game configuration.training_actions(list[str] | None) – Optional list of action names to use for training. If None, all actions are used.device(device) – The device to create the mask tensor on.
Raises:
ValueError– If none of the training actions are found inaction_names.
Properties
is_masked
python
is_masked: boolCheck if action masking is enabled.
Returns:
bool– True if masking is enabled, False otherwise.
mask
python
mask: torch.Tensor | NoneGet the action mask tensor.
Returns:
Tensor | None– The mask tensor (True for allowed actions) or None if no masking is applied.
training_indices
python
training_indices: list[int] | NoneGet the indices of training actions.
Returns:
list[int] | None– List of allowed action indices, or None if no masking is applied.
Methods
apply_mask
python
apply_mask(q_values: torch.Tensor) -> torch.TensorApply the action mask to Q-values by setting excluded actions to a very small value.
Parameters:
q_values(Tensor) – Q-values tensor with shape (batch_size,num_actions) or (1,num_actions).
Returns:
Tensor– Masked Q-values tensor.
sample_random_action
python
sample_random_action(fallback_action_space_sample: Callable[[], int | np.integer] | None = None) -> intSample a random action from the allowed actions.
Parameters:
fallback_action_space_sample(Callable[[], int | integer] | None) – Optional callable to sample from action space when no mask is applied.
Returns:
int– A randomly sampled action index.