how to get the number of ticks for a turtle takes to go to a green patch? - netlogo

There is a one red patch and 10 turtles moving randomly.
When a turtle comes to the red patch, it becomes green.
I want to run the model 100 times and get the number of ticks (it takes for the first patch color change) for all 100 runs into an excel sheet.
to setup
clear-all ; clear everything when we click setup
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask n-of humans patches [set pcolor green]
end
to setup-turtles
create-turtles Mosquitos
ask turtles [
set size 1
set shape "bug"
set color yellow
setxy random-xcor random-ycor
]
ask turtles
[
ifelse coin-flip?
[ifelse coin-flip? [set heading 0][set heading 90]]
[ifelse coin-flip? [set heading 180][set heading 270]]
]
end
to go
ask turtles
[
ifelse coin-flip?
[ifelse coin-flip? [set heading 0][set heading 90]]
[ifelse coin-flip? [set heading 180][set heading 270]]
forward 1
if pcolor = green
[
set pcolor red
show ticks
]
]
tick
end
to-report coin-flip?
report random 2 = 0
end

BehaviorSpace automatically keeps track of the steps. All you need to do is set up a BehaviorSpace experiment that stops the simulation wh`en the patch turns green.
You need to tell BehaviorSpace to stop when a patch turns green. So any? patches with [pcolor = green] as the stop condition in the experiment. You don't need anything in the box "Measure runs using these reporters" box as the step is sent to the file, so just leave it as count turtles. Also uncheck the box so that it only reports at the end of the run. Use the table form of BehaviorSpace output.

Related

How can I move a turtle as close as possible to a certain patch?

I have a single blue patch and would like to move a turtle to the closest, empty patch to it. The only way I can think of doing this is using in-radius in a loop, increasing the radius size by one each time, but is there a better way?
globals [bluey]
to setup
ca
ask one-of patches [set pcolor blue set bluey self]
ask n-of 250 patches [sprout 1]
end
to-report nearest-empty [#patch]
report min-one-of
[other (patches with [not any? turtles-here])] of #patch
[distance #patch]
end
to test
setup
;the following fails if all patches occupied
;(can add a test for nobody)
ask nearest-empty bluey [set pcolor red]
end

NetLogo: Apply set [variable] from sub-procedure to global process?

I want to simulate reproduction process of turtles in time when single patch can by used only once. If the patch is red and ticks mod 50 = 0 then turtles-here (on this patch) hatch new 10 turtles. Every patch can be used only once during the whole simulation run.
Please, how can I include this condition into my code? I tried simple to change patch color to green with hope that the next hatching process will run only with red ones. However next time step NetLogo doesn't keep this patch green but changes it back to red. Thus my reproduction run from the same patch.
Any suggestions will be highly appreciated
The part of my code:
to go
if ticks mod 50 = 0 [ask patches with [pcolor= red] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[hatch 10 ;
die]
; set pcolor green - change infestlev from 2 to 5 only for specific tick, not for the rest of the simulation
end
The code you have should work fine. In your description, you state that the colour turns back to red - so that's why this code isn't working, somewhere else you have a colouring procedure. Alternatively, if you don't want to rely on colour (or if you want colours to mean something else), then you can add a variable to each patch to keep track of whether it has already reproduced.
patches-own [reproduced?]
to setup
...
ask patches [set reproduced? FALSE]
...
end
to go
if ticks mod 50 = 0 [ask patches with [not reproduced?] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[ hatch 10
die ]
set reproduced? TRUE
end
Just as a general comment, it is a little odd to ask the patch to reproduce when what you are really trying to do is have the turtle on a patch reproduce. Logically you are saying that once one turtle on a patch has reproduced, then no other turtle on that patch can ever reproduce. If the reproduction is truly governed by the patch, it would be more usual to use sprout instead of hatch. That gets you code that looks like this:
to reproduce
sprout 10 [ any commands you want the new turtles to do ]
set reproduced? TRUE
end
My final working code with steps (available here: http://ulozto.cz/xRqtDDfV/timing-of-turtle-sprout-nlogo):
setup turtles
if turtle touch red patch, turn this patch blue
at the same time - tick 10 -> sprout from every blue patch 10 nwe turtles
every patch can be used only once during simulation run (turn red, assured by reproduced? variable)
enter code here
patches-own [reproduced?]
to setup
clear-all
setup-turtles
setup-patches
change-color
reset-ticks
end
to setup-patches
ask patches [set reproduced? FALSE]
ask patches [set pcolor green]
ask n-of 80 patches [set pcolor red] ; identify turles which could be a source for new turtles
end
to setup-turtles
crt 1
ask turtles [set color yellow]
end
to go
if ticks mod 10 = 0 [
ask patches with [(pcolor = blue) and not (reproduced?)]
[reproduce] ; set reproduction to every 10 ticks for all blue patches
]
move-turtles
change-color
tick
end
to move-turtles
ask turtles [fd 1]
end
to change-color ; if turtle touch red patch, red turns blue
ask turtles [if pcolor = red
[set pcolor blue]
]
end
to reproduce ; sprout 10 new turtles from blue patches at defined time step (multiply of 10 ticks)
sprout 10
set reproduced? TRUE
end

Delete patches that have a specific color

How can I delete patches that have a specific color ("clear-patches" deletes all patches? For example, from this code, I would like to delete all blue patches and keep only yellow patches.
to test
clear-all
ask patches [ set pcolor yellow]
repeat 20 [
ask one-of patches with [pcolor = yellow] [ set pcolor blue]
]
end
Thank you very much for your help.
Not exactly sure what you what "deleting" a patch to mean. Unlike turtles and links, patches cannot be killed.
If by "delete a patch" you mean "reset all of the patch's variables to their defaults", there isn't any command in NetLogo that does that to only some patches, instead of all them.
If you just want to clear certain variables in certain patches, you have to name those variables explicitly, for example:
ask patches with [pcolor = blue] [
set pcolor black
set plabel ""
...
]

NetLogo - change color of patch when agent is on top

I am trying to make my turtle change the color of a patch when it comes into contact with it and have tried the following code:
to deesculateviolence
ask turtles [
if pcolor = red [set pcolor blue]
]
end
The code does not come up with any errors but when I play the model, the color of the patch does not change. I have tried similar codes from different models and still cannot get the patch to change color. If anyone knows where I am going wrong I'd really appreciate your help.
I think your code does the right thing :
to setup
clear-all
create-turtles 5 [
move-to patch random 20 random 20
]
ask n-of 25 patches [set pcolor red]
reset-ticks
end
to go
ask turtles [
rt random 10
fd 1
if pcolor = red [set pcolor blue]
]
tick
end
you can see the effect in following example better

asking red turtles to avoid other turtles and move to one of its neighbor which is empty and has highest conc

I'm new to Netlogo. Here I'm trying ask red turtles to move towards the hight conc. patches. yellow turtles do not move. I did that! but I also want to ask the red turtles to avoid the patches which have yellow or red turtles on them and move to the neighbor of high conc.. In my code I asked them to stop once they become next to an occupied patch just because I couldn't do it. I also want to avoid getting 2 turtles on the same patch at any time. Any one could help me with that please?
patches-own [conc]
to set-up
clear-all
ask patch random-pxcor random-pycor [
set conc 200
set pcolor scale-color red conc 0 1]
crt 5 [setxy random-xcor random-ycor set shape "circle" set color red]
crt 20 [setxy random-xcor random-ycor set shape "circle" set color yellow]
reset-ticks
end
to go
diffuse conc 0.1
ask patches [set pcolor scale-color red conc 0 1]
ask turtles with [color = red]
[ifelse not any? turtles-on neighbors
[if [conc] of max-one-of neighbors [conc] > conc [
face max-one-of neighbors4 [conc]
move-to max-one-of neighbors4 [conc]]]
[stop]
]
tick
end
I think your code would read a little nicer if you used let to avoid repetition, like this:
let target max-one-of neighbors [conc]
if [conc] of target > conc [
face target
move-to target
]
For some different possible approaches to enforcing a "one turtle per patch" rule, see the One Turtle Per Patch Example model, in the Code Examples section of NetLogo's Models Library.
I assume that ifelse not any? turtles-on neighbors is your attempt to make turtles avoid occupied patches. But as you've written it, it has a stronger effect than that — it makes it so that any turtle with an adjacent occupied patch won't move at all.
I think you may have meant something more like:
ask turtles with [color = red] [
let targets neighbors with [not any? turtles-here]
let target max-one-of targets [conc]
if target != nobody and [conc] of target > conc [
face target
move-to target
]
]
Hope this helps.