NPointCrossover 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 mate((AbstractListChromosome<T>) first, (AbstractListChromosome<T>) second); } NPointCrossover(final int crossoverPoints); int getCrossoverPoints(); @SuppressWarnings("unchecked") // OK because of instanceof checks ChromosomePair crossover(final Chromosome first, final Chromosome second);  }
@Test(expected = DimensionMismatchException.class) public void testCrossoverDimensionMismatchException() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final Integer[] p2 = new Integer[] {0,1,1,0,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final BinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new NPointCrossover<Integer>(1); cp.crossover(p1c,p2c); }
@Test(expected = NumberIsTooLargeException.class) public void testNumberIsTooLargeException() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final Integer[] p2 = new Integer[] {0,1,1,0,1,0,1,1,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final BinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new NPointCrossover<Integer>(15); cp.crossover(p1c,p2c); }
@Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeFirst() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final Chromosome p2c = new Chromosome() { public double fitness() { return 0; } }; final CrossoverPolicy cp = new NPointCrossover<Integer>(1); cp.crossover(p1c,p2c); }
@Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeSecond() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final BinaryChromosome p2c = new DummyBinaryChromosome(p1); final Chromosome p1c = new Chromosome() { public double fitness() { return 0; } }; final CrossoverPolicy cp = new NPointCrossover<Integer>(1); cp.crossover(p1c,p2c); }
@Test public void testCrossover() { Integer[] p1 = new Integer[] {1,0,1,0,1,0,1,0,1}; Integer[] p2 = new Integer[] {0,1,0,1,0,1,0,1,0}; BinaryChromosome p1c = new DummyBinaryChromosome(p1); BinaryChromosome p2c = new DummyBinaryChromosome(p2); final int order = 3; NPointCrossover<Integer> npc = new NPointCrossover<Integer>(order); for (int i=0; i<20; i++) { ChromosomePair pair = npc.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(order, detectCrossoverPoints(p1c, p2c, (BinaryChromosome) pair.getFirst())); Assert.assertEquals(order, detectCrossoverPoints(p2c, p1c, (BinaryChromosome) pair.getSecond())); } }