I would like to use ask to loop over all of my turtles, but I do not want the order to be random. My solution is to add all of the turtles to a list, sorted by their ID, and then loop over this list:
let sorted-turtles sort-on [who] turtles
foreach sorted-turtles [
x -> ask x [ ; x is the turtle
; do something
]
]
That works correctly, but vastly slows down my model.
My question is: is this the best way to ask all the turtles to do something in a pre-defined order?
I have tried using ask on the list directly (e.g. ask sorted-turtles [ print who ] but NetLogo says that ask needs an agentset rather than a list.
Thanks in advance,
Nick
First, you can use sort turtles instead of sort-on [who] turtles. It does the same thing, but it's shorter and more idiomatic. Not sure if it's faster, though (it might help a little bit).
The foreach sorted-turtles [ x -> ask x [ ... ] ] pattern should be fast enough. It's the list creation that is costly.
The main thing you can do to improve the speed of your model is thus to store the sorted list of turtles in a global variable and reuse that list instead of re-sorting every time.
That would only work, however, if you're not constantly creating and/or killing turtles, it which case you would have to re-create the sorted list every time.
Related
Picture of my behaviourspace menu
I'm working on an agent based model where a variable (agentvariable1) owned by all agents changes every tick. I want to report a time series for the values of this variable for every agent using Behaviourspace.
However, when I measure runs using the following reporter
[agentvariable1] of turtles
the values that are reported for agentvariable1 are randomly shuffled, because "turtles" calls all turtles in a random order, which is different every tick. Because of this the data that is exported is not usable to create a time-series.
Is it posstible to create a reporter in Behaviourspace that reports the values of the agentvariable1 in a sequence that remains the same every tick?
Using sort on an agentset creates a list of those agents sorting them by some criteria. In the case of turtles, they are sorted by their who which means that their relative order will always be the same.
However you cannot directly do [agentvariable1] of sort turtles, because of expects an agent/agentset but you are giving it a list.
What you can do is creating a global variable as a list: at each tick the list is emptied, and later all turtles (sorted as per sort) will append their value to the list.
That list is what you will report in your Behavior Space.
globals [
all-values
]
turtles-own [
my-value
]
to setup
clear-all
reset-ticks
create-turtles 5
end
to go
set all-values (list)
ask turtles [
set my-value random 10
]
foreach sort turtles [
t ->
ask t [
set all-values lput my-value all-values
]
]
show all-values
tick
end
As an alternative to Matteo's answer (which is perfectly suitable and directly addresses your intention, I just present another option depending on preference) you could also pair the variable of interest with some turtle identifier and report that as a list of lists. This adds a bit of flexibility in cases where the number of turtles increases or decreases. In this example, I use who and xcor for simplicity, but you may want to create your own unique turtle identifier for more explicit tracking. With this toy model:
to setup
ca
crt 5
reset-ticks
end
to go
ask turtles [
rt random 30 - 15
fd 1
]
tick
end
to-report report-who-x
report list who xcor
end
At any point, you can call the list with [report-who-x] of turtles to get a list of lists. With a behaviorspace setup such as:
you get an output that would look something like:
I have one doubt:
Context: I have a code in which, briefly, turtles have an integer variable (energy-collected) and from that, patches update their own variable (energy-of-my-agent), as described in the code snippet below.
Problem: The turtle variable is of type int (-1, for example), but the patch variable is a one-element list ( [-1] ).
Question: Should this happen? Otherwise, how can I make the patch variable just an integer value?
ask turtles
[
set energy-collected (energy - euse)
]
ask patches
[
set energy-of-my-agent [energy-collected] of turtles-here
]
Thanks in advance
The main thing you have to consider is what of reports.
In your case turtles-here is an agentset, not a specific agent.
This is because, although you might have a single turtle on a patch, you may also have multiple turtles on a patch. Therefore turtles-here reports an agentset, even if that agentset may be made of a single turtle.
It follows that a collection of values from an agentset, obtained with of (and [energy-collected] of turtles-here is exactly that), will be a list of values - even if that list contains only one element.
Therefore I would say:
Is your model made in such a way that each patch cannot have more than one turtle at a time? Then you could do:
ask patches [
if any? turtles-here [
set energy-of-my-agent [energy-collected] of one-of turtles-here
]
]
In the code above, one-of turtles-here reports a specific agent - not an agentset anymore.
So its variable's value, obtained with of, will be stored as a single value (provided that the agent's variable is not a list itself, but that's not your case).
Can it happen that your patches have more than one turtle at a time? Then, if you're interested in the single patch holding "its" turtles' values, dealing with lists is probably necessary.
Update
I made a connection between this question and your other one suggesting that you want to use patches as elements of matrices.
Maybe this is useful to your case: if your model allows for the possibility of having more than one turtle on the same patch, you might be interested in doing something like:
ask patches [
set energy-of-my-agent sum [energy-collected] of turtles-here
]
As you can see, sum takes a list as input and reports a number. Each patch will take the sum of all the values of energy-collected by turtles standing there, or you can change the calculation using whatever you want (e.g. mean, max etc).
Actually, you can use this approach regardless: this way, even when you have a single turtle on a patch, sum (or any other function taking a lost and returning a value) will give you a single value where before you had a list of one value.
I am trying to create a code that will allow me to get the who numbers of 10 turtles and store them in a list. However, the "who" number can only be added to the list if it has not been added to the list before.
I have to use the "foreach" command to check if the number has already been added to the list.
Any suggestions on how to do so?
My code:
turtles-own [contact-list]
to setup
clear-all
create-turtles 10 [
set contact-list []
print who
]
reset-ticks
end
to go
ask turtles [
set contact-list lput [who] of one-of other turtles-here contact-list
foreach contact-list[
; this is where the command goes
]
print contact-list
]
end
Might it be easier to add all the who numbers to the list and then use the remove-duplicates primitive to remove the duplicate numbers? I assume that you have a reason for putting who numbers into the list rather than having a list of the agents themselves, or an agentset. In general, who numbers are to be avoided.
in my netlogo code I have a network with companies (that is my breed). I want to ask the companies to share information with their neighbors and their neighbors and so on, this works (see code below, the agentsets are b, c and d).
However when I ask for information on the third level neighbors my agentset also includes the first level neighbors (obviously since it takes all neighbors into acount), so I want to remove these first level neighbors from the third level neighbors agentset. In the code this means I want to remove agents present in D which are also present in B
But I cant find the way to do it, other doesnt work since it is not the agent asking which has to be removed. And remove also doesnt seem to do the job. I also tried != not equal to the first level but this reports a true or false and I just want to remove these agents from the third level agentset so I dont double count them.
ask companies [
let i who
let b link-neighbors
ask b [ let c link-neighbors
ask c [ let d link-neighbors
ask companies with [who = i] [
set iburen [who] of b
set iiburen [who] of other c
set iiiburen [who] of d
]
]
]
]
can somebody help me with this?
I think what you want is the member? primitive. If D and B are agentsets, the following should give you the members of D that are not members of B.
let DminusB D with [not member? self B]
Many things to say here:
Charles' answer is technically correct.
If a and b are agentsets, a with [ not member? self b ] will give you agents from a that are not already in b.
But I think there are better ways to accomplish what you are trying to do. I will come back to that, but first, a general piece of advice:
Don't use who!
The who primitive has some (very few) legitimate usages, but it's mostly evil. It tends to lead to brittle, inefficient code. For example, when you do:
let i who
; ...
ask companies with [who = i] [ ... ]
NetLogo has to scan all companies to find the one with that specific who number.
NetLogo can store agent references directly. Use that instead! For example:
let this-company self
; ...
ask this-company [ ... ]
Especially don't use lists of who numbers!
NetLogo is adequate for manipulating lists, but its awesome for manipulating agentsets. If you do something like this:
set iburen [who] of b
set iiburen [who] of other c
set iiiburen [who] of d
You are forfeiting the power of agentsets. I don't know why you want to store the three different levels separately, but supposing it's OK to store all your neighbors together, you could do:
set my-neighbors other (turtle-set b c d)
The use of other will exclude the original company and turtle-set will make sure that each agent in the set is unique (as agentsets can only contain unique agents anyway).
If you really want three separate variables, use Charles' answer, but make sure to store agentsets, not lists of who numbers!
If you don't need separate variables, however, I think the best solution would be to:
Use nw:turtles-in-radius.
NetLogo's Networks extension has a primitive that does exactly what I think you want to do:
ask companies [ set my-neighbors nw:turtles-in-radius 3 ]
That's it.
In NetLogo: suppose the model has
a turtle (0) of breed A with undirected links with 3 turtles (1, 2 and 3) of breed B;
the turtle 0 has an attribute named "number-of-links" that equals 3.
Now, let one of the 3 neighbors of 0 dies..
How can I program turtle 0 to change its number-of-links automatically to 2?
If all you want is a way of keeping track of the number links, use count my-links instead of a custom variable.
In general, the least bug prone way of having a value update when the number of links changes is to compute that value when you need it. For number of links, this is simply count my-links. For more complicated things, wrap them in a reporter:
to-report energy-of-neighbors
report sum [ energy ] of link-neighbors
end
If this doesn't work for whatever reason (agents need to react to a link disappearing or you're seeing a serious, measurable performance hit from calculating on the fly), you'll have to make the updates yourself when the number of links change. The best way to do this is to encapsulate the behavior in a command:
to update-on-link-change [ link-being-removed ] ;; turtle procedure
; update stuff
end
and then encapsulate the things that can cause the number of links to change (such as turtle death) in commands as well:
to linked-agent-death ;; turtle procedure
ask links [
ask other-end [ update-on-link-change myself ]
]
die
end