1 10 package org.jgap; 11 12 import java.util.*; 13 import org.jgap.impl.*; 14 import org.jgap.util.*; 15 import junit.framework.*; 16 17 23 public class GenotypeTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.58 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(GenotypeTest.class); 30 return suite; 31 } 32 33 public void setUp() { 34 super.setUp(); 35 Configuration.reset(); 36 } 37 38 44 public void testConstruct_0() 45 throws Exception { 46 try { 47 new Genotype(null, new Population(conf, 0)); 48 fail(); 49 } 50 catch (IllegalArgumentException invex) { 51 ; } 53 } 54 55 62 public void testConstruct_02() 63 throws Exception { 64 conf = new DefaultConfiguration(); 65 try { 66 new Genotype(conf, new Population(conf, 0)); 67 fail(); 68 } 69 catch (InvalidConfigurationException invex) { 70 ; } 72 } 73 74 80 public void testConstruct_1() 81 throws Exception { 82 try { 83 Population pop = null; 84 new Genotype(new DefaultConfiguration(), pop); 85 fail(); 86 } 87 catch (IllegalArgumentException invex) { 88 ; } 90 } 91 92 98 public void testConstruct_2() 99 throws Exception { 100 Chromosome[] chroms = new Chromosome[1]; 101 chroms[0] = new Chromosome(new ConfigurationForTest(), new Gene[] { 102 new IntegerGene(new ConfigurationForTest(), 1, 5) 103 }); 104 try { 105 new Genotype(null, chroms); 106 fail(); 107 } 108 catch (InvalidConfigurationException invex) { 109 ; } 111 catch (IllegalArgumentException invex) { 112 ; } 114 } 115 116 122 public void testConstruct_3() 123 throws Exception { 124 Chromosome[] chroms = new Chromosome[1]; 125 chroms[0] = new Chromosome(conf, new Gene[] { 126 new IntegerGene(conf, 1, 5) 127 }); 128 try { 129 new Genotype(new DefaultConfiguration(), chroms); 130 fail(); 131 } 132 catch (InvalidConfigurationException invex) { 133 ; } 135 } 136 137 143 public void testConstruct_4() 144 throws Exception { 145 Configuration conf = new DefaultConfiguration(); 146 Chromosome[] chroms = new Chromosome[1]; 147 chroms[0] = new Chromosome(conf, new Gene[] { 148 new IntegerGene(conf, 1, 5)}); 149 conf.setFitnessFunction(new StaticFitnessFunction(5)); 150 try { 151 new Genotype(conf, chroms); 152 fail(); 153 } 154 catch (InvalidConfigurationException invex) { 155 ; } 157 } 158 159 165 public void testConstruct_5() 166 throws Exception { 167 Configuration conf = new DefaultConfiguration(); 168 Chromosome[] chroms = new Chromosome[1]; 169 chroms[0] = new Chromosome(conf, new Gene[] { 170 new IntegerGene(conf, 1, 5)}); 171 conf.setFitnessFunction(new StaticFitnessFunction(5)); 172 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 173 try { 174 new Genotype(conf, chroms); 175 fail(); 176 } 177 catch (InvalidConfigurationException invex) { 178 ; } 180 } 181 182 188 public void testConstruct_6() 189 throws Exception { 190 Configuration conf = new DefaultConfiguration(); 191 Chromosome[] chroms = new Chromosome[1]; 192 chroms[0] = new Chromosome(conf, new Gene[] { 193 new IntegerGene(conf, 1, 5)}); 194 conf.setFitnessFunction(new StaticFitnessFunction(5)); 195 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 196 conf.setPopulationSize(7); 197 Genotype genotype = new Genotype(conf, chroms); 198 assertTrue(genotype.getConfiguration().getFitnessEvaluator() 199 instanceof DefaultFitnessEvaluator); 200 assertSame(conf, genotype.getConfiguration()); 201 } 202 203 209 public void testConstruct_7() 210 throws Exception { 211 Configuration conf = new DefaultConfiguration(); 212 conf.setFitnessFunction(new StaticFitnessFunction(5)); 213 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 214 conf.setPopulationSize(7); 215 try { 216 new Genotype(conf, new Chromosome[] {null}); 217 fail(); 218 } 219 catch (IllegalArgumentException illex) { 220 ; } 222 } 223 224 235 public void testConstruct_8() 236 throws Exception { 237 Chromosome[] chroms = new Chromosome[1]; 238 chroms[0] = new Chromosome(conf, new Gene[] { 239 new IntegerGene(conf, 1, 5)}); 240 conf.setFitnessFunction(new StaticFitnessFunction(5)); 241 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 242 conf.setPopulationSize(7); 243 new Genotype(conf, chroms); 244 } 245 246 252 public void testConstruct_9() 253 throws Exception { 254 Chromosome[] chroms = new Chromosome[1]; 255 Configuration conf = new DefaultConfiguration(); 256 conf.setFitnessFunction(new StaticFitnessFunction(5)); 257 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 258 conf.setPopulationSize(7); 259 try { 260 new Genotype(conf, chroms); 261 fail(); 262 } 263 catch (IllegalArgumentException iex) { 264 ; } 266 } 267 268 274 public void testGetChromosomes_0() 275 throws Exception { 276 Configuration conf = new DefaultConfiguration(); 277 conf.setFitnessFunction(new StaticFitnessFunction(5)); 278 Chromosome[] chroms = new Chromosome[1]; 279 Chromosome chrom = new Chromosome(conf, new Gene[] { 280 new IntegerGene(conf, 1, 5)}); 281 chroms[0] = chrom; 282 conf.setSampleChromosome(chrom); 283 conf.setPopulationSize(7); 284 Genotype genotype = new Genotype(conf, chroms); 285 assertEquals(1, genotype.getChromosomes().length); 286 assertEquals(chrom, genotype.getChromosomes()[0]); 287 } 288 289 295 public void testGetFittestChromosome_0() 296 throws Exception { 297 Configuration conf = new DefaultConfiguration(); 298 conf.setFitnessFunction(new StaticFitnessFunction(5)); 299 Chromosome[] chroms = new Chromosome[1]; 300 Chromosome chrom = new Chromosome(conf, new Gene[] { 301 new IntegerGene(conf, 1, 5)}); 302 chroms[0] = chrom; 303 conf.setSampleChromosome(chrom); 304 conf.setPopulationSize(7); 305 Genotype genotype = new Genotype(conf, chroms); 306 privateAccessor.setField(genotype, "m_population", new Population(conf, 1)); 307 assertEquals(null, genotype.getFittestChromosome()); 308 } 309 310 316 public void testGetFittestChromosome_1() 317 throws Exception { 318 Configuration conf = new DefaultConfiguration(); 319 conf.setFitnessFunction(new StaticFitnessFunction(5)); 320 IChromosome[] chroms = new Chromosome[1]; 321 IChromosome chrom = new Chromosome(conf, new Gene[] { 322 new IntegerGene(conf, 1, 5)}); 323 chroms[0] = chrom; 324 conf.setSampleChromosome(chrom); 325 conf.setPopulationSize(7); 326 Genotype genotype = new Genotype(conf, chroms); 327 IChromosome chrom2 = genotype.getFittestChromosome(); 328 assertEquals(chrom, chrom2); 329 } 330 331 337 public void testGetFittestChromosomes_0() 338 throws Exception { 339 Configuration conf = new DefaultConfiguration(); 340 conf.setFitnessFunction(new StaticFitnessFunction(5)); 341 Chromosome[] chroms = new Chromosome[1]; 342 Chromosome chrom = new Chromosome(conf, new Gene[] { 343 new IntegerGene(conf, 1, 5)}); 344 chroms[0] = chrom; 345 conf.setSampleChromosome(chrom); 346 conf.setPopulationSize(7); 347 Genotype genotype = new Genotype(conf, chroms); 348 List l = genotype.getFittestChromosomes(1); 349 Chromosome chrom2 = (Chromosome) l.get(0); 350 assertEquals(chrom, chrom2); 351 } 352 353 361 public void testEvolve_0() 362 throws Exception { 363 Configuration config = new ConfigurationForTest(); 364 config.removeNaturalSelectors(false); 366 config.removeNaturalSelectors(true); 367 config.setKeepPopulationSizeConstant(false); 368 config.addNaturalSelector(new WeightedRouletteSelector(config), true); 370 Genotype genotype = Genotype.randomInitialGenotype(config); 371 int popSize = config.getPopulationSize() 372 * config.getSampleChromosome().getGenes().length; 373 genotype.evolve(1); 374 assertTrue(popSize >= genotype.getPopulation().size()); 375 } 376 377 385 public void testEvolve_1() 386 throws Exception { 387 Configuration config = new ConfigurationForTest() { 390 public synchronized void setFitnessFunction(FitnessFunction 391 a_functionToSet) 392 throws InvalidConfigurationException { 393 setBulkFitnessFunction(new BulkFitnessOffsetRemover(a_functionToSet)); 394 } 395 }; 396 Genotype genotype = Genotype.randomInitialGenotype(config); 397 genotype.evolve(); 400 } 401 402 411 public void testEvolve_2_1() 412 throws Exception { 413 Configuration config = new ConfigurationForTest(); 414 config.setKeepPopulationSizeConstant(false); 415 config.removeNaturalSelectors(false); 417 config.removeNaturalSelectors(true); 418 BestChromosomesSelector bcs = new BestChromosomesSelector(config); 419 bcs.setOriginalRate(1); 420 bcs.setDoubletteChromosomesAllowed(true); 421 config.addNaturalSelector(bcs, false); 422 Genotype genotype = Genotype.randomInitialGenotype(config); 423 int popSize = config.getPopulationSize(); 424 genotype.evolve(); 425 assertEquals(popSize, genotype.getPopulation().size()); 426 } 427 428 435 public void testEvolve_2_2() 436 throws Exception { 437 Configuration config = new ConfigurationForTest(); 438 BestChromosomesSelector bcs = new BestChromosomesSelector(config); 441 bcs.setOriginalRate(1); 442 bcs.setDoubletteChromosomesAllowed(true); 443 config.addNaturalSelector(bcs, false); 444 Genotype genotype = Genotype.randomInitialGenotype(config); 445 int popSize = config.getPopulationSize(); 446 genotype.evolve(); 447 assertEquals(popSize, genotype.getPopulation().size()); 448 } 449 450 458 public void testEvolve_3_1() 459 throws Exception { 460 Configuration config = new ConfigurationForTest(); 461 config.setKeepPopulationSizeConstant(true); 462 Genotype genotype = Genotype.randomInitialGenotype(config); 463 int popSize = config.getPopulationSize(); 464 genotype.evolve(2); 465 assertEquals(popSize, genotype.getPopulation().size()); 466 } 467 468 476 public void testEvolve_3_2() 477 throws Exception { 478 Configuration config = new ConfigurationForTest(); 479 config.setKeepPopulationSizeConstant(!true); 481 Genotype genotype = Genotype.randomInitialGenotype(config); 482 int popSize = config.getPopulationSize(); 483 genotype.evolve(); 484 assertTrue(popSize < genotype.getPopulation().size()); 485 } 486 487 495 public void testEvolve_3_3() 496 throws Exception { 497 Configuration config = new ConfigurationForTest(); 498 config.removeNaturalSelectors(false); 500 config.removeNaturalSelectors(true); 501 try { 502 Genotype.randomInitialGenotype(config); 503 fail(); 504 } catch (InvalidConfigurationException iex) { 505 ; } 507 } 508 509 518 public void testEvolve_5_1() 519 throws Exception { 520 Configuration config = new ConfigurationForTest(); 521 Gene[] genes = new Gene[] { 522 new BooleanGene(conf)}; 523 Configuration.resetProperty(Configuration.PROPERTY_SAMPLE_CHROM_INST); 524 config.setSampleChromosome(new ChromosomeForTest(config, genes)); 525 config.setPreservFittestIndividual(true); 526 config.setKeepPopulationSizeConstant(true); 527 BestChromosomesSelector sel = (BestChromosomesSelector) config. 528 getNaturalSelector(true, 0); 529 sel.setDoubletteChromosomesAllowed(true); 530 config.getGeneticOperators().clear(); 531 SwappingMutationOperator op = new SwappingMutationOperator(config, 1); 532 op.setStartOffset(0); config.addGeneticOperator(op); 534 assertTrue(doTestEvolve_5(config) > 0); 535 } 536 537 546 public void testEvolve_5_2() 547 throws Exception { 548 Configuration config = new ConfigurationForTest(); 549 Gene[] genes = new Gene[] { 550 new BooleanGene(conf)}; 551 Configuration.resetProperty(Configuration.PROPERTY_SAMPLE_CHROM_INST); 552 config.setSampleChromosome(new ChromosomeForTest(config, genes)); 553 config.setPreservFittestIndividual(true); 554 config.setKeepPopulationSizeConstant(true); 555 BestChromosomesSelector sel = (BestChromosomesSelector) config. 556 getNaturalSelector(true, 0); 557 sel.setDoubletteChromosomesAllowed(true); 558 config.getGeneticOperators().clear(); 559 config.addGeneticOperator(new MutationOperator(config, 1)); 560 assertTrue(doTestEvolve_5(config) > 0); 561 } 562 563 573 public void testEvolve_5_3() 574 throws Exception { 575 Configuration config = new ConfigurationForTest(); 576 Gene[] genes = new Gene[] { 577 new BooleanGene(conf)}; 578 Configuration.resetProperty(Configuration.PROPERTY_SAMPLE_CHROM_INST); 579 config.setSampleChromosome(new ChromosomeForTest(config, genes)); 580 config.setPreservFittestIndividual(true); 581 config.setKeepPopulationSizeConstant(true); 582 BestChromosomesSelector sel = (BestChromosomesSelector) config. 583 getNaturalSelector(true, 0); 584 sel.setDoubletteChromosomesAllowed(true); 585 config.getGeneticOperators().clear(); 586 config.addGeneticOperator(new MutationOperator(config, 0)); 587 assertEquals(0, doTestEvolve_5(config)); 588 } 589 590 600 private int doTestEvolve_5(Configuration config) 601 throws Exception { 602 Genotype genotype = Genotype.randomInitialGenotype(config); 603 genotype.evolve(2); 604 ChromosomeForTest chrom = (ChromosomeForTest) genotype.getPopulation(). 608 getChromosome(0); 609 chrom.resetComputedTimes(); 610 for (int i = 0; i < genotype.getPopulation().size(); i++) { 612 chrom = (ChromosomeForTest) genotype.getPopulation().getChromosome(i); 613 chrom.resetIsCloned(); 614 } 615 genotype.evolve(2); 617 chrom = (ChromosomeForTest) genotype.getPopulation(). 621 getChromosome(0); 622 return chrom.getComputedTimes(); 623 } 624 625 633 public void testEvolve_6() 634 throws Exception { 635 Configuration config = new ConfigurationForTest(); 636 Genotype genotype = Genotype.randomInitialGenotype(config); 637 genotype.setPopulation(null); 638 try { 639 genotype.evolve(); 640 fail(); 641 } 642 catch (NullPointerException iex) { 643 ; } 645 } 646 647 655 public void testEvolve_7() 656 throws Exception { 657 Configuration config = new ConfigurationForTest(); 658 config.setMinimumPopSizePercent(290); 659 config.setKeepPopulationSizeConstant(!true); 661 config.setPopulationSize(10); 662 config.setPreservFittestIndividual(false); 663 Genotype genotype = Genotype.randomInitialGenotype(config); 664 genotype.evolve(1); 665 assertEquals( (int) (10 * 290.0 / 100), genotype.getPopulation().size()); 666 } 667 668 676 public void testEvolve_8() 677 throws Exception { 678 Configuration config = new ConfigurationForTest(); 679 config.setMinimumPopSizePercent(290); 680 config.setKeepPopulationSizeConstant(!true); 682 config.setPopulationSize(10); 683 config.setPreservFittestIndividual(true); 684 Genotype genotype = Genotype.randomInitialGenotype(config); 685 genotype.evolve(1); 686 IChromosome fittest = genotype.getFittestChromosome(); 687 genotype.evolve(1); 688 assertEquals(fittest, genotype.getFittestChromosome()); 689 } 690 691 699 public void testEvolve_9() 700 throws Exception { 701 Configuration config = new ConfigurationForTest(); 702 config.setMinimumPopSizePercent(290); 703 config.setKeepPopulationSizeConstant(!true); 705 config.setPopulationSize(10); 706 config.setPreservFittestIndividual(true); 707 Genotype genotype = Genotype.randomInitialGenotype(config); 708 genotype.evolve(1); 709 } 710 711 717 public void testToString_0() 718 throws Exception { 719 Configuration conf = new DefaultConfiguration(); 720 conf.setFitnessFunction(new StaticFitnessFunction(5)); 721 Chromosome[] chroms = new Chromosome[1]; 722 Chromosome chrom = new Chromosome(conf, new Gene[] { 723 new IntegerGene(conf, 1, 55)}); 724 chroms[0] = chrom; 725 conf.setSampleChromosome(chrom); 726 conf.setPopulationSize(7); 727 Genotype genotype = new Genotype(conf, chroms); 728 assertTrue(genotype.toString() != null); 729 assertTrue(genotype.toString().length() > 0); 730 assertEquals(IChromosome.S_SIZE + ":1, " 731 + IChromosome.S_FITNESS_VALUE + ":" 732 + FitnessFunction.NO_FITNESS_VALUE 733 + ", " 734 + IChromosome.S_ALLELES + ":[IntegerGene(1,55)=null], " 735 + IChromosome.S_APPLICATION_DATA + ":null" 736 + " [" 737 + FitnessFunction.NO_FITNESS_VALUE 738 + "]\n", genotype.toString()); 739 } 740 741 748 public void testToString_1() 749 throws Exception { 750 final double fitnessvalue = 5.0d; 751 Configuration conf = new DefaultConfiguration(); 752 conf.setFitnessFunction(new StaticFitnessFunction(fitnessvalue)); 753 Chromosome[] chroms = new Chromosome[1]; 754 Chromosome chrom = new Chromosome(conf, new Gene[] { 755 new IntegerGene(conf, 1, 55)}); 756 chroms[0] = chrom; 757 conf.setSampleChromosome(chrom); 758 conf.setPopulationSize(7); 759 Genotype genotype = new Genotype(conf, chroms); 760 assertTrue(genotype.toString() != null); 761 assertTrue(genotype.toString().length() > 0); 762 genotype.getFittestChromosome(); 764 assertEquals(IChromosome.S_SIZE + ":1, " 765 + IChromosome.S_FITNESS_VALUE + ":" 766 + fitnessvalue 767 + ", " 768 + IChromosome.S_ALLELES + ":[IntegerGene(1,55)=null], " 769 + IChromosome.S_APPLICATION_DATA + ":null" 770 + " [" 771 + fitnessvalue 772 + "]\n", genotype.toString()); 773 } 774 775 781 public void testRandomInitialGenotype_0() 782 throws Exception { 783 try { 784 Genotype.randomInitialGenotype(null); 785 fail(); 786 } 787 catch (IllegalArgumentException illex) { 788 ; } 790 } 791 792 798 public void testRandomInitialGenotype_1() 799 throws Exception { 800 Configuration conf = new DefaultConfiguration(); 801 Chromosome chrom = new Chromosome(conf, new Gene[] { 802 new IntegerGene(conf, 1, 9999)}); 803 conf.setPopulationSize(7777); 804 conf.setFitnessFunction(new StaticFitnessFunction(5)); 805 conf.setSampleChromosome(chrom); 806 Genotype genotype = Genotype.randomInitialGenotype(conf); 807 assertEquals(7777, genotype.getChromosomes().length); 808 } 809 810 817 public void testRandomInitialGenotype_2() 818 throws Exception { 819 Configuration conf = new DefaultConfiguration(); 820 Gene aGene = new IntegerGene(conf, 1, 9999); 821 aGene.setEnergy(22.3d); 822 Chromosome chrom = new ChromosomeForTest2(conf, new Gene[] {aGene}); 823 conf.setPopulationSize(7777); 824 conf.setFitnessFunction(new StaticFitnessFunction(5)); 825 conf.setSampleChromosome(chrom); 826 Genotype genotype = Genotype.randomInitialGenotype(conf); 827 assertEquals(7777, genotype.getPopulation().size()); 828 Chromosome c = (Chromosome) genotype.getPopulation().getChromosome(0); 829 Gene g = c.getGene(0); 830 assertEquals(aGene.getEnergy(), g.getEnergy(), DELTA); 831 } 832 833 839 public void testRandomInitialGenotype_3() 840 throws Exception { 841 Configuration conf = new DefaultConfiguration(); 842 IChromosome chrom = new MyChromosome(conf); 843 conf.setPopulationSize(7777); 844 conf.setFitnessFunction(new StaticFitnessFunction(5)); 845 conf.setSampleChromosome(chrom); 846 try { 847 Genotype.randomInitialGenotype(conf); 848 fail(); 849 } 850 catch (IllegalStateException iex) { 851 ; } 853 } 854 855 862 public void testRandomInitialGenotype_4() 863 throws Exception { 864 Configuration config = new ConfigurationForTest(); 865 config.getGeneticOperators().clear(); 867 config.addNaturalSelector(new WeightedRouletteSelector(), true); 868 try { 869 Genotype.randomInitialGenotype(config); 870 fail(); 871 } 872 catch (InvalidConfigurationException iex) { 873 ; } 875 } 876 877 883 public void testEquals_0() 884 throws Exception { 885 Configuration conf = new DefaultConfiguration(); 886 conf.setFitnessFunction(new StaticFitnessFunction(5)); 887 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 888 conf.setPopulationSize(99999); 889 Chromosome[] chroms = new Chromosome[1]; 890 chroms[0] = new Chromosome(conf, new Gene[] {new IntegerGene(conf, 1, 5)}); 891 Genotype genotype = new Genotype(conf, chroms); 892 assertNotNull(genotype); 893 Genotype genotype2 = new Genotype(conf, chroms); 894 assertTrue(genotype.equals(genotype2)); 895 assertEquals(genotype.toString(), genotype2.toString()); 896 assertEquals(genotype.toString(), genotype2.toString()); 897 assertFalse(genotype.equals(new Chromosome(conf))); 898 } 899 900 906 public void testEquals_1() 907 throws Exception { 908 Configuration conf = new DefaultConfiguration(); 909 conf.setFitnessFunction(new StaticFitnessFunction(5)); 910 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 911 conf.setPopulationSize(99999); 912 Chromosome[] chroms = new Chromosome[1]; 913 chroms[0] = new Chromosome(conf, new Gene[] { 914 new IntegerGene(conf, 1, 5)}); 915 Genotype genotype = new Genotype(conf, chroms); 916 Chromosome[] chroms2 = new Chromosome[2]; 917 chroms2[0] = new Chromosome(conf, new Gene[] {new IntegerGene(conf, 1, 5)}); 918 chroms2[1] = new Chromosome(conf, new Gene[] {new IntegerGene(conf, 2, 4)}); 919 Genotype genotype2 = new Genotype(conf, chroms2); 920 assertFalse(genotype.equals(genotype2)); 921 } 922 923 929 public void testEquals_2() 930 throws Exception { 931 Configuration conf = new DefaultConfiguration(); 932 conf.setFitnessFunction(new StaticFitnessFunction(5)); 933 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 934 conf.setPopulationSize(99999); 935 Chromosome[] chroms = new Chromosome[1]; 936 chroms[0] = new Chromosome(conf, new Gene[] { 937 new IntegerGene(conf, 1, 5)}); 938 Genotype genotype = new Genotype(conf, chroms); 939 assertFalse(genotype.equals(null)); 940 } 941 942 951 public void testHashcode_0() 952 throws Exception { 953 Configuration conf = new DefaultConfiguration(); 954 conf.setFitnessFunction(new StaticFitnessFunction(5)); 955 conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 9)); 956 conf.setPopulationSize(99999); 957 Chromosome[] chroms = new Chromosome[1]; 958 chroms[0] = new Chromosome(conf, new Gene[] { 959 new IntegerGene(conf, 1, 5)}); 960 Genotype genotype = new Genotype(conf, chroms); 961 genotype.hashCode(); 962 } 963 964 private final static int MAX_CHROMOSOME_TO_TEST = 1000; 965 966 private final static int MAX_GENES_TO_TEST = 25; 967 968 private final static int MAX_GENES_TYPES = 6; 969 970 public void testHashcode_1() 971 throws Exception { 972 int Count; 973 int NumGenes; 974 int GeneCount; 975 int GeneType; 976 Gene[] genes; 977 Chromosome chrom; 978 TestHashcode thc = new TestHashcode(); 979 thc.setVerbose(!true); 980 List UniqueChromosome = new ArrayList(); 981 List EqualChromosome = new ArrayList(); 982 Genotype geno; 983 for (Count = 0; Count < MAX_CHROMOSOME_TO_TEST; Count++) { 985 NumGenes = (int) (Math.random() * MAX_GENES_TO_TEST) + 1; 986 genes = new Gene[NumGenes]; 987 for (GeneCount = 0; GeneCount < NumGenes; GeneCount++) { 988 GeneType = (int) (Math.random() * MAX_GENES_TYPES); 989 switch (GeneType) { 990 case 0: 991 genes[GeneCount] = new IntegerGene(conf); 992 break; 993 case 1: 994 genes[GeneCount] = new BooleanGene(conf); 995 break; 996 case 2: 997 genes[GeneCount] = new CompositeGene(conf); 998 break; 999 case 3: 1000 genes[GeneCount] = new DoubleGene(conf); 1001 break; 1002 case 4: 1003 genes[GeneCount] = new FixedBinaryGene(conf, 5); 1004 break; 1005 case 5: 1006 genes[GeneCount] = new StringGene(conf); 1007 break; 1008 } 1009 } 1010 Configuration.reset(); 1011 Configuration conf = new DefaultConfiguration(); 1012 chrom = new Chromosome(conf, genes); 1013 conf.setFitnessFunction(new StaticFitnessFunction(0.5d)); 1014 conf.setSampleChromosome(chrom); 1015 conf.setPopulationSize(5); 1016 Population pop = new Population(conf); 1017 pop.addChromosome(chrom); 1018 geno = new Genotype(conf, pop); 1019 if (UniqueChromosome.contains(geno) == false) { 1022 UniqueChromosome.add(geno); 1023 } 1024 } 1025 thc.setFractionUnique(.95); 1027 if (!thc.testHashCodeUniqueness(UniqueChromosome)) { 1028 System.out.println( 1029 "testHashCodeUniqueness failed\n Actual Percent unique = " + 1030 thc.getActualFractionUnique()); 1031 fail(); 1032 } 1033 1044 for (Count = 0; Count < 3; Count++) { 1046 genes = new Gene[1]; 1047 genes[0] = new IntegerGene(conf); 1048 Configuration.reset(); 1049 Configuration conf = new DefaultConfiguration(); 1050 chrom = new Chromosome(conf, genes); 1051 conf.setFitnessFunction(new StaticFitnessFunction(0.5d)); 1052 conf.setSampleChromosome(chrom); 1053 conf.setPopulationSize(5); 1054 Population pop = new Population(conf); 1055 pop.addChromosome(chrom); 1056 geno = new Genotype(conf, pop); 1057 EqualChromosome.add(geno); 1058 } 1059 if (!thc.testHashCodeEquality(EqualChromosome)) { 1061 fail(); 1062 } 1063 } 1064 1065 1072 1121 1128 public void testIsSerializable_0() 1129 throws Exception { 1130 Configuration conf = new ConfigurationForTest(); 1131 Chromosome[] chroms = new Chromosome[1]; 1132 chroms[0] = new Chromosome(conf, new Gene[] { 1133 new IntegerGene(conf, 1, 5)}); 1134 assertTrue(isSerializable(new Genotype(conf, chroms))); 1135 } 1136 1137 1144 public void testdoSerialize_0() 1145 throws Exception { 1146 Configuration conf = new ConfigurationForTest(); 1148 Chromosome[] chroms = new Chromosome[1]; 1149 chroms[0] = new Chromosome(conf, new Gene[] { 1150 new IntegerGene(conf, 1, 5)}); 1151 Genotype genotype = new Genotype(conf, chroms); 1152 assertEquals(genotype, super.doSerialize(genotype)); 1155 } 1156 1157 public class ChromosomeForTest2 1158 extends ChromosomeForTest { 1159 public ChromosomeForTest2(Configuration a_config, 1160 final Gene[] a_initialGenes) 1161 throws InvalidConfigurationException { 1162 super(a_config, a_initialGenes); 1163 } 1164 1165 public boolean isHandlerFor(Class a_class) { 1166 if (a_class == ChromosomeForTest2.class) { 1167 return true; 1168 } 1169 else { 1170 return false; 1171 } 1172 } 1173 1174 public Object perform(Object a_obj, Class a_class, Object a_params) 1175 throws Exception { 1176 return randomInitialChromosome2(); 1177 } 1178 } 1179 public class MyChromosome 1180 implements IChromosome { 1181 private transient Configuration m_conf; 1182 1183 public MyChromosome(Configuration a_conf) { 1184 m_conf = a_conf; 1185 } 1186 1187 public Gene getGene(int a_desiredLocus) { 1188 try { 1189 return new IntegerGene(m_conf); 1190 } 1191 catch (InvalidConfigurationException iex) { 1192 throw new IllegalStateException (iex.getMessage()); 1193 } 1194 } 1195 1196 public Gene[] getGenes() { 1197 return new Gene[] {}; 1198 } 1199 1200 public int size() { 1201 return 1; 1202 } 1203 1204 public void setFitnessValue(double a_newFitnessValue) { 1205 } 1206 1207 public double getFitnessValue() { 1208 return 0; 1209 } 1210 1211 public void setFitnessValueDirectly(double a_newFitnessValue) { 1212 } 1213 1214 public double getFitnessValueDirectly() { 1215 return getFitnessValue(); 1216 } 1217 1218 public int compareTo(Object other) { 1219 return 0; 1220 } 1221 1222 public void setGenes(Gene[] a_genes) 1223 throws InvalidConfigurationException { 1224 } 1225 1226 public void setIsSelectedForNextGeneration(boolean a_isSelected) { 1227 } 1228 1229 public boolean isSelectedForNextGeneration() { 1230 return true; 1231 } 1232 1233 public Object clone() { 1234 return null; 1235 } 1236 1237 public void setConstraintChecker(IGeneConstraintChecker a_constraintChecker) 1238 throws InvalidConfigurationException { 1239 } 1240 1241 public void setApplicationData(Object a_newData) { 1242 } 1243 1244 public Object getApplicationData() { 1245 return null; 1246 } 1247 1248 public void cleanup() { 1249 } 1250 1251 public Configuration getConfiguration() { 1252 return m_conf; 1253 } 1254 } 1255} 1256 | Popular Tags |