我正在用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发布于 2020-08-19 14:11:49
抱歉,你第一次问这个问题的时候,由于某种原因,我没有看到这个问题。本代码的关键步骤是:
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,也不会返回空的turtleset。修复它的最好方法是使用一个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发布于 2020-08-19 10:52:10
你可以让两种不同类型的海龟拥有自己的规则。我做了一个多代理的工作,每个人都有自己的规则,有了执法者、海龟、自己的团队,一切都像魅力一样运作。
ant-own[ velocita-ant metabolismo-ant sf ;;scorta ants massimo_sf] killer own[velocita-killer metabolismo-killer sk ;;scorta killers massimo_sk]https://stackoverflow.com/questions/63342515
复制相似问题