NetLogo - Getting the 'who' of another turtle in the same patch - netlogo

I'm looking to get the 'who'/turtle ID of a turtle that occupies the same patch as another, and then add this as an item into a list for both turtles.
For example, say turtle A and turtle B are on the same patch, I'd like to store the who of turtle A in the list for turtle B and the who for turtle B in the list for turtle A.
I realise this may be quite a trivial thing to do, so I attempted to do this with the following code:
if not any? turtles-on neighbors[
if who != who[
set collision-list fput (list (who)) collision-list
]
]
Here, I'm checking the patch to see if it contains another turtle, if it does then I'm trying to store the who (using a condition for if the who is not the same as the current who) and if it isn't, then store this in the collision-list for each agent.

Ordinarily it is a mistake to work with who numbers instead of the turtles themselves. So I'll illustrate how you might augment a "collision list" of turtles.
turtles-own [clist]
to setup
ca
crt 100 [
setxy random-xcor random-ycor
set clist []
]
ask turtles [adjust-clist]
end
to adjust-clist ;turtle proc
let _ts [self] of (other turtles-here)
set clist (sentence _ts clist)
end

Related

How to extract the who number of agents in an agentset? (NetLogo)

I have a patch variable set as visitantBees [].
The intention is that each patch has a record of which agents were in it.
The method for registering the "visiting agents" is as follows:
to computing Frequency
ask patches
[
if any? turtles-here
[
set visitantBees lput [who] of turtles-here visitantBees
]
]
end
However, this way a list of list is returned ([[3 1 0 2 4]], for example).
Would anyone know how I can add only the who number to the visitantBees list?
Maybe a way to extract all the items from the turtles-here.
The reason why you get a list of lists is that both visitantBees and [who] of turtles-here are lists.
While visitantBees is a list because you set it as a list, why is [who] of turtles-here a list? Because turtles-here is an agentset - and the only way to report a variable of an agentset is to create a list. For example, if you wanted to know [color] of turtles, the only way NetLogo has to give you this information is to put all the turtles' colors in a list.
So, why is turtles-here an agentset? Because, even if sometimes turtles-here can contain 0 or 1 turtle, it can also contain multiple turtles. And anything that is fit to contain multiple agents has to be an agentset.
On the other hand, when you ask a single agent to report one of its variables, you get the value as such, i.e. not a list (unless that value is a list in itself, of course). For example, [color] of turtle 0 is just its color, not a list containing a color.
Therefore, you can achieve your goal by individually asking every turtle on the patch to append their who to visitantBees:
to computingFrequency
ask patches [
ask turtles-here [
set visitantBees lput who visitantBees
]
]
end
Or, given that turtles can automatically read and change the patches-own variables of the patch they are standing on, you can make it even simpler:
to computingFrequency
ask turtles [
set visitantBees lput who visitantBees
]
end
Which is also faster because it will only engage with turtles (who, by definition, are standing on a patch) rather than engaging with every patch even if there are no turtles on it.

Can I direct a turtle to a specific patch based on the result of comparing their variable values?

I have N groups of turtles which own the variable group-id (1 to N, N is defined at setup, each group has 25 turtles).
At the beginning each group of turtles spawns on a random patch with variable patch-group-id that matches the turtle group-id, like so:
let i 0
while [ i < n_groups ] [
let x random 100
let y random 100
ask patch x y [ set patch-group-id i set pcolor gray ]
create-turtles 25 [
set group-id i
setxy x y
]
set i i + 1
]
The turtles move around but at a later step in the process I would like them to move back to their ‘home’ patch. Other turtles may also change their group-id and therefore their home patch and I would like them to move there too.
Is there any way to do a comparison along the lines of…
Ask turtles [
Let target one-of patches with [patch-group-id = group-id]
]
And then ask the turtle to move-to or jump to the target?
NetLogo doesn’t like this (‘Min-one-of expected this input to be a number block, but got a true/false block instead’) and I think there are probably other issues too.
patch-group-id is a variable owned by patches and group-id is owned by turtles. So you need to tell NetLogo which group-id the patches are trying to match to. Try this:
ask turtles
[ let target one-of patches with [patch-group-id = [group-id] of myself]
]
Another way to do this is to drop the idea of group-id and simply have each turtle remember its home patch. Conceptually, this implements the idea that a group is entirely defined by its home patch. So your setup would look something like:
turtles-own
[ my-home
]
to setup
clear-all
ask n-of n-groups patches
[ sprout turtles 25
[ set my-home patch-here
]
set pcolor gray
]
end
Then you never need to construct the target, you simply get then to go to their variable my-home.
If you went down this path, you would also need to change the code that uses group-id. For example, you said that sometimes the turtles change their group-id, instead of set group-id [group-id] of ... you would have set my-home [my-home] of ...

