这是我想要做的:
-
我需要所有代理使用 BOIDS 规则在组中向前移动
-
我希望他们在遇到墙壁后能够转身(我的世界不是包裹的,他们无法穿透墙壁到达世界的另一端(
这是我的代码:
globals [nearest-teammates teammmembers]
turtles-own [ myteamset teamID myID teammatesID ]
to setup-groups
let teamCounter 1
ask turtles [
if myteamset = nobody [
let possible-teammates other turtles with [ myteamset = nobody ]
ifelse count possible-teammates > 1 [
set myteamset ( turtle-set self n-of 2 possible-teammates )
let ids sort [myID] of myteamset
set teammmembers myteamset
set nearest-teammates min-one-of myteamset[distance myself]
let tempteam teamCounter
set teamCounter teamCounter + 1
ask myteamset [
set teammatesID ids
set myteamset teammmembers
set teamID tempteam
]
] [
show "Not enough turtles to make a new team!"
]
]
]
end
植绒部分:
to move-in-groups
let teamNumbers remove-duplicates [teamID] of turtles
foreach teamNumbers [
ask one-of turtles with [ teamID = ? ] [
if myteamset != nobody [
ask myteamset [
set heading mean-heading [ heading ] of myteamset
ifelse distance one-of turtles with [ teamID = ? ] < 3
[separate]
[align
cohere]]
fd 1 ]]]
end
帮助程序代码
to-report mean-heading [ headings ]
let mean-x mean map sin headings
let mean-y mean map cos headings
report atan mean-x mean-y
end
to align
turn-towards average-flockmate-heading max-align-turn
end
to cohere
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to separate
turn-away ([heading] of nearest-teammates) max-separate-turn
end
to turn-away [new-heading max-turn]
turn-at-most (subtract-headings heading new-heading) max-turn
end
to-report average-heading-towards-flockmates
let x-component mean [sin (towards myself + 180)] of nearest-teammates
let y-component mean [cos (towards myself + 180)] of nearest-teammates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
to-report average-flockmate-heading
let x-component sum [dx] of nearest-teammates
let y-component sum [dy] of nearest-teammates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
to turn-at-most [turn max-turn]
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
to turn-towards [new-heading max-turn]
turn-at-most (subtract-headings new-heading heading) max-turn
end
我得到的错误:
SUM expected input to be a list but got the number -0.961261695938319 instead.
error while turtle 4 running SUM
called by procedure AVERAGE-FLOCKMATE-HEADING
called by procedure ALIGN
called by (command task from: procedure MOVE-IN-GROUPS)
called by procedure MOVE-IN-GROUPS
called by procedure GO
called by Button 'go'
我该怎么办?我需要帮助... ·' ̄(>▂<)´¯
·. 谢谢你的时间。
您收到的错误发生在以下行:
let x-component sum [dx] of nearest-teammates
NetLogo告诉您,您尝试将单个数字传递给sum
而不是向其传递列表。怎么会这样?如果nearest-teammates
包含单个代理而不是代理集,则会发生这种情况。
因此,让我们看看您在哪里定义nearest-teammates
:
set nearest-teammates min-one-of myteamset[distance myself]
你能看出问题吗?您正在使用min-one-of
,这确实为您提供了一个代理!
例如,您如何获得 10 个最接近的代理,而不仅仅是最接近的代理?幸运的是,NetLogo有一个原语可以做到这一点:min-n-of
。以下是您将如何使用它:
set nearest-teammates min-n-of 10 myteamset [ distance myself ]
当然,将10
替换为您想要的队友数量。