首页 > 解决方案 > 如何将乌龟的局部变量与其邻居进行比较

问题描述

我正在尝试将乌龟的局部变量与其邻居进行比较,并尝试找出符合此标准的邻居 总数总计附近= 邻居总数。我正在根据海龟的颜色进行检查,如果颜色不同,那么我将检查属性/变量 Error: A patch can't access a turtle or link variable without指定哪个代理

代码:

 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]   
    ]
]

标签: netlogoneighbours

解决方案


neighborspatch变量,不是turtle变量。因此,模型中的海龟使用原语,当他们想要查询海龟neighbors的代理集时,它们正在查询补丁的代理集。海龟有几种方法可以评估附近的海龟,例如or ,但在这种情况下,如果您想要专门位于直接相邻的补丁上的海龟,您可以使用、和原语来获得您正在寻找的东西. 举个简单的例子,看看这个玩具模型:in-radiusin-coneotherturtles-onneighbors

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

查看其他turtles-on邻居的字典定义以获取更多信息。


推荐阅读