Reporting a list in behaviorspace Netlogo - 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.

Related

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

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:

How to create condition in NetLogo that 2 slider values cannot exceed defined value?

I created a model which as 2 different sliders, namely ratio1 and ratio2. They are located on the interface and their values should add up to 1 (here: labour-unit), and also cannot exceed this value. For now, NetLogo let's me exceed the condition.
I tried this:
to setup
create-turtles number-of-turtles ;; number of firms to be defined through slider
set labour-unit ratio1 + ratio2
set labour-unit 1
end
Therefore, my question is: How to create a condition in the setup that 2 slider values cannot exceed a defined value?
Is there any reason you actually need two sliders if the values always add to 1? Could you just have one slider called "proportion with labor-type x" or whatever you're modelling? Then, you can just have reporters to return the values for the actual proportion you're after- for example:
to-report ratio1
report proportion-slider
end
to-report ratio2
report precision ( 1 - proportion-slider ) 2
end
Then on your interface you could have the slider (and monitors if needed):

NetLogo - Random setup - Random value to each patch - Error

As part of the setup procedure I am trying to use a slider to set the density of patches which will be displayed and assigned a random value. The slider on the interface for density ranges 0 to 100 and the random value of the patch is set using an input on the interface. This will normally be set in the region go 4. So, if 50% is set the the procedure will assign 50% of the patches with a random value.
When i do i get the following error: "Expected command" and the variable 'error-count' is highlighted in the code.
;; The density of patches to be set with a random value is set using variable init-errors on interface.
;; Every patch uses a task which reports a random value.
;; The random value is set using variable error-count on interface
to setup-random
ask patches [
if (random-float 100.0) < init-errors
[setup task random error-count]
]
end
You need to change:
setup task random error-count
to
setup task [ random error-count ]
Anytime the task body isn't just a single primitive (the "concise task syntax"), it needs to be surrounded by square brackets. The error message you're getting happens because task random is valid syntax; NetLogo interprets it as short for task [ random ? ]. Then it doesn't know what to do with the following error-count, hence the error.
I am not entirely clear on the task syntax you are using (in particular, why setup is at the beginning of that line) or why you want to use a task here - but that could be a difference in coding style. But the NetLogo compiler is letting you know that it expected a command after the word random but got a value. Since random does expect a value after, I think it's something to do with the setup and task elements. Here's an alternative way of doing what you want, which might be easier. I have also turned the non-zero error patches green so you can see them. This code assumes you have the two sliders you defined in your problem description.
patches-own [errors]
to setup-random
ask patches [
if (random-float 100.0) < init-errors
[ set errors random error-count
set pcolor green ]
]
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.