How do I spread a color between neighbouring patches? - netlogo

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

Related

Assigning colors to patches with no neighboring patches having the same color

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.

How to get turtles run back and forth a coloured patch?

I have a simple image as a map. I would like the turtles to start from the lighter grey colour and run to the darker patches for resources. How can I do that?
My Code
to setup-patches
import-drawing "01.png"
import-pcolors "01.png"
ask patches [
setup-house
;setup-resource
]
end
to setup-house
create-turtles [setxy where pcolor = grey]
set house? where pcolor = grey
end
The image is at https://i.imgur.com/dmODyUW.png.
I can provide more details on request.
Okay, a patch is actually a NetLogo term for one of the grid cells in the World. Your image suggests that the grey areas will cover multiple NetLogo patches each. The following code creates some random grey multi-patch areas and a house at one of the patches in one of the grey areas.
breed [houses house]
to testme
clear-all
setup-patches
setup-houses
end
to setup-patches
ask n-of 3 patches
[ set pcolor gray
ask neighbors
[ set pcolor gray
ask neighbors
[ set pcolor gray
]
]
]
end
to setup-houses
ask one-of patches with [pcolor = gray]
[ sprout-houses 1
[ set color red
]
]
end
Your question is too vague to properly answer, but hopefully this will get you on the right track. I suggest you redo the NetLogo tutorials and look at some of the models in the library included in the software to find pieces of code that do tasks you are going to need.

Netlogo random patches setup without them touching at all

I am trying to set up random patches that have no corners touching at all, and the color of those patches are different colors of green (to visually represent each patch having a random quality value in my model). My conceptual idea of how to do this is to have the “false” part of my “ifelse” statement to basically tell the program to keep trying to assign the patch a different place if it has another patch touching any part of it, until all patches have a place with no other patches touching it all (including corners). I just have no idea if that is possible or what code could do that. Is there a netlogo primitive that is equivalent to "until" ? Any help or ideas would be appreciated!
Here is a part of my code:
patches-own [ quality ]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask n-of number-of-patches patches
[ ifelse sum [ pcolor ] of neighbors = 0
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10
]
[ set pcolor black ; this is the line I need to change
]
]
end
Have a look at the primitives while and neighbors in the NetLogo dictionary. I am not completely clear on the sequencing you want in your code so I can't provide a full answer. However, you want something like:
while any? neighbors [(however you define the ones you don't want to touch]
[(try again)
]
Note that patches always touch other patches because the world is constructed as a grid of patches. So you presumably want patches with specific conditions (such as high values of quality) to not touch each other.
As JenB says, patches are always touching other patches, so I'm assuming the question is "How can I generate number-of-patches green patches that aren't touching any other green patches?". Then I'd do this:
to setup-patches
while [ count patches with [ shade-of? green pcolor ] < number-of-patches ]
[ ask one-of patches with [ quality = 0 and count neighbors with [ shade-of? green pcolor ] = 0 ]
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10 ] ]
end
Basically, while the number of green patches is less than the desired number of green patches, choose one of the patches that isn't green and doesn't have any green neighbors. Then turn it green.

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 ""
...
]

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.