Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 120   Methods: 4
NCLOC: 50   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InversionOperator.java 100% 100% 100% 100%
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    * The inversion operator randomly selects one chromosomes from the
 17    * population and inverses it by randomly picking a splitting locus on which
 18    * to swap the first part with the last part of the chromosome.
 19    *
 20    * @author Klaus Meffert
 21    * @since 2.3
 22    */
 23    public class InversionOperator
 24    extends BaseGeneticOperator {
 25    /** String containing the CVS revision. Read out via reflection!*/
 26    private final static String CVS_REVISION = "$Revision: 1.9 $";
 27   
 28    /**
 29    * Default constructor.<p>
 30    * Attention: The configuration used is the one set with the static method
 31    * Genotype.setConfiguration.
 32    *
 33    * @author Klaus Meffert
 34    * @since 2.3
 35    */
 36  1 public InversionOperator()
 37    throws InvalidConfigurationException {
 38  1 this(Genotype.getStaticConfiguration());
 39    }
 40   
 41    /**
 42    * Constructs a new instance of this operator.
 43    * @param a_config the configuration to use
 44    *
 45    * @author Klaus Meffert
 46    * @since 3.0
 47    */
 48  9 public InversionOperator(Configuration a_config)
 49    throws InvalidConfigurationException {
 50  9 super(a_config);
 51    }
 52   
 53    /**
 54    * @author Klaus Meffert
 55    * @since 2.3
 56    */
 57  7 public void operate(final Population a_population,
 58    final List a_candidateChromosomes) {
 59    // Work out the number of crossovers that should be performed.
 60    // -----------------------------------------------------------
 61  7 int size = Math.min(getConfiguration().getPopulationSize(),
 62    a_population.size());
 63  7 RandomGenerator generator = getConfiguration().getRandomGenerator();
 64    // For the inversion, grab a random chromosome, pick a random
 65    // locus (gene location), and then swap that gene and all genes
 66    // to the "right" (those with greater loci) of that gene with the left
 67    // genes.
 68    // -------------------------------------------------------------------
 69  7 int index1;
 70  7 index1 = generator.nextInt(size);
 71  7 IChromosome chrom1 = a_population.getChromosome(index1);
 72  7 IChromosome firstMate = (IChromosome) chrom1.clone();
 73  7 Gene[] firstGenes = firstMate.getGenes();
 74  7 int locus = generator.nextInt(firstGenes.length);
 75    // Swap the genes.
 76    // ---------------
 77  7 Gene[] invertedGenes = new Gene[firstGenes.length];
 78  7 int index = 0;
 79  7 int len = firstGenes.length;
 80  7 for (int j = locus; j < len; j++) {
 81  7 invertedGenes[index++] = firstGenes[j];
 82    }
 83  7 for (int j = 0; j < locus; j++) {
 84  1 invertedGenes[index++] = firstGenes[j];
 85    }
 86  7 try {
 87  7 firstMate.setGenes(invertedGenes);
 88    }
 89    catch (InvalidConfigurationException cex) {
 90    // Rethrow to have an unchecked exception.
 91    // ---------------------------------------
 92  1 throw new Error(cex);
 93    }
 94    // Add the modified chromosome to the candidate pool so that it'll be
 95    // considered for natural selection during the next phase of evolution.
 96    // --------------------------------------------------------------------
 97  6 a_candidateChromosomes.add(firstMate);
 98    }
 99   
 100    /**
 101    * Compares the given GeneticOperator to this GeneticOperator.
 102    *
 103    * @param a_other the instance against which to compare this instance
 104    * @return a negative number if this instance is "less than" the given
 105    * instance, zero if they are equal to each other, and a positive number if
 106    * this is "greater than" the given instance
 107    *
 108    * @author Klaus Meffert
 109    * @since 2.6
 110    */
 111  4 public int compareTo(final Object a_other) {
 112  4 if (a_other == null) {
 113  1 return 1;
 114    }
 115  3 InversionOperator op = (InversionOperator) a_other;
 116    // Everything is equal. Return zero.
 117    // ---------------------------------
 118  2 return 0;
 119    }
 120    }