1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.JGAPTestCase.*; 15 import junit.framework.*; 16 17 23 public class SwappingMutationOperatorTest 24 extends JGAPTestCase { 25 26 private static final String CVS_REVISION = "$Revision: 1.17 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(SwappingMutationOperatorTest.class); 30 return suite; 31 } 32 33 public void setUp() { 34 super.setUp(); 35 Configuration.reset(); 36 } 37 38 44 public void testConstruct_0() 45 throws Exception { 46 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, 234); 47 assertEquals(234, mutOp.getMutationRate()); 48 assertNull(mutOp.getMutationRateCalc()); 49 } 50 51 57 public void testConstruct_1() 58 throws Exception { 59 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf); 60 assertEquals(0, mutOp.getMutationRate()); 61 assertNotNull(mutOp.getMutationRateCalc()); 62 } 63 64 70 public void testConstruct_2() 71 throws Exception { 72 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, null); 73 assertEquals(0, mutOp.getMutationRate()); 74 assertNull(mutOp.getMutationRateCalc()); 75 } 76 77 83 public void testConstruct_3() 84 throws Exception { 85 IUniversalRateCalculator calc = new DefaultMutationRateCalculator(conf); 86 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, calc); 87 assertEquals(0, mutOp.getMutationRate()); 88 assertEquals(calc, mutOp.getMutationRateCalc()); 89 } 90 91 97 public void testConstruct_4() 98 throws Exception { 99 Genotype.setStaticConfiguration(conf); 100 SwappingMutationOperator op = new SwappingMutationOperator(); 101 assertSame(conf, op.getConfiguration()); 102 } 103 104 110 public void testOperate_0() 111 throws Exception { 112 Configuration conf = new DefaultConfiguration(); 113 conf.setFitnessFunction(new TestFitnessFunction()); 114 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, 115 new DefaultMutationRateCalculator(conf)); 116 List candChroms = new Vector(); 117 Chromosome[] population = new Chromosome[] {}; 118 mutOp.operate(new Population(conf, population), candChroms); 119 assertEquals(candChroms.size(), population.length); 120 candChroms.clear(); 121 RandomGeneratorForTest gen = new RandomGeneratorForTest(); 122 gen.setNextInt(9); 123 conf.setRandomGenerator(gen); 124 Chromosome c1 = new Chromosome(conf, new BooleanGene(conf), 9); 125 conf.setSampleChromosome(c1); 126 conf.addNaturalSelector(new BestChromosomesSelector(conf), true); 127 conf.setPopulationSize(5); 128 for (int i = 0; i < c1.getGenes().length; i++) { 129 c1.getGene(i).setAllele(Boolean.TRUE); 130 } 131 Chromosome c2 = new Chromosome(conf, new IntegerGene(conf), 4); 132 for (int i = 0; i < c2.getGenes().length; i++) { 133 c2.getGene(i).setAllele(new Integer (27)); 134 } 135 population = new Chromosome[] { 136 c1, c2}; 137 mutOp.operate(new Population(conf, population), candChroms); 138 assertEquals(candChroms.size(), population.length); 139 assertEquals(c1, candChroms.get(0)); 140 assertFalse(candChroms.get(0) == c1); 141 assertEquals(c2, candChroms.get(1)); 142 assertFalse(candChroms.get(1) == c2); 143 } 144 145 151 public void testOperate_1() 152 throws Exception { 153 List candChroms = new Vector(); 154 Configuration conf = new Configuration(); 155 conf.setRandomGenerator(new StockRandomGenerator()); 156 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, null); 157 Chromosome[] population = new Chromosome[] { 158 new Chromosome(conf, new BooleanGene(conf), 9), 159 (new Chromosome(conf, new IntegerGene(conf), 4))}; 160 mutOp.operate(new Population(conf, population), candChroms); 161 162 } 163 164 170 public void testOperate_2() 171 throws Exception { 172 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf); 173 List candChroms = new Vector(); 174 Chromosome[] population = new Chromosome[] { 175 new Chromosome(conf, new BooleanGene(conf), 9), 176 (new Chromosome(conf, new IntegerGene(conf), 4))}; 177 try { 178 mutOp.operate(new Population(null, population), candChroms); 179 fail(); 180 } 181 catch (InvalidConfigurationException nex) { 182 ; } 184 } 185 186 193 public void testOperate_3() 194 throws Exception { 195 DefaultConfiguration conf = new DefaultConfiguration(); 196 SwappingMutationOperator op = new SwappingMutationOperator(conf, 197 new DefaultMutationRateCalculator(conf)); 198 op.setStartOffset(0); 199 conf.addGeneticOperator(op); 200 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 201 rand.setNextDouble(0.45d); 202 rand.setNextInt(0); 203 conf.setRandomGenerator(rand); 204 conf.setFitnessFunction(new TestFitnessFunction()); 205 Gene sampleGene = new IntegerGene(conf, 1, 10); 206 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 207 conf.setSampleChromosome(chrom); 208 conf.setPopulationSize(6); 209 Gene cgene1 = new IntegerGene(conf, 1, 10); 210 cgene1.setAllele(new Integer (6)); 211 Gene[] genes1 = new Gene[] { 212 cgene1}; 213 Chromosome chrom1 = new Chromosome(conf, genes1); 214 Gene cgene2 = new IntegerGene(conf, 1, 10); 215 cgene2.setAllele(new Integer (9)); 216 Gene[] genes2 = new Gene[] { 217 cgene2}; 218 Chromosome chrom2 = new Chromosome(conf, genes2); 219 Chromosome[] population = new Chromosome[] { 220 chrom1, chrom2}; 221 List chroms = new Vector(); 222 Gene gene1 = new IntegerGene(conf, 1, 10); 223 gene1.setAllele(new Integer (5)); 224 chroms.add(gene1); 225 Gene gene2 = new IntegerGene(conf, 1, 10); 226 gene2.setAllele(new Integer (7)); 227 chroms.add(gene2); 228 Gene gene3 = new IntegerGene(conf, 1, 10); 229 gene3.setAllele(new Integer (4)); 230 chroms.add(gene3); 231 assertEquals(3, chroms.size()); 232 Population pop = new Population(conf, population); 233 op.operate(pop, chroms); 234 assertEquals(2, pop.size()); 235 assertEquals(3 + 2, chroms.size()); 236 op.operate(pop, chroms); 237 assertEquals(2, pop.size()); 238 assertEquals(3 + 2 + 2, chroms.size()); 239 } 240 241 250 public void testOperate_4() 251 throws Exception { 252 Configuration conf = new DefaultConfiguration(); 253 conf.setFitnessFunction(new TestFitnessFunction()); 254 int n_iterations = 20; 255 RandomGenerator generator = new StockRandomGenerator(); 256 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf); 257 mutOp.setStartOffset(0); 258 for (int n_genes = 0; n_genes < 20; n_genes++) { 259 Gene[] genes = new IntegerGene[n_genes]; 260 for (int i = 0; i < genes.length; i++) { 261 genes[i] = new IntegerGene(conf, -1000, 1000); 262 genes[i].setToRandomValue(generator); 263 } 264 final long checksum = checksum(genes); 265 Gene[] prev = new Gene[genes.length]; 266 for (int i = 0; i < n_iterations; i++) { 267 for (int gene = 0; gene < genes.length; gene++) { 268 System.arraycopy(genes, 0, prev, 0, genes.length); 269 genes = mutOp.operate(generator, gene, genes); 270 assertEquals(checksum, checksum(genes)); 272 } 273 } 274 } 275 } 276 277 284 public void testOperate_5() 285 throws Exception { 286 Configuration conf = new DefaultConfiguration(); 287 SwappingMutationOperator mutOp = new SwappingMutationOperator(conf, 0); 288 mutOp.setMutationRateCalc(null); 289 List candChroms = new Vector(); 290 BooleanGene gene1 = new BooleanGene(conf); 291 Chromosome chrom1 = new Chromosome(conf, gene1, 1); 292 chrom1.getGene(0).setAllele(Boolean.valueOf(false)); 293 IntegerGene gene2 = new IntegerGene(conf, 0, 10); 294 Chromosome chrom2 = new Chromosome(conf, gene2, 1); 295 chrom2.getGene(0).setAllele(new Integer (3)); 296 candChroms.add(chrom1); 297 candChroms.add(chrom2); 298 mutOp.operate(null, candChroms); 299 assertEquals(2, candChroms.size()); 300 assertEquals(chrom1, candChroms.get(0)); 301 assertEquals(chrom2, candChroms.get(1)); 302 } 303 304 private long checksum(Gene[] a_genes) { 305 long s = 0; 306 for (int i = 0; i < a_genes.length; i++) { 307 s += ( (IntegerGene) a_genes[i]).intValue(); 308 } 309 return s; 310 } 311 312 318 public void testStartoffset_0() 319 throws Exception { 320 SwappingMutationOperator op = new SwappingMutationOperator(conf); 321 assertEquals(1, op.getStartOffset()); 322 op.setStartOffset(2); 323 assertEquals(2, op.getStartOffset()); 324 op.setStartOffset(1); 325 assertEquals(1, op.getStartOffset()); 326 op.setStartOffset(0); 327 assertEquals(0, op.getStartOffset()); 328 } 329 330 337 public void testIsSerializable_0() 338 throws Exception { 339 SwappingMutationOperator op = new SwappingMutationOperator(conf); 340 assertTrue(isSerializable(op)); 341 } 342 343 350 public void testDoSerialize_0() 351 throws Exception { 352 SwappingMutationOperator op = new SwappingMutationOperator(conf, 354 new DefaultCrossoverRateCalculator(conf)); 355 SwappingMutationOperator o = (SwappingMutationOperator) doSerialize(op); 356 assertEquals(o, op); 357 } 358 359 366 public void testEquals_0() 367 throws Exception { 368 GeneticOperator op = new SwappingMutationOperator(conf); 369 assertFalse(op.equals(new Chromosome(conf))); 370 } 371 } 372 | Popular Tags |