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 ""
...
]
Related
I'm a beginner, and I'm trying to generate a geographical space with three different colored patches. I'd also like for them to not be neighbors if they have the same color (for yellow and black) as that would make no sense in the model I'm working on. This is the code I've used so far
to setup-patches
ask patches [set pcolor green]
ask n-of number-of-cities patches [
set pcolor yellow]
ask n-of number-of-coal patches [
set pcolor black]
end
Again, I'd like black patches to not have any black neighbour, and the same for yellow.
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.
We are designing a classroom in Netlogo that looks like this:
Classroom
The person represents the teacher walking around in the classroom, the grey patches represent empty seats, the green patches represent seats with concentrated students and the red patches represent seats with unconcentrated students. We want to 'spread' the lack of concentration as a 'disease' to the neighbouring GREEN patches. We found some lines of code that almost did what we want:
ask patches with [pcolor = green] [set pcolor [pcolor] of one-of neighbors4]
But this would eventually turn all patches grey, so we tried to change it into this:
ask patches with [pcolor = green] [set pcolor [pcolor] of one-of neighbors4 with [pcolor = red]]
This line gives the following error: 'OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.'
Does any of you know how to fix this?
The error you're getting comes up because you're telling green patches to change their color to one of their neighbors that is red, but not all green patches necessarily have red neighbors. In such a case when you tell a green patch "change your color to one of your neighbors that is red" the green patch goes, "well, among my neighbors there is nobody with that color." The special agentset nobody is then returned, and nobody has no color for the original green patch to access!
I think you might have an easier time of this by going the other way- that is, having the red patches be the ones that spread. For example, with this example setup:
to setup
ca
resize-world 0 25 0 25
ask patches [
set pcolor green
]
ask n-of 5 patches [ set pcolor red ]
reset-ticks
end
You have a world with a bunch of concentrating students, and 5 troublemakers. Now, if you get your troublemakers to check if they have any neighboring patches who could be corrupted, you can have the lack of concentration spread outwards with increasing speed:
to colorswap
ask patches with [ pcolor = red ] [
; See if there is any possible neighbor patch
; to whom I can spread my lack of concentration
let target one-of neighbors4 with [ pcolor = green ]
; if the target exists, have them change their color
if target != nobody [
ask target [
set pcolor red
]
]
]
end
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
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