我尝试使用以下方案在植绒行为中添加领导者规则: 在一个房间里有两种类型的海龟(领导者和追随者),在正常情况下,所有海龟都会在房间内游荡,但是当发生紧急情况时,海龟将遵循植绒行为(分离,对齐,凝聚)并考虑领导者规则。如果他们找到附近的领导者,他们会涌向领导者的头衔,同时每只乌龟必须保持一定的距离。谢谢你的帮助。
turtles-own
[
flockmates
nearest-neighbor
]
breed [type1s type1]
breed [type2s type2]
globals
[time-to-evacuate]
to setup
__clear-all-and-reset-ticks
create-type1s (population)
[set color white
set size 0.6
move-to one-of patches with [pcolor = blue]]
create-type2s ((percent_of_leader / 100) * population )
[ set color black
set size 0.6
move-to one-of patches with [pcolor = blue]]
end
to go
ask turtles [wander fd 0.1]
if emergency? = true [move]
end
to wander
[ do something]
end
to move
set time-to-evacuate time-to-evacuate + 1
ask type1s [ flock]
ask type1s [ follow-leader ]
ask type1s [avoid-obstacles]
ask type1s [evacuate1]
ask type1s [ fd speed ]
ask type2s [ fd leader-speed ]
ask type2s [avoid-obstacles]
ask type2s [evacuate2]
ask turtles with [pcolor = red][die]
if all? turtles [ pcolor = red ]
[ stop ]
tick
end
to evacuate1
let beings-seen patches in-cone vision vision-angle with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]
end
to evacuate2
let beings-seen patches in-cone leader-vision leader-vision-angle with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]
end
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ if any? flockmates
[align
cohere ] ] ]
end
to find-flockmates ;; turtle procedure
set flockmates other breed in-cone vision vision-angle
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
to follow-leader
let leaders type2s in-cone vision vision-angle
if any? leaders
[ let any-leader min-one-of leaders [distance myself]
face min-one-of leaders [distance myself ]
let observed-heading ([towards myself + 180] of any-leader)
turn-away observed-heading max-avoid-turn ]
end
to separate ;; turtle procedure
let observed-heading ([heading] of nearest-neighbor)
turn-away observed-heading max-separate-turn
end
to align ;; turtle procedure
turn-towards average-flockmate-heading max-align-turn
end
to-report average-flockmate-heading ;; turtle procedure
let x-component sum [sin heading] of flockmates
let y-component sum [cos heading] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
to cohere ;; turtle procedure
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates ;; turtle procedure
let x-component mean [sin (towards myself + 180)] of flockmates
let y-component mean [cos (towards myself + 180)] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
to turn-towards [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings new-heading heading) max-turn
end
to turn-away [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings heading new-heading) max-turn
end
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
to avoid-obstacles
if [pcolor] of patch-ahead 1 = green
[lt random-float 360]
end