HPRMutator extends Mutator<G, C> { @Override protected MutatorResult<Chromosome<G>> mutate( final Chromosome<G> chromosome, final double p, final Random random ) { final MutatorResult<Chromosome<G>> result; if (chromosome.length() > 1) { final int P = Probabilities.toInt(p); final int[] points = Combinatorics.subset(chromosome.length(), 2); final MSeq<G> genes = MSeq.of(chromosome); int mutations = (points[1] - points[0] + 1)/2; for (int i = points[0], j = points[1]; i < j; ++i, --j) { genes.swap(i, j); if (random.nextInt() < P) { genes.swap(i, random.nextInt(chromosome.length())); ++mutations; } } result = MutatorResult.of( chromosome.newInstance(genes.toISeq()), mutations ); } else { result = MutatorResult.of(chromosome); } return result; } HPRMutator(final double probability);  HPRMutator();   }
@Test(invocationCount = 10) public void mutate() { final PermutationChromosome<Integer> original = PermutationChromosome.ofInteger(100); final HPRMutator<EnumGene<Integer>, Integer> mutator = new HPRMutator<>(1.0); final MutatorResult<Chromosome<EnumGene<Integer>>> result = mutator.mutate(original, 1, new Random()); final int[] a = original.stream() .mapToInt(EnumGene::allele) .toArray(); final int[] b = result.result().stream() .mapToInt(EnumGene::allele) .toArray(); Assert.assertNotEquals(a, b); }