class SIRModel extends Model {
setup () {
const {width, height} = this.world
this.world.time = 0
this.world.S = 0
this.world.I = 0
this.world.R = 0
this.turtles.own('state')
this.turtles.setDefault('z', 0.1)
this.turtles.setDefault('shape', 'circle')
this.turtles.setDefault('size', 0.5)
this.turtles.create(numTurtles, (t) => {
t.x = util.randomFloat2(-width/2,width/2)
t.y = util.randomFloat2(-height/2,height/2)
t.state = "S"
this.world.S += 1
})
this.infectAgent(this.turtles.oneOf((f) => {}))
}
step () {
this.world.time += tstep
this.turtles.ask((t) => {
if(t.state=="S"){
const alter = this.turtles.oneOf((f) => {})
if(alter.state == "I"){
if(util.randomFloat(1.0) < 0.05){
this.infectAgent(t,alter)
this.links.create(t, alter, (link) => {})
this.update()
}
}
}
if(t.state=="I"){
if(util.randomFloat(1.0) < 0.01){
this.recoverAgent(t)
for(var l of t.links){
l.die()
}
this.update()
}
}
})
}
infectAgent(t){
t.state = "I"
this.world.S -= 1
this.world.I += 1
}
recoverAgent(t){
t.state = "R"
this.world.I -= 1
this.world.R += 1
}
update(){
times.push(this.world.time)
S.push(this.world.S)
I.push(this.world.I)
R.push(this.world.R)
}
}