-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathVirusModel.js
More file actions
145 lines (128 loc) · 4.68 KB
/
VirusModel.js
File metadata and controls
145 lines (128 loc) · 4.68 KB
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
136
137
138
139
140
141
142
143
144
145
// This model demonstrates the spread of a virus through a network. Although
// the model is somewhat abstract, one interpretation is that each node
// represents a computer, and we are modeling the progress of a computer
// virus (or worm) through this network. Each node may be in one of three
// states: susceptible, infected, or resistant. In the academic literature
// such a model is sometimes referred to as an SIR model for epidemics.
import World from '/src/World.js'
import Model from '/src/Model.js'
import * as util from '/src/utils.js'
// A port of the NetLogo "Virus on a Network" model
export default class VirusModel extends Model {
population = 150
averageNodeDegree = 6
initialOutbreakSize = 3
virusSpreadPercent = 2.5
virusCheckFrequency = 1
recoveryPercent = 5.0
gainResistancePercent = 5.0
// ======================
constructor(worldOptions = World.defaultOptions(40)) {
super(worldOptions)
}
setup() {
this.setupNodes()
this.setupNetwork()
this.turtles
.nOf(this.initialOutbreakSize)
.ask(t => this.becomeInfected(t))
this.postState()
}
setupNodes() {
this.turtles.create(this.population, t => {
// for visual reasons, we don't put any nodes *too* close to the edges
t.setxy(...this.world.randomPoint().map(cor => cor * 0.95))
this.becomeSusceptible(t)
t.virusCheckTimer = util.randomInt(this.virusCheckFrequency)
})
}
setupNetwork() {
// REMIND
const numLinks = (this.averageNodeDegree * this.population) / 2
// console.log(numLinks)
while (this.links.length < numLinks) {
const t1 = this.turtles.oneOf()
const others = this.turtles
.other(t1)
.with(t => !t.linkNeighbors().includes(t1))
const choice = others.minOneOf(t => t1.distance(t))
this.links.createOne(t1, choice)
}
}
postState() {
this.susceptible = this.turtles.with(
t => t.state === 'susceptible'
).length
this.resistant = this.turtles.with(t => t.state === 'resistant').length
this.infected = this.turtles.with(t => t.state === 'infected').length
}
step() {
if (this.done) return
this.turtles.ask(t => {
t.virusCheckTimer++
if (t.virusCheckTimer >= this.virusCheckFrequency)
t.virusCheckTimer = 0
})
this.spreadVirus()
this.doVirusChecks()
this.postState()
this.done = this.infected === 0
// this.done = this.turtles.all(o => !o.infected)
// if (this.done) {
// const t = this.ticks
// const n = this.population
// const s = this.turtles.with(t => t.state === 'susceptible').length
// const r = this.turtles.with(t => t.state === 'resistant').length
// const f = val => Math.round((val * 10000) / n) / 100
// console.log(
// `done at ticks: ${t}, nodes: ${n}, resistant: ${f(r)}%, susceptible: ${f(s)}%`
// )
// this.susceptible = f(s)
// this.resistant = f(r)
// // window.susceptible = this.susceptible
// // window.resistant = this.resistant
// }
}
becomeInfected(t) {
t.infected = true
t.resistant = false
t.state = 'infected'
}
becomeSusceptible(t) {
t.infected = false
t.resistant = false
t.state = 'susceptible'
}
becomeResistant(t) {
t.infected = false
t.resistant = true
t.state = 'resistant'
}
spreadVirus() {
this.turtles
.with(t => t.infected)
.ask(t => {
t.linkNeighbors()
.with(n => !n.resistant)
.ask(n => {
if (util.randomFloat(100) < this.virusSpreadPercent)
this.becomeInfected(n)
})
})
}
doVirusChecks() {
this.turtles
.with(t => t.infected && t.virusCheckTimer === 0)
.ask(t => {
if (util.randomInt(100) < this.recoveryPercent) {
if (util.randomInt(100) < this.gainResistancePercent) {
this.becomeResistant(t)
} else {
this.becomeSusceptible(t)
}
}
})
}
}
// `done at ticks: ${t}, nodes: ${n}, resistant: ${(r * 100) / n}%, susceptible: ${(s * 100) / n}%`
// `done at ticks: ${t}, nodes: ${n}, resistant: ${f('resistant')}%, susceptible: ${f('susceptible')}%`