1 10 package org.jgap; 11 12 import java.util.*; 13 import org.jgap.impl.*; 14 import junit.framework.*; 15 16 23 public class PopulationTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.37 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(PopulationTest.class); 30 return suite; 31 } 32 33 public void testConstruct_0() 34 throws Exception { 35 try { 36 new Population(new ConfigurationForTest(), (IChromosome[])null); 37 fail(); 38 } catch (NullPointerException e) { 39 ; } 41 } 42 43 public void testConstruct_1() 44 throws Exception { 45 try { 46 new Population(conf, -1); 47 } catch (IllegalArgumentException iae) { 48 ; } 50 } 51 52 public void testConstruct_2() 53 throws Exception { 54 try { 55 new Population(new ConfigurationForTest(), (IChromosome)null); 56 fail(); 57 } catch (IllegalArgumentException e) { 58 ; } 60 } 61 62 public void testConstruct_3() 63 throws Exception { 64 int nTot = 100; 65 Chromosome[] chromosomes = new Chromosome[nTot]; 66 Population pop = new Population(conf, chromosomes); 67 assertNotNull(pop); 68 assertEquals(nTot, pop.size()); 70 } 71 72 public void testAddChromosome_0() 73 throws Exception { 74 Gene g = new IntegerGene(conf); 75 Chromosome c = new Chromosome(conf, g, 29); 76 c.setFitnessValue(45); 77 Population p = new Population(conf); 78 p.addChromosome(c); 79 assertEquals(1, p.size()); 80 } 81 82 public void testAddChromosome_1() 83 throws Exception { 84 Population p = new Population(conf); 85 p.addChromosome(null); 86 assertEquals(0, p.size()); 87 } 88 89 public void testAddChromosomes_0() 90 throws Exception { 91 Gene g = new DoubleGene(conf); 92 Chromosome c = new Chromosome(conf, g, 10); 93 c.setFitnessValue(45); 94 Population p1 = new Population(conf); 95 p1.addChromosome(c); 96 Population p2 = new Population(conf); 97 p2.addChromosomes(p1); 98 assertEquals(p1.size(), p2.size()); 99 } 100 101 public void testAddChromosomes_1() 102 throws Exception { 103 Population p = new Population(conf); 104 p.addChromosomes(null); 105 assertEquals(0, p.size()); 106 } 107 108 public void testSetChromosome_0() 109 throws Exception { 110 Gene g = new DoubleGene(conf); 111 Chromosome c = new Chromosome(conf, g, 10); 112 Population p = new Population(conf); 113 p.setChromosome(0, c); 114 p.setChromosome(0, c); 115 assertEquals(1, p.size()); 116 try { 117 p.setChromosome(2, c); 118 fail(); 119 } catch (IndexOutOfBoundsException oex) { 120 ; } 122 } 123 124 public void testSetChromosome_1() 125 throws Exception { 126 Gene g = new DoubleGene(conf); 127 Chromosome c = new Chromosome(conf, g, 10); 128 Population p = new Population(conf); 129 p.addChromosome(c); 130 Chromosome c2 = new Chromosome(conf, g, 20); 131 p.setChromosome(0, c2); 132 assertEquals(1, p.size()); 133 assertEquals(p.getChromosome(0), c2); 134 assertFalse(c.equals(c2)); 135 } 136 137 public void testSetChromosomes_0() 138 throws Exception { 139 List chromosomes = new ArrayList(); 140 Gene g = null; 141 Chromosome c = null; 142 int nTot = 200; 143 for (int i = 0; i < nTot; i++) { 144 g = new DoubleGene(conf); 145 c = new Chromosome(conf, g, 10); 146 chromosomes.add(c); 147 } 148 Population p = new Population(conf); 149 p.setChromosomes(chromosomes); 150 assertEquals(nTot, p.size()); 151 } 152 153 public void testGetChromosomes_0() 154 throws Exception { 155 List chromosomes = new ArrayList(); 156 Gene g = null; 157 Chromosome c = null; 158 int nTot = 200; 159 for (int i = 0; i < nTot; i++) { 160 g = new DoubleGene(conf); 161 c = new Chromosome(conf, g, 10); 162 chromosomes.add(c); 163 } 164 Population p = new Population(conf); 165 p.setChromosomes(chromosomes); 166 assertEquals(chromosomes, p.getChromosomes()); 167 } 168 169 public void testGetChromosome_0() 170 throws Exception { 171 List chromosomes = new ArrayList(); 172 Gene g = null; 173 Chromosome c = null; 174 Chromosome thechosenone = null; 175 int nTot = 200; 176 for (int i = 0; i < nTot; i++) { 177 g = new DoubleGene(conf); 178 c = new Chromosome(conf, g, 10); 179 chromosomes.add(c); 180 if (i == 100) { 181 thechosenone = c; 182 } 183 } 184 Population p = new Population(conf); 185 p.setChromosomes(chromosomes); 186 assertEquals(thechosenone, p.getChromosome(100)); 187 } 188 189 public void testToChromosomes_0() 190 throws Exception { 191 List chromosomes = new ArrayList(); 192 Gene g = null; 193 Chromosome c = null; 194 int nTot = 200; 195 for (int i = 0; i < nTot; i++) { 196 g = new DoubleGene(conf); 197 c = new Chromosome(conf, g, 10); 198 c.getGene(0).setAllele(new Double (i)); 199 chromosomes.add(c); 200 } 201 Population p = new Population(conf); 202 p.setChromosomes(chromosomes); 203 IChromosome[] aChrom = p.toChromosomes(); 204 assertEquals(aChrom.length, chromosomes.size()); 205 Population newPop = new Population(conf, aChrom); 207 assertEquals(p, newPop); 208 assertEquals(newPop, p); 209 Collections.sort(chromosomes); 211 List toChromosomes = Arrays.asList(aChrom); 212 Collections.sort(toChromosomes); 213 assertEquals(chromosomes, toChromosomes); 214 Population newPop2 = new Population(conf); 216 newPop2.setChromosomes(toChromosomes); 217 assertEquals(p, newPop2); 218 assertEquals(newPop, newPop2); 219 assertEquals(newPop2, p); 220 assertEquals(newPop2, newPop); 221 } 222 223 230 public void testDetermineFittestChromosome_0() 231 throws Exception { 232 Population p = new Population(conf); 233 assertEquals(null, p.determineFittestChromosome()); 234 } 235 236 243 public void testDetermineFittestChromosome_1() 244 throws Exception { 245 List chromosomes = new ArrayList(); 246 Gene g = null; 247 Chromosome c = null; 248 Population p = new Population(conf); 249 g = new DoubleGene(conf); 250 c = new Chromosome(conf, g, 10); 251 c.setFitnessValue(5); 252 p.addChromosome(c); 253 chromosomes.add(c); 254 c = new Chromosome(conf, g, 3); 255 c.setFitnessValue(19); 256 p.addChromosome(c); 257 chromosomes.add(c); 258 c = new Chromosome(conf, g, 1); 259 c.setFitnessValue(11); 260 p.addChromosome(c); 261 chromosomes.add(c); 262 c = new Chromosome(conf, g, 8); 263 c.setFitnessValue(18); 264 p.addChromosome(c); 265 chromosomes.add(c); 266 Chromosome fittest = (Chromosome) chromosomes.get(1); 267 p.setChromosomes(chromosomes); 268 assertEquals(fittest, p.determineFittestChromosome()); 269 } 270 271 275 public void testDetermineFittestChromosome_2() 276 throws Exception { 277 List chromosomes = new ArrayList(); 278 Gene g = null; 279 Chromosome c = null; 280 Population p = new Population(conf); 281 int nTot = 100; 282 for (int i = 0; i < nTot; i++) { 283 g = new DoubleGene(conf); 284 c = new Chromosome(conf, g, 10); 285 c.setFitnessValue(i); 286 p.addChromosome(c); 287 chromosomes.add(c); 288 } 289 Chromosome fittest = (Chromosome) chromosomes.get(99); 290 p.setChromosomes(chromosomes); 291 assertEquals(fittest, p.determineFittestChromosome()); 292 } 293 294 302 public void testDetermineFittestChromosome_3() 303 throws Exception { 304 conf.resetProperty(Configuration.PROPERTY_FITEVAL_INST); 305 conf.setFitnessEvaluator(new DeltaFitnessEvaluator()); 306 List chromosomes = new ArrayList(); 307 Gene g = null; 308 Chromosome c = null; 309 Population p = new Population(conf); 310 int nTot = 100; 311 for (int i = 0; i < nTot; i++) { 312 g = new DoubleGene(conf); 313 c = new Chromosome(conf, g, 10); 314 c.setFitnessValue(i); 315 p.addChromosome(c); 316 chromosomes.add(c); 317 } 318 Chromosome fittest = (Chromosome) chromosomes.get(0); 319 p.setChromosomes(chromosomes); 320 assertEquals(fittest, p.determineFittestChromosome()); 321 } 322 323 329 public void testDetermineFittestChromosome_4() 330 throws Exception { 331 Population p = new Population(conf); 332 Gene g = new DoubleGene(conf); 333 Chromosome c = new Chromosome(conf, g, 10); 334 c.setFitnessValue(22); 335 p.addChromosome(c); 336 assertEquals(null, p.determineFittestChromosomes(0)); 337 assertEquals(c, p.determineFittestChromosomes(1).get(0)); 338 } 339 340 347 public void testDetermineFittestChromosome_5() 348 throws Exception { 349 List chromosomes = new ArrayList(); 350 Gene g = null; 351 Chromosome c = null; 352 Population p = new Population(conf); 353 conf.reset(); 354 conf.setFitnessEvaluator(new MyFitnessEvaluator()); 355 g = new DoubleGene(conf); 356 c = new Chromosome(conf, g, 10); 357 c.setFitnessValue(5); 358 chromosomes.add(c); 359 g = new DoubleGene(conf); 360 c = new Chromosome(conf, g, 10); 361 c.setFitnessValue(3); 362 chromosomes.add(c); 363 p.setChromosomes(chromosomes); 364 assertEquals(c, p.determineFittestChromosome()); 365 c = new Chromosome(conf, g, 10); 367 c.setFitnessValue(1); p.addChromosome(c); 369 assertEquals(c, p.determineFittestChromosome()); 370 } 371 372 378 public void testDetermineFittestChromosomes_1() 379 throws Exception { 380 Population p = getNewPopulation(conf); 381 assertEquals(1, p.determineFittestChromosomes(1).size()); 382 assertEquals(1, p.determineFittestChromosomes(1).size()); 383 assertEquals(3, p.determineFittestChromosomes(3).size()); 384 assertEquals(3, p.determineFittestChromosomes(3).size()); 385 assertEquals(5, p.determineFittestChromosomes(6).size()); 389 } 390 391 398 public void testDetermineFittestChromosomes_2() 399 throws Exception { 400 Population population = getNewPopulation(conf); 401 IChromosome topC = population.determineFittestChromosome(); 402 List top = population.determineFittestChromosomes(1); 403 assertEquals(topC, population.determineFittestChromosome()); 404 assertEquals(top.get(0), population.determineFittestChromosome()); 405 top = population.determineFittestChromosomes(2); 406 assertEquals(top.get(0), population.determineFittestChromosome()); 407 top = population.determineFittestChromosomes(3); 408 assertEquals(top.get(0), population.determineFittestChromosome()); 409 } 410 411 418 public void testDetermineFittestChromosomes_3() 419 throws Exception { 420 Population population = getNewPopulation(conf); 421 assertTrue(population.isChanged()); 422 List top = population.determineFittestChromosomes(1); 423 assertEquals(top.get(0), population.determineFittestChromosome()); 424 top = population.determineFittestChromosomes(2); 425 assertEquals(top.get(0), population.determineFittestChromosome()); 426 top = population.determineFittestChromosomes(3); 427 assertEquals(top.get(0), population.determineFittestChromosome()); 428 population = getNewPopulation(conf); 429 assertEquals(24, population.determineFittestChromosome().getFitnessValue(), 430 DELTA); 431 top = population.determineFittestChromosomes(5); 432 assertFalse(population.isChanged()); 433 assertEquals(24, ( (IChromosome) top.get(0)).getFitnessValue(), DELTA); 434 assertEquals(23, ( (IChromosome) top.get(1)).getFitnessValue(), DELTA); 435 assertEquals(23, ( (IChromosome) top.get(2)).getFitnessValue(), DELTA); 436 assertEquals(23, ( (IChromosome) top.get(3)).getFitnessValue(), DELTA); 437 assertEquals(22, ( (IChromosome) top.get(4)).getFitnessValue(), DELTA); 438 double oldFitness = population.getChromosome(0).getFitnessValue(); 439 for (int i = 1; i < population.size(); i++) { 440 double currFitness = population.getChromosome(i).getFitnessValue(); 441 assertTrue(currFitness <= oldFitness); 442 oldFitness = currFitness; 443 } 444 } 445 446 private static Population getNewPopulation(Configuration a_conf) 447 throws InvalidConfigurationException { 448 Population population = new Population(a_conf); 449 Gene g = new DoubleGene(a_conf); 450 Chromosome c = new Chromosome(a_conf, g, 10); 451 c.setFitnessValue(22); 452 population.addChromosome(c); 453 c = new Chromosome(a_conf, g, 10); 454 c.setFitnessValue(24); 455 population.addChromosome(c); 456 c = new Chromosome(a_conf, g, 10); 457 c.setFitnessValue(23); 458 population.addChromosome(c); 459 population.addChromosome(c); 460 population.addChromosome(c); 461 return population; 462 } 463 464 public void testSize_0() 465 throws Exception { 466 Population p = new Population(conf, 10); 467 assertEquals(0, p.size()); 469 Gene g = new DoubleGene(conf); 470 Chromosome c = new Chromosome(conf, g, 5); 471 p.addChromosome(c); 472 assertEquals(1, p.size()); 473 c = new Chromosome(conf, g, 3); 474 p.addChromosome(c); 475 assertEquals(2, p.size()); 476 } 477 478 public void testIterator_0() 479 throws Exception { 480 Population p = new Population(conf, 10); 481 Iterator it = p.iterator(); 482 assertFalse(it.hasNext()); 483 assertEquals(0, p.size()); 485 Gene g = new DoubleGene(conf); 486 Chromosome c = new Chromosome(conf, g, 5); 487 p.addChromosome(c); 488 assertTrue(it.hasNext()); 489 } 490 491 public void testContains_0() 492 throws Exception { 493 Gene g = new DoubleGene(conf); 494 Chromosome c = new Chromosome(conf, g, 10); 495 c.setFitnessValue(45); 496 Population p1 = new Population(conf); 497 assertFalse(p1.contains(c)); 498 assertFalse(p1.contains(null)); 499 p1.addChromosome(c); 500 assertTrue(p1.contains(c)); 501 assertFalse(p1.contains(null)); 502 assertFalse(p1.contains(new Chromosome(conf, g, 5))); 503 } 504 505 512 public void testGetGenome_0() 513 throws Exception { 514 Population pop = new Population(conf); 515 Gene g1 = new DoubleGene(conf); 516 Gene g2 = new StringGene(conf); 517 Chromosome c1 = new Chromosome(conf, new Gene[] { 518 g1, g2 519 }); 520 pop.addChromosome(c1); 521 List genes = pop.getGenome(true); 522 assertEquals(2, genes.size()); 523 assertEquals(g1, genes.get(0)); 524 assertEquals(g2, genes.get(1)); 525 genes = pop.getGenome(false); 526 assertEquals(2, genes.size()); 527 assertEquals(g1, genes.get(0)); 528 assertEquals(g2, genes.get(1)); 529 } 530 531 538 public void testGetGenome_1() 539 throws Exception { 540 Population pop = new Population(conf); 541 assertTrue(pop.isChanged()); 542 Gene g1 = new DoubleGene(conf); 543 Gene g2 = new StringGene(conf); 544 Chromosome c1 = new Chromosome(conf, new Gene[] {g1, g2}); 545 pop.addChromosome(c1); 546 Gene g3 = new BooleanGene(conf); 547 Gene g4 = new IntegerGene(conf, 0, 10); 548 Gene g5 = new FixedBinaryGene(conf, 4); 549 Chromosome c2 = new Chromosome(conf, new Gene[] {g3, g4, g5}); 550 pop.addChromosome(c2); 551 List genes = pop.getGenome(true); 552 assertEquals(5, genes.size()); 553 assertEquals(g1, genes.get(0)); 554 assertEquals(g2, genes.get(1)); 555 assertEquals(g3, genes.get(2)); 556 assertEquals(g4, genes.get(3)); 557 assertEquals(g5, genes.get(4)); 558 genes = pop.getGenome(false); 559 assertEquals(5, genes.size()); 560 assertEquals(g1, genes.get(0)); 561 assertEquals(g2, genes.get(1)); 562 assertEquals(g3, genes.get(2)); 563 assertEquals(g4, genes.get(3)); 564 assertEquals(g5, genes.get(4)); 565 } 566 567 574 public void testGetGenome_2() 575 throws Exception { 576 Population pop = new Population(conf); 577 Gene g1 = new DoubleGene(conf); 578 Gene g2 = new StringGene(conf); 579 Chromosome c1 = new Chromosome(conf, new Gene[] {g1, g2}); 580 pop.addChromosome(c1); 581 Gene g3 = new BooleanGene(conf); 582 CompositeGene g4 = new CompositeGene(conf); 583 Gene g5 = new FixedBinaryGene(conf, 4); 584 g4.addGene(g5); 585 Gene g6 = new DoubleGene(conf, 1.0d, 4.0d); 586 g4.addGene(g6); 587 Chromosome c2 = new Chromosome(conf, new Gene[] {g3, g4}); 588 pop.addChromosome(c2); 589 assertTrue(pop.isChanged()); 590 List genes = pop.getGenome(true); 592 assertEquals(5, genes.size()); 593 assertEquals(g1, genes.get(0)); 594 assertEquals(g2, genes.get(1)); 595 assertEquals(g3, genes.get(2)); 596 assertEquals(g5, genes.get(3)); 597 assertEquals(g6, genes.get(4)); 598 genes = pop.getGenome(false); 600 assertEquals(4, genes.size()); 601 assertEquals(g1, genes.get(0)); 602 assertEquals(g2, genes.get(1)); 603 assertEquals(g3, genes.get(2)); 604 assertEquals(g4, genes.get(3)); 605 } 606 607 615 public void testIsSerializable_0() 616 throws Exception { 617 assertTrue(isSerializable(new Population(conf))); 618 } 619 620 627 public void testDoSerialize_0() 628 throws Exception { 629 Chromosome[] chroms = new Chromosome[1]; 631 chroms[0] = new Chromosome(conf, new Gene[] { 632 new IntegerGene(conf, 1, 5) 633 }); 634 Population pop = new Population(conf, chroms); 635 assertEquals(pop, doSerialize(pop)); 636 assertTrue(pop.isChanged()); 637 } 638 639 645 public void testRemoveChromosome_0() 646 throws Exception { 647 Chromosome[] chroms = new Chromosome[1]; 648 chroms[0] = new Chromosome(conf, new Gene[] { 649 new IntegerGene(conf, 1, 5)}); 650 Population pop = new Population(conf, chroms); 651 assertEquals(chroms[0], pop.removeChromosome(0)); 652 assertEquals(0, pop.size()); 653 try { 654 pop.removeChromosome(0); 655 fail(); 656 } catch (IllegalArgumentException iex) { 657 ; } 659 } 660 661 668 public void testRemoveChromosome_1() 669 throws Exception { 670 Chromosome[] chroms = new Chromosome[1]; 671 chroms[0] = new Chromosome(conf, new Gene[] { 672 new IntegerGene(conf, 1, 5)}); 673 Population pop = new Population(conf, chroms); 674 try { 675 pop.removeChromosome(1); 676 fail(); 677 } catch (IllegalArgumentException iex) { 678 ; } 680 } 681 682 689 public void testRemoveChromosome_2() 690 throws Exception { 691 Chromosome[] chroms = new Chromosome[1]; 692 chroms[0] = new Chromosome(conf, new Gene[] { 693 new IntegerGene(conf, 1, 5)}); 694 Population pop = new Population(conf, chroms); 695 try { 696 pop.removeChromosome( -1); 697 fail(); 698 } catch (IllegalArgumentException iex) { 699 ; } 701 } 702 703 709 public void testRemoveChromosome_3() 710 throws Exception { 711 Chromosome[] chroms = new Chromosome[1]; 712 chroms[0] = new Chromosome(conf, new Gene[] { 713 new IntegerGene(conf, 1, 5)}); 714 Population pop = new Population(conf, chroms); 715 IChromosome c = pop.removeChromosome(0); 716 assertEquals(chroms[0], c); 717 assertTrue(pop.isChanged()); 718 } 719 720 726 public void testSortByFitness_0() 727 throws Exception { 728 IChromosome[] chroms = new Chromosome[3]; 729 chroms[0] = new Chromosome(conf, new Gene[] { 730 new DoubleGene(conf, 1, 5)}); 731 chroms[0].setFitnessValueDirectly(45d); 732 chroms[1] = new Chromosome(conf, new Gene[] { 733 new DoubleGene(conf, 1, 5)}); 734 chroms[1].setFitnessValueDirectly(41d); 735 chroms[2] = new Chromosome(conf, new Gene[] { 736 new DoubleGene(conf, 1, 5)}); 737 chroms[2].setFitnessValueDirectly(47d); 738 Population pop = new Population(conf, chroms); 739 assertTrue(pop.isChanged()); 740 assertFalse(pop.isSorted()); 741 pop.sortByFitness(); 742 assertFalse(pop.isChanged()); 743 assertTrue(pop.isSorted()); 744 double oldFitness = pop.getChromosome(0).getFitnessValue(); 745 for (int i = 1; i < pop.size(); i++) { 746 double currFitness = pop.getChromosome(i).getFitnessValue(); 747 assertTrue(currFitness <= oldFitness); 748 oldFitness = currFitness; 749 } 750 } 751 752 758 public void testSortByFitness_1() 759 throws Exception { 760 Population pop = new Population(conf); 761 Gene g = new DoubleGene(conf); 762 Chromosome c = new Chromosome(conf, g, 10); 763 c.setFitnessValue(4.5d); 764 pop.addChromosome(c); 765 g = new DoubleGene(conf); 766 c = new Chromosome(conf, g, 10); 767 c.setFitnessValue(4.1d); 768 pop.addChromosome(c); 769 g = new DoubleGene(conf); 770 c = new Chromosome(conf, g, 10); 771 c.setFitnessValue(4.7d); 772 pop.addChromosome(c); 773 assertTrue(pop.isChanged()); 774 assertFalse(pop.isSorted()); 775 pop.sortByFitness(); 776 assertFalse(pop.isChanged()); 777 assertTrue(pop.isSorted()); 778 double oldFitness = pop.getChromosome(0).getFitnessValue(); 779 for (int i = 1; i < pop.size(); i++) { 780 double currFitness = pop.getChromosome(i).getFitnessValue(); 781 assertTrue(currFitness <= oldFitness); 782 oldFitness = currFitness; 783 } 784 } 785 786 792 public void testCompareTo_0() 793 throws Exception { 794 Population pop = new Population(conf); 795 assertEquals(1, pop.compareTo(null)); 796 Population pop2 = new Population(conf); 797 assertEquals(0, pop.compareTo(pop2)); 798 pop2.addChromosome(new Chromosome(conf)); 799 assertEquals( -1, pop.compareTo(pop2)); 800 assertEquals(1, pop2.compareTo(pop)); 801 } 802 803 809 public void testPersistentRepresentation_0() 810 throws Exception { 811 Gene g = new IntegerGene(conf); 812 Chromosome c = new Chromosome(conf, g, 29); 813 c.setFitnessValue(45); 814 Population p = new Population(conf); 815 p.addChromosome(c); 816 String repr = p.getPersistentRepresentation(); 817 Population q = new Population(conf); 818 q.setValueFromPersistentRepresentation(repr); 819 assertEquals(p, q); 820 assertEquals(p.getPersistentRepresentation(), q.getPersistentRepresentation()); 821 } 822 823 830 public void testPersistentRepresentation_1() 831 throws Exception { 832 Gene[] genes1 = new Gene[2]; 833 genes1[0] = new StringGene(conf); 834 genes1[1] = new DoubleGene(conf); 835 Chromosome chrom = new Chromosome(conf, genes1); 836 Population pop = new Population(conf, chrom); 837 String repr = pop.getPersistentRepresentation(); 838 Population q = new Population(conf); 839 q.setValueFromPersistentRepresentation(repr); 840 assertEquals(pop, q); 841 assertEquals(pop.getPersistentRepresentation(), 842 q.getPersistentRepresentation()); 843 } 844 845 851 public void testPersistentRepresentation_2() 852 throws Exception { 853 Population pop = new Population(conf); 854 pop.setValueFromPersistentRepresentation(null); 855 assertEquals(0, pop.size()); 856 } 857 858 864 public void testPersistentRepresentation_3() 865 throws Exception { 866 Population pop = new Population(conf); 867 try { 868 pop.setValueFromPersistentRepresentation("1" 869 + Population.CHROM_DELIMITER 870 + "2"); 871 fail(); 872 } catch (UnsupportedRepresentationException uex) { 873 ; } 875 } 876 877 883 public void testPersistentRepresentation_4() 884 throws Exception { 885 Population pop = new Population(conf); 886 try { 887 pop.setValueFromPersistentRepresentation("1" 888 + Population.CHROM_DELIMITER 889 + "0" 890 + Population.CHROM_DELIMITER); 891 fail(); 892 } catch (UnsupportedRepresentationException uex) { 893 ; } 895 } 896 897 905 public void testPersistentRepresentation_5() 906 throws Exception { 907 Population pop = new Population(conf); 908 pop.setValueFromPersistentRepresentation(Population.CHROM_DELIMITER_HEADING 909 + Chromosome.class.getName() 910 + Population.CHROM_DELIMITER 911 + "47.11" 912 + Chromosome.CHROM_DELIMITER 913 + "1" 914 + Chromosome.CHROM_DELIMITER 915 + "<" + IntegerGene.class.getName() 916 + Chromosome.GENE_DELIMITER 917 + "2:4:4>" 918 + Population.CHROM_DELIMITER_CLOSING); 919 assertEquals(1, pop.size()); 920 IChromosome chrom = pop.getChromosome(0); 921 assertEquals(1, chrom.size()); 922 } 923 924 932 public void testPersistentRepresentation_6() 933 throws Exception { 934 Population pop = new Population(conf); 935 try { 936 pop.setValueFromPersistentRepresentation(Population. 937 CHROM_DELIMITER_HEADING 938 + Chromosome.class.getName() 939 + Population.CHROM_DELIMITER 940 + "47.11" 941 + Chromosome.CHROM_DELIMITER 942 + "1" 943 + Chromosome.CHROM_DELIMITER 944 + "<" + IntegerGene.class.getName() 945 + Chromosome.GENE_DELIMITER 946 + "2:4:4>/"); 947 fail(); 948 } catch (UnsupportedRepresentationException uex) { 949 ; } 951 } 952 953 class MyFitnessEvaluator 954 implements FitnessEvaluator { 955 public boolean isFitter(final double a_fitness_value1, 956 final double a_fitness_value2) { 957 return a_fitness_value1 < a_fitness_value2; 958 } 959 960 public boolean isFitter(IChromosome a_chrom1, IChromosome a_chrom2) { 961 return isFitter(a_chrom1.getFitnessValue(), a_chrom2.getFitnessValue()); 962 } 963 } 964 } 965 | Popular Tags |