Ask turtles to change colour and stop when they meet other turtles Netlogo - netlogo

I have two turtles of colour 'green' and colour 'blue'. I would like when the green turtles meet the blue turtles, the blue turtles turn white and stop moving while the green turtles continue moving randomly. Here is my code, I did but it is gives an error of Expected a literal value and is not doing returning the expected results. Any assistance is greatly appreciated.
ask turtles with [color = green]
[if any? other turtles-here with [color = blue]
[set turtles with [color = blue] [color = white]
]
]

You forgot to ask (instruct) the turtles. The keyword with subsets the correct turtles but you need to set a variable, not set a turtleset. Also, '=' is used for evaluation (comparing), not for assigning values. I think you want this:
ask turtles with [color = green]
[ if any? other turtles-here with [color = blue]
[ ask turtles-here with [color = blue]
[ set color white
]
]
]
Note that I had to write turtles-here again, or all the blue turtles would turn white. A more readable way that also reduces errors is to set up a temporary variable (using let) for the relevant turtles.
ask turtles with [color = green]
[ let changers other turtles-here with [color = blue]
if any? changers
[ ask changers
[ set color white
]
]
]

Related

one-of neighbors with [pcolor = blue], why does the with line give me an error? ASK expected input to be an agent or agentset but got NOBODY instead

Im working on an assignment on netlogo and im making an aquarium.
i need to write code to grow the algae in the aquarium, but i get this error message:
ASK expected input to be an agent or agentset but got NOBODY instead.
second one is my code:
to groei_algen
ask patches [
if pcolor = green [
if %omringing = 100 [set pcolor blue]
ask one-of neighbors with [pcolor = blue] [
if random 100 > %omringing [set pcolor green]
]
]
]
end
with %omringing is how many algae there are around the patch
it works without the 'with'
i need to decrease the chance to grow algae when there is more algae around an algae patch.
It seems like you are getting this error because you did not account for the possibility of a patch not having any neighboring patches with color blue.
A simple way to solve this is to add an additional if statement like below:
to groei_algen
ask patches [
if pcolor = green [
if %omringing = 100 [set pcolor blue]
if any? neighbors with [pcolor = blue] [
ask one-of neighbors with [pcolor = blue] [
if random 100 > %omringing [set pcolor green]
]
]
]
]
end

How to change color of n number of turtles out of total in a specific region?

I am working on an infectious disease model, I've created a world with two regions. The specified number of people get initialized in their specific region.
Now, I need to initially infect the same number of people in each region with the disease using the initially_infected global variable. e.g 10 people infected in one region and 10 in the other region.
The infection should be indicated by changing color of turtles to red.
I've tried using "n-of" but it is not working correctly as want to restrict the initially infected people in their respective regions.
to setup_agents
create-humans green_population [
move-to one-of patches with [pcolor = green and not any? humans-here]
set shape "green person"
set color yellow
set size 1
set green_antibodies 0
]
ask n-of initially_infected humans [set color red]
create-humans blue_population [
move-to one-of patches with [pcolor = blue and not any? humans-here]
set shape "blue person"
set color yellow
set size 1
set blue_antibodies 0
]
ask n-of initially_infected humans [set color red]
end
Your code is not working because you are asking humans in general (by using ask n-of initially_infected humans [set color red]), and not based on where they are.
You can tackle this in a variety of pretty much equivalent ways. To do it in the tidies way, you should ask yourself what is the main programming feature that univocally differentiates these two groups: their shape? the color of the patche they are on? should they hold a turtles-own variable differentiating them? should they be separate breeds?
Whatever you choose this feature to be, use that feature in the ask n-of ... statement.
In the fully-reproducible example below, I just use the turtles' color as the distinguishing element:
breed [humans human]
globals [
green_population
blue_population
initially_infected
]
humans-own [
green_antibodies
starting_region
]
to setup
clear-all
ask patches [
ifelse (pxcor > 0)
[set pcolor green]
[set pcolor blue]
]
set green_population 300
set blue_population 270
set initially_infected 10
setup_agents
end
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
ask n-of initially_infected humans with [color = green + 1] [
set color red
]
ask n-of initially_infected humans with [color = blue + 1] [
set color red
]
end
Perhaps a more elegant option would be to create a little procedure that takes the populations' color and the quantity to infect as inputs. Modifying only the relevant part of the code, it would be:
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
infect-humans (green + 1) initially_infected
infect-humans (blue + 1) initially_infected
end
to infect-humans [hue quantity]
ask n-of quantity humans with [color = hue] [
set color red
]
end
In my example I used the humans' colors as distinguishing factor, but you can easily adapt this to any other thing you might want to use.

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

