Skip to content

Commit 35cee2c

Browse files
committed
Use Agent.Role
1 parent cfb4f2c commit 35cee2c

4 files changed

Lines changed: 14 additions & 12 deletions

File tree

agents/action_list_base_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
import json
44
from os import path
5-
from netsecgame import Action, Observation, GameState, BaseAgent, generate_valid_actions
5+
from netsecgame import Action, Observation, GameState, BaseAgent, generate_valid_actions, AgentRole
66

77

88
class ActionListAgent(BaseAgent):
@@ -14,7 +14,7 @@ class ActionListAgent(BaseAgent):
1414
Compatible with the WhiteBoxNSGCoordinator.
1515
"""
1616

17-
def __init__(self, host, port, role: str):
17+
def __init__(self, host, port, role: AgentRole):
1818
super().__init__(host, port, role)
1919
self._action_list = []
2020
self._action_to_idx = {}
@@ -70,7 +70,7 @@ def get_valid_action_mask(self , state: GameState) -> np.ndarray:
7070

7171
args = parser.parse_args()
7272
log_filename = path.dirname(path.abspath(__file__)) + '/action_List_base_agent.log'
73-
agent = ActionListAgent(args.host, args.port, "Attacker")
73+
agent = ActionListAgent(args.host, args.port, AgentRole.Attacker)
7474
observation = agent.register()
7575
print(f"Total actions: {len(agent._action_list)}")
7676
print(f"Valid action mask: {agent.get_valid_action_mask(observation.state)}")

agents/attackers/markov_chain_agent/markov_chain_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from os import path, makedirs
1010

1111

12-
from AIDojoCoordinator.game_components import Action, Observation, AgentStatus, ActionType
12+
from AIDojoCoordinator.game_components import Action, Observation, AgentStatus, ActionType, AgentRole
1313

1414
sys.path.append(path.dirname(path.dirname(path.dirname(path.abspath(__file__) ))))
1515

@@ -312,7 +312,7 @@ def save_solutions_json(self, filepath: str):
312312
level=logging.INFO
313313
)
314314

315-
agent = MarkovChainAgent(args.host, args.port, "Attacker", args.episodes)
315+
agent = MarkovChainAgent(args.host, args.port, AgentRole.Attacker, args.episodes)
316316
observation = agent.register()
317317

318318
if not args.evaluate:

agents/benign/random/benign_random_agent.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import numpy as np
77
import time
88
from random import uniform, choice
9-
from AIDojoCoordinator.game_components import Action, Observation, ActionType
10-
from NetSecGameAgents.agents.base_agent import BaseAgent
11-
from NetSecGameAgents.agents.agent_utils import generate_valid_actions
9+
from netsecgame import Action, Observation, ActionType, BaseAgent, generate_valid_actions, AgentRole
1210

1311
class RandomBenignAgent(BaseAgent):
1412

15-
def __init__(self, host:str, port:int, role:str, allowed_actions:list, apm_limit:int=None) -> None:
13+
def __init__(self, host:str, port:int, role:AgentRole, allowed_actions:list, apm_limit:int=None) -> None:
1614
super().__init__(host, port, role)
1715
self._allowed_actions = allowed_actions
1816
self._apm_limit = apm_limit
@@ -28,6 +26,10 @@ def play_game(self, num_episodes=1):
2826
"""
2927

3028
observation = self.register()
29+
if observation is None:
30+
self._logger.error("Registration failed. Terminating.")
31+
return
32+
3133
returns = []
3234
for episode in range(num_episodes):
3335
episodic_returns = []
@@ -79,5 +81,5 @@ def select_action(self, observation:Observation)->Action:
7981
logging.basicConfig(filename=os.path.join(args.logdir, "benign_random_agent.log"), filemode='w', format='%(asctime)s %(name)s %(levelname)s %(message)s', datefmt='%H:%M:%S',level=logging.DEBUG)
8082

8183
# Create agent
82-
agent = RandomBenignAgent(args.host, args.port, "Benign", allowed_actions=[ActionType.FindData, ActionType.ExfiltrateData, ActionType.FindServices], apm_limit=args.apm)
84+
agent = RandomBenignAgent(args.host, args.port, AgentRole.Benign, allowed_actions=[ActionType.FindData, ActionType.ExfiltrateData, ActionType.FindServices], apm_limit=args.apm)
8385
agent.play_game(args.episodes)

agents/defenders/random/random_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import mlflow
99

1010
from random import uniform, choice
11-
from AIDojoCoordinator.game_components import Action, Observation, ActionType
11+
from AIDojoCoordinator.game_components import Action, Observation, ActionType, AgentRole
1212
from NetSecGameAgents.agents.base_agent import BaseAgent
1313
from NetSecGameAgents.agents.agent_utils import generate_valid_actions
1414

@@ -88,7 +88,7 @@ def select_action(self, observation:Observation)->Action:
8888
logging.basicConfig(filename=os.path.join(args.logdir, "defender_random_agent.log"), filemode='w', format='%(asctime)s %(name)s %(levelname)s %(message)s', datefmt='%H:%M:%S',level=logging.DEBUG)
8989

9090
# Create agent
91-
agent = RandomDefenderAgent(args.host, args.port, "Defender", allowed_actions=[ActionType.FindData, ActionType.ExfiltrateData, ActionType.FindServices, ActionType.BlockIP], apm_limit=args.apm)
91+
agent = RandomDefenderAgent(args.host, args.port, AgentRole.Defender, allowed_actions=[ActionType.FindData, ActionType.ExfiltrateData, ActionType.FindServices, ActionType.BlockIP], apm_limit=args.apm)
9292

9393
print("Agent created. Starting interaction with the game server...")
9494

0 commit comments

Comments
 (0)