1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.JGAPTestCase.*; 15 import junit.framework.*; 16 17 23 public class InversionOperatorTest 24 extends JGAPTestCase { 25 26 private static final String CVS_REVISION = "$Revision: 1.10 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(InversionOperatorTest.class); 30 return suite; 31 } 32 33 public void setUp() { 34 super.setUp(); 35 Configuration.reset(); 36 } 37 38 43 public void testConstruct_0() 44 throws Exception { 45 new InversionOperator(conf); 46 } 47 48 54 public void testConstruct_1() 55 throws Exception { 56 Genotype.setStaticConfiguration(conf); 57 InversionOperator op = new InversionOperator(); 58 assertSame(conf, op.getConfiguration()); 59 } 60 61 67 public void testOperate_0() 68 throws Exception { 69 DefaultConfiguration conf = new DefaultConfiguration(); 70 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 71 rand.setNextIntSequence(new int[] { 73 0, 1, 1, 1}); 74 conf.setRandomGenerator(rand); 75 conf.setFitnessFunction(new TestFitnessFunction()); 76 Gene sampleGene = new IntegerGene(conf, 1, 10); 77 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 78 conf.setSampleChromosome(chrom); 79 conf.setPopulationSize(6); 80 InversionOperator op = new InversionOperator(conf); 81 Gene cgene1 = new IntegerGene(conf, 1, 10); 82 cgene1.setAllele(new Integer (6)); 83 Gene[] genes1 = new Gene[] { 84 cgene1}; 85 Chromosome chrom1 = new Chromosome(conf, genes1); 86 Gene cgene2 = new IntegerGene(conf, 1, 10); 87 cgene2.setAllele(new Integer (8)); 88 Gene cgene3 = new IntegerGene(conf, 2, 120); 89 cgene3.setAllele(new Integer (99)); 90 Gene[] genes2 = new Gene[] { 91 cgene2, cgene3}; 92 Chromosome chrom2 = new Chromosome(conf, genes2); 93 Chromosome[] population = new Chromosome[] { 94 chrom1, chrom2}; 95 List chroms = new Vector(); 96 Gene gene1 = new IntegerGene(conf, 1, 10); 97 gene1.setAllele(new Integer (5)); 98 chroms.add(gene1); 99 Gene gene2 = new IntegerGene(conf, 1, 10); 100 gene2.setAllele(new Integer (7)); 101 chroms.add(gene2); 102 Gene gene3 = new IntegerGene(conf, 1, 10); 103 gene3.setAllele(new Integer (4)); 104 chroms.add(gene3); 105 final int size = chroms.size(); 106 op.operate(new Population(conf, population), chroms); 107 assertEquals(size + 1, chroms.size()); 108 Chromosome target = (Chromosome) chroms.get(size); 109 assertEquals(6, ( (Integer ) target.getGene(0).getAllele()).intValue()); 110 op.operate(new Population(conf, population), chroms); 111 assertEquals(size + 2, chroms.size()); 112 target = (Chromosome) chroms.get(size + 1); 113 assertEquals(99, ( (Integer ) target.getGene(0).getAllele()).intValue()); 114 assertEquals(8, ( (Integer ) target.getGene(1).getAllele()).intValue()); 115 } 116 117 124 public void testOperate_1() 125 throws Exception { 126 DefaultConfiguration conf = new DefaultConfiguration(); 127 GeneticOperator op = new InversionOperator(conf); 128 conf.addGeneticOperator(op); 129 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 130 rand.setNextIntSequence(new int[] { 131 0, 1, 0, 1, 2}); 132 conf.setRandomGenerator(rand); 133 conf.setFitnessFunction(new TestFitnessFunction()); 134 Gene sampleGene = new IntegerGene(conf, 1, 10); 135 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 136 conf.setSampleChromosome(chrom); 137 conf.setPopulationSize(6); 138 Gene cgene1 = new IntegerGene(conf, 1, 10); 139 cgene1.setAllele(new Integer (6)); 140 Gene[] genes1 = new Gene[] { 141 cgene1}; 142 Chromosome chrom1 = new Chromosome(conf, genes1); 143 Gene cgene2 = new IntegerGene(conf, 1, 10); 144 cgene2.setAllele(new Integer (8)); 145 Gene[] genes2 = new Gene[] { 146 cgene2}; 147 Chromosome chrom2 = new Chromosome(conf, genes2); 148 Chromosome[] population = new Chromosome[] { 149 chrom1, chrom2}; 150 List chroms = new Vector(); 151 Gene gene1 = new IntegerGene(conf, 1, 10); 152 gene1.setAllele(new Integer (5)); 153 chroms.add(gene1); 154 Gene gene2 = new IntegerGene(conf, 1, 10); 155 gene2.setAllele(new Integer (7)); 156 chroms.add(gene2); 157 Gene gene3 = new IntegerGene(conf, 1, 10); 158 gene3.setAllele(new Integer (4)); 159 chroms.add(gene3); 160 Chromosome[] population2 = (Chromosome[]) population.clone(); 161 op.operate(new Population(conf, population), chroms); 162 op.operate(new Population(conf, population2), chroms); 163 assertTrue(isChromosomesEqual(population, population2)); 164 } 165 166 173 public void testOperate_2() 174 throws Exception { 175 DefaultConfiguration conf = new DefaultConfiguration(); 176 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 177 rand.setNextIntSequence(new int[] { 179 0, 1, 1, 1, 2}); 180 conf.setRandomGenerator(rand); 181 conf.setFitnessFunction(new TestFitnessFunction()); 182 Gene sampleGene = new IntegerGene(conf, 1, 10); 183 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 184 conf.setSampleChromosome(chrom); 185 conf.setPopulationSize(6); 186 InversionOperator op = new InversionOperator(conf); 187 Gene cgene1 = new IntegerGene(conf, 1, 10); 188 cgene1.setAllele(new Integer (6)); 189 CompositeGene compGene = new CompositeGene(conf); 190 compGene.addGene(cgene1); 191 Gene[] genes1 = new Gene[] { 192 compGene}; 193 Chromosome chrom1 = new Chromosome(conf, genes1); 194 Gene cgene2 = new IntegerGene(conf, 1, 10); 195 cgene2.setAllele(new Integer (8)); 196 Gene[] genes2 = new Gene[] { 197 cgene2}; 198 Chromosome chrom2 = new Chromosome(conf, genes2); 199 Chromosome[] population = new Chromosome[] { 200 chrom1, chrom2}; 201 List chroms = new Vector(); 202 Gene gene1 = new IntegerGene(conf, 1, 10); 203 gene1.setAllele(new Integer (5)); 204 chroms.add(gene1); 205 Gene gene2 = new IntegerGene(conf, 1, 10); 206 gene2.setAllele(new Integer (7)); 207 chroms.add(gene2); 208 Gene gene3 = new IntegerGene(conf, 1, 10); 209 gene3.setAllele(new Integer (4)); 210 chroms.add(gene3); 211 int size = chroms.size(); 212 op.operate(new Population(conf, population), chroms); 213 assertEquals(size + 1, chroms.size()); 214 Chromosome target = (Chromosome) chroms.get(size); 215 CompositeGene cog = (CompositeGene) target.getGene(0); 216 assertEquals(6, ( (Integer ) ( (Vector) cog.getAllele()).get(0)).intValue()); 217 op.operate(new Population(conf, population), chroms); 218 assertEquals(size + 2, chroms.size()); 219 target = (Chromosome) chroms.get(size + 1); 220 IntegerGene result = (IntegerGene) target.getGene(0); 221 assertEquals(8, ( (Integer ) result.getAllele()).intValue()); 222 } 223 224 231 public void testEquals_0() 232 throws Exception { 233 GeneticOperator op = new InversionOperator(conf); 234 assertFalse(op.equals(new Chromosome(conf))); 235 } 236 237 243 public void testOperate_3() 244 throws Exception { 245 DefaultConfiguration conf = new DefaultConfiguration(); 246 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 247 rand.setNextIntSequence(new int[] { 249 0, 1, 1, 1, 2}); 250 conf.setPopulationSize(6); 251 conf.setRandomGenerator(rand); 252 Gene sampleGene = new IntegerGene(conf, 1, 10); 253 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 254 conf.setSampleChromosome(chrom); 255 Gene gene1 = new IntegerGene(conf, 1, 10); 256 gene1.setAllele(new Integer (5)); 257 Gene cgene1 = new IntegerGene(conf, 1, 10); 258 cgene1.setAllele(new Integer (6)); 259 Gene cgene1_2 = new IntegerGene(conf, 1, 10); 260 cgene1_2.setAllele(new Integer (9)); 261 CompositeGene compGene = new CompositeGene(conf); 262 compGene.addGene(cgene1); 263 Gene[] genes1 = new Gene[] { 264 compGene}; 265 Chromosome chrom1 = new Chromosome(conf, genes1); 266 chrom1.setConstraintChecker(new TestConstraintChecker()); 267 Gene cgene2 = new IntegerGene(conf, 1, 10); 268 cgene2.setAllele(new Integer (8)); 269 Gene[] genes2 = new Gene[] { 270 cgene2, cgene1_2}; 271 Chromosome chrom2 = new Chromosome(conf); 272 chrom2.setGenes(genes2); 273 chrom2.setConstraintChecker(new TestConstraintChecker()); 274 Chromosome[] population = new Chromosome[] { 275 chrom1, chrom2}; 276 List chroms = new Vector(); 277 chroms.add(gene1); 278 Gene gene2 = new IntegerGene(conf, 1, 10); 279 gene2.setAllele(new Integer (7)); 280 chroms.add(gene2); 281 Gene gene3 = new IntegerGene(conf, 1, 10); 282 gene3.setAllele(new Integer (4)); 283 chroms.add(gene3); 284 InversionOperator op = new InversionOperator(conf); 285 try { 286 op.operate(new Population(conf, population), chroms); 287 fail(); 288 } catch (Error iex) { 289 ; } 291 } 292 293 299 public void testCompareTo_0() 300 throws Exception { 301 InversionOperator op = new InversionOperator(conf); 302 assertEquals(1, op.compareTo(null)); 303 InversionOperator op2 = new InversionOperator(conf); 304 assertEquals(0, op.compareTo(op2)); 305 assertEquals(0, op2.compareTo(op)); 306 } 307 308 public class TestConstraintChecker 309 implements IGeneConstraintChecker { 310 private int m_callcount; 311 312 public boolean verify(Gene a_gene, Object a_alleleValue, 313 IChromosome a_chrom, int a_index) { 314 if (m_callcount++ >= 2) { 315 return false; 316 } 317 else { 318 return true; 319 } 320 } 321 } 322 } 323 | Popular Tags |