UniformCrossover 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); } UniformCrossover(final double ratio); double getRatio(); @SuppressWarnings("unchecked") ChromosomePair crossover(final Chromosome first, final Chromosome second);  }
@Test public void testCrossover() { performCrossover(0.5); performCrossover(0.7); performCrossover(0.2); }
@Test(expected = DimensionMismatchException.class) public void testCrossoverDimensionMismatchException(){ @SuppressWarnings("boxing") final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; @SuppressWarnings("boxing") 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 UniformCrossover<Integer>(0.5d); cp.crossover(p1c, p2c); }
@Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeFirst() { @SuppressWarnings("boxing") 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 UniformCrossover<Integer>(0.5d); cp.crossover(p1c, p2c); }
@Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeSecond() { @SuppressWarnings("boxing") 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 UniformCrossover<Integer>(0.5d); cp.crossover(p1c, p2c); }