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

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.

Related

What is causing my patches to cover my turtles?

I have been following the tutorial listed here Netlogo tutorial pt3
In the section "Patches and variables" I simply do not observe the image shown when I followed the steps exactly.
Instead, I can only see a green cube with my agents seemingly stuck inside.
What am I missing here? My word is only a single cube.
These are the routines that I've copied from the tutorial that should show turtles scattered across the green patch.
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-turtles
create-turtles 100
ask turtles [ setxy random-xcor random-ycor ]
end
to setup-patches
ask patches [ set pcolor green ]
end
It turns out the tutorial omits to tell you that you need to set the max-pzcor value in model settings to zero for this to work.

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 do I spread a color between neighbouring patches?

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

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.

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