【发布时间】:2022-01-08 19:58:23
【问题描述】:
在一个编队中,机器人是相互联系的,一个邻域内的机器人数量可能会有所不同。如果一个机器人有 5 个邻居,我怎样才能找到那个机器人与其另一个邻居的角度?
【问题讨论】:
标签: netlogo simulator angle robotics agent-based-modeling
在一个编队中,机器人是相互联系的,一个邻域内的机器人数量可能会有所不同。如果一个机器人有 5 个邻居,我怎样才能找到那个机器人与其另一个邻居的角度?
【问题讨论】:
标签: netlogo simulator angle robotics agent-based-modeling
(在评论之后,我替换了 face + read heading> 只是使用 towards的序列,这是我忽略的一个选项。由于某种原因,我所指的评论已被迅速删除,所以我不知道是谁提出了建议,但我从单元格通知中读到了足够多的内容)
在 NetLogo 中,通常可以使用海龟的heading 来了解度数。
由于您的代理已链接,首先想到的可能是使用link-heading,直接使用reports the heading in degrees from end1 to end2。
但是请注意,这可能并不理想:只有当您有兴趣了解从 end1 到 end2 的标题时,使用 link-heading 才能一尘不染,这意味着:
如果您对此感兴趣,那很好。但可能并非如此!例如,如果您有无向链接并且有兴趣知道从turtle 1 到turtle 0 的角度,使用link-heading 会给出错误的值:
to setup
clear-all
create-turtles 2 [
setxy random-xcor random-ycor
set color black
set label who
]
ask turtle 0 [
create-link-with turtle 1
]
end
to go
ask link 0 1 [
show link-heading
]
end
...虽然我们知道,通过查看两只海龟的位置,从turtle 1 到turtle 0 的度数必须在 45 附近。
一种更适合所有可能情况的方法是直接查看您感兴趣的海龟的heading,无论链接的性质或方向如何。你可以让你的参考龟face成为目标龟,然后读取参考龟的heading。或者更好:您可以直接使用towards,它只报告相同的信息,但不必让海龟真正改变它们的方向。复制并运行下面的代码,看看这种方法如何总是给出正确的答案!
to setup
clear-all
create-turtles 5 [
setxy random-xcor random-ycor
set color black
set label who
]
end
to go
let reference sort turtles
foreach reference [
r ->
ask r [
print "---------------------------------------------------------------------------------------------------------------------------------------------"
let targets sort other turtles
foreach targets [
t ->
let direction (towards t)
type "I am " type self type ". The NetLogo angle between me and " type t type " is " type direction type ", while the normal mathematical angle is " print heading-to-angle direction
]
print "---------------------------------------------------------------------------------------------------------------------------------------------"
]
]
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
在您的情况下,目标组(我在上面的简短示例中设置为other turtles)可以基于实际链接,因此可以构造为(list link-neighbors) 或sort link-neighbors(因为如果您想要要使用foreach,代理集必须作为列表传递 - see here)。
更新:实际上,我最终还制作了一个更接近您的案例的玩具模型,即带有链接并使用link-neighbors。见下文:
to setup
clear-all
create-turtles 100 [
move-to one-of patches with [not any? turtles-here]
set color black
set label who
]
ask n-of 2 turtles [
create-links-with n-of 5 other turtles
]
end
to go
let reference-turtles sort turtles with [count my-links > 2]
foreach reference-turtles [
r ->
ask r [
print "-----------------------------------------------------------------------------------------------------------------------------------------------"
let targets sort link-neighbors
foreach targets [
t ->
let direction (towards t)
type "I am " type self type ". The NetLogo angle between me and " type t type " is " type direction type ", while the normal mathematical angle is " print heading-to-angle direction
]
print "-----------------------------------------------------------------------------------------------------------------------------------------------"
]
]
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
最后一点:您肯定注意到了heading-to-angle 过程,它直接取自atan 条目here。这是一种有用的方法,可以将 NetLogo 几何中表示的度数(北为 0,东为 90)转换为以通常的数学方式表示的度数(北为 90,东为 0)。我不知道你对什么学位感兴趣,所以值得在这里留下这个提示。
【讨论】: