如何根据海龟的颜色和最小距离将海龟分组
How to group turtles together depending on their colour and minimum distance
我正在 NetLogo 中编写一个模型,我从 5 个颜色(蓝色、绿色、黄色、红色、白色)的海龟种群开始,起始坐标是随机的。当我 运行 模型时,我希望当白色乌龟遇到绿色或黄色或红色乌龟时,它们会加入并作为一个整体移动。当白海龟遇到蓝海龟时,它与蓝海龟分开并继续随机移动。我在模型库中看到了鸟群的示例模型,我一直在尝试为我的模型修改它(见附件代码)。
在模型库的 'flocking example' 中,海龟被设置为在彼此太近时对齐、凝聚和分离(取决于最近邻和最小分离的距离)。这很有效,我能够用它告诉海龟聚集在一起,在它们继续移动时保持它们作为一个整体完整。我的问题是如何让(绿、黄、红)海龟遇到白海龟时只聚集在一起,而让蓝海龟随机移动。
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
您可以制作两种不同类型的 turtles-own,它们都有自己的规则。我做了一个多代理工作,每个人都有自己的规则,并且有聋人 turtles-own 组一切都像魅力一样。
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
第一行让蓝龟找不到 flockmates。第二行将蓝海龟排除在其他海龟群之外。
-----更新----
你实际上在一个关于植绒的乌龟程序中有这个程序。此过程将变量 'flockmates' 设置为找到的海龟集合,但是,如果海龟是蓝色的,则没有 flockmates 并且它不是 return 空海龟集合。修复它的最佳方法是使用 to-report
过程,这样所有海龟的变量都会有合适的数据类型(海龟集)。
to-report find-flockmates ;; turtle procedure
ifelse color = blue
[ report no-turtles ] ; this is an empty turtle-set
[ report 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
我正在 NetLogo 中编写一个模型,我从 5 个颜色(蓝色、绿色、黄色、红色、白色)的海龟种群开始,起始坐标是随机的。当我 运行 模型时,我希望当白色乌龟遇到绿色或黄色或红色乌龟时,它们会加入并作为一个整体移动。当白海龟遇到蓝海龟时,它与蓝海龟分开并继续随机移动。我在模型库中看到了鸟群的示例模型,我一直在尝试为我的模型修改它(见附件代码)。
在模型库的 'flocking example' 中,海龟被设置为在彼此太近时对齐、凝聚和分离(取决于最近邻和最小分离的距离)。这很有效,我能够用它告诉海龟聚集在一起,在它们继续移动时保持它们作为一个整体完整。我的问题是如何让(绿、黄、红)海龟遇到白海龟时只聚集在一起,而让蓝海龟随机移动。
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
您可以制作两种不同类型的 turtles-own,它们都有自己的规则。我做了一个多代理工作,每个人都有自己的规则,并且有聋人 turtles-own 组一切都像魅力一样。
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
第一行让蓝龟找不到 flockmates。第二行将蓝海龟排除在其他海龟群之外。
-----更新----
你实际上在一个关于植绒的乌龟程序中有这个程序。此过程将变量 'flockmates' 设置为找到的海龟集合,但是,如果海龟是蓝色的,则没有 flockmates 并且它不是 return 空海龟集合。修复它的最佳方法是使用 to-report
过程,这样所有海龟的变量都会有合适的数据类型(海龟集)。
to-report find-flockmates ;; turtle procedure
ifelse color = blue
[ report no-turtles ] ; this is an empty turtle-set
[ report 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