Reputation: 19
I am writing a model in NetLogo where I start with a 5 turtles populations in colours (blue, green, yellow, red, white) with random starting co-ordinates. When I run the model, I want when the white turtle meets the green or yellow or red turtle, they become joined with it and move together as a group. And when the white turtle meets the blue turtle, it separates from it and both continue moving randomly. I saw the example model for bird flocking in the model library and I have been trying to modify it for my model (see code attached).
In the 'flocking example' from the models' library, turtles are set to align, cohere and separate when too close to each other (depending on the distance for the nearest-neighbour and minimum-separation). This works well and I was able to use it to tell turtles to flock, keeping them intact as a group as they continue to move. My question is how to get only the (green, yellow, red) turtles to flock when they meet the white turtles and leaving the blue turtles moving randomly.
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
Upvotes: 1
Views: 511
Reputation: 17678
Sorry about the delay, for some reason I didn't see this question when you first asked it. The key procedure in this code is:
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
What this does is says that any turtle that the source turtle can see is considered a flockmate. What you want to do is say that blue turtles cannot be flockmates (that is, they don't create flocks themselves and are not considered flockmates by other coloured turtles). Try this:
to find-flockmates ;; turtle procedure
if color != blue
[ set flockmates other turtles with [color != blue] in-radius vision
]
end
The first line makes it so that blue turtle don't find flockmates. The second line excludes blue turtles from the flocks of others.
-----UPDATE----
You actually have this procedure within a turtle procedure about flocking. This procedure sets the variable 'flockmates' to the set of turtles found but, if the turtle is blue, there are no flockmates and it doesn't return an empty turtleset. The best way to fix it is to use a to-report
procedure, so all turtles will have an appropriate data type (a turtle set) for their variable.
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
Then, to use this procedure, you need to change:
to flock ;; turtle procedure
find-flockmates
if any? flockmates
to this:
to flock ;; turtle procedure
set flockmates find-flockmates
if any? flockmates
Upvotes: 0
Reputation: 32
You can make two different types of turtles-own with their own rules. I did a multi agents works and everyone has their owns rules and with deafferents turtles-own group everything works like a charm.
ant-own[ velocita-ant metabolismo-ant sf ;;scorta ants massimo_sf] killer own[velocita-killer metabolismo-killer sk ;;scorta killers massimo_sk]
Upvotes: 0