Netlogo flocking model modification for multiple turtles in 1 patch

落花浮王杯 提交于 2019-12-11 11:05:38

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/37261948/netlogo-flocking-model-modification-for-multiple-turtles-in-1-patch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!