I'm creating clusters of agents using the nw extension weak-component-clusters. It produces a list of agentsets.
My first goal is to calculate the average xcor and ycor of each of those agentsets in the list.
I can use map to count
the number of agents in each agentset, but I can't map mean [xcor]
Example:
clear-all
create-turtles 5
ask turtle 0 [ create-link-with turtle 1 ]
ask turtle 0 [ create-link-with turtle 2 ]
ask turtle 3 [ create-link-with turtle 4 ]
let clusters nw:weak-component-clusters ; create list of agentsets
; output: [(agentset, 2 turtles) (agentset, 3 turtles)]
map count clusters ; Works
;output: [2 3]
map mean [xcor] clusters ; Does not work
;output: Expected a literal value
Secondary question: I will be calculating the distance between the clusters next and I was wondering if there was an extension or function that I could use instead of just using the distance between two points formula.
Thanks!
CodePudding user response:
The problem is in the usage of your map
function. As long as you are doing a very simple calculation (like for example your counting) map function list
is sufficient. But when you want to calculate your mean xcor
, you will need to add some anonymous procedure syntax.
map [ list-item -> function list-item ] list
In this case, you define a variable name that will be used for all the items of the list. This variable name is on the left of the ->
. On the right side of the ->
, you add a function utilising that variable. This function will then be applied to every item of the initial list, as you already know from map
.
In your case, that gives us:
map [a-cluster -> mean [xcor] of a-cluster] clusters
Standard netlogo has a function for calculating the distance between agents (distance
) and the distance between an agent and a point (distancexy
) but no function for a distance between points. That said, the mathematical formula is simple so you could easily write your own function for that using to-report