powered by NetLogo

view/download model file: OpinionFormationModelToy.nlogo

WHAT IS IT?

This is a toy model of opinion formation in a community. There are two choices, a structured network with 10 nodes and 2 communities, and a random network with 10 nodes and the same number of edges.


HOW IT WORKS

At each time step a node wakes up and decides to update its opinion. If most of its neighbors are blue, it will turn blue, if most of its neighbors are red, it will turn red. If its neighbors are split, it will flip a coin and decide in that way.


HOW TO USE IT

You have a switch to change the structure from community to random. You then click "setup" and "update opinion" repeatedly until the opinions have converged.


THINGS TO NOTICE

How easy is it for nodes to maintain dissenting opinions in the two different configurations?


CREDITS AND REFERENCES

Created by Lada Adamic 2008


PROCEDURES

; adapted from models library by Lada Adamic
; for the purposes of SI708/CSCS608

globals
[
  time
]

turtles-own
[
  proneighbors  ;; number of neigh
  antineighbors
  pro? ;; if true turtle is pro
]

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

to setup
  ca
  set-default-shape turtles "circle"
  
  repeat 10 [
    make-node ;; add the very first node
  ]
  
  ifelse community? [
  make-edge turtle 0 turtle 1
  make-edge turtle 0 turtle 2
  make-edge turtle 0 turtle 3
  make-edge turtle 0 turtle 4
  make-edge turtle 1 turtle 2
  make-edge turtle 1 turtle 3
  make-edge turtle 1 turtle 4
  make-edge turtle 2 turtle 3
  make-edge turtle 2 turtle 4
  make-edge turtle 3 turtle 4

  make-edge turtle 2 turtle 6
  make-edge turtle 3 turtle 5
  
  make-edge turtle 5 turtle 6
  make-edge turtle 5 turtle 7
  make-edge turtle 5 turtle 8
  make-edge turtle 5 turtle 9
  make-edge turtle 6 turtle 7
  make-edge turtle 6 turtle 8
  make-edge turtle 6 turtle 9
  make-edge turtle 7 turtle 8
  make-edge turtle 7 turtle 9
  make-edge turtle 8 turtle 9
  ]
  [
  while [count links < 22] [
    make-edge (one-of turtles) (one-of turtles)
  ]
  ]
  set time 0

  ask links [set color white]
  
    if layout? [ do-layout ]

    display

    while [(count turtles with [pro? = true]) < 5] [
      ask one-of turtles [
        set pro? true
      ]
    ]
  
    ask turtles ;;this sets pro turtles to color red or blue
      [ifelse pro? = true 
        [set color red]
        [set color blue]
      ]
 
end



;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Runtime Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;

;;spread opinion to network
to updateopinion
  
  if all? turtles [pro?]
    [stop]
  if all? turtles [not pro?]
    [stop]
    
  ask turtles [
    set size 2
    set shape "circle"
  ]
  ask links [
    set color white
  ]
  ask one-of turtles [

    do-highlight self
    set proneighbors count link-neighbors with [pro? = true]
    set antineighbors count link-neighbors with [pro? = false]
    if (proneighbors > antineighbors) [
      if (pro? = false) [set shape "square 2"]
      set pro? true
      set color red
    ]
    if (proneighbors < antineighbors) [
      if (pro? = true) [set shape "square 2"]
      set pro? false
      set color blue
     ]
     if (proneighbors = antineighbors) [
      ifelse (random-float 1 <= 0.5)
        [
         if (pro? = false) [set shape "square 2"]
         set pro? true
         set color red]
        [
           if (pro? = true) [set shape "square 2"]
           set pro? false
         set color blue]
     ]
     
   
  ]
  
  set time time + 1
  do-plotting
  
end  

to do-highlight [node]
    ;; highlight the chosen node
    ask node
    [
      set size 5
      set shape "circle 2"
    ]
    let neighbor-nodes [ link-neighbors ] of node
    let direct-links [ my-links ] of node
    ;; highlight neighbors
    ask neighbor-nodes
    [
      set shape "circle 2"
      set size 3

      ;; highlight edges connecting the chosen node to its neighbors
      ask my-links [
        if (end1 = node or end2 = node)
        [
          set color green;
        ]
      ]
    ]

end




;; used for creating a new node
to make-node
  create-turtles 1  
  [
    set color white + 2
    set size 2
    set pro? false
  ]
end


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

;; connects the two turtles
to make-edge [node1 node2]
  ask node1 [
    ifelse (node1 = node2) 
    [
      show "error: self-loop attempted"
    ]
    [
      create-link-with node2 [ set color green ]
     ;; position the new node near its partner
      setxy ([xcor] of node2) ([ycor] of node2)
      rt random 360
      fd 8
     ]
  ]
end


;;;;;;;;;;;;;;;;
;;; Plotting ;;;
;;;;;;;;;;;;;;;;

to do-plotting
     ;; plot the number of infected individuals at each step
     set-current-plot "number that are pro"
     set-current-plot-pen "inf"
     
     plotxy time (count turtles with [pro? = true])
end

;;;;;;;;;;;;;;
;;; Layout ;;;
;;;;;;;;;;;;;;
;; resize-turtles, change back and forth from size based on degree to a size of 1

;; spring layout of infection tree while in tree mode
;; otherwise, layout all nodes and links
to do-layout
  repeat 400 [layout-spring turtles links 0.05 10 30]
  display
end