If the Java does not work on your machine, you can download the model file and run in on your computer. You need to have NetLogo installed on your computer.
Download model file: NetLogoModel.nlogo
turtles-own [belief ;; belief on the likelihood the rumor being true
converted? ;; whether the turtle just receive the message and believe
distance-from-other-turtles ;; list of distances of this node from other turtles
node-clustering-coefficient
]
globals
[
clustering-coefficient ;; the clustering coefficient of the network; this is the
;; average of clustering coefficients of all turtles
average-path-length ;; average path length of the network
infinity ;; a very large number.
;; used to denote distance between two turtles which
;; don't have a connected or unconnected path between them
]
to setup
clear-all
set infinity 9999999 ;; just an arbitrary choice for a large number
set-default-shape turtles "circle"
ask patches [set pcolor white]
if network-type = "small-world" [ ;; generate a small-world network
create-turtles node-count
layout-circle sort turtles (world-width / 2 - 2)
let d 1.001 * [distance-to-nearest-neighbor] of one-of turtles
ask turtles
[create-links-with other turtles in-radius (d * neighbor-count / 2)]
rewire
]
if network-type = "scale-free" [ ;; generate a scale-free network
make-node nobody ;; first node, unattached
make-node turtle 0 ;; second node, attached to first node
repeat node-count - 2 [
make-node find-partner
layout
]
layout
]
ask turtles [
ifelse binary-belief?
[set belief random 2]
[set belief random-float 1 ]
;[set belief (random 2) * 2 - 1 ]
;[set belief random-float 2 - 1 ]
if show-belief? [
set label precision belief 2
set label-color black
set color 9
]
]
ask one-of turtles with [belief > 0.5] [
set color red
set converted? true
;if devout-spreader? [ set belief 1]
set belief 1
if show-belief? [set label precision belief 2]
]
do-calculations
reset-ticks
end
to go
if not any? turtles with [converted? = true] [stop]
ask turtles with [converted? = true] [ ;; update the beliefs of neighbors of newly converted node
set converted? false
ask link-neighbors with [ color = 9] [
set belief (belief + [belief] of myself) / 2
; set belief (belief + 1) / 2
if show-belief? [set label precision belief 2]
if belief > 0.5 [
set color red
set converted? true
]
]
]
tick
end
;;
;; Procedures for small-world network
to-report distance-to-nearest-neighbor ;; turtle method. spatial, NOT graph distance!
report distance min-one-of other turtles [distance myself]
end
to rewire ;; the Watts - Strogatz algorithm to rewire a small world network
ask turtles
[foreach sort my-links ;; 'sort' just to get a list
[if random-float 1 < p
[ask ? [die]
create-link-with one-of other turtles
with [not link-neighbor? myself]]]]
;ask turtles [ ;; make sure all turtles are connected
; if count link-neighbors = 0 [ create-link-with one-of other turtles ]
;]
end
;;
;; Procedures for scale-free network
;; used for creating a new node
to make-node [old-node]
crt 1
[
set color gray
if old-node != nobody
[ create-link-with old-node
;; position the new node near its partner
move-to old-node
fd 8
]
]
end
;; This code is the heart of the "preferential attachment" mechanism, and acts like
;; a lottery where each node gets a ticket for every connection it already has.
;; While the basic idea is the same as in the Lottery Example (in the Code Examples
;; section of the Models Library), things are made simpler here by the fact that we
;; can just use the links as if they were the "tickets": we first pick a random link,
;; and than we pick one of the two ends of that link.
to-report find-partner
report [one-of both-ends] of one-of links
end
to layout
;; the number here is arbitrary; more repetitions slows down the
;; model, but too few gives poor layouts
repeat 10 [
;; 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) (15 / 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 [number limit]
if number > limit [ report limit ]
if number < (- limit) [ report (- limit) ]
report number
end
;;
;; Calculate the statistics of the graph
;; 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 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
set average-path-length (sum [sum distance-from-other-turtles] of turtles) / (num-connected-pairs)
if average-path-length > infinity / num-connected-pairs
[set average-path-length -1] ;; when the graph is not connected, set average path length to -1
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
;; 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
;show 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
;show distance-from-other-turtles
]
]
;; infinite to everyone else
[
ask node1 [
set distance-from-other-turtles lput infinity distance-from-other-turtles
;show 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
;show distance-from-other-turtles
]
]
set j j + 1
]
set i i + 1
]
set k k + 1
]
end
;; Huanren Zhang June 2014 @GWCSS