Voter dynamics in complex networks

Voter dynamics in complex networks preview image

1 collaborator

Tags

complex networks 

Tagged by Pablo Gonzalez de Prado about 7 years ago

voter model 

Tagged by Pablo Gonzalez de Prado about 7 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.3.1 • Viewed 836 times • Downloaded 31 times • Run 0 times
Download the 'Voter dynamics in complex networks' modelDownload this modelEmbed this model

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


Comments and Questions

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

Click to Run Model

globals [
        average-path-length                  ;; average path length of the network
        average-path-length-of-lattice       ;; average path length of the initial lattice
        cal                                  ;; background colouring stuff
        clustering-coefficient               ;; the clustering coefficient of the network; this is the
                                             ;; average of clustering coefficients of all turtles
        clustering-coefficient-of-lattice    ;; the clustering coefficient of the initial lattice
        highlight-string                     ;; message that appears on the node properties monitor
        idea                                 ;; number of active links
        infinity                             ;; a very large number
                                             ;; used to denote distance between two turtles which
                                             ;; don't have a connected or unconnected path between them
        network                              ;; records the kind of network that has been generated
        number-rewired                       ;; number of edges that have been rewired. used for plots
        partner2                             ;; in B�rab�si-Albert network with degree 2, to compare the two
                                             ;; neighbours chosen and check they are not the same
        rewire-one?                          ;; these two variables record which button was last pushed
        rewire-all?
        tick?                                ;; if a node of degree 0 is chosen, there's no need to tick
        yrad                                 ;; this three are used to measure the lattice
        rad
        nume
        ]


turtles-own [
            opinion                          ;; because colour might be lost when highlighting, another
                                             ;; magnitude is needed to save the nodes' opinion
            node-clustering-coefficient
            distance-from-other-turtles      ;; list of distances of this node from other turtles
            ]


links-own [
          rewired?                           ;; keeps track of whether the link has been rewired or not
          ]



;;ALLOCATION OF OPINIONS, VOTING, HIGHLIGHTING AND MORE***************************
;;********************************************************************************
;;********************************************************************************


;; called by set structure

to wire
  ask links [ ifelse [color] of end1 = [color] of end2
  ;; likns between equal nodes will take their colour, whereas those between different nodes are black,
  ;; and known as "active"
               [ set color [color] of end1 ]
               [ ask patch 0 0 [set cal pcolor] ifelse cal = black
                                                    [set color white]
                                                    [set color black]
               ]
            ]
  if structure = "random"
    [ ask turtles [ setxy random-xcor random-ycor ] ]
  if structure = "circle"
    [ layout-circle turtles (world-width / 2 - 1) ]
  if structure = "radial"
    [ layout-radial turtles links (turtle 0) ]
  if structure = "tutte"
    [ repeat 10 [ layout-tutte (turtles with [count link-neighbors <= 4]) links 12 ] ]
  do-plots
  do-plotting
end 


;; draw! is called by "set opinions" button and is also used to reset opinions

to draw!
  reset-ticks
  set-current-plot "totals"
  clear-plot
  ask turtles [ let col random diversity-of-opinions
  ;; col is used to assign opinions with equal chances
                  if col < 1 [ set opinion 1 set color red ]
                  if (col > 0.9) and (col < 2) [ set opinion 2 set color blue ]
                  if (col > 1.9) and (col < 3) [ set opinion 3 set color green ]
                  if (col > 2.9) and (col < 4) [ set opinion 4 set color turquoise ]
                  if (col > 3.9) and (col < 5) [ set opinion 5 set color brown ]
                  if (col > 4.9) and (col < 6) [ set opinion 6 set color violet ]
                  if (col > 5.9) and (col < 7) [ set opinion 7 set color orange ]
              ]
  wire
end 

to color-who [a]
  ask turtle a [
   if [opinion] of turtle a = 1 [ set color red ]
   if [opinion] of turtle a = 2 [ set color blue ]
   if [opinion] of turtle a = 3 [ set color green ]
   if [opinion] of turtle a = 4 [ set color turquoise ]
   if [opinion] of turtle a = 5 [ set color brown ]
   if [opinion] of turtle a = 6 [ set color violet ]
   if [opinion] of turtle a = 7 [ set color orange ]
   ]
