查询/创建每只乌龟的海龟子集

时间:2015-12-22 09:45:29

标签: simulation netlogo agent-based-modeling

我有一个agentset,所有的海龟都有不同的年龄/经验值。我想做的是找到每只乌龟更有经验的海龟,然后跟着那些乌龟。不幸的是,我收到ifelse [ age-experience > my-own-age-experience ]行的以下错误:

  

这里预期为TRUE / FALSE,而不是列表或块。

这是我的代码:

turtles-own [
  age-experience
  more-dominant
  dominant-flockmates  
]

to setup
  clear-all
  create-turtles 10 [ set age-experience random-float 1 ]
  reset-ticks
end

to go
  ask turtles [
    find-dominant-flockmates
    ifelse any? dominant-flockmates
      [ show "follow the more dominant flockmates" ]
      [ show "take the lead" ]
  ]
  tick
end

to find-dominant-flockmates
  let my-own-age-experience age-experience
  ask other turtles [
    ifelse [ age-experience > my-own-age-experience ]
      [ set more-dominant true ]
      [ set more-dominant false ]
  ]
  set dominant-flockmates other turtles with [ more-dominant ]
end

1 个答案:

答案 0 :(得分:2)

好的,你有几个选择。您可以通过删除setDT(df)[order(factor(var3, levels=LETTERS[1:3])), head(.SD, 1L), .(var1, var2)]

来消除错误
[]

但是有更有效的方法来编码。如果你打算将变量to find-dominant-flockmates let my-own-age-experience age-experience ask other turtles [ ifelse age-experience > my-own-age-experience [ set more-dominant true ] [ set more-dominant false ] ] set dominant-flockmates other turtles with [ more-dominant ] end 用于其他目的(所以你需要它),你可以像这样使用more-dominant(因为它是真/假,你实际上并不需要ifelse-value但是知道这是一件好事:

ifelse-value

但是,如果变量的唯一原因是创建代理集,则可以直接执行整个操作:

to find-dominant-flockmates
  let my-own-age-experience age-experience
  ask other turtles
  [ set more-dominant ifelse-value (age-experience > my-own-age-experience)
      [ true ]
      [ false ]
  ]
  set dominant-flockmates other turtles with [ more-dominant ]
end