问题
I'm trying to calculate the variance of a turtle owned factor on a single patch. In other words at a single patch I'd like to know the mean and variance of the factor among all turtles on that patch.
I know 'mean [FACTOR] of turtles-here' will give me the mean, but for some reason variance isn't working as well. Question 1: What is the proper syntax for asking a patch to output the variance of a turtle-owned factor?
I've worked up a super simple example model. Just paste this in for your code, then create 'setup' and 'go' buttons on the interface. It should have pink turtles seeking and stopping on black patches.
turtles-own
[FACTOR]
patches-own
[DEPTH]
to setup
clear-all
reset-ticks
make_patches
make_turtles
end
to go
move
if count (turtles with [DEPTH > 0]) = 0 [stop]
end
to make_patches
ask patches [set depth 20 set pcolor green - 2]
ask n-of 5 patches [set depth -50 set pcolor black]
end
to make_turtles
create-turtles 10
ask turtles
[
set color pink
set size 2
set xcor random max-pxcor
set ycor random max-pycor
set FACTOR random 100
]
end
to move
ask turtles[
let D min [DEPTH] of patches in-radius 3
let Dn min-one-of patches in-radius 3 [DEPTH]
let LDe [DEPTH] of patch-here
ifelse DEPTH < 0
[
move-to patch-here
stop
]
[ifelse LDE > D AND D < 0
[
move-to DN
stop
]
[
right random-float 150
forward random 3
]
]
]
end
In the end I'd like to do a behavior-space experiment where at the end of each run every patch with DEPTH< 0 calculates and outputs the mean, standard deviation, and variance of FACTOR for turtles on that exact patch. My plan is to create a list of the sort
ask patches with [DEPTH< 0] [set FACTOR_LIST (list ("[")(COORDINATES) (",") (VARIANCE_FACTOR) ("]") )]
where FACTOR_LIST is the exported list, COORDINATES is a list consisting of the x and y coordinates of the patch, and VARIANCE_FACTOR is the variance of the factor (which I'm asking how to do here). Question 2: Is there a more efficient way to get this list?
Thanks much!
回答1:
writing the question led me to the answer to the first one at least.
I changed:
patches-own
[
DEPTH
DENSITY
FACTOR_VARIANCE
FACTOR_LIST
]
to go
move
if count (turtles with [DEPTH > 0]) = 0
[
final_tick
ask patches with [DEPTH < 0] [show FACTOR_LIST]
stop
]
end
and added a new procedure that calculated the variance and put it in a list I can call from the patch
to final_tick
ask patches with [DEPTH < 0] [set DENSITY count turtles-here]
ask patches with [DEPTH < 0 AND DENSITY <= 1] [set FACTOR_VARIANCE 0]
ask patches with [DEPTH < 0 AND DENSITY > 1] [set FACTOR_VARIANCE (variance [FACTOR] of turtles-here)]
ask patches with [DEPTH < 0] [set FACTOR_LIST (list (pxcor)(",")(pycor)(",")(FACTOR_VARIANCE))]
end
Would still like to know if anyone has a more efficient way of accomplishing this goal.
来源:https://stackoverflow.com/questions/40730974/calculating-variance-of-a-turtle-owned-factor-on-a-single-patch-in-netlogo