我在我的遗传算法
项目中编写了以下函数来实现一种突变(蠕变
)。由于我使用了 java 的内置随机生成库,因此获得每个 index
的概率是统一的。我被要求修改函数,使其使用二项分布而不是均匀分布。就我在谷歌上搜索而言,我找不到任何演示将统一转换为二项式的示例/教程。如何实现?
int mutationRate = 0.001;
public void mutate_creep() {
if (random.nextDouble() <= mutationRate) {
// uniform random generation
int index = random.nextInt(chromoLen);
if(index%2 == 0) { // even index
chromo[index] += 1;
} else { // odd index
chromo[index] -= 1;
}
}
}
注意:我已经在 A efficient binomial random number generator code in Java 看到了解决方案.由于我这里的问题是特定于 creep mutation algorithm
的,所以我不确定如何直接应用它。
最佳答案
根据 Wikipedia ,你这样做:
One way to generate random samples from a binomial distribution is to use an inversion algorithm. To do so, one must calculate the probability that P(X=k) for all values k from 0 through n. (These probabilities should sum to a value close to one, in order to encompass the entire sample space.) Then by using a pseudorandom number generator to generate samples uniformly between 0 and 1, one can transform the calculated samples U[0,1] into discrete numbers by using the probabilities calculated in step one.
我将留给您“计算从 0 到 n 的所有值 k 的概率 [...]”。之后就是加权分布了。
您可以使用 TreeMap
来做到这一点,类似于我在 this answer 中展示的方式.
关于java - 将均匀随机生成转换为二项式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943898/