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

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 !

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

Pedestrian environment: turtles do not recognise patch-type and surroundings

Recently I've started working on a pedestrian model simulation. I'm currently having a difficult time with controlling the movement patterns of my turtles. My code and blueprint.png is uploaded to Github.
So first, I upload a floor plan and tried to setup-variables and ask patches with pcolor = 0 to set as walls, pcolor = white to set as the ground, pcolor = red for doors, etc.
I'm able to create turtles, and let's say they start at the doors. I've tried to instruct them to avoid walls, yet the code breaks with runtime error: MOVE-TO expected input to be an agent but got NOBODY instead. Why can turtles start at patches with a colour but not a patch-type?
Even just the way the turtles are walking is unlike previous models I've tested in the model library. Any feedback would be welcome and appreciated. Thanks
Netlogo Code
extensions [ time ]
globals [
time-passed
walls
doors
exits
ground
art
corners-top-left
corners-top-right
corners-bottom-left
corners-bottom-right
]
patches-own [
patch-type
]
turtles-own
[
speed
wait-time
]
to setup
clear-all
import-dwg
setup-turtles
setup-variables
reset-ticks
end
to go
move
tick
update-time
end
to import-dwg
import-pcolors "blueprint.png"
end
to update-time
let minutes floor (ticks / 60)
let seconds ticks mod 60
if(minutes < 10)[set minutes (word "0" minutes)]
if(seconds < 10)[set seconds (word "0" seconds)]
set time-passed (word minutes ":" seconds)
end
to setup-turtles
create-turtles 2 [
move-to one-of patches with [ patch-type = "ground" ]
set heading towards one-of patches with [ pcolor = 65 ]
]
ask turtles [
set speed 1
set wait-time 0
set size 2
set color blue
pen-down
]
end
to move
ask turtles [
If any? Patches with [ pcolor = white ]
[set heading towards one-of patches with [ pcolor = white ]
fd 1]
]
tick
end
to setup-variables
ask patches with [ pcolor = 0 ] [
set patch-type "walls"
]
ask patches with [ pcolor = 15 ] [
set patch-type "doors"
]
ask patches with [ pcolor = white ] [
set patch-type "ground"
]
ask patches with [ pcolor = 65 ] [
set patch-type "art"
]
set time-passed "00:00"
end
Cross-posted to Reddit and found my answer (link):
You're going to hate this - in your setup function, setup-variables needs to be before setup-turtles, otherwise it doesn't know what "patch-type" is.
Edit: Also using "neighbors" for your move so they're looking at
adjacent patches, may help them not walk through walls and art.

How I can stop agent reach an other netlogo

I need a function of an agent against an agent which they stop when they reach each other
i tryed this psodo code
ask turtles [
if heading = 90 with [pcolor = red] [ stop ]
]
end
and thanks a lot.
The following code will stop if the patch ahead (whatever heading the turtle is facing) is red:
ask turtles
[ if [ pcolor ] of patch-ahead 1 = red [stop]
]
If you want a particular direction, such as your code implies with heading = 90 then you need something like:
ask turtles
[ if [ pcolor ] of patch-at-heading-and-distance 90 1 = red [stop]
]
In response to the additional information that the check should be for a turtle rather than a patch... This code makes no assumption about the number of turtles on each patch and stops if at least one such turtle is red.
ask turtles
[ if any? turtles-at 1 1 with [ color = red ] [stop]
]

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.