OnePointCrossover implements CrossoverPolicy { @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (! (first instanceof AbstractListChromosome<?> && second instanceof AbstractListChromosome<?>)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return crossover((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); }  @SuppressWarnings("unchecked") // OK because of instanceof checks ChromosomePair crossover(final Chromosome first, final Chromosome second);  }
@Test public void testCrossover() { @SuppressWarnings("boxing") Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; @SuppressWarnings("boxing") Integer[] p2 = new Integer[] {0,1,1,0,1,0,1,1,1}; BinaryChromosome p1c = new DummyBinaryChromosome(p1); BinaryChromosome p2c = new DummyBinaryChromosome(p2); OnePointCrossover<Integer> opc = new OnePointCrossover<Integer>(); for (int i=0; i<20; i++) { ChromosomePair pair = opc.crossover(p1c,p2c); Integer[] c1 = new Integer[p1.length]; Integer[] c2 = new Integer[p2.length]; c1 = ((BinaryChromosome) pair.getFirst()).getRepresentation().toArray(c1); c2 = ((BinaryChromosome) pair.getSecond()).getRepresentation().toArray(c2); Assert.assertEquals(p1[0], c1[0]); Assert.assertEquals(p2[0], c2[0]); Assert.assertEquals(p1[p1.length-1], c1[c1.length-1]); Assert.assertEquals(p2[p2.length-1], c2[c2.length-1]); Assert.assertEquals(p1[2], c1[2]); Assert.assertEquals(p2[2], c2[2]); Assert.assertEquals(p1[3], c1[3]); Assert.assertEquals(p2[3], c2[3]); Assert.assertEquals(p1[7], c1[7]); Assert.assertEquals(p2[7], c2[7]); } }