RandomKeyMutation implements MutationPolicy { public Chromosome mutate(final Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof RandomKey<?>)) { throw new MathIllegalArgumentException(LocalizedFormats.RANDOMKEY_MUTATION_WRONG_CLASS, original.getClass().getSimpleName()); } RandomKey<?> originalRk = (RandomKey<?>) original; List<Double> repr = originalRk.getRepresentation(); int rInd = GeneticAlgorithm.getRandomGenerator().nextInt(repr.size()); List<Double> newRepr = new ArrayList<Double> (repr); newRepr.set(rInd, GeneticAlgorithm.getRandomGenerator().nextDouble()); return originalRk.newFixedLengthChromosome(newRepr); }  Chromosome mutate(final Chromosome original);  }
@Test public void testMutate() { MutationPolicy mutation = new RandomKeyMutation(); int l=10; for (int i=0; i<20; i++) { DummyRandomKey origRk = new DummyRandomKey(RandomKey.randomPermutation(l)); Chromosome mutated = mutation.mutate(origRk); DummyRandomKey mutatedRk = (DummyRandomKey) mutated; int changes = 0; for (int j=0; j<origRk.getLength(); j++) { if (origRk.getRepresentation().get(j) != mutatedRk.getRepresentation().get(j)) { changes++; } } Assert.assertEquals(1,changes); } }