加权随机图
假设我有一个大的二维数组,其值范围在 [0,1] 范围内,其中 0 表示“不可能”,1 表示“极有可能”。
如何根据上述概率在该数组中选择一组随机点?
Suppose I have a big 2D array of values in the range [0,1] where 0 means "impossible" and 1 means "highly probable".
How can I select a random set of points in this array according to the probabilities described above ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看待问题的一种方法是(暂时)忽略您正在处理二维网格的事实。你拥有的是一组加权的项目。从这样的集合中随机选择的标准方法是:
s
,0 <= u << s
t
,t >= u
,选择您当前正在查看的物品(您刚刚添加了重量的物品)。可以通过添加以下步骤进行修改,以进行多项选择而无需替换:
每次选择后,从
s
中扣除所选项目的重量(如果您的重量是浮点且稳定性是问题,您可能更愿意从头开始重新计算,至少偶尔)。从 2 开始重复,但在步骤 3 中跳过先前选择的项目。
如果对权重求和不可行或不受欢迎(如果您的数组特别大,则可能是这样),还有其他选择。第一个想到的是拒绝抽样,这是一个相当广泛的主题,所以我只会向您推荐有关该主题的谷歌和维基百科,因为它们的覆盖范围非常好。
编辑:忘记回到你有一个二维数组的事实。您可以通过预先计算(MIPMAP 样式)地图中区域层次结构的权重总和来显着加快速度,这样您就可以快速跳到实际选定权重的位置。
One way to look at the problem is to ignore (for the moment) the fact that you're dealing with a 2d grid. What you have are a set of weighted items. A standard way of randomly selecting from such a set is to:
s
0 <= u < s
t
of the weights of the items you've examinedt >= u
, select the item you're currently looking at (the one whose weight you just added).This can be modified to make multiple selections without replacement by adding the following steps:
After each selection, deduct the weight of the selected item from
s
(if your weights are floating point and stability is an issue, you might prefer to recalculate it from scratch, at least occasionally).repeat from 2, but in step 3 skip items that have been previously selected.
If summing the weights is infeasible or undesirable (which it may be if your array is particularly large) there are other options. The first that comes to mind is rejection sampling, which is a fairly broad topic so I'll just refer you to google and wikipedia on that one, as their coverage is pretty good.
EDIT: Forgot to come back to the fact that you have a 2D array. You can speed things up significantly by pre-computing (MIPMAP-style) the sums of weights of a hierarchy of regions in the map, so you can skip quickly to the location of the actual selected weight.
代码:
如果您需要任何特定语言的版本,请询问我
the code:
if u need it in any specific language just ask me