Modified Complex Contagion

Modified Complex Contagion preview image

1 collaborator

Default-person Ulrike Hahn (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.2.2 • Viewed 72 times • Downloaded 5 times • Run 0 times
Download the 'Modified Complex Contagion' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Info tab cannot be displayed because of an encoding error

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;; this model adds a simple rewiring procedure to Joshua Becker's complex contagion model in order to simulate
;; the effects of additional links as might result from a key word search on a social network
;; see Hahn, U., Maes, M., & Grossi, D. (2023) for discussion of this model and its outputs


globals [
  k                                    ;; determines the average number of neighbors each node has
  this-cluster                         ;; a tool used by the identify-clusters procedure
  saturated?                           ;; When this is TRUE, no more agents can be activated
  ;with_search                          ;; when this = 1 search is activated
]

turtles-own [
  cluster                              ;; a tool used by the identify-clusters procedure
  distance-from-other-turtles          ;; list of distances of this node from other turtles, used by the identify-clusters procedure
  attempted?                           ;; keeps track of which turtles have state-checked since a change in the system
                                       ;; the attempted? variable is used to help the model be more efficient
]

links-own
[
  rewired?                             ;; keeps track of whether the link has been rewired or not, used by the rewire-all procedure
]





;;  The "setup" procedure sets up the model.
;;  First, it creates the network using "rewire-all."
;;  Then, it seeds the contagion

to setup
  clear-all
  ask patches [ set pcolor white ]

  set k 4
  rewire-all



  ask turtles [ set color black set attempted? false ]
  set saturated? false
  if count turtles > 0 [ seed-contagion ]
  reset-ticks
end 



;;  The "construct-agent" procedure simply constructs an agent.
;;  In more complex models, a procedure like this
;;  can be helpful in generating agents with many different
;;  parameters or rules.

to construct-agent
  ;; turtles-own agent constructor
  set shape "circle"
  set label ""
end 




;;  The "restart" procedure resets the contagion process without
;;  changing the network structure.

to restart
  ask turtles [ set color black set attempted? false ]
  set saturated? false
  if count turtles > 0 [ seed-contagion ]
  set-current-plot "success of contagion"
  clear-plot
  reset-ticks
end 


;;  The "run-step" procedure runs a single step of the diffusion process.
;;  With each step, the model tries all the agents that are black (not red).
;;  When an agent is checked, it either updates (turns red) or remains the same.
;;  If the agent doesn't change, we set attempted? to TRUE for that turtle.

to run-step

  ;;  Check to see whether there are any turtles that might still become activated.
  ;;  If not, set "saturated?" to true and stop the function.
  ;;  If all the agents are red (none of them are black) then we know it's saturated
  ;;  We also know it's saturated if there are zero black agents who we haven't checked already.

  if count turtles with [ color = black ] = 0 [ set saturated? true stop ]
  if count turtles with [ color = black and attempted? = false ] = 0 [ set saturated? true stop ]

  if not saturated? [
    ask one-of turtles with [color = black ] [
      let count-triggered 0
      set count-triggered count link-neighbors with [color = red]

      ;;  If enough neighbors are active (red), an inactive (black) agent will be activated.
      ;;  Once a turtle turns from black to red, it's possible their neighbors may wish to change as well.
      ;;  Thus, whenever a turtle is activated, we reset attempted? to false for the other turtles.

      ifelse count-triggered >= threshold [
        set color red ask turtles with [ self != myself ] [ set attempted? false ]
      ]
      [
        set attempted? true
      ]

    ]
  ]
  ;let temp  random  100
  ;if temp  < 10
  ;[
  if with_search = 1
    [search]
  ;]
 ;set temp  100

  tick
end 




;;  This procedure "seeds" the contagion by randomly selecting a turtle
;;  and activating that turtle, as well as all of their neighbors (turning them red)

to seed-contagion
  ask one-of turtles [
    ask link-neighbors [ set color red ]
    set color red
  ]
end 








;;  The "create-ringlat" procedure creates a new ring lattice.
;;  All this procedure does is create N turtles, set their color black,
;;  and then run the "wire-ringlat" procedure.

to create-ringlat
  crt N [ set color black construct-agent ]
  wire-ringlat
end 






;;  The "wire-ringlat" procedure contains the machinery needed to wire a ring lattice.
;;  The parameter k determines the number of neighbors each node will have -- in this
;;  version, k is fixed.  However,  this parameter can be turned into a variable controlled
;;  from the interface.

to wire-ringlat
  layout-circle (sort turtles) max-pxcor - 1

  layout-circle (sort turtles with [ who mod 2 = 0] ) max-pxcor - 4
  ;; iterate over the turtles
  let ni 0
  while [ni < count turtles]
  [
    ;; make edges with the next two neighbors
    ;; this makes a lattice with average degree of 4
    let z 1
    while [z <= floor (k / 2)]
    [
      ask turtle ni [ create-link-with turtle ((ni + z) mod count turtles) [ set rewired? false ] ]
      set z z + 1
    ]
    set ni ni + 1
  ]
end 









;; the identify-clustesr and grow-clusters procedure ensure count the number of
;; components in the network.  This code is inspired by the NetLogo model "Dissemination of Culture"
;; by Iain Weaver.  You can find this model at: http://ccl.northwestern.edu/netlogo/models/community/Dissemination%20of%20Culture

to-report identify-clusters
  let max-cluster 0
  let num-clusters 0

  let seed one-of turtles
  ask turtles [ set cluster nobody ]
  while [seed != nobody] [
    ask seed [
      set cluster self
      set num-clusters num-clusters + 1
      set this-cluster 1
      grow-cluster
    ]
    if this-cluster > max-cluster [ set max-cluster this-cluster]
    set seed one-of turtles with [cluster = nobody]
  ]
  report list num-clusters max-cluster
end 

to grow-cluster

    ask link-neighbors with [cluster = nobody] [
      if cluster = nobody [ set this-cluster this-cluster + 1 ]
      set cluster [cluster] of myself
      grow-cluster
    ]
end 



;;  The "rewire-all" procedure generats a Small-World network according to the algorithm
;;  developed by Watts & Strogatz (1998).  This code is adapted from the Small Worlds NetLogo model
;;  developed by Uri Wilensky.  Original code Copyright 2005 Uri Wilensky.   See info tab for more details.

to rewire-all

  create-ringlat

  ;; set up a variable to see if the network is connected
  let success? false

  ;; if we end up with a disconnected network, we keep trying, because the APL distance
  ;; isn't meaningful for a disconnected network.
  let count-tries 0
  while [not success?] [
    ;; kill the old lattice, reset neighbors, and create new lattice
    ask links [ die ]
    wire-ringlat
;   set number-rewired 0

    ask links [

      ;; whether to rewire it or not?
      if (random-float 1) < p
      [
        ;; "a" remains the same
        let node1 end1
        ;; if "a" is not connected to everybody
        if [ count link-neighbors ] of end1 < (count turtles - 1)
        [
          ;; find a node distinct from node1 and not already a neighbor of node1
          let node2 one-of turtles with [ (self != node1) and (not link-neighbor? node1) ]
          ;; wire the new edge
          ask node1 [ create-link-with node2 [ set color cyan  set rewired? true ] ]

;          set number-rewired number-rewired + 1  ;; counter for number of rewirings
          set rewired? true
        ]
     ]
      ;; remove the old edge
      if (rewired?)
      [
        die
      ]
    ]

    set success? ( item 0 identify-clusters = 1 )
    set count-tries count-tries + 1
    if ( count-tries > 1000 ) [ set success? true print "couldn't make connected network!  try different parameters!" ]
  ]
end 





;; This procedure rewires ties to mirror effects of search.

to search

  ;; make sure num-turtles is setup correctly else run setup first
  if count turtles != N [
    setup
  ]

 if (random-float 1) < .02  ;;good num is .02
      [
  let potential-edges links with [ not rewired? ]
  ifelse any? potential-edges [
    ask one-of potential-edges [
      ;; "a" remains the same
      let node1 end1
      ;; if "a" is not connected to everybody
      if [ count link-neighbors ] of end1 < (count turtles - 1)
      [
        ;; find a node distinct from node1 and not already a neighbor of node1
        let node2 one-of turtles with [ (self != node1) and (not link-neighbor? node1) ]
        ;; wire the new edge
        ask node1 [ create-link-with node2 [ set color blue set rewired? true ] ]

        let node3 one-of turtles with [ (self != node1) and (not link-neighbor? node1) ]
        ;; wire the new edge
        ask node1 [ create-link-with node3 [ set color green set rewired? true ] ]


        let node4 one-of turtles with [ (self != node1) and (not link-neighbor? node1) ]
        ;; wire the new edge
        ask node1 [ create-link-with node4 [ set color green set rewired? true ] ]



        ask node1 [ set shape "star"]

                ;ask potential-edges [set rewired? true]

       ;; remove the old edge
        die
      ]
    ]
  ]
[   print "all rewired" ]
  ]
end 








;;  This procedure counts the fraction (percent) of nodes who are activated, that is 'infected' (red)

to-report percent-saturated
  report ((count turtles with [color = red ] ) / (count turtles))
end 

;; This procedure counts the percentage of nodes who have had links added through 'search'

to-report percent-searched
  report ((count turtles with [shape = "star" ] ) / (count turtles))
end 

;; This procedure counts the number of nodes who have had links added through 'search'

to-report searched
  report ((count turtles with [shape = "star" ] ))
end 

;; This procedure counts the number of links in the network

to-report link-total
  report (count links)
end 

There is only one version of this model, created 12 months ago by Ulrike Hahn.

Attached files

File Type Description Last updated
Modified Complex Contagion.png preview Model Preview 12 months ago, by Ulrike Hahn Download

This model does not have any ancestors.

This model does not have any descendants.