{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Action Masking\n", "\n", "© Crown-owned copyright 2024, Defence Science and Technology Laboratory UK\n", "\n", "PrimAITE environments support action masking. The action mask shows which of the agent's actions are applicable with the current environment state. For example, a node can only be turned on if it is currently turned off." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!primaite setup" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from primaite.session.environment import PrimaiteGymEnv\n", "from primaite.config.load import data_manipulation_config_path\n", "from prettytable import PrettyTable\n", "from primaite.game.agent.scripted_agents import probabilistic_agent\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "env = PrimaiteGymEnv(data_manipulation_config_path())\n", "env.action_masking = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The action mask is a list of booleans that specifies whether each action in the agent's action map is currently possible. Demonstrated here:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "act_table = PrettyTable((\"number\", \"action\", \"parameters\", \"mask\"))\n", "mask = env.action_masks()\n", "actions = env.agent.action_manager.action_map\n", "max_str_len = 70\n", "for act,mask in zip(actions.items(), mask):\n", " act_num, act_data = act\n", " act_type, act_params = act_data\n", " act_params = s if len(s:=str(act_params))