How I can stop agent reach an other netlogo - 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]
]

Related

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.

NetLogo - how do I get all patches the turtle facing?

How do I get a patch set that contains all patches that the turtle is facing?
I know patch-ahead report the patch with a specific distance. But what if I want to get all patches in this direction instead of the single one with specific distance?
What you can do is hatch a turtle and move it forward until it reaches the edge of the world, adding all the patches it crosses.
Here's a visible version to see the approach:
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set pcolor red
hatch 1
[ while [can-move? 1]
[ forward 1
set pcolor red
]
die
]
]
end
To actually do the patchset version, you need to start with the current patch and add the patches as the hatched turtle moves over them. Try this for a procedure version and a demonstration of how it can be used:
turtles-own [ my-path ]
to testme
clear-all
create-turtles 1 [setxy random-xcor random-ycor]
ask one-of turtles
[ set my-path get-patches-forward self
print my-path
]
end
to-report get-patches-forward [ #me ] ; turtle procedure
let front-patches patch-here
hatch 1
[ while [can-move? 1]
[ forward 1
set front-patches (patch-set front-patches patch-here)
]
die
]
report front-patches
end
This will return the wrong answer if the world is wrapped because the hatched turtle can keep on going indefinitely. Instead, you would need to check its coordinates rather than relying on the can-move? primitive.

Conditional network neighbours

I want to select the network neighbours filtered by a link attribute rather than a turtle attribute. For example, this might be selecting only the closest friends based on a strength score on the friendship link. I could do this by having different link breeds for close friends, but this would require constantly changing breeds as the condition was or was not required.
Below is an example with a boolean condition.
links-own [flag?]
to testme
clear-all
ask patches [set pcolor white]
create-turtles 20
[ setxy 0.95 * random-xcor 0.95 * random-ycor
set color black
]
ask n-of 3 turtles [set color red]
repeat 40
[ ask one-of turtles
[ create-link-with one-of other turtles
[ set flag? random-float 1 < 0.4
set color ifelse-value flag? [red] [gray]
]
]
]
; colour-neighbours
colour-neighbours2
end
to colour-neighbours
ask turtles with [color = red ]
[ ask my-links with [flag?]
[ ask other-end [ set color blue ]
]
]
end
to colour-neighbours2
ask turtles with [color = red ]
[ ask turtle-set [ other-end ] of my-links with [flag?]
[ set color blue ]
]
end
I am currently doing the equivalent of colour-neighbours, but it involves stepping through several contexts. The colour-neighbours2 version is conceptually closer because it is referring directly to the network neighbours. However, because of the of, I get a list of neighbours that I then have to convert to an agentset.
This is for teaching and, while both work, they seem very convoluted when compared to the unconditional network neighbourhood with the link-neighbors primitive. That is, if I didn't care about the flag, I could simply say ask link-neighbors [ set color blue ].
Is there a more direct way to identify network neighbours conditional on a link attribute?
You already cover most possibilities. Another way to do it would be:
to colour-neighbours3
foreach [ other-end ] of my-links with [ flag? ] [ t ->
ask t [ set color blue ]
]
end
I would avoid colour-neighbours2 because, as you stated, it requires the conversion from a list to an agentset. Whether you should use colour-neighbours or colour-neighbours3 is, I think, a matter of personal preference.
Just to add a more-convoluted (worse?) option that uses an agentset:
to colour-neighbours4
ask turtles with [ color = red ] [
let flag-links my-links with [flag?]
ask link-neighbors with [ any? my-links with [ member? self flag-links ] ] [
set color blue
]
]
end

NetLogo : Keep turtles moving continuously till the end

Readers,
I'm a beginner in NetLogo. Please help me in solving few issues with my code that is below:
I'm getting an error "You can't use tick in a turtle context, because tick is observer-only.
I need to get tick value updated after each turtle go all three of "arrive-reception, arrive-triage, go-drroom".
the rest of people is not moving around the arrive reception, arrive triage is running.
to setup-people
set-default-shape turtles "person"
set destination ( patch-set patch -2 34 patch 8 34 )
create-turtles uninfected
[ set color blue
allocate-turtles
]
create-turtles infected
[ set color red
allocate-turtles
]
end
to allocate-turtles
if (pcolor = 9.9 and any? turtles-here)
[
set size 1.5
set heading 0
setxy int random-xcor int random-ycor
]
end
to go
move-people
arrive-reception
arrive-triage
go-drroom
tick
end
to move-people
ask turtles [
rt 360
forward 1
]
end
to arrive-reception
ask n-of (random count turtles) turtles
[
if windows = 1
[
move-to patch -2 34
ifelse not any? turtles-here
[ wait wait-time ]
[ wait-in-queue ]
]
]
end
to wait-in-queue
set arrival-time ticks
bk 1
if any? other turtles-here
[ wait-in-queue ]
wait wait-time
if ticks - arrival-time > wait-time
[ set arrival-time 0
fd 1 ]
end
to arrive-triage
if triage = "Open"
[
move-to patch 26 11
if any? other turtles-here
[ wait-in-queue]
wait wait-time
move-to one-of patches with [pcolor = 109 and not any? other turtles-here ]
wait wait-time
]
end
to go-drroom
move-to one-of patches with [pcolor = 128]
if ( min-one-of other turtles in-radius 5 [distance myself] != nobody)
[
move-to one-of patches with [pcolor = 129]
if ( min-one-of other turtles in-radius 5 [distance myself] != nobody)
[
move-to one-of patches with [pcolor = 5]
if any? seats with [turtles = nobody]
[
move-to one-of max-n-of 6 neighbors [seats]
]
]
]
wait wait-time
die
end
Thanks.
First, some basic programming tricks - don't write so much before trying to debug. If you make a small change and check it, then it's easy to work out where the error is. The first draft of a procedure can be as simple as:
to go-drroom
end
and then fill in the details of what happens in the procedure later.
Typically this error is because you forgot to close a bracket somewhere. That is, one of the procedures starts with ask turtles [ ... and there is no ] so NetLogo is still thinking that the code applies to turtles. However, I can't see an obvious missing ].
But you do have a conceptual problem. The term context is used in NetLogo to refer to who is asking the code to be done and to whom. So ask turtles [ forward 1] is the observer asking the turtles to move and is an observer context procedure. You are not thinking about what context you are in when writing the procedures, and this is probably what is setting off your error.
In the go procedure, you first call move-people. This does ask turtles [ ] so is (appropriately) from the observer context. Then you call arrive-reception and it is also okay.
But then you call arrive-triage and go-drroom still from the observer context and have commands like move-to. Who is being asked to move? You don't have ask turtles .... On the other hand, the procedure wait-in-queue has commands like move-to, but it is fine because it is only called from within an ask turtles ... in the arrive-reception procedure.

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.