Netlogo: ask patches in-radius but not the center patch itself

I would like to make things with patches in-radius but excluding the patch with the agent itself, the center patch, so I modify the Myself example from the model library:
to splotch
ask turtles [
ask one-of patches in-radius 2 with [not any? turtles-here] [
set pcolor [ color ] of myself
]
]
tick
end
but this code also excludes other patches with turtles so it should be something like
to splotch
ask turtles [
ask one-of patches in-radius 2 [not self][
set pcolor [ color ] of myself
]
]
tick
end
But this code isn't working and I don't figure out how it has to be.
You need the other primitive. However, other excludes agents of the same type and you are wanting a turtle to exclude a patch. So, you need to get the relevant patch to ask the other patches. Here's one approach:
to testme
clear-all
create-turtles 3 [setxy random-xcor random-ycor]
splotch
end
to splotch
ask turtles
[ let mycolor color
ask patch-here
[ ask other patches in-radius 4
[ set pcolor mycolor
]
]
]
end
If you want something more like the way you were doing it, you can create a local variable to store the patch and then exclude it like this:
to splotch
ask turtles
[ let mypatch patch-here
ask patches in-radius 4 with [self != mypatch]
[ set pcolor [color] of myself
]
]
end

How to replace color with another color in a polygon?

I have patches as follows:
I would like to color white patches as in this figure:
Here my code to color white patches in blue:
ask patches with [pcolor = white] [
if any? neighbors with [pcolor = blue] [set pcolor blue] ]
But the problem is that I obtain this figure: .
Thanks in advance for your help.
Alan is right about the cause of your problem, but you don't need to create a next-pcolor patch variable. If you put both conditions inside the with block, NetLogo will first construct the agentset of patches, and then ask these patches to do stuff, thereby avoiding the timing problem that you had. Let's try it. And since you're obviously going to have to do it with both blue and cyan patches, let's build a more general version:
to color-white-patches-v1 [ c ]
ask patches with [ pcolor = white and any? neighbors with [ pcolor = c ]] [
set pcolor c
]
end
That you can then call with:
color-white-patches-v1 cyan
color-white-patches-v1 blue
Here is the result:
But it's not quite what you wanted. That's because neighbors gives you all 8 neighbors of a patch. Let's try with neighbors4 instead:
to color-white-patches-v2 [ c ]
ask patches with [ pcolor = white and any? neighbors4 with [ pcolor = c ]] [
set pcolor c
]
end
Not quite there yet. It think you are going to have to resort to something like patch-at. In this example, I look at only the patch above:
to color-white-patches-v3 [ c ]
ask patches with [ pcolor = white and [ pcolor ] of patch-at 0 1 = c ] [
set pcolor c
]
end
I'm not sure if that was exactly what you wanted and how well that applies to your general problem, but with some combination of patch-at, you should be able to get what you need.
The problem arises because patches are being changed sequentially, so some white patches find they have new blue neighbors by the time they respond to ask.
ask patches with [pcolor = white and any? neighbors with [pcolor = blue]] [set pcolor blue]
Note: this answer is edited in response to Nicholas's observation that with will create the entire agent set before ask is called. However I stuck with neighbors since that's how I read the question.