View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.genetics;
18  
19  import java.util.Arrays;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
24  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class OrderedCrossoverTest {
29  
30      @Test
31      public void testCrossover() {
32          final Integer[] p1 = new Integer[] { 8, 4, 7, 3, 6, 2, 5, 1, 9, 0 };
33          final Integer[] p2 = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
34          final DummyListChromosome p1c = new DummyListChromosome(p1);
35          final DummyListChromosome p2c = new DummyListChromosome(p2);
36  
37          final CrossoverPolicy cp = new OrderedCrossover<>();
38  
39          for (int i = 0; i < 20; i++) {
40              final Set<Integer> parentSet1 = new HashSet<>(Arrays.asList(p1));
41              final Set<Integer> parentSet2 = new HashSet<>(Arrays.asList(p2));
42  
43              final ChromosomePair pair = cp.crossover(p1c, p2c);
44  
45              final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]);
46              final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]);
47  
48              Assert.assertNotSame(p1c, pair.getFirst());
49              Assert.assertNotSame(p2c, pair.getSecond());
50  
51              // make sure that the children have exactly the same elements as their parents
52              for (int j = 0; j < c1.length; j++) {
53                  Assert.assertTrue(parentSet1.contains(c1[j]));
54                  parentSet1.remove(c1[j]);
55                  Assert.assertTrue(parentSet2.contains(c2[j]));
56                  parentSet2.remove(c2[j]);
57              }
58          }
59      }
60  
61      @Test(expected = DimensionMismatchException.class)
62      public void testCrossoverDimensionMismatchException() {
63          final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 };
64          final Integer[] p2 = new Integer[] { 0, 1, 1, 0, 1 };
65  
66          final BinaryChromosome p1c = new DummyBinaryChromosome(p1);
67          final BinaryChromosome p2c = new DummyBinaryChromosome(p2);
68  
69          final CrossoverPolicy cp = new OrderedCrossover<>();
70          cp.crossover(p1c, p2c);
71      }
72  
73      @Test(expected = MathIllegalArgumentException.class)
74      public void testCrossoverInvalidFixedLengthChromosomeFirst() {
75          final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 };
76          final BinaryChromosome p1c = new DummyBinaryChromosome(p1);
77          final Chromosome p2c = new Chromosome() {
78              @Override
79              public double fitness() {
80                  // Not important
81                  return 0;
82              }
83          };
84  
85          final CrossoverPolicy cp = new OrderedCrossover<>();
86          cp.crossover(p1c, p2c);
87      }
88  
89      @Test(expected = MathIllegalArgumentException.class)
90      public void testCrossoverInvalidFixedLengthChromosomeSecond() {
91          final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 };
92          final BinaryChromosome p2c = new DummyBinaryChromosome(p1);
93          final Chromosome p1c = new Chromosome() {
94              @Override
95              public double fitness() {
96                  // Not important
97                  return 0;
98              }
99          };
100 
101         final CrossoverPolicy cp = new OrderedCrossover<>();
102         cp.crossover(p1c, p2c);
103     }
104 }