Jesse001
Jesse001

Reputation: 924

Netlogo flocking model modification for multiple turtles in 1 patch

I'm attempting to modify the Flocking model code example to represent fishes forming schools (flocks) when they encounter each other, then move together using the logic of the rest of the code. Unfortunately, adhering to that logic requires them to occupy the same patch at the same time. The problem arises during the average-heading-towards-schoolmates report:

to-report average-heading-towards-schoolmates  ;; turtle procedure

  let x-component mean [sin (towards myself + 180)] of schoolmates
  let y-component mean [cos (towards myself + 180)] of schoolmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end

When the nearest schoolmate is on the same patch as the turtle running "towards-myself" it gets an error because there is no heading toward your exact location. I've tried adding

set xcor xcor - 0.001

and

forward 0.001

To the front of the code so there would be some disparity in location but it didn't help. What I'd like it to do is if it can't decide on a heading then to invoke the "search" protocol.

Any creative ideas for solving this conundrum will be greatly appreciated!

Upvotes: 1

Views: 221

Answers (1)

mattsap
mattsap

Reputation: 3806

You need to do error-checking for when the patches are the same. For your model, you'll need to consider what to do in the situation when the agents are in the same patch. In the below code, I ignore them.

From the documentation on towards:

Note: asking for the heading from an agent to itself, or an agent on the same location, will cause a runtime error.

to-report average-heading-towards-schoolmates  ;; turtle procedure

  let my-schoolmates schoolmates with [patch-here != [patch-here] of myself]
  ifelse any? my-schoolmates
  [
    let x-component mean [sin (towards myself + 180)] of my-schoolmates
    let y-component mean [cos (towards myself + 180)] of my-schoolmates
    report atan x-component y-component 
  ]
  [report heading]
end

You may want to try incorporating turtles on the same patch into your heading calculation:

to-report average-heading-towards-schoolmates  ;; turtle procedure

  let x-component mean [ifelse-value patch-here != [patch-here] of myself [0] [sin (towards myself + 180)]] of schoolmates
  let y-component mean [ifelse-value patch-here != [patch-here] of myself [0][cos (towards myself + 180)]] of schoolmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end

Upvotes: 2

Related Questions