Skip to content

ActionMasker ​

python
ActionMasker(action_names: list[str], training_actions: list[str] | None, device: torch.device) -> None

Handles 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 in action_names.

Source

Properties ​

is_masked ​

python
is_masked: bool

Check if action masking is enabled.

Returns:

  • bool – True if masking is enabled, False otherwise.

Source

mask ​

python
mask: torch.Tensor | None

Get the action mask tensor.

Returns:

  • Tensor | None – The mask tensor (True for allowed actions) or None if no masking is applied.

Source

training_indices ​

python
training_indices: list[int] | None

Get the indices of training actions.

Returns:

  • list[int] | None – List of allowed action indices, or None if no masking is applied.

Source

Methods ​

apply_mask ​

python
apply_mask(q_values: torch.Tensor) -> torch.Tensor

Apply 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.

Source

sample_random_action ​

python
sample_random_action(fallback_action_space_sample: Callable[[], int | np.integer] | None = None) -> int

Sample 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.

Source