【问题标题】:Converting agentset into list in NetLogo将代理集转换为 NetLogo 中的列表
【发布时间】:2018-08-04 03:31:48
【问题描述】:

我正在编写一些代码来为流感流行人群中的个体接种疫苗。疫苗有两种(代码中命名为HOV和HEV); HOV 已经完成。我尝试/想要做的是将蓝海龟的代理集转换为一个列表,然后我可以一次迭代一只海龟。我需要这样做,因为 HEV 状况必须为每只蓝龟接种略有不同的疫苗(即每种疫苗中的流感毒株必须略有不同)。但是,NetLogo 突出显示“foreach [vax-turtles]”,并带有一条错误消息,上面写着“期望一个文字值”。

我的代码在下面,“如果疫苗=“HEV””这一段代码是我需要帮助的:

extensions [csv]
globals [strain_list_list epidemic-threshold cumulative-infections proportion-of-infection currently-infected peak-prevalence vax-strain]
turtles-own [infection-period infection-number tflu-strain immune-label ant-distance cross-immunity]
patches-own [fomite-age pflu-strain]

to setup
  clear-all
  setup-turtles
  set strain_list_list (list t-sorted-strain-list)
  print strain_list_list
  reset-ticks
end

to-report t-sorted-strain-list
  report map [i -> [tflu-strain] of i] sort turtles
end

to setup-turtles
  create-turtles 100
  ask turtles [setxy random-xcor random-ycor]
  ask turtles [set color white set infection-number 0 set immune-label -999999999999 set tflu-strain -999999999999]
  ask one-of turtles [set color red set infection-period 0 set infection-number 1 set immune-label 0 set tflu-strain 0]
  if vaccine = "HOV" [
    ifelse (one-of [1 2] = 1) [
      set vax-strain (random ((2 * drift-size) + 1))]
    [set vax-strain (-1 * random ((2 * drift-size) + 1))]
  ask n-of prop-vax turtles with [color = white] [set color blue set immune-label vax-strain]
  ]
  if vaccine = "HEV" [
    let vax-turtles (list turtles with [color = blue])
    ask n-of prop-vax turtles with [color = white] [set color blue] 
    foreach [vax-turtles] [
      ifelse (one-of [1 2] = 1) [
      set vax-strain (random ((2 * drift-size) + 1))]
    [set vax-strain (-1 * random ((2 * drift-size) + 1))]
      ]
    ]
  set epidemic-threshold "no"
  set cumulative-infections 0
  set peak-prevalence 0
end

to go
  if ticks = flu-season-length [stop]
  move-infecteds
  move-uninfecteds
  transmit
  mutate
  update-immunity
  track-infecteds
  set cumulative-infections (count turtles with [infection-period = 1] + cumulative-infections)
  set proportion-of-infection (100 - (count turtles with [immune-label = -999999999999]))
  set currently-infected (count turtles with [infection-period = 1])
  csv:to-file "strains_each_tick.csv" strain_list_list
  set strain_list_list lput t-sorted-strain-list strain_list_list
  tick
end

to move-uninfecteds ;; uninfected turtles move faster than infected ones
  ask turtles with [color = white or color = blue] [
    right random 360
    forward 5
  ]
end

to move-infecteds ;; infected turtles move slower than uninfected ones and always transmit infection to patches before they leave them
  ask turtles with [color = red] [
  if pcolor = black [
      set pcolor red
    set fomite-age 0
    set pflu-strain tflu-strain]
  right random 360
  forward 3
]
end

to transmit ;; uninfected turtles are infected by fomites (red patches) with some probability. immune-labelling currently first infection
  ask turtles with [color = white or color = blue and pcolor = red] [
    if immune-label != pflu-strain [
      set ant-distance (abs (immune-label - pflu-strain))
      set cross-immunity (natural-immunity * (1 - (ant-distance / antigenic-distance-limit)))
    if cross-immunity < 0 [set cross-immunity 0]
    if random 100 < (((100 - cross-immunity) / 100) * probability-of-infection)
      [set color red set infection-period 0 set infection-number infection-number + 1 set tflu-strain pflu-strain]
        ] 
  if immune-label = pflu-strain [ 
    if random 100 < (((100 - natural-immunity) / 100) * probability-of-infection)
      [set color red set infection-period 0 set infection-number infection-number + 1 set tflu-strain pflu-strain]
    ] 
]
end