end 

to color-all
  ask turtles [
   if [opinion] of turtle who = 1 [ set color red ]
   if [opinion] of turtle who = 2 [ set color blue ]
   if [opinion] of turtle who = 3 [ set color green ]
   if [opinion] of turtle who = 4 [ set color turquoise ]
   if [opinion] of turtle who = 5 [ set color brown ]
   if [opinion] of turtle who = 6 [ set color violet ]
   if [opinion] of turtle who = 7 [ set color orange ]
   ]
end 

to reprint
   ;; this option will refresh link and node colours
   color-all
   ask links [ ifelse [color] of end1 = [color] of end2
                [ set color [color] of end1 ]
                [ ask patch 0 0 [set cal pcolor] ifelse cal = black
                                                    [set color white]
                                                    [set color black]
               ]
             ]
end 

to highlight
  ;; remove any previous highlights
  ask turtles [ set color gray ]
  ask links [ set color gray + 2 ]
  if mouse-inside? [ do-highlight ]
  display
end 

to do-highlight
  ;; getting the node closest to the mouse
  let min-d min [distancexy mouse-xcor mouse-ycor] of turtles
  let a-turtle one-of turtles with [count link-neighbors > 0 and distancexy mouse-xcor mouse-ycor = min-d]
  if a-turtle != nobody
  [
    ;; highlight the chosen node
    ask a-turtle
    [
      color-who who
   ;;   let pairs (length remove infinity distance-from-other-nodos)
   ;;   let local-val (sum remove infinity distance-from-other-nodos) / pairs
      ;; show node's clustering coefficient
     ;; set highlight-string (word "clustering coefficient = " precision nodo-clustering-coefficient 3
     ;;                            " and avg path length = " precision local-val 3
     ;;                            " (for " pairs " nodos )")
    ]
    let neighbor-turtles [ link-neighbors ] of a-turtle
    ;let direct-links [ my-links ] of node
    ;; highlight neighbors
    ask neighbor-turtles
    [
      color-who who

      ;; highlight edges connecting the chosen node to its neighbors
      ask my-links [
        if (end1 = a-turtle or end2 = a-turtle)
        [
          ifelse [opinion] of end1 = [opinion] of end2
            [set color [color] of end1]
            [set color black]
          ;
        ]
;        [
;          if (member? end1 neighbor-nodes and member? end2 neighbor-nodes)  ;;  activa triangulos cerrados, agregar else en el if anteriorc y descomentar
;            [ set color yellow ]
;        ]
      ]
    ]
  ]
end 


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

to resize-nodes
  ifelse all? turtles [size <= 1]
  [
    ;; a node is a circle with diameter determined by
    ;; the SIZE variable; using SQRT makes the circle's
    ;; area proportional to its degree
    ask turtles [ set size sqrt count link-neighbors ]
  ]
  [
    ask turtles [ set size 1 ]
  ]
end 

to vote
   go2
   ;; go2 allows user to move nodes
   countactive
   if idea < 1 [stop]
   ;; stop when there are no active links left
   ask one-of turtles [
     ;; chooses only a node with neighbours, otherwise there will be no tick
     ifelse any? link-neighbors [
       ;; let is used so the colour and opinion adopted belong to the same node
       let node [who] of one-of link-neighbors
       set color [color] of turtle node
       set opinion [opinion] of turtle node
       set tick? 1
     ]
     [set tick? 2]
   ]
   if tick? = 1 [ if reprint? [reprint] tick do-plots]
end 


;; to count active links even without colour

to countactive
   set idea 0
   ask links [ if [opinion] of end1 != [opinion] of end2
                [ set idea (idea + 1) ]
             ]
end 


;; to move nodes while voting

to go2
  if mouse-down? [
    ;; find the closest node
    let grabbed min-one-of turtles [distancexy mouse-xcor mouse-ycor]
    ;; loop until the mouse button is released
    while [mouse-down?] [
      ask grabbed [ setxy mouse-xcor mouse-ycor ]
      display
    ]
   ]
end 



;;LATTICE SPECIFICATIONS**********************************************************
;;********************************************************************************
;;********************************************************************************

to setup-Lattice
  clear-all
