I want to calculate and store two certain quantities associated with turtles’ properties, namely count how many turtles meet a condition and the sum of these turtles’ specific attribute. That specific attribute takes discrete, integer, non-sequential values.
A minimal example code of the model would look like this:
globals [ N-x1 N-x2, N-x5 sum-A-x1 sum-A-x2 sum-A-x5 x-list ]
turtles-own [ condition1? A x ]
set x-list ( list 1 2 5 )
Given that in the actual model the x-list
takes 88 discrete values I thought using a ‘foreach’ loop would be an appropriate way to handle this. Note that the number of turtles in the actual model is around 75k so efficiency matters.
Following a combination from previous answers (here and here), I came up with the following solution:
foreach x-list
[ x ->
; Count turtles by x
let temp1 (word "set N-x" x " count turtles with [ condition1? = true and x = " x "]" )
run temp1
; Calculate sum of A by x
let temp2 (word "set sum-A-x" x " sum [ A ] of turtles with [ condition1? = true and x = " x "]" )
; Capture errors due to empty list in calculating the sum
carefully [ run temp2 ]
[ let temp3 (word "set sum-A-x" x " 0" )
run temp3
]
]
Although it works, each run of the model was substantially slowed down. A simple (yet crude) solution that I thought it would help to speed up the code is to first define the turtle agentset on which the subsequent commands (counting and summation) will be performed. Since I could not find a way to use let
inside the ‘foreach’ loop to do that, I went with the manual solution of defining everything by hand (for every single of the 88 values of x
):
; Define needed agentsets
let turtles-x1 with [ condition1? = true and x = 1 ]
let turtles-x2 with [ condition1? = true and x = 2 ]
let turtles-x5 with [ condition1? = true and x = 5 ]
; Count turtles meeting condition1
set N-x1 count turtles-x1
set N-x2 count turtles-x2
set N-x5 count turtles-x5
; Sum A over turtles meeting condition1
if N-x1 > 0 [ set sum-A-x1 sum [ A ] of turtles-x1 ]
if N-x2 > 0 [ set sum-A-x2 sum [ A ] of turtles-x2 ]
if N-x5 > 0 [ set sum-A-x5 sum [ A ] of turtles-x5 ]
That seemed to improve efficiency by a small margin. Could anyone think of a more efficient (i.e. smarter) way to do the same? E.g. using map
, which I can’t my head around of?
G. Papad. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.