有效停用社交网络中特定代理人与品种之间的特定链接 [英] Effectively deactivating specific links between specific agents and breeds in a social network

查看:102
本文介绍了有效停用社交网络中特定代理人与品种之间的特定链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于以下内容,在NetLogo中建立了不同年龄的座席社交网络,这导致通过链接连接的座席圈.这些链接的总体目的是代表这些链接之间的联系.该模型模拟了通过网络传播感染的情况.代理一开始就很容易受到感染,如果他们与感染性邻居发生接触,就有可能被感染.我想建模例如感染者的隔离或隔离.即他们与他人的链接将被完全停用,或者至少他们的大多数链接将被停用.理想情况下,我将在观察者界面上按一个按钮以停用受感染代理的链接.我还希望能够建立一个关闭学校的模型,例如,大多数幼儿和儿童及其彼此之间的联系将被停用,从而停止儿童之间的感染能力.如果数量减少,它们与成年动物的联系应该保持开放,尽管现在最好一次只关注一个品种之间的联系.

A social network of agents of different ages is set up in NetLogo similarly to the following, which results in a circle of agents connected by links. The overall purpose of these links is to represent contact between those links. The model simulates the spread of infection through the network. Agents begin as susceptible, with the possibility of becoming infected if they come into contact with an infectious link neighbour. I want to model for example the isolation or quarantine of an infected individual. i.e. their links to others would be deactivated completely or at least the majority of their links would be deactivated. Ideally I'd press a button on the observer interface to deactivate the links of infected agents. I'd also like to be able to model the closure of a school for instance whereby the majority of toddlers and children and their connections between each other would be deactivated, stopping the ability of infection between children. Their links to adults should probably remain open if fewer of them, though it may be better to just focus on links between one breed at a time for now.

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

如您所见,这里有两个问题,一个是根据终端节点处于什么状态的链接的停用(理想情况下,一旦学校关闭或一旦感染者恢复就可以再次打开它们)或它们是哪个品种.

As you can see there are two problems here, one being the deactivation (ideally they could be turned on again once school closures are over or once an infected individual recovers for example) of links based on what state the end nodes are in or the breed that they are.

第二个问题是计算被感染的链接邻居的数量而不包含已停用的链接.隐藏链接不会阻止它的存在,只是变得不可见,因此不会阻止联系.我还考虑过简单地将停用链接的颜色更改为例如黑色.有没有一种方法可以重写受感染邻居的计数,以仅计数非黑色或隐藏的链接(例如set num-infected-neighbors count (link-neighbors with [infected? = true] ...)和隐藏的链接? = false ....或link-color!="黑色? )

And the second problem of counting the number of infected link-neighbours without including the links which are deactivated. Hiding a link does not stop it from existing it just becomes invisible and thus doesn't stop the contact. I've also considered simply changing the color of the deactivated links to for example black. Is there a way in which I can rewrite the counting of infected neighbors to count only links that aren't black or hidden for example set num-infected-neighbors count (link-neighbors with [infected? = true]...and link hidden? = false.... or link-color "!=" black? )

我觉得第二个问题可能是两者中比较容易的一个,但我可能会误会.我在这个阶段打了太多的砖墙,对此事,我的头被炸了.任何帮助将不胜感激.感谢您花时间阅读本文章,我意识到这有点麻烦:)再次感谢Nicolas Payette之前的帮助

I feel the second problem is probably the easier of the two but I may be mistaken. I've hit too many brick walls at this stage and my head is fried on the matter. Any help would really be greatly appreciated. Thanks for your time reading this already, I realise it was a bit of a rant :) Thanks again to Nicolas Payette for his help previously

为关闭添加了链接拥有的布尔值?

Added links-own boolean for closed?

试图更改计算打开链接上被感染邻居数量的部分,但出现错误 this code can't be run by a link error while link 14 15 running SET called by procedure SPREAD called by Button 'Spread'

Tried to alter the section which counts the number of infected neighbours on open links but I get the error this code can't be run by a link error while link 14 15 running SET called by procedure SPREAD called by Button 'Spread'

还在关闭学校"中添加了一个功能,该功能通过将关闭设置为关闭,从而基本关闭了所有幼儿的链接?真实

Also added in a function in "to close-schools" which gives a very basic closure of all toddlers links by setting closed? to true

这可能是这里的解决方案

Would this be a possible solution here

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似乎始终超过乌龟拥有的链接邻居的实际数量.这也是错误的.我不明白为什么...

num-infected-neighbors should be a turtles-own variable as far as I can tell, but when I put a watch on a turtle and run the simulation, the num-infected-neighbors that a turtle has seems to consistently surpass the actual number of link-neighbors that the turtle has. It is also just wrong. I can't see why though....

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

似乎也无法正常工作...

doesn't seem to be working properly either...

-

set num-infected-neighbors length filter [[infected?] of ?]
                        [other-end] of my-links with [not closed?]

