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 - netlogo

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

Related

Ask turtles to change colour and stop when they meet other turtles 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
]
]
]

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

Why do i get this error every time i try to run the code within my U.I.?

I have the code:
if pcolor = blue
[ask turtles-here [set heading towards one-of neighbors4 with [pcolor] = grey ] fd speed ].
This has the aim of making a turtle that is spawned on a blue patch set its heading towards a neighbour with pcolor = grey and then follow that path. However when I run the code I get the error :
WITH expected a true/false value from (patch -12 14), but got 55 instead.
I don't know what caused the error and what I should do to correct it. Any help would be greatly appreciated.
I think it come from a mise understanding of with condition ...
something like
ask patches [
if pcolor = blue [
ask turtles-here [
set heading towards one-of patches with [pcolor = grey]
]
]
]
work for me !

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.

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.