Search code examples
netlogo

how does 'netlogo' 's 'any? turtles-on patch-ahead 1' command work?


I'm trying to get my turtles to turn away when there is a turtle in the patch ahead of them. I have some code

to setup
  
  clear-all
  reset-ticks
  ;random-seed 13

 let xcord one-of (list min-pxcor max-pxcor)
  crt 3 [
    setxy xcord min-pycor + (random (max-pycor - min-pycor))
    set size 4
    set color yellow
    pen-down
  ]

  ask turtles [
    (ifelse
      xcor = min-pxcor [facexy max-pxcor ycor]
      [facexy min-pxcor ycor]
     )
    ]

end


to go
  
  tick
  ask turtles [search]
  
end

to search

    ifelse any? turtles-on patch-ahead 1 [
      set pcolor blue
      right 90
    ][right (random 21) - 10]

  ;step forward
  fd 1

end

but the problem is that turtles seem to detect a turtle ahead even when there is not? I made it so patches turn blue when there is a turtle ahead. You can see the upper blue square definitely did not have any turtles in the surrounding patches. I must be missing something but idk what. I'm using netlogo 6.3.0

enter image description here


Solution

  • One possible reason could be, that the turtle is counting itself. The patch-ahead looks for turtles on the patch in the direction of the heading with distance 1. The width (or height) of a patch is 1, but if a turtle is "looking" diagonal across a patch this distance can be greater than 1. turtles-on patch-ahead 1 would therefore report an agentset that includes the calling turtle itself. If you want the turtle not to be included in the agentset, you can use the other reporter.

    to search
    
        ifelse any? other turtles-on patch-ahead 1 [
          set pcolor blue
          right 90
        ][right (random 21) - 10]
    
      ;step forward
      fd 1
    
    end