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 ChromosomeTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.61 $"; 27 28 public static Test suite() { 29 return new TestSuite(ChromosomeTest.class); 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 45 public void testConstruct_0() 46 throws Exception { 47 try { 48 new Chromosome(conf, null, 1); 49 fail(); 50 } catch (IllegalArgumentException iex) { 51 ; } 53 } 54 55 63 public void testConstruct_0_2() 64 throws Exception { 65 try { 66 new Chromosome(conf, null, 1, new MyConstraintChecker()); 67 fail(); 68 } catch (IllegalArgumentException iex) { 69 ; } 71 } 72 73 80 public void testConstruct_1() 81 throws Exception { 82 try { 83 new Chromosome(conf, new IntegerGene(conf), 0); 84 fail(); 85 } catch (IllegalArgumentException iex) { 86 ; } 88 } 89 90 98 public void testConstruct_1_2() 99 throws Exception { 100 try { 101 new Chromosome(conf, new IntegerGene(conf), -1); 102 fail(); 103 } catch (IllegalArgumentException iex) { 104 ; } 106 } 107 108 116 public void testConstruct_1_3() 117 throws Exception { 118 try { 119 new Chromosome(new ConfigurationForTest(), new IntegerGene(conf), -500); 120 fail(); 121 } catch (IllegalArgumentException iex) { 122 ; } 124 } 125 126 133 public void testConstruct_2() 134 throws Exception { 135 try { 136 new Chromosome(conf, null, 0); 137 fail(); 138 } catch (IllegalArgumentException iex) { 139 ; } 141 } 142 143 151 public void testConstruct_2_2() 152 throws Exception { 153 try { 154 new Chromosome(conf, null, 0, null); 155 fail(); 156 } catch (IllegalArgumentException iex) { 157 ; } 159 } 160 161 168 public void testConstruct_3() 169 throws Exception { 170 Chromosome chrom = new Chromosome(conf, new IntegerGene(conf), 1); 171 assertEquals(1, chrom.size()); 172 chrom = new Chromosome(conf, new IntegerGene(conf), 1, null); 173 assertEquals(1, chrom.size()); 174 assertNull(chrom.getConstraintChecker()); 175 IGeneConstraintChecker cc = new MyConstraintChecker(); 176 chrom = new Chromosome(conf, new IntegerGene(conf), 1, cc); 177 assertEquals(1, chrom.size()); 178 assertEquals(cc, chrom.getConstraintChecker()); 179 } 180 181 188 public void testConstruct_5_2() 189 throws Exception { 190 try { 191 Gene[] genes = new IntegerGene[1]; 192 genes[0] = null; 193 new Chromosome(conf, genes); 194 fail(); 195 } catch (IllegalArgumentException iex) { 196 ; } 198 } 199 200 209 public void testConstruct_5_3() 210 throws Exception { 211 try { 212 Gene[] genes = new Gene[2]; 213 genes[0] = new IntegerGene(); 214 genes[1] = new DoubleGene(); 215 IGeneConstraintChecker cc = new MyConstraintChecker(DoubleGene.class); 216 new Chromosome(conf, genes, cc); 217 fail(); 218 } catch (IllegalArgumentException iex) { 219 fail(); 220 } catch (InvalidConfigurationException cex) { 221 ; } 223 } 224 225 232 public void testConstruct_6() 233 throws Exception { 234 try { 235 new Chromosome(conf, (Gene[])null); 236 fail(); 237 } catch (IllegalArgumentException illex) { 238 ; } 240 } 241 242 250 public void testConstruct_7_1() 251 throws Exception { 252 try { 253 new Chromosome(conf, 0); 254 fail(); 255 } catch (IllegalArgumentException illex) { 256 ; } 258 } 259 260 268 public void testConstruct_7_2() 269 throws Exception { 270 try { 271 new Chromosome(conf, -5); 272 fail(); 273 } catch (IllegalArgumentException illex) { 274 ; } 276 } 277 278 284 public void testConstruct_7_3() 285 throws Exception { 286 Chromosome chrom = new Chromosome(conf, 5); 287 assertEquals(5, chrom.getGenes().length); 288 for (int i = 0; i < 5; i++) { 289 assertEquals(null, chrom.getGene(i)); 290 } 291 } 292 293 300 public void testConstruct_8() 301 throws Exception { 302 try { 303 Gene[] genes = new IntegerGene[2]; 304 genes[0] = new IntegerGene(conf); 305 genes[1] = null; 306 new Chromosome(conf, genes); 307 fail(); 308 } catch (IllegalArgumentException illex) { 309 ; } 311 } 312 313 320 public void testConstruct_9() 321 throws Exception { 322 Configuration conf = new DefaultConfiguration(); 323 Gene[] genes1 = new Gene[2]; 324 genes1[0] = new IntegerGene(conf); 325 genes1[1] = new BooleanGene(conf); 326 Chromosome chrom = new Chromosome(conf, genes1); 327 String repr = chrom.getPersistentRepresentation(); 328 Chromosome chrom2 = new Chromosome(conf, repr); 329 assertEquals(chrom, chrom2); 330 assertEquals(chrom.getPersistentRepresentation(), 331 chrom2.getPersistentRepresentation()); 332 } 333 334 339 public void testConstruct_10() 340 throws Exception { 341 Configuration conf = new DefaultConfiguration(); 342 Gene[] genes = new IntegerGene[2]; 343 genes[0] = new IntegerGene(conf); 344 genes[1] = new IntegerGene(conf); 345 Chromosome chrom = new Chromosome(conf, genes); 346 assertEquals(2, chrom.size()); 347 assertEquals(genes[0], chrom.getGene(0)); 348 assertEquals(genes[1], chrom.getGene(1)); 349 } 350 351 356 public void testConstruct_11() 357 throws Exception { 358 Configuration conf = new DefaultConfiguration(); 359 Gene[] genes = new BooleanGene[3]; 360 genes[0] = new BooleanGene(conf); 361 genes[1] = new BooleanGene(conf); 362 genes[2] = new BooleanGene(conf); 363 genes[2].setAllele(Boolean.valueOf(true)); 364 conf.setFitnessFunction(new RandomFitnessFunction()); 365 Chromosome chrom = new Chromosome(conf, genes); 366 assertEquals(3, chrom.size()); 367 assertEquals(genes[0], chrom.getGene(0)); 368 assertEquals(genes[1], chrom.getGene(1)); 369 assertEquals(genes[2], chrom.getGene(2)); 370 } 371 372 377 public void testConstruct_12() 378 throws Exception { 379 Configuration conf = new DefaultConfiguration(); 380 Gene[] genes = new IntegerGene[2]; 381 genes[0] = new IntegerGene(conf); 382 genes[1] = new IntegerGene(conf); 383 conf.setFitnessFunction(new RandomFitnessFunction()); 384 Chromosome chrom2 = new Chromosome(conf, genes); 385 conf.setSampleChromosome(chrom2); 386 new Chromosome(conf, genes); 387 } 388 389 394 public void testConstruct_14() 395 throws Exception { 396 Gene gene = new BooleanGene(conf); 397 Chromosome chrom = new Chromosome(conf, gene, 7); 398 Gene[] genes = chrom.getGenes(); 399 assertEquals(7, genes.length); 400 Gene sample; 401 for (int i = 0; i < genes.length; i++) { 402 sample = (Gene) genes[i]; 403 assertEquals(gene, sample); 404 } 405 } 406 407 415 public void testClone_5() 416 throws Exception { 417 Configuration conf = new DefaultConfiguration(); 418 conf.setFitnessFunction(new RandomFitnessFunction()); 419 Chromosome chrom = new Chromosome(conf); 420 assertEquals(0, chrom.size()); 421 Chromosome chrom2 = (Chromosome) chrom.clone(); 422 assertEquals(chrom, chrom2); 423 conf.getChromosomePool().releaseChromosome(chrom); 424 assertEquals(Chromosome.class, chrom.clone().getClass()); 425 chrom.cleanup(); 426 conf.setChromosomePool(null); 427 } 428 429 437 public void testClone_6() 438 throws Exception { 439 Configuration conf = new DefaultConfiguration(); 440 conf.setFitnessFunction(new RandomFitnessFunction()); 441 Gene[] genes = new IntegerGene[2]; 442 genes[0] = new IntegerGene(conf); 443 genes[1] = new IntegerGene(conf); 444 Chromosome chrom = new Chromosome(conf, genes); 445 chrom.cleanup(); 446 assertEquals(2, chrom.size()); 447 Chromosome chrom2 = (Chromosome) chrom.clone(); 448 assertEquals(chrom, chrom2); 449 Chromosome chrom3 = (Chromosome) chrom.clone(); 450 assertEquals(chrom3, chrom2); 451 } 452 453 460 public void testClone_1() 461 throws InvalidConfigurationException { 462 Gene[] genes = new Gene[2]; 463 genes[0] = new IntegerGene(conf); 464 genes[1] = new IntegerGene(conf); 465 Configuration conf = new DefaultConfiguration(); 466 conf.setFitnessFunction(new StaticFitnessFunction(20)); 467 Chromosome chrom3 = new Chromosome(conf, genes); 468 conf.setSampleChromosome(chrom3); 469 conf.setPopulationSize(5); 470 Chromosome chrom = new Chromosome(conf, genes); 471 Chromosome chrom2 = (Chromosome) chrom.clone(); 472 assertEquals(chrom.hashCode(), chrom2.hashCode()); 473 assertEquals(chrom.getFitnessValue(), chrom2.getFitnessValue(), DELTA); 474 assertEquals(chrom.isSelectedForNextGeneration(), 475 chrom2.isSelectedForNextGeneration()); 476 assertEquals(chrom.size(), chrom2.size()); 477 assertEquals(chrom.getGene(0), chrom2.getGene(0)); 478 assertEquals(chrom.getGene(1), chrom2.getGene(1)); 479 assertEquals(chrom.getGenes().getClass(), chrom2.getGenes().getClass()); 480 assertEquals(chrom.toString(), chrom2.toString()); 481 assertTrue(chrom.equals(chrom2)); 482 assertNotSame(chrom.getGenes(), chrom2.getGenes()); 483 assertNotSame(chrom.getGene(0), chrom2.getGene(0)); 484 assertNotSame(chrom.getGene(1), chrom2.getGene(1)); 485 } 486 487 498 public void testClone_2() 499 throws InvalidConfigurationException { 500 Configuration conf = new DefaultConfiguration(); 501 Gene[] genes = new IntegerGene[2]; 502 genes[0] = new IntegerGene(conf); 503 genes[1] = new IntegerGene(conf); 504 conf.setFitnessFunction(new StaticFitnessFunction(20)); 505 Chromosome chrom2 = new Chromosome(conf, genes); 506 conf.setSampleChromosome(chrom2); 507 conf.setPopulationSize(5); 508 Chromosome chrom = new Chromosome(conf, genes); 509 Object appObj = new MyAppObject(); 510 chrom.setApplicationData(appObj); 511 Chromosome cloned = (Chromosome) chrom.clone(); 512 assertSame(appObj, cloned.getApplicationData()); 513 } 514 515 524 public void testClone_3() 525 throws InvalidConfigurationException { 526 Configuration conf = new DefaultConfiguration(); 527 Gene[] genes = new IntegerGene[2]; 528 genes[0] = new IntegerGene(conf); 529 genes[1] = new IntegerGene(conf); 530 conf.setFitnessFunction(new StaticFitnessFunction(20)); 531 Chromosome chrom2 = new Chromosome(conf, genes); 532 conf.setSampleChromosome(chrom2); 533 conf.setPopulationSize(5); 534 Chromosome chrom = new Chromosome(conf, genes); 535 Object appObj = new MyAppObject2(); 536 chrom.setApplicationData(appObj); 537 chrom2 = (Chromosome) chrom.clone(); 538 assertTrue(chrom.equals(chrom2)); 539 assertEquals(appObj, chrom2.getApplicationData()); 540 assertFalse(appObj == chrom2.getApplicationData()); 541 } 542 543 551 public void testClone_4() 552 throws InvalidConfigurationException { 553 Configuration conf = new DefaultConfiguration(); 554 Gene[] genes = new Gene[2]; 555 genes[0] = new IntegerGene(conf); 556 genes[0].setEnergy(47.11d); 557 genes[1] = new IntegerGene(conf); 558 genes[1].setEnergy(8.15d); 559 conf.setFitnessFunction(new StaticFitnessFunction(20)); 560 Chromosome chrom3 = new Chromosome(conf, genes); 561 conf.setSampleChromosome(chrom3); 562 conf.setPopulationSize(5); 563 Chromosome chrom = new Chromosome(conf, genes); 564 Chromosome chrom2 = (Chromosome) chrom.clone(); 565 Gene[] clonedGenes = chrom2.getGenes(); 566 assertEquals(genes[0].getEnergy(), clonedGenes[0].getEnergy(), DELTA); 567 assertEquals(genes[1].getEnergy(), clonedGenes[1].getEnergy(), DELTA); 568 } 569 570 private final static int MAX_CHROMOSOME_TO_TEST = 1000; 571 572 private final static int MAX_GENES_TO_TEST = 25; 573 574 private final static int MAX_GENES_TYPES = 6; 575 576 586 public void testHashCode_0() 587 throws InvalidConfigurationException { 588 int count; 589 int numGenes; 590 int geneCount; 591 int geneType; 592 Gene[] genes; 593 Chromosome chrom; 594 TestHashcode thc = new TestHashcode(); 595 thc.setVerbose(!true); 596 List uniqueChromosome = new ArrayList(); 597 List equalChromosome = new ArrayList(); 598 for (count = 0; count < MAX_CHROMOSOME_TO_TEST; count++) { 600 numGenes = (int) (Math.random() * (MAX_GENES_TO_TEST - 1)) + 1; 601 genes = new Gene[numGenes]; 602 for (geneCount = 0; geneCount < numGenes; geneCount++) { 603 geneType = (int) (Math.random() * MAX_GENES_TYPES); 604 switch (geneType) { 605 case 0: 606 genes[geneCount] = new IntegerGene(conf); 607 break; 608 case 1: 609 genes[geneCount] = new BooleanGene(conf); 610 break; 611 case 2: 612 genes[geneCount] = new CompositeGene(conf); 613 break; 614 case 3: 615 genes[geneCount] = new DoubleGene(conf); 616 break; 617 case 4: 618 genes[geneCount] = new FixedBinaryGene(conf, 5); 619 break; 620 case 5: 621 genes[geneCount] = new StringGene(conf); 622 break; 623 } 624 } 625 chrom = new Chromosome(new ConfigurationForTest(), genes); 626 if (!uniqueChromosome.contains(chrom)) { 629 uniqueChromosome.add(chrom); 630 } 631 } 632 thc.setFractionUnique(.95); 634 if (!thc.testHashCodeUniqueness(uniqueChromosome)) { 635 System.out.println( 636 "testHashCodeUniqueness failed\n Actual Percent unique = " + 637 thc.getActualFractionUnique()); 638 fail(); 639 } 640 thc.setAverageMax(2100000000); 644 thc.setAverageMin( -140000000); 645 thc.setStdDevMax(2100000000); 646 thc.setStdDevMin(9000000); 647 if (!thc.testDispersion(uniqueChromosome)) { 648 fail(); 649 } 650 for (count = 0; count < 3; count++) { 652 genes = new Gene[1]; 653 genes[0] = new IntegerGene(conf); 654 chrom = new Chromosome(new ConfigurationForTest(), genes); 655 equalChromosome.add(chrom); 656 } 657 if (!thc.testHashCodeEquality(equalChromosome)) { 659 fail(); 660 } 661 } 662 663 669 public void testEquals_0() 670 throws Exception { 671 Gene[] genes = new IntegerGene[2]; 672 genes[0] = new IntegerGene(conf); 673 genes[1] = new IntegerGene(conf); 674 Chromosome chrom = new Chromosome(conf, genes); 675 assertTrue(chrom.equals(chrom)); 676 Chromosome chrom2 = new Chromosome(conf, genes); 677 assertTrue(chrom.equals(chrom2)); 678 } 679 680 686 public void testEquals_1() 687 throws Exception { 688 Gene[] genes = new IntegerGene[2]; 689 genes[0] = new IntegerGene(conf); 690 genes[1] = new IntegerGene(conf); 691 Chromosome chrom = new Chromosome(conf, genes); 692 assertTrue(chrom.equals(chrom)); 693 genes = new BooleanGene[2]; 694 genes[0] = new BooleanGene(conf); 695 genes[1] = new BooleanGene(conf); 696 Chromosome chrom2 = new Chromosome(conf, genes); 697 assertFalse(chrom.equals(chrom2)); 698 } 699 700 706 public void testEquals_2() 707 throws Exception { 708 Gene[] genes = new IntegerGene[2]; 709 Gene gen0 = new IntegerGene(conf); 710 gen0.setAllele(new Integer (1)); 711 Gene gen1 = new IntegerGene(conf); 712 gen1.setAllele(new Integer (2)); 713 genes[0] = gen0; 714 genes[1] = gen1; 715 Chromosome chrom = new Chromosome(conf, genes); 716 assertTrue(chrom.equals(chrom)); 717 genes = new IntegerGene[2]; 718 gen0 = new IntegerGene(conf); 719 gen0.setAllele(new Integer (1)); 720 gen1 = new IntegerGene(conf); 721 gen1.setAllele(new Integer (3)); 722 genes[0] = gen0; 723 genes[1] = gen1; 724 Chromosome chrom2 = new Chromosome(conf, genes); 725 assertFalse(chrom.equals(chrom2)); 726 gen1.setAllele(new Integer (2)); 727 assertTrue(chrom.equals(chrom2)); 728 gen0.setAllele(new Integer (2)); 729 assertFalse(chrom.equals(chrom2)); 730 gen1.setAllele(new Integer (1)); 731 assertFalse(chrom.equals(chrom2)); 732 } 733 734 740 public void testEquals_3() 741 throws Exception { 742 Gene[] genes = new IntegerGene[2]; 743 genes[0] = new IntegerGene(conf); 744 genes[1] = new IntegerGene(conf); 745 Chromosome chrom = new Chromosome(conf, genes); 746 assertFalse(chrom.equals(null)); 747 assertFalse(chrom.equals(genes)); 750 } 751 752 758 public void testGetFitnessValue_0() 759 throws Exception { 760 Configuration conf = new DefaultConfiguration(); 761 Gene[] genes = new IntegerGene[2]; 762 genes[0] = new IntegerGene(conf); 763 genes[1] = new IntegerGene(conf); 764 StaticFitnessFunction ff = new StaticFitnessFunction(20); 765 conf.setFitnessFunction(ff); 766 Chromosome chrom = new Chromosome(conf, genes); 767 conf.setSampleChromosome(chrom); 768 conf.setPopulationSize(5); 769 chrom = new Chromosome(conf, genes); 770 assertEquals(ff.getStaticFitnessValue(), chrom.getFitnessValue(), DELTA); 771 assertEquals(ff.getStaticFitnessValue(), chrom.getFitnessValue(), DELTA); 773 } 774 775 781 public void testGetFitnessValue_1() 782 throws Exception { 783 Configuration conf = new DefaultConfiguration(); 784 Gene[] genes = new IntegerGene[2]; 785 genes[0] = new IntegerGene(conf); 786 genes[1] = new IntegerGene(conf); 787 StaticFitnessFunction ff = new StaticFitnessFunction(20); 788 conf.setFitnessFunction(ff); 789 Chromosome chrom = new Chromosome(conf, genes); 790 conf.setSampleChromosome(chrom); 791 conf.setPopulationSize(5); 792 chrom = new Chromosome(conf, genes); 793 assertEquals(ff.getStaticFitnessValue(), chrom.getFitnessValue(), DELTA); 794 ff.setStaticFitnessValue(44.235d); 798 assertEquals(20, chrom.getFitnessValue(), DELTA); 799 } 800 801 807 public void testSize_0() 808 throws Exception { 809 Gene[] genes = new IntegerGene[2]; 810 genes[0] = new IntegerGene(conf); 811 genes[1] = new IntegerGene(conf); 812 Chromosome chrom = new Chromosome(conf, genes); 813 assertEquals(2, chrom.size()); 814 } 815 816 822 public void testSize_1() 823 throws Exception { 824 Gene[] genes = new IntegerGene[1]; 825 genes[0] = new IntegerGene(conf); 826 Chromosome chrom = new Chromosome(conf, genes); 827 assertEquals(1, chrom.size()); 828 } 829 830 836 public void testSize_2() 837 throws Exception { 838 Gene[] genes = new IntegerGene[0]; 839 try { 840 new Chromosome(conf, genes); 841 fail(); 842 } catch (IllegalArgumentException iex) { 843 ; } 845 } 846 847 853 public void testCompareTo_0() 854 throws Exception { 855 Configuration conf = new DefaultConfiguration(); 856 Gene[] genes = new IntegerGene[2]; 857 genes[0] = new IntegerGene(conf); 858 genes[1] = new IntegerGene(conf); 859 conf.setFitnessFunction(new StaticFitnessFunction(20)); 860 Chromosome chrom2 = new Chromosome(conf, genes); 861 conf.setSampleChromosome(chrom2); 862 conf.setPopulationSize(5); 863 Chromosome chrom = new Chromosome(conf, genes); 864 assertTrue(chrom.compareTo(chrom2) == 0); 865 assertTrue(chrom2.compareTo(chrom) == 0); 866 } 867 868 874 public void testCompareTo_0_2() 875 throws Exception { 876 Configuration conf = new DefaultConfiguration(); 877 Gene[] genes = new IntegerGene[2]; 878 genes[0] = new IntegerGene(conf); 879 genes[1] = new IntegerGene(conf); 880 conf.setFitnessFunction(new StaticFitnessFunction(20)); 881 Chromosome chrom2 = new Chromosome(conf, genes); 882 chrom2.setCompareApplicationData(true); 883 conf.setSampleChromosome(chrom2); 884 conf.setPopulationSize(5); 885 Chromosome chrom = new Chromosome(conf, genes); 886 chrom.setCompareApplicationData(true); 887 assertTrue(chrom.compareTo(chrom2) == 0); 888 assertTrue(chrom2.compareTo(chrom) == 0); 889 } 890 891 897 public void testCompareTo_0_3() 898 throws Exception { 899 Configuration conf = new DefaultConfiguration(); 900 Gene[] genes = new IntegerGene[2]; 901 genes[0] = new IntegerGene(conf); 902 genes[1] = new IntegerGene(conf); 903 conf.setFitnessFunction(new StaticFitnessFunction(20)); 904 Chromosome chrom2 = new Chromosome(conf, genes); 905 chrom2.setCompareApplicationData(true); 906 chrom2.setApplicationData(new Object ()); 907 conf.setSampleChromosome(chrom2); 908 conf.setPopulationSize(5); 909 Chromosome chrom = new Chromosome(conf, genes); 910 chrom.setCompareApplicationData(true); 911 chrom.setApplicationData(null); 912 assertFalse(chrom.compareTo(chrom2) == 0); 913 assertFalse(chrom2.compareTo(chrom) == 0); 914 } 915 916 922 public void testCompareTo_0_4() 923 throws Exception { 924 Configuration conf = new DefaultConfiguration(); 925 Gene[] genes = new IntegerGene[2]; 926 genes[0] = new IntegerGene(conf); 927 genes[1] = new IntegerGene(conf); 928 conf.setFitnessFunction(new StaticFitnessFunction(20)); 929 Chromosome chrom2 = new Chromosome(conf, genes); 930 chrom2.setCompareApplicationData(true); 931 chrom2.setApplicationData(new Object ()); 932 conf.setSampleChromosome(chrom2); 933 conf.setPopulationSize(5); 934 Chromosome chrom = new Chromosome(conf, genes); 935 chrom.setCompareApplicationData(true); 936 chrom.setApplicationData(new Date()); 937 assertFalse(chrom.compareTo(chrom2) == 0); 938 assertFalse(chrom2.compareTo(chrom) == 0); 939 } 940 941 947 public void testCompareTo_1() 948 throws Exception { 949 Configuration conf = new DefaultConfiguration(); 950 Gene[] genes = new IntegerGene[2]; 951 Gene gen0 = new IntegerGene(conf); 952 gen0.setAllele(new Integer (1147)); 953 genes[0] = gen0; 954 genes[1] = new IntegerGene(conf); 955 Chromosome chrom0 = new Chromosome(conf, genes); 956 conf.setFitnessFunction(new StaticFitnessFunction(20)); 957 conf.setSampleChromosome(chrom0); 958 conf.setPopulationSize(5); 959 Chromosome chrom = new Chromosome(conf, genes); 960 Gene[] genes2 = new IntegerGene[2]; 961 Gene gene01 = new IntegerGene(conf); 962 gene01.setAllele(new Integer (4711)); 963 genes2[0] = gene01; 964 genes2[1] = new IntegerGene(conf); 965 Chromosome chrom2 = new Chromosome(conf, genes2); 966 assertTrue(chrom.compareTo(chrom2) < 0); 967 assertTrue(chrom2.compareTo(chrom) > 0); 968 } 969 970 976 public void testCompareTo_2() 977 throws Exception { 978 Configuration conf = new DefaultConfiguration(); 979 Gene[] genes = new IntegerGene[2]; 980 genes[0] = new IntegerGene(conf); 981 genes[1] = new IntegerGene(conf); 982 conf.setFitnessFunction(new StaticFitnessFunction(20)); 983 Chromosome chrom = new Chromosome(conf, genes); 984 Gene[] genes2 = new IntegerGene[3]; 985 genes2[0] = new IntegerGene(conf); 986 genes2[1] = new IntegerGene(conf); 987 genes2[2] = new IntegerGene(conf); 988 Chromosome chrom2 = new Chromosome(conf, genes2); 989 conf.setSampleChromosome(chrom2); 990 conf.setPopulationSize(5); 991 assertTrue(chrom.compareTo(chrom2) < 0); 992 assertTrue(chrom2.compareTo(chrom) > 0); 993 } 994 995 1001 public void testCompareTo_3() 1002 throws Exception { 1003 Gene[] genes = new IntegerGene[1]; 1004 genes[0] = new IntegerGene(conf); 1005 Chromosome chrom = new Chromosome(conf, genes); 1006 assertTrue(chrom.compareTo(null) > 0); 1007 } 1008 1009 1015 public void testCompareTo_4() 1016 throws Exception { 1017 Configuration conf = new DefaultConfiguration(); 1018 Gene[] genes = new IntegerGene[2]; 1019 Gene gen0 = new IntegerGene(conf); 1020 gen0.setAllele(new Integer (4711)); 1021 genes[0] = gen0; 1022 genes[1] = new IntegerGene(conf); 1023 conf.setFitnessFunction(new StaticFitnessFunction(20)); 1024 conf.setPopulationSize(5); 1025 Chromosome chrom0 = new Chromosome(conf, genes); 1026 conf.setSampleChromosome(chrom0); 1027 Chromosome chrom = new Chromosome(conf, genes); 1028 Gene[] genes2 = new IntegerGene[2]; 1029 Gene gene01 = new IntegerGene(conf); 1030 gene01.setAllele(new Integer (4711)); 1031 genes2[0] = gene01; 1032 genes2[1] = new IntegerGene(conf); 1033 Chromosome chrom2 = new Chromosome(conf, genes2); 1034 assertTrue(chrom.compareTo(chrom2) == 0); 1035 assertTrue(chrom2.compareTo(chrom) == 0); 1036 } 1037 1038 1044 public void testCompareTo_5() 1045 throws Exception { 1046 Configuration conf = new DefaultConfiguration(); 1047 Gene[] genes = new Gene[2]; 1048 genes[0] = new IntegerGene(conf); 1049 genes[1] = new BooleanGene(conf); 1050 conf.setFitnessFunction(new StaticFitnessFunction(20)); 1051 Chromosome chrom2 = new Chromosome(conf, genes); 1052 conf.setSampleChromosome(chrom2); 1053 conf.setPopulationSize(5); 1054 Chromosome chrom = new Chromosome(conf, genes); 1055 assertTrue(chrom.compareTo(chrom2) == 0); 1056 assertTrue(chrom2.compareTo(chrom) == 0); 1057 } 1058 1059 1065 public void testCompareTo_6() 1066 throws Exception { 1067 Configuration conf = new DefaultConfiguration(); 1068 Gene[] genes1 = new Gene[2]; 1069 genes1[0] = new IntegerGene(conf); 1070 genes1[1] = new BooleanGene(conf); 1071 Gene[] genes2 = new Gene[2]; 1072 genes2[0] = new IntegerGene(conf); 1073 genes2[1] = new BooleanGene(conf); 1074 conf.setFitnessFunction(new StaticFitnessFunction(20)); 1075 Chromosome chrom2 = new Chromosome(conf, genes1); 1076 conf.setSampleChromosome(chrom2); 1077 conf.setPopulationSize(5); 1078 Chromosome chrom = new Chromosome(conf, genes2); 1079 assertTrue(chrom.compareTo(chrom2) == 0); 1080 assertTrue(chrom2.compareTo(chrom) == 0); 1081 genes2[1].setAllele(Boolean.valueOf(false)); 1082 genes1[1].setAllele(Boolean.valueOf(true)); 1083 assertFalse(chrom2.compareTo(chrom) == 0); 1084 } 1085 1086 1092 public void testRandomInitialChromosome_0() 1093 throws Exception { 1094 try { 1095 Chromosome.randomInitialChromosome(null); 1096 fail(); 1097 } catch (IllegalArgumentException iex) { 1098 ; } 1100 } 1101 1102 1108 public void testRandomInitialChromosome_1() 1109 throws Exception { 1110 Configuration conf = new ConfigurationForTest(); 1111 conf.setChromosomePool(new ChromosomePool()); 1112 conf.setRandomGenerator(new RandomGeneratorForTest(true)); 1113 IChromosome chrom = Chromosome.randomInitialChromosome(conf); 1114 assertTrue( ( (BooleanGene) chrom.getGene(0)).booleanValue()); 1116 } 1117 1118 1124 public void testRandomInitialChromosome_2() 1125 throws Exception { 1126 Configuration conf = new ConfigurationForTest(); 1127 conf.setChromosomePool(new ChromosomePool()); 1128 conf.setRandomGenerator(new RandomGeneratorForTest(false)); 1129 IChromosome chrom = Chromosome.randomInitialChromosome(conf); 1130 assertFalse( ( (BooleanGene) chrom.getGene(0)).booleanValue()); 1132 } 1133 1134 1142 public void testRandomInitialChromosome_3() 1143 throws Exception { 1144 Configuration conf = new ConfigurationForTest(); 1145 conf.setChromosomePool(new ChromosomePool()); 1146 conf.setRandomGenerator(new RandomGeneratorForTest(true)); 1147 IChromosome chrom = Chromosome.randomInitialChromosome(conf); 1148 chrom.cleanup(); chrom = Chromosome.randomInitialChromosome(conf); 1150 assertEquals(FitnessFunction.NO_FITNESS_VALUE, 1151 chrom.getFitnessValueDirectly(), DELTA); 1152 assertTrue( ( (BooleanGene) chrom.getGene(0)).booleanValue()); 1153 } 1154 1155 1163 public void testRandomInitialChromosome_4() 1164 throws Exception { 1165 Configuration conf = new DefaultConfiguration(); 1166 conf.setFitnessFunction(new StaticFitnessFunction(2.5d)); 1167 Gene[] genes = new Gene[1]; 1168 Gene gene = new BooleanGene(conf); 1169 genes[0] = gene; 1170 Chromosome chrom = new Chromosome(conf, genes); 1171 conf.setSampleChromosome(chrom); 1172 conf.setPopulationSize(5); 1173 Chromosome.randomInitialChromosome(conf); 1175 try { 1176 conf.setRandomGenerator(new RandomGeneratorForTest(true)); 1177 fail(); 1178 } catch (InvalidConfigurationException iex) { 1179 ; } 1181 } 1182 1183 1189 public void testCleanup_0() 1190 throws Exception { 1191 Configuration conf = new ConfigurationForTest(); 1192 IChromosome chrom = Chromosome.randomInitialChromosome(conf); 1193 chrom.cleanup(); 1194 } 1195 1196 1202 public void testCleanup_2() 1203 throws Exception { 1204 Configuration conf = new ConfigurationForTest(); 1205 ChromosomePool chromosomePool = new ChromosomePool(); 1206 assertNull(chromosomePool.acquireChromosome()); 1207 conf.setChromosomePool(chromosomePool); 1208 IChromosome chrom = Chromosome.randomInitialChromosome(conf); 1209 chrom.cleanup(); 1210 assertSame(chrom, chromosomePool.acquireChromosome()); 1211 } 1212 1213 1218 public void testSetCompareApplicationData_0() 1219 throws Exception { 1220 Configuration conf = new ConfigurationForTest(); 1221 Chromosome chrom = (Chromosome) Chromosome.randomInitialChromosome(conf); 1222 assertFalse(chrom.isCompareApplicationData()); 1223 chrom.setCompareApplicationData(true); 1224 assertTrue(chrom.isCompareApplicationData()); 1225 } 1226 1227 1235 public void testToString_0() 1236 throws Exception { 1237 Configuration conf = new ConfigurationForTest(); 1238 Chromosome chrom = new Chromosome(conf, 3); 1239 assertEquals(IChromosome.S_SIZE + ":" + chrom.size() 1240 + ", " + IChromosome.S_FITNESS_VALUE + ":" + 1241 FitnessFunction.NO_FITNESS_VALUE 1242 + ", " + IChromosome.S_ALLELES + ":[null, null, null]" 1243 + ", " + IChromosome.S_APPLICATION_DATA + ":null", 1244 chrom.toString()); 1245 } 1246 1247 1255 public void testToString_1() 1256 throws Exception { 1257 Configuration conf = new ConfigurationForTest(); 1258 Gene[] genes = new IntegerGene[2]; 1259 genes[0] = new IntegerGene(conf, 0, 77); 1260 genes[0].setAllele(new Integer (47)); 1261 genes[1] = new IntegerGene(conf, 2, 333); 1262 genes[1].setAllele(new Integer (55)); 1263 Chromosome chrom = new Chromosome(conf, 2); 1264 chrom.setFitnessValue(47.11d); 1265 chrom.setGenes(genes); 1266 assertEquals(IChromosome.S_SIZE + ":" + chrom.size() 1267 + ", " + IChromosome.S_FITNESS_VALUE + ":" + 1268 47.11d 1269 + ", " + IChromosome.S_ALLELES + ":[IntegerGene(0,77)=47," 1270 + " IntegerGene(2,333)=55]" 1271 + ", " + IChromosome.S_APPLICATION_DATA + ":null", 1272 chrom.toString()); 1273 } 1274 1275 1283 public void testToString_2() 1284 throws Exception { 1285 Configuration conf = new ConfigurationForTest(); 1286 Chromosome chrom = new Chromosome(conf, 3); 1287 chrom.setApplicationData("uIoP"); 1288 assertEquals(IChromosome.S_SIZE + ":" + chrom.size() 1289 + ", " + IChromosome.S_FITNESS_VALUE + ":" + 1290 FitnessFunction.NO_FITNESS_VALUE 1291 + ", " + IChromosome.S_ALLELES + ":[null, null, null]" 1292 + ", " + IChromosome.S_APPLICATION_DATA + ":uIoP", 1293 chrom.toString()); 1294 } 1295 1296 1304 public void testSetConstraintChecker_0() 1305 throws Exception { 1306 Chromosome c = new Chromosome(new ConfigurationForTest(), 2); 1307 assertNull(c.getConstraintChecker()); 1308 IGeneConstraintChecker cc = new MyConstraintChecker(); 1309 c.setConstraintChecker(cc); 1310 assertEquals(cc, c.getConstraintChecker()); 1311 c.setConstraintChecker(null); 1312 assertNull(c.getConstraintChecker()); 1313 } 1314 1315 1323 public void testSetConstraintChecker_1() 1324 throws Exception { 1325 Gene gene = new IntegerGene(conf); 1326 Chromosome c = new Chromosome(conf, gene, 2); 1327 assertNull(c.getConstraintChecker()); 1328 IGeneConstraintChecker cc = new MyConstraintChecker(IntegerGene.class); 1329 try { 1330 c.setConstraintChecker(cc); 1331 fail(); 1332 } catch (InvalidConfigurationException cex) { 1333 ; } 1335 assertNull(c.getConstraintChecker()); 1336 } 1337 1338 1346 public void testSetConstraintChecker_2() 1347 throws Exception { 1348 Gene gene = new IntegerGene(conf); 1349 Chromosome c = new Chromosome(conf, gene, 2); 1350 Gene[] genes = new Gene[] { 1351 gene}; 1352 c.setConstraintChecker(null); 1353 assertNull(c.getConstraintChecker()); 1354 c.setGenes(genes); 1356 } 1357 1358 1366 public void testSetConstraintChecker_3() 1367 throws Exception { 1368 Gene gene = new IntegerGene(conf); 1369 Chromosome c = new Chromosome(conf, gene, 2); 1370 IGeneConstraintChecker cc = new MyConstraintChecker(IntegerGene.class); 1371 try { 1372 c.setConstraintChecker(cc); 1373 fail(); 1374 } catch (InvalidConfigurationException iex) { 1375 ; } 1377 } 1378 1379 1387 public void testSetConstraintChecker_4() 1388 throws Exception { 1389 Gene gene = new IntegerGene(conf); 1390 Chromosome c = new Chromosome(conf, gene, 2); 1391 IGeneConstraintChecker cc = new MyConstraintChecker(DoubleGene.class); 1392 c.setConstraintChecker(cc); 1393 Gene[] genes = new Gene[] { 1394 gene}; 1395 c.setGenes(genes); 1397 } 1398 1399 class MyAppObject 1400 extends TestFitnessFunction implements Cloneable { 1401 public int compareTo(Object o) { 1402 return 0; 1403 } 1404 1405 public Object clone() { 1406 return this; 1407 } 1408 } 1409 class MyAppObject2 1410 extends TestFitnessFunction implements IApplicationData { 1411 public boolean equals(Object o2) { 1412 return compareTo(o2) == 0; 1413 } 1414 1415 public int compareTo(Object o) { 1416 return 0; 1417 } 1418 1419 public Object clone() { 1420 return new MyAppObject2(); 1421 } 1422 } 1423 1430 static class MyConstraintChecker 1431 implements IGeneConstraintChecker { 1432 private Class m_forbidden; 1433 1434 public MyConstraintChecker() { 1435 this(null); 1436 } 1437 1438 public MyConstraintChecker(Class a_forbiddenClass) { 1439 m_forbidden = a_forbiddenClass; 1440 } 1441 1442 public boolean verify(final Gene a_gene, final Object a_value, 1443 final IChromosome a_chrom, final int a_geneIndex) { 1444 if (m_forbidden == null) { 1445 return true; 1446 } 1447 return! (a_gene.getClass().equals(m_forbidden)); 1448 } 1449 } 1450 1458 public void testIsSerializable_0() 1459 throws Exception { 1460 Chromosome chrom = new Chromosome(new ConfigurationForTest(), new Gene[] { 1461 new IntegerGene(new ConfigurationForTest(), 1, 5) 1462 }); 1463 assertTrue(isSerializable(chrom)); 1464 } 1465 1466 1475 public void testDoSerialize_0() 1476 throws Exception { 1477 Chromosome chrom = new Chromosome(new ConfigurationForTest(), new Gene[] { 1479 new IntegerGene(new ConfigurationForTest(), 1, 5) 1480 }); 1481 IGeneConstraintChecker checker = new MyConstraintChecker(); 1482 chrom.setConstraintChecker(checker); 1483 Object o = doSerialize(chrom); 1484 assertEquals(o, chrom); 1485 } 1486 1487 1493 public void testIsHandlerFor_0() 1494 throws Exception { 1495 Chromosome chrom = new Chromosome(new ConfigurationForTest(), 3); 1496 assertTrue(chrom.isHandlerFor(chrom, Chromosome.class)); 1497 assertFalse(chrom.isHandlerFor(chrom, ChromosomeForTest.class)); 1498 assertFalse(chrom.isHandlerFor(chrom, Object .class)); 1499 assertTrue(chrom.perform(chrom, Chromosome.class, null) instanceof 1500 Chromosome); 1501 } 1502 1503 1509 public void testSetMultiObjectives_0() 1510 throws Exception { 1511 Chromosome chrom = new Chromosome(new ConfigurationForTest(), 3); 1512 List l = new Vector(); 1513 l.add("Entry_1"); 1514 l.add("Entry_2"); 1515 chrom.setMultiObjectives(l); 1516 assertEquals(l, chrom.getMultiObjectives()); 1517 List l2 = new Vector(); 1518 l2.add("Entry_3"); 1519 l2.add("Entry_4"); 1520 chrom.setMultiObjectives(l2); 1521 assertEquals(l2, chrom.getMultiObjectives()); 1522 assertEquals(2, chrom.getMultiObjectives().size()); 1523 } 1524 1525 1532 public void testPersistentRepresentation_0() 1533 throws Exception { 1534 Configuration conf = new DefaultConfiguration(); 1535 Gene[] genes1 = new Gene[2]; 1536 genes1[0] = new IntegerGene(conf); 1537 genes1[1] = new BooleanGene(conf); 1538 Chromosome chrom = new Chromosome(conf, genes1); 1539 String repr = chrom.getPersistentRepresentation(); 1540 Chromosome chrom2 = new Chromosome(conf); 1541 chrom2.setValueFromPersistentRepresentation(repr); 1542 assertEquals(chrom, chrom2); 1543 assertEquals(chrom.getPersistentRepresentation(), 1544 chrom2.getPersistentRepresentation()); 1545 } 1546 1547 1554 public void testPersistentRepresentation_1() 1555 throws Exception { 1556 Configuration conf = new DefaultConfiguration(); 1557 Gene[] genes1 = new Gene[2]; 1558 genes1[0] = new StringGene(conf); 1559 genes1[1] = new DoubleGene(conf); 1560 Chromosome chrom = new Chromosome(conf, genes1); 1561 String repr = chrom.getPersistentRepresentation(); 1562 Chromosome chrom2 = new Chromosome(conf); 1563 chrom2.setValueFromPersistentRepresentation(repr); 1564 assertEquals(chrom, chrom2); 1565 assertEquals(chrom.getPersistentRepresentation(), 1566 chrom2.getPersistentRepresentation()); 1567 } 1568 1569 1575 public void testPersistentRepresentation_2() 1576 throws Exception { 1577 Chromosome chrom = new Chromosome(conf); 1578 chrom.setValueFromPersistentRepresentation(null); 1579 assertEquals(0, chrom.size()); 1580 } 1581 1582 1588 public void testPersistentRepresentation_3() 1589 throws Exception { 1590 Chromosome chrom = new Chromosome(conf); 1591 try { 1592 chrom.setValueFromPersistentRepresentation("1" 1593 + Chromosome.CHROM_DELIMITER 1594 + "2"); 1595 fail(); 1596 } catch (UnsupportedRepresentationException uex) { 1597 ; } 1599 } 1600 1601 1607 public void testPersistentRepresentation_4() 1608 throws Exception { 1609 Chromosome chrom = new Chromosome(conf); 1610 try { 1611 chrom.setValueFromPersistentRepresentation("1" 1612 + Chromosome.CHROM_DELIMITER 1613 + "0" 1614 + Chromosome.CHROM_DELIMITER); 1615 fail(); 1616 } catch (UnsupportedRepresentationException uex) { 1617 ; } 1619 } 1620 1621 1629 public void testPersistentRepresentation_6() 1630 throws Exception { 1631 Chromosome chrom = new Chromosome(conf); 1632 try { 1633 chrom.setValueFromPersistentRepresentation("47.11" 1634 + Chromosome.CHROM_DELIMITER 1635 + "1" 1636 + Chromosome.CHROM_DELIMITER 1637 + "<" + IntegerGene.class.getName() 1638 + Chromosome.GENE_DELIMITER 1639 + "2<"); 1640 fail(); 1641 } catch (UnsupportedRepresentationException uex) { 1642 ; } 1644 } 1645 1646 1653 public void testPersistentRepresentation_7() 1654 throws Exception { 1655 Configuration conf = new DefaultConfiguration(); 1656 Gene[] genes1 = new Gene[2]; 1657 genes1[0] = new BooleanGene(conf); 1658 genes1[1] = new DoubleGene(conf); 1659 Chromosome chrom = new Chromosome(conf, genes1); 1660 String repr = chrom.getPersistentRepresentation(); 1661 Chromosome chrom2 = new Chromosome(conf); 1662 chrom2.setValueFromPersistentRepresentation(repr); 1663 assertEquals(chrom, chrom2); 1664 assertEquals(chrom.getPersistentRepresentation(), 1665 chrom2.getPersistentRepresentation()); 1666 } 1667 1668 1676 public void testPersistentRepresentation_5() 1677 throws Exception { 1678 Chromosome chrom = new Chromosome(conf); 1679 try { 1680 chrom.setValueFromPersistentRepresentation("47.11" 1681 + Chromosome.CHROM_DELIMITER 1682 + "1" 1683 + Chromosome.CHROM_DELIMITER 1684 + "<" + IntegerGene.class.getName() 1685 + Chromosome.GENE_DELIMITER 1686 + "2:4:4" 1687 + Chromosome.GENE_DELIMITER 1688 + "><>"); 1689 fail(); 1690 } catch (UnsupportedRepresentationException uex) { 1691 ; assertEquals(1, chrom.size()); 1693 } 1694 } 1695} 1696 | Popular Tags |