-
Notifications
You must be signed in to change notification settings - Fork 0
/
answerQuestion10.py
135 lines (100 loc) · 4.3 KB
/
answerQuestion10.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import simulateVirusSpread as sim
import displayVirusSpread as disp
import viruses
import Network as n
import fileMaker
import numpy as N
import matplotlib.pyplot as plt
networkSize = 50
n_runs = 100
def runSimulation(theNetwork, startingPoint, virus):
"""
Description:
This function calls runSimulation passing in the network, starting point for the virus
to start, the virus, and a True/False value. The infected percentage is then calcualted
by getting the infectedCount array and counting the number of infected and dividing by
network size.
Pre:
theNetwork is a Network object predefined. startingPoint is an integer value
specifying the starting point. virus is the type of virus and is either a Trojan
or a Worm object.
Post:
returns the infectedPercent
"""
if virus == "W":
virus = viruses.Worm()
else:
virus = viruses.Trojan()
sim.runOnce(theNetwork, startingPoint, virus, False)
infectedCount = 0
for node in theNetwork.infectedList[1:]:
if node == n.State.infected:
infectedCount += 1
infectedPercent = (infectedCount / networkSize) * 100
return infectedPercent
fileMaker.tree(networkSize)
infectedPercentLowStrengthTreeWorm = []
infectedPercentMedStrengthTreeWorm = []
infectedPercentHighStrengthTreeWorm = []
infectedPercentLowStrengthTreeTrojan = []
infectedPercentMedStrengthTreeTrojan = []
infectedPercentHighStrengthTreeTrojan = []
virus = viruses.Worm()
for x in range(n_runs):
nodeStrengthRange = (0, .2)
theNetwork = n.Network(disp.graphType.TREE)
theNetwork.createnetwork("tree.txt", nodeStrengthRange)
infectedPercentLowStrengthTreeWorm.append(runSimulation(theNetwork, 1, "W"))
nodeStrengthRange = (.2, .4)
theNetwork = n.Network(disp.graphType.TREE)
theNetwork.createnetwork("tree.txt", nodeStrengthRange)
infectedPercentMedStrengthTreeWorm.append(runSimulation(theNetwork, 1, "W"))
nodeStrengthRange = (.4, .6)
theNetwork = n.Network(disp.graphType.TREE)
theNetwork.createnetwork("tree.txt", nodeStrengthRange)
infectedPercentHighStrengthTreeWorm.append(runSimulation(theNetwork, 1, "W"))
virus = viruses.Trojan()
for x in range(n_runs):
nodeStrengthRange = (0.4, .6)
theNetwork = n.Network(disp.graphType.TREE)
theNetwork.createnetwork("tree.txt", nodeStrengthRange)
infectedPercentLowStrengthTreeTrojan.append(runSimulation(theNetwork, 1, "T"))
nodeStrengthRange = (.2, .4)
theNetwork = n.Network(disp.graphType.TREE)
theNetwork.createnetwork("tree.txt", nodeStrengthRange)
infectedPercentMedStrengthTreeTrojan.append(runSimulation(theNetwork, int(networkSize / 2), "T"))
nodeStrengthRange = (0, .2)
theNetwork = n.Network(disp.graphType.TREE)
theNetwork.createnetwork("tree.txt", nodeStrengthRange)
infectedPercentHighStrengthTreeTrojan.append(runSimulation(theNetwork, networkSize, "T"))
infectedPercentLowStrengthTreeWorm = N.array(infectedPercentLowStrengthTreeWorm)
infectedPercentMedStrengthTreeWorm = N.array(infectedPercentMedStrengthTreeWorm)
infectedPercentHighStrengthTreeWorm = N.array(infectedPercentHighStrengthTreeWorm)
infectedPercentLowStrengthTreeTrojan = N.array(infectedPercentLowStrengthTreeTrojan)
infectedPercentMedStrengthTreeTrojan = N.array(infectedPercentMedStrengthTreeTrojan)
infectedPercentHighStrengthTreeTrojan = N.array(infectedPercentHighStrengthTreeTrojan)
meansWorm = [N.mean(infectedPercentLowStrengthTreeWorm), N.mean(infectedPercentMedStrengthTreeWorm),
N.mean(infectedPercentHighStrengthTreeWorm)]
meansTrojan = [N.mean(infectedPercentLowStrengthTreeTrojan), N.mean(infectedPercentMedStrengthTreeTrojan),
N.mean(infectedPercentHighStrengthTreeTrojan)]
bar_width = 0.125
bar_locations = N.array([1, 1.5, 2])
fig, ax = plt.subplots()
plt.bar(bar_locations,
meansWorm, bar_width,
alpha=.4,
color='r',
label='Worm')
plt.bar(bar_locations + (bar_width * 2),
meansTrojan,
bar_width,
alpha=.4,
color='b',
label='Trojan')
plt.xticks(bar_locations + bar_width, ('High Strength', 'Medium Strength', 'Low Strength'))
plt.ylabel('percentage infected')
plt.xlabel('Node Strength')
plt.title('Tree')
plt.legend()
plt.show()
fig.savefig('tree-infection-vs-node-strength.png')