1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 import junit.framework.*; 15 16 22 public class GaussianMutationOperatorTest 23 extends JGAPTestCase { 24 25 private static final String CVS_REVISION = "$Revision: 1.13 $"; 26 27 public static Test suite() { 28 TestSuite suite = new TestSuite(GaussianMutationOperatorTest.class); 29 return suite; 30 } 31 32 public void setUp() { 33 super.setUp(); 34 Configuration.reset(); 35 } 36 37 43 public void testOperate_0() 44 throws Exception { 45 DefaultConfiguration conf = new DefaultConfiguration(); 46 GeneticOperator op = new GaussianMutationOperator(conf, 0.15d); 47 conf.addGeneticOperator(op); 48 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 49 rand.setNextDouble(0.45d); 50 conf.setRandomGenerator(rand); 51 conf.setFitnessFunction(new TestFitnessFunction()); 52 Gene sampleGene = new IntegerGene(conf, 1, 10); 53 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 54 conf.setSampleChromosome(chrom); 55 conf.setPopulationSize(6); 56 Gene cgene1 = new IntegerGene(conf, 1, 10); 57 cgene1.setAllele(new Integer (6)); 58 Gene[] genes1 = new Gene[] { 59 cgene1}; 60 Chromosome chrom1 = new Chromosome(conf, genes1); 61 Gene cgene2 = new IntegerGene(conf, 1, 10); 62 cgene2.setAllele(new Integer (9)); 63 Gene[] genes2 = new Gene[] { 64 cgene2}; 65 Chromosome chrom2 = new Chromosome(conf, genes2); 66 Chromosome[] population = new Chromosome[] { 67 chrom1, chrom2}; 68 List chroms = new Vector(); 69 Gene gene1 = new IntegerGene(conf, 1, 10); 70 gene1.setAllele(new Integer (5)); 71 chroms.add(gene1); 72 Gene gene2 = new IntegerGene(conf, 1, 10); 73 gene2.setAllele(new Integer (7)); 74 chroms.add(gene2); 75 Gene gene3 = new IntegerGene(conf, 1, 10); 76 gene3.setAllele(new Integer (4)); 77 chroms.add(gene3); 78 op.operate(new Population(conf, population), chroms); 79 Chromosome target = (Chromosome) chroms.get(4); 80 assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 9), 81 ( (Integer ) target.getGene(0).getAllele()).intValue()); 82 target = (Chromosome) chroms.get(3); 83 assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 6), 84 ( (Integer ) target.getGene(0).getAllele()).intValue()); 85 } 86 87 94 public void testOperate_1() 95 throws Exception { 96 DefaultConfiguration conf = new DefaultConfiguration(); 97 GeneticOperator op = new GaussianMutationOperator(conf); 98 conf.addGeneticOperator(op); 99 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 100 rand.setNextIntSequence(new int[] { 101 0, 1, 0, 1, 2}); 102 conf.setRandomGenerator(rand); 103 conf.setFitnessFunction(new TestFitnessFunction()); 104 Gene sampleGene = new IntegerGene(conf, 1, 10); 105 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 106 conf.setSampleChromosome(chrom); 107 conf.setPopulationSize(6); 108 Gene cgene1 = new IntegerGene(conf, 1, 10); 109 cgene1.setAllele(new Integer (6)); 110 Gene[] genes1 = new Gene[] { 111 cgene1}; 112 Chromosome chrom1 = new Chromosome(conf, genes1); 113 Gene cgene2 = new IntegerGene(conf, 1, 10); 114 cgene2.setAllele(new Integer (8)); 115 Gene[] genes2 = new Gene[] { 116 cgene2}; 117 Chromosome chrom2 = new Chromosome(conf, genes2); 118 Chromosome[] population = new Chromosome[] { 119 chrom1, chrom2}; 120 List chroms = new Vector(); 121 Gene gene1 = new IntegerGene(conf, 1, 10); 122 gene1.setAllele(new Integer (5)); 123 chroms.add(gene1); 124 Gene gene2 = new IntegerGene(conf, 1, 10); 125 gene2.setAllele(new Integer (7)); 126 chroms.add(gene2); 127 Gene gene3 = new IntegerGene(conf, 1, 10); 128 gene3.setAllele(new Integer (4)); 129 chroms.add(gene3); 130 Chromosome[] population2 = (Chromosome[]) population.clone(); 131 op.operate(new Population(conf, population), chroms); 132 op.operate(new Population(conf, population2), chroms); 133 assertTrue(isChromosomesEqual(population, population2)); 134 } 135 136 143 public void testOperate_2() 144 throws Exception { 145 DefaultConfiguration conf = new DefaultConfiguration(); 146 GeneticOperator op = new GaussianMutationOperator(conf, 0.15d); 147 conf.addGeneticOperator(op); 148 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 149 rand.setNextDouble(0.45d); 150 conf.setRandomGenerator(rand); 151 conf.setFitnessFunction(new TestFitnessFunction()); 152 Gene sampleGene = new IntegerGene(conf, 1, 10); 153 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 154 conf.setSampleChromosome(chrom); 155 conf.setPopulationSize(6); 156 Gene cgene1 = new IntegerGene(conf, 1, 10); 157 cgene1.setAllele(new Integer (6)); 158 CompositeGene compGene = new CompositeGene(conf); 159 compGene.addGene(cgene1); 160 Gene[] genes1 = new Gene[] { 161 compGene}; 162 Chromosome chrom1 = new Chromosome(conf, genes1); 163 Gene cgene2 = new IntegerGene(conf, 1, 10); 164 cgene2.setAllele(new Integer (9)); 165 Gene[] genes2 = new Gene[] { 166 cgene2}; 167 Chromosome chrom2 = new Chromosome(conf, genes2); 168 Chromosome[] population = new Chromosome[] { 169 chrom1, chrom2}; 170 List chroms = new Vector(); 171 Gene gene1 = new IntegerGene(conf, 1, 10); 172 gene1.setAllele(new Integer (5)); 173 chroms.add(gene1); 174 Gene gene2 = new IntegerGene(conf, 1, 10); 175 gene2.setAllele(new Integer (7)); 176 chroms.add(gene2); 177 Gene gene3 = new IntegerGene(conf, 1, 10); 178 gene3.setAllele(new Integer (4)); 179 chroms.add(gene3); 180 assertEquals(3, chroms.size()); 181 op.operate(new Population(conf, population), chroms); 182 Chromosome target = (Chromosome) chroms.get(4); 183 assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 9), 184 ( (Integer ) target.getGene(0).getAllele()).intValue()); 185 target = (Chromosome) chroms.get(3); 186 compGene = (CompositeGene) target.getGene(0); 187 Gene gene = compGene.geneAt(0); 188 assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 6), 189 ( (Integer ) gene.getAllele()).intValue()); 190 } 191 192 199 public void testOperate_4() 200 throws Exception { 201 DefaultConfiguration conf = new DefaultConfiguration(); 202 GeneticOperator op = new GaussianMutationOperator(conf, 0.15d); 203 conf.addGeneticOperator(op); 204 RandomGeneratorForTest rand = new RandomGeneratorForTest(); 205 rand.setNextDouble(0.45d); 206 conf.setRandomGenerator(rand); 207 conf.setFitnessFunction(new TestFitnessFunction()); 208 Gene sampleGene = new IntegerGene(conf, 1, 10); 209 Chromosome chrom = new Chromosome(conf, sampleGene, 3); 210 conf.setSampleChromosome(chrom); 211 conf.setPopulationSize(6); 212 Gene cgene1 = new IntegerGene(conf, 1, 10); 213 cgene1.setAllele(new Integer (6)); 214 Gene[] genes1 = new Gene[] { 215 cgene1}; 216 Chromosome chrom1 = new Chromosome(conf, genes1); 217 Gene cgene2 = new IntegerGene(conf, 1, 10); 218 cgene2.setAllele(new Integer (9)); 219 Gene[] genes2 = new Gene[] { 220 cgene2}; 221 Chromosome chrom2 = new Chromosome(conf, genes2); 222 Chromosome[] population = new Chromosome[] { 223 chrom1, chrom2}; 224 List chroms = new Vector(); 225 Gene gene1 = new IntegerGene(conf, 1, 10); 226 gene1.setAllele(new Integer (5)); 227 chroms.add(gene1); 228 Gene gene2 = new IntegerGene(conf, 1, 10); 229 gene2.setAllele(new Integer (7)); 230 chroms.add(gene2); 231 Gene gene3 = new IntegerGene(conf, 1, 10); 232 gene3.setAllele(new Integer (4)); 233 chroms.add(gene3); 234 assertEquals(3, chroms.size()); 235 Population pop = new Population(conf, population); 236 op.operate(pop, chroms); 237 assertEquals(2, pop.size()); 238 assertEquals(3 + 2, chroms.size()); 239 op.operate(pop, chroms); 240 assertEquals(2, pop.size()); 241 assertEquals(3 + 2 + 2, chroms.size()); 242 } 243 244 250 public void testConstruct_0() 251 throws Exception { 252 GaussianMutationOperator mutOp = new GaussianMutationOperator(conf, 234); 253 assertEquals(234, mutOp.getDeviation(), DELTA); 254 } 255 256 262 public void testConstruct_1() 263 throws Exception { 264 GaussianMutationOperator mutOp = new GaussianMutationOperator(conf); 265 assertEquals(0.05d, mutOp.getDeviation(), DELTA); 266 } 267 268 274 public void testConstruct_2() 275 throws Exception { 276 Genotype.setStaticConfiguration(conf); 277 GaussianMutationOperator mutOp = new GaussianMutationOperator(); 278 assertSame(conf, mutOp.getConfiguration()); 279 } 280 281 287 public void testConstruct_3() 288 throws Exception { 289 GaussianMutationOperator mutOp = new GaussianMutationOperator(conf, 1.0d); 290 assertEquals(1.0d, mutOp.getDeviation(), DELTA); 291 } 292 293 299 public void testCompareTo_0() 300 throws Exception { 301 GaussianMutationOperator op = new GaussianMutationOperator(conf); 302 assertEquals(1, op.compareTo(null)); 303 GaussianMutationOperator op2 = new GaussianMutationOperator(conf); 304 assertEquals(0, op.compareTo(op2)); 305 op = new GaussianMutationOperator(conf, 0.03d); 306 assertEquals( -1, op.compareTo(op2)); 307 assertEquals(1, op2.compareTo(op)); 308 op = new GaussianMutationOperator(conf, 0.05d); 309 assertEquals(0, op.compareTo(op2)); 310 } 311 } 312 | Popular Tags |