MultiPointCrossover extends Crossover<G, C> { @Override protected int crossover(final MSeq<G> that, final MSeq<G> other) { assert that.length() == other.length(); final int n = min(that.length(), other.length()); final int k = min(n, _n); final Random random = RandomRegistry.random(); final int[] points = k > 0 ? Combinatorics.subset(n, k, random) : new int[0]; crossover(that, other, points); return 2; } MultiPointCrossover(final double probability, final int n);  MultiPointCrossover(final double probability);  MultiPointCrossover(final int n);  MultiPointCrossover(); int crossoverPointCount(); @Override String toString();  }
@Test(dataProvider = "crossoverParameters") public void crossover( final String stringA, final String stringB, final Seq<Integer> points, final String expectedA, final String expectedB ) { final ISeq<Character> a = CharSeq.toISeq(stringA); final ISeq<Character> b = CharSeq.toISeq(stringB); final MSeq<Character> ma = a.copy(); final MSeq<Character> mb = b.copy(); final int[] intPoints = points.stream() .mapToInt(Integer::intValue) .toArray(); MultiPointCrossover.crossover(ma, mb, intPoints); Assert.assertEquals(toString(ma), expectedA); Assert.assertEquals(toString(mb), expectedB); }
@Test public void singlePointCrossoverConsistency() { final ISeq<Character> a = CharSeq.toISeq("1234567890"); final ISeq<Character> b = CharSeq.toISeq("ABCDEFGHIJ"); for (int i = 0; i < a.length() + 1; ++i) { final MSeq<Character> ma1 = a.copy(); final MSeq<Character> mb1 = b.copy(); final MSeq<Character> ma2 = a.copy(); final MSeq<Character> mb2 = b.copy(); MultiPointCrossover.crossover(ma1, mb1, new int[]{i}); SinglePointCrossover.crossover(ma2, mb2, i); Assert.assertEquals(ma1, ma2); Assert.assertEquals(mb1, mb2); } }
@Test(dataProvider = "numberOfCrossoverPoints") public void reverseCrossover(final Integer npoints) { for (int i = 1; i < 500; ++i) { final CharSeq chars = CharSeq.of("a-zA-Z"); final ISeq<Character> a = ISeq.of(CharacterChromosome.of(chars, i)) .map(CharacterGene::allele); final ISeq<Character> b = ISeq.of(CharacterChromosome.of(chars, i)) .map(CharacterGene::allele); final MSeq<Character> ma1 = a.copy(); final MSeq<Character> mb1 = b.copy(); final int[] points = Combinatorics.subset( a.length() + 1, min(npoints, a.length() + 1), new Random(1234) ); MultiPointCrossover.crossover(ma1, mb1, points); MultiPointCrossover.crossover(ma1, mb1, points); Assert.assertEquals(ma1, a); Assert.assertEquals(mb1, b); } }
@Test public void crossoverAll1() { final CharSeq chars = CharSeq.of("a-zA-Z"); final ISeq<CharacterGene> g1 = ISeq.of(CharacterChromosome.of(chars, 20)); final ISeq<CharacterGene> g2 = ISeq.of(CharacterChromosome.of(chars, 20)); final MultiPointCrossover<CharacterGene, Double> crossover = new MultiPointCrossover<>(2000); final int[] points = new int[g1.length()]; for (int i = 0; i < points.length; ++i) { points[i] = i; } final MSeq<CharacterGene> ms1 = g1.copy(); final MSeq<CharacterGene> ms2 = g2.copy(); crossover.crossover(ms1, ms2); }