netlogo patch affected by distance from a certain patch - distance

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.

Related

Netlogo: Setting pcolor patches with specific distance from each other

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.

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.

Tell agent to not cross a road in Netlogo model

I'm trying to add a condition that doesn't allow an agent to cross over a road. The road patches are set to color red. I can't seem to figure out how to get this condition to work. I ultimately want the agent to turn around if the road is in the patch ahead. Here is my net logo code so far.
to go
ask turtles [
move
]
tick
if ticks >= 60 [stop]
end
to move
ifelse random-float 1 < q
[
ifelse random-float 1 < w
[let target-patch max-one-of neighbors [veg-suitability]
face target-patch]
[let target-patch max-one-of neighbors [pelev]
face target-patch]
]
[
ifelse [pcolor] of patch-ahead 1 = red
[lt random-float 180]
move-to one-of neighbors
ldd-normal
]
end
to ldd-normal
let ldd-distance (ldd-scale)
fd ldd-distance
end
The logic of your move procedure is a bit confused I think. First you have a random chance to either move to a patch with a higher value of a variable of interest (with the uphill primitive) or, if the random draw fails, it moves to a random neighbour. If you don't want it to move onto a red patch then you need to test if the patch that is chosen is red, but you just move it without checking.
After you have moved the turtle, you then check the colour of patch-ahead. Your problem here is that patch-ahead depends on the direction the turtle is facing, which has nothing to do with the direction it has already been moving. You either make it turn (though it may not turn enough) OR move forward. So it never actually moves away.
I can't give you an actual answer because I don't know what your logic is supposed to be. But you could look at structures like:
move-to one-of neighbors with [pcolor != red]
Or, if there are enough red patches that it is possible that there aren't any non-red neighbours (which would cause an error if you tried to move to one), you could use:
let okay-patches neighbors with [pcolor != red]
if any? okay-patches [move-to one-of okay-patches]
Another option is that you only meant to face rather than move to the patch in the first sections of code, then test whether it is facing a red patch and turn around if it is.

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: Moving turtles towards patch color

Is it possible to have a turtle move towards a patch with a certain color?
i.e. have a turtle move towards red patch from blue patch?
sure enough
ask the turtles in question to
face one-of patches with [pcolor = red]
fd 1
although you should do some exception handling because if there is not a patch of that color you will get a
"FACE expected input to be an agent but got NOBODY instead. error
while turtle 0 running FACE called by Agent Monitor"
error
I do it something like this
let targ one-of patches with[pcolor = red]
if targ != nobody [set heading towards targ fd 1]
I hope that works for what you want.
Alternately as I was reminded in the comments.
If any? Patches with[ pcolor = red]
[set heading towards one-of patches with[ pcolor = red ] fd 1]
But as Seth says in the comments that computes the red patches twice which is costly.