Skip to content

Commit

Permalink
Fix bug where time() was referring to the wrong module and was retu…
Browse files Browse the repository at this point in the history
…rning the NULL time. #95

Upgrade pyright
  • Loading branch information
William Blum committed Oct 13, 2023
1 parent 7322056 commit f8e870d
Show file tree
Hide file tree
Showing 8 changed files with 944 additions and 17,961 deletions.
2 changes: 0 additions & 2 deletions .vscode/settings.json
@@ -1,6 +1,4 @@
{
"python.pythonPath": "/usr/bin/python",
"python.venvPath": "venv",
"python.testing.pytestArgs": [
"cyberbattle"
],
Expand Down
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -206,7 +206,15 @@ Cumulative rewards -- DQN=Red, Random=Green
## Jupyter notebooks

To quickly get familiar with the project, you can open one of the provided Jupyter notebooks to play interactively with
the gym environments. Just start jupyter with `jupyter notebook` from the conda environment.
the gym environments. At the root of the repository run the following command and then open the notebook in the `notebooks` folder
from the Jupyter interface:

```python
export PYTHONPATH=$(pwd)
jupyter lab
```

Some notebooks to get started:

- 'Capture The Flag' toy environment notebooks:
- [Random agent](notebooks/toyctf-random.ipynb)
Expand Down
2 changes: 1 addition & 1 deletion cyberbattle/_env/cyberbattle_env.py
Expand Up @@ -794,7 +794,7 @@ def __property_vector(self, node_id: model.NodeID, node_info: model.NodeInfo) ->
vector[properties_indices] = 1
return vector

def __get_property_matrix(self) -> Tuple[numpy.ndarray]:
def __get_property_matrix(self) -> Tuple[numpy.ndarray, ...]:
"""Return the Node-Property matrix,
where 0 means the property is not set for that node
1 means the property is set for that node
Expand Down
75 changes: 42 additions & 33 deletions cyberbattle/agents/baseline/plotting.py
Expand Up @@ -7,14 +7,15 @@
import numpy as np

import matplotlib # type: ignore
matplotlib.use('Agg')

matplotlib.use("Agg")


def new_plot(title):
"""Prepare a new plot of cumulative rewards"""
plt.figure(figsize=(10, 8))
plt.ylabel('cumulative reward', fontsize=20)
plt.xlabel('step', fontsize=20)
plt.ylabel("cumulative reward", fontsize=20)
plt.xlabel("step", fontsize=20)
plt.xticks(size=20)
plt.yticks(size=20)
plt.title(title, fontsize=12)
Expand All @@ -23,56 +24,60 @@ def new_plot(title):
def pad(array, length):
"""Pad an array with 0s to make it of desired length"""
padding = np.zeros((length,))
padding[:len(array)] = array
padding[: len(array)] = array
return padding


def plot_episodes_rewards_averaged(results):
"""Plot cumulative rewards for a given set of specified episodes"""
max_iteration_count = np.max([len(r) for r in results['all_episodes_rewards']])
max_iteration_count = np.max([len(r) for r in results["all_episodes_rewards"]])

all_episodes_rewards_padded = [pad(rewards, max_iteration_count) for rewards in results['all_episodes_rewards']]
all_episodes_rewards_padded = [
pad(rewards, max_iteration_count) for rewards in results["all_episodes_rewards"]
]
cumrewards = np.cumsum(all_episodes_rewards_padded, axis=1)
avg = np.average(cumrewards, axis=0)
std = np.std(cumrewards, axis=0)
x = [i for i in range(len(std))]
plt.plot(x, avg, label=results['title'])
plt.plot(x, avg, label=results["title"])
plt.fill_between(x, avg - std, avg + std, alpha=0.5)


def fill_with_latest_value(array, length):
pad = length - len(array)
if pad > 0:
return np.pad(array, (0, pad), mode='edge')
return np.pad(array, (0, pad), mode="edge")
else:
return array


def plot_episodes_availability_averaged(results):
"""Plot availability for a given set of specified episodes"""
data = results['all_episodes_availability']
data = results["all_episodes_availability"]
longest_episode_length = np.max([len(r) for r in data])

all_episodes_padded = [fill_with_latest_value(av, longest_episode_length) for av in data]
all_episodes_padded = [
fill_with_latest_value(av, longest_episode_length) for av in data
]
avg = np.average(all_episodes_padded, axis=0)
std = np.std(all_episodes_padded, axis=0)
x = [i for i in range(len(std))]
plt.plot(x, avg, label=results['title'])
plt.plot(x, avg, label=results["title"])
plt.fill_between(x, avg - std, avg + std, alpha=0.5)


def plot_episodes_length(learning_results):
"""Plot length of every episode"""
plt.figure(figsize=(10, 8))
plt.ylabel('#iterations', fontsize=20)
plt.xlabel('episode', fontsize=20)
plt.ylabel("#iterations", fontsize=20)
plt.xlabel("episode", fontsize=20)
plt.xticks(size=20)
plt.yticks(size=20)
plt.title("Length of each episode", fontsize=12)

for results in learning_results:
iterations = [len(e) for e in results['all_episodes_rewards']]
episode = [i for i in range(len(results['all_episodes_rewards']))]
iterations = [len(e) for e in results["all_episodes_rewards"]]
episode = [i for i in range(len(results["all_episodes_rewards"]))]
plt.plot(episode, iterations, label=f"{results['title']}")

plt.legend(loc="upper right")
Expand All @@ -81,48 +86,50 @@ def plot_episodes_length(learning_results):

def plot_each_episode(results):
"""Plot cumulative rewards for each episode"""
for i, episode in enumerate(results['all_episodes_rewards']):
for i, episode in enumerate(results["all_episodes_rewards"]):
cumrewards = np.cumsum(episode)
x = [i for i in range(len(cumrewards))]
plt.plot(x, cumrewards, label=f'Episode {i}')
plt.plot(x, cumrewards, label=f"Episode {i}")


def plot_all_episodes(r):
"""Plot cumulative rewards for every episode"""
new_plot(r['title'])
new_plot(r["title"])
plot_each_episode(r)
plt.legend(loc="lower right")
plt.show()


def plot_averaged_cummulative_rewards(title, all_runs):
def plot_averaged_cummulative_rewards(title, all_runs, show=True):
"""Plot averaged cumulative rewards"""
new_plot(title)
for r in all_runs:
plot_episodes_rewards_averaged(r)
plt.legend(loc="lower right")
plt.show()
if show:
plt.show()


def plot_averaged_availability(title, all_runs):
def plot_averaged_availability(title, all_runs, show=False):
"""Plot averaged network availability"""
plt.figure(figsize=(10, 8))
plt.ylabel('network availability', fontsize=20)
plt.xlabel('step', fontsize=20)
plt.ylabel("network availability", fontsize=20)
plt.xlabel("step", fontsize=20)
plt.xticks(size=20)
plt.yticks(size=20)
plt.title(title, fontsize=12)
for r in all_runs:
plot_episodes_availability_averaged(r)
plt.legend(loc="lower right")
plt.show()
if show:
plt.show()


def new_plot_loss():
"""Plot MSE loss averaged over all episodes"""
plt.figure(figsize=(10, 8))
plt.ylabel('loss', fontsize=20)
plt.xlabel('episodes', fontsize=20)
plt.ylabel("loss", fontsize=20)
plt.xlabel("episodes", fontsize=20)
plt.xticks(size=12)
plt.yticks(size=20)
plt.title("Loss", fontsize=12)
Expand All @@ -131,7 +138,7 @@ def new_plot_loss():
def plot_all_episodes_loss(all_episodes_losses, name, label):
"""Plot loss for one learning episode"""
x = [i for i in range(len(all_episodes_losses))]
plt.plot(x, all_episodes_losses, label=f'{name} {label}')
plt.plot(x, all_episodes_losses, label=f"{name} {label}")


def running_mean(x, size):
Expand All @@ -153,9 +160,9 @@ def plot_durations(self, average_window=5):
plt.figure()
# plt.clf()
durations_t = np.array(self.episode_durations, dtype=np.float32)
plt.title('Training...')
plt.xlabel('Episode')
plt.ylabel('Duration')
plt.title("Training...")
plt.xlabel("Episode")
plt.ylabel("Duration")
plt.title(self.title, fontsize=12)

episodes = [i + 1 for i in range(len(self.episode_durations))]
Expand All @@ -182,7 +189,7 @@ def plot_end(self):

def length_of_all_episodes(run):
"""Get the length of every episode"""
return [len(e) for e in run['all_episodes_rewards']]
return [len(e) for e in run["all_episodes_rewards"]]


def reduce(x, desired_width):
Expand All @@ -191,8 +198,10 @@ def reduce(x, desired_width):

def episodes_rewards_averaged(run):
"""Plot cumulative rewards for a given set of specified episodes"""
max_iteration_count = np.max([len(r) for r in run['all_episodes_rewards']])
all_episodes_rewards_padded = [pad(rewards, max_iteration_count) for rewards in run['all_episodes_rewards']]
max_iteration_count = np.max([len(r) for r in run["all_episodes_rewards"]])
all_episodes_rewards_padded = [
pad(rewards, max_iteration_count) for rewards in run["all_episodes_rewards"]
]
cumrewards = np.cumsum(all_episodes_rewards_padded, axis=1)
avg = np.average(cumrewards, axis=0)
return list(avg)
Expand Down

0 comments on commit f8e870d

Please sign in to comment.