;;  set network 4
  set-default-shape turtles "circle"
  ask patches [ set pcolor white ]

  set rad 0
  set yrad 0
  ifelse side mod 2 = 0 [ set nume (side / 2) ] [ set nume ((side - 1) / 2) ]
  ask patch 0 0  [ sprout 1 ]

      while [ rad < nume ]
            [ ask patch (rad + 1) 0 [ sprout 1]
              ask patch (-(rad + 1)) 0 [ sprout 1]
              set rad (rad + 1)
            ]

  while [ yrad < nume ]
    [ ask patch 0 (yrad + 1) [ sprout 1 ]
      ask patch 0 (-(yrad + 1)) [ sprout 1 ]
      set rad 0
      while [ rad < nume ]
            [ ask patch (rad + 1) (yrad + 1) [ sprout 1]
              ask patch (-(rad + 1)) (yrad + 1) [ sprout 1]
              ask patch (rad + 1) (-(yrad + 1)) [ sprout 1]
              ask patch (-(rad + 1)) (-(yrad + 1)) [ sprout 1]
              set rad (rad + 1)
            ]
      set yrad (yrad + 1)
    ]

  if wrap
  [ask patch nume 0 [ ask turtles-here
                      [ create-links-with turtles with [(xcor = (- nume)) and (ycor = 0)] ]
                   ]
  set yrad 1
  while [ yrad <= nume ]
    [ ask patch nume yrad [ ask turtles-here
                          [ create-links-with turtles with [(xcor = (- nume)) and (ycor = yrad)] ]
                       ]
      ask patch nume (- yrad) [ ask turtles-here
                              [ create-links-with turtles with [(xcor = (- nume)) and (ycor = (- yrad))] ]
                       ]
      set yrad (yrad + 1)
    ]
  ask patch 0 nume [ ask turtles-here
                      [ create-links-with turtles with [(xcor = 0) and (ycor = (- nume))] ]
                   ]
  set rad 1
  while [ rad <= nume ]
    [ ask patch rad nume [ ask turtles-here
                          [ create-links-with turtles with [(xcor = rad) and (ycor = (- nume))] ]
                       ]
      ask patch (- rad) nume [ ask turtles-here
                              [ create-links-with turtles with [(xcor = (- rad)) and (ycor = (- nume))] ]
                       ]
      set rad (rad + 1)
    ]
  ]

  ask patches [ ask turtles-here [ create-links-with other turtles in-radius 1 ] ]

  reset-ticks
end 

to layout-spr
layout-spring turtles links constant long repulsion
display
end 



;;END OF LATICE SPECIFICATIONS****************************************************
;;********************************************************************************
;;********************************************************************************



;;ERDOS-RENY SPECIFICATIONS*******************************************************
;;********************************************************************************
;;********************************************************************************

to setup-Erdos-Reny
  clear-all
  set network 1
  set-default-shape turtles "circle"
  ask patches [ set pcolor white ]
  create-turtles number-of-nodes
  ask turtles [ setxy random-xcor random-ycor
  ;; ER networks will be given opinion without draw! yet nothing prevents the sceptical
  ;; user to use draw! afterwards, which will only repeat the process
                let col random diversity-of-opinions
                  if col < 1 [ set opinion 1 set color red ]
                  if (col > 0.9) and (col < 2) [ set opinion 2 set color blue ]
                  if (col > 1.9) and (col < 3) [ set opinion 3 set color green ]
                  if (col > 2.9) and (col < 4) [ set opinion 4 set color turquoise ]
                  if (col > 3.9) and (col < 5) [ set opinion 5 set color brown ]
                  if (col > 4.9) and (col < 6) [ set opinion 6 set color violet ]
                  if (col > 5.9) and (col < 7) [ set opinion 7 set color orange ]
              ]
  while [ (count links < links-number) and (count links < number-of-nodes * (number-of-nodes - 1) / 2) ]
        [ ask one-of turtles [ create-link-with one-of other turtles ] ]
  if structure = "circle"
    [ layout-circle turtles (world-width / 2 - 1) ]
  reset-ticks
end 



;;END OF ERDOS-RENY SPECIFICATIONS************************************************
;;********************************************************************************
;;********************************************************************************



;;BARABASI SPECIFICATIONS*********************************************************
;;********************************************************************************
;;********************************************************************************


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

