NetLogo,来自列表的加权随机抽取:如何使用rnd-extension? [英] NetLogo, weighted random draw from a list: how to use rnd-extension?
问题描述
我在NetLogo中有一个带有值的列表以及每个值的概率列表.现在,我想根据其概率绘制一个随机值(加权随机绘制).我曾想过使用 Rnd扩展名,但是我无法弄清楚如何正确权重,请帮助
I have a list in NetLogo with values and a list of probabilities for each value. Now I want to draw a random value based on its probability (weighted random draw). I thought of using the Rnd extension, but I cant quite figure out how to get the 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 )
推荐答案
如果您希望/需要使用两个单独的列表来表示您的值和概率,执行此操作的方法是让扩展程序选择一个 index 并使用此索引来访问传递给rnd:weighted-one-of
的报告程序中的概率以及列表中的值(一旦选择).在下面的代码中是example1
.
If you want/need to use two separate lists for your values and probabilities, the way to do it is to have the extension pick an index and use this index to access both the probability in the reporter passed to rnd:weighted-one-of
and, once it is chosen, the value in your list. That's example1
in the code below.
如果您可以将值和概率都放在同一列表中,则扩展名更易于使用.这是通过构建对"列表(即,每个子列表中包含两个项目的列表的列表)来完成的.有了该标记后,您可以在报告器中使用该对的第二个项目(item 1
),并使用该对的第一项(item 0
)设置状态. example2
显示如何执行此操作.
The extension is easier to work with, however, if you can put both your values and your probabilities in the same list. This is done by building a list of "pairs" (i.e., a list of lists with two items in each sublist). When you have that, you can use the second item of the pair (item 1
) in the reporter and set your state using the first item of the pair (item 0
). example2
shows how to do this.
extensions [ rnd ]
to 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
to example2
let pairs [[1 0.2] [2 0.3] [3 0.5]]
let state item 0 rnd:weighted-one-of pairs [ item 1 ? ]
end
正如Seth在评论中提到的那样,您可以使用(map list values probabilities)
从两个单独的列表中构建配对列表.他还提到代码可能会用first
和last
而不是item 0
和item 1
更清晰."
As mentioned by Seth in the comments, you can construct your list of pairs from the two separate lists with (map list values probabilities)
. He also mentions that the code might be "clearer with first
and last
instead of item 0
and item 1
."
example3
整合了两个建议:
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
这篇关于NetLogo,来自列表的加权随机抽取:如何使用rnd-extension?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!