-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomNetwork.py
54 lines (45 loc) · 1.43 KB
/
randomNetwork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from NetworkEnvironment import game, actors, network
import numpy
import matplotlib.pyplot as plt
import copy
import os
def maybeInspectState(state):
pass
def maybeInspectActor(actor):
pass
def maybeInspectResults(result):
print("Time spent : ", results.elapsedTime)
gameResults.append(True if results.attackDetected else False)
pass
attacker = actors.RandomAttacker()
defender = actors.RandomDefender()
net = network.fromYedGraphML(os.path.join(os.path.dirname(os.path.realpath(__file__)), "./entry.graphml"))
net.display()
nbGames = 0
last10times = []
gameResults = []
timeThreshold = 1500
gamesThreshold = 1000
#while nbGames < gamesThreshold or np.mean(last10times) < timeThreshold :
while nbGames < 200:
print("New game starting ... ", nbGames)
thisGame = game.CyberGame(attacker, defender, copy.deepcopy(net))
thisGame.addStepHook(lambda state : maybeInspectState(state))
results = thisGame.run()
maybeInspectResults(results)
if len(last10times) == 10:
last10times.pop(0)
last10times.append(results.elapsedTime)
maybeInspectActor(defender)
gameResults.append(results.elapsedTime)
nbGames += 1
window_size = 10
i = 0
moving_averages = []
while i < len(gameResults) - window_size + 1:
this_window = gameResults[i : i + window_size]
window_average = sum(this_window) / window_size
moving_averages.append(window_average)
i += 1
plt.plot(moving_averages)
plt.show()