Netlogo 如何根据海龟的颜色和最小距离将它们分组
我正在用NetLogo写一个模型,我从5个海龟种群开始,它们的颜色(蓝、绿、黄、红、白)随机开始坐标。当我运行模型时,我希望当白海龟遇到绿海龟、黄海龟或红海龟时,它们会加入模型,并作为一个群体一起移动。当白海龟遇到蓝海龟时,它会从中分离出来,然后继续随机移动。我在模型库中看到了鸟群的示例模型,我一直在尝试为我的模型修改它(请参阅所附的代码) 在模型库中的“群集示例”中,海龟被设置为在彼此太近时对齐、粘合和分离(取决于最近邻的距离和最小间隔)。这个方法很有效,我可以用它来告诉海龟们聚集在一起,在它们继续移动的过程中保持完整。我的问题是如何让(绿、黄、红)海龟在遇到白海龟时聚集,让蓝海龟随意移动Netlogo 如何根据海龟的颜色和最小距离将它们分组,netlogo,Netlogo,我正在用NetLogo写一个模型,我从5个海龟种群开始,它们的颜色(蓝、绿、黄、红、白)随机开始坐标。当我运行模型时,我希望当白海龟遇到绿海龟、黄海龟或红海龟时,它们会加入模型,并作为一个群体一起移动。当白海龟遇到蓝海龟时,它会从中分离出来,然后继续随机移动。我在模型库中看到了鸟群的示例模型,我一直在尝试为我的模型修改它(请参阅所附的代码) 在模型库中的“群集示例”中,海龟被设置为在彼此太近时对齐、粘合和分离(取决于最近邻的距离和最小间隔)。这个方法很有效,我可以用它来告诉海龟们聚集在一起,在它
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
]
;Setting up the 5 turtles here
to setup
clear-all
create-turtles pop1
[ set color blue ; all pop
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop2
[ set color green ; stage1
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop3
[ set color yellow ; stage2
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop4
[ set color red ;stage3
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
create-turtles pop5
[ set color white ; chv
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set shape "person"]
reset-ticks
end
to go
ask turtles [ flock ]
;ask turtles [ meet]
;; the following line is used to make the turtles ;; animate more smoothly.
repeat 5 [ ask turtles [ fd 0.2 ] ] ;this seems much faster
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;ask turtles [ fd 1 ] ;than this
tick
end
;How do I make to get only the (green, yellow, red) turtles to flock when
;they meet the white turtles. Leaving the blue turtles moving randomly. How can do that?
; find-samecolor
; if any? flock
; ifelse separate
;end
;If green, yellow, red meets white turtle, they move together; nearest distance
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse
distance nearest-neighbor < minimum-separation
[ align ]
[ cohere ]
;[separate]
]
end
;If blue meets white turtle, they move together; nearest distance
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
;;; SEPARATE
;to separate ;; turtle procedure
; turn-away ([heading] of nearest-neighbor) max-separate-turn
;end
;;; ALIGN
to align ;; turtle procedure
turn-towards average-flockmate-heading max-align-turn
end
to-report average-flockmate-heading ;; turtle procedure
;; We can't just average the heading variables here.
;; For example, the average of 1 and 359 should be 0,
;; not 180. So we have to use trigonometry.
let x-component sum [dx] of flockmates
let y-component sum [dy] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; COHERE
to cohere ;; turtle procedure
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates ;; turtle procedure
;; "towards myself" gives us the heading from the other turtle
;; to me, but we want the heading from me to the other turtle,
;; so we add 180
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
;;; HELPER PROCEDURES
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
;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees
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
海龟自己的[
羊群伙伴;附近海龟的代理
最近的邻居;我们最亲密的一个同伴
]
;在这里安置5只海龟
设置
清除所有
创建海龟pop1
[设置颜色为蓝色;全部为流行色
设置大小为1.5;;更易于查看
setxy随机xcor随机ycor
设置形状“人”]
创建海龟pop2
[将颜色设置为绿色;阶段1
设置大小为1.5;;更易于查看
setxy随机xcor随机ycor
设置形状“人”]
创建海龟pop3
[将颜色设置为黄色;阶段2
设置大小为1.5;;更易于查看
setxy随机xcor随机ycor
设置形状“人”]
创建海龟pop4
[设置颜色为红色;阶段3
设置大小为1.5;;更易于查看
setxy随机xcor随机ycor
设置形状“人”]
创建海龟pop5
[将颜色设置为白色;chv
设置大小为1.5;;更易于查看
setxy随机xcor随机ycor
设置形状“人”]
重置滴答声
结束
外带
问海龟[羊群]
;问海龟[见面]
;; 下面的线条是用来制作海龟的;;动画制作更加流畅。
重复5次[询问海龟[fd 0.2];这似乎要快得多
;; 为了提高效率,以牺牲平滑为代价
;; 动画,替换以下行:
;询问海龟[fd 1];比这个
打上钩
结束
;我该怎么做才能让(绿、黄、红)海龟在
;他们遇到了白海龟。让蓝海龟随意移动。你怎么能这么做?
; 找到samecolor
; 如果有的话?一大群
; 如果分开的话
;结束
;如果绿、黄、红遇到白海龟,它们会一起移动;最近距离
聚集;;海龟手术
寻找同伴
如果有的话?羊群
[寻找最近的邻居
如果有
距离最近邻<最小间隔
[对齐]
[凝聚]
[分开]
]
结束
;如果蓝海龟遇到白海龟,它们会一起行动;最近距离
寻找同伴;;海龟手术
将其他海龟置于视野范围内
结束
寻找最近的邻居;;海龟手术
将最近的邻居min设置为一个羊群伙伴[与自己保持距离]
结束
;;; 分离
;分开;;海龟手术
; 转向(最近邻居的[航向])最大单独转向
;结束
;;; 排列
对齐;;海龟手术
转向平均flockmate航向最大对齐转向
结束
报告平均羊群头目;;海龟手术
;; 我们不能只是平均标题变量。
;; 例如,1和359的平均值应为0,
;; 不是180。所以我们必须使用三角学。
让x-分量和[dx]为配偶
让y-分量和[dy]为群配偶
如果x分量=0,y分量=0
[报告标题]
[报告x组件和y组件]
结束
;;; 凝聚
凝聚;;海龟手术
转向平均航向转向flockmates max cohere转向
结束
报告朝向羊群的平均航向;;海龟手术
;; “对我自己”给出了另一只乌龟的标题
;; 对我来说,但我们要我给另一只乌龟的标题,
;; 所以我们加上180
让x-分量表示配偶的[sin(对我+180)]
让y分量表示群友的[cos(朝向我+180)]
如果x分量=0,y分量=0
[报告标题]
[报告x组件和y组件]
结束
;;; 助手程序
转向[新航向最大转弯];;海龟手术
最多转向(减去新转向)最大转向
结束
;转向[新航向最大转向];;海龟手术
; 最多转弯(减去航向新航向)最多转弯
;结束
;; 右转“转”度(如果“转”为负数,则向左),
;; 但转弯角度不得超过“最大转弯”度
最多转动[转动最大转动];;海龟手术
如果abs转向>最大转向
[If else turn>0
[rt最大转弯次数]
[lt max turn]]
[右转弯]
结束
您可以根据自己的规则让两种不同类型的海龟拥有自己的海龟。我做了一个多代理的工作,每个人都有他们自己的规则,和Deafters turtles自己的团队一起工作,一切都像一个符咒
ant-own[ velocita-ant metabolismo-ant sf ;;scorta ants massimo_sf] killer own[velocita-killer metabolismo-killer sk ;;scorta killers massimo_sk]
你可以让两种不同类型的海龟拥有自己的规则。我做了一个多代理的工作,每个人都有他们自己的规则,和Deafters turtles自己的团队一起工作,一切都像一个符咒
ant-own[ velocita-ant metabolismo-ant sf ;;scorta ants massimo_sf] killer own[velocita-killer metabolismo-killer sk ;;scorta killers massimo_sk]
很抱歉耽搁了,因为某种原因,当你第一次问这个问题时,我没有看到。本规范中的关键程序是:
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
这样做的目的是说,任何海龟,源海龟可以看到被认为是一个植物学家。你想做的是说蓝海龟不能成为群友(也就是说,它们自己不创造群友,也不被其他有色海龟视为群友)。试试这个:
to find-flockmates ;; turtle procedure
if color != blue
[ set flockmates other turtles with [color != blue] in-radius vision
]
end
第一条线是为了让蓝海龟找不到同伴。第二行将蓝海龟排除在其他动物群之外
to flock ;; turtle procedure
find-flockmates
if any? flockmates
to flock ;; turtle procedure
set flockmates find-flockmates
if any? flockmates