关于C#:boost :: random :: discrete_distribution是否可以动态调整大小?

Is boost::random::discrete_distribution dynamically resizable?

我找不到关于Boost_discrete_distribution的大量文档。在Google大量搜索之后,我什至找不到该类具有的方法列表,以及它们是否有功能重新分配这些概率。

就我而言,我正在编写一种进化动力学算法。在每个时间步长,可以随机选择种群成员去死或繁殖。因此,离散分布中的条目总数几乎每次迭代都会更改。

我想有一个在仿真开始之前定义的对象,称为gillespie_dist(控制此Gillespie算法的离散分布)。但是我想在每次迭代结束时潜在地更改特定值和/或向gillespie_dist添加新值,并且特别是不想在每次迭代时都创建离散离散的新实例。

对此有什么好的方法。是否有将新值压入discrete_distribution对象的方法,是否有更改特定索引处的分布值的方法,或者更好的方法是使用上述向量迭代器思想以某种方式"重新初始化"整个分布在这里吗?


我研究了std :: discrete_distribution的gcc libstdc 4.7实现的代码。

权重作为vector<double>存储在私有成员中。在公共接口中无法访问其调整大小方法。

我将尝试找出其operator()的实现(看起来像在cpp中),滚动自己的应该没问题。

这是主要动作,下面是我的解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  template<typename _IntType>
    void
    discrete_distribution<_IntType>::param_type::
    _M_initialize()
    {
      if (_M_prob.size() < 2)
        {
          _M_prob.clear();
          return;
        }

      const double __sum = std::accumulate(_M_prob.begin(),
                                           _M_prob.end(), 0.0);
      // Now normalize the probabilites.
      __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
                          std::bind2nd(std::divides<double>(), __sum));
      // Accumulate partial sums.
      _M_cp.reserve(_M_prob.size());
      std::partial_sum(_M_prob.begin(), _M_prob.end(),
                       std::back_inserter(_M_cp));
      // Make sure the last cumulative probability is one.
      _M_cp[_M_cp.size() - 1] = 1.0;
    }

  template<typename _IntType>
    template<typename _UniformRandomNumberGenerator>
      typename discrete_distribution<_IntType>::result_type
      discrete_distribution<_IntType>::
      operator()(_UniformRandomNumberGenerator& __urng,
                 const param_type& __param)
      {
        if (__param._M_cp.empty())
          return result_type(0);

        __detail::_Adaptor<_UniformRandomNumberGenerator, double>
          __aurng(__urng);

        const double __p = __aurng();
        auto __pos = std::lower_bound(__param._M_cp.begin(),
                                      __param._M_cp.end(), __p);

        return __pos - __param._M_cp.begin();
      }

因此,基本上,它在初始化时计算辅助向量_M_cp,该向量实际上是权重的离散累积密度函数。因此,生成样本仅意味着生成一个统一的随机变量,并在累积分布中搜索该变量的首次出现(这是上面的lower_bound调用),并返回其索引。

例如,如果权重向量为:

1
{ 1, 2, 1, 3 }

则cp的计算公式为:

1
2
3
{ 1, 1+2, 1+2+1, 1+2+1+3 }
=
{ 1, 3, 4, 7 }

所以我统一选择0..6并得到4,所以我选择了第三个。


After much Google searching, I still can't even find a list of methods
that this class has, and whether any of them function to re-assign the
probabilities.

http://www.boost.org/doc/html/boost/random/discrete_distribution.html

1
void param(const param_type & param);

Sets the parameters of the distribution.