to setup-BA
  clear-all
  set network 2
  ask patches [set pcolor white]
  set-default-shape turtles "circle"
  ;; make the initial network of two (three) turtles and an edge (three)
  make-node nobody        ;; first node, unattached
  make-node turtle 0      ;; second node, attached to first node
  if degree? = "2" [ make-node turtle 1 ask turtle 2 [ create-link-with turtle 0 ] ]
  ;; if degree is two, then adds the node and links
  reset-ticks
end 


;;;;;;;;;;;;;;;;;;;;;;;
;;; Main Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;

to go
  ;; new edge is green, old edges are gray
  ask links [ set color gray - 2 ]
  if degree? = "1"
    [ make-node find-partner         ;; find partner & use it as attachment
    ]                               ;; point for new node
  if degree? = "2"
    [ create-turtles 1
      set partner2 1
      ;; if the future partners turn out to be the same, then partner2 is made 1 and the loop is repeated
      while [partner2 > 0]
        [let turt1 find-partner
         let turtle2 find-partner
          ifelse turt1 = turtle2
            [set partner2 1]
            [set partner2 0 ask max-one-of turtles [who] [create-link-with turt1 create-link-with turtle2]]
            ;; the last turtle added makes the new links
        ]
    ]
  tick
  if layout? [ layout ]
  if plot? [ do-plotting ]
end 

;; used for creating a new node

to make-node [old-node]
  crt 1
  [
    set color red
    if old-node != nobody
      [ create-link-with old-node [ set color green ]
        ;; position the new node near its partner
        move-to old-node
        fd 8
      ]
  ]
end 

;; This code is borrowed from Lottery Example (in the Code Examples
;; section of the Models Library).
;; The idea behind the code is a bit tricky to understand.
;; Basically we take the sum of the degrees (number of connections)
;; of the turtles, and that's how many "tickets" we have in our lottery.
;; Then we pick a random "ticket" (a random number).  Then we step
;; through the turtles to figure out which node holds the winning ticket.

to-report find-partner
  ;; endSearch is a random number up to the total number of links
  let endSearch random-float sum [count link-neighbors] of turtles
  let partner nobody
  ask turtles
  [
    let nc count link-neighbors

    ;; if there's no winner yet...
    if partner = nobody
    [
      ifelse nc > endSearch
        [ set partner self ] ;; Because we now have a partner the ifelse will be ignored now.
        [ set endSearch endSearch - nc ]
    ]
  ]
  report partner
end 


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

to layout
  ;; the number 3 here is arbitrary; more repetitions slows down the
  ;; model, but too few gives poor layouts
  repeat 3 [
    ;; the more turtles we have to fit into the same amount of space,
    ;; the smaller the inputs to layout-spring we'll need to use
    let factor sqrt count turtles
    ;; numbers here are arbitrarily chosen for pleasing appearance
    layout-spring turtles links (1 / factor) (7 / factor) (1 / factor)
    display  ;; for smooth animation
  ]
  ;; don't bump the edges of the world
  let x-offset max [xcor] of turtles + min [xcor] of turtles
  let y-offset max [ycor] of turtles + min [ycor] of turtles
  ;; big jumps look funny, so only adjust a little each time
  set x-offset limit-magnitude x-offset 0.1
  set y-offset limit-magnitude y-offset 0.1
  ask turtles [ setxy (xcor - x-offset / 2) (ycor - y-offset / 2) ]
end 

to-report limit-magnitude [number2 limit]
  if number2 > limit [ report limit ]
  if number2 < (- limit) [ report (- limit) ]
  report number2
end 



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; note that subtle modfications might have been applied to the model by Uri Wilensky:

; *** NetLogo 4.0.2 Model Copyright Notice ***
;
; Copyright 2005 by Uri Wilensky.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from Uri Wilensky.
; Contact Uri Wilensky for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in academic publications, please use:
; Wilensky, U. (2005).  NetLogo Preferential Attachment model.
; http://ccl.northwestern.edu/netlogo/models/PreferentialAttachment.
; Center for Connected Learning and Computer-Based Modeling,
; Northwestern University, Evanston, IL.
;
; In other publications, please use:
; Copyright 2005 Uri Wilensky.  All rights reserved.
; See http://ccl.northwestern.edu/netlogo/models/PreferentialAttachment
; for terms of use.
;
; *** End of NetLogo 4.0.2 Model Copyright Notice ***



