Final Model v2

No preview image

1 collaborator

Default-person Rei Eugenio (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 7.0.0-beta0 • Viewed 24 times • Downloaded 2 times • Run 0 times
Download the 'Final Model v2' modelDownload this modelEmbed this model

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


WHAT IS IT?

In a network, a "component" is a group of nodes (people) that are all connected to each other, directly or indirectly. So if a network has a "giant component", that means almost every node is reachable from almost every other. This model shows how quickly a giant component arises if you grow a random network.

HOW IT WORKS

Initially we have nodes but no connections (edges) between them. At each step, we pick two nodes at random which were not directly connected before and add an edge between them. All possible connections between them have exactly the same probability of occurring.

As the model runs, small chain-like "components" are formed, where the members in each component are either directly or indirectly connected to each other. If an edge is created between nodes from two different components, then those two components merge into one. The component with the most members at any given point in time is the "giant" component and it is colored red. (If there is a tie for largest, we pick a random component to color.)

HOW TO USE IT

The NUM-NODES slider controls the size of the network. Choose a size and press SETUP.

Pressing the GO ONCE button adds one new edge to the network. To repeatedly add edges, press GO.

As the model runs, the nodes and edges try to position themselves in a layout that makes the structure of the network easy to see. Layout makes the model run slower, though. To get results faster, turn off the LAYOUT? switch.

The REDO LAYOUT button runs the layout-step procedure continuously to improve the layout of the network.

A monitor shows the current size of the giant component, and the plot shows how the giant component's size changes over time.

THINGS TO NOTICE

The y-axis of the plot shows the fraction of all nodes that are included in the giant component. The x-axis shows the average number of connections per node. The vertical line on the plot shows where the average number of connections per node equals 1. What happens to the rate of growth of the giant component at this point?

The model demonstrates one of the early proofs of random graph theory by the mathematicians Paul Erdos and Alfred Renyi (1959). They showed that the largest connected component of a network formed by randomly connecting two existing nodes per time step, rapidly grows after the average number of connections per node equals 1. In other words, the average number of connections has a "critical point" where the network undergoes a "phase transition" from a rather unconnected world of a bunch of small, fragmented components, to a world where most nodes belong to the same connected component.

THINGS TO TRY

Let the model run until the end. Does the "giant component" live up to its name?

Run the model again, this time slowly, a step at a time. Watch how the components grow. What is happening when the plot is steepest?

Run it with a small number of nodes (like 10) and watch the plot. How does it differ from the plot you get when you run it with a large number of nodes (like 300)? If you do multiple runs with the same number of nodes, how much does the shape of the plot vary from run to run? You can turn off the LAYOUT? switch to get results faster.

EXTENDING THE MODEL

Right now the probability of any two nodes getting connected to each other is the same. Can you think of ways to make some nodes more attractive to connect to than others? How would that impact the formation of the giant component?

When creating new links in the add-edge procedure, you might be wondering why we don't do something like this:

ask one-of turtles [
  create-link-with one-of other turtles with [ not link-neighbor? myself ]
]

Imagine that we have one node in the network that is already connected to most of the other nodes. In the original version of add-edge, if that node is picked as one of the two nodes between which we try to create an edge, it will probably get rejected because it is likely that it's already linked with the other node picked. In this alternate version of add-edge, however, we tell NetLogo to explicitly go looking for a node that is not already connected to the first one (even if there are very few of those). That makes a big difference. Try it and see how it impacts the formation of the giant component.

NETWORK CONCEPTS

Identification of the connected components is done using a standard search algorithm called "depth first search." "Depth first" means that the algorithm first goes deep into a branch of connections, tracing them out all the way to the end. For a given node it explores its neighbor's neighbors (and then their neighbors, etc) before moving on to its own next neighbor. The algorithm is recursive so eventually all reachable nodes from a particular starting node will be explored. Since we need to find every reachable node, and since it doesn't matter what order we find them in, another algorithm such as "breadth first search" would have worked equally well. We chose depth first search because it is the simplest to code.

The position of the nodes is determined by the "spring" method, which is further described in the Preferential Attachment model.

NETLOGO FEATURES

Nodes are turtle agents and edges are link agents. The layout-spring primitive places the nodes, as if the edges are springs and the nodes are repelling each other.

Though it is not used in this model, there exists a network extension for NetLogo that you can download at: https://github.com/NetLogo/NW-Extension.

RELATED MODELS

See other models in the Networks section of the Models Library, such as Preferential Attachment.

See also Network Example, in the Code Examples section.

There is also a version of this model using the (NW extension)[https://github.com/NetLogo/NW-Extension] in the demo folder of the extension.

CREDITS AND REFERENCES

This model is adapted from: Duncan J. Watts. Six Degrees: The Science of a Connected Age (W.W. Norton & Company, New York, 2003), pages 43-47.

The work Watts describes was originally published in: P. Erdos and A. Renyi. On random graphs. Publ. Math. Debrecen, 6:290-297, 1959.

This paper has some additional analysis: S. Janson, D.E. Knuth, T. Luczak, and B. Pittel. The birth of the giant component. Random Structures & Algorithms 4, 3 (1993), pages 233-358.

HOW TO CITE

If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.

For the model itself:

Please cite the NetLogo software as:

COPYRIGHT AND LICENSE

Copyright 2005 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

Comments and Questions

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

Click to Run Model

turtles-own
[
  ;; this is used to mark turtles we have already visited
  explored?
  infected?           ;; if true, the turtle is infectious
  resistant?          ;; if true, the turtle can't be infected
  vaccinated?         ;; if true, the turtle is vaccinated
  recovering?         ;; really, this is just for efficiency
  virus-check-timer   ;; number of ticks since this turtle's last virus-check
  day-of-infection    ;; day they got infected
  time-of-infection   ;; time they got infected


  base_xcor
  base_ycor

  scale
  scale_xcor
  scale_ycor
  humanities
  humanities_xcor
  humanities_ycor
  core
  elective
  math-section
  research
  core-set
  elective-set
  inactive-set

  a_xcor
  a_ycor
  b_xcor
  b_ycor
  c_xcor
  c_ycor
  move
;  moved?
]

links-own [ link-type ]

undirected-link-breed [ friend-links friend-link]
undirected-link-breed [ scale-links scale-link ]
undirected-link-breed [ humanities-links humanities-link ]
undirected-link-breed [ core-links core-link ]
undirected-link-breed [ elective-links elective-link ]
undirected-link-breed [ math-section-links math-section-link ]
undirected-link-breed [ research-links research-link ]

globals
[
  component-size          ;; number of turtles explored so far in the current component
  giant-component-size    ;; number of turtles in the giant component
  giant-start-node        ;; node from where we started exploring the giant component
  done?
  instant?
  day
  time
  current-period
  total-infected
  layout?

  standard-start

  divide_3
  divide_4
  divide_5
]

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

;; the number of connections made can matter based on the population around them

to instant-go-1
  loop [
    let a 0
    if done? = true [stop]
    if-else any? turtles with [color = 7] [
      instant-go
    ]
    [ set a 1 ]
    if a = 1 and done? = false [
      if layout? [
        repeat 200 [
          layout-spring (turtles with [any? friend-link-neighbors]) links 0.3 150 200
          ;layout-tutte (turtles with [count link-neighbors > 1]) links 200+
        ]
      ]

      ask turtles [
        set base_xcor xcor
        set base_ycor ycor
      ]

      ask n-of (initial-outbreak-size) turtles [ become-infected ]
    ;  ask n-of 5 turtles with [infected? = false] [become-resistant]
      set done? true
      stop
    ]
  ]
end 

to setup
  clear-all
  set-default-shape turtles "person"
  set divide_5 [ -73.47315653655913 -101.12712429686843 118.88206453689419 38.62712429686843 0 125 -118.8820645368942 38.6271242968684 73.47315653655916 -101.12712429686842 ]
  set divide_3 [-86.60254037844385 -50.00000000000004 86.60254037844388 -49.99999999999998 0 100]
  set divide_4 [125 125 -125 -125 125 -125 -125 125]
  make-turtles
  set done? false
  ;; at this stage, all the components will be of size 1,
  ;; since there are no edges yet
  ask links [hide-link]
  find-all-components
  color-giant-component
  reset-ticks
  set layout? true
  set standard-start standard-run
end 

to make-turtles
  create-turtles num-nodes [
    set size 15
    set infected? false
    set resistant? false
    set recovering? false
    set virus-check-timer random virus-check-frequency
    set elective (random 9)
    set core (random 3)
    if elective <= 4 [
      while [core = (floor (elective / 2))] [
        set core (random 3)
      ]
    ]
  ]

  let x 1
  while [x < 5] [
    ask n-of ((count turtles) / 5) turtles with [scale = 0] [
      set scale x
    ]
    set x x + 1
  ]
  set x 1
  ask n-of 75 turtles [
    set research x
    set x (x + 1)
    if (x = 26) [set x 1]
  ]
  set x 52
  ask n-of 10 turtles with [research = 0] [
    set research (floor (x / 2))
    set x (x + 1)
  ]

  ask turtles [
    let my-research [research] of self
    if my-research != 0 [ create-research-links-with other turtles with [research = my-research]]
  ]

  ask n-of ((count turtles) * vaccinated-percentage / 100) turtles [ set vaccinated? true ]
  ask turtles with [vaccinated? != true] [set vaccinated? false]
  ask n-of ((count turtles) / 3) turtles with [humanities = 0] [ set humanities 1 ]
  ask n-of ((count turtles) / 3) turtles with [humanities = 0] [ set humanities 2 ]

  ask turtles [
    (if-else elective >= 7 [
      set elective-set 2
    ]
    elective = 5 or elective = 6 [
      set elective-set 1
    ]
    elective = 0 [
      set elective-set 0
    ]
    elective = 2 [
      set elective-set 2
    ]) ;; 1 (0), 3 (1), and 4 (2) are treated as cores
    if core = 0 and (elective = 2 or elective >= 7) [
      set core-set 1
    ]
    if core = 0 and (elective = 5 or elective = 6) [
      set core-set 2
    ]
    if core = 1 and (elective = 5 or elective = 6) [
      set core-set 0
    ]
    if core = 1 and elective = 0 [
      set core-set 1
    ]
    if core = 2 and (elective = 2 or elective >= 7) [
      set core-set 0
    ]
    if core = 2 and elective = 0 [
      set core-set 2
    ]
  ]

  ask n-of abs((count turtles with [core = 0 or elective = 1]) / 2 - (count turtles with [core = 0 and core-set = 1])) turtles with [(core = 0 and core-set != 1 and elective-set != 1) or elective = 1] [
    if core = 0 [set core-set 1]
    if elective = 1 [set elective-set 1]
  ]
  ask turtles with [core = 0 and core-set != 1] [set core-set 2]
  ask turtles with [elective = 1 and elective-set != 1] [set elective-set 2]


  ask n-of abs((count turtles with [core = 1 or elective = 3]) / 2 - (count turtles with [core = 1 and core-set = 1])) turtles with [(core = 1 and core-set != 1 and elective-set != 1) or (elective = 3 and core-set != 1)] [
    if core = 1 [set core-set 1]
    if elective = 3 [set elective-set 1]
  ]

  ask turtles with [core = 1 and core-set = 0 and elective = 4] [set elective-set 2]
  ask turtles with [elective = 3 and elective-set = 0 and core = 2] [set core-set 2]


  if-else ((count turtles with [core = 2 or elective = 4]) / 2 - (count turtles with [core = 2 and core-set = 2]) - (count turtles with [elective = 4 and elective-set = 2])) >= (count turtles with [(core = 2 and core-set != 2 and elective-set != 2) or (elective = 4 and core-set != 2 and elective-set != 2)]) [
    if (count turtles with [core = 2 or elective = 4]) / 2 >= (count turtles with [core = 2 and core-set = 0]) + (count turtles with [elective = 4 and elective-set = 0]) [
      ask n-of (ceiling (count turtles with [core = 2 or elective = 4]) / 2 - (count turtles with [core = 2 and core-set = 0]) - (count turtles with [elective = 4 and elective-set = 0])) turtles with [(core = 2 and core-set != 0 and elective-set != 0) or (elective = 4 and core-set != 0 and elective-set != 0)] [
        if core = 2 [set core-set 0]
        if elective = 4 [set elective-set 0]
      ]
    ]
  ]
  [
    ask n-of abs((ceiling (count turtles with [core = 2 or elective = 4]) / 2) - (count turtles with [core = 2 and core-set = 2]) - (count turtles with [elective = 4 and elective-set = 2])) turtles with [(core = 2 and core-set != 2 and elective-set != 2) or (elective = 4 and core-set != 2 and elective-set != 2)] [
      if core = 2 [set core-set 2]
      if elective = 4 [set elective-set 2]
    ]
  ]

  ask turtles [
    set math-section random 3
    set inactive-set (3 - core-set - elective-set)
  ]

  setup_shc
  layout-circle (sort turtles) max-pxcor - 10
end 

;;;;;;;;;;;;;;;;;;;;;;
;;; Main Procedure ;;;
;;;;;;;;;;;;;;;;;;;;;;

to go1
  set day 0
  set time 745

  let a 0
  add-specific-edge 0
  add-specific-edge 1
  add-specific-edge 2
  add-specific-edge 2
  add-specific-edge 3
  add-specific-edge 4
  add-specific-edge 5

  find-all-components
  color-giant-component
  ask links [ set color [color] of end1 ]  ;; recolor all edges
  ;; layout the turtles with a spring layout, but stop laying out when all nodes are in the giant component
  if not all? turtles [ color = blue ] [ layout ]
  ask links [
    set color white
    hide-link
  ]
  ask friend-links [show-link]
end 

to instant-go
  set day 0
  set time 745
  set instant? true
  let a 0
  add-specific-edge 0
  add-specific-edge 1
  add-specific-edge 2
  add-specific-edge 2
  add-specific-edge 3
  add-specific-edge 3
  add-specific-edge 4
  add-specific-edge 4
  add-specific-edge 5
  find-all-components
  color-giant-component
  ask links [ set color [color] of end1 ]  ;; recolor all edges
  ;; layout the turtles with a spring layout, but stop laying out when all nodes are in the giant component
  if not all? turtles [ color = blue ] [ layout ]
  ask links [
    set color white
    hide-link
  ]
  ask friend-links [show-link]
end 

to go2
  if not any? turtles with [infected? = true] [
    display
    stop
  ]
  if not layout? [ layout-circle (sort turtles) max-pxcor - 10 ]
  if ((day mod 7) = 5 or (day mod 7) = 6) or (ticks mod 5) = 0 [
    set time time + 5
    if (time mod 100) = 60 [
      set time time + 40
    ]
    if time = 1600 [
      set time 730
      set day day + 1
    ]
    clear-output
    output-type "Day: "
    output-print day
    output-type "Time: "
    output-print time
    output-type "Total: "
    output-print total-infected
  ]

  if standard-start = true [

    (ifelse (((day mod 7) = 0 and time >= 800 and time <= 840) or (day mod 7) = 2) [
      if current-period != "SCALE" [
        animate_move 1
        ask links [ hide-link ]
        ask scale-links [ show-link ]
        set current-period "SCALE"
      ]

    ]
    (((day mod 7) = 0 or (day mod 7) = 3) and time >= 800 and time <= 1040) or ((day mod 7) = 1 and time >= 800 and time <= 920)
    [
      if current-period != "Humanities" [
        animate_move 2
        ask links [ hide-link ]
        ask humanities-links [ show-link ]
        set current-period "Humanities"
      ]
    ]
    (((day mod 7) = 0 and time >= 1315 and time <= 1350) or ((day mod 7) = 1 and time >= 1055 and time <= 1215) or ((day mod 7) = 4 and time >= 800 and time <= 915))
    [
      if current-period != "Set A" [
        animate_move 3
        ask links [ hide-link ]
        ask turtles with [core-set = 0] [
          ask my-core-links [ show-link ]
        ]
        ask turtles with [elective-set = 0] [
          ask my-elective-links [ show-link ]
        ]
        set current-period "Set A"
      ]
    ]
    (((day mod 7) = 0 and time >= 1355 and time <= 1430) or ((day mod 7) = 3 and time >= 1435 and time <= 1555) or ((day mod 7) = 4 and time >= 1055 and time <= 1215))  ;; add more here!!!!!!!!!!!!!!!!
    [
      if current-period != "Set B" [
        animate_move 4
        ask links [ hide-link ]
        ask turtles with [core-set = 1] [
          ask my-core-links [ show-link ]
        ]
        ask turtles with [elective-set = 1] [
          ask my-elective-links [ show-link ]
        ]
        set current-period "Set B"
      ]
    ]
    (((day mod 7) = 0 and time >= 1435 and time <= 1515) or ((day mod 7) = 1 and time >= 1435 and time <= 1555) or ((day mod 7) = 4 and time >= 920 and time <= 1040))  ;; add more here!!!!!!!!!!!!!!!!
    [
      if current-period != "Set C" [
        animate_move 5
        ask links [ hide-link ]
        ask turtles with [core-set = 2] [
          ask my-core-links [ show-link ]
        ]
        ask turtles with [elective-set = 2] [
          ask my-elective-links [ show-link ]
        ]
        set current-period "Set C"
      ]
    ]
    (((day mod 7) = 1 and time >= 920 and time <= 1040) or (((day mod 7) = 3 or (day mod 7) = 4) and time >= 1315 and time <= 1430)) [
      if current-period != "Research" [
        animate_move 0
        ask links [hide-link]
        ask research-links [ show-link ]
        set current-period "Research"
      ]
    ]
    (((day mod 7) = 0 and time >= 1055 and time <= 1210) or ((day mod 7) = 1 and time >= 1315 and time <= 1430) or ((day mod 7) = 3 and time >= 1055 and time <= 1210))
    [
      if current-period != "Math" [
        animate_move 0
        ask links [hide-link]
        ask math-section-links [ show-link ]
        set current-period "Math"
      ]
    ]
    ((day mod 7) <= 4)
    [
      if current-period != "Free Time!" [
        animate_move 0
        ask links [hide-link]
        ask friend-links [ show-link ]
        set current-period "Free Time!"
      ]
    ])

  ]

  ask turtles with [infected? = true and recovering? = false] [
    if (day-of-infection + 7) * 2400 + time-of-infection <= day * 2400 + time [set recovering? true]
  ]
  if (ticks mod 5) = 0 and ((time mod 100) mod 15) = 0 [
    if all? turtles [infected? = false] [ stop ]
    ask turtles with [recovering? = true]
    [
       set virus-check-timer virus-check-timer + 1
       if virus-check-timer >= virus-check-frequency [
        set virus-check-timer 0
      ]
    ]
    if ((time mod 100 ) mod 30 = 0) [spread-virus]
  ]
  display
  do-virus-checks
  tick
end 

to animate_move [kind]
  if layout? [
    (ifelse kind = 1 [
      ask turtles [
        facexy scale_xcor scale_ycor
        set move distancexy scale_xcor scale_ycor
      ]
    ]
    kind = 2 [
      ask turtles [
        facexy humanities_xcor humanities_ycor
        set move distancexy humanities_xcor humanities_ycor
      ]
    ]
    kind = 3 [
      ask turtles [
        facexy a_xcor a_ycor
        set move distancexy a_xcor a_ycor
      ]
    ]
    kind = 4 [
      ask turtles [
        facexy b_xcor b_ycor
        set move distancexy b_xcor b_ycor
      ]
    ]
    kind = 5 [
      ask turtles [
        facexy c_xcor c_ycor
        set move distancexy c_xcor c_ycor
      ]
    ]
    [
      ask turtles [
        facexy base_xcor base_ycor
        set move distancexy base_xcor base_ycor
      ]
    ])
    let a 0
    while [a <= 9] [
      ask turtles [ forward (move / 10) ]
      set a (a + 1)
      display
    ]
  ]
end 

to setup_shc
  let x 0
  while [x < 10] [
    layout-circle turtles with [scale = (x / 2)] 40
    ask turtles with [scale = (x / 2)] [
      setxy (xcor + item x divide_5) (ycor + (item (x + 1) divide_5))
    ]
    set x x + 2
  ]
  ask turtles [
    set scale_xcor xcor
    set scale_ycor ycor
  ]

  set x 0
  while [x < 6] [
    layout-circle turtles with [humanities = (x / 2)] 75
    ask turtles with [humanities = (x / 2)] [
      setxy (xcor + (item x divide_3)) (ycor + (item (x + 1) divide_3))
    ]
    set x x + 2
  ]
  ask turtles [
    set humanities_xcor xcor
    set humanities_ycor ycor
  ]

  layout-circle turtles with [elective = 0] 50
  ask turtles with [elective = 0] [
    setxy xcor (ycor + 125)
  ]
  layout-circle turtles with [(core-set = 0 and core = 1) or (elective-set = 0 and elective = 3)] 50
  ask turtles with [(core-set = 0 and core = 1) or (elective-set = 0 and elective = 3)] [
    setxy (xcor + 125 * cos 30) (ycor - 125 * sin 30)
  ]
  layout-circle turtles with [(core-set = 0 and core = 2) or (elective-set = 0 and elective = 4)] 50
  ask turtles with [(core-set = 0 and core = 2) or (elective-set = 0 and elective = 4)] [
    setxy (xcor - 125 * cos 30) (ycor - 125 * sin 30)
  ]
  layout-circle turtles with [inactive-set = 0] 40
  ask turtles [
    set a_xcor xcor
    set a_ycor ycor
  ]

  layout-circle turtles with [elective = 5] 60
  ask turtles with [elective = 5] [
    setxy (xcor + (item 0 divide_4)) (ycor + (item 1 divide_4))
  ]
  layout-circle turtles with [elective = 6] 60
  ask turtles with [elective = 6] [
    setxy (xcor + (item 2 divide_4)) (ycor + (item 3 divide_4))
  ]
  layout-circle turtles with [(core-set = 1 and core = 0) or (elective-set = 1 and elective = 1)] 60
  ask turtles with [(core-set = 1 and core = 0) or (elective-set = 1 and elective = 1)] [
    setxy (xcor + (item 4 divide_4)) (ycor + (item 5 divide_4))
    set color white
  ]
  layout-circle turtles with [(core-set = 1 and core = 1) or (elective-set = 1 and elective = 3)] 60
  ask turtles with [(core-set = 1 and core = 1) or (elective-set = 1 and elective = 3)] [
    setxy (xcor + (item 6 divide_4)) (ycor + (item 7 divide_4))
    set color white - 2
  ]

  layout-circle turtles with [inactive-set = 1] 40
  ask turtles [
    set b_xcor xcor
    set b_ycor ycor
  ]

  layout-circle turtles with [(core-set = 2 and core = 0) or (elective-set = 2 and elective = 1)] 40
  ask turtles with [(core-set = 2 and core = 0) or (elective-set = 2 and elective = 1)] [
    setxy (xcor + item 0 divide_5) (ycor + (item 1 divide_5))
  ]
  layout-circle turtles with [(core-set = 2 and core = 2) or (elective-set = 2 and elective = 4)] 40
  ask turtles with [(core-set = 2 and core = 2) or (elective-set = 2 and elective = 4)] [
    setxy (xcor + item 2 divide_5) (ycor + (item 3 divide_5))
  ]
  layout-circle turtles with [elective = 2] 40
  ask turtles with [elective = 2] [
    setxy (xcor + item 4 divide_5) (ycor + (item 5 divide_5))
  ]
  layout-circle turtles with [elective = 7] 40
  ask turtles with [elective = 7] [
    setxy (xcor + item 6 divide_5) (ycor + (item 7 divide_5))
  ]
  layout-circle turtles with [elective = 8] 40
  ask turtles with [elective = 8] [
    setxy (xcor + item 8 divide_5) (ycor + (item 9 divide_5))
  ]
  layout-circle turtles with [inactive-set = 2] 40
  ask turtles [
    set c_xcor xcor
    set c_ycor ycor
  ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Network Exploration ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; to find all the connected components in the network, their sizes and starting turtles

to find-all-components
  ask turtles [ set explored? false ]
  ;; keep exploring till all turtles get explored
  loop
  [
    ;; pick a node that has not yet been explored
    let start one-of turtles with [ not explored? ]
    if start = nobody [ stop ]
    ;; reset the number of turtles found to 0
    ;; this variable is updated each time we explore an
    ;; unexplored node.
    set component-size 0
    ;; at this stage, we recolor everything to light gray
    ask start [ explore (gray + 2) ]
    ;; the explore procedure updates the component-size variable.
    ;; so check, have we found a new giant component?
    if component-size > giant-component-size
    [
      set giant-component-size component-size
      set giant-start-node start
    ]
  ]
end 

;; Finds all turtles reachable from this node (and recolors them)

to explore [new-color]  ;; node procedure
  if explored? [ stop ]
  set explored? true
  set component-size component-size + 1
  ;; color the node
  set color new-color
  ask friend-link-neighbors [ explore new-color ]
end 

;; color the giant component red

to color-giant-component
  ask turtles [ set explored? false ]
  ask giant-start-node [ explore blue ]
end 

;;;;;;;;;;;;;;;;;;;;;;;
;;; Edge Operations ;;;
;;;;;;;;;;;;;;;;;;;;;;;

to add-specific-edge [input]
  let node1 one-of turtles
  let node2 one-of turtles
  (if-else input = 1 [
    let test [scale] of node1
    if-else any? turtles with [any? my-scale-links = false and scale = test]
    [
      set node2 one-of turtles with [any? my-scale-links = false and scale = test]
    ]
    [
      if-else any? turtles with [color = 7 and scale = test]
      [
        set node2 one-of turtles with [color = 7 and scale = test]
      ]
      [
        set node2 one-of turtles with [scale = test]
      ]
    ]
  ]
  input = 2 [
    let test [humanities] of node1
    if-else any? turtles with [any? my-humanities-links = false and humanities = test]
    [
      set node2 one-of turtles with [any? my-humanities-links = false and humanities = test]
    ]
    [
      if-else any? turtles with [color = 7 and humanities = test]
      [
        set node2 one-of turtles with [color = 7 and humanities = test]
      ]
      [
        set node2 one-of turtles with [humanities = test]
      ]
    ]
  ]
  input = 3 [
    let test [core] of node1
    let my-set [core-set] of node1
    if-else any? turtles with [any? my-core-links = false and core = test and core-set = my-set]
    [
      set node2 one-of turtles with [any? my-core-links = false and core = test and core-set = my-set]
    ]
    [
      if-else any? turtles with [color = 7 and core = test and core-set = my-set]
      [
        set node2 one-of turtles with [color = 7 and core = test and core-set = my-set]
      ]
      [
        set node2 one-of turtles with [core = test and core-set = my-set]
      ]
    ]
  ]
  input = 4 [
    let test [elective] of node1
    let my-set [elective-set] of node1
    if-else any? turtles with [any? my-elective-links = false and elective = test and elective-set = my-set]
    [
      set node2 one-of turtles with [any? my-elective-links = false and elective = test and elective-set = my-set]
    ]
    [
      if-else any? turtles with [color = 7 and elective = test and elective-set = my-set]
      [
        set node2 one-of turtles with [color = 7 and elective = test and elective-set = my-set]
      ]
      [
        set node2 one-of turtles with [elective = test and elective-set = my-set]
      ]
    ]
  ]
  input = 5 [
    let test [math-section] of node1
    if-else any? turtles with [any? my-math-section-links = false and math-section = test]
    [
      set node2 one-of turtles with [any? my-math-section-links = false and math-section = test]
    ]
    [
      if-else any? turtles with [color = 7 and math-section = test]
      [
        set node2 one-of turtles with [color = 7 and math-section = test]
      ]
      [
        set node2 one-of turtles with [math-section = test]
      ]
    ]
  ]
  [
    if-else any? turtles with [any? my-links = false]
    [
      set node2 one-of turtles with [any? my-links = false]
    ]
    [
      if any? turtles with [color = 7]
      [
        set node2 one-of turtles with [color = 7]
      ]
    ]
  ])
;      scale
;  humanities
;  core
;  elective
;  math-section
  ask node1 [
    let a link-neighbors
    (ifelse input = 1 [
      set a scale-link-neighbors
    ]
    [
      set a friend-link-neighbors
    ])
    ifelse member? node2 a or node1 = node2
    ;; if there's already an edge there, then go back
    ;; and pick new turtles
    [ add-specific-edge input ]
    ;; else, go ahead and make it
    [(ifelse input = 1 [
      create-scale-link-with node2
    ]
    input = 2 [
      create-humanities-link-with node2
    ]
    input = 3 [
      create-core-link-with node2
    ]
    input = 4 [
      create-elective-link-with node2
    ]
    input = 5 [
      create-math-section-link-with node2
    ]
    [
      create-friend-link-with node2
    ])]
  ]
end 

;;;;;;;;;;;;;;
;;; Layout ;;;
;;;;;;;;;;;;;;

to layout
  if not layout? [ stop ]
  ;; the number 10 here is arbitrary; more repetitions slows down the
  ;; model, but too few gives poor layouts
  repeat 10 [
    do-layout
    if instant? != true [display]  ;; so we get smooth animation
  ]
end 

to do-layout
  layout-spring (turtles with [any? link-neighbors]) links 0.5 5 5
end 

to highlight
  ask turtles [set color blue]
  ask links [set color white]
  ask turtles with [infected? = true]
  [
    set color red
  ]
  ask turtles with [resistant? = true]
  [
    set color gray
    ask my-links [set color gray - 2]
  ]
  ; if the mouse is in the View, go ahead and highlight
  if mouse-inside? [ do-highlight ]

  ; force updates since we don't use ticks
  display
end 
;

to do-highlight
  ; getting the node closest to the mouse
  let min-d min [ distancexy mouse-xcor mouse-ycor ] of turtles
  let node one-of turtles with [count link-neighbors > 0 and distancexy mouse-xcor mouse-ycor = min-d]
;
  if node != nobody [
;    ; highlight the chosen node
    ask node [
      set color white
    ]
;
    let neighbor-nodes [ link-neighbors ] of node
    let direct-links [ my-links with [hidden? = false] ] of node

    ask direct-links [ set color orange ]
    ask node [
      ask direct-links [
        ask other-end [
          set color orange
          ask my-links [
            if [color] of other-end = orange [ set color yellow ]
          ]
        ]
      ]
    ]
  ]
end 

to become-infected  ;; turtle procedure
  set infected? true
  set resistant? false
  set recovering? false
  set color red
  set day-of-infection day
  set time-of-infection time
  set total-infected total-infected + 1
end 

to become-susceptible  ;; turtle procedure
  set infected? false
  set resistant? false
  set color blue
end 

to become-resistant  ;; turtle procedure
  set infected? false
  set resistant? true
  set color gray
  ask my-links [ set color gray - 2 ]
end 

to spread-virus
  ask turtles with [infected? = true] [
    ask my-links with [hidden? = false] [
      if [resistant?] of other-end = false and [infected?] of other-end = false [
        ask other-end [
          let b virus-spread-chance
          if vaccinated? [ set b b * vaccination-infection-multiplier]
          if random-float 100 < b [
            become-infected
          ]
        ]
      ]
    ]
  ]
end 

to do-virus-checks
  ask turtles with [infected? = true and virus-check-timer = 0]
  [
    let a recovery-chance
    let b gain-resistance-chance
    if vaccinated? [
      set a a * vaccination-recovery-multiplier
      set b b * vaccination-resistance-multiplier
    ]
    if random 100 < a
    [
      set recovering? false
      ifelse random 100 < b
        [ become-resistant ]
        [ become-susceptible ]
    ]
  ]
end 

to looper
  loop [if-else any? turtles with [infected? = true] [ go2 ] [stop]]
end 

to-report infected-count
  report total-infected
end 


; Copyright 2005 Uri Wilensky.
; See Info tab for full copyright and license.

There is only one version of this model, created 11 days ago by Rei Eugenio.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.