Skip to content

Note

Click here to download the full example code

Agent-based SIR

Five hundred people walk on a grid and infect those who share their cell. The epidemic grows, peaks, and burns out like the SIR equations predict, but walking limits the contacts, so the outbreak is slower and smaller than for a well-mixed population.

The example builds AgentBasedSIRSystem with five infected among 500 people on a 50 by 50 grid. The observer saves every step with the default blueprint, whose primary view shows the people on the grid.

import matplotlib.pyplot as plt
import numpy as np

from skcomplex.core.engine.viz import RerunObserver
from skcomplex.systems.biology.epidemiology import AgentBasedSIRSystem

SYSTEM = 'agent_based_sir'

observer = RerunObserver(SYSTEM, save_path=f'{SYSTEM}.rrd', blueprint='default', interval=1)
trajectory = (
    AgentBasedSIRSystem(people=500, infected=5, beta=0.6, gamma=0.02, random_state=0)
    .prepare()
    .simulate(dt=1, observers=[observer], random_state=0, until=299)
)

plot agent based sir

Open the recording in the Rerun viewer

Compartments over time

Counting the health states of the people at every step, the infected should rise, peak, and decline while the recovered accumulate, and part of the population should never be infected.

health = np.array([trajectory.get_agents('people', frame).values['health'] for frame in range(len(trajectory.times))])
fig, ax = plt.subplots(figsize=(8, 4))
for value, label, color in [(0, 'susceptible', 'tab:blue'), (2, 'infected', 'tab:red'), (3, 'recovered', 'tab:green')]:
    ax.plot(trajectory.times, np.sum(health == value, axis=1), label=label, color=color)
ax.set_xlabel('step')
ax.set_ylabel('people')
ax.set_title(f'{np.sum(health[-1] == 0)} of 500 people never infected')
ax.legend()
plt.show()

64 of 500 people never infected

About the system

People who walk on a grid and infect those in their cell.

References

  • Argun et al. 2021
  • Kermack and McKendrick 1927

Total running time of the script: ( 0 minutes 3.822 seconds)

Download Python source code: plot_agent_based_sir.py

Download Jupyter notebook: plot_agent_based_sir.ipynb

Gallery generated by mkdocs-gallery