问题描述
我想使用半径或距离来定义邻居.到目前为止,我的解决方案是:
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
问题是大多数乌龟都有0到4个邻居,但其中一些却有相对较多的邻居(例如34和65).这些乌龟位于世界中心附近.
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?
推荐答案
这与程序中出现副作用的时间有关.
It has to do with the timing of side effects in your program.
假设第一个要移动的乌龟在中心附近移动.其他乌龟尚未移动,因此它们都仍在patch 0 0
上,并且set my-neighbors (other turtles) in-radius 3
将捕获它们.即使它们移到其他地方,它们仍将包含在第一个乌龟的my-neighbors
代理集中.
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
这篇关于使用半径或距离定义邻居海龟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!