How to measure a set of attributes of individual agents in Behaviour Space? - export-to-csv

So far, all the experiments I have run using Behaviour Space have been to either record global variables or the average values of agent variables across different agentsets.
However, I need to measure a set of attributes (violence, entitativity, homogeneity, size) of individual emergent agents (extremist groups) at two/three different timesteps each run. I also need to do this across a number of different model scenarios and ideally have it all summarised in the same spreadsheet. The aim is to plot the relationship between each agent attributes, with individual agents as my cases.
I can't seem to workout if this is possible using Behaviour Space. I have tried using e.g. [violence] of groups as a reporter in Behaviour Space, but the output is then a single string variable that I can't do anything with. I've also thought about using the export-world primitive but, from what I understand, this will overwrite the file every time it is executed or create separate files each time.

There may be a more elegant way to do this but the following should work.
Create global variables, say v0, v1, v2 ...,vn for the individual violence within group n.
Set these at each tick. Report them separately in Behavior space.
Example:
globals [ mass-violence v0 v1 v2]
turtles-own [ violence]
to setup
clear-all
create-turtles 3 [ setxy random-xcor random-ycor set violence 0 ]
reset-ticks
end
to go
set mass-violence 0
if ( ticks > 4 ) [ stop ]
ask turtles [ set violence random 100 set mass-violence mass-violence + violence]
set v0 [violence] of turtle 0
set v1 [violence] of turtle 1
set v2 [violence] of turtle 2
print (word mass-violence " " v0 " " v1 " " v2 )
tick
end
Or you could parse the string you ended up with in Excel using Excel commands to pull selected items out of the string and put them into separate columns. That would end you up in the same place. For example, 5 runs of the above code produces this:

Related

How to report agent variables in a consistent order in Netlogo's Behaviourspace

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:

Reporting a list in behaviorspace Netlogo

I want to capture the agent set of agent variable X in behaviorspace.
to-report MeanOfX
report mean [X] of turtles
end
to-report AgentSetOfX
report [X] of turtles
end
After I run the experiment I am not getting the same mean of my agent set, and also weird numbers. Is this the right way to do it?
Yes, those reporters will report the mean value of X for your turtles and a LIST of the X VALUES for the turtles, like "[ 1 34 3 4 8 92 ]" It will not return an "agent-set" containing a SET of TURTLES themselves despite your name. Did you want such an agent-set for some reason?
If you are using any randomization at all anywhere there is no reason the means should come out the same for different runs. If you need the randomization-generated X values to be identical across runs you should set "random-seed" to some value in your setup.
if the values of X are "weird numbers" you need to debug using some logic or printing out the X values as you generate them to see where they deviate from not being weird.

How to change a turtle's attribute if one of its links disappear?

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

How to avoid individual patch updates

I am modeling diffusion in my model, but I think I am getting a calculation artifact due to NetLogo sequentially updating individual patches. I will not be using the diffuse command (due to inaccurate diffusion). However, much like how this command works, I would like to update all the calculations of the patches simultaneously, rather than sequentially. I have a slight recollection of seeing some sample code that used values at the beginning of the tick, however I canĀ“t seem to find it now.
Specifically, I need help programming a way to store patch values at the turn of each tick, and then carry out a simultaneous calculation based on these stored values.
Great question. As you indicate, basically you want to calculate the new value of the variable in one ask block, but store it in a separate variable, and then update the actual value of the variable in a second ask block, like so:
turtles-own [
value
new-value
]
...
to go
ask patches [
;; Change this line to however you want the diffusion to work
set new-value 0.5 * value + sum [ 0.5 * value / 4 ] of neighbors4
]
ask patches [
set value new-value
]
end
This way all patches calculate their updated values from the same information, and then actually update the values themselves simultaneously.

NetLogo - compare single agent against many agents (expected input not list)

I am first time poster, six month reader. I love this site and am grateful for the vast array of topics covered. Now that I am feeling a bit more competent using NetLogo, I've tried some harder stuff and got stuck...
Basically, I have created a membership function which measures agents against one another on a vector containing two variables (opinions on rock and hip-hop):
to-report membership [ agent1 agent2 ]
let w 0.5
let w2 sq w
report exp (- d2 agent1 agent2 / w2)
end
where
;;;;;;;;;;;;;;Shortcut functions;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report d2 [agent1 agent2 ]
report ( (sq ([rock] of agent1 - [rock] of agent2)) + (sq ([hip-hop] of agent1 - [hip-hop] of agent2)) )
end
to-report sq [ x ]
report x * x
end
This all works fine, and I am able to compare any two agents without problem.
However, my trouble arises when I try to compare a single agent [agent1] with all of the agents within his neighbourhood.
to go
ask turtles [
let neighbours turtle-set turtles in-radius neighbourhood
show membership self neighbours]
end
Whenever I run this model I receive an error that the d2 reporter expected an input not a list - which I theoretically understand - by having a neighbourhood of 1+ agent(s), the calculation is receiving for example [0.1 0.8] [0.2 0.4] [0.5 0.6]..............
I was just wondering, is there any way that the procedure can consider all of the neighbours and arrive at one single membership number? I have searched extensively through posts and a couple of netlogo books I have, but no luck so far. Thank you for taking the time to read this post and for any helpful comments.
Your understanding of what is happening is correct: your membership reporter expects two individual agents and you are passing it an agent and an agentset. To calculate each membership individually, and get back a list of membership values, you can use of:
to go
ask turtles [
let neighbours turtle-set turtles in-radius 10
show [ membership myself self ] of neighbours
]
end
Notice the use of myself and self, which can sometimes be tricky to understand. In this case, self is the neighbour and myself is the outer asking turtle.
So now you have a list of membership numbers, but you wonder:
is there any way that the procedure can consider all of the neighbours and arrive at one single membership number?
There are plenty of ways! But we can't really tell you which one to use: it depends on your model and what you want to do with it.
If you wanted something very straightforward, you could just take the mean of the list:
show mean [ membership myself self ] of neighbours
...but I don't know if it makes sense in your context. In any case, NetLogo has plenty of
mathematical primitives that you should be able to use to arrive at the number you want.