Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 193   Methods: 8
NCLOC: 80   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GaussianMutationOperator.java 95% 97,1% 87,5% 95,2%
coverage coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.impl;
 11   
 12    import java.util.*;
 13    import org.jgap.*;
 14   
 15    /**
 16    * This genetic operator performs Gaussian mutation across all genes in a given
 17    * Chromosome.
 18    *
 19    * @author Klaus Meffert (modified JOONEGAP source)
 20    * @since 2.0
 21    */
 22    public class GaussianMutationOperator
 23    extends BaseGeneticOperator {
 24    /** String containing the CVS revision. Read out via reflection!*/
 25    private static final String CVS_REVISION = "$Revision: 1.21 $";
 26   
 27    private double m_deviation;
 28   
 29    private RandomGenerator m_rg;
 30   
 31    /**
 32    * Constructs a GaussianMutationOperator with a default deviation of 0.05.
 33    * Attention: The configuration used is the one set with the static method
 34    * Genotype.setConfiguration.
 35    * @throws InvalidConfigurationException
 36    *
 37    * @author Klaus Meffert
 38    * @since 2.0
 39    */
 40  1 public GaussianMutationOperator()
 41    throws InvalidConfigurationException {
 42  1 this(Genotype.getStaticConfiguration());
 43    }
 44   
 45    /**
 46    * Constructs a GaussianMutationOperator with a default deviation of 0.05.
 47    * @param a_config the configuration to use
 48    *
 49    * @throws InvalidConfigurationException
 50    *
 51    * @author Klaus Meffert
 52    * @since 3.0
 53    */
 54  5 public GaussianMutationOperator(Configuration a_config)
 55    throws InvalidConfigurationException {
 56  5 this(a_config, 0.05d);
 57    }
 58   
 59    /**
 60    * Constructs a GaussianMutationOperator with the given deviation.
 61    * @param a_configuration the configuration to use
 62    * @param a_deviation sic
 63    *
 64    * @throws InvalidConfigurationException
 65    *
 66    * @since 3.0 (since 2.0 without a_configuration)
 67    */
 68  12 public GaussianMutationOperator(final Configuration a_configuration,
 69    final double a_deviation)
 70    throws InvalidConfigurationException {
 71  12 super(a_configuration);
 72  12 m_deviation = a_deviation;
 73    }
 74   
 75    /**
 76    * Executes the operation.
 77    *
 78    * @param a_population containing chromosomes to be mutated
 79    * @param a_candidateChromosomes resulting chromosomes
 80    *
 81    * @author Klaus Meffert
 82    * @since 2.0
 83    */
 84  6 public void operate(final Population a_population,
 85    final List a_candidateChromosomes) {
 86  6 int size = Math.min(getConfiguration().getPopulationSize(),
 87    a_population.size());
 88  6 if (m_rg == null) {
 89  4 RandomGenerator rn = getConfiguration().getRandomGenerator();
 90  4 m_rg = rn;
 91    }
 92    // if (rn instanceof RandomGenerator) {
 93    // setRandomGenerator(rn);
 94    // }
 95    // else {
 96    // setRandomGenerator(new GaussianRandomGenerator(1.0d));
 97    // }
 98  6 for (int i = 0; i < size; i++) {
 99  12 Gene[] genes = a_population.getChromosome(i).getGenes();
 100  12 IChromosome copyOfChromosome = null;
 101    // For each Chromosome in the population...
 102    // ----------------------------------------
 103  12 for (int j = 0; j < genes.length; j++) {
 104  12 double nextGaussian = m_rg.nextDouble();
 105  12 double diff = nextGaussian * m_deviation;
 106    // ...take a copy of it...
 107    // -----------------------
 108  12 if (copyOfChromosome == null) {
 109  12 copyOfChromosome = (IChromosome) a_population.getChromosome(i).clone();
 110    // ...add it to the candidate pool...
 111    // ----------------------------------
 112  12 a_candidateChromosomes.add(copyOfChromosome);
 113    // ...then Gaussian mutate all its genes
 114  12 genes = copyOfChromosome.getGenes();
 115    }
 116    // Process all atomic elements in the gene. For a StringGene this
 117    // would be the length of the string, for an IntegerGene, it is
 118    // always one element.
 119    // --------------------------------------------------------------
 120  12 if (genes[j] instanceof CompositeGene) {
 121  1 CompositeGene compositeGene = (CompositeGene) genes[j];
 122  1 for (int k = 0; k < compositeGene.size(); k++) {
 123  1 mutateGene(compositeGene.geneAt(k), diff);
 124    }
 125    }
 126    else {
 127  11 mutateGene(genes[j], diff);
 128    }
 129    }
 130    }
 131    }
 132   
 133    /**
 134    * Helper: mutate all atomic elements of a gene.
 135    *
 136    * @param a_gene the gene to be mutated
 137    * @param a_percentage the percentage the gene is to be mutated with
 138    *
 139    * @author Klaus Meffert
 140    * @since 2.0
 141    */
 142  12 private void mutateGene(final Gene a_gene, final double a_percentage) {
 143  12 for (int k = 0; k < a_gene.size(); k++) {
 144    // Mutate atomic element by given percentage.
 145    // ------------------------------------------
 146  12 a_gene.applyMutation(k, a_percentage);
 147    }
 148    }
 149   
 150  0 private void setRandomGenerator(final RandomGenerator a_rg) {
 151  0 m_rg = a_rg;
 152    }
 153   
 154    /**
 155    * Compares the given GeneticOperator to this GeneticOperator.
 156    *
 157    * @param a_other the instance against which to compare this instance
 158    *
 159    * @return a negative number if this instance is "less than" the given
 160    * instance, zero if they are equal to each other, and a positive number if
 161    * this is "greater than" the given instance
 162    *
 163    * @author Klaus Meffert
 164    * @since 2.6
 165    */
 166  5 public int compareTo(final Object a_other) {
 167  5 if (a_other == null) {
 168  1 return 1;
 169    }
 170  4 GaussianMutationOperator op = (GaussianMutationOperator) a_other;
 171  4 if (m_deviation != op.m_deviation) {
 172  2 if (m_deviation > op.m_deviation) {
 173  1 return 1;
 174    }
 175    else {
 176  1 return -1;
 177    }
 178    }
 179    // Everything is equal. Return zero.
 180    // ---------------------------------
 181  2 return 0;
 182    }
 183   
 184    /**
 185    * @return the deviation set
 186    *
 187    * @author Klaus Meffert
 188    * @since 3.1
 189    */
 190  3 public double getDeviation() {
 191  3 return m_deviation;
 192    }
 193    }