Updating a list in Netlogo - netlogo

I'm trying to update the values in a list on netlogo.
The code here is my collision function: one breed collides into another, turns the circle black and then I want it to store the 'who' number of the 'found' circle in a list.
It currently just outputs the who number of each found one and prints a 'list' of one number each time.
Here's the code.
to collide2
if any? circles2-on neighbors [
set mylist [] ; not sure why I need this but
;doesn't work otherwise
set whovalue [who] of circles2-on neighbors
set mylist lput whovalue mylist
ask circles2-on neighbors [set color black]
output-print mylist
]
end

If there are multiple circles-on neighbors, then the code [who] of circles-2 on neighbors will return a list of their values of the who ID. You need the sentence command to concatenate two lists, using lput will add the list as one item in a list. For example: print lput [1 2] [3 4] will print out the list [3 4 [1 2]]. In your case, you are adding this to an empty list so the created list only has one element, and that element is itself a list.
But if your goal is to get a list of the who values, you don't need to do anything that complicated. The code let whovalues [who] of circles2-on neighbors will create a list automatically, because of creates a list.
So your code could be replaced with:
to collide2
if any? circles2-on neighbors
[ set whovalues [who] of circles2-on neighbors
ask circles2-on neighbors [set color black]
output-print whovalues
]
end
However, see how you're having to keep track of the who values but then separately re-find all the neighbouring turtles? This is because you are storing the who values instead of storing the agents themselves. It is almost always better to refer to agents directly rather than identify them via their who value, so you can avoid awkward code like: ask turtle with [who = <some value>] [do-something].
So, switching to turtles instead of who, and also switching to agentsets rather than lists (not essential but lists are only relevant if you need to track the order or have multiple instances etc):
to collide2
if any? circles2-on neighbors
[ set colliders circles2-on neighbors
ask colliders [set color black]
output-print colliders
]
end
So now it is clear that the same turtles are being found and turned black. Even better, you can do this so that the agentset is only created once (more efficient as well as easier to read):
to collide2
let colliders circles2-on neighbors
if any? colliders
[ ask colliders [set color black]
output-print colliders
]
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.

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.

In Netlogo, how do you move a turtle to the other end of its link?

I've created two breeds of turtles in my simulation: one is a regular turtle and the other is a halo that is intended to overlap each turtle. Whenever a turtle is hatched (either created as part of the setup procedure or created with netlogo's hatch function), a halo is also hatched and linked by calling a separate make-halo function.
create turtles turtle-initial-number
;;(all the turtle genes are set here)
if halos-enabled [make-halo]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
create-link-from myself
[ tie
hide-link ] ]
end
Due to some interactions I've implemented, sometimes the turtles and the halos become detached from one another, so I'd like to add a step at the end of each tick where all halos snap back to their turtles where they belong. Is there a way to move the halo or set its coordinates to the turtle at the other end of the link?
The other option is to solve whatever is happening when the disconnect occurs. I have another breed of turtle (people) who can "push" others with this push-away function below. Turtles (and their halos) occupying the 9 squares in front of the person are pushed forward along the same direction at the person is facing. When they are pushed, for some reason the turtle isn't at the centre of the halo anymore.
to push-away
ask people [
let push-dir heading
ask patch-ahead 2
[ask turtles-here
[set heading push-dir
fd 2]
ask neighbors
[ask turtles-here
[set heading push-dir
fd 2]
]
]
]
end
In theory, the tie should link the movements. But to snap the halo to its turtle, you can ask the halo to move-to the turtle. The only trick will be identifying the correct turtle and you haven't shown enough of your code for me to sort out the identification for you.
I suggest you actually add a variable to the halos that records their turtle rather than using a link. If the link has no other purpose, there is no need to create all those extra model entities. You would use it like this:
halos-own [my-owner]
to make-halo
hatch-halos 1
[ set size sight-radius * 2 + 1
set shape "square"
set color lput 64 extract-rgb color
__set-line-thickness 0.5
set my-owner myself ; this is the new line
]
end
to push-away
<all the code you have already>
ask halos
[ move-to my-owner
]
end

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

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

simultaneously coordination in Netlogo

I'm currently trying to implement a model in Netlogo where the turtles’ behaviors depend on all of their neighbors.
My point of departure is the coordination game code provided by:
http://modelingcommons.org/browse/one_model/2549#model_tabs_browse_info
According to this model the payoff of for the turtle is determined by introducing a variable which takes the color of neighbor as its value.
ask turtles [
let his-color [color] of one-of turtles-on neighbors
if color = yellow and his-color = yellow [set payoff A-yellow-yellow set alt-payoff B-red-yellow]
However, I need to my turtles to gain their payoff by comparing their color with all of their neighbors simultaneously. The last part is problematic due to Netlogo's default synchronic update
Can anyone guide me in how to make the update simultaneously and depending on all of the neighbors, or does someone have a reference to a place where this is discussed?
Just collect all colors before changing any of them. E.g.,
turtles-own [nbr-colors]
to go
ask turtles [
set nbr-colors [color] of neighbors ;get list of current colors
]
ask turtles [
set payoff compute-payoff nbr-colors
set color anything-you-want
]
end