C++.加权标准::随机播放

C++. Weighted std::shuffle

本文关键字:随机 播放 标准 加权 C++      更新时间:2023-10-16

有没有办法使用标准库进行漂亮而优雅的加权洗牌? 有std::discrete_distribution. 我想要的是这样的东西:

std::vector<T> data { N elements };
std::vector<int> weights { N weights };
std::shuffle(std::begin(data), std::end(data), something based on discrete distribution);

如果 OP 意图是随机排列项目列表r

这样,给定一个权重列表w,权重为 w[i] 的元素 a[i] 应该是概率为 w[i]/sum(w(的随机洗牌r的第一个元素。

正如Severin Pappadeux链接的页面所述:

加权随机洗牌与从列表 a 中加权随机抽样相同,无需替换。也就是说,从 a 中选择概率 w[i]/sum(w( 元素 a[i]。将此元素存储在列表 r 中。然后,从 a 中删除元素 a[i],从 w 中删除元素 w[i],并选择修改后的列表 a 的新元素,依此类推,直到 a 为空。

我不知道标准库中有这样的算法,但一个简单的实现可能是:

#include <random>
#include <algorithm>
#include <iterator>
template <class D, class W, class URBG>
void weighted_shuffle
( D first, D last
, W first_weight, W last_weight
, URBG&& g )
{
while (first != last and first_weight != last_weight)
{
std::discrete_distribution dd(first_weight, last_weight);
auto i = dd(g);
if ( i )
{
std::iter_swap(first, std::next(first, i));
std::iter_swap(first_weight, std::next(first_weight, i));
}
++first;
++first_weight;
}
}

活生生的例子 这里.

查看加权随机抽样(2005;埃夫莱米迪斯,斯皮拉基斯(。 如果您创建一个值列表-pow(rand(0,1), weights[i])并对此进行排序,您将获得所需的内容。

等效地(并且速度稍快(,可以使用指数分布创建此列表。

std::vector<size_t> weighted_shuffle(std::vector<double> const &weights, std::mt19937 &rng)
{
//auto uniform_dist = std::uniform_real_distribution<double>();
auto exp_dist = std::exponential_distribution<double>();
std::vector<std::pair<double, size_t>> index_pairs;
index_pairs.reserve(weights.size());
for (size_t i=0; i<weights.size(); ++i) {
double const p = weights[i];
// from Efraimidis, Spirakis
//index_pairs.emplace_back(-std::pow(uniform_dist(rng), 1.0/p), i);
// equivalent and a bit faster
//index_pairs.emplace_back(-std::log(uniform_dist(rng))/p, i);
// equivalent and fastest
index_pairs.emplace_back(exp_dist(rng)/p, i);
}
std::sort(index_pairs.begin(), index_pairs.end());
std::vector<size_t> indices;
indices.reserve(weights.size());
for (auto const &[w, i] : index_pairs)
indices.emplace_back(i);
return indices;
}