1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class TournamentSelectorTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.12 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(TournamentSelectorTest.class); 29 return suite; 30 } 31 32 39 public void testConstruct_0() 40 throws Exception { 41 TournamentSelector sel = new TournamentSelector(null, 1, 1.0d); 42 assertNotNull(privateAccessor.getField(sel, "m_chromosomes")); 43 assertNotNull(privateAccessor.getField(sel, "m_fitnessValueComparator")); 44 new TournamentSelector(null, 1, 0.5d); 45 new TournamentSelector(null, 10, 0.00001d); 46 new TournamentSelector(null, 50, 0.4d); 47 } 48 49 56 public void testConstruct_1() 57 throws Exception { 58 try { 59 new TournamentSelector(null, 0, 0.5d); 60 fail(); 61 } 62 catch (IllegalArgumentException iex) { 63 ; } 65 } 66 67 74 public void testConstruct_2() 75 throws Exception { 76 try { 77 new TournamentSelector(null, -1, 0.5d); 78 fail(); 79 } 80 catch (IllegalArgumentException iex) { 81 ; } 83 } 84 85 92 public void testConstruct_3() 93 throws Exception { 94 try { 95 new TournamentSelector(null, 4, 0.0d); 96 fail(); 97 } 98 catch (IllegalArgumentException iex) { 99 ; } 101 } 102 103 110 public void testConstruct_4() 111 throws Exception { 112 try { 113 new TournamentSelector(null, 4, 1.0001d); 114 fail(); 115 } 116 catch (IllegalArgumentException iex) { 117 ; } 119 } 120 121 public void testAdd_0() 122 throws Exception { 123 TournamentSelector selector = new TournamentSelector(conf, 5, 0.5d); 124 Gene gene = new BooleanGene(conf); 125 IChromosome chrom = new Chromosome(conf, gene, 5); 126 selector.add(chrom); 127 List chromosomes = ( (Vector) privateAccessor.getField(selector, 128 "m_chromosomes")); 129 assertEquals(1, chromosomes.size()); 130 assertEquals(chrom, chromosomes.get(0)); 131 selector.add(chrom); 132 assertEquals(chrom, chromosomes.get(0)); 133 assertEquals(2, chromosomes.size()); 134 selector.add(chrom); 135 assertEquals(3, chromosomes.size()); 136 } 137 138 public void testEmpty_0() 139 throws Exception { 140 TournamentSelector selector = new TournamentSelector(conf, 4, 0.1d); 141 Gene gene = new BooleanGene(conf); 142 IChromosome chrom = new Chromosome(conf, gene, 5); 143 selector.add(chrom); 144 selector.empty(); 145 List chromosomes = ( (Vector) privateAccessor.getField(selector, 146 "m_chromosomes")); 147 assertEquals(0, chromosomes.size()); 148 } 149 150 157 public void testEmpty_1() 158 throws Exception { 159 TournamentSelector selector = new TournamentSelector(conf, 4, 0.1d); 160 Gene gene = new BooleanGene(conf); 161 Chromosome chrom = new Chromosome(conf, gene, 5); 162 Population pop = new Population(conf, 1); 163 pop.addChromosome(chrom); 164 selector.add(chrom); 165 Population popNew = new Population(conf); 166 selector.select(1, null, popNew); 167 selector.empty(); 168 assertEquals(1, popNew.size()); 169 assertNotNull(popNew.getChromosome(0)); 170 } 171 172 179 public void testEmpty_2() 180 throws Exception { 181 TournamentSelector selector = new TournamentSelector(conf, 10, 1.0d); 182 Gene gene = new BooleanGene(conf); 183 Chromosome chrom = new Chromosome(conf, gene, 5); 184 Population pop = new Population(conf, 1); 185 pop.addChromosome(chrom); 186 selector.add(chrom); 187 Population popNew = new Population(conf); 188 selector.select(1, null, popNew); 189 pop = popNew; 190 selector.empty(); 191 assertEquals(1, pop.size()); 192 assertNotNull(pop.getChromosome(0)); 193 } 194 195 202 public void testSelect_0() 203 throws Exception { 204 TournamentSelector selector = new TournamentSelector(conf, 4, 0.3d); 205 Gene gene = new IntegerGene(conf); 206 gene.setAllele(new Integer (444)); 207 Chromosome secondBestChrom = new Chromosome(conf, gene, 3); 208 secondBestChrom.setFitnessValue(11); 209 selector.add(secondBestChrom); 210 gene = new BooleanGene(conf); 211 gene.setAllele(Boolean.valueOf(false)); 212 Chromosome bestChrom = new Chromosome(conf, gene, 3); 213 bestChrom.setFitnessValue(12); 214 selector.add(bestChrom); 215 selector.select(1, null, new Population(conf)); 216 } 217 218 226 public void testSelect_1() 227 throws Exception { 228 RandomGeneratorForTest rn = new RandomGeneratorForTest(1); 230 conf.setRandomGenerator(rn); 231 TournamentSelector selector = new TournamentSelector(conf, 4, 1.0d); 232 Gene gene = new BooleanGene(conf); 235 gene.setAllele(Boolean.valueOf(true)); 236 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 237 thirdBestChrom.setFitnessValue(10); 238 selector.add(thirdBestChrom); 239 gene = new BooleanGene(conf); 242 gene.setAllele(Boolean.valueOf(false)); 243 Chromosome bestChrom = new Chromosome(conf, gene, 3); 244 bestChrom.setFitnessValue(12); 245 selector.add(bestChrom); 246 gene = new IntegerGene(conf); 249 gene.setAllele(new Integer (444)); 250 Chromosome secondBestChrom = new Chromosome(conf, gene, 3); 251 secondBestChrom.setFitnessValue(11); 252 selector.add(secondBestChrom); 253 Population pop = new Population(conf); 256 selector.select(1, null, pop); 257 IChromosome[] bestChroms = pop.toChromosomes(); 258 assertEquals(1, bestChroms.length); 259 assertEquals(bestChrom, bestChroms[0]); 260 pop.getChromosomes().clear(); 263 selector.select(3, null, pop); 264 bestChroms = pop.toChromosomes(); 265 assertEquals(3, bestChroms.length); 266 assertEquals(bestChrom, bestChroms[0]); 267 assertEquals(bestChrom, bestChroms[1]); 268 assertEquals(bestChrom, bestChroms[2]); 269 } 270 271 279 public void testSelect_2() 280 throws Exception { 281 RandomGeneratorForTest rn = new RandomGeneratorForTest(1); 283 conf.setRandomGenerator(rn); 284 TournamentSelector selector = new TournamentSelector(conf, 4, 1.0d); 285 Gene gene = new BooleanGene(conf); 288 gene.setAllele(Boolean.valueOf(true)); 289 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 290 thirdBestChrom.setFitnessValue(10); 291 selector.add(thirdBestChrom); 292 gene = new BooleanGene(conf); 295 gene.setAllele(Boolean.valueOf(false)); 296 Chromosome bestChrom = new Chromosome(conf, gene, 3); 297 bestChrom.setFitnessValue(12); 298 selector.add(bestChrom); 299 Population pop = new Population(conf); 302 selector.select(1, null, pop); 303 IChromosome[] bestChroms = pop.toChromosomes(); 304 assertEquals(1, bestChroms.length); 305 assertEquals(bestChrom, bestChroms[0]); 306 pop.getChromosomes().clear(); 309 selector.select(30, null, pop); 310 bestChroms = pop.toChromosomes(); 311 assertEquals(30, bestChroms.length); 312 assertEquals(bestChrom, bestChroms[0]); 313 assertEquals(bestChrom, bestChroms[1]); 314 assertTrue(bestChrom == bestChroms[0]); 315 } 316 317 323 public void testSelect_3() 324 throws Exception { 325 RandomGeneratorForTest rn = new RandomGeneratorForTest(0); 329 rn.setNextDouble(0.0d); 330 conf.setRandomGenerator(rn); 331 TournamentSelector selector = new TournamentSelector(conf, 2, 1.0d); 332 Gene gene = new BooleanGene(conf); 335 gene.setAllele(Boolean.valueOf(true)); 336 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 337 thirdBestChrom.setFitnessValue(10); 338 selector.add(thirdBestChrom); 339 gene = new BooleanGene(conf); 342 gene.setAllele(Boolean.valueOf(false)); 343 Chromosome bestChrom = new Chromosome(conf, gene, 3); 344 bestChrom.setFitnessValue(12); 345 selector.add(bestChrom); 346 Population pop = new Population(conf); 349 selector.select(1, null, pop); 350 IChromosome[] bestChroms = pop.toChromosomes(); 351 assertFalse(bestChroms[0].equals(bestChrom)); 352 } 353 354 361 public void testSelect_4() 362 throws Exception { 363 TournamentSelector selector = new TournamentSelector(conf, 1, 0.2d); 364 Gene gene = new BooleanGene(conf); 367 gene.setAllele(Boolean.valueOf(true)); 368 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 369 thirdBestChrom.setFitnessValue(10); 370 selector.add(thirdBestChrom); 371 gene = new BooleanGene(conf); 374 gene.setAllele(Boolean.valueOf(false)); 375 Chromosome bestChrom = new Chromosome(conf, gene, 3); 376 bestChrom.setFitnessValue(12); 377 selector.add(bestChrom); 378 Population pop = new Population(conf); 381 selector.select(30, null, pop); 382 Population bestChroms = pop; 383 List chromosomes = (Vector) privateAccessor.getField(selector, 384 "m_chromosomes"); 385 assertFalse(bestChroms.equals(chromosomes)); 386 } 387 388 396 public void testSelect_5() 397 throws Exception { 398 RandomGeneratorForTest rn = new RandomGeneratorForTest(0); 399 conf.setRandomGenerator(rn); 400 TournamentSelector selector = new TournamentSelector(conf, 4, 0.00001d); 401 setNestedField(selector, "m_config", "m_probability", new Double (0.0d)); 402 Gene gene = new BooleanGene(conf); 405 gene.setAllele(Boolean.valueOf(true)); 406 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 407 thirdBestChrom.setFitnessValue(10); 408 selector.add(thirdBestChrom); 409 gene = new BooleanGene(conf); 412 gene.setAllele(Boolean.valueOf(false)); 413 IChromosome bestChrom = new Chromosome(conf, gene, 3); 414 bestChrom.setFitnessValue(12); 415 selector.add(bestChrom); 416 Population pop = new Population(conf); 419 IChromosome[] bestChroms; 420 selector.select(5, null, pop); 421 bestChroms = pop.toChromosomes(); 422 assertEquals(thirdBestChrom, bestChroms[0]); 423 assertEquals(thirdBestChrom, bestChroms[1]); 424 } 425 426 432 public void testSelect_6() 433 throws Exception { 434 RandomGeneratorForTest rn = new RandomGeneratorForTest(1); 436 conf.setRandomGenerator(rn); 437 TournamentSelector selector = new TournamentSelector(conf, 4, 1.0d); 438 Population toAddFrom = new Population(conf); 439 Gene gene = new BooleanGene(conf); 442 gene.setAllele(Boolean.valueOf(true)); 443 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 444 thirdBestChrom.setFitnessValue(10); 445 toAddFrom.addChromosome(thirdBestChrom); 446 gene = new BooleanGene(conf); 449 gene.setAllele(Boolean.valueOf(false)); 450 Chromosome bestChrom = new Chromosome(conf, gene, 3); 451 bestChrom.setFitnessValue(12); 452 toAddFrom.addChromosome(bestChrom); 453 gene = new IntegerGene(conf); 456 gene.setAllele(new Integer (444)); 457 Chromosome secondBestChrom = new Chromosome(conf, gene, 3); 458 secondBestChrom.setFitnessValue(11); 459 toAddFrom.addChromosome(secondBestChrom); 460 Population pop = new Population(conf); 463 selector.select(1, null, pop); 464 IChromosome[] bestChroms = pop.toChromosomes(); 465 assertEquals(0, bestChroms.length); 467 selector.select(1, toAddFrom, pop); 468 bestChroms = pop.toChromosomes(); 469 assertEquals(1, bestChroms.length); 470 assertEquals(bestChrom, bestChroms[0]); 471 pop.getChromosomes().clear(); 474 selector.select(3, toAddFrom, pop); 475 bestChroms = pop.toChromosomes(); 476 assertEquals(3, bestChroms.length); 477 assertEquals(bestChrom, bestChroms[0]); 478 assertEquals(bestChrom, bestChroms[1]); 479 assertEquals(bestChrom, bestChroms[2]); 480 } 481 482 486 public void testReturnsUniqueChromosomes_0() { 487 TournamentSelector selector = new TournamentSelector(null, 2, 0.5d); 488 assertFalse(selector.returnsUniqueChromosomes()); 489 } 490 491 497 public void testSetTournametSize_0() 498 throws Exception { 499 TournamentSelector sel = new TournamentSelector(null, 1, 1.0d); 500 sel.setTournamentSize(5); 501 assertEquals(5, sel.getTournamentSize()); 502 try { 503 sel.setTournamentSize(0); 504 fail(); 505 } 506 catch (IllegalArgumentException iex) { 507 ; } 509 } 510 511 517 public void testSetProbability_0() 518 throws Exception { 519 TournamentSelector sel = new TournamentSelector(null, 1, 1.0d); 520 sel.setProbability(0.6d); 521 assertEquals(0.6d, sel.getProbability(), DELTA); 522 try { 523 sel.setProbability(1.6d); 524 fail(); 525 } 526 catch (IllegalArgumentException iex) { 527 ; } 529 } 530 } 531 | Popular Tags |