View Javadoc
1   /**
2    * @(#)PartiallyMatchedCrossoverTest.java
3    * Copyright (C) 2008-2010 delhezi.com
4    *
5    * This class is released under the:
6    * GNU Lesser General Public License (LGPL) version 3 or later.
7    * http://www.gnu.org/copyleft/lesser.html
8    */
9   package com.delhezi.ga.crossover.permutation;
10  
11  import com.delhezi.ga.Chromosome;
12  import com.delhezi.ga.ChromosomeProperties;
13  import java.util.ArrayList;
14  import org.junit.Test;
15  import static org.junit.Assert.*;
16  
17  public class PartiallyMatchedCrossoverTest {
18      public PartiallyMatchedCrossoverTest() {
19      }
20  
21      /**
22       * @see com.delhezi.ga.crossover.permutation.PartiallyMatchedCrossover#crossover(com.delhezi.ga.Chromosome, com.delhezi.ga.Chromosome)
23       */
24      @Test
25      public void testCrossover() {
26          ChromosomeProperties chromosomeProperties = ChromosomeProperties.getInstance();
27          //TODO zamockowac geny
28          Integer[] genes1 = {6, 11, 8, 5, 9, 7, 1, 4, 10, 2, 3};
29          Chromosome<Integer> chromosome1 =
30                  new Chromosome<Integer>(genes1, chromosomeProperties);
31  
32          Integer[] genes2 = {5, 11, 3, 9, 8, 6, 7, 1, 10, 4, 2};
33          Chromosome<Integer> chromosome2 =
34                  new Chromosome<Integer>(genes2, chromosomeProperties);
35  
36          PartiallyMatchedCrossover instance = new PartiallyMatchedCrossover();
37          instance.crossover(chromosome1, chromosome2);
38      }
39  
40      /**
41       * @see com.delhezi.ga.crossover.permutation.PartiallyMatchedCrossover#crossover(com.delhezi.ga.Chromosome, com.delhezi.ga.Chromosome, int, int)
42       */
43      @Test
44      public void testCrossover1() {
45          //given
46          ChromosomeProperties chromosomeProperties = ChromosomeProperties.getInstance();
47          Integer[] genes1 = {6, 11, 8, 5, 9, 7, 1, 4, 10, 2, 3};
48          Chromosome<Integer> chromosome1 =
49                  new Chromosome<Integer>(genes1, chromosomeProperties);
50  
51          Integer[] genes2 = {5, 11, 3, 9, 8, 6, 7, 1, 10, 4, 2};
52          Chromosome<Integer> chromosome2 =
53                  new Chromosome<Integer>(genes2, chromosomeProperties);
54  
55          int cutpoint1 = 1;
56          int cutpoint2 = 6;
57  
58          //when
59          PartiallyMatchedCrossover instance = new PartiallyMatchedCrossover();
60          instance.crossover(chromosome1, chromosome2, cutpoint1, cutpoint2);
61          //for (int i=0; i<chromosome1.size(); i++)
62          //    System.out.println(chromosome1.getGene(i).toString());
63          //System.out.println("-----------");
64          //for (int i = 0; i < chromosome2.size(); i++)
65          //    System.out.println(chromosome2.getGene(i).toString());
66  
67          //then
68          assertEquals(chromosome1.size(), 11);
69          assertEquals(chromosome1.getGene(0).intValue(), 7);
70          assertEquals(chromosome1.getGene(1).intValue(), 11);
71          assertEquals(chromosome1.getGene(2).intValue(), 3);
72          assertEquals(chromosome1.getGene(3).intValue(), 9);
73          assertEquals(chromosome1.getGene(4).intValue(), 8);
74          assertEquals(chromosome1.getGene(5).intValue(), 6);
75          assertEquals(chromosome1.getGene(6).intValue(), 1);
76          assertEquals(chromosome1.getGene(7).intValue(), 4);
77          assertEquals(chromosome1.getGene(8).intValue(), 10);
78          assertEquals(chromosome1.getGene(9).intValue(), 2);
79          assertEquals(chromosome1.getGene(10).intValue(), 5);
80  
81  
82          assertEquals(chromosome2.size(), 11);
83          assertEquals(chromosome2.getGene(0).intValue(), 3);
84          assertEquals(chromosome2.getGene(1).intValue(), 11);
85          assertEquals(chromosome2.getGene(2).intValue(), 8);
86          assertEquals(chromosome2.getGene(3).intValue(), 5);
87          assertEquals(chromosome2.getGene(4).intValue(), 9);
88          assertEquals(chromosome2.getGene(5).intValue(), 7);
89          assertEquals(chromosome2.getGene(6).intValue(), 6);
90          assertEquals(chromosome2.getGene(7).intValue(), 1);
91          assertEquals(chromosome2.getGene(8).intValue(), 10);
92          assertEquals(chromosome2.getGene(9).intValue(), 4);
93          assertEquals(chromosome2.getGene(10).intValue(), 2);
94      }
95  
96      /**
97       * @see com.delhezi.ga.crossover.permutation.PartiallyMatchedCrossover#optSubstring(java.util.ArrayList,java.util.ArrayList)
98       */
99      @Test
100     public void testOptSubstring() {
101         //given
102         ArrayList<Integer> substring1 = new ArrayList<Integer>(5);
103         substring1.add(11);
104         substring1.add(8);
105         substring1.add(5);
106         substring1.add(9);
107         substring1.add(7);
108         ArrayList<Integer> substring2 = new ArrayList<Integer>(5);
109         substring2.add(11);
110         substring2.add(3);
111         substring2.add(9);
112         substring2.add(8);
113         substring2.add(6);
114 
115         //when
116         PartiallyMatchedCrossover instance = new PartiallyMatchedCrossover();
117         instance.optSubstring(substring1, substring2);
118 
119         //Określenie owzorowania dla sekcji dopasowania.
120         // 11 <-> 11
121         // 8 <-> 3
122         // 5 <-> 9
123         // 9 <-> 8
124         // 7 <-> 6
125         // "zlepiając" powyższe odwzorowanie otrzymujemy
126         // 5 <-> 9 <-> 8 <-> 3
127         // 7 <-> 6
128         // Dodatkowo pominęliśmy odwzorowanie 11<->11
129         // Po skróceniu otrzymujemy
130         // 5 <-> 3
131         // 7 <-> 6
132         //for (int i = 0; i < substring1.size(); i++)
133         //    System.out.println(substring1.get(i) +" <=> "+substring2.get(i));
134 
135         //then
136         assertEquals(substring1.size(), 2);
137         assertEquals(substring1.get(0).intValue(), 5);
138         assertEquals(substring1.get(1).intValue(), 7);
139         assertEquals(substring2.get(0).intValue(), 3);
140         assertEquals(substring2.get(1).intValue(), 6);
141     }
142 
143 }