我的应用程序使用遗传算法来进化神经网络.在开发(随机)突变算法的单元测试时,我希望确保对于给定的随机值,发生正确的突变.
在问题here中,涉及随机性的单元测试算法问题的最流行的答案是使用模拟.这对我来说似乎很明智,但我的问题是,是否有人对如何做到这一点有坚实的建议.
以下是我目前如何实现这一目标.我为随机性生成器定义了一个接口(这里为了说明目的而高度简化):
public interface Mutator {
float randomSynapseWeightChange();
float randomSynapseThresholdChange();
}
In the main application this interface has an implementation that wraps `Random`. In my unit testing framework I use:
public class TestMutator implements Mutator {
List synapseWeightChanges = new ArrayList<>();
public void addTestSynapseWeightChange(float weightChange) {
synapseWeightChanges.add(weightChange);
}
public float randomSynapseWeightChange() {
return synapseWeightChanges.remove();
}
}
我的单元测试然后看起来像:
@Test
public void testDecreas