Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/cyberbattle/agents/random_agent.py
597 views
1
# Copyright (c) Microsoft Corporation.
2
# Licensed under the MIT License.
3
4
"""Helper to run the random agent from a jupyter notebook"""
5
6
import cyberbattle._env.cyberbattle_env as cyberbattle_env
7
import logging
8
9
LOGGER = logging.getLogger(__name__)
10
11
12
def run_random_agent(episode_count: int, iteration_count: int, gym_env: cyberbattle_env.CyberBattleEnv):
13
"""Run a simple random agent on the specified gym environment and
14
plot exploration graph and reward function
15
"""
16
17
for i_episode in range(episode_count):
18
observation, _ = gym_env.reset()
19
20
total_reward = 0.0
21
22
for t in range(iteration_count):
23
action = gym_env.sample_valid_action()
24
25
LOGGER.debug(f"action={action}")
26
observation, reward, done, truncated, info = gym_env.step(action)
27
28
total_reward += reward
29
30
if reward > 0:
31
prettry_printed = gym_env.pretty_print_internal_action(action)
32
print(f"+ rewarded action: {action} total_reward={total_reward} reward={reward} @t={t}\n {prettry_printed}")
33
gym_env.render()
34
35
if truncated:
36
print(f"Episode truncated after {t+1} timesteps")
37
break
38
39
if done:
40
print(f"Episode finished after {t+1} timesteps")
41
break
42
43
gym_env.render()
44
45
gym_env.close()
46
print("simulation ended")
47
48