1 10 package org.jgap; 11 12 import org.jgap.event.*; 13 import org.jgap.impl.*; 14 import junit.framework.*; 15 16 22 public class ConfigurationTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.36 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(ConfigurationTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 37 } 38 39 42 public void testAddGeneticOperator_0() { 43 Configuration conf = new Configuration(); 44 try { 45 conf.addGeneticOperator(null); 46 fail(); 47 } catch (InvalidConfigurationException invex) { 48 ; } 50 } 51 52 55 public void testAddGeneticOperator_1() { 56 Configuration conf = new Configuration(); 57 try { 58 conf.lockSettings(); 59 conf.addGeneticOperator(new MutationOperator()); 60 fail(); 61 } catch (InvalidConfigurationException invex) { 62 ; } 64 } 65 66 71 public void testAddGeneticOperator_2() 72 throws Exception { 73 Configuration conf = new Configuration(); 74 conf.addGeneticOperator(new MutationOperator(conf)); 75 assertEquals(1, conf.getGeneticOperators().size()); 76 conf.addGeneticOperator(new MutationOperator(conf)); 77 assertEquals(2, conf.getGeneticOperators().size()); 78 } 79 80 84 public void testVerifyStateIsValid_0() 85 throws Exception { 86 Configuration conf = new Configuration(); 87 assertEquals(false, conf.isLocked()); 88 conf.setFitnessFunction(new StaticFitnessFunction(2)); 89 try { 90 conf.verifyStateIsValid(); 91 fail(); 92 } catch (InvalidConfigurationException invex) { 93 ; } 95 } 96 97 101 public void testVerifyStateIsValid_1() 102 throws Exception { 103 Configuration conf = new Configuration(); 104 assertEquals(false, conf.isLocked()); 105 conf.setFitnessFunction(new StaticFitnessFunction(2)); 106 Gene gene = new BooleanGene(conf); 107 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 108 try { 109 conf.verifyStateIsValid(); 110 fail(); 111 } catch (InvalidConfigurationException invex) { 112 ; } 114 } 115 116 120 public void testVerifyStateIsValid_2() 121 throws Exception { 122 Configuration conf = new Configuration(); 123 assertEquals(false, conf.isLocked()); 124 conf.setFitnessFunction(new StaticFitnessFunction(2)); 125 Gene gene = new BooleanGene(conf); 126 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 127 conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); 128 try { 129 conf.verifyStateIsValid(); 130 fail(); 131 } catch (InvalidConfigurationException invex) { 132 ; } 134 } 135 136 140 public void testVerifyStateIsValid_3() 141 throws Exception { 142 Configuration conf = new Configuration(); 143 assertEquals(false, conf.isLocked()); 144 conf.setFitnessFunction(new StaticFitnessFunction(2)); 145 Gene gene = new BooleanGene(conf); 146 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 147 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 148 conf.setRandomGenerator(new StockRandomGenerator()); 149 try { 150 conf.verifyStateIsValid(); 151 fail(); 152 } catch (InvalidConfigurationException invex) { 153 ; } 155 } 156 157 161 public void testVerifyStateIsValid_4() 162 throws Exception { 163 Configuration conf = new Configuration(); 164 assertEquals(false, conf.isLocked()); 165 conf.setFitnessFunction(new StaticFitnessFunction(2)); 166 Gene gene = new BooleanGene(conf); 167 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 168 conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); 169 conf.setRandomGenerator(new StockRandomGenerator()); 170 conf.setEventManager(new EventManager()); 171 try { 172 conf.verifyStateIsValid(); 173 fail(); 174 } catch (InvalidConfigurationException invex) { 175 ; } 177 } 178 179 183 public void testVerifyStateIsValid_5() 184 throws Exception { 185 Configuration conf = new Configuration(); 186 assertEquals(false, conf.isLocked()); 187 conf.setFitnessFunction(new StaticFitnessFunction(2)); 188 Gene gene = new BooleanGene(conf); 189 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 190 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 191 conf.setRandomGenerator(new StockRandomGenerator()); 192 conf.setEventManager(new EventManager()); 193 conf.addGeneticOperator(new MutationOperator(conf)); 194 try { 195 conf.verifyStateIsValid(); 196 fail(); 197 } catch (InvalidConfigurationException invex) { 198 ; } 200 } 201 202 207 public void testVerifyStateIsValid_6() 208 throws Exception { 209 Configuration conf = new Configuration(); 210 assertEquals(false, conf.isLocked()); 211 conf.setFitnessFunction(new StaticFitnessFunction(2)); 212 Gene gene = new BooleanGene(conf); 213 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 214 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 215 conf.setRandomGenerator(new StockRandomGenerator()); 216 conf.setEventManager(new EventManager()); 217 conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); 218 conf.addGeneticOperator(new MutationOperator(conf)); 219 conf.setPopulationSize(1); 220 conf.verifyStateIsValid(); 221 } 222 223 228 public void testVerifyStateIsValid_7() 229 throws Exception { 230 Configuration conf = new Configuration(); 231 assertEquals(false, conf.isLocked()); 232 conf.setFitnessFunction(new StaticFitnessFunction(2)); 233 Gene gene = new BooleanGene(conf); 234 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 235 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 236 conf.setRandomGenerator(new StockRandomGenerator()); 237 conf.setEventManager(new EventManager()); 238 conf.addGeneticOperator(new MutationOperator(conf)); 239 conf.setPopulationSize(1); 240 try { 241 conf.verifyStateIsValid(); 242 fail(); 243 } catch (IllegalArgumentException illex) { 244 ; } 246 } 247 248 256 public void testVerifyStateIsValid_8() 257 throws Exception { 258 Configuration conf = new DefaultConfiguration(); 259 conf.setFitnessFunction(new TestFitnessFunction()); 260 Chromosome sample = new Chromosome(conf); 261 conf.setSampleChromosome(sample); 262 conf.setPopulationSize(5); 263 try { 264 conf.verifyStateIsValid(); 265 fail(); 266 } catch (InvalidConfigurationException illex) { 267 ; } 269 } 270 271 280 public void testVerifyStateIsValid_9() 281 throws Exception { 282 Configuration conf = new DefaultConfiguration(); 283 conf.setFitnessFunction(new TestFitnessFunction()); 284 BooleanGene gene = new BooleanGene(conf); 285 gene.setAllele(null); 286 Chromosome sample = new Chromosome(conf, gene, 55); 287 conf.setSampleChromosome(sample); 288 conf.setPopulationSize(5); 289 conf.verifyStateIsValid(); 290 } 291 292 297 public void testIsLocked_0() 298 throws Exception { 299 Configuration conf = new Configuration(); 300 assertEquals(false, conf.isLocked()); 301 conf.setFitnessFunction(new StaticFitnessFunction(2)); 302 conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); 303 Gene gene = new BooleanGene(conf); 304 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 305 conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); 306 conf.setRandomGenerator(new StockRandomGenerator()); 307 conf.setEventManager(new EventManager()); 308 conf.addGeneticOperator(new MutationOperator(conf)); 309 conf.setPopulationSize(1); 310 assertEquals(false, conf.isLocked()); 311 conf.lockSettings(); 312 assertEquals(true, conf.isLocked()); 313 } 314 315 320 public void testGetter_0() 321 throws Exception { 322 Configuration conf = new Configuration(); 323 assertEquals(false, conf.isLocked()); 324 FitnessFunction fitFunc = new StaticFitnessFunction(2); 325 conf.setFitnessFunction(fitFunc); 326 Gene gene = new BooleanGene(conf); 327 Chromosome sample = new Chromosome(conf, gene, 55); 328 conf.setSampleChromosome(sample); 329 NaturalSelector natSel = new WeightedRouletteSelector(); 330 conf.addNaturalSelector(natSel, false); 331 RandomGenerator randGen = new StockRandomGenerator(); 332 conf.setRandomGenerator(randGen); 333 IEventManager evMan = new EventManager(); 334 conf.setEventManager(evMan); 335 GeneticOperator mutOp = new MutationOperator(conf); 336 conf.addGeneticOperator(mutOp); 337 GeneticOperator croOp = new CrossoverOperator(conf); 338 conf.addGeneticOperator(croOp); 339 conf.setPopulationSize(7); 340 assertEquals(fitFunc, conf.getFitnessFunction()); 341 assertEquals(natSel, conf.getNaturalSelectors(false).get(0)); 342 assertEquals(randGen, conf.getRandomGenerator()); 343 assertEquals(sample, conf.getSampleChromosome()); 344 assertEquals(evMan, conf.getEventManager()); 345 assertEquals(7, conf.getPopulationSize()); 346 assertEquals(2, conf.getGeneticOperators().size()); 347 assertEquals(mutOp, conf.getGeneticOperators().get(0)); 348 assertEquals(croOp, conf.getGeneticOperators().get(1)); 349 } 350 351 355 public void testSetNaturalSelector_0() 356 throws Exception { 357 Configuration conf = new Configuration(); 358 NaturalSelector selector = new WeightedRouletteSelector(); 359 conf.setNaturalSelector(selector); 360 assertEquals(selector, conf.getNaturalSelectors(false).get(0)); 361 } 362 363 366 public void testGetNaturalSelector_0() { 367 Configuration conf = new Configuration(); 368 NaturalSelector selector = conf.getNaturalSelector(); 369 assertEquals(null, selector); 370 } 371 372 378 public void testGetNaturalSelector_1() 379 throws Exception { 380 Configuration conf = new Configuration(); 381 NaturalSelector selector = new BestChromosomesSelector(conf); 382 conf.addNaturalSelector(selector, false); 383 assertEquals(selector, conf.getNaturalSelector()); 384 } 385 386 390 public void testGetNaturalSelector_2() 391 throws Exception { 392 Configuration conf = new Configuration(); 393 try { 394 conf.getNaturalSelector(true, 0); 395 fail(); 396 } catch (IllegalArgumentException iex) { 397 ; } 399 } 400 401 405 public void testGetNaturalSelector_3() 406 throws Exception { 407 Configuration conf = new Configuration(); 408 try { 409 conf.getNaturalSelector(false, 0); 410 fail(); 411 } catch (IllegalArgumentException iex) { 412 ; } 414 } 415 416 422 public void testGetNaturalSelector_4() 423 throws Exception { 424 Configuration conf = new Configuration(); 425 NaturalSelector selector = new BestChromosomesSelector(conf); 426 conf.addNaturalSelector(selector, false); 427 assertEquals(selector, conf.getNaturalSelector(false, 0)); 428 } 429 430 434 public void testAddNaturalSelector_0() 435 throws Exception { 436 Configuration conf = new Configuration(); 437 NaturalSelector selector = new WeightedRouletteSelector(); 438 conf.addNaturalSelector(selector, true); 439 assertEquals(selector, conf.getNaturalSelectors(true).get(0)); 440 } 441 442 446 public void testAddNaturalSelector_1() 447 throws Exception { 448 Configuration conf = new Configuration(); 449 NaturalSelector selector = new WeightedRouletteSelector(); 450 conf.addNaturalSelector(selector, false); 451 assertEquals(selector, conf.getNaturalSelectors(false).get(0)); 452 } 453 454 458 public void testAddNaturalSelector_2() 459 throws Exception { 460 Configuration conf = new Configuration(); 461 NaturalSelector selector1 = new WeightedRouletteSelector(); 462 NaturalSelector selector2 = new WeightedRouletteSelector(); 463 conf.addNaturalSelector(selector1, false); 464 assertEquals(selector1, conf.getNaturalSelectors(false).get(0)); 465 conf.addNaturalSelector(selector2, true); 466 assertEquals(selector2, conf.getNaturalSelectors(true).get(0)); 467 assertEquals(selector1, conf.getNaturalSelectors(false).get(0)); 468 try { 469 assertEquals(null, conf.getNaturalSelectors(false).get(1)); 470 fail(); 471 } catch (Exception ex) { 472 ; } 474 } 475 476 480 public void testAddNaturalSelector_3() 481 throws Exception { 482 Configuration conf = new Configuration(); 483 NaturalSelector selector = new WeightedRouletteSelector(); 484 conf.addNaturalSelector(selector, false); 485 conf.getNaturalSelectors(false).clear(); 486 conf.addNaturalSelector(selector, false); 487 Integer i = (Integer ) privateAccessor.getField(conf, 488 "m_sizeNaturalSelectorsPost"); 489 assertEquals(1, i.intValue()); 490 } 491 492 496 public void testAddNaturalSelector_4() 497 throws Exception { 498 Configuration conf = new Configuration(); 499 NaturalSelector selector = new WeightedRouletteSelector(); 500 conf.addNaturalSelector(selector, true); 501 conf.getNaturalSelectors(true).clear(); 502 conf.addNaturalSelector(selector, true); 503 Integer i = (Integer ) privateAccessor.getField(conf, 504 "m_sizeNaturalSelectorsPre"); 505 assertEquals(1, i.intValue()); 506 } 507 508 511 public void testSetFitnessFunction_0() { 512 Configuration conf = new Configuration(); 513 try { 514 conf.setFitnessFunction(null); 515 fail(); 516 } catch (InvalidConfigurationException invex) { 517 ; } 519 } 520 521 527 public void testSetFitnessFunction_1() 528 throws Exception { 529 Configuration conf = new Configuration(); 530 conf.setFitnessFunction(new TestFitnessFunction()); 531 try { 532 conf.setFitnessFunction(new StaticFitnessFunction(2.3d)); 533 fail(); 534 } catch (RuntimeException invex) { 535 ; } 537 } 538 539 545 public void testSetFitnessFunction_2() 546 throws Exception { 547 Configuration conf = new Configuration(); 548 conf.setFitnessFunction(new TestFitnessFunction()); 549 Configuration conf2 = new Configuration(); 550 try { 551 conf2.setFitnessFunction(new StaticFitnessFunction(2.3d)); 552 fail(); 553 } catch (RuntimeException invex) { 554 ; } 556 } 557 558 564 public void testSetFitnessFunction_3() 565 throws Exception { 566 Configuration conf = new Configuration(); 567 conf.setFitnessFunction(new StaticFitnessFunction(2.3d)); 568 Configuration conf2 = new Configuration(); 569 try { 570 conf2.setFitnessFunction(new StaticFitnessFunction(4.5d)); 571 fail(); 572 } catch (RuntimeException invex) { 573 ; } 575 } 576 577 583 public void testSetFitnessFunction_4() 584 throws Exception { 585 new Thread (new MyThread()).start(); 586 new Thread (new MyThread()).start(); 587 } 588 589 592 public void testSetFitnessEvaluator_0() { 593 Configuration conf = new Configuration(); 594 try { 595 conf.setFitnessEvaluator(null); 596 fail(); 597 } catch (IllegalStateException istex) { 598 ; } 600 } 601 602 605 public void testSetPreserveFittestIndividual_0() { 606 Configuration conf = new Configuration(); 607 assertFalse(conf.isPreserveFittestIndividual()); 608 conf.setPreservFittestIndividual(true); 609 assertTrue(conf.isPreserveFittestIndividual()); 610 conf.setPreservFittestIndividual(false); 611 assertFalse(conf.isPreserveFittestIndividual()); 612 } 613 614 617 public void testGenerationNr_0() { 618 Configuration conf = new Configuration(); 619 assertEquals(0, conf.getGenerationNr()); 620 conf.incrementGenerationNr(); 621 assertEquals(1, conf.getGenerationNr()); 622 conf.incrementGenerationNr(); 623 conf.incrementGenerationNr(); 624 assertEquals(3, conf.getGenerationNr()); 625 } 626 627 631 public void testSetBulkFitnessFunction_0() { 632 Configuration conf = new Configuration(); 633 try { 634 conf.setBulkFitnessFunction(null); 635 fail(); 636 } catch (InvalidConfigurationException invex) { 637 ; } 639 } 640 641 645 public void testSetBulkFitnessFunction_1() 646 throws Exception { 647 Configuration conf = new Configuration(); 648 conf.setFitnessFunction(new TestFitnessFunction()); 649 try { 650 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 651 fail(); 652 } catch (InvalidConfigurationException invex) { 653 ; } 655 } 656 657 661 public void testSetBulkFitnessFunction_2() 662 throws Exception { 663 Configuration conf = new Configuration(); 664 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 665 try { 666 conf.setFitnessFunction(new TestFitnessFunction()); 667 fail(); 668 } catch (InvalidConfigurationException invex) { 669 ; } 671 } 672 673 677 public void testSetBulkFitnessFunction_3() 678 throws Exception { 679 Configuration conf = new Configuration(); 680 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 681 } 682 683 689 public void testSetBulkFitnessFunction_4() 690 throws Exception { 691 Configuration conf = new Configuration(); 692 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 693 try { 694 conf.setBulkFitnessFunction(new TestBulkFitnessFunction2()); 695 fail(); 696 } catch (RuntimeException rex) { 697 ; } 699 } 700 701 708 public void testSetBulkFitnessFunction_5() 709 throws Exception { 710 Configuration conf = new Configuration(); 711 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 712 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 713 } 714 715 721 public void testSetBulkFitnessFunction_6() 722 throws Exception { 723 new Thread (new MyThreadBulk()).start(); 724 new Thread (new MyThreadBulk()).start(); 725 } 726 727 731 public void testGetPopulationSize_0() 732 throws Exception { 733 Configuration conf = new Configuration(); 734 assertEquals(0, conf.getPopulationSize()); 735 final int SIZE = 22; 736 conf.setPopulationSize(SIZE); 737 assertEquals(SIZE, conf.getPopulationSize()); 738 } 739 740 744 public void testSetPopulationSize_1() 745 throws Exception { 746 Configuration conf = new Configuration(); 747 try { 748 conf.setPopulationSize(0); 749 fail(); 750 } catch (InvalidConfigurationException invex) { 751 ; } 753 } 754 755 758 public void testSetRandomGenerator_0() { 759 Configuration conf = new Configuration(); 760 try { 761 conf.setRandomGenerator(null); 762 fail(); 763 } catch (InvalidConfigurationException invex) { 764 ; } 766 } 767 768 771 public void testSetEventManager_0() { 772 Configuration conf = new Configuration(); 773 try { 774 conf.setEventManager(null); 775 fail(); 776 } catch (InvalidConfigurationException invex) { 777 ; } 779 } 780 781 787 public void testSetEventManager_1() 788 throws Exception { 789 Configuration conf = new Configuration(); 790 conf.setEventManager(new EventManager()); 791 conf.setEventManager(new EventManager()); 792 } 793 794 800 public void testSetEventManager_2() 801 throws Exception { 802 Configuration conf = new Configuration(); 803 conf.setEventManager(new EventManager()); 804 try { 805 conf.setEventManager(new TestEventManager()); 806 fail(); 807 } catch (RuntimeException rex) { 808 ; } 810 } 811 812 818 public void testSetEventManager_3() 819 throws Exception { 820 new Thread (new MyThreadEvent()).start(); 821 new Thread (new MyThreadEvent()).start(); 822 } 823 824 828 public void testLock_0() 829 throws Exception { 830 Configuration conf = new Configuration(); 831 try { 832 conf.lockSettings(); 833 fail(); 834 } catch (InvalidConfigurationException invex) { 835 ; } 837 } 838 839 843 public void testLock_1() 844 throws Exception { 845 Configuration conf = new Configuration(); 846 conf.setFitnessFunction(new TestFitnessFunction()); 847 Gene gene = new BooleanGene(conf); 848 Chromosome sample = new Chromosome(conf, gene, 55); 849 conf.setSampleChromosome(sample); 850 conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); 851 conf.setRandomGenerator(new GaussianRandomGenerator()); 852 conf.setEventManager(new EventManager()); 853 conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); 854 conf.addGeneticOperator(new MutationOperator(conf)); 855 conf.setPopulationSize(1); 856 conf.lockSettings(); 857 assertEquals(true, conf.isLocked()); 858 try { 859 conf.verifyChangesAllowed(); 860 fail(); 861 } catch (InvalidConfigurationException invex) { 862 ; } 864 } 865 866 870 public void testSetSampleChromosome_0() 871 throws Exception { 872 Configuration conf = new Configuration(); 873 Gene gene = new BooleanGene(conf); 874 new Chromosome(conf, gene, 55); 875 try { 876 conf.setSampleChromosome(null); 877 fail(); 878 } catch (InvalidConfigurationException invex) { 879 ; } 881 } 882 883 887 public void testGetChromosomeSize_0() 888 throws Exception { 889 Configuration conf = new Configuration(); 890 Gene gene = new BooleanGene(conf); 891 final int SIZE = 55; 892 Chromosome sample = new Chromosome(conf, gene, SIZE); 893 conf.setSampleChromosome(sample); 894 assertEquals(SIZE, conf.getChromosomeSize()); 895 } 896 897 904 public void testToString_0() 905 throws Exception { 906 Configuration conf = new ConfigurationForTest(); 907 conf.addGeneticOperator(new MutationOperator(conf)); 908 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 909 conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); 910 conf.addNaturalSelector(new BestChromosomesSelector(conf), false); 911 String s = trimString(conf.toString()); 912 String eventmgr = conf.getEventManager() != null ? 913 conf.getEventManager().getClass().getName() : conf.S_NONE; 914 String genops = ""; 915 if (conf.getGeneticOperators().size() < 1) { 916 genops = conf.S_NONE; 917 } 918 else { 919 for (int i = 0; i < conf.getGeneticOperators().size(); i++) { 920 if (i > 0) { 921 genops += "; "; 922 } 923 genops += conf.getGeneticOperators().get(i).getClass().getName(); ; 924 } 925 } 926 String natselsPre = ""; 928 int natsize = conf.getNaturalSelectors(true).size(); 929 if (natsize < 1) { 930 natselsPre = conf.S_NONE; 931 } 932 else { 933 for (int i = 0; i < natsize; i++) { 934 if (i > 0) { 935 natselsPre += "; "; 936 } 937 natselsPre += " " + 938 conf.getNaturalSelectors(true).get(i).getClass().getName(); 939 } 940 } 941 String natselsPost = ""; 943 natsize = conf.getNaturalSelectors(false).size(); 944 if (natsize < 1) { 945 natselsPost = conf.S_NONE; 946 } 947 else { 948 for (int i = 0; i < natsize; i++) { 949 if (i > 0) { 950 natselsPost += "; "; 951 } 952 natselsPost += " " + 953 conf.getNaturalSelectors(false).get(i).getClass().getName(); 954 } 955 } 956 assertEquals(trimString(conf.S_CONFIGURATION + ":" 957 + conf.S_CONFIGURATION_NAME + ":" 958 + conf.getName() + " " 959 + conf.S_POPULATION_SIZE + ":" 960 + conf.getPopulationSize() + " " 961 + conf.S_MINPOPSIZE + ":" 962 + conf.getMinimumPopSizePercent() + " " 963 + conf.S_CHROMOSOME_SIZE + ":" 964 + conf.getChromosomeSize() + " " 965 + conf.S_SAMPLE_CHROM + ":" 966 + conf.S_SIZE + ":" 967 + conf.getSampleChromosome().size() + " " 968 + conf.S_TOSTRING + ":" 969 + conf.getSampleChromosome().toString() + " " 970 + conf.S_RANDOM_GENERATOR + ":" 971 + conf.getRandomGenerator().getClass().getName() + 972 " " 973 + conf.S_EVENT_MANAGER + ":" 974 + eventmgr + " " 975 + conf.S_CONFIGURATION_HANDLER + ":" 976 + conf.getConfigurationHandler().getName() + " " 977 + conf.S_FITNESS_FUNCTION + ":" 978 + conf.getFitnessFunction().getClass().getName() + 979 " " 980 + conf.S_FITNESS_EVALUATOR + ":" 981 + conf.getFitnessEvaluator().getClass().getName() + 982 " " 983 + conf.S_GENETIC_OPERATORS + ":" 984 + genops + " " 985 + conf.S_NATURAL_SELECTORS + "(" + conf.S_PRE + ")" + 986 ":" 987 + natselsPre + " " 988 + conf.S_NATURAL_SELECTORS + "(" + conf.S_POST + 989 ")" + ":" 990 + natselsPost + " " 991 ), s); 992 } 993 994 1000 public void testToString_1() 1001 throws Exception { 1002 Configuration conf = new ConfigurationForTest(); 1003 conf.getGeneticOperators().clear(); 1004 conf.getNaturalSelectors(false).clear(); 1005 conf.getNaturalSelectors(true).clear(); 1006 privateAccessor.setField(conf, "m_eventManager", null); 1007 String s = trimString(conf.toString()); 1008 String eventmgr = conf.S_NONE; 1009 String genops = conf.S_NONE; 1010 String natselsPre = conf.S_NONE; 1012 String natselsPost = conf.S_NONE; 1014 assertEquals(trimString(conf.S_CONFIGURATION + ":" 1015 + conf.S_CONFIGURATION_NAME + ":" 1016 + conf.getName() + " " 1017 + conf.S_POPULATION_SIZE + ":" 1018 + conf.getPopulationSize() + " " 1019 + conf.S_MINPOPSIZE + ":" 1020 + conf.getMinimumPopSizePercent() + " " 1021 + conf.S_CHROMOSOME_SIZE + ":" 1022 + conf.getChromosomeSize() + " " 1023 + conf.S_SAMPLE_CHROM + ":" 1024 + conf.S_SIZE + ":" 1025 + conf.getSampleChromosome().size() + " " 1026 + conf.S_TOSTRING + ":" 1027 + conf.getSampleChromosome().toString() + " " 1028 + conf.S_RANDOM_GENERATOR + ":" 1029 + conf.getRandomGenerator().getClass().getName() + 1030 " " 1031 + conf.S_EVENT_MANAGER + ":" 1032 + eventmgr + " " 1033 + conf.S_CONFIGURATION_HANDLER + ":" 1034 + conf.getConfigurationHandler().getName() + " " 1035 + conf.S_FITNESS_FUNCTION + ":" 1036 + conf.getFitnessFunction().getClass().getName() + 1037 " " 1038 + conf.S_FITNESS_EVALUATOR + ":" 1039 + conf.getFitnessEvaluator().getClass().getName() + 1040 " " 1041 + conf.S_GENETIC_OPERATORS + ":" 1042 + genops + " " 1043 + conf.S_NATURAL_SELECTORS + "(" + conf.S_PRE + ")" + 1044 ":" 1045 + natselsPre + " " 1046 + conf.S_NATURAL_SELECTORS + "(" + conf.S_POST + 1047 ")" + ":" 1048 + natselsPost + " " 1049 ), s); 1050 } 1051 1052 1060 private String trimString(String s1) { 1061 String result = s1; 1062 int index = 0; 1063 while (index < result.length()) { 1064 if (result.charAt(index) == '\n') { 1065 result = result.substring(0, index) + result.substring(index + 1); 1066 } 1067 else { 1068 if (result.charAt(index) == ':') { 1069 if (index < result.length() - 1) { 1070 if (result.charAt(index + 1) == ' ' || 1071 result.charAt(index + 1) == '\n') { 1072 result = result.substring(0, index + 1) + 1073 result.substring(index + 2); 1074 } 1075 else { 1076 index++; 1077 } 1078 } 1079 else { 1080 index++; 1081 } 1082 } 1083 else if (result.charAt(index) == ' ') { 1084 if (index == 0) { 1085 result = result.substring(1); 1086 } 1087 else if (index == result.length() - 1) { 1088 result = result.substring(0, result.length() - 1); 1089 } 1090 else 1091 if (result.charAt(index + 1) == ' ' || 1092 result.charAt(index + 1) == '\n') { 1093 result = result.substring(0, index) + result.substring(index + 1); 1094 } 1095 else { 1096 index++; 1097 } 1098 } 1099 else { 1100 index++; 1101 } 1102 } 1103 } 1104 return result; 1105 } 1106 1107 1110 public void testGetName_0() { 1111 Configuration conf = new Configuration("tEstName"); 1112 assertEquals("tEstName", conf.getName()); 1113 } 1114 1115 1118 public void testGetName_1() { 1119 Configuration conf = new Configuration("tEstName"); 1120 conf.setName("HallI"); 1121 assertEquals("HallI", conf.getName()); 1122 conf.setName("HallA"); 1123 assertEquals("HallA", conf.getName()); 1124 } 1125 1126 1129 public void testGetName_2() { 1130 Configuration conf = new Configuration(); 1131 assertEquals(null, conf.getName()); 1132 } 1133 1134 1138 public void testKeepPopSizeConstant_0() { 1139 Configuration conf = new Configuration(); 1140 assertEquals(true, conf.isKeepPopulationSizeConstant()); 1141 conf.setKeepPopulationSizeConstant(false); 1142 assertEquals(false, conf.isKeepPopulationSizeConstant()); 1143 conf.setKeepPopulationSizeConstant(true); 1144 assertEquals(true, conf.isKeepPopulationSizeConstant()); 1145 conf.setKeepPopulationSizeConstant(false); 1146 assertEquals(false, conf.isKeepPopulationSizeConstant()); 1147 assertEquals(JGAPFactory.class, conf.getJGAPFactory().getClass()); 1148 } 1149 1150 1156 public void testConstruct_0() { 1157 System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, 1158 "org.jgap.MyFactoryTest"); 1159 Configuration conf = new Configuration(); 1160 assertEquals(MyFactoryTest.class, conf.getJGAPFactory().getClass()); 1161 } 1162 1163 1169 public void testConstruct_1() { 1170 System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, 1171 "org.jgap.myFactoryTest"); 1172 try { 1173 new Configuration(); 1174 fail(); 1175 } catch (RuntimeException rex) { 1176 ; } 1178 } 1179 1180 1186 public void testConstruct_2() { 1187 System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, ""); 1188 Configuration conf = new Configuration(); 1189 assertEquals(JGAPFactory.class, conf.getJGAPFactory().getClass()); 1190 } 1191 1192 1198 public void testClone_0() 1199 throws Exception { 1200 Configuration conf = new Configuration(); 1201 conf.setFitnessFunction(new StaticFitnessFunction(2)); 1202 Gene gene = new BooleanGene(conf); 1203 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 1204 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 1205 conf.setRandomGenerator(new StockRandomGenerator()); 1206 conf.setEventManager(new EventManager()); 1207 conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); 1208 conf.addGeneticOperator(new MutationOperator(conf)); 1209 conf.setPopulationSize(1); 1210 assertEquals(conf, conf.clone()); 1211 } 1212 1213 1221 public void testClone_1() 1222 throws Exception { 1223 Configuration conf = new Configuration(); 1224 conf.setFitnessFunction(new StaticFitnessFunction(2)); 1225 Gene gene = new BooleanGene(conf); 1226 conf.setSampleChromosome(new Chromosome(conf, gene, 5)); 1227 conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); 1228 conf.setRandomGenerator(new StockRandomGenerator()); 1229 conf.setEventManager(new EventManager()); 1230 conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); 1231 conf.addGeneticOperator(new MutationOperator(conf)); 1232 conf.setPopulationSize(1); 1233 conf.clone(); 1234 conf.clone(); 1235 } 1236 1237 1245 public void testRemoveNaturalSelector_0() 1246 throws Exception { 1247 Configuration conf = new Configuration(); 1248 NaturalSelector selector = new BestChromosomesSelector(conf); 1249 conf.addNaturalSelector(selector, false); 1250 conf.removeNaturalSelectors(false); 1251 assertEquals(0, conf.getNaturalSelectors(false).size()); 1252 assertEquals(0, conf.getNaturalSelectorsSize(false)); 1253 } 1254 1255 1263 public void testRemoveNaturalSelector_1() 1264 throws Exception { 1265 Configuration conf = new Configuration(); 1266 NaturalSelector selector = new BestChromosomesSelector(conf); 1267 conf.addNaturalSelector(selector, !false); 1268 conf.removeNaturalSelectors(!false); 1269 assertEquals(0, conf.getNaturalSelectors(!false).size()); 1270 assertEquals(0, conf.getNaturalSelectorsSize(!false)); 1271 } 1272 1273 1279 public void testRemoveNaturalSelector_2() 1280 throws Exception { 1281 Configuration conf = new Configuration(); 1282 NaturalSelector selector = new BestChromosomesSelector(conf); 1283 conf.addNaturalSelector(selector, true); 1284 conf.removeNaturalSelectors(false); 1285 assertEquals(0, conf.getNaturalSelectors(false).size()); 1286 assertEquals(0, conf.getNaturalSelectorsSize(false)); 1287 assertEquals(1, conf.getNaturalSelectors(true).size()); 1288 assertEquals(1, conf.getNaturalSelectorsSize(true)); 1289 } 1290 1291 1297 public void testRemoveNaturalSelector_3() 1298 throws Exception { 1299 Configuration conf = new Configuration(); 1300 NaturalSelector selector = new BestChromosomesSelector(conf); 1301 conf.addNaturalSelector(selector, false); 1302 conf.removeNaturalSelectors(true); 1303 assertEquals(0, conf.getNaturalSelectors(true).size()); 1304 assertEquals(0, conf.getNaturalSelectorsSize(true)); 1305 assertEquals(1, conf.getNaturalSelectors(false).size()); 1306 assertEquals(1, conf.getNaturalSelectorsSize(false)); 1307 } 1308 1309} 1310class MyFactoryTest 1311 extends JGAPFactory { 1312 public MyFactoryTest() { 1313 super(false); 1314 } 1315} 1316class TestBulkFitnessFunction 1317 extends BulkFitnessFunction { 1318 public void evaluate(Population a_subjects) { 1319 } 1320 1321 public int hashCode() { 1322 return -199; 1323 } 1324} 1325class TestBulkFitnessFunction2 1326 extends BulkFitnessFunction { 1327 public void evaluate(Population a_subjects) { 1328 } 1329 1330 public int hashCode() { 1331 return -297; 1332 } 1333} 1334class MyThread 1335 implements Runnable { 1336 public void run() { 1337 Configuration conf = new Configuration(); 1338 try { 1339 conf.setFitnessFunction(new StaticFitnessFunction(2.3)); 1340 Thread.sleep(100); 1341 } catch (Exception ex) { 1342 ex.printStackTrace(); 1343 } 1344 } 1345} 1346class MyThreadBulk 1347 implements Runnable { 1348 public void run() { 1349 Configuration conf = new Configuration(); 1350 try { 1351 conf.setBulkFitnessFunction(new TestBulkFitnessFunction()); 1352 Thread.sleep(100); 1353 } catch (Exception ex) { 1354 ex.printStackTrace(); 1355 } 1356 } 1357} 1358class MyThreadEvent 1359 implements Runnable { 1360 public void run() { 1361 Configuration conf = new Configuration(); 1362 try { 1363 conf.setEventManager(new EventManager()); 1364 Thread.sleep(100); 1365 } catch (Exception ex) { 1366 ex.printStackTrace(); 1367 } 1368 } 1369} 1370class TestEventManager 1371 extends EventManager { 1372} 1373 | Popular Tags |