【问题标题】:How to compare the local variables of a turtle with its neighbors如何将乌龟的局部变量与其邻居进行比较
【发布时间】:2021-08-28 23:27:47
【问题描述】:

我正在尝试将乌龟的局部变量与其邻居进行比较,并尝试找出符合此标准的邻居总数 total-nearby = 邻居总数。 我正在根据海龟的颜色进行检查,如果颜色不同,那么我将检查属性/变量 错误:如果不指定,补丁无法访问海龟或链接变量 哪个代理

代码:

 turtle-own[
 total-similar-nearby     ; sum of previous two variables
total-other-nearby
total-nearby
  native
  language
  income
  maritalstatus
]
;;then assigning multiple number of turtles with different values to the local variables.

ask turtles[
repeat  total-nearby

    [
      if color = [color] of one-of neighbors
      [set d1 d1 + 1]
     if color != [color] of one-of neighbors 
    [
       if native = [ native ] of one-of neighbors
      [set a 1]
      if language = [ language ] of one-of neighbors
      [set b 1]
        if income = [ income ] of one-of neighbors
      [set c 1]
      if maritalstatus = [ maritalstatus ] of one-of neighbors
      [set d 1]
      
    ] set p  a  +  b + c  + d 
      if p >= 50 [set d1 d1 + 1]   
    ]
]

【问题讨论】:

  • 嗨!请确保您的问题满足您可以找到的herehere,这将更容易为您提供帮助。例如,在这种情况下,您提供的代码不足以制作可重现的问题示例。理想情况下,您将提供一段代码,每个人都可以复制并粘贴到他们的 NetLogo 中并看到与您看到的相同的错误,而无需编写任何代码。在当前示例中可能会出现问题,但请编辑您的问题以更清楚地说明
  • Supratim,我看到你修改了你的帖子——但这并不是一个实质性的改变。您可以自己尝试一下:如果您创建一个全新的 NetLogo 文件,然后复制并粘贴您在此处编写的代码,您会得到什么?你得到一些无法编译的东西。您能与我们分享您的代码的工作示例吗?即,如果我们将它放在一个全新的 NetLogo 文件中,将显示您所询问的错误。
  • 我想知道有没有办法,我可以将乌龟的局部变量与它的邻居进行比较?对于每只海龟,它会看它们是否有不同的颜色,然后它们会比较它们的属性

标签: netlogo neighbours


【解决方案1】:

neighborspatch 变量,而不是 turtle 变量。因此,模型中的海龟使用原语 neighbors,当它们想要查询 turtles 的代理集时,它们正在查询 patches 的代理集。海龟有几种方法可以评估附近的海龟,例如in-radiusin-cone,但在这种情况下,如果您想要专门在直接相邻的补丁上的海龟,您可以使用other、@987654331 @ 和 neighbors 原语来获取您要查找的内容。举个简单的例子,看看这个玩具模型:

to setup
  ca
  ask n-of 300 patches [
    sprout 1 [
      set color one-of [ red blue ]
    ]
  ]
  reset-ticks
end

to go 
  ask turtles [
    ; Make an agent-set of turtles on neighboring patches
    let nearby-turtles other turtles-on neighbors
    
    ; If there are any turtles on neighboring patches, 
    ; assume the color from one of them.
    if any? nearby-turtles  [
      set color [color] of one-of nearby-turtles
    ] 
  ]
  tick
end

查看otherturtles-onneighbors 的字典定义以了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    相关资源
    最近更新 更多