;;END OF BARABASI-ALBERT SPECIFICATIONS****************************************
;;*****************************************************************************
;;*****************************************************************************



;;WATTS-STROGATZ SPECIFICATIONS***************************************************
;;********************************************************************************
;;********************************************************************************



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

to startup
  set highlight-string ""
end 

to setup-WS
  clear-all
  ask patches [set pcolor white]
  set network 3
  set infinity 99999  ;; just an arbitrary choice for a large number
  set-default-shape turtles "circle"
  make-turtles

  ;; set up a variable to determine if we still have a connected network
  ;; (in most cases we will since it starts out fully connected)
  let success? false
  while [not success?] [
    ;; we need to find initial values for lattice
    wire-them
    ;;calculate average path length and clustering coefficient for the lattice
    set success? do-calculations
  ]

  ;; setting the values for the initial lattice
  set clustering-coefficient-of-lattice clustering-coefficient
  set average-path-length-of-lattice average-path-length
  set number-rewired 0
  set highlight-string ""
  reset-ticks
end 

to make-turtles
  crt number-of-nodes [ set color gray + 2 ]
  ;; arrange them in a circle in order by who number
  layout-circle (sort turtles) max-pxcor - 1
end 


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

to rewire-one

  ;; make sure num-turtles is setup correctly else run setup first
  if count turtles != number-of-nodes [
    setup-WS
  ]

  ;; record which button was pushed
  set rewire-one? true
  set rewire-all? false

  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 cyan  set rewired? true ] ]

        set number-rewired number-rewired + 1  ;; counter for number of rewirings

        ;; remove the old edge
        die
      ]
    ]
    ;; plot the results
    let connected? do-calculations
  ]
  [ user-message "all edges have already been rewired once" ]
end 

to rewire-all

  ;; make sure num-turtles is setup correctly; if not run setup first
  if count turtles != number-of-nodes [
    setup-WS
  ]

  ;; record which button was pushed
  set rewire-one? false
  set rewire-all? true

  ;; 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.
  while [not success?] [
    ;; kill the old lattice, reset neighbors, and create new lattice
    ask links [ die ]
    wire-them
    set number-rewired 0

    ask links [

      ;; whether to rewire it or not?
      if (random-float 1) < rewiring-probability
      [
        ;; "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
      ]
    ]

    ;; check to see if the new network is connected and calculate path length and clustering
    ;; coefficient at the same time
    set success? do-calculations
  ]
end 

;; do-calculations reports true if the network is connected,
;;   and reports false if the network is disconnected.
;; (In the disconnected case, the average path length does not make sense,
;;   or perhaps may be considered infinite)

to-report do-calculations

  ;; set up a variable so we can report if the network is disconnected
  let connected? true

  ;; find the path lengths in the network
  find-path-lengths

  let num-connected-pairs sum [length remove infinity (remove 0 distance-from-other-turtles)] of turtles

  ;; In a connected network on N nodes, we should have N(N-1) measurements of distances between pairs,
  ;; and none of those distances should be infinity.
  ;; If there were any "infinity" length paths between nodes, then the network is disconnected.
  ;; In that case, calculating the average-path-length doesn't really make sense.
  ifelse ( num-connected-pairs != (count turtles * (count turtles - 1) ))
  [
      set average-path-length infinity
      ;; report that the network is not connected
      set connected? false
  ]
  [
    set average-path-length (sum [sum distance-from-other-turtles] of turtles) / (num-connected-pairs)
  ]
  ;; find the clustering coefficient and add to the aggregate for all iterations
  find-clustering-coefficient

  ;; report whether the network is connected or not
  report connected?
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Clustering computations ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report in-neighborhood? [ hood ]
  report ( member? end1 hood and member? end2 hood )
end 

