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
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
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]
Related
I am a teacher of Primary Education and Early Childhood Education and I am trying to generate a simulator through NetLogo on how fertilization and pesticides are decimating the butterfly population. However, despite having read the manual, I am not managing to program the code to make it work.
My problem is that although I set the turtles I can't establish the following relationship between the variables/buttons:
If butterflies randomly touch a plant (which is fertilized with pesticide) its pollinating capacity is reduced by a certain percentage (depends on the amount of pesticide)
My problem is that I can't get the pollination capacity of the butterfly to be set to 100% initially and that the greater the amount of pesticide, the lower its pollination capacity is when touching a flower. Currently, although the amount of pesticide is the highest, there are peaks where its pollination capacity increases instead of being reduced.
breed [butterflies butterfly]
breed [flowers flower]
globals
[
butterfliesless-neighborhoods ;; how many patches have no butterflies in any neighboring patches?
pollinating-capacity ;; measures how well-bivouaced the butterflies are
]
patches-own
[
butterflies-nearby ;; how many butterflies in neighboring patches?
]
flowers-own
[
carried-butterflies ;; the butterflies I'm carrying (or nobody if I'm not carrying in)
found-bivouac? ;; becomes true when I find a bivouac to drop it in
]
to setup
clear-all
set-default-shape butterflies "butterflies"
set-default-shape flowers "flower"
ask patches
[ set pcolor green + (random-float 0.8) - 0.4] ;; varying the green just makes it look nicer
create-butterflies num-butterflies
[ set color white
set size 1.5 ;; easier to see
setxy random-xcor random-ycor ]
create-flowers num-flowers
[ set color brown
set size 1.5 ;; easier to see
set carried-butterflies nobody
set found-bivouac? false
setxy random-xcor random-ycor ]
reset-ticks
end
to update-butterflies-counts
ask patches
[ set butterflies-nearby (sum [count butterflies-here] of neighbors) ]
set butterfliesless-neighborhoods (count patches with [butterflies-nearby = 0])
end
to calculate-pollinating-capacity
set pollinating-capacity (butterfliesless-neighborhoods / (count patches with [not any? butterflies-here])) * 100
end
to go
ask flowers
[ ifelse carried-butterflies = nobody
[ search-for-butterflies ] ;; find a butterflies and pick it up
[ ifelse found-bivouac?
[ find-empty-spot ] ;; find an empty spot to drop the butterflies
[ find-new-bivouac ] ] ;; find a bivouac to drop the butterflies in
wiggle
fd 1
if carried-butterflies != nobody
;; bring my butterflies to where I just moved to
[ ask carried-butterflies [ move-to myself ] ] ]
ask butterflies with [not hidden?]
[ wiggle
fd pesticide-amount ]
tick
end
to wiggle ;; turtle procedure
rt random 50 - random 50
end
to search-for-butterflies ;; flowers procedure
set carried-butterflies one-of butterflies-here with [not hidden?]
if (carried-butterflies != nobody)
[ ask carried-butterflies
[ hide-turtle ] ;; make the butterflies invisible to other flowers
set color blue ;; turn flower blue while carrying butterflies
fd 1 ]
end
to find-new-bivouac ;; flowers procedure
if any? butterflies-here with [not hidden?]
[ set found-bivouac? true ]
end
to find-empty-spot ;; flowers procedure
if all? butterflies-here [hidden?]
[ ask carried-butterflies
[ show-turtle ] ;; make the butterflies visible again
set color brown ;; set my own color back to brown
set carried-butterflies nobody
set found-bivouac? false
rt random 360
fd 20 ]
end
Defined Buttons
Your screenshot is a nice start with sliders, buttons and plots. In the code tab consider making two breeds of turtles: butterflies and plants.
breed [butterflies butterfly]
butterflies-own [pollinatingCapacity]
breed [plants plant]
plants-own [pesticideAmount]
And then think about how you might decrease pollinatingCapacity of a butterfly when it is on the same patch as a plant. A butterfly can see if any plants are on the same patch using plants-here
I'm modeling territory selection in NetLogo, where turtles pick a territory center ("start-patch") and then build a territory out from there based on value of patches. Turtles always return to the start-patch after claiming a new patch, then choose, move to, and claim the next most valuable patch. After choosing a territory, the turtle knows which patches it owns, and patches know their owner.
Ultimately, the start-patch of a territory may not actually end up being the true geographic center. After a turtle has selected its territory, how might I ask it to evaluate the territory, identify the geographic center, and calculate proximity of the start-patch to the true center of the territory? (Note: I don't want to force turtles to keep the start-patch in the geographic center--they are free to choose any patches they want. But I may force turtles to re-select a territory if there isn't a close match--these territories are not very efficient otherwise.)
Here's an example of how territory start-patches (black stars) do not equal geographic centers. Some example code is below. Any suggestions? Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own [
start-patch ;; the selected center of the territory
sum-value ;; sum of values of patches in my territory
territory-list ;; list of patches I've selected
territory ;; agentset of patches I've selected
established ] ;; true/false, true when has settled in a final territory after assessing geographic center
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [
pick-center
build-territory]
tick
end
to pick-center
if start-patch = 0
[move-to best-center ;; (calculated by a reporter elsewhere as moving windows for a cluster of high-benefit patches)
set start-patch patch-here
set owner self
set territory-list (list patch-here)
set territory (patches with [owner = myself])
]
to build-territory
ifelse sum-value < threshold
[ pick-patch ] ;; keeps picking patches for territory until I've met my threshold
[ assess-geographic-center] ;; once met threshold, assess real center of patch-set
end
to pick-patch
let _destination highest-value ;; (this is calculated by reporters elsewhere based on benefit / travel costs to a patch)
face _destination forward 1
if patch-here = _destination
[ claim-patch _destination ]
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set territory-list lput patch-here territory-list
set territory (patch-set territory _patch)
move-to start-patch
end
to assess-geographic-center
;; Once the territory is built, the turtle should identify the actual
;; geographic center of the patch-set it has selected...how to do this?
;; Once it knows the center, the turtle should compare the distance to the original start-patch.
;; If >10 patches away, it will move to that start-patch and start over with selecting a territory....
end
Could you get away with just the mean pxcor and pycor of all patches? Something like:
to setup
ca
reset-ticks
let n 70
ask one-of patches [
set pcolor red
]
while [ ( count patches with [pcolor = red ] ) < n ] [
ask one-of patches with [ pcolor = red ] [
ask one-of neighbors4 [set pcolor red ]
]
]
let xmean mean [pxcor] of patches with [ pcolor = red ]
print xmean
let ymean mean [pycor] of patches with [ pcolor = red ]
print ymean
ask patch xmean ymean [ set pcolor blue
]
end
Following the previous answer, here is what I came up with. This goes in the last procedure in my original code:
to assess-geographic-center
let xmean mean [pxcor] of patches with [ owner = myself ]
let ymean mean [pycor] of patches with [ owner = myself ]
ask patch xmean ymean [ set pcolor black ] ;; make the geographic center visible
let geographic-center patch xmean ymean
let distance-away distance geographic-center ;; the turtle is on its start-patch when assessing this distance
ifelse distance-away <= 5
[ set established true ] ;; turtle is happy if start-patch and geographic-center are approximately equal, territory "established"
[ move-to geographic-center ;; otherwise, turtle moves to geographic-center,
reposition ] ;; and follows a procedure "reposition" to makes this the new start-patch and repick the territory
end
I'm new to programming in Netlogo and I need to adapt the code so that turtles get bored after 50 ticks of flying in formation and make a right turn of 90 degrees. I modified the code while looking at some other examples but I get a very strong hunch it doesn't do what it's supposed to do. Can you please help me or give me a hint? Here's the code in question:
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
flock-tick
]
to setup
clear-all
create-turtles population
[ set color yellow - 2 + random 7 ;; random shades look nice
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set flockmates no-turtles
set flock-tick 0 ]
reset-ticks
end
to go
ask turtles [ flock ]
;; the following line is used to make the turtles
;; animate more smoothly.
repeat 5 [ ask turtles [ fd 1 ] display ]
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;; ask turtles [ fd 1 ]
tick
end
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision 90
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
to flock-timer
if any? flockmates [
set flock-tick 50
while [ flock-tick > 0 ] [
set flock-tick ( flock-tick - 1 ) ]
if flock-tick = 0
[ set heading ( heading - 90 ) ] ]
end
The rest of the code is still the same as the the model in the library.
I must admit I'm not really sure of what I'm doin and the northwestern website is also not very helpfull to me (probably my own fault).
Thanks!
If we add comments to your code, we can see the logic problem.
to flock-timer
if any? flockmates [
;the next line sets `flock-tick` to 50
set flock-tick 50
;the next line sets `flock-tick` to 0
while [flock-tick > 0] [set flock-tick (flock-tick - 1)]
;so the next condition will always be satisfied
if (flock-tick = 0) [
;and so the heading will always be changed
set heading (heading - 90)
]
]
end
You might break things up to make it easier to follow the logic.
to flock-timer ;turtle proc
update-flock-tick
if (flock-tick = 0) [get-bored]
end
to update-flock-ticks ;turtle proc
;you can add additional logic here if you wish
if (flock-tick > 0) [
set flock-tick (flock-tick - 1)
]
end
to get-bored ;turtle proc
set heading (heading + one-of [-90, 90])
end
This is not a complete solution, but it should get you started.
what i'm trying to do is:
Pseudo code
to flock
check flockmates
if find any leader inside the flockmates
change myheading to leader's heading
else
follow flocking rule [separate, allign, cohesion]
end
Below is code that I use.
turtles-own
[ flockmates
nearest-neighbor
leader?
leader
]
to setup
__clear-all-and-reset-ticks
ask n-of population patches with [ pcolor = blue]
[sprout 1
[set color white
set size 0.6
set leader? false]
]
choose-leaders
end
to choose-leaders
ask n-of ((percent_of_leader / 100) * population ) turtles
[set leader? true
set color black
set size 0.6
set leader self
]
end
to go
ask turtles [flock]
end
to flock
find-flockmates
let nearby-leaders turtles with [leader? ]
ifelse any? nearby-leaders
[ set heading [heading] of nearby-leaders]
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[separate]
[ if any? flockmates
[align
cohere ]]]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision vision-angle
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
however when I run the code, this error message pop-up can't set turtles variable HEADING to non-number [147]. and it point to this code [set heading [heading] of nearby-leaders]. what did i do wrong here? really appreciate if someone can help.
Because nearby-leaders in your code is a turtle-set, you should use one-of:
set heading [heading] of one-of nearby-leaders
Sorry if this is obvious, I have searched everything I can think of and asked a colleague and we are both stuck.
I have some code (below - a slight twist on wolf sheep predation) that I want to run behaviorspace (input also below). However, it never runs to completion. It always seems to get stuck on step #0 of a run (the run # it gets stuck on varies). The runs that it sticks on work fine if they are run in isolation (so all the variables seem fine). It just seems to freeze (producing no error messages, nor does it crash).
Any ideas what is causing it?
Thanks,
Simon
Code:
globals [grass] ;; keep track of how much grass there is
;; Sheep and wolves are both breeds of turtle.
breed [sheep a-sheep] ;; sheep is its own plural, so we use "a-sheep" as the singular.
breed [wolves wolf]
turtles-own [energy] ;; both wolves and sheep have energy
patches-own [countdown]
to setup
clear-all
ask patches [ set pcolor green ]
;; check GRASS? switch.
;; if it is true, then grass grows and the sheep eat it
;; if it false, then the sheep don't need to eat
if grass? [
ask patches [
set countdown random grass-regrowth-time ;; initialize grass grow clocks randomly
set pcolor one-of [green brown]
]
]
if fences?[ ; this code adds in blue fences to create patches of various size (if fences are turned on). Turtles cannot pass over fences
ask patches with [pxcor mod connectivity2 = 0]
[ set pcolor blue ]
ask patches with [pycor mod connectivity = 0]
[ set pcolor blue ]
]
set-default-shape sheep "sheep"
create-sheep ((count patches - (count patches with [pcolor = blue])) / 25) ;; create the sheep, then initialize their variables. The starting density always remains constant despite a varying world size
[
set color white
set size 1.5 ;; easier to see
set label-color blue - 2
set energy random (2 * sheep-gain-from-food)
setxy random-xcor random-ycor
]
set-default-shape wolves "wolf"
create-wolves ((count patches - (count patches with [pcolor = blue])) / 50) ;; create the wolves, then initialize their variables. The starting density always remains constant despite a varying world size
[
set color black
set size 2 ;; easier to see
set energy random (2 * wolf-gain-from-food)
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
reset-ticks
end
to go
if count wolves = 0 and count sheep = 0 and ((count patches with [pcolor = green]) = (count patches - (count patches with [pcolor = blue]))) [ stop ] ; stop model when the whole world has flipped to grass
ask sheep [
move
if grass? [
set energy energy - 1 ;; deduct energy for sheep only if grass? switch is on
eat-grass
]
death
reproduce-sheep
]
ask wolves [
move
set energy energy - 1 ;; wolves lose energy as they move
catch-sheep
death
reproduce-wolves
]
if grass? [ ask patches [ grow-grass ] ]
set grass count patches with [pcolor = green]
tick
display-labels
end
to move ;; turtle procedure
rt random 50
lt random 50
ifelse [pcolor] of patch-ahead 1 = blue
[ move] ;; If there is a blue patch ahead of you, choose another random direction
[ fd 1 ] ;; Otherwise, it is safe to move forward.
end
to eat-grass ;; sheep procedure
;; sheep eat grass, turn the patch brown
if pcolor = green [
set pcolor brown
set energy energy + sheep-gain-from-food ;; sheep gain energy by eating
]
end
to reproduce-sheep ;; sheep procedure
if random-float 100 < sheep-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ ifelse [pcolor] of patch-ahead 1 = blue ;; hatch an offspring
[ move] ;; If there is a blue patch ahead of the offspring, it chooses another random direction
[ fd 1 ] ;;If not, offspring move forward 1 step
]]
end
to reproduce-wolves ;; wolf procedure
if random-float 100 < wolf-reproduce [ ;; throw "dice" to see if you will reproduce
set energy (energy / 2) ;; divide energy between parent and offspring
hatch 1 [ ifelse [pcolor] of patch-ahead 1 = blue ;; hatch an offspring
[ move ] ;; If there is a blue patch ahead of the offspring, it chooses another random direction
[ fd 1 ] ;;If not, offspring move forward 1 step
] ]
end
to catch-sheep ;; wolf procedure
let prey one-of sheep-here ;; grab a random sheep
if prey != nobody ;; did we get one? if so,
[ ask prey [ die ] ;; kill it
set energy energy + wolf-gain-from-food ] ;; get energy from eating
end
to death ;; turtle procedure
;; when energy dips below zero, die
if energy < 0 [ die ]
end
to grow-grass ;; patch procedure
;; countdown on brown patches: if reach 0, grow some grass
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown grass-regrowth-time ]
[ set countdown countdown - 1 ]
]
end
to display-labels
ask turtles [ set label "" ]
if show-energy? [
ask wolves [ set label round energy ]
if grass? [ ask sheep [ set label round energy ] ]
]
end
; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.
behaviorspace input:
variables:
["world-width" 51]
["fences?" true]
["wolf-gain-from-food" 20]
["sheep-reproduce" 7]
["show-energy?" false]
["grass?" true]
["connectivity" 10 25 50]
["wolf-reproduce" 5]
["grass-regrowth-time" [1 1 100]]
["sheep-gain-from-food" 4]
repetitions: 1
reporters:
count wolves
count sheep
count patches with [pcolor = green]
measure runs at each step
setup: setup
Go: go
Stop (I have tried it with and without this): count wolves = 0 and count sheep = 0 and ((count patches with [pcolor = green]) = (count patches - (count patches with [pcolor = blue])))
Time limit: 5000
It's difficult for me to imagine this could be caused by anything other than running low on memory. Running very low on memory can cause the JVM to slow to a crawl.
What do the memory usage stats in the System tab of "About NetLogo" say? You can verify there that you're actually getting the heap size you asked for.