Netlogo: Setting pcolor patches with specific distance from each other - netlogo

I'm trying to set pcolor orange for different patches. I want them to be created randomly, but always at a distance of R from each other. In this current code, it creates the patches randomly just fine, but simply removes the orange patch if they are within the radius, resulting in fewer than N orange patches. How can I solve this so that all N patches are created at a distance R instead of just the ones which randomly fall out of radius R of each other?
ask n-of N patches
[
if not any? other patches with [pcolor = orange] in-radius R [
set pcolor orange
]]

You are vey close, but asking N random patches can result in any number of orange patches, maybe one patch will get colored and the other random N-1 patches will be within the radius R, even if there are lots of patches that still can be colored.
Maybe if you ask all patches you will obtain more consistent results,
ask patches
[
if not any? other patches with [pcolor = orange] in-radius R
and count patches with [pcolor = orange] < N
[
set pcolor orange
]
]
In case that you want all the possible patches colored instead of just N you can remove (or comment) the whole line and count patches with [pcolor = orange] < N.
Also note that ask patches will iterate over the patches randomly, so if you need a reproducible or ordered coloring of the patches this is not the ideal way to do it.

Related

Is there a way to kill a certain number of turtles within a radius of a patch with a certain color in Netlogo?

I am very new to Netlogo and I am trying to simulate culling of turtles within a certain region based on the characteristics of the patch. Within my model landscape, I have one single patch that is orange. I want 50% of turtles within 5 pixels of that patch to die. I would also like this process to continue every year after the first year of the simulation (this part I think I have). Can anyone help me?
if d = 10 [
if year >= 2 [
let ring nobody
set ring patches in-radius 5 patches with [pcolor = orange]
ask turtles-on ring [die]
]
]
in-radius reports agents that are within a certain distance from the caller, so the orange patch must be the one calling in-radius.
The code in your example is not reproducible, so I'll make an arbitrary example:
to setup
clear-all
ask one-of patches [
set pcolor orange
]
create-turtles 100 [
setxy random-xcor random-ycor
]
end
to go
ask patches with [pcolor = orange] [
ask turtles-on patches in-radius 5 [
if (random 2 < 1) [die]
]
]
end
Note that in-radius reports agents within a certain distance, where the unit of measure of distance is a patch`s side.
Identifying patches within a certain distance and then asking all turtles on those patches to die (as in the example above, which follows the approach in your question) has a different effect than asking all turtles within a certain distance to die.
Which of the two approaches fits your case is something that you have to decide. Below, the code that would address turtles directly:
to go
ask patches with [pcolor = orange] [
ask turtles in-radius 5 [
if (random 2 < 1) [die]
]
]
end

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

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 patch affected by distance from a certain patch

I would like to ask how i can set up patches.
Initial condition is there is a red patch at a certain point. The other patches will be affected by a distance from the red patch. As a distance go far, the impact will be greater.
It might not be simple as a beginner. please help me out!
Edited to include Nicholas' commends
to example
let red-patch one-of patches with [pcolor = red]
ask red-patch [
ask other patches [
if random-float 1 > (distance red-patch / (max [distance red-patch] of patches))
[set pcolor blue]
]
]
end
This would have it be a gradient effect so they are more likely to be blue the closer they are to the red patch. Flipping the > to a < just inverses the effect.