使用 with-min 时遇到问题
Trouble using with-min
一个简单的问题,但我完全失败了。
我有一群海龟需要找到最近的邻居,因为我希望在它们之间创建一个 link。我已经尝试了以下代码,但我一直返回一个空集 [nobody found]:
ask turtles [create-links-with one-of other turtles with-min [distance myself]]
有人能给我指出正确的方向吗?
此致
西蒙
这里有两个问题。
一个是 create-links-with
是错误的,因为 one-of
returns 是单个代理,而不是代理集。你需要 create-link-with
.
但主要问题出在这部分:
other turtles with-min [...]
NetLogo 将此理解为 other (turtles with-min [...])
。这报告了一个空代理集,因为海龟本身赢得了 with-min
竞争,因为它的距离为零,然后 other
淘汰了那只海龟,留下了空代理集。
相反,您必须写成:
(other turtles) with-min [...]
所以将两个修复一起,我们得到:
ask turtles [
create-link-with one-of (other turtles) with-min [distance myself]
]
如果需要,可以使用 min-one-of
而不是 with-min
进一步缩短,如下所示:
ask turtles [
create-link-with min-one-of other turtles [distance myself]
]
我制作了一些海龟,并在 NetLogo 的 Command Center 中试用,我得到:
一个简单的问题,但我完全失败了。
我有一群海龟需要找到最近的邻居,因为我希望在它们之间创建一个 link。我已经尝试了以下代码,但我一直返回一个空集 [nobody found]:
ask turtles [create-links-with one-of other turtles with-min [distance myself]]
有人能给我指出正确的方向吗?
此致
西蒙
这里有两个问题。
一个是 create-links-with
是错误的,因为 one-of
returns 是单个代理,而不是代理集。你需要 create-link-with
.
但主要问题出在这部分:
other turtles with-min [...]
NetLogo 将此理解为 other (turtles with-min [...])
。这报告了一个空代理集,因为海龟本身赢得了 with-min
竞争,因为它的距离为零,然后 other
淘汰了那只海龟,留下了空代理集。
相反,您必须写成:
(other turtles) with-min [...]
所以将两个修复一起,我们得到:
ask turtles [
create-link-with one-of (other turtles) with-min [distance myself]
]
如果需要,可以使用 min-one-of
而不是 with-min
进一步缩短,如下所示:
ask turtles [
create-link-with min-one-of other turtles [distance myself]
]
我制作了一些海龟,并在 NetLogo 的 Command Center 中试用,我得到: