/** * It is an error if the number of seed candidates is greater than the population size. In this * case an exception should be thrown. Not throwing an exception is wrong because it would permit * undetected bugs in programs that use the factory. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testTooManySeedCandidates() { CandidateFactory<String> factory = new StringFactory(alphabet, candidateLength); // The following call should cause an exception since the 3 seed candidates // won't fit into a population of size 2. factory.generateInitialPopulation( 2, Arrays.asList("abcdefgh", "ijklmnop", "qrstuvwx"), FrameworkTestUtils.getRNG()); }
/** * The {@link ListOrderCrossover} operator is only defined to work on populations containing lists * of equal lengths. Any attempt to apply the operation to populations that contain different * length lists should throw an exception. Not throwing an exception should be considered a bug * since it could lead to hard to trace bugs elsewhere. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testDifferentLengthParents() { EvolutionaryOperator<List<Integer>> crossover = new ListOrderCrossover<Integer>(new ConstantGenerator<Probability>(Probability.ONE)); List<List<Integer>> population = new ArrayList<List<Integer>>(2); population.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8)); population.add(Arrays.asList(9, 10, 11)); // This should cause an exception since the parents are different lengths. crossover.apply(population, FrameworkTestUtils.getRNG()); }
/** * Generate a completely random population. Checks to make sure that the correct number of * candidate solutions is generated and that each is valid. */ @Test public void testUnseededPopulation() { CandidateFactory<String> factory = new StringFactory(alphabet, candidateLength); List<String> population = factory.generateInitialPopulation(populationSize, FrameworkTestUtils.getRNG()); assert population.size() == populationSize : "Wrong size population generated: " + population.size(); validatePopulation(population); }
/** * Generate a random population with some seed candidates. Checks to make sure that the correct * number of candidate solutions is generated and that each is valid. */ @Test public void testSeededPopulation() { CandidateFactory<String> factory = new StringFactory(alphabet, candidateLength); String seed1 = "cdefghij"; String seed2 = "bbbbbbbb"; List<String> population = factory.generateInitialPopulation( populationSize, Arrays.asList(seed1, seed2), FrameworkTestUtils.getRNG()); // Check that the seed candidates appear in the generated population. assert population.contains(seed1) : "Population does not contain seed candidate 1."; assert population.contains(seed2) : "Population does not contain seed candidate 2."; validatePopulation(population); }
@Test public void testCrossover() { EvolutionaryOperator<List<Integer>> operator = new ListOrderCrossover<Integer>(); List<Integer> parent1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); List<Integer> parent2 = Arrays.asList(3, 7, 5, 1, 6, 8, 2, 4); List<List<Integer>> population = new ArrayList<List<Integer>>(2); population.add(parent1); population.add(parent2); for (int i = 0; i < 50; i++) // Do several cross-overs to check different cross-over points. { population = operator.apply(population, FrameworkTestUtils.getRNG()); for (List<Integer> offspring : population) { for (int j = 1; j <= 8; j++) { assert offspring.contains(j) : "Evolved candidate missing required element " + j; } } } }