How to compare the local variables of a turtle with its neighbors

190 Views Asked by At

I am trying to compare the local variables of a turtle, with its neighbors and trying to find out the total number of neighbors which match this criterion total-nearby = total number of neighbors. I am checking according to the color of the turtles, if the color is different, then I will check for the attributes/variables Error: A patch can't access a turtle or link variable without specifying which agent

CODE:

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

There are 1 best solutions below

0
Luke C On

neighbors is a patch variable, not a turtle variable. So, the turtles in your model use the primitive neighbors, they are querying an agentset of patches when they want to query an agentset of turtles. There are several ways for turtles to evaluate nearby turtles, such in-radius or in-cone, but in this case if you're wanting the turtles that are specifically on the patches directly adjacent, you could use the other, turtles-on, and neighbors primitives to get what you're looking for. For a simple example, have a look at this toy model:

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

Check out the dictionary definitions for other, turtles-on, and neighbors for more information.