powered by NetLogo
view/download model file: ERDiffusion.nlogo
This model is a SI diffusion model on a random graph. SI means that individuals can be in two states - susceptible (meaning they can get the disease) or infected (meaning that they have the disease). Once they have the disease, they stay infected and can continue to infect others.
The original documentation is placed in quotes.
I've only really modified it to only optionally lay out the network, to have a probability 'p' of an infection spreading over an edge at each time step, and to plot the number of infected individuals over time.
Additionally, this is a diffusion model. After setting up the network with the desired number of nodes (num-nodes), it will infect one node at random. You can then step through the diffusion process either step by step (spread once) or until every node is infected (spread complete). The plot will show the number of nodes infected at each time point. The time monitor will give the current time, and in the case of the 'spread complete' option, how many steps it took until all the nodes are infected. Note that this is the SI model - nodes are either susceptible or infected and there is no recovery. The 'p' parameter gives the probability that a an infected node will infect a neighbor at each time step.
From here on down is Uri's original, much more organized documentation:
The model creates a network of the "numnodes" number of nodes with the given average degree, connecting pairs of nodes at random.
It then infects one individual at random.
The p slider determines the probability that an infected individual will infect a susceptible contact at every time step.
"The LAYOUT? switch controls whether or not the layout procedure is run. This procedure attempts to move the nodes around to make the structure of the network easier to see.
The PLOT? switch turns off the plots which speeds up the model.
The RESIZE-NODES button will make all of the nodes take on a size representative of their degree distribution. If you press it again the nodes will return to equal size.
If you want the model to run faster, you can turn off the LAYOUT? and PLOT? switches and/or freeze the view (using the on/off button in the control strip over the view). The LAYOUT? switch has the greatest effect on the speed of the model.
If you have LAYOUT? switched off, and then want the network to have a more appealing layout, press the REDO-LAYOUT button which will run the layout-step procedure until you press the button again. You can press REDO-LAYOUT at any time even if you had LAYOUT? switched on and it will try to make the network easier to see."
Use the p slider to specify the probability that an infected individual will infect a susceptible contact at every time step.
View the progress of the disease diffusion on the network on the plot showing the cumulative number of individuals infected.
You can also specify the number of nodes and the average degree/node.
Observe how varying the transmission probability p, and the average degree in the network affects the reach and speed of diffusion.
Try to see if you can create the SIS model - nodes recover and return to the 'susceptible state' after either a fixed time period, or with some probability at each time step. In this case you are looking for the conditions (e.g. given value of p or average degree) under which you will observe epidemics - outbreaks that affect a significant fraction of the network, vs. conditions under which the outbreak remains small and contained.
See other models in the Networks section of the Models Library, such as Giant Component.
See also Network Example, in the Code Examples section.
globals [ num-infected tree-mode? ;; true after killing uninfected nodes / links infectious-component ;; the graph component that includes the infected node ] turtles-own [ infected? ;; true if agent has been infected infection-count ;; the number of turtles as turtle has infected explored? ;; used to find the infectious component ] ;;;;;;;;;;;;;;;;;;;;;;; ;;; Main Procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;; to spread ;; infection can't take place while in infection tree mode ;; or if every agent has already been infected if all? infectious-component [infected?] [stop] ask turtles with [ infected? = true ] [ ;; infect neighbors ask link-neighbors [ if ( random-float 1 <= p ) ;; infect with probability p [ if not infected? ;; agents can be infected only once [ set infected? true show-turtle set color red + 1 ;; incremement infection-count of the node doing the infection ask myself [set infection-count infection-count + 1] ;; color the link with the node doing the infection ask link-with myself [set color red - 1 show-link] ;; increment the total number of infected agents set num-infected num-infected + 1 ] ] ] ;; resize node so that the area is proportional to the current number of infections set size 2.25 * sqrt (infection-count + 1) ] do-plotting tick end ;; toggle infection tree mode ;; when tree-mode is on, only links responsible for contagion and infected nodes ;; are displayed. tree-mode also affects layout to toggle-tree ifelse tree-mode? [ ask turtles with [not infected?] [show-turtle] ask links with [color != red - 1] [show-link] set tree-mode? false ] [ ask turtles with [not infected?] [hide-turtle] ask links with [color != red - 1] [hide-link] set tree-mode? true ] end ;; spring layout of infection tree while in tree mode ;; otherwise, layout all nodes and links to do-layout ifelse tree-mode? [repeat 5 [layout-spring (turtles with [infected?]) (links with [color = red - 1]) 0.2 4 0.9]] [repeat 5 [layout-spring turtles links 0.2 4 0.9]] end ;;;;;;;;;;;;;;;;;;;;;;;; ;;; Setup Procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;;; to generate-topology ca set-default-shape turtles "outlined circle" ;; setup small world topology create-turtles num-nodes [set explored? false] ;; generate erdos-renyi random graph let num-links ceiling (num-nodes * avg-degree / 2) while [count links < num-links] [ ask one-of turtles [ create-link-with one-of other turtles ] ] setup ;; Layout turtles: layout-circle (sort turtles) 20 repeat 15 [do-layout] repeat 55 [do-layout display] end to setup clear-all-plots set tree-mode? false ask links [set color gray + 1.5] ask turtles [reset-node] ;; infect a single agent ask one-of turtles [ set infected? true set color red + 1 set size 2.25 explore ] set num-infected 1 set infectious-component turtles with [explored?] end to reset-node set color gray - 0.75 set size 2.1 set infected? false set infection-count 0 set explored? false end ;; finds all turtles reachable from this turtle to explore ;; turtle procedure if explored? [ stop ] set explored? true ask link-neighbors [ explore ] end to mouse-infect if mouse-down? [ let nearest-nodes turtles with [distancexy-nowrap mouse-xcor mouse-ycor < 1.5] if any? nearest-nodes [ ask one-of nearest-nodes [ ifelse infected? [ set infected? false set color gray - 0.75 set num-infected num-infected - 1 ] [ set infected? true set color red + 1 set num-infected num-infected + 1 ] ] display ] ] end ;;;;;;;;;;;;;;;; ;;; Plotting ;;; ;;;;;;;;;;;;;;;; to do-plotting ;; plot the number of infected individuals at each step set-current-plot "Number infected" set-current-plot-pen "inf" plotxy ticks num-infected end