to mutate ;; some probability of mutation (change in strain label) when an individual receives infection from a patch
  ifelse in-host [
    ask turtles with [color = red] [
     if random 100 < probability-of-mutation [
      ifelse (one-of [1 2] = 1) [
      set tflu-strain (tflu-strain + (random (drift-size + 1)))]
      [set tflu-strain (tflu-strain - (random (drift-size + 1)))]
     ]
    ]
   ]
  [ask turtles with [color = red and infection-period = 0] [
     if random 100 < probability-of-mutation [
      ifelse (one-of [1 2] = 1) [
      set tflu-strain (tflu-strain + (random (drift-size + 1)))]
      [set tflu-strain (tflu-strain - (random (drift-size + 1)))]
      ]
     ]
    ]
end

to update-immunity
  ask turtles with [color = red and infection-period = 0] [
    ifelse immune-labelling = "first-infection" [
  if immune-label = -999999999999 [
        set immune-label tflu-strain]]
  [set immune-label tflu-strain]
]
end

 to track-infecteds ;; turtles with given infection period should become uninfected
  ask turtles with [color = red] [
    set infection-period infection-period + 1
  if infection-period = age-infection-max [
    set color white set infection-period 0 set tflu-strain -999999999999
    ]
  ]
  ask patches with [pcolor = red] [
  set fomite-age fomite-age + 1
  if fomite-age > max-fomite-persistence [
  set pcolor black
    ]
  ]
end

任何建议将不胜感激!谢谢

