在编队中,机器人彼此相连,附近的机器人数量可能有所不同。如果一个机器人有5个邻居,我怎么能找到一个机器人和它的另一个邻居的角度呢?
发布于 2021-12-02 11:38:00
(在注释之后,我将<face + read heading>的顺序替换为仅使用towards,而我忽略了这一选项。由于某些原因,我所指的评论很快就被删除了,所以我不知道是谁给出了这个建议,但我从单元格通知中读到了足够多的建议)
在NetLogo中,经常可以使用海龟的heading来了解学位。
由于您的代理是链接的,首先考虑的可能是使用link-heading,直接使用至。
但是,请注意,这可能并不理想:只有当您对从end1到end2的标题感兴趣时,使用end1才能一帆风顺地工作,这意味着:
如果这是你感兴趣的事,好吧。但可能不是这样的!例如,如果您有无向链接,并且有兴趣了解从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,则必须将代理集作为列表-请看这里传递)。
Update:实际上,我最后还制作了一个玩具模型,它更接近于代表您的情况,即使用链接和使用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条目这里。将NetLogo几何中表示的度(其中北为0,东为90)转换为用通常的数学方法表示的度(其中北为90,东为0)是一种有用的方法。我不知道你对什么学位感兴趣,所以在这里留下这个提示是值得的。
https://stackoverflow.com/questions/70197548
复制相似问题