1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class TwoWayMutationOperatorTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.1 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(TwoWayMutationOperatorTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 43 public void testConstruct_0() 44 throws Exception { 45 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 234); 46 assertEquals(234, mutOp.getMutationRate()); 47 assertNull(mutOp.getMutationRateCalc()); 48 } 49 50 56 public void testConstruct_1() 57 throws Exception { 58 Genotype.setStaticConfiguration(conf); 59 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(); 60 assertEquals(0, mutOp.getMutationRate()); 61 assertNotNull(mutOp.getMutationRateCalc()); 62 assertSame(conf, mutOp.getConfiguration()); 63 } 64 65 71 public void testConstruct_2() 72 throws Exception { 73 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, null); 74 assertEquals(0, mutOp.getMutationRate()); 75 assertNull(mutOp.getMutationRateCalc()); 76 } 77 78 84 public void testConstruct_3() 85 throws Exception { 86 IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf); 87 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, calc); 88 assertEquals(0, mutOp.getMutationRate()); 89 assertEquals(calc, mutOp.getMutationRateCalc()); 90 } 91 92 100 public void testOperate_0() 101 throws Exception { 102 Configuration conf = new DefaultConfiguration(); 103 conf.setFitnessFunction(new TestFitnessFunction()); 104 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf); 105 List candChroms = new Vector(); 106 Chromosome[] population = new Chromosome[] {}; 107 mutOp.operate(new Population(conf, population), candChroms); 108 assertEquals(candChroms.size(), population.length); 109 } 110 111 120 public void testOperate_0_2() 121 throws Exception { 122 Configuration conf = new DefaultConfiguration(); 123 conf.setFitnessFunction(new TestFitnessFunction()); 124 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 125 new DefaultMutationRateCalculator(conf)); 126 List candChroms = new Vector(); 127 RandomGeneratorForTest gen = new RandomGeneratorForTest(); 128 gen.setNextInt(9); 129 conf.setRandomGenerator(gen); 130 Chromosome c1 = new Chromosome(conf, new BooleanGene(conf), 9); 131 conf.setSampleChromosome(c1); 132 conf.addNaturalSelector(new BestChromosomesSelector(conf), true); 133 conf.setPopulationSize(5); 134 for (int i = 0; i < c1.getGenes().length; i++) { 135 c1.getGene(i).setAllele(Boolean.TRUE); 136 } 137 Chromosome c2 = new Chromosome(conf, new IntegerGene(conf), 4); 138 for (int i = 0; i < c2.getGenes().length; i++) { 139 c2.getGene(i).setAllele(new Integer (27)); 140 } 141 Chromosome[] population = new Chromosome[] { 142 c1, c2}; 143 mutOp.operate(new Population(conf, population), candChroms); 144 assertEquals(candChroms.size(), population.length); 145 } 146 147 156 public void testOperate_1() 157 throws Exception { 158 List candChroms = new Vector(); 159 Configuration conf = new Configuration(); 160 conf.setPopulationSize(3); 161 conf.setRandomGenerator(new StockRandomGenerator()); 162 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 163 new DefaultMutationRateCalculator(conf)); 164 Chromosome[] population = new Chromosome[] { 165 new Chromosome(conf, new BooleanGene(conf), 9), 166 (new Chromosome(conf, new IntegerGene(conf), 4))}; 167 mutOp.operate(new Population(conf, population), candChroms); 168 } 169 170 178 public void testOperate_2() 179 throws Exception { 180 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf); 181 List candChroms = new Vector(); 182 Chromosome[] population = new Chromosome[] { 183 new Chromosome(conf, new BooleanGene(conf), 9), 184 (new Chromosome(conf, new IntegerGene(conf), 4))}; 185 try { 186 mutOp.operate(new Population(null, population), candChroms); 187 fail(); 188 } catch (InvalidConfigurationException nex) { 189 ; } 191 } 192 193 201 public void testOperate_3() 202 throws Exception { 203 DefaultConfiguration conf = new DefaultConfiguration(); 204 TwoWayMutationOperator op = new TwoWayMutationOperator(conf, 205 new DefaultMutationRateCalculator(conf)); 206 conf.addGeneticOperator(op); 207 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 208 rand.setNextInt(0); 209 conf.setRandomGenerator(rand); 210 conf.setFitnessFunction(new TestFitnessFunction()); 211 Gene sampleGene = new IntegerGene(conf, 1, 10); 212 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 213 conf.setSampleChromosome(chrom); 214 conf.setPopulationSize(6); 215 Gene cgene1 = new IntegerGene(conf, 1, 10); 216 cgene1.setAllele(new Integer (6)); 217 Gene[] genes1 = new Gene[] { 218 cgene1}; 219 Chromosome chrom1 = new Chromosome(conf, genes1); 220 Gene cgene2 = new IntegerGene(conf, 1, 10); 221 cgene2.setAllele(new Integer (9)); 222 Gene[] genes2 = new Gene[] { 223 cgene2}; 224 Chromosome chrom2 = new Chromosome(conf, genes2); 225 Chromosome[] population = new Chromosome[] { 226 chrom1, chrom2}; 227 List chroms = new Vector(); 228 Gene gene1 = new IntegerGene(conf, 1, 10); 229 gene1.setAllele(new Integer (5)); 230 chroms.add(gene1); 231 Gene gene2 = new IntegerGene(conf, 1, 10); 232 gene2.setAllele(new Integer (7)); 233 chroms.add(gene2); 234 Gene gene3 = new IntegerGene(conf, 1, 10); 235 gene3.setAllele(new Integer (4)); 236 chroms.add(gene3); 237 assertEquals(3, chroms.size()); 238 Population pop = new Population(conf, population); 239 op.operate(pop, chroms); 240 assertEquals(2, pop.size()); 241 assertEquals(3 + 2, chroms.size()); 242 op.operate(pop, chroms); 243 assertEquals(2, pop.size()); 244 assertEquals(3 + 2 + 2, chroms.size()); 245 } 246 247 253 public void testOperate_3_1() 254 throws Exception { 255 DefaultConfiguration conf = new DefaultConfiguration(); 256 GeneticOperator op = new TwoWayMutationOperator(conf, 10); 257 conf.addGeneticOperator(op); 258 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 259 rand.setNextIntSequence(new int[] {0, 0, 1}); 262 rand.setNextDouble(0.7d); 263 conf.setRandomGenerator(rand); 264 conf.setFitnessFunction(new TestFitnessFunction()); 265 Gene sampleGene = new IntegerGene(conf, 0, 9); 266 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 267 conf.setSampleChromosome(chrom); 268 conf.setPopulationSize(6); 269 Gene[] genes1 = new Gene[3]; 270 for (int i = 0; i < genes1.length; i++) { 271 genes1[i] = new IntegerGene(conf, 0, 9); 272 genes1[i].setAllele(new Integer (i)); 273 } 274 Chromosome chrom1 = new Chromosome(conf, genes1); 275 Gene[] genes2 = new Gene[3]; 276 for (int i = 0; i < genes2.length; i++) { 277 genes2[i] = new IntegerGene(conf, 0, 9); 278 genes2[i].setAllele(new Integer (i + 3)); 279 } 280 Chromosome chrom2 = new Chromosome(conf, genes2); 281 Chromosome[] population = new Chromosome[] { 282 chrom1, chrom2}; 283 List chroms = new Vector(); 286 Population pop = new Population(conf, population); 287 op.operate(pop, chroms); 288 assertEquals(2, chroms.size()); 289 Chromosome c1 = (Chromosome) chroms.get(0); 291 assertEquals(new Integer (0), c1.getGene(0).getAllele()); 292 assertEquals(new Integer (1), c1.getGene(1).getAllele()); 293 assertEquals(new Integer (2 + 4), c1.getGene(2).getAllele()); 294 Chromosome c2 = (Chromosome) chroms.get(1); 296 assertEquals(new Integer (3), c2.getGene(0).getAllele()); 297 assertEquals(new Integer (4), c2.getGene(1).getAllele()); 298 assertEquals(new Integer (5 + 4), c2.getGene(2).getAllele()); 299 op.operate(pop, chroms); 300 assertEquals(3, chroms.size()); 301 } 302 303 310 public void testOperate_4() 311 throws Exception { 312 DefaultConfiguration conf = new DefaultConfiguration(); 313 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 0); 314 mutOp.setMutationRateCalc(null); 315 List candChroms = new Vector(); 316 BooleanGene gene1 = new BooleanGene(conf); 317 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 318 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 319 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 320 Chromosome chrom2 = new Chromosome(conf, gene2, 1); 321 chrom2.getGene(0).setAllele(new Integer (3)); 322 candChroms.add(chrom1); 323 candChroms.add(chrom2); 324 mutOp.operate(null, candChroms); 325 assertEquals(2, candChroms.size()); 326 assertEquals(chrom1, candChroms.get(0)); 327 assertEquals(chrom2, candChroms.get(1)); 328 } 329 330 338 public void testOperate_5() 339 throws Exception { 340 Configuration conf = new Configuration(); 341 conf.setPopulationSize(5); 342 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 343 rn.setNextInt(0); 344 rn.setNextDouble(0.8d); conf.setRandomGenerator(rn); 346 BooleanGene gene1 = new BooleanGene(conf); 347 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 348 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 349 IntegerGene gene2 = new IntegerGene(conf, 0, 10); Chromosome chrom2 = new Chromosome(conf, gene2, 1); 351 chrom2.getGene(0).setAllele(new Integer (3)); Chromosome[] chroms = new Chromosome[] { 353 chrom1, chrom2}; 354 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 355 new DefaultMutationRateCalculator(conf)); 356 Population pop = new Population(conf, chroms); 357 mutOp.operate(pop, pop.getChromosomes()); 358 assertEquals(2 + 2, pop.getChromosomes().size()); 361 assertFalse( ( (BooleanGene) pop.getChromosome(0).getGene(0)) 363 .booleanValue()); 364 assertTrue( ( (BooleanGene) pop.getChromosome(2).getGene(0)).booleanValue()); 366 assertEquals(3, ( (IntegerGene) pop.getChromosome(1).getGene(0)).intValue()); 368 assertEquals( (int) Math.round(3 + (10 - 0) * ( -1 + 0.8d * 2)), 372 ( (IntegerGene) pop.getChromosome(3).getGene(0)).intValue()); 373 } 374 375 383 public void testOperate_5_2() 384 throws Exception { 385 Configuration conf = new Configuration(); 386 conf.setPopulationSize(5); 387 BooleanGene gene1 = new BooleanGene(conf); 388 CompositeGene comp1 = new CompositeGene(conf); 389 comp1.addGene(gene1); 390 Chromosome chrom1 = new Chromosome(conf, comp1, 1); 391 ( (CompositeGene) chrom1.getGene(0)).geneAt(0).setAllele( 392 Boolean.valueOf(false)); 393 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 394 CompositeGene comp2 = new CompositeGene(conf); 395 comp2.addGene(gene2); 396 Chromosome chrom2 = new Chromosome(conf, comp2, 1); 397 ( (CompositeGene) chrom2.getGene(0)).geneAt(0).setAllele(new Integer (3)); 398 Chromosome[] chroms = new Chromosome[] { 399 chrom1, chrom2}; 400 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 401 new DefaultMutationRateCalculator(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). 425 getGene(0)). 426 geneAt(0)).intValue()); 427 } 428 429 437 public void testOperate_6() 438 throws Exception { 439 Configuration conf = new DefaultConfiguration(); 440 MutationOperator mutOp = new MutationOperator(conf, 0); 441 mutOp.setMutationRateCalc(null); 442 mutOp.operate(null, null); 443 } 444 445 451 public void testOperate_6_2() 452 throws Exception { 453 Configuration conf = new DefaultConfiguration(); 454 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 0); 455 mutOp.setMutationRateCalc(new DefaultMutationRateCalculator(conf)); 456 mutOp.operate(null, null); 457 } 458 459 468 public void testOperate_8() 469 throws Exception { 470 Configuration conf = new Configuration(); 471 conf.setPopulationSize(5); 472 RandomGeneratorForTest rn = new RandomGeneratorForTest(); 473 rn.setNextInt(0); 474 rn.setNextInt(0); 475 rn.setNextDouble(0.8d); 476 conf.setRandomGenerator(rn); 477 BooleanGene gene1 = new BooleanGene(conf); 478 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 479 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 480 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 481 Chromosome chrom2 = new Chromosome(conf, gene2, 1); 482 chrom2.getGene(0).setAllele(new Integer (3)); 483 Chromosome[] chroms = new Chromosome[] { 484 chrom1, chrom2}; 485 TwoWayMutationOperator mutOp = new TwoWayMutationOperator(conf, 486 new DefaultMutationRateCalculator(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 TwoWayMutationOperator op = new TwoWayMutationOperator(conf); 515 assertTrue(isSerializable(op)); 516 } 517 518 525 public void testDoSerialize_0() 526 throws Exception { 527 IUniversalRateCalculator calc = new DefaultCrossoverRateCalculator(conf); 529 TwoWayMutationOperator op = new TwoWayMutationOperator(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 TwoWayMutationOperator(conf); 553 assertFalse(op.equals(new Chromosome(conf))); 554 } 555 556 562 public void testCompareTo_0() 563 throws Exception { 564 TwoWayMutationOperator op = new TwoWayMutationOperator(conf); 565 assertEquals(1, op.compareTo(null)); 566 TwoWayMutationOperator op2 = new TwoWayMutationOperator(conf); 567 assertEquals(0, op.compareTo(op2)); 568 op = new TwoWayMutationOperator(conf, 3); 569 assertEquals( -1, op.compareTo(op2)); 570 assertEquals(1, op2.compareTo(op)); 571 op = new TwoWayMutationOperator(conf, 572 new DefaultMutationRateCalculator(conf)); 573 assertEquals(0, op.compareTo(op2)); 574 op = new TwoWayMutationOperator(conf, 3); 575 op2 = new TwoWayMutationOperator(conf, 4); 576 assertEquals( -1, op.compareTo(op2)); 577 assertEquals(1, op2.compareTo(op)); 578 } 579 } 580 | Popular Tags |