Path: blob/main/cyberbattle/agents/random_agent.py
597 views
# Copyright (c) Microsoft Corporation.1# Licensed under the MIT License.23"""Helper to run the random agent from a jupyter notebook"""45import cyberbattle._env.cyberbattle_env as cyberbattle_env6import logging78LOGGER = logging.getLogger(__name__)91011def run_random_agent(episode_count: int, iteration_count: int, gym_env: cyberbattle_env.CyberBattleEnv):12"""Run a simple random agent on the specified gym environment and13plot exploration graph and reward function14"""1516for i_episode in range(episode_count):17observation, _ = gym_env.reset()1819total_reward = 0.02021for t in range(iteration_count):22action = gym_env.sample_valid_action()2324LOGGER.debug(f"action={action}")25observation, reward, done, truncated, info = gym_env.step(action)2627total_reward += reward2829if reward > 0:30prettry_printed = gym_env.pretty_print_internal_action(action)31print(f"+ rewarded action: {action} total_reward={total_reward} reward={reward} @t={t}\n {prettry_printed}")32gym_env.render()3334if truncated:35print(f"Episode truncated after {t+1} timesteps")36break3738if done:39print(f"Episode finished after {t+1} timesteps")40break4142gym_env.render()4344gym_env.close()45print("simulation ended")464748