/* * This file is part of JGAP. * * JGAP offers a dual license model containing the LGPL as well as the MPL. * * For licensing information please see the file license.txt included with JGAP * or have a look at the top of class org.jgap.Chromosome which representatively * includes the JGAP license policy applicable for any file delivered with JGAP. */ package org.jgap.impl; import java.util.*; import org.jgap.*; import junit.framework.*; /** * Tests the GaussianMutationOperator class. * * @author Klaus Meffert * @since 2.0 */ public class GaussianMutationOperatorTest extends JGAPTestCase { /** String containing the CVS revision. Read out via reflection!*/ private static final String CVS_REVISION = "$Revision: 1.15 $"; public static Test suite() { TestSuite suite = new TestSuite(GaussianMutationOperatorTest.class); return suite; } public void setUp() { super.setUp(); Configuration.reset(); } /** * @throws Exception * * @author Klaus Meffert * @since 2.0 */ public void testOperate_0() throws Exception { DefaultConfiguration conf = new DefaultConfiguration(); GeneticOperator op = new GaussianMutationOperator(conf, 0.15d); conf.addGeneticOperator(op); RandomGeneratorForTesting rand = new RandomGeneratorForTesting(); rand.setNextDouble(0.45d); conf.setRandomGenerator(rand); conf.setFitnessFunction(new TestFitnessFunction()); Gene sampleGene = new IntegerGene(conf, 1, 10); Chromosome chrom = new Chromosome(conf, sampleGene, 3); conf.setSampleChromosome(chrom); conf.setPopulationSize(6); Gene cgene1 = new IntegerGene(conf, 1, 10); cgene1.setAllele(new Integer(6)); Gene[] genes1 = new Gene[] { cgene1}; Chromosome chrom1 = new Chromosome(conf, genes1); Gene cgene2 = new IntegerGene(conf, 1, 10); cgene2.setAllele(new Integer(9)); Gene[] genes2 = new Gene[] { cgene2}; Chromosome chrom2 = new Chromosome(conf, genes2); Chromosome[] population = new Chromosome[] { chrom1, chrom2}; List chroms = new Vector(); Gene gene1 = new IntegerGene(conf, 1, 10); gene1.setAllele(new Integer(5)); chroms.add(gene1); Gene gene2 = new IntegerGene(conf, 1, 10); gene2.setAllele(new Integer(7)); chroms.add(gene2); Gene gene3 = new IntegerGene(conf, 1, 10); gene3.setAllele(new Integer(4)); chroms.add(gene3); op.operate(new Population(conf, population), chroms); Chromosome target = (Chromosome) chroms.get(4); assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 9), ( (Integer) target.getGene(0).getAllele()).intValue()); target = (Chromosome) chroms.get(3); assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 6), ( (Integer) target.getGene(0).getAllele()).intValue()); } /** * Tests if mutation produces same results for two operate-runs * @throws Exception * * @author Klaus Meffert * @since 2.0 */ public void testOperate_1() throws Exception { DefaultConfiguration conf = new DefaultConfiguration(); GeneticOperator op = new GaussianMutationOperator(conf); conf.addGeneticOperator(op); RandomGeneratorForTesting rand = new RandomGeneratorForTesting(); rand.setNextIntSequence(new int[] { 0, 1, 0, 1, 2}); conf.setRandomGenerator(rand); conf.setFitnessFunction(new TestFitnessFunction()); Gene sampleGene = new IntegerGene(conf, 1, 10); Chromosome chrom = new Chromosome(conf, sampleGene, 3); conf.setSampleChromosome(chrom); conf.setPopulationSize(6); Gene cgene1 = new IntegerGene(conf, 1, 10); cgene1.setAllele(new Integer(6)); Gene[] genes1 = new Gene[] { cgene1}; Chromosome chrom1 = new Chromosome(conf, genes1); Gene cgene2 = new IntegerGene(conf, 1, 10); cgene2.setAllele(new Integer(8)); Gene[] genes2 = new Gene[] { cgene2}; Chromosome chrom2 = new Chromosome(conf, genes2); Chromosome[] population = new Chromosome[] { chrom1, chrom2}; List chroms = new Vector(); Gene gene1 = new IntegerGene(conf, 1, 10); gene1.setAllele(new Integer(5)); chroms.add(gene1); Gene gene2 = new IntegerGene(conf, 1, 10); gene2.setAllele(new Integer(7)); chroms.add(gene2); Gene gene3 = new IntegerGene(conf, 1, 10); gene3.setAllele(new Integer(4)); chroms.add(gene3); Chromosome[] population2 = (Chromosome[]) population.clone(); op.operate(new Population(conf, population), chroms); op.operate(new Population(conf, population2), chroms); assertTrue(isChromosomesEqual(population, population2)); } /** * Tests if mutation works for CompositeGene. * @throws Exception * * @author Klaus Meffert * @since 2.1 */ public void testOperate_2() throws Exception { DefaultConfiguration conf = new DefaultConfiguration(); GeneticOperator op = new GaussianMutationOperator(conf, 0.15d); conf.addGeneticOperator(op); RandomGeneratorForTesting rand = new RandomGeneratorForTesting(); rand.setNextDouble(0.45d); conf.setRandomGenerator(rand); conf.setFitnessFunction(new TestFitnessFunction()); Gene sampleGene = new IntegerGene(conf, 1, 10); Chromosome chrom = new Chromosome(conf, sampleGene, 3); conf.setSampleChromosome(chrom); conf.setPopulationSize(6); Gene cgene1 = new IntegerGene(conf, 1, 10); cgene1.setAllele(new Integer(6)); CompositeGene compGene = new CompositeGene(conf); compGene.addGene(cgene1); Gene[] genes1 = new Gene[] { compGene}; Chromosome chrom1 = new Chromosome(conf, genes1); Gene cgene2 = new IntegerGene(conf, 1, 10); cgene2.setAllele(new Integer(9)); Gene[] genes2 = new Gene[] { cgene2}; Chromosome chrom2 = new Chromosome(conf, genes2); Chromosome[] population = new Chromosome[] { chrom1, chrom2}; List chroms = new Vector(); Gene gene1 = new IntegerGene(conf, 1, 10); gene1.setAllele(new Integer(5)); chroms.add(gene1); Gene gene2 = new IntegerGene(conf, 1, 10); gene2.setAllele(new Integer(7)); chroms.add(gene2); Gene gene3 = new IntegerGene(conf, 1, 10); gene3.setAllele(new Integer(4)); chroms.add(gene3); assertEquals(3, chroms.size()); op.operate(new Population(conf, population), chroms); Chromosome target = (Chromosome) chroms.get(4); assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 9), ( (Integer) target.getGene(0).getAllele()).intValue()); target = (Chromosome) chroms.get(3); compGene = (CompositeGene) target.getGene(0); Gene gene = compGene.geneAt(0); assertEquals(Math.round( (10 - 1) * (0.45d * 0.15d) + 6), ( (Integer) gene.getAllele()).intValue()); } /** * Tests if population size grows expectedly after two consecutive calls. * @throws Exception * * @author Klaus Meffert * @since 2.1 */ public void testOperate_4() throws Exception { DefaultConfiguration conf = new DefaultConfiguration(); GeneticOperator op = new GaussianMutationOperator(conf, 0.15d); conf.addGeneticOperator(op); RandomGeneratorForTesting rand = new RandomGeneratorForTesting(); rand.setNextDouble(0.45d); conf.setRandomGenerator(rand); conf.setFitnessFunction(new TestFitnessFunction()); Gene sampleGene = new IntegerGene(conf, 1, 10); Chromosome chrom = new Chromosome(conf, sampleGene, 3); conf.setSampleChromosome(chrom); conf.setPopulationSize(6); Gene cgene1 = new IntegerGene(conf, 1, 10); cgene1.setAllele(new Integer(6)); Gene[] genes1 = new Gene[] { cgene1}; Chromosome chrom1 = new Chromosome(conf, genes1); Gene cgene2 = new IntegerGene(conf, 1, 10); cgene2.setAllele(new Integer(9)); Gene[] genes2 = new Gene[] { cgene2}; Chromosome chrom2 = new Chromosome(conf, genes2); Chromosome[] population = new Chromosome[] { chrom1, chrom2}; List chroms = new Vector(); Gene gene1 = new IntegerGene(conf, 1, 10); gene1.setAllele(new Integer(5)); chroms.add(gene1); Gene gene2 = new IntegerGene(conf, 1, 10); gene2.setAllele(new Integer(7)); chroms.add(gene2); Gene gene3 = new IntegerGene(conf, 1, 10); gene3.setAllele(new Integer(4)); chroms.add(gene3); assertEquals(3, chroms.size()); Population pop = new Population(conf, population); op.operate(pop, chroms); assertEquals(2, pop.size()); assertEquals(3 + 2, chroms.size()); op.operate(pop, chroms); assertEquals(2, pop.size()); assertEquals(3 + 2 + 2, chroms.size()); } /** * @throws Exception * * @author Klaus Meffert * @since 3.1 */ public void testConstruct_0() throws Exception { GaussianMutationOperator mutOp = new GaussianMutationOperator(conf, 234); assertEquals(234, mutOp.getDeviation(), DELTA); } /** * @throws Exception * * @author Klaus Meffert * @since 3.1 */ public void testConstruct_1() throws Exception { GaussianMutationOperator mutOp = new GaussianMutationOperator(conf); assertEquals(0.05d, mutOp.getDeviation(), DELTA); } /** * @throws Exception * * @author Klaus Meffert * @since 3.1 */ public void testConstruct_2() throws Exception { Genotype.setStaticConfiguration(conf); GaussianMutationOperator mutOp = new GaussianMutationOperator(); assertSame(conf, mutOp.getConfiguration()); } /** * @throws Exception * * @author Klaus Meffert * @since 3.1 */ public void testConstruct_3() throws Exception { GaussianMutationOperator mutOp = new GaussianMutationOperator(conf, 1.0d); assertEquals(1.0d, mutOp.getDeviation(), DELTA); } /** * @throws Exception * * @author Klaus Meffert * @since 3.1 */ public void testCompareTo_0() throws Exception { GaussianMutationOperator op = new GaussianMutationOperator(conf); assertEquals(1, op.compareTo(null)); GaussianMutationOperator op2 = new GaussianMutationOperator(conf); assertEquals(0, op.compareTo(op2)); op = new GaussianMutationOperator(conf, 0.03d); assertEquals( -1, op.compareTo(op2)); assertEquals(1, op2.compareTo(op)); op = new GaussianMutationOperator(conf, 0.05d); assertEquals(0, op.compareTo(op2)); } }