Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/notebooks/toyctf-random.py
597 views
1
# ---
2
# jupyter:
3
# jupytext:
4
# formats: py:percent,ipynb
5
# text_representation:
6
# extension: .py
7
# format_name: percent
8
# format_version: '1.3'
9
# jupytext_version: 1.16.4
10
# kernelspec:
11
# display_name: Python 3 (ipykernel)
12
# language: python
13
# name: python3
14
# ---
15
16
# %%
17
# %% [markdown] magic_args="[markdown]"
18
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
19
#
20
# # Random agent playing the Capture The Flag toy environment
21
22
# %%
23
import sys
24
import logging
25
import gymnasium as gym
26
27
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(levelname)s: %(message)s")
28
# %matplotlib inline
29
30
# %% [markdown]
31
32
# ### CyberBattle simulation
33
# - **Environment**: a network of nodes with assigned vulnerabilities/functionalities, value, and firewall configuration
34
# - **Action space**: local attack | remote attack | authenticated connection
35
# - **Observation**: effects of action on environment
36
37
# %%
38
from typing import cast
39
from cyberbattle._env.cyberbattle_env import CyberBattleEnv
40
41
_gym_env = gym.make("CyberBattleToyCtf-v0")
42
43
gym_env = cast(CyberBattleEnv, _gym_env)
44
45
# %%
46
gym_env.environment
47
48
# %%
49
gym_env.action_space
50
51
# %%
52
gym_env.action_space.sample()
53
54
# %% [markdown]
55
# ## A random agent
56
57
# %%
58
for i_episode in range(1):
59
observation, _ = gym_env.reset()
60
61
total_reward = 0
62
63
for t in range(5600):
64
action = gym_env.sample_valid_action()
65
66
observation, reward, done, _, info = gym_env.step(action)
67
68
total_reward += reward
69
70
if reward > 0:
71
print("####### rewarded action: {action}")
72
print(f"total_reward={total_reward} reward={reward}")
73
gym_env.render()
74
75
if done:
76
print("Episode finished after {} timesteps".format(t + 1))
77
break
78
79
gym_env.render()
80
81
gym_env.close()
82
print("simulation ended")
83
84
# %% [markdown]
85
# ### End of simulation
86
87
# %%
88
89