一次检查两种不同类型代理的条件

checking condition for two different breed of agents at once

我有蚊子和人类两种特工。他们都有不同的状态,如易感和感染。 我想检查我的代理的状态并执行操作 我想检查蚊子是否被感染并且人类易感然后感染人类,反之亦然。 请帮助代码

to go
  ask human
  [
    set heading  random 360
    forward random 2
      
      ]
  **if any? human-here infected? true and any? mosquito-here infected? false
ask mosquito [ set infected? true
    set color red]
  tick** 
end

您首先需要决定要让哪些特工采取必要的行动来感染或被感染。为此,我建议使用“with”原语,它允许您创建代理集的子集,以便检查它们是否可以被感染。 humans with [susceptible? = true and infected? = false]

之后你可以要求这个小组检查自己被感染的必要条件。 if any? mosquitos-here with [infected? = true] [set infected? true]

我建议对人类和蚊子分别进行此操作,因为两者的条件可能不同。合并后,您的代码可能如下所示:

to go
  ask humans
  [
    set heading  random 360
    forward random 2
      ]
  
  ask mosquitos
  [
    set heading  random 360
    forward random 2
      ]  
  
  ask humans with [susceptible? = true and infected? = false] [
    if any? mosquitos-here with [infected? = true] [set infected? true set color red]
]
  
  ask mosquitos with [susceptible? = true and infected? = false] [
    if any? humans-here with [infected? = true] [set infected? true set color red]
]
  
  tick
end

如果感染以对称方式发生,你也可以使用类似下面的方法,但我不建议这样做,因为我假设你想要区分人类和蚊子

  ask turtles with [susceptible? = true and infected? = false] [
    let my-breed breed
    if any? turtles-here with [infected? = true and breed != my-breed] [set infected? true set color red]
]