Published
Edited
Feb 19, 2018
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
numTurtles = 1000
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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

// Set defaults for turtles
this.turtles.own('state')
this.turtles.setDefault('z', 0.1)
this.turtles.setDefault('shape', 'circle')
this.turtles.setDefault('size', 0.5)
// Create turtles
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
})

// and infect a random one
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)
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more