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  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  public class ChromosomeTest {
27  
28      @Test
29      public void testCompareTo() {
30          Chromosome c1 = new Chromosome() {
31              @Override
32              public double fitness() {
33                  return 0;
34              }
35          };
36          Chromosome c2 = new Chromosome() {
37              @Override
38              public double fitness() {
39                  return 10;
40              }
41          };
42          Chromosome c3 = new Chromosome() {
43              @Override
44              public double fitness() {
45                  return 10;
46              }
47          };
48  
49          Assert.assertTrue(c1.compareTo(c2) < 0);
50          Assert.assertTrue(c2.compareTo(c1) > 0);
51          Assert.assertEquals(0,c3.compareTo(c2));
52          Assert.assertEquals(0,c2.compareTo(c3));
53      }
54  
55      private abstract static class DummyChromosome extends Chromosome {
56          private final int repr;
57  
58          DummyChromosome(final int repr) {
59              this.repr = repr;
60          }
61          @Override
62          protected boolean isSame(Chromosome another) {
63              return ((DummyChromosome) another).repr == repr;
64          }
65      }
66  
67      @Test
68      public void testFindSameChromosome() {
69          Chromosome c1 = new DummyChromosome(1) {
70              @Override
71              public double fitness() {
72                  return 1;
73              }
74          };
75          Chromosome c2 = new DummyChromosome(2) {
76              @Override
77              public double fitness() {
78                  return 2;
79              }
80          };
81          Chromosome c3 = new DummyChromosome(3) {
82              @Override
83              public double fitness() {
84                  return 3;
85              }
86          };
87          Chromosome c4 = new DummyChromosome(1) {
88              @Override
89              public double fitness() {
90                  return 5;
91              }
92          };
93          Chromosome c5 = new DummyChromosome(15) {
94              @Override
95              public double fitness() {
96                  return 15;
97              }
98          };
99  
100         List<Chromosome> popChr = new ArrayList<>();
101         popChr.add(c1);
102         popChr.add(c2);
103         popChr.add(c3);
104         Population pop = new ListPopulation(popChr,3) {
105             @Override
106             public Population nextGeneration() {
107                 // not important
108                 return null;
109             }
110         };
111 
112         Assert.assertNull(c5.findSameChromosome(pop));
113         Assert.assertEquals(c1, c4.findSameChromosome(pop));
114 
115         c4.searchForFitnessUpdate(pop);
116         Assert.assertEquals(1, c4.getFitness(),0);
117     }
118 }
119