Netlogo: "foreach" command with two lists

I have a complete directed graph with each link a weight of it's own. I've managed to select the max-out-link of every turtle. But, sometimes the max-out-link of two turtles are opposite of each other resulting in both links opposite of one another being selected. if this happens i want the link with the lower value to die.
i have created the two lists with this:
set max-end1 [[end1] of max-one-of my-out-links [trust]] of turtles
set max-end2 [[end2] of max-one-of my-out-links [trust]] of turtles
and by setting an x and y parameter like so:
ask turtles
[
set x max-one-of my-out-links [label]
set y my-in-links
]
i was hoping to compare each item of the two lists like so:
if [x] of max-end2 = any? [y] of max-end1
[
ifelse x X y
[ask x [die]]
[ask y [die]]
]
but i have no idea how to combine the foreach command with the if command
can someone help me?
I can't actually figure out how your code is supposed to work. Lists seems like an awkward way to approach this and the way you are using any? is not going to work. So, I have instead started again and written a standalone model (put it in an empty NetLogo session) to do what I think you are trying to do.
links-own [ weight ]
to testme
clear-all
create-turtles 15
ask turtles
[ create-links-to other turtles
[ set weight random 100
]
]
layout-circle turtles 10
kill-lowers
end
to kill-lowers
; first remove any that's not a max weight
ask turtles
[ let big-link max-one-of my-out-links [weight]
let dying my-out-links with [not member? self (link-set big-link)]
ask dying [die]
]
; find pairs and make lower turn red
ask links
[ let opp-links links with [end1 = [end2] of myself and end2 = [end1] of myself ]
if any? opp-links
[ ask opp-links [set color red]
]
]
end
The testme procedure just creates a complete network. It then calls the kill-lowers procedure to do the link removal. The first section has each turtle identify its out-link with the largest weight (or randomly selects one if two equally high) and then creates the link-set of all the other links and gets them to die. I think you already have that happening in your code.
So the interesting bit is the section that starts ask links. It randomly runs through all the links (think of it as a foreach except operating on a set of links rather than a list). In turn, for the particular link, the code checks if there is a link in the opposite direction and sets that to the variable named opp-links. The way it checks is to simply see if there is a link that has end2 to be its own end1 and also the other way. end1 is the source of a directed link and end2 is the target.
If there is such a link, it becomes red. That's so you can check the code is doing what you want. After you have tested it, then you have it die instead.

How to get a patch to count the turtles passing through it

I'd like to have patches count the number of turtles that have stood on them. What would be ideal is a event such as:
if turtle-lands-on-me [add one to count]
because a turtles could leave and come back and be counted twice (which is what I want) and it would avoid counting turtles who stand still twice or more (which I don't want). Is there any way to achieve this?
Thank you!
What you need is a variable for each patch (I am calling it 'landed' below). The following code assumes you want to know about the patch it lands on each time step, but ignores the ones it passes over. It also updates the counts only where the turtle changes the patch, as requested, and labels the patch with the count.
patches-own [landed]
to setup
create-turtles 20
[ setxy random-xcor random-ycor
]
end
to go
ask turtles
[ let old-patch patch-here
set heading random 360
forward one-of [0 0.5 1 3]
if old-patch != patch-here
[ ask patch-here
[ set landed landed + 1
]
]
]
ask patches [set plabel landed]
end
The problem is that a turtle can pass over multiple patches during one time step. You can see this in the example model for those turtles that move 3. If you also want them, you will need to do something like the 'Line of Sight' model in the NetLogo models library.

Changing Node ID with every Setup in Netlogo

We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]