【问题标题】:NetLogo - Finding Turtles Within 2 SquaresNetLogo - 在 2 个正方形内找到海龟
【发布时间】:2020-05-12 16:33:43
【问题描述】:

我正在尝试找出如何检查附近是否有任何海龟(我们将其称为基础海龟或基地)。我需要这个包括对角线,因为我在这些基地周围产卵,所以需要在基地周围空出 1 个完整的正方形。因此。生成基地时,我需要确保 2 个方格内没有其他人,包括对角线。

我尝试使用“turtles in-radius (2,3,4,...)”但它不起作用,因为半径命令似乎是圆形的。我该怎么办?

【问题讨论】:

  • 一个相关的代码示例是摩尔和冯诺依曼示例,在 NetLogo 模型库的代码示例部分。 (方形社区称为“摩尔社区”。)

标签: netlogo


【解决方案1】:

NetLogo 的neighbors 为您提供围绕海龟所在补丁的8 个补丁。你需要周围的 16 个补丁,还是需要 8 个?

在非环绕世界中,neighbors16 的一个简单过程可能是

to-report neighbors16
  ; can be called by a patch or a turtle
  ; assumes there is no wrapping of the world
  let xm max (list (pxcor - 2) min-pxcor)
  let xh min (list (pxcor + 2) max-pxcor)
  let ym max (list (pycor - 2) min-pycor)
  let yh min (list (pycor + 2) max-pycor)

  let ptchs no-patches
  foreach (range xm (xh + 1) 1) [x ->
    foreach (range ym (yh + 1) 1) [y ->
      set ptchs (patch-set patch x y ptchs)
    ]
  ]
  report ptchs with [self != patch [pxcor] of myself [pycor] of myself]
end

希望这会有所帮助, 查尔斯

【讨论】:

  • 您可以直接说let xm pxcor - 2,而不是let base patch-herelet xm [pxcor] of base - 2。海龟可以直接访问它所在的补丁的变量。
  • 这段代码看起来可以工作,但在我看来它也很慢,因为patches with ... 必须检查世界上的每个补丁,看看它是否合格。
  • 回应赛斯总是有说服力的观察,我已经编辑了我的答案(我认为)更有效率。它可以被乌龟或补丁调用。
  • 不错!您可能还喜欢查看 Moore & Von Neumann Example 中的代码,它做了类似的事情,但使用了at-points
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多