首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NetLogo中将代理集转换为列表

在NetLogo中将代理集转换为列表
EN

Stack Overflow用户
提问于 2018-02-24 10:10:46
回答 3查看 2.3K关注 0票数 4

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

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

代码语言:javascript
复制
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

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

EN

回答 3

Stack Overflow用户

发布于 2018-02-24 21:31:09

问题出在线路上

代码语言:javascript
复制
let vax-turtles (list turtles with [color = blue])

list原语创建一个列表,但不是turtles with [color = blue]的列表。相反,它创建了一个参数列表,在本例中是一个单一的角度集。相反,您希望获得中的代理列表。因为of总是返回一个列表,所以of报告器是最简单的方法。

代码语言:javascript
复制
let vax-turtles [self] of turtles with [color = blue]

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

代码语言:javascript
复制
let vax-turtles sort turtles with [color = blue]

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

代码语言:javascript
复制
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海龟都是相同的,那么在给定random的情况下,JenB下面的解决方案不太可能为任何两个海龟提供相同的vax-strain值,不是吗?

票数 6
EN

Stack Overflow用户

发布于 2018-02-25 05:00:42

看看你的代码,我不明白为什么你需要做迭代而不是ask,而且坚持使用ask要干净得多。因此,尝试替换:

代码语言:javascript
复制
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))]
      ]
    ]

使用

代码语言:javascript
复制
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)) ]
    ]
  ]

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

代码语言:javascript
复制
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只被赋值一次,并且海龟改变颜色来显示这一点,尝试一下:

代码语言:javascript
复制
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给每个代理一个新的随机数。您可以通过运行完整的模型来查看:

代码语言:javascript
复制
turtles-own [testnum]

to setup
  clear-all
  create-turtles 10
  [ set testnum random 5 ]
  print [testnum] of turtles
end
票数 2
EN

Stack Overflow用户

发布于 2018-03-13 12:41:40

我认为

代码语言:javascript
复制
foreach sort-on [who] turtles with [color = blue]

就足够了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48958647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档