用Netlogo实现无标度网络下的病毒传播模型

   由于最近的武汉疫情,我只能宅在家里看看集智俱乐部的视频了。在学习完张江老师的课程《Netlogo多主体建模入门
》后,闲着没事,就来整合一下模型库中的Preferential Attachment 模型和 之前学的病毒传播模型,以实现 无标度网络下的病毒传播模型。

  • 集智乐园  《Netlogo多主体建模入门》  https://campus.swarma.org/play/play?id=429

用Netlogo实现无标度网络下的病毒传播模型_第1张图片

模型库中的Preferential Attachment 模型已经能很好得实现无标度模型生成了,所以我简单地整合一下代码就行。

  • 新加的滑块和之前的一样,只不过多了 set-all-susceptible 和 remove-infected

前者是为了实验第二波病毒传播会是怎样的情形,

后者是为了清除所有初始感染者,方便重新设置。

用Netlogo实现无标度网络下的病毒传播模型_第2张图片

  • 绘图方面和之前不变

用Netlogo实现无标度网络下的病毒传播模型_第3张图片

 

Network Status

 

横轴: time

纵轴 : % of nodes

 

  • susceptible

plot (count turtles with [not infected? and not resistant?]) / (count turtles) * 100

  • infected

plot (count turtles with [infected?]) / (count turtles) * 100

  • resistant

plot (count turtles with [resistant?]) / (count turtles) * 100

 

整合的时候有几个要留意的地方:

  • setup2 不用调用初始化节点和网络,也不用layout-spring函数
  • setup2 要删掉 “ clear-all ”
  • setup2 要给变量 infected? 和 resistant? 初始化为布尔值。
  ask turtles [
  set infected? false
  set resistant? false
  ]

不然会报错。因为默认初始化的方法是让这个变量为0。

用Netlogo实现无标度网络下的病毒传播模型_第4张图片

 

模型运行界面:

用Netlogo实现无标度网络下的病毒传播模型_第5张图片

相应代码:

turtles-own
[
  infected?
  resistant?
  virus-check-timer
]

to setup
  clear-all
  set-default-shape turtles "circle"
  ;; make the initial network of two turtles and an edge
  make-node nobody        ;; first node, unattached
  make-node turtle 0      ;; second node, attached to first node
  reset-ticks
end


to go
  ;; new edge is green, old edges are gray
  ask links [ set color gray ]
  make-node find-partner         ;; find partner & use it as attachment
                                 ;; point for new node
  tick
  if layout? [ layout ]
end

to setup2
  ask turtles [
  set infected? false
  set resistant? false
  ]
  ask n-of initial-outbreak-size turtles
    [ become-infected ]  ;; 初始化指定数量的感染者

  reset-ticks
end

to set-all-susceptible
  ask turtles [
    set color blue
  ]
end

to remove-infected
    ask turtles with [color = red][
    set color blue
  ]

end

to spread
  if all? turtles [not infected?] ;; 如果全部的状态为未被感染,那就停止运行
    [ stop ]
  ask turtles
  [
     set virus-check-timer virus-check-timer + 1 ;;每次go,都给 “距离上一次病毒检查过去了多少时间步” 记一次时
     if virus-check-timer >= virus-check-frequency ;; 定期做检查
       [ set virus-check-timer 0 ] ;; 置0
  ]
  ;;调用函数,实现感染者传播病毒
  spread-virus

   ;; 调用函数,实现健康者定期做治疗
  do-virus-checks

  tick
end

;; used for creating a new node
to make-node [old-node]
  create-turtles 1
  [
    set color blue
    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 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

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

;; 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 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 [number limit]
  if number > limit [ report limit ]
  if number < (- limit) [ report (- limit) ]
  report number
end


; Copyright 2005 Uri Wilensky.
; See Info tab for full copyright and license.


;; -------------------------------------------------------------------------------

to become-infected  ;; 个体被感染
  set infected? true
  set resistant? false
  set color red
end

to become-susceptible  ;; 个体变成易感的
  set infected? false
  set resistant? false
  set color blue
end

to become-resistant  ;; 个体被获得病毒抗性
  set infected? false
  set resistant? true
  set color gray
  ask my-links [ set color gray - 2 ] ;; 让无效节点的连边颜色变灰
end

to spread-virus ;; 感染者传播病毒
  ask turtles with [ infected? ]
    [ ask link-neighbors with [not resistant?]
        [ if random-float 100 < virus-spread-chance
            [ become-infected ] ] ]
end

to do-virus-checks  ;; 健康者定期做治疗
  ask turtles with [infected? and virus-check-timer = 0]
  [
    if random 100 < recovery-chance  ;; 有一定概率治愈
    [
      ifelse random 100 < gain-resistance-chance
        [ become-resistant ]    ;;在治愈的基础上,有一定概率能获得病毒抗性
        [ become-susceptible ]  ;;不够幸运,未能获得病毒抗性的人,重新变成易感的
    ]
  ]
end

附件在这,想探索的同学自行下来玩玩:

https://share.weiyun.com/5HwOVCb

你可能感兴趣的:(Netlogo多主体建模入门,数据仿真,Netlogo,Netlogo多主体建模入门,数据仿真,Netlogo)