to find-clustering-coefficient
  ifelse all? turtles [count link-neighbors <= 1]
  [
    ;; it is undefined
    ;; what should this be?
    set clustering-coefficient 0
  ]
  [
    let total 0
    ask turtles with [ count link-neighbors <= 1]
      [ set node-clustering-coefficient "undefined" ]
    ask turtles with [ count link-neighbors > 1]
    [
      let hood link-neighbors
      set node-clustering-coefficient (2 * count links with [ in-neighborhood? hood ] /
                                         ((count hood) * (count hood - 1)) )
      ;; find the sum for the value at turtles
      set total total + node-clustering-coefficient
    ]
    ;; take the average
    set clustering-coefficient total / count turtles with [count link-neighbors > 1]
  ]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Path length computations ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; Implements the Floyd Warshall algorithm for All Pairs Shortest Paths
;; It is a dynamic programming algorithm which builds bigger solutions
;; from the solutions of smaller subproblems using memoization that
;; is storing the results.
;; It keeps finding incrementally if there is shorter path through
;; the kth node.
;; Since it iterates over all turtles through k,
;; so at the end we get the shortest possible path for each i and j.

to find-path-lengths
  ;; reset the distance list
  ask turtles
  [
    set distance-from-other-turtles []
  ]

  let i 0
  let j 0
  let k 0
  let node1 one-of turtles
  let node2 one-of turtles
  let node-count count turtles
  ;; initialize the distance lists
  while [i < node-count]
  [
    set j 0
    while [j < node-count]
    [
      set node1 turtle i
      set node2 turtle j
      ;; zero from a node to itself
      ifelse i = j
      [
        ask node1 [
          set distance-from-other-turtles lput 0 distance-from-other-turtles
        ]
      ]
      [
        ;; 1 from a node to it's neighbor
        ifelse [ link-neighbor? node1 ] of node2
        [
          ask node1 [
            set distance-from-other-turtles lput 1 distance-from-other-turtles
          ]
        ]
        ;; infinite to everyone else
        [
          ask node1 [
            set distance-from-other-turtles lput infinity distance-from-other-turtles
          ]
        ]
      ]
      set j j + 1
    ]
    set i i + 1
  ]
  set i 0
  set j 0
  let dummy 0
  while [k < node-count]
  [
    set i 0
    while [i < node-count]
    [
      set j 0
      while [j < node-count]
      [
        ;; alternate path length through kth node
        set dummy ( (item k [distance-from-other-turtles] of turtle i) +
                    (item j [distance-from-other-turtles] of turtle k))
        ;; is the alternate path shorter?
        if dummy < (item j [distance-from-other-turtles] of turtle i)
        [
          ask turtle i [
            set distance-from-other-turtles replace-item j distance-from-other-turtles dummy
          ]
        ]
        set j j + 1
      ]
      set i i + 1
    ]
    set k k + 1
  ]
end 


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


;; creates a new lattice

to wire-them
  ;; iterate over the turtles
  let n 0
  while [n < count turtles]
  [
    ;; make edges with the next two neighbors
    ;; this makes a lattice with average degree of 4
    make-edge turtle n
              turtle ((n + 1) mod count turtles)
    if dist? = "2"
    [ make-edge turtle n
               turtle ((n + 2) mod count turtles)
    ]
    set n n + 1
  ]
end 


;; connects the two turtles

to make-edge [node1 node2]
  ask node1 [ create-link-with node2  [
    set rewired? false
  ] ]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; note that subtle modfications might have been applied to the model by Uri Wilensky:

; *** NetLogo 4.0.3 Model Copyright Notice ***
;
; Copyright 2005 by Uri Wilensky.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from Uri Wilensky.
; Contact Uri Wilensky for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in academic publications, please use:
; Wilensky, U. (2005).  NetLogo Small Worlds model.
; http://ccl.northwestern.edu/netlogo/models/SmallWorlds.
; Center for Connected Learning and Computer-Based Modeling,
; Northwestern University, Evanston, IL.
;
; In other publications, please use:
; Copyright 2005 Uri Wilensky.  All rights reserved.
; See http://ccl.northwestern.edu/netlogo/models/SmallWorlds
; for terms of use.
;
; *** End of NetLogo 4.0.3 Model Copyright Notice ***



;;END OF WATTS-STROGATZ SPECIFICATIONS********************************************
;;********************************************************************************
;;********************************************************************************



;;TO PLOT, PLOTTING AND PLOTTING2*************************************************
;;********************************************************************************
;;********************************************************************************

