예제 #1
0
 /**
  * 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());
 }
예제 #2
0
  /**
   * 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);
  }
예제 #3
0
 @Test
 public void testMaxDepth() {
   final int maxDepth = 3;
   CandidateFactory<Node> factory =
       new TreeFactory(2, maxDepth, new Probability(0.6), Probability.EVENS);
   List<Node> trees = factory.generateInitialPopulation(20, ExamplesTestUtils.getRNG());
   for (Node tree : trees) {
     // Make sure that each tree is no bigger than the maximum permitted.
     assert tree.getDepth() <= maxDepth : "Generated tree is too deep: " + tree.getDepth();
   }
 }
예제 #4
0
  /**
   * 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);
  }