在 NetLogo 中建立了一个不同年龄代理的社交网络,类似如下,从而形成一个由链接连接的代理圈。这些链接的总体目的是代表这些链接之间的联系。该模型模拟了感染通过网络的传播。代理开始时是易感的,如果他们与传染性链接邻居接触,就有可能被感染。例如,我想模拟受感染个体的隔离或隔离。即他们与其他人的链接将被完全停用,或者至少他们的大部分链接将被停用。理想情况下,我会按下观察者界面上的按钮来停用受感染代理的链接。我' d 还希望能够模拟关闭学校,例如,大多数幼儿和儿童以及他们之间的联系将被停用,从而阻止儿童之间的感染能力。如果它们数量较少,它们与成年人的联系可能应该保持开放,尽管现在一次只关注一个品种之间的联系可能会更好。
breed [ toddlers toddler ]
breed [ children child ]
breed [ adults adult ]
breed [ over45s over45 ]
globals
[
num-nodes
num-infected
num-susceptible
prob-infection-toddler
prob-infection-child
prob-infection-adult
prob-infection-over45
force-of-infection
]
turtles-own
[
temp-infected? ;; temporary infected state
infected? ;; true if agent has been infected
susceptible?
num-infected-neighbors
]
links-own
[
closed?
]
to setup
clear-all
create-toddlers 20
create-children 20
create-adults 20
create-over45s 20
create-network
reset-ticks
layout-circle (sort turtles) max-pxcor - 8
ask turtles
[
facexy 0 0
if who mod 2 = 0 [fd 4]
]
ask turtles
[set susceptible? true]
ask one-of turtles
[
set infected? true
set susceptible? false
set color red
]
display
end
to create-network
let connexions (list
(list toddlers toddlers 5)
(list toddlers children 2)
(list toddlers adults 2)
(list toddlers over45s 1)
(list children toddlers 3)
(list children children 8)
(list children adults 5)
(list children over45s 1)
(list adults toddlers 1)
(list adults children 3)
(list adults adults 6)
(list adults over45s 3)
(list over45s toddlers 1)
(list over45s children 1)
(list over45s adults 5)
(list over45s over45s 5)
)
foreach connexions [
let source-breed item 0 ?
let target-breed item 1 ?
let num-connexions item 2 ?
let reverse-num-connexions item 2 first filter [
item 0 ? = target-breed and item 1 ? = source-breed
] connexions
ask source-breed [
repeat num-connexions [
let possible-targets other target-breed with [
(not member? myself link-neighbors) and
(count link-neighbors with [ breed = source-breed ] < reverse-num-connexions)
]
let target one-of possible-targets
if target != nobody [ create-link-with target ]
]
]
]
ask links [set closed? false]
end
to spread
;;; there is one of these for each age group as they have different probabilities for infection
ask toddlers with [ susceptible? = true ]
[
;;tried changing to something like this but this is the line that throws up an error
ask my-links with [closed? = false][
set num-infected-neighbors count (link-neighbors with [infected? = true])
]
;;should only include active links
set force-of-infection (prob-infection-toddler) ^ num-infected-neighbors ;;paraphrased equation but num-infected-neigbours is important
if ( random-float 1 <= force-of-infection) ;; infect with probability p
[
set temp-infected? true
set susceptible? false
]
]
ask turtles with [temp-infected? = true]
[
set infected? true
set temp-infected? false
]
end
to isolate-infected
;;for all infected individuals deactivate their links
end
to close-schools
ask toddlers
[
ask my-links [set closed? true]
]
end
to close-offices
;; e.g cut the number of links between adults and so on
end
正如您所看到的,这里有两个问题,一个是根据终端节点所处的状态或品种来停用链接(理想情况下,一旦学校停课结束或一旦受感染的个体恢复,它们可以再次打开)他们是。
第二个问题是计算受感染链接邻居的数量,而不包括停用的链接。隐藏链接并不能阻止它的存在,它只是变得不可见,因此不会停止联系。我还考虑过简单地将停用链接的颜色更改为例如黑色。有没有一种方法可以重写受感染邻居的计数以仅计算非黑色或隐藏的链接,例如set num-infected-neighbors count (link-neighbors with [infected? = true]
......并且链接隐藏?= false .... 或链接颜色“!=”黑色?)
我觉得第二个问题可能是两者中较容易的一个,但我可能弄错了。在这个阶段我遇到了太多的砖墙,我的头被这件事炒了。任何帮助将不胜感激。感谢您花时间阅读本文,我意识到这有点咆哮 :) 再次感谢 Nicolas Payette 之前的帮助
编辑:为关闭添加了链接自己的布尔值?
试图更改计算打开链接上受感染邻居数量的部分,但出现错误
this code can't be run by a link
error while link 14 15 running SET
called by procedure SPREAD
called by Button 'Spread'
还在“关闭学校”中添加了一个功能,该功能通过设置关闭来提供所有幼儿链接的非常基本的关闭?为真
编辑:这会是一个可能的解决方案吗
set num-infected-neighbors 0
ask my-links with [closed? = false][
ask other-end [if (infected?) [set num-infected-neighbors num-infected-neighbors + 1 ]]
]
set force-of-infection 1 - (1 - prob-infection-adult) ^ num-infected-neighbors
编辑:据我所知,num-infected-neighbors 应该是一个turtles-own
变量,但是当我将手表放在乌龟身上并运行模拟时,乌龟的 num-infected-neighbors 似乎一直超过实际数量乌龟拥有的链接邻居。这也是错误的。虽然我不明白为什么....
编辑:
let infectnum 0
set num-infected-neighbors 0
ask my-links with [closed? = false][
ask other-end [if (infected?) [set infectnum infectnum + 1 ]]
;;set num-infected-neighbors count (link-neighbors with [infected? = true])
]
set num-infected-neighbors infectnum
似乎也不能正常工作......
编辑:供将来参考问题已解决 -
set num-infected-neighbors length filter [[infected?] of ?]
[other-end] of my-links with [not closed?]
首先,由未关闭链接上的链接邻居组成一个列表,然后对其进行过滤以仅提供受感染的链接。num-infected-neighbors 是该列表的长度