globals [ seeds ;; stores the initial seeds roundActiveCount ;; stores the number of agents who turned active in the previous round beta ;; influence weight parameter theta ;; activation threshold global-activation-rate ;; overall activation rate of the network neighbor-activation-rate ;; activation rate of neighbors ]

turtles-own [ threshold ;; threshold of adoption for each agent active? ;; whether this agent is active or not weight-sum ;; used to sum up influence weight from active neighbors neighbor-count ;; number of active neighbors ]

links-own [ end1-weight ;; influence weight exerted by the end2 on end1 end2-weight ;; vice versa ]

to setup-network clear-all

ifelse network-type = 'preferential attachment' [ setup-preferential-attachment-network ] [ ifelse network-type = 'small world' [ setup-small-world-network ] [ setup-clustered-network ] ]

set-edge-weights set-thresholds end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;Preferential Atachment Network;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup-preferential-attachment-network set-default-shape turtles 'person'

;; make the initial network of two turtles and an edge make-node nobody ;; first node, unattached make-node turtle 0

;; now add rest of the nodes repeat number-of-nodes - 2 [ make-node find-partner layout-preferential-attachment-network ] end

;; used for creating a new node to make-node [old-node] crt 1 [ set color gray + 2 set active? false set size 2 if old-node != nobody [ create-link-with old-node [ set color cyan ] ;; 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 let total 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 > total [ set partner self ] [ set total total - nc ] ] ] report partner end

;; lays out the preferential attachment network in aesthetic way to layout-preferential-attachment-network ;; the number 3 here is arbitrary; more repetitions slows down the ;; model, but too few gives poor layouts repeat 5 [ ;; 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 [number limit] if number > limit [ report limit ] if number < (- limit) [ report (- limit) ] report number end

;;;;;;;;;;;;;;;;;;;;;;; ;;;Small World Network;;; ;;;;;;;;;;;;;;;;;;;;;;;

to setup-small-world-network

make-turtles ;; we need to setup the initial lattice wire-them rewire-all end

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

;; 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) 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 color cyan ] ] end

to rewire-all

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

ask links [

let rewired? false
;; 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
  ]
]
;; remove the old edge
if (rewired?)
[
  die
]

]

end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;Spatially Clustered Network;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup-clustered-network setup-nodes setup-spatially-clustered-network end

to setup-nodes set-default-shape turtles 'person' crt number-of-nodes [ ; for visual reasons, we don't put any nodes too close to the edges setxy (random-xcor * 0.95) (random-ycor * 0.95) set color gray + 2 set active? false set size 2 ] end

to setup-spatially-clustered-network let num-links (average-node-degree * number-of-nodes) / 2 while [count links < num-links ] [ ask one-of turtles [ let choice (min-one-of (other turtles with [not link-neighbor? myself]) [distance myself]) if choice != nobody [ create-link-with choice [set color cyan] ] ] ] ; make the network look a little prettier repeat 10 [ layout-spring turtles links 0.3 (world-width / (sqrt number-of-nodes)) 1 ] end

;;;;;;;;;;;;;;;;;; ;;;Resize Nodes;;; ;;;;;;;;;;;;;;;;;;

;; resize-nodes, change back and forth from size based on degree to a size of 1 to resize-nodes ifelse all? turtles [size <= 2] [ ;; 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 2 ] ] end

;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;Linear Threshold Model;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; sets edge weights such that sum of incoming edge weights equal to 1, ;; and each incoming edge for an agent has same weight to set-edge-weights ask turtles [ if count my-links != 0 [ let weight 1.0 / count my-links ask my-links [ set-weight myself weight ] ] ] end

;; link procedure to set-weight [node weight] ifelse end1 = node [ set end1-weight weight ] [ set end2-weight weight ] end

;; set agent thresholds before every run to set-thresholds clear-all-plots

ask links [set color cyan]

ask turtles [ set active? false set color gray + 2 set neighbor-count 0 ]

ifelse seeds = 0 [ set roundActiveCount 0 ] [ set roundActiveCount count seeds
ask seeds [
set active? true set color magenta ] ]

ask turtles [ ifelse threshold-fixed-to-0.5? [set threshold 0.5][set threshold random-float 1] ]

reset-ticks end

;; selct seeds by mouse; overrides the number-of-seeds parameter to select-seeds-by-mouse ;; remove previous highlight ask turtles [if color = pink [set color gray + 2]] if mouse-inside? [ highlight if mouse-down? [select-this-node-as-seed] ] display end

to highlight ;; getting the node closest to the mouse let node min-one-of turtles [distancexy mouse-xcor mouse-ycor] if node != nobody [ ;; highlight the chosen node ask node [if color != magenta [set color pink]] ] end

to select-this-node-as-seed ;; getting the node closest to the mouse let node min-one-of turtles [distancexy mouse-xcor mouse-ycor] if node != nobody [ ;; select the chosen node as seed ask node [ set color magenta set active? true ifelse seeds = 0 [ set seeds turtle-set node ] [ set seeds (turtle-set node seeds) ] set roundActiveCount roundActiveCount + 1 ] ] end

;; selects seeds uniformly at random from the set of all agents to select-seeds-randomly set seeds n-of number-of-seeds turtles set roundActiveCount count seeds ask seeds [ set active? true set color magenta ] end

;; Influence propagation to go if roundActiveCount = 0 [stop]

set roundActiveCount 0

propagate-influence turn-active

tick end

to propagate-influence ask turtles [set weight-sum 0.0] ask turtles with [active?] [ ask my-links [ influence myself set color yellow ] ]
end

;; influencer influences the other end of the lingk; link procedure to influence [influencer] ifelse end1 = influencer [ let weight end2-weight ask end2 [set weight-sum weight-sum + weight] ] [ let weight end1-weight ask end1 [set weight-sum weight-sum + weight] ] end

;; decision to turn active or not to turn-active ask turtles with [not active?] [ if (beta * neighbor-activation-rate) + ((1 - beta) * global-activation-rate) >= threshold [ set active? true set color magenta set roundActiveCount roundActiveCount + 1 ] ] End

to calculate-activation-rates set global-activation-rate (count turtles with [active?]) / count turtles set neighbor-activation-rate (sum [neighbor-count] of turtles) / (sum [count link-neighbors] of turtles) if global-activation-rate = 0 [stop] print ["global-activation-rate: " global-activation-rate] print ["neighbor-activation-rate: " neighbor-activation-rate] print ["beta: " beta] print ["theta: " theta] print ["---------------------------"] end

to update-neighbor-count ask turtles with [active?] [ ask my-links [ ifelse end1 = myself [ ask end2 [set neighbor-count neighbor-count + 1] ] [ ask end1 [set neighbor-count neighbor-count + 1] ] ] ] end

to go if roundActiveCount = 0 [stop]

set roundActiveCount 0

propagate-influence turn-active update-neighbor-count calculate-activation-rates tick end


原文地址: http://www.cveoy.top/t/topic/nOeU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录