首先在未关闭的链接上形成一个由链接邻居组成的列表,然后对其进行过滤以仅给出被感染的列表. num-infected-neighbors是该列表的长度

First a list is formed of the link-neighbors on not-closed links, then that is filtered to give just the infected ones. num-infected-neighbors is the length of that list

推荐答案

首先提供一些一般性建议:最好给出一个最小的工作示例(MWE),即,将其分解为最起码的示例以显示问题.该代码的细节太多,因此人们回答的可能性较小.另外,有时候,一旦将其剥离,就会找出答案.该代码也不起作用:缺少全局变量的定义.

First some general advice: It would be better to give a minimum working example (MWE), i.e. one that's stripped down to the bare minimum to show the problem. This code has too much detail, so people are less likely to answer. Also, sometimes once you strip it down, you'll figure out the answer. This code doesn't work, either: The definitions for the global variables are missing.

您可能想看看NetLogo随附的模型库中的网络上的病毒".它做类似的事情,但是使用了更多的"NetLogoey"编码风格.

You might want to take a look at "Virus on a Network" in the Models Library that comes with NetLogo. It does similar things, but using a more "NetLogoey" coding style.

您可以测试链接的颜色.也就是说,您可以使用链接是已停用"颜色这一事实来测试是否应该对它进行计数或遵循.更好的主意是使用links-own为链接提供变量,然后对其进行测试.但是,以下是一些示例代码(从网络上的病毒"中的过程进行了修改),该示例为此目的使用了颜色:

You can test links for their color. i.e. you could use the fact that a link is the "deactivated" color to test whether it should be counted, or followed. A better idea would be to give the links a variable using links-own, and test that. But here is some example code (modified from a procedure in "Virus on a Network") that uses color for this purpose:

to spread-virus
  ask turtles with [infected?]
    [ ask my-links with [color != red]
      [ask other-end
        [ if (not resistant?) and (random-float 100 < virus-spread-chance)
            [ become-infected ] ] ] ]
end

诀窍在于,我不直接看link-neighbors,而是要求每只乌龟提供my-links,然后测试它们的颜色.然后对于通过测试的每个链接,要求它提供other-end,即不是通过my-links找到链接的乌龟的乌龟.然后,病毒会传播给该乌龟-但前提是尚未禁用该链接. (我认为该代码正常工作-似乎可以.但是,如果您这样做,则应进行彻底测试.)使用my-links计数禁用或非禁用链接应该更加容易.对于isolate-infected,您可以执行ask turtles with [infected?] [ask my-links [set color/deactivated/etc之类的操作. ]].还没有测试.

The trick is that rather than looking directly at link-neighbors, I ask each turtle for its my-links, then test their colors. Then for each link that passes the test, I ask it for its other-end, i.e. the turtle that is not the turtle that found the link via my-links. Then the virus is transmitted to that turtle--but only if the link had not been disabled. (I think this code works right--seems to. But you should test it thoroughly if you do something like this.) Counting disabled or non-disabled links using my-links should be even easier. For isolate-infected you can do something like ask turtles with [infected?] [ask my-links [set color/deactivated/etc. ]]. Haven't tested that.

对于响应于我的回答的编辑版本中的新错误,我认为问题可能是此代码:

For the new error in the edited version in response to my answer, I think the problem might be this code:

ask my-links with [closed? = false][
  set num-infected-neighbors count (link-neighbors with [infected? = true])
]

这要求一些链接来运行过程link-neighbors,但是该过程仅设计为由乌龟运行. (在NetLogo中,某些过程仅设计为由代理"运行,例如,在ask ...之后的花括号内.但是,其中一些过程仅在龟代理中起作用,而其他过程仅在链接代理中起作用.)可能应该使用other-end,这是一个设计为由链接运行的过程. (不过,这是一个有趣的过程,因为它仅设计为由链接运行,而要求链接运行other-endask命令是由乌龟调用的ask.因此,other-end仅当您拥有ask turtle 1 [ask mylinks [ ... other-end ...]]之类的东西时才有效.)

This asks some links to run the procedure link-neighbors, but that procedure is only designed to be run by turtles. (In NetLogo, certain procedures are only to be designed to be run "by an agent", e.g. within braces after ask .... However, some of these procedures only work in turtle agents, and others only work in link agents.) You probably should use other-end, which is a procedure designed to be run by a link. (It's kind of a funny procedure, though, because it's only designed to be run by a link when the ask command that's asking the link to run other-end, was an ask invoked by a turtle. So other-end only works if you've got something like ask turtle 1 [ask mylinks [ ... other-end ...]].)

(可以在第一个座席ask所引用的另一个座席中引用座席.对于其中一个级别,可以使用myself.对于ask s的其他级别,可以在以下位置定义变量外部层次,然后在内部层次中引用它们.)

(It is possible to reference an agent inside another agent that the first agent asked. For one level of this, you can use myself. For additional levels of asks, you can define variables at the outer levels and then reference them in the inner levels.)

这篇关于有效停用社交网络中特定代理人与品种之间的特定链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