to do-plots
  set-current-plot "totals"
  set-current-plot-pen "active"
  plot idea / count links
  if opinions-to-plot > 0 [
    set-current-plot-pen "red"
    plot count links with [ color = red ] / count links
    ]
  if opinions-to-plot > 1 [
    set-current-plot-pen "blue"
    plot count links with [ color = blue ] / count links
    ]
  if opinions-to-plot > 2 [
    set-current-plot-pen "green"
    plot count links with [ color = green ] / count links
     ]
  if opinions-to-plot > 3 [
    set-current-plot-pen "turquoise"
    plot count links with [ color = turquoise ] / count links
     ]
  if opinions-to-plot > 4 [
    set-current-plot-pen "brown"
    plot count links with [ color = brown ] / count links
     ]
  if opinions-to-plot > 5 [
    set-current-plot-pen "violet"
    plot count links with [ color = violet ] / count links
     ]
  if opinions-to-plot > 6 [
    set-current-plot-pen "orange"
    plot count links with [ color = orange ] / count links
     ]
end 


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

to do-plotting ;; plotting procedure
  let max-degree max [count link-neighbors] of turtles

  set-current-plot "Degree Distribution"
  plot-pen-reset  ;; erase what we plotted before
  set-plot-x-range 1 (max-degree + 1)  ;; + 1 to make room for the width of the last bar
  histogram [count link-neighbors] of turtles

  ;; for this plot, the axes are logarithmic, so we can't
  ;; use "histogram-from"; we have to plot the points
  ;; ourselves one at a time
  set-current-plot "Degree Distribution (log-log)"
  plot-pen-reset  ;; erase what we plotted before
  ;; the way we create the network there is never a zero degree node,
  ;; so start plotting at degree one
  let degree 1
  while [degree <= max-degree]
  [
    let matches turtles with [count link-neighbors = degree]
    if any? matches
      [ plotxy log degree 10
               log (count matches) 10 ]
    set degree degree + 1
  ]
end 


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

to do-plotting2

   if rewire-one? [
     ;; plot the rewire-one graph
     set-current-plot "Network Properties WS"
     set-current-plot-pen "apl"
     ;; note: dividing by value at initial value to normalize the plot
     plotxy number-rewired / count links
            average-path-length / average-path-length-of-lattice

     set-current-plot-pen "cc"
     ;; note: dividing by initial value to normalize the plot
     plotxy number-rewired / count links
            clustering-coefficient / clustering-coefficient-of-lattice
   ]

   if rewire-all? [
     ;; plot the rewire-all graph
     set-current-plot "Network Properties WS"
     set-current-plot-pen "apl"
     ;; note: dividing by value at initial value to normalize the plot
     plotxy rewiring-probability
            average-path-length / average-path-length-of-lattice

     set-current-plot-pen "cc"
     ;; note: dividing by initial value to normalize the plot
     plotxy rewiring-probability
            clustering-coefficient / clustering-coefficient-of-lattice
   ]
end 



; *** NetLogo 4.0.2 Model Copyright Notice ***
;
; Copyright 2008 by Pablo Gonz�lez de Prado Salas and Juan Carlos Gonz�lez.
; All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from Pablo Gonz�lez de Prado Salas and Juan Carlos Gonz�lez.
; Contact Pablo Gonz�lez de Prado Salas for appropriate licenses for redistribution for
; profit. pablogps86@gmail.com
;
; To refer to this model in academic publications, please use:
; Pablo Gonz�lez de Prado Salas and Juan Carlos Gonz�lez. (2008).  NetLogo Voter model.
; IFISC
;
; (And please let us know!)
;
; In other publications, please use:
; Copyright 2008 Pablo Gonz�lez de Prado Salas and Juan Carlos Gonz�lez.
; All rights reserved.
; contact pablogps86@gmail.com for terms of use.
;
; *** End of NetLogo 4.0.2 Model Copyright Notice ***

There is only one version of this model, created about 7 years ago by Pablo Gonzalez de Prado.

Attached files

File Type Description Last updated
Voter dynamics in complex networks.png preview Preview for 'Voter dynamics in complex networks' about 7 years ago, by Pablo Gonzalez de Prado Download

This model does not have any ancestors.

This model does not have any descendants.