1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 import junitx.util.*; 16 17 23 public class ThresholdSelectorTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.16 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(ThresholdSelectorTest.class); 30 return suite; 31 } 32 33 public void testConstruct_0() 34 throws Exception { 35 try { 36 new ThresholdSelector(null, 1.1d); 37 fail(); 38 } 39 catch (IllegalArgumentException ex) { 40 ; } 42 } 43 44 public void testConstruct_1() 45 throws Exception { 46 try { 47 new ThresholdSelector(null, -0.5d); 48 fail(); 49 } 50 catch (IllegalArgumentException ex) { 51 ; } 53 } 54 55 public void testConstruct_2() 56 throws Exception { 57 ThresholdSelector selector = new ThresholdSelector(null, 0.5d); 58 Double m_bestChroms_Percentage = (Double ) getNestedField(selector, 59 "m_config", "m_bestChroms_Percentage"); 60 assertEquals(0.5d, m_bestChroms_Percentage.doubleValue(), DELTA); 61 assertFalse(selector.returnsUniqueChromosomes()); 62 Object m_fitnessValueComparator = privateAccessor.getField(selector, 63 "m_fitnessValueComparator"); 64 assertTrue(m_fitnessValueComparator != null); 65 } 66 67 73 public void testConstruct_3() 74 throws Exception { 75 Genotype.setStaticConfiguration(conf); 76 ThresholdSelector op = new ThresholdSelector(); 77 assertSame(conf, op.getConfiguration()); 78 } 79 80 86 public void testAdd_0() 87 throws Exception { 88 ThresholdSelector selector = new ThresholdSelector(conf, 0.5d); 89 Gene gene = new BooleanGene(conf); 90 Chromosome chrom = new Chromosome(conf, gene, 5); 91 selector.add(chrom); 92 List chromosomes = ( (Vector) PrivateAccessor.getField(selector, 93 "m_chromosomes")); 94 assertEquals(1, chromosomes.size()); 95 assertEquals(chrom, chromosomes.get(0)); 96 selector.add(chrom); 97 assertEquals(chrom, chromosomes.get(0)); 98 assertEquals(2, chromosomes.size()); 99 selector.add(chrom); 100 assertEquals(3, chromosomes.size()); 101 } 102 103 110 public void testSelect_0() 111 throws Exception { 112 ThresholdSelector selector = new ThresholdSelector(conf, 0.3d); 113 Gene gene = new IntegerGene(conf); 114 gene.setAllele(new Integer (444)); 115 Chromosome secondBestChrom = new Chromosome(conf, gene, 3); 116 secondBestChrom.setFitnessValue(11); 117 selector.add(secondBestChrom); 118 gene = new BooleanGene(conf); 119 gene.setAllele(Boolean.valueOf(false)); 120 Chromosome bestChrom = new Chromosome(conf, gene, 3); 121 bestChrom.setFitnessValue(12); 122 selector.add(bestChrom); 123 selector.select(1, null, new Population(conf)); 124 } 125 126 132 public void testSelect_1() 133 throws Exception { 134 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 135 Gene gene = new BooleanGene(conf); 138 gene.setAllele(Boolean.valueOf(true)); 139 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 140 thirdBestChrom.setFitnessValue(10); 141 selector.add(thirdBestChrom); 142 gene = new BooleanGene(conf); 145 gene.setAllele(Boolean.valueOf(false)); 146 Chromosome bestChrom = new Chromosome(conf, gene, 3); 147 bestChrom.setFitnessValue(12); 148 selector.add(bestChrom); 149 gene = new IntegerGene(conf); 152 gene.setAllele(new Integer (444)); 153 Chromosome secondBestChrom = new Chromosome(conf, gene, 3); 154 secondBestChrom.setFitnessValue(11); 155 selector.add(secondBestChrom); 156 Population pop = new Population(conf); 159 selector.select(1, null, pop); 160 IChromosome[] bestChroms = pop.toChromosomes(); 161 assertEquals(1, bestChroms.length); 162 assertEquals(bestChrom, bestChroms[0]); 163 pop.getChromosomes().clear(); 166 selector.select(3, null, pop); 167 bestChroms = pop.toChromosomes(); 168 assertEquals(3, bestChroms.length); 169 assertEquals(bestChrom, bestChroms[0]); 170 assertEquals(secondBestChrom, bestChroms[1]); 171 assertEquals(thirdBestChrom, bestChroms[2]); 172 } 173 174 181 public void testSelect_2() 182 throws Exception { 183 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 184 Gene gene = new BooleanGene(conf); 187 gene.setAllele(Boolean.valueOf(true)); 188 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 189 thirdBestChrom.setFitnessValue(10); 190 selector.add(thirdBestChrom); 191 gene = new BooleanGene(conf); 194 gene.setAllele(Boolean.valueOf(false)); 195 Chromosome bestChrom = new Chromosome(conf, gene, 3); 196 bestChrom.setFitnessValue(12); 197 selector.add(bestChrom); 198 Population pop = new Population(conf); 201 selector.select(1, null, pop); 202 IChromosome[] bestChroms = pop.toChromosomes(); 203 assertEquals(1, bestChroms.length); 204 assertEquals(bestChrom, bestChroms[0]); 205 pop.getChromosomes().clear(); 208 selector.select(30, null, pop); 209 bestChroms = pop.toChromosomes(); 210 assertEquals(30, bestChroms.length); 211 assertEquals(bestChrom, bestChroms[0]); 212 assertEquals(thirdBestChrom, bestChroms[1]); 213 assertTrue(bestChrom == bestChroms[0]); 214 } 215 216 223 public void testSelect_3() 224 throws Exception { 225 conf.setRandomGenerator(new RandomGeneratorForTest(1)); 229 ThresholdSelector selector = new ThresholdSelector(conf, 0.0d); 230 Gene gene = new BooleanGene(conf); 233 gene.setAllele(Boolean.valueOf(true)); 234 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 235 thirdBestChrom.setFitnessValue(10); 236 selector.add(thirdBestChrom); 237 gene = new BooleanGene(conf); 240 gene.setAllele(Boolean.valueOf(false)); 241 Chromosome bestChrom = new Chromosome(conf, gene, 3); 242 bestChrom.setFitnessValue(12); 243 selector.add(bestChrom); 244 Population pop = new Population(conf); 247 selector.select(1, null, pop); 248 IChromosome[] bestChroms = pop.toChromosomes(); 249 assertFalse(bestChroms[0].equals(bestChrom)); 250 } 251 252 259 public void testSelect_4() 260 throws Exception { 261 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 262 Gene gene = new BooleanGene(conf); 265 gene.setAllele(Boolean.valueOf(true)); 266 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 267 thirdBestChrom.setFitnessValue(10); 268 selector.add(thirdBestChrom); 269 gene = new BooleanGene(conf); 272 gene.setAllele(Boolean.valueOf(false)); 273 Chromosome bestChrom = new Chromosome(conf, gene, 3); 274 bestChrom.setFitnessValue(12); 275 selector.add(bestChrom); 276 Population pop = new Population(conf); 279 selector.select(30, null, pop); 280 Population bestChroms = pop; 281 List chromosomes = (Vector) PrivateAccessor.getField(selector, 282 "m_chromosomes"); 283 assertFalse(bestChroms.equals(chromosomes)); 284 } 285 286 294 public void testSelect_5() 295 throws Exception { 296 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 297 Gene gene = new BooleanGene(conf); 300 gene.setAllele(Boolean.valueOf(true)); 301 Chromosome thirdBestChrom = new Chromosome(conf, gene, 7); 302 thirdBestChrom.setFitnessValue(10); 303 selector.add(thirdBestChrom); 304 gene = new BooleanGene(conf); 307 gene.setAllele(Boolean.valueOf(false)); 308 Chromosome bestChrom = new Chromosome(conf, gene, 3); 309 bestChrom.setFitnessValue(12); 310 selector.add(bestChrom); 311 Population pop = new Population(conf); 314 selector.select(1, null, pop); 315 IChromosome[] bestChroms = pop.toChromosomes(); 316 assertEquals(1, bestChroms.length); 317 assertEquals(bestChrom, bestChroms[0]); 318 selector.select(30, pop, pop); 321 bestChroms = pop.toChromosomes(); 322 assertEquals(31, bestChroms.length); 323 assertEquals(bestChrom, bestChroms[0]); 324 assertEquals(thirdBestChrom, bestChroms[3]); 325 assertTrue(bestChrom == bestChroms[0]); 326 } 327 328 334 public void testEmpty_0() 335 throws Exception { 336 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 337 Gene gene = new BooleanGene(conf); 338 Chromosome chrom = new Chromosome(conf, gene, 5); 339 selector.add(chrom); 340 selector.empty(); 341 Boolean needsSorting = (Boolean ) PrivateAccessor.getField(selector, 342 "m_needsSorting"); 343 assertEquals(Boolean.FALSE, needsSorting); 344 List chromosomes = ( (Vector) PrivateAccessor.getField(selector, 345 "m_chromosomes")); 346 assertEquals(0, chromosomes.size()); 347 } 348 349 356 public void testEmpty_1() 357 throws Exception { 358 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 359 Gene gene = new BooleanGene(conf); 360 Chromosome chrom = new Chromosome(conf, gene, 5); 361 Population pop = new Population(conf, 1); 362 pop.addChromosome(chrom); 363 selector.add(chrom); 364 Population popNew = new Population(conf); 365 selector.select(1, null, popNew); 366 selector.empty(); 367 assertEquals(1, popNew.size()); 368 assertNotNull(popNew.getChromosome(0)); 369 } 370 371 378 public void testEmpty_2() 379 throws Exception { 380 ThresholdSelector selector = new ThresholdSelector(conf, 1.0d); 381 Gene gene = new BooleanGene(conf); 382 Chromosome chrom = new Chromosome(conf, gene, 5); 383 Population pop = new Population(conf, 1); 384 pop.addChromosome(chrom); 385 selector.add(chrom); 386 Population popNew = new Population(conf); 387 selector.select(1, null, popNew); 388 pop = popNew; 389 selector.empty(); 390 assertEquals(1, pop.size()); 391 assertNotNull(pop.getChromosome(0)); 392 } 393 } 394 | Popular Tags |