The deal is this:
I have three types of turtles.
Every type of turtles contain three turtles
Each turtle has three variable with its own value
How could i ask netlogo to get the average value of each group of turtles
I think you mean something like
let type1-average-x mean [x] of turtles with [type = "1"]
You'll want to do this for each type and for each variable that you'd like the measure of.
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 want to add the values of a variable ( let's say transportation cost) for specific turtles which are in a group. The transportation cost depends on the distance between a specific patch and the location assigned to the turtle. As the value of the said cost is different for each turtle, I want to sum the total cost for turtle which are in a group. To clarify, let's suppose there are 7 turtles in total and only 4 are in a group.
The values of transportation cost of each turtle is assigned as tcost.
to calculate-ttcost
set ttcost 0
let cnt 0
ask turtles [
if in-group? [
set ttcost (tcost + tcost)
set cnt cnt + 1
]
]
end
With the correction of one typo, the code you have should work, assuming that tcost is declared as a turtles-own variable with that turtle's transportation cost (or a reporter that gives the transportation cost for the turtle that calls it), and assuming that those turtles that are in the group in which you are interested have their turtles-own variable in-group? set to true. The typo is in the line
set ttcost (tcost + tcost)
which should be
set ttcost (ttcost + tcost)
But there is a more straight-forward coding that will accomplish the same task.
let cnt count turtles with [in-group?]
let ttcost sum [tcost] of turtles with [in-group?]
with limits the set of turtles to those for which in-group is true. of creates a list of the values of tcost for each of those turtles, which can then be summed up.
If I have an agentset called zoo belonging to turtle 1, how do I count all the breed of camels in the zoo? NetLogo has a breed-on command, but not a breed-in. So this won't work, although it will illustrate what I'd like:
ask turtle 1 [let c count camels-in zoo]
Thanks.
Best guess is you want something like:
let c count ([zoo] of turtle 1) with [breed = camels]
But an example of a zoo turtleset would be very helpful in understanding what you are trying to do. Perhaps also what your turtles are representing?
I have 2 agent types, boys and girls.
breed [boys boy]
breed [girls girl]
Each turtle has an age from a dataset. Also when an agent is a boy, its boy? is true, and if it is a girl, girl? is true.
turtles-own [
age
boy?
girl?
]
They are connected by some random links. Now I want for each boy, I can access its girl neighbors, and the difference between their ages gets calculated. In other words, the age difference of two different breeds. I wrote this, but it does not work.
ask boys [
ask link-neighbors with [girls? = true]
[
set Gage age]
let H abs(item 0 age - item 0 Gage)
]
Edit When I use ask link-neighbors with [girls? = true]the neighbors are considered all together, while I want them to one by one be considered where I can compare their age difference and base on that do some other stuff.
Any suggestions?
Thanks
This is untested, but I hope it's close enough to get you there if it's not correct.
First, you have some confusion with your breeds and turtles-own sex indicator. It would be much easier to have one or the other. Scrap your turtles-own statement entirely and simply test the breed because then you can't introduce errors where (for example) you have have the flag (girl? or boy?) inconsistent with the breed, or both set to TRUE or whatever. The way you have it set up, it is possible to have a turtle of breed boy but accidentally set its variable boy? to FALSE. There is no need for these variables at all, breed is an automatic variable (like who number or size that is created with the turtle) and you can test on the breed directly.
Getting to your actual error, you are asking the link-neighbors to set their variable Gage rather than setting the value of the original turtle that is doing the asking (that is, the turtle that is the centre of this ego network).
UPDATED from the comments, you want the boy to have a list (called age-diff below) of the difference in age between his own and all the girls he is linked to. The primitive map is used to substract a constant from a list, and asking for the variable values of and agentset constructs the list of those values.
boys-own [age-diff]
ask boys
[ let my-girls link-neighbors with [breed = girls]
if any? my-girls
[ set age-diff map [ x -> abs (x - age) [age] of my-girls ] ]
]