【问题讨论】:

    标签: list netlogo modeling agent


    【解决方案1】:

    问题出在线路上

    let vax-turtles (list turtles with [color = blue])
    

    list 原语创建一个列表,但不是turtles with [color = blue] 的列表。相反,它创建了一个参数列表,在这种情况下是一个单一的代理集。相反,您需要代理集 in 的代理列表。 of 记者是最简单的方法,因为of 总是返回一个列表。

    let vax-turtles [self] of turtles with [color = blue]
    

    或者,如果您希望对列表进行排序,您可以使用sort,它也总是返回一个列表。

    let vax-turtles sort turtles with [color = blue]
    

    至于您在下面的评论中描述的问题,您需要让每个 vax-turtle 设置自己的 vax-strain(我假设这是一个海龟自己的变量)。假设您使用的是 NetLogo 6.x,

    if vaccine = "HEV" [
        let vax-turtles [self] of turtles with [color = blue]
        ask n-of prop-vax turtles with [color = white] [set color blue] 
        foreach [vax-turtles] [t ->
          ifelse (one-of [1 2] = 1) [
          ask t [set vax-strain (random ((2 * drift-size) + 1))]]
        [ask t [set vax-strain (-1 * random ((2 * drift-size) + 1))]]
          ]
        ]
    

    但是,如果所有 vax-turtles 的漂移大小都相同,那么考虑到 random,JenB 的以下解决方案不太可能为任何两只海龟提供相同的 vax-strain 值,不是吗?

    【讨论】:

    • 得到一个错误:这个代码不能被观察者运行,只有一个乌龟错误,而观察者运行 SET 调用由(匿名命令来自:程序 SETUP-TURTLES:[ifelse one of [1 2] = 1 [ 设置 vax 应变随机 2 * 漂移大小 + 1 ] [ 设置 vax 应变 -1 * 随机 2 * 漂移大小 + 1 ] 设置 vax 应变随机 2 * 漂移大小 + 1 设置 vax-菌株 -1 * 随机 2 * 漂移大小 + 1 组免疫标签 vax-strain])我的代码:如果疫苗 =“HEV”[用 [颜色 = 白色] [设置颜色蓝色] 询问 n-of prop-vax 海龟让乌龟的 vax-turtles [self] 带有 [color = blue]
    【解决方案2】:

    我认为

    foreach sort-on [who] turtles with [color = blue]
    

    足够了。

    【讨论】:

      【解决方案3】:

      查看您的代码,我不明白为什么您需要进行迭代而不是询问,而且坚持询问要干净得多。所以,尝试替换:

      if vaccine = "HEV" [
          let vax-turtles (list turtles with [color = blue])
          ask n-of prop-vax turtles with [color = white] [set color blue] 
          foreach [vax-turtles] [
            ifelse (one-of [1 2] = 1) [
            set vax-strain (random ((2 * drift-size) + 1))]
          [set vax-strain (-1 * random ((2 * drift-size) + 1))]
            ]
          ]
      

      if vaccine = "HEV" [
          let vax-turtles turtles with [color = blue]
          ask n-of prop-vax turtles with [color = white] [set color blue] 
          ask [vax-turtles] [
            ifelse (one-of [1 2] = 1)
            [ set vax-strain (random ((2 * drift-size) + 1)) ]
            [ set vax-strain (-1 * random ((2 * drift-size) + 1)) ]
          ]
        ]
      

      就此而言,我认为不同的值只是相同值的不同符号,所以你可以这样做:

      if vaccine = "HEV" [
          let vax-turtles turtles with [color = blue]
          ask n-of prop-vax turtles with [color = white] [set color blue] 
          ask [vax-turtles] [
            set vax-strain one-of [-1 1] * (random ((2 * drift-size) + 1))
          ]
        ]
      

      另外,我不确定您是否有意这样做,但您在创建 vax-turtles 代理集后将比例更改为蓝色。这意味着直到下一个tick 或下次运行代码时,新更改的海龟才会获得vax-strain。此外,您拥有的代码将为所有蓝色海龟分配一个新的vax-strain,即使它们在前面的步骤中被设为蓝色。如果您真的希望 vax-strain 只分配一次并且海龟更改颜色以显示此内容,请尝试:

      if vaccine = "HEV" [
          ask n-of prop-vax turtles with [color = white]
          [ set color blue
            set vax-strain one-of [-1 1] * (random ((2 * drift-size) + 1))
            type "drift-size is " type drift-size type " and vax-strain is " print vax-strain ;; new line here for diagnostics
          ]
        ]
      

      更新:ask 给每个代理一个新的随机数。您可以通过运行完整的模型来看到这一点:

      turtles-own [testnum]
      
      to setup
        clear-all
        create-turtles 10
        [ set testnum random 5 ]
        print [testnum] of turtles
      end
      

      【讨论】:

      • 您好 JenB,我必须针对 HEV 进行迭代,因为每个人都需要接种不同的疫苗。我尝试了您建议的一些代码,但不幸的是,所有海龟都接种了相同的疫苗株——这适用于 HOV 条件,但不适用于 HEV。我需要确保只有蓝海龟被分配了疫苗株,并且在 HEV 条件下,每个个体的株系都不同。使用 ask 似乎并没有这样做,因为它将一个疫苗株分配给整个代理集。希望这是有道理的!
      • 其实ask会给每个代理一个新的随机数。我已经给你写了一段完整的代码,所以你可以看到这个。如果你所有的海龟都被分配了相同的随机数,那么你的random 调用就有问题。漂移大小有哪些类型的值?
      • 感谢询问码!漂移大小决定了当循环病毒发生变异时变异事件有多大。最小值为 0,最大值为 6,例如如果漂移大小为 5 并且病毒以毒株 0 开始,它可能会变异为 5 或 -5。我正在尝试实施的疫苗将使用相同的整数系统使个体对特定菌株免疫。
      • 好的。我建议你添加一些打印语句或使用检查窗口来查看你的一些值并向后工作,直到你弄清楚为什么你的 vax-strain 都是一样的。我会添加一行来让你开始。
      猜你喜欢
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      相关资源
      最近更新 更多