NetLogo, weighted random draw from a list: how to use rnd-extension? -
i have list in netlogo values , list of probabilities each value. want draw random value based on probability (weighted random draw). thought of using rnd extension, cant quite figure out how weights right, please help
set values [1 2 3] set probabilities [0.2 0.3 0.5] set state ( rnd:weighted-one-of agentset reporter-task )
if want/need use 2 separate lists values , probabilities, way have extension pick index , use index access both probability in reporter passed rnd:weighted-one-of
and, once chosen, value in list. that's example1
in code below.
the extension easier work with, however, if can put both values , probabilities in same list. done building list of "pairs" (i.e., list of lists 2 items in each sublist). when have that, can use second item of pair (item 1
) in reporter , set state using first item of pair (item 0
). example2
shows how this.
extensions [ rnd ] example1 let values [1 2 3] let probabilities [0.2 0.3 0.5] let indices n-values length values [ ? ] let index rnd:weighted-one-of indices [ item ? probabilities ] let state item index values end example2 let pairs [[1 0.2] [2 0.3] [3 0.5]] let state item 0 rnd:weighted-one-of pairs [ item 1 ? ] end
edit:
as mentioned seth in comments, can construct list of pairs 2 separate lists (map list values probabilities)
. mentions code might "clearer first
, last
instead of item 0
, item 1
."
example3
integrates both suggestions:
to example3 let values [1 2 3] let probabilities [0.2 0.3 0.5] let pairs (map list values probabilities) let state first rnd:weighted-one-of pairs [ last ? ] end
Comments
Post a Comment