这是我想做的事情:
我需要所有代理使用 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'
我应该怎么办?我需要帮助... .·´¯ (>▂<)´¯
·。感谢您的时间。