Finding the closest agent - netlogo

I want to find the closest agent, For example, if I have two breeds foxes and rabbits. How to find the closest rabbit for each fox. I've come up with the following code which stores the distances in a list from each of the foxes to a subset of rabbits in a particular radius.
I am wondering if there is a better and/or a simpler way to do this netlogo?
breed [rabbits rabbit]
breed [foxes fox]
foxes-own [closest-rabbit]
to setup
clear-all
create-rabbits 10
create-foxes 2
ask rabbits[
setxy random-xcor random-ycor
set color white
]
ask foxes[
setxy random-xcor random-ycor
set color orange
]
end
to go
ask foxes[
let rabbits-in-range rabbits in-radius 10
let dist-list []
let rabbit-list []
ask rabbits-in-range[
set dist-list lput distance myself dist-list
set rabbit-list lput self rabbit-list
let min-dist-idx position (min dist-list) dist-list
set closest-rabbit item min-dist-idx rabbit-list
]
]
end

Have you tried with something like this?
ask turtles [ show min-one-of other turtles [distance myself] ]
In a more general form
ask <agent_set> [ <function> min-one-of <agent_set> [distance myself] ]
In your case would be ask foxes [ set closest-rabbit min-one-of rabbits [distance myself] ]
breed [rabbits rabbit]
breed [foxes fox]
foxes-own [closest-rabbit]
to setup
clear-all
create-rabbits 10
create-foxes 2
ask rabbits[
setxy random-xcor random-ycor
set color white
]
ask foxes[
setxy random-xcor random-ycor
set color orange
]
end
to go
ask foxes[
show min-one-of rabbits [distance myself]
set closest-rabbit min-one-of rabbits [distance myself]
face closest-rabbit
]
end
You can also check this other related questions
How to find the closest turtle(s)?
Minimum distance between turtles

Related

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

If variable target = fila 1, how do i ask a condition that reports true if target is any of filas

I need a condition where one turtle evaluates if other turtle has variable target any of filas
i have tried, target = filas, target = any? filas, member? target filas (this one works but in my code sometimes target = nobody and creates a netlogo error message)
set people-ahead min-one-of (other people in-cone (velocidad + espacio-personal + 0.5 ) 106.36 with [puesto? = false and target != any? filas]) [distance myself]
condition that reports true if target is any of filas
breed is an attribute of a turtle in exactly the same way as other built in variables (such as size) or variables defined by a turtles-own statement. This means you can directly check with [breed] of.
Here is a complete example that checks the breed of a selected turtle named target and changes its colour.
breed [type1s type1]
breed [type2s type2]
to setup
clear-all
create-type1s 20
[ setxy random-xcor random-ycor
set color red
]
create-type2s 20
[ setxy random-xcor random-ycor
set color yellow
]
ask one-of turtles
[ set size 2
let target min-one-of other turtles [distance myself]
if [breed] of target = type1s [ask target [set color white]]
if [breed] of target = type2s [ask target [set color blue]]
]
end
You can also just use the name of the breed to get the turtle-set that contains all the turtles of that breed. I'm not exactly sure what you are trying to do, but I think you probably want to replace:
set people-ahead min-one-of (other people in-cone (velocidad + espacio-personal + 0.5 ) 106.36 with [puesto? = false and target != any? filas]) [distance myself]
with
let candidates (filas in-cone (velocidad + espacio-personal + 0.5) 106.36 with [not puesto?])
set people-ahead min-one-of candidates [distance myself]

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.

How to ask turtles to place in a desired area, on Netlogo?

I want to create turtles, which they place in a desired area with random coordinate:
they should place in the white area and in middle of it in a line. in other words, in the top regtangle, their xcor should be random and their ycor is 10. in the right regtangle, their ycor should be random and their xcor is 10 and so on.
When you create turtles, you can give them instructions such as their location. For example:
create-turtles 1 [ set ycor 10 ]
Alternatively, you can sprout the turtles from the relevant patches and their location will already be set. For example:
ask n-of 5 patches with [pcolor > 1] [ sprout 1 ]
to place-on-color [#color]
let _patches (patches with [pcolor = #color])
ask turtles [
move-to one-of (_patches with [not any? turtles-here])
]
end
Add error checking if you may have too many turtles. (Or remove the unique occupancy constraint if you don't want it.)