1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class DoubleGeneTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.37 $"; 26 27 public void setUp() { 28 super.setUp(); 29 } 30 31 public static Test suite() { 32 TestSuite suite = new TestSuite(DoubleGeneTest.class); 33 return suite; 34 } 35 36 41 public void testConstruct_0() 42 throws Exception { 43 Configuration conf = new ConfigurationForTest(); 44 Gene gene = new DoubleGene(conf, 1.1d, 100.0d); 45 gene.setAllele(new Double (101.1d)); 47 } 48 49 54 public void testConstruct_1() 55 throws Exception { 56 Gene gene = new DoubleGene(conf); 57 gene.setAllele(new Double (Double.MAX_VALUE)); 58 } 59 60 65 public void testConstruct_2() 66 throws Exception { 67 Gene gene = new DoubleGene(conf); 68 gene.setAllele(new Double ( - (Double.MAX_VALUE / 2))); 69 } 70 71 77 public void testConstruct_3() 78 throws Exception { 79 Genotype.setStaticConfiguration(conf); 80 Gene gene = new DoubleGene(); 81 assertSame(conf, gene.getConfiguration()); 82 } 83 84 89 public void testToString_0() 90 throws Exception { 91 Gene gene = new DoubleGene(conf, 1.2d, 99.7d); 92 gene.setAllele(new Double (47.3d)); 93 assertEquals("DoubleGene(1.2,99.7)=47.3", gene.toString()); 94 } 95 96 101 public void testToString_1() 102 throws Exception { 103 Gene gene = new DoubleGene(conf, -100.0d, 100.0d); 104 gene.setAllele(new Double ( -88.75286d)); 105 assertEquals("DoubleGene(-100.0,100.0)=-88.75286", gene.toString()); 106 } 107 108 114 public void testToString_2() 115 throws Exception { 116 Gene gene = new DoubleGene(conf, 1.2d, 99.7d); 117 assertEquals("DoubleGene(1.2,99.7)=null", gene.toString()); 118 } 119 120 125 public void testGetAllele_0() 126 throws Exception { 127 Gene gene = new DoubleGene(conf, 1.9d, 100.4d); 128 gene.setAllele(new Double (33.0d)); 129 assertEquals(new Double (33.0d), gene.getAllele()); 130 } 131 132 137 public void testGetAllele_1() 138 throws Exception { 139 Gene gene = new DoubleGene(conf, 1.8d, 100.1d); 140 gene.setAllele(new Double (1.9d)); 141 assertEquals(new Double (1.9d), gene.getAllele()); 142 } 143 144 149 public void testGetAllele_2() 150 throws Exception { 151 Gene gene = new DoubleGene(conf, 1.0d, 100.0d); 152 gene.setAllele(new Double (100.0d)); 153 assertEquals(new Double (100.0d), gene.getAllele()); 154 } 155 156 161 public void testEquals_0() 162 throws Exception { 163 Gene gene1 = new DoubleGene(conf, 1.1d, 100.2d); 164 Gene gene2 = new DoubleGene(conf, 1.1d, 100.2d); 165 assertTrue(gene1.equals(gene2)); 166 assertTrue(gene2.equals(gene1)); 167 } 168 169 174 public void testEquals_1() 175 throws Exception { 176 Gene gene1 = new DoubleGene(conf, 1.9d, 100.4d); 177 assertFalse(gene1.equals(null)); 178 } 179 180 185 public void testEquals_2() 186 throws Exception { 187 Gene gene1 = new DoubleGene(conf, 11.2d, 100.7d); 188 assertFalse(gene1.equals(new BooleanGene(conf))); 189 } 190 191 196 public void testEquals_3() 197 throws Exception { 198 Gene gene1 = new DoubleGene(conf, 1.0d, 100.7d); 199 assertFalse(gene1.equals(new Vector())); 200 } 201 202 207 public void testEquals_4() 208 throws Exception { 209 Gene gene1 = new DoubleGene(conf, 1.2d, 100.3d); 210 Gene gene2 = new DoubleGene(conf, 1.2d, 99.5d); 211 assertTrue(gene1.equals(gene2)); 212 assertTrue(gene2.equals(gene1)); 213 } 214 215 220 public void testEquals_5() 221 throws Exception { 222 Gene gene1 = new FixedBinaryGene(conf, 5); 223 Gene gene2 = new DoubleGene(conf, 1, 99); 224 assertFalse(gene1.equals(gene2)); 225 assertFalse(gene2.equals(gene1)); 226 } 227 228 233 public void testEquals_6() 234 throws Exception { 235 Gene gene1 = new DoubleGene(conf, 1, 99); 236 Gene gene2 = new BooleanGene(conf); 237 assertFalse(gene1.equals(gene2)); 238 assertFalse(gene2.equals(gene1)); 239 } 240 241 246 public void testEquals_7() 247 throws Exception { 248 Gene gene1 = new DoubleGene(conf, 1, 99); 249 Gene gene2 = new IntegerGene(conf); 250 assertFalse(gene1.equals(gene2)); 251 assertFalse(gene2.equals(gene1)); 252 } 253 254 260 public void testEquals_8() 261 throws Exception { 262 Configuration conf = new ConfigurationForTest(); 263 Gene gene1 = new DoubleGene(conf, 1.2d, 100.3d); 264 gene1.setAllele(new Double (1)); 265 Gene gene2 = new DoubleGene(conf, 1.2d, 99.5d); 266 gene2.setAllele(new Double ( -1)); 267 assertFalse(gene1.equals(gene2)); 268 assertFalse(gene2.equals(gene1)); 269 } 270 271 276 public void testDoubleValue_0() 277 throws Exception { 278 DoubleGene gene1 = new DoubleGene(conf, 1.0d, 10000.0d); 279 gene1.setAllele(new Double (4711.0d)); 280 assertEquals(4711.0d, gene1.doubleValue(), DELTA); 281 } 282 283 288 public void testDoubleValue_1() 289 throws Exception { 290 DoubleGene gene1 = new DoubleGene(conf, 1.765d, 10000.0d); 291 gene1.setAllele(null); 292 try { 293 assertEquals(0.0d, gene1.doubleValue(), DELTA); 294 fail(); 295 } 296 catch (NullPointerException nullex) { 297 ; } 299 } 300 301 307 public void testSetAllele_0() 308 throws Exception { 309 Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d); 310 gene1.setAllele(null); 311 } 312 313 318 public void testSetAllele_1() 319 throws Exception { 320 Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d); 321 try { 322 gene1.setAllele("22"); 323 fail(); 324 } 325 catch (ClassCastException classex) { 326 ; } 328 } 329 330 335 public void testSetAllele_2() 336 throws Exception { 337 Gene gene1 = new DoubleGene(conf, 1.0d, 10000.0d); 338 try { 339 gene1.setAllele(new Integer (22)); 340 fail(); 341 } 342 catch (ClassCastException classex) { 343 ; } 345 } 346 347 354 public void testSetAllele_3() 355 throws Exception { 356 Gene gene1 = new DoubleGene(conf, 1.0d, 1000.0d); 357 gene1.setAllele(new Double (2000.0d)); 358 } 359 360 365 public void testNewGene_0() 366 throws Exception { 367 DoubleGene gene1 = new DoubleGene(conf, 1.0d, 10000.0d); 368 IGeneConstraintChecker checker = new GeneConstraintChecker(); 369 gene1.setConstraintChecker(checker); 370 gene1.setAllele(new Double (4711.0d)); 371 Double lower1 = (Double ) privateAccessor.getField(gene1, 372 "m_lowerBound"); 373 Double upper1 = (Double ) privateAccessor.getField(gene1, 374 "m_upperBound"); 375 DoubleGene gene2 = (DoubleGene) gene1.newGene(); 376 Double lower2 = (Double ) privateAccessor.getField(gene2, 377 "m_lowerBound"); 378 Double upper2 = (Double ) privateAccessor.getField(gene2, 379 "m_upperBound"); 380 assertEquals(lower1, lower2); 381 assertEquals(upper1, upper2); 382 assertEquals(checker, gene2.getConstraintChecker()); 383 } 384 385 390 public void testPersistentRepresentation_0() 391 throws Exception { 392 Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d); 393 gene1.setAllele(new Double (4.5d)); 394 String pres1 = gene1.getPersistentRepresentation(); 395 Gene gene2 = new DoubleGene(conf); 396 gene2.setValueFromPersistentRepresentation(pres1); 397 String pres2 = gene2.getPersistentRepresentation(); 398 assertEquals(pres1, pres2); 399 } 400 401 406 public void testPersistentRepresentation_1() 407 throws Exception { 408 Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d); 409 gene1.setValueFromPersistentRepresentation(null); 410 } 411 412 417 public void testPersistentRepresentation_2() 418 throws Exception { 419 Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d); 420 try { 421 gene1.setValueFromPersistentRepresentation("2.3"); 422 fail(); 423 } 424 catch (UnsupportedRepresentationException uex) { 425 ; } 427 } 428 429 434 public void testPersistentRepresentation_3() 435 throws Exception { 436 Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d); 437 try { 438 gene1.setValueFromPersistentRepresentation("2.3" 439 + DoubleGene. 440 PERSISTENT_FIELD_DELIMITER 441 + "4.6" 442 + DoubleGene. 443 PERSISTENT_FIELD_DELIMITER 444 + "6,5"); 445 fail(); 446 } 447 catch (UnsupportedRepresentationException uex) { 448 ; } 450 } 451 452 457 public void testPersistentRepresentation_4() 458 throws Exception { 459 Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d); 460 try { 461 gene1.setValueFromPersistentRepresentation("2.3" 462 + DoubleGene. 463 PERSISTENT_FIELD_DELIMITER 464 + "b" 465 + DoubleGene. 466 PERSISTENT_FIELD_DELIMITER 467 + "a"); 468 fail(); 469 } 470 catch (UnsupportedRepresentationException uex) { 471 ; } 473 } 474 475 480 public void testPersistentRepresentation_5() 481 throws Exception { 482 Gene gene1 = new DoubleGene(conf, 2.05d, 7.53d); 483 try { 484 gene1.setValueFromPersistentRepresentation("a" 485 + DoubleGene. 486 PERSISTENT_FIELD_DELIMITER 487 + "b" 488 + DoubleGene. 489 PERSISTENT_FIELD_DELIMITER 490 + "a"); 491 fail(); 492 } 493 catch (UnsupportedRepresentationException uex) { 494 ; } 496 } 497 498 503 public void testCleanup_0() 504 throws Exception { 505 Gene gene = new DoubleGene(conf, 1.3d, 6.5d); 507 Gene copy = gene.newGene(); 508 gene.cleanup(); 509 assertEquals(copy, gene); 510 } 511 512 517 public void testSetToRandomValue_0() 518 throws Exception { 519 Gene gene = new DoubleGene(conf, 1.3d, 6.5d); 520 gene.setAllele(new Double (5.8d)); 521 gene.setToRandomValue(new RandomGeneratorForTest(0.789d)); 522 assertEquals(new Double (0.789d * (6.5d - 1.3d) + 1.3d), gene.getAllele()); 523 } 524 525 530 public void testSetToRandomValue_1() 531 throws Exception { 532 Gene gene = new DoubleGene(conf, -1.3d, 6.5d); 533 gene.setAllele(new Double (5.8d)); 534 conf.setRandomGenerator(new RandomGeneratorForTest(0.258d)); 535 gene.setToRandomValue(new RandomGeneratorForTest(0.014)); 536 assertEquals(new Double (0.014d * (6.5d + 1.3d) - 1.3d), gene.getAllele()); 537 } 538 539 544 public void testSetToRandomValue_2() 545 throws Exception { 546 547 Configuration conf = new ConfigurationForTest(); 548 Gene gene = new DoubleGene(conf, -1.3d, -0.5d); 549 gene.setAllele(new Double (5.8d)); 550 conf.setRandomGenerator(new RandomGeneratorForTest(0.258d)); 551 gene.setToRandomValue(new RandomGeneratorForTest(0.83d)); 552 assertEquals(new Double (0.83d * ( -0.5d + 1.3d) - 1.3d), gene.getAllele()); 553 } 554 555 560 public void testSetToRandomValue_3() 561 throws Exception { 562 DoubleGene gene = new DoubleGene(conf, 1.3d, 6.5d); 563 gene.setAllele(new Double (5.8d)); 564 gene.setToRandomValue(new RandomGeneratorForTest(0.478d)); 565 if (gene.doubleValue() < 1.3d 566 || gene.doubleValue() > 6.5d) { 567 fail(); 568 } 569 } 570 571 576 public void testSetToRandomValue_4() 577 throws Exception { 578 DoubleGene gene = new DoubleGene(conf, 1.3d, 6.5d); 579 gene.setAllele(new Double (5.8d)); 580 gene.setToRandomValue(new RandomGeneratorForTest(8.584d)); 581 if (gene.doubleValue() < 1.3d 582 || gene.doubleValue() > 6.5d) { 583 fail(); 584 } 585 } 586 587 592 public void testCompareToNative_0() 593 throws Exception { 594 Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d); 595 gene1.setAllele(new Double (5.8d)); 596 Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d); 597 gene2.setAllele(new Double (5.9d)); 598 assertEquals( ( (Double ) gene1.getAllele()).compareTo( (Double ) gene2. 599 getAllele()), gene1.compareTo(gene2)); 600 } 601 602 607 public void testCompareToNative_1() 608 throws Exception { 609 Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d); 610 gene1.setAllele(new Double (5.8d)); 611 Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d); 612 gene2.setAllele(new Double (5.8d)); 613 assertEquals( ( (Double ) gene1.getAllele()).compareTo( (Double ) gene2. 614 getAllele()), gene1.compareTo(gene2)); 615 } 616 617 622 public void testCompareToNative_2() 623 throws Exception { 624 Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d); 625 gene1.setAllele(new Double (5.9d)); 626 Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d); 627 gene2.setAllele(new Double (5.8d)); 628 assertEquals( ( (Double ) gene1.getAllele()).compareTo( (Double ) gene2. 629 getAllele()), gene1.compareTo(gene2)); 630 } 631 632 637 public void testCompareToNative_3() 638 throws Exception { 639 Gene gene1 = new DoubleGene(conf, 1.3d, 6.5d); 640 gene1.setAllele(new Double (5.9d)); 641 Gene gene2 = new DoubleGene(conf, 5.3d, 6.7d); 642 gene2.setAllele(new Double (5.4d)); 643 assertEquals( ( (Double ) gene1.getAllele()).compareTo( (Double ) gene2. 644 getAllele()), gene1.compareTo(gene2)); 645 } 646 647 652 public void testCompareToNative_4() 653 throws Exception { 654 Gene gene1 = new DoubleGene(conf, -1.3d, 6.5d); 655 gene1.setAllele(new Double (0.0d)); 656 Gene gene2 = new DoubleGene(conf, -5.3d, 6.7d); 657 gene2.setAllele(new Double ( -0.0d)); 658 assertEquals( ( (Double ) gene1.getAllele()).compareTo( (Double ) gene2. 659 getAllele()), gene1.compareTo(gene2)); 660 } 661 662 667 public void testApplyMutation_0() 668 throws Exception { 669 DoubleGene gene = new DoubleGene(conf, 0, 100); 670 gene.setAllele(new Double (50)); 671 gene.applyMutation(0, 0.0d); 672 assertEquals(50.0d, gene.doubleValue(), DELTA); 673 } 674 675 680 public void testApplyMutation_1() 681 throws Exception { 682 conf.setRandomGenerator(new RandomGeneratorForTest(15.0d)); 683 DoubleGene gene = new DoubleGene(conf, 0, 100); 684 gene.setAllele(new Double (50)); 685 gene.applyMutation(0, 0.5d); 686 assertEquals(50 + (100 - 0) * 0.5d, gene.doubleValue(), DELTA); 687 } 688 689 694 public void testApplyMutation_2() 695 throws Exception { 696 conf.setRandomGenerator(new RandomGeneratorForTest(15.0d)); 697 DoubleGene gene = new DoubleGene(conf, 44, 100); 698 gene.setAllele(new Double (50)); 699 gene.applyMutation(0, 0.3d); 700 assertEquals(50 + (100 - 44) * 0.3d, gene.doubleValue(), DELTA); 701 } 702 703 708 public void testApplyMutation_3() 709 throws Exception { 710 conf.setRandomGenerator(new RandomGeneratorForTest(0.5d)); 711 DoubleGene gene = new DoubleGene(conf, 33, 100); 712 gene.setAllele(new Double (50)); 713 gene.applyMutation(0, 1.9d); 714 assertEquals(33 + 0.5d * (100 - 33), gene.doubleValue(), DELTA); 715 } 716 717 722 public void testApplyMutation_4() 723 throws Exception { 724 conf.setRandomGenerator(new RandomGeneratorForTest(0.4d)); 725 DoubleGene gene = new DoubleGene(conf, 2, 100); 726 gene.setAllele(new Double (60)); 727 gene.applyMutation(0, 1.9d); 728 assertEquals(2 + 0.4d * (100 - 2), gene.doubleValue(), DELTA); 729 } 730 731 737 public void testApplyMutation_5() 738 throws Exception { 739 conf.setRandomGenerator(new RandomGeneratorForTest(0.8d)); 740 DoubleGene gene = new DoubleGene(conf, 0, 100); 741 gene.setAllele(new Double (60)); 742 gene.applyMutation(1, -1.0d); 743 assertEquals(0 + 0.8d * (100 - 0), gene.doubleValue(), DELTA); 744 } 745 746 751 public void testApplyMutation_6() 752 throws Exception { 753 DoubleGene gene = new DoubleGene(conf, 0, 100); 754 gene.setAllele(new Double (60)); 755 gene.applyMutation(77, -0.4d); 756 assertEquals(60 + (100 * ( -0.4d)), gene.doubleValue(), DELTA); 757 } 758 759 764 public void testApplyMutation_7() 765 throws Exception { 766 DoubleGene gene = new DoubleGene(conf, 0, 100); 767 try { 768 gene.applyMutation(0, -0.4d); 769 fail(); 770 } 771 catch (NullPointerException nex) { 772 ; } 774 } 775 776 782 public void testSetConstraintChecker_0() 783 throws Exception { 784 DoubleGene gene = new DoubleGene(conf, 0, 100); 785 assertNull(gene.getConstraintChecker()); 786 gene.setConstraintChecker(new IGeneConstraintChecker() { 787 public boolean verify(Gene a_gene, Object a_alleleValue, 788 IChromosome a_chrom, int a_index) { 789 return false; 790 } 791 }); 792 assertNotNull(gene.getConstraintChecker()); 793 } 794 795 801 public void testHashCode_0() 802 throws Exception { 803 DoubleGene gene = new DoubleGene(conf, 0, 100); 804 assertEquals( -3, gene.hashCode()); 805 } 806 807 813 public void testHashCode_1() 814 throws Exception { 815 Configuration conf = new ConfigurationForTest(); 816 DoubleGene c1 = new DoubleGene(conf); 817 DoubleGene c2 = new DoubleGene(conf); 818 assertEquals(c1.hashCode(), c2.hashCode()); 819 c1.setAllele(new Double (2)); 820 assertFalse(c1.hashCode() == c2.hashCode()); 821 assertEquals(c1.hashCode(), c1.hashCode()); 822 c2.setAllele(new Double (2)); 823 assertTrue(c1.hashCode() == c2.hashCode()); 824 } 825 826 832 public void testSetEnergy_0() 833 throws Exception { 834 BaseGene gene = new DoubleGene(conf); 835 assertEquals(0.0, gene.getEnergy(), DELTA); 836 } 837 838 844 public void testSetEnergy_1() 845 throws Exception { 846 BaseGene gene = new DoubleGene(conf); 847 gene.setEnergy(2.3); 848 assertEquals(2.3, gene.getEnergy(), DELTA); 849 gene.setEnergy( -55.8); 850 assertEquals( -55.8, gene.getEnergy(), DELTA); 851 gene.setEnergy(0.5); 852 gene.setEnergy(0.8); 853 assertEquals(0.8, gene.getEnergy(), DELTA); 854 } 855 856 857 863 public void testBounds_0() 864 throws Exception { 865 DoubleGene gene = new DoubleGene(conf, -3.4d, +5.7d); 866 assertEquals( -3.4d, gene.getLowerBound(), DELTA); 867 assertEquals( 5.7d, gene.getUpperBound(), DELTA); 868 } 869 870 876 public void testBounds_1() 877 throws Exception { 878 DoubleGene gene = new DoubleGene(conf); 879 assertEquals( - (Double.MAX_VALUE / 2), gene.getLowerBound(), DELTA); 880 assertEquals(Double.MAX_VALUE / 2, gene.getUpperBound(), DELTA); 881 } 882 883 class GeneConstraintChecker 884 implements IGeneConstraintChecker { 885 public boolean verify(Gene a_gene, Object a_alleleValue, 886 IChromosome a_chrom, int a_index) { 887 return true; 888 } 889 } 890 } 891 | Popular Tags |