-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulateVirusSpread.py
70 lines (58 loc) · 2.34 KB
/
simulateVirusSpread.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
import Network as n
import displayVirusSpread as disp
import numpy as N
def nowhereToGo(network):
count = 1
for node in network.infectedList[1:]:
if node == n.State.infected:
for neighbor in network.nodes[count].adjacentNodes:
if network.infectedList[neighbor] != n.State.infected and network.infectedList[neighbor] != n.State.immune:
return False
count += 1
return True
def runOnce(network, startingPoint, virus, displayAnimation = True):
network.infectedList[startingPoint] = n.State.infected
simulationNotOver = True
displayData = disp.dataToDisplay()
displayData.typeOfGraph = network.networkType
#analysis variables
count = 0
infectedCount = 0
rate = 0
infectionRates = []
percent_infected = []
percent_infected.append(percentage(network))
while simulationNotOver == True:
count+=1
hasNewAnimationInfo = False
currentTurnMoves = []
# currentNodeID = 1
for x in range(1, len(network.infectedList)):
if network.infectedList[x] == n.State.infected:
for neighbor in network.nodes[x].adjacentNodes:
if network.infectedList[neighbor] != n.State.infected:
if virus.infectOrNot(network, neighbor, x):
currentTurnMoves.append((x, neighbor))
hasNewAnimationInfo = True
rate, infectedCount = checkRate(network, infectedCount)
infectionRates.append(rate)
percent_infected.append(percentage(network))
for move in currentTurnMoves:
network.infectedList[move[1]] = n.State.infected
if hasNewAnimationInfo:
displayData.animationSteps.append(currentTurnMoves)
if nowhereToGo(network):
simulationNotOver = False
if displayAnimation:
disp.display(displayData)
return count,infectionRates, percent_infected
def checkRate(network, lastInfectedCount):
infected_nodes_count = network.infectedList.count(n.State.infected)
if(infected_nodes_count > lastInfectedCount):
return infected_nodes_count - lastInfectedCount, infected_nodes_count
else:
return 0, lastInfectedCount
def percentage(network):
newList = N.array(network.infectedList)
infected_nodes = N.where(newList == n.State.infected)
return 100.0 * len(infected_nodes) / len(network.nodes)