Published
Edited
Jan 23, 2020
10 stars
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
md`## Simulation code`
Insert cell
simulation_results = {
rerun_simulation;
const states = [];
states[0] = initialize_simulation(edge_list(graph));
for(let timestep = 1; timestep < timesteps; timestep++) {
states[timestep] = tick_simulation(states[timestep - 1], edge_list(graph));
}
return states;
}
Insert cell
initialize_simulation = function(edge_list) {
const state = {
node_states: [],
time_in_state: [],
infection_pathways: []
};
// Set all nodes to susceptible
for(let i = 0; i < edge_list.length; i++) {
state.node_states[i] = STATES.SUSCEPTIBLE;
state.time_in_state[i] = 0;
}
// Pick random nodes to be infected
const shuffled = Array.from({length: N}, (_, i) => i).sort(() => 0.5 - Math.random());
const selected = shuffled.slice(0, num_infected_initialization);
for(let index in selected) {
state.node_states[index] = STATES.INFECTED;
state.time_in_state[index] = 0;
}
return state;
}
Insert cell
tick_simulation = function(state, edge_list) {
const new_state = { node_states: state.node_states.slice(0),
time_in_state: state.time_in_state.slice(0),
infection_pathways: []};
// Update time spent in state
for(let i = 0; i < edge_list.length; i++) {
new_state.time_in_state[i] = state.time_in_state[i] + 1;
}
// Loop through each individual
for(let i = 0; i < edge_list.length; i++) {
if(state.node_states[i] == STATES.INFECTED) {
// If they have been infected for N timesteps, set them back to susceptible
if(state.time_in_state[i] == time_infected) {
new_state.node_states[i] = STATES.RECOVERED;
new_state.time_in_state[i] = 0;
}
// If they are still infected, infect their neighbors
for(let j = 0; j < edge_list[i].length; j++) {
if(state.node_states[edge_list[i][j]] == STATES.SUSCEPTIBLE && Math.random() < infection_probability) {
new_state.node_states[edge_list[i][j]] = STATES.INFECTED;
new_state.time_in_state[edge_list[i][j]] = 0;
new_state.infection_pathways.push({
source: i,
target: edge_list[i][j]
});
}
}
}
if(state.node_states[i] == STATES.RECOVERED && state.time_in_state[i] == time_recovered) {
new_state.node_states[i] = STATES.SUSCEPTIBLE;
new_state.time_in_state[i] = 0;
}
}
return new_state;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
STATES = ({
SUSCEPTIBLE: 0,
INFECTED: 1,
RECOVERED: 2
});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
color = (node_state) => {
if(node_state == STATES.INFECTED) return palette.pomegranate;
if(node_state == STATES.RECOVERED) return palette.belize_hole;
if(node_state == STATES.SUSCEPTIBLE) return palette.concrete;
}
Insert cell
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
import { palette } from "@herbps10/flat-ui-colors"
Insert cell
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