1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class WeightedRouletteSelectorTest 23 extends JGAPTestCase { 24 25 private final static String CVS_REVISION = "$Revision: 1.27 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(WeightedRouletteSelectorTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 40 public void testConstruct_0() { 41 new WeightedRouletteSelector(); 42 } 43 44 51 public void testAdd_0() 52 throws Exception { 53 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 54 Configuration conf = new DefaultConfiguration(); 55 Gene gene = new BooleanGene(conf); 56 Chromosome chrom = new Chromosome(conf, gene, 5); 57 conf.setFitnessFunction(new TestFitnessFunction()); 58 conf.setSampleChromosome(chrom); 59 conf.setPopulationSize(5); 60 selector.add(chrom); 61 Map chromosomes = (Map) privateAccessor.getField(selector, 62 "m_wheel"); 63 assertEquals(1, chromosomes.size()); 64 Iterator it = chromosomes.keySet().iterator(); 65 assertEquals(chrom, it.next()); 66 selector.add(chrom); 67 assertEquals(1, chromosomes.size()); 68 it = chromosomes.keySet().iterator(); 69 assertEquals(chrom, it.next()); 70 } 71 72 78 public void testSelect_0() 79 throws Exception { 80 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 81 Gene gene = new BooleanGene(conf); 82 gene.setAllele(Boolean.valueOf(true)); 83 Chromosome bestChrom = new Chromosome(conf, gene, 7); 84 bestChrom.setFitnessValue(10); 85 selector.add(bestChrom); 86 Population p = new Population(conf); 87 selector.select(1, null, p); 88 assertSame(bestChrom, p.getChromosome(0)); 89 assertEquals(1, p.size()); 90 } 91 92 99 public void testSelect_1() 100 throws Exception { 101 DefaultConfiguration conf = new DefaultConfiguration(); 102 RandomGeneratorForTest randgen = new RandomGeneratorForTest(); 103 randgen.setNextDouble(0.9999d); 104 conf.setRandomGenerator(randgen); 105 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 106 selector.setDoubletteChromosomesAllowed(false); 107 Gene gene = new BooleanGene(conf); 110 gene.setAllele(Boolean.valueOf(true)); 111 Chromosome thirdBestChrom = new Chromosome(conf, gene, 4); 112 thirdBestChrom.setFitnessValue(10); 113 selector.add(thirdBestChrom); 114 gene = new DoubleGene(conf); 117 gene.setAllele(new Double (2.3d)); 118 Chromosome bestChrom = new Chromosome(conf, gene, 3); 119 bestChrom.setFitnessValue(12); 120 selector.add(bestChrom); 121 gene = new IntegerGene(conf); 124 gene.setAllele(new Integer (444)); 125 Chromosome secondBestChrom = new Chromosome(conf, gene, 2); 126 secondBestChrom.setFitnessValue(11); 127 selector.add(secondBestChrom); 128 Population popNew = new Population(conf); 131 selector.select(1, null, popNew); 132 IChromosome[] bestChroms = popNew.toChromosomes(); 133 assertEquals(1, bestChroms.length); 134 assertEquals(thirdBestChrom, bestChroms[0]); 135 assertSame(thirdBestChrom, bestChroms[0]); 136 popNew.getChromosomes().clear(); 139 selector.select(4, null, popNew); 140 bestChroms = popNew.toChromosomes(); 141 assertEquals(3, bestChroms.length); 142 } 143 144 150 public void testSelect_2() 151 throws Exception { 152 DefaultConfiguration conf = new DefaultConfiguration(); 153 RandomGeneratorForTest randgen = new RandomGeneratorForTest(); 154 randgen.setNextDouble(0.9999d); 155 conf.setRandomGenerator(randgen); 156 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 157 selector.setDoubletteChromosomesAllowed(false); 158 Population toAddFrom = new Population(conf); 159 Gene gene = new BooleanGene(conf); 162 gene.setAllele(Boolean.valueOf(true)); 163 Chromosome thirdBestChrom = new Chromosome(conf, gene, 4); 164 thirdBestChrom.setFitnessValue(10); 165 toAddFrom.addChromosome(thirdBestChrom); 166 gene = new DoubleGene(conf); 169 gene.setAllele(new Double (2.3d)); 170 Chromosome bestChrom = new Chromosome(conf, gene, 3); 171 bestChrom.setFitnessValue(12); 172 toAddFrom.addChromosome(bestChrom); 173 gene = new IntegerGene(conf); 176 gene.setAllele(new Integer (444)); 177 Chromosome secondBestChrom = new Chromosome(conf, gene, 2); 178 secondBestChrom.setFitnessValue(11); 179 toAddFrom.addChromosome(secondBestChrom); 180 Population popNew = new Population(conf); 183 selector.select(1, toAddFrom, popNew); 184 IChromosome[] bestChroms = popNew.toChromosomes(); 185 assertEquals(1, bestChroms.length); 186 assertEquals(thirdBestChrom, bestChroms[0]); 187 popNew.getChromosomes().clear(); 190 selector.select(4, toAddFrom, popNew); 191 bestChroms = popNew.toChromosomes(); 192 assertEquals(3, bestChroms.length); 193 } 194 195 203 public void testSelect_3() 204 throws Exception { 205 DefaultConfiguration conf = new DefaultConfiguration(); 206 conf.addNaturalSelector(new WeightedRouletteSelector(), false); 207 RandomGeneratorForTest randgen = new RandomGeneratorForTest(); 208 randgen.setNextDouble(0.0d); 209 conf.setRandomGenerator(randgen); 210 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 211 selector.setDoubletteChromosomesAllowed(false); 212 Population toAddFrom = new Population(conf); 213 Gene gene = new BooleanGene(conf); 216 gene.setAllele(Boolean.valueOf(true)); 217 Chromosome thirdBestChrom = new Chromosome(conf, gene, 4); 218 thirdBestChrom.setFitnessValue(0); 219 toAddFrom.addChromosome(thirdBestChrom); 220 gene = new DoubleGene(conf); 223 gene.setAllele(new Double (2.3d)); 224 Chromosome bestChrom = new Chromosome(conf, gene, 3); 225 bestChrom.setFitnessValue(1.0d); 226 toAddFrom.addChromosome(bestChrom); 227 gene = new IntegerGene(conf); 230 gene.setAllele(new Integer (444)); 231 Chromosome secondBestChrom = new Chromosome(conf, gene, 2); 232 secondBestChrom.setFitnessValue( -1.0d); 233 toAddFrom.addChromosome(secondBestChrom); 234 Population popNew = new Population(conf); 237 selector.select(3, toAddFrom, popNew); 238 } 239 240 247 public void testSelect_4() 248 throws Exception { 249 conf.setFitnessEvaluator(new DeltaFitnessEvaluator()); 250 RandomGeneratorForTest randgen = new RandomGeneratorForTest(); 251 randgen.setNextDouble(0.9999d); 252 conf.setRandomGenerator(randgen); 253 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 254 selector.setDoubletteChromosomesAllowed(false); 255 Population toAddFrom = new Population(conf); 256 Gene gene = new BooleanGene(conf); 259 gene.setAllele(Boolean.valueOf(true)); 260 Chromosome thirdBestChrom = new Chromosome(conf, gene, 4); 261 thirdBestChrom.setFitnessValue(1); 262 toAddFrom.addChromosome(thirdBestChrom); 263 gene = new DoubleGene(conf); 266 gene.setAllele(new Double (2.3d)); 267 Chromosome secondBestChrom = new Chromosome(conf, gene, 3); 268 secondBestChrom.setFitnessValue(2); 269 toAddFrom.addChromosome(secondBestChrom); 270 gene = new IntegerGene(conf); 273 gene.setAllele(new Integer (444)); 274 Chromosome bestChrom = new Chromosome(conf, gene, 2); 275 bestChrom.setFitnessValue(3); 276 toAddFrom.addChromosome(bestChrom); 277 Population popNew = new Population(conf); 280 selector.select(1, toAddFrom, popNew); 281 IChromosome[] bestChroms = popNew.toChromosomes(); 282 assertEquals(1, bestChroms.length); 283 assertEquals(bestChrom, bestChroms[0]); 284 popNew.getChromosomes().clear(); 287 selector.select(4, toAddFrom, popNew); 288 bestChroms = popNew.toChromosomes(); 289 assertEquals(3, bestChroms.length); 290 } 291 292 299 public void testEmpty_0() 300 throws Exception { 301 WeightedRouletteSelector selector = new WeightedRouletteSelector(); 302 Configuration conf = new DefaultConfiguration(); 303 conf.setPopulationSize(7); 304 conf.setFitnessFunction(new TestFitnessFunction()); 305 Gene gene = new BooleanGene(conf); 306 Chromosome chrom = new Chromosome(conf, gene, 5); 307 conf.setSampleChromosome(chrom); 308 selector.add(chrom); 309 selector.empty(); 310 Map chromosomes = (Map) privateAccessor.getField(selector, "m_wheel"); 311 assertEquals(0, chromosomes.size()); 312 } 313 314 320 public void testEmpty_1() 321 throws Exception { 322 Configuration conf = new DefaultConfiguration(); 323 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 324 Gene gene = new BooleanGene(conf); 325 Chromosome chrom = new Chromosome(conf, gene, 5); 326 chrom.setFitnessValue(3); 327 Population pop = new Population(conf, 1); 328 pop.addChromosome(chrom); 329 selector.add(chrom); 330 Population popNew = new Population(conf); 331 selector.select(1, null, popNew); 332 selector.empty(); 333 assertEquals(1, popNew.size()); 334 } 335 336 342 public void testEmpty_2() 343 throws Exception { 344 Configuration conf = new DefaultConfiguration(); 345 WeightedRouletteSelector selector = new WeightedRouletteSelector(conf); 346 Gene gene = new BooleanGene(conf); 347 Chromosome chrom = new Chromosome(conf, gene, 5); 348 chrom.setFitnessValue(7); 349 Population pop = new Population(conf, 1); 350 pop.addChromosome(chrom); 351 selector.add(chrom); 352 Population popNew = new Population(conf); 353 selector.select(1, null, popNew); 354 selector.empty(); 355 assertEquals(1, popNew.size()); 356 } 357 358 362 public void testReturnsUniqueChromosomes_0() { 363 WeightedRouletteSelector selector = new WeightedRouletteSelector(); 364 assertFalse(selector.returnsUniqueChromosomes()); 365 } 366 367 } 386 | Popular Tags |