GaussianMutator extends Mutator<G, C> { @Override protected G mutate(final G gene, final Random random) { return gene.isValid() ? mutate0(gene, random) : gene; } GaussianMutator(final double probability);  GaussianMutator(); @Override String toString();  }
@Test(invocationCount = 20, successPercentage = 95) public void mutate() { final Random random = RandomRegistry.random(); final double min = 0; final double max = 10; final double mean = 5; final double var = Math.pow((max - min)/4.0, 2); final DoubleGene gene = DoubleGene.of(mean, min, max); final GaussianMutator<DoubleGene, Double> mutator = new GaussianMutator<>(); final Histogram<Double> histogram = Histogram.ofDouble(0.0, 10.0, 10); for (int i = 0; i < 10000; ++i) { final double value = mutator.mutate(gene, random).allele(); histogram.accept(value); } final Range<Double> domain = new Range<>(min, max); }
@Test public void mutateValidGene() { final var mutator = new GaussianMutator<DoubleGene, Double>() { public DoubleGene mutate(final DoubleGene gene, final Random random) { return super.mutate(gene, random); } }; final var random = new Random() { @Override public double nextGaussian() { return 1; } }; DoubleGene gene = DoubleGene.of(0.9, 0, 1); Assert.assertTrue(gene.isValid()); gene = mutator.mutate(gene, random); Assert.assertTrue(gene.isValid()); Assert.assertEquals(gene.doubleValue(), Math.nextDown(1.0)); }
@Test public void mutateInvalidGene() { final var mutator = new GaussianMutator<DoubleGene, Double>() { public DoubleGene mutate(final DoubleGene gene, final Random random) { return super.mutate(gene, random); } }; final DoubleGene gene = DoubleGene.of(0.9, 5, 1); Assert.assertFalse(gene.isValid()); final DoubleGene gene1 = mutator.mutate(gene, new Random()); Assert.assertSame(gene1, gene); }