以下是修改后的代码:

globals [ seeds ;; stores the initial seeds roundActiveCount ;; stores the number of agents who turned active in the previous round global-threshold ;; stores the global threshold for the network ]

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-penetration-rate ;; penetration rate 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-node

globals seeds ;; stores the initial seeds roundActiveCount ;; stores the number of agents who turned active in the previous roundturtles-own threshold ;; threshold of adoption for each agent a

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

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