define neighbor turtles using in-radius or distance

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

I would like to define neighbors using in-radius or distance. My solution so far is:

turtles-own [   my-neighbors   num-neighbors]  to setup  ca crt 100 [   move-to one-of patches with [ not any? turtles-here ]   set my-neighbors (other turtles) in-radius 3   set num-neighbors count my-neighbors ]  end

The problem with this is that most of the turtles have between 0 to 4 neighbors, but a few of them have a relatively huge number of neighbors (e.g., 34 and 65). Those turtles are located close to the center of the world.

Any ideas about what I am doing wrong?

回答1:

It has to do with the timing of side effects in your program.

Suppose that the very first turtle to move moves near the center. None of the other turtles have moved yet, so they're all still on patch 0 0 and set my-neighbors (other turtles) in-radius 3 will capture them all. And even after they move elsewhere, they will still be included in the first turtle's my-neighbors agentset.

You can avoid the problem by first moving all the turtles and then calculate their neighbors:

to setup   clear-all   crt 100 [     move-to one-of patches with [ not any? turtles-here ]   ]   ask turtles [     set my-neighbors (other turtles) in-radius 3     set num-neighbors count my-neighbors   ] end


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