1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class MutationOperatorTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.36 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(MutationOperatorTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 42 public void testConstruct_0() 43 throws Exception { 44 MutationOperator mutOp = new MutationOperator(conf, 234); 45 assertEquals(234, mutOp.getMutationRate()); 46 assertNull(mutOp.getMutationRateCalc()); 47 } 48 49 54 public void testConstruct_1() 55 throws Exception { 56 MutationOperator mutOp = new MutationOperator(conf); 57 assertEquals(0, mutOp.getMutationRate()); 58 assertNotNull(mutOp.getMutationRateCalc()); 59 } 60 61 66 public void testConstruct_2() 67 throws Exception { 68 MutationOperator mutOp = new MutationOperator(conf, null); 69 assertEquals(0, mutOp.getMutationRate()); 70 assertNull(mutOp.getMutationRateCalc()); 71 } 72 73 78 public void testConstruct_3() 79 throws Exception { 80 IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf); 81 MutationOperator mutOp = new MutationOperator(conf, calc); 82 assertEquals(0, mutOp.getMutationRate()); 83 assertEquals(calc, mutOp.getMutationRateCalc()); 84 } 85 86 93 public void testOperate_0() 94 throws Exception { 95 Configuration conf = new DefaultConfiguration(); 96 conf.setFitnessFunction(new TestFitnessFunction()); 97 MutationOperator mutOp = new MutationOperator(conf); 98 List candChroms = new Vector(); 99 Chromosome[] population = new Chromosome[] {}; 100 mutOp.operate(new Population(conf, population), candChroms); 101 assertEquals(candChroms.size(), population.length); 102 } 103 104 112 public void testOperate_0_2() 113 throws Exception { 114 Configuration conf = new DefaultConfiguration(); 115 conf.setFitnessFunction(new TestFitnessFunction()); 116 MutationOperator mutOp = new MutationOperator(conf, 117 new 118 DefaultMutationRateCalculator( 119 conf)); 120 List candChroms = new Vector(); 121 RandomGeneratorForTest gen = new RandomGeneratorForTest(); 122 gen.setNextInt(9); 123 conf.setRandomGenerator(gen); 124 Chromosome c1 = new Chromosome(conf, new BooleanGene(conf), 9); 125 conf.setSampleChromosome(c1); 126 conf.addNaturalSelector(new BestChromosomesSelector(conf), true); 127 conf.setPopulationSize(5); 128 for (int i = 0; i < c1.getGenes().length; i++) { 129 c1.getGene(i).setAllele(Boolean.TRUE); 130 } 131 Chromosome c2 = new Chromosome(conf, new IntegerGene(conf), 4); 132 for (int i = 0; i < c2.getGenes().length; i++) { 133 c2.getGene(i).setAllele(new Integer (27)); 134 } 135 Chromosome[] population = new Chromosome[] { 136 c1, c2}; 137 mutOp.operate(new Population(conf, population), candChroms); 138 assertEquals(candChroms.size(), population.length); 139 } 140 141 149 public void testOperate_1() 150 throws Exception { 151 List candChroms = new Vector(); 152 Configuration conf = new Configuration(); 153 conf.setPopulationSize(3); 154 conf.setRandomGenerator(new StockRandomGenerator()); 155 MutationOperator mutOp = new MutationOperator(conf, 156 new 157 DefaultMutationRateCalculator( 158 conf)); 159 Chromosome[] population = new Chromosome[] { 160 new Chromosome(conf, new BooleanGene(conf), 9), 161 (new Chromosome(conf, new IntegerGene(conf), 4))}; 162 mutOp.operate(new Population(conf, population), candChroms); 163 } 164 165 171 public void testOperate_2() 172 throws Exception { 173 MutationOperator mutOp = new MutationOperator(conf); 174 List candChroms = new Vector(); 175 Chromosome[] population = new Chromosome[] { 176 new Chromosome(conf, new BooleanGene(conf), 9), 177 (new Chromosome(conf, new IntegerGene(conf), 4))}; 178 try { 179 mutOp.operate(new Population(null, population), candChroms); 180 fail(); 181 } 182 catch (InvalidConfigurationException nex) { 183 ; } 185 } 186 187 195 public void testOperate_3() 196 throws Exception { 197 DefaultConfiguration conf = new DefaultConfiguration(); 198 MutationOperator op = new MutationOperator(conf, 199 new 200 DefaultMutationRateCalculator( 201 conf)); 202 conf.addGeneticOperator(op); 203 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 204 rand.setNextInt(0); 205 conf.setRandomGenerator(rand); 206 conf.setFitnessFunction(new TestFitnessFunction()); 207 Gene sampleGene = new IntegerGene(conf, 1, 10); 208 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 209 conf.setSampleChromosome(chrom); 210 conf.setPopulationSize(6); 211 Gene cgene1 = new IntegerGene(conf, 1, 10); 212 cgene1.setAllele(new Integer (6)); 213 Gene[] genes1 = new Gene[] { 214 cgene1}; 215 Chromosome chrom1 = new Chromosome(conf, genes1); 216 Gene cgene2 = new IntegerGene(conf, 1, 10); 217 cgene2.setAllele(new Integer (9)); 218 Gene[] genes2 = new Gene[] { 219 cgene2}; 220 Chromosome chrom2 = new Chromosome(conf, genes2); 221 Chromosome[] population = new Chromosome[] { 222 chrom1, chrom2}; 223 List chroms = new Vector(); 224 Gene gene1 = new IntegerGene(conf, 1, 10); 225 gene1.setAllele(new Integer (5)); 226 chroms.add(gene1); 227 Gene gene2 = new IntegerGene(conf, 1, 10); 228 gene2.setAllele(new Integer (7)); 229 chroms.add(gene2); 230 Gene gene3 = new IntegerGene(conf, 1, 10); 231 gene3.setAllele(new Integer (4)); 232 chroms.add(gene3); 233 assertEquals(3, chroms.size()); 234 Population pop = new Population(conf, population); 235 op.operate(pop, chroms); 236 assertEquals(2, pop.size()); 237 assertEquals(3 + 2, chroms.size()); 238 op.operate(pop, chroms); 239 assertEquals(2, pop.size()); 240 assertEquals(3 + 2 + 2, chroms.size()); 241 } 242 243 249 public void testOperate_3_1() 250 throws Exception { 251 DefaultConfiguration conf = new DefaultConfiguration(); 252 GeneticOperator op = new MutationOperator(conf, 10); 253 conf.addGeneticOperator(op); 254 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 255 rand.setNextIntSequence(new int[] {1, 0, 1}); 258 rand.setNextDouble(0.7d); 259 conf.setRandomGenerator(rand); 260 conf.setFitnessFunction(new TestFitnessFunction()); 261 Gene sampleGene = new IntegerGene(conf, 0, 9); 262 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 263 conf.setSampleChromosome(chrom); 264 conf.setPopulationSize(6); 265 Gene[] genes1 = new Gene[3]; 266 for (int i = 0; i < genes1.length; i++) { 267 genes1[i] = new IntegerGene(conf, 0, 9); 268 genes1[i].setAllele(new Integer (i)); 269 } 270 Chromosome chrom1 = new Chromosome(conf, genes1); 271 Gene[] genes2 = new Gene[3]; 272 for (int i = 0; i < genes2.length; i++) { 273 genes2[i] = new IntegerGene(conf, 0, 9); 274 genes2[i].setAllele(new Integer (i + 3)); 275 } 276 Chromosome chrom2 = new Chromosome(conf, genes2); 277 Chromosome[] population = new Chromosome[] { 278 chrom1, chrom2}; 279 List chroms = new Vector(); 282 Population pop = new Population(conf, population); 283 op.operate(pop, chroms); 284 assertEquals(2, chroms.size()); 285 Chromosome c1 = (Chromosome) chroms.get(0); 287 assertEquals(new Integer (0), c1.getGene(0).getAllele()); 288 assertEquals(new Integer (1 + 4), c1.getGene(1).getAllele()); 289 assertEquals(new Integer (2), c1.getGene(2).getAllele()); 290 Chromosome c2 = (Chromosome) chroms.get(1); 292 assertEquals(new Integer (3), c2.getGene(0).getAllele()); 293 assertEquals(new Integer (4 + 4), c2.getGene(1).getAllele()); 294 assertEquals(new Integer (5), c2.getGene(2).getAllele()); 295 op.operate(pop, chroms); 296 assertEquals(4, chroms.size()); 297 } 298 299 306 public void testOperate_4() 307 throws Exception { 308 DefaultConfiguration conf = new DefaultConfiguration(); 309 MutationOperator mutOp = new MutationOperator(conf, 0); 310 mutOp.setMutationRateCalc(null); 311 List candChroms = new Vector(); 312 BooleanGene gene1 = new BooleanGene(conf); 313 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 314 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 315 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 316 Chromosome chrom2 = new Chromosome(conf, gene2, 1); 317 chrom2.getGene(0).setAllele(new Integer (3)); 318 candChroms.add(chrom1); 319 candChroms.add(chrom2); 320 mutOp.operate(null, candChroms); 321 assertEquals(2, candChroms.size()); 322 assertEquals(chrom1, candChroms.get(0)); 323 assertEquals(chrom2, candChroms.get(1)); 324 } 325 326 334 public void testOperate_5() 335 throws Exception { 336 Configuration conf = new Configuration(); 337 conf.setPopulationSize(5); 338 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 339 rn.setNextInt(0); 340 rn.setNextDouble(0.8d); conf.setRandomGenerator(rn); 342 BooleanGene gene1 = new BooleanGene(conf); 343 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 344 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 345 IntegerGene gene2 = new IntegerGene(conf, 0, 10); Chromosome chrom2 = new Chromosome(conf, gene2, 1); 347 chrom2.getGene(0).setAllele(new Integer (3)); Chromosome[] chroms = new Chromosome[] { 349 chrom1, chrom2}; 350 MutationOperator mutOp = new MutationOperator(conf, 351 new 352 DefaultMutationRateCalculator( 353 conf)); 354 Population pop = new Population(conf, chroms); 355 mutOp.operate(pop, pop.getChromosomes()); 356 assertEquals(2 + 2, pop.getChromosomes().size()); 359 assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0)) 361 .booleanValue()); 362 assertTrue( ( (BooleanGene) pop.getChromosome(2).getGene(0)).booleanValue()); 364 assertEquals(3, ( (IntegerGene) pop.getChromosome(1).getGene(0)).intValue()); 366 assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)), 370 ( (IntegerGene) pop.getChromosome(3).getGene(0)).intValue()); 371 } 372 373 381 public void testOperate_5_2() 382 throws Exception { 383 Configuration conf = new Configuration(); 384 conf.setPopulationSize(5); 385 BooleanGene gene1 = new BooleanGene(conf); 386 CompositeGene comp1 = new CompositeGene(conf); 387 comp1.addGene(gene1); 388 Chromosome chrom1 = new Chromosome(conf, comp1, 1); 389 ( (CompositeGene) chrom1.getGene(0)).geneAt(0).setAllele( 390 Boolean.valueOf(false)); 391 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 392 CompositeGene comp2 = new CompositeGene(conf); 393 comp2.addGene(gene2); 394 Chromosome chrom2 = new Chromosome(conf, comp2, 1); 395 ( (CompositeGene) chrom2.getGene(0)).geneAt(0).setAllele(new Integer (3)); 396 Chromosome[] chroms = new Chromosome[] { 397 chrom1, chrom2}; 398 MutationOperator mutOp = new MutationOperator(conf, 399 new 400 DefaultMutationRateCalculator( 401 conf)); 402 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 403 rn.setNextInt(0); 404 rn.setNextDouble(0.8d); 405 conf.setRandomGenerator(rn); 406 Population pop = new Population(conf, chroms); 407 mutOp.operate(pop, pop.getChromosomes()); 408 assertEquals(2 + 2, pop.getChromosomes().size()); 409 assertFalse( ( (BooleanGene) ( (CompositeGene) pop.getChromosome(0).getGene( 411 0)).geneAt(0)).booleanValue()); 412 assertTrue( ( (BooleanGene) ( (CompositeGene) pop.getChromosome(2).getGene( 414 0)).geneAt(0)).booleanValue()); 415 assertEquals(3, 417 ( (IntegerGene) ( (CompositeGene) pop.getChromosome(1). 418 getGene(0)).geneAt(0)).intValue()); 419 assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)), 423 ( ( 424 IntegerGene) ( (CompositeGene) pop.getChromosome(3).getGene(0)). 425 geneAt(0)).intValue()); 426 } 427 428 435 public void testOperate_6() 436 throws Exception { 437 Configuration conf = new DefaultConfiguration(); 438 MutationOperator mutOp = new MutationOperator(conf, 0); 439 mutOp.setMutationRateCalc(null); 440 mutOp.operate(null, null); 441 } 442 443 449 public void testOperate_6_2() 450 throws Exception { 451 Configuration conf = new DefaultConfiguration(); 452 MutationOperator mutOp = new MutationOperator(conf, 0); 453 mutOp.setMutationRateCalc(new DefaultMutationRateCalculator(conf)); 454 mutOp.operate(null, null); 455 } 456 457 466 public void testOperate_8() 467 throws Exception { 468 Configuration conf = new Configuration(); 469 conf.setPopulationSize(5); 470 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 471 rn.setNextInt(0); 472 rn.setNextInt(0); 473 rn.setNextDouble(0.8d); 474 conf.setRandomGenerator(rn); 475 BooleanGene gene1 = new BooleanGene(conf); 476 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 477 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 478 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 479 Chromosome chrom2 = new Chromosome(conf, gene2, 1); 480 chrom2.getGene(0).setAllele(new Integer (3)); 481 Chromosome[] chroms = new Chromosome[] { 482 chrom1, chrom2}; 483 MutationOperator mutOp = new MutationOperator(conf, 484 new 485 DefaultMutationRateCalculator( 486 conf)); 487 IGeneticOperatorConstraint constraint = new 488 GeneticOperatorConstraintForTest(); 489 conf.getJGAPFactory().setGeneticOperatorConstraint( 490 constraint); 491 Population pop = new Population(conf, chroms); 492 mutOp.operate(pop, pop.getChromosomes()); 493 assertEquals(2 + 1, pop.getChromosomes().size()); 495 assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0)).booleanValue()); 497 assertEquals(3, ( (IntegerGene) pop.getChromosome(1). 499 getGene(0)).intValue()); 500 assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)), 502 ( (IntegerGene) pop.getChromosome(2).getGene(0)).intValue()); 503 } 504 505 512 public void testIsSerializable_0() 513 throws Exception { 514 MutationOperator op = new MutationOperator(conf); 515 assertTrue(isSerializable(op)); 516 } 517 518 525 public void testDoSerialize_0() 526 throws Exception { 527 IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf); 529 MutationOperator op = new MutationOperator(conf, calc); 530 Object o = doSerialize(op); 531 assertEquals(o, op); 532 } 533 534 public class GeneticOperatorConstraintForTest 535 implements IGeneticOperatorConstraint { 536 public boolean isValid(Population a_pop, List a_chromosomes, 537 GeneticOperator a_caller) { 538 Chromosome chrom = (Chromosome) a_chromosomes.get(0); 539 Gene gene = chrom.getGene(0); 540 return gene.getClass() != BooleanGene.class; 541 } 542 } 543 550 public void testEquals_0() 551 throws Exception { 552 GeneticOperator op = new MutationOperator(conf); 553 assertFalse(op.equals(new Chromosome(conf))); 554 } 555 556 562 public void testCompareTo_0() 563 throws Exception { 564 MutationOperator op = new MutationOperator(conf); 565 assertEquals(1, op.compareTo(null)); 566 MutationOperator op2 = new MutationOperator(conf); 567 assertEquals(0, op.compareTo(op2)); 568 op = new MutationOperator(conf, 3); 569 assertEquals( -1, op.compareTo(op2)); 570 assertEquals(1, op2.compareTo(op)); 571 op = new MutationOperator(conf, new DefaultMutationRateCalculator(conf)); 572 assertEquals(0, op.compareTo(op2)); 573 op = new MutationOperator(conf, 3); 574 op2 = new MutationOperator(conf, 4); 575 assertEquals( -1, op.compareTo(op2)); 576 assertEquals(1, op2.compareTo(op)); 577 } 578 } 579 | Popular Tags |