Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 112   Methods: 3
NCLOC: 55   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
FittestPopulationMerger.java 100% 94,7% 100% 96,4%
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    import org.jgap.distr.*;
 15   
 16    /**
 17    * A implementation of the IPopulationMerger interface that merges two
 18    * populations as specified based on the fitness function, that is, the n
 19    * fittest chromosomes are returned in the new population, where n is supplied
 20    * by parameter.
 21    *
 22    * @author Henrique Goulart
 23    * @since 2.0
 24    */
 25    public class FittestPopulationMerger
 26    implements IPopulationMerger {
 27    /** String containing the CVS revision. Read out via reflection!*/
 28    private final static String CVS_REVISION = "$Revision: 1.15 $";
 29   
 30  1 public Population mergePopulations(final Population a_population1,
 31    final Population a_population2,
 32    final int a_new_population_size) {
 33    /**@todo check if configurations of both pops are equal resp.
 34    * their fitness evaluators!*/
 35  1 try {
 36    //All the chromosomes are placed in the first population for sorting.
 37  1 a_population1.addChromosomes(a_population2);
 38    //A sorting is made according to the chromosomes fitness values
 39    //See the private class FitnessChromosomeComparator below to understand.
 40  1 List allChromosomes = a_population1.getChromosomes();
 41  1 Collections.sort(allChromosomes,
 42    new FitnessChromosomeComparator(a_population1.
 43    getConfiguration()));
 44    //Then a new population is created and the fittest "a_new_population_size"
 45    //chromosomes are added.
 46  1 Chromosome[] chromosomes = (Chromosome[]) allChromosomes.toArray(new
 47    Chromosome[0]);
 48  1 Population mergedPopulation = new Population(a_population1.
 49    getConfiguration(),
 50    a_new_population_size);
 51  1 for (int i = 0; i < a_new_population_size && i < chromosomes.length; i++) {
 52  4 mergedPopulation.addChromosome(chromosomes[i]);
 53    }
 54    //The merged population is then returned.
 55  1 return mergedPopulation;
 56    }
 57    catch (InvalidConfigurationException iex) {
 58    //this should never happen
 59  0 throw new IllegalStateException(iex.getMessage());
 60    }
 61    }
 62   
 63    /**
 64    * This class is used to sort the merged population chromosomes
 65    * according to their fitness values. For convenience, the
 66    * sorting is done in a reverse way, so this comparator
 67    * returns 1 if the first chromosome has a LOWER fitness value.
 68    *
 69    * @author Henrique Goulart
 70    * @since 2.0
 71    */
 72    private class FitnessChromosomeComparator
 73    implements Comparator {
 74    private transient Configuration m_config;
 75   
 76    //Reference to the current FitnessEvaluator Object, used for comparing
 77    //chromosomes
 78    private FitnessEvaluator m_fEvaluator;
 79   
 80  1 public FitnessChromosomeComparator(Configuration a_config) {
 81  1 m_config = a_config;
 82  1 m_fEvaluator = m_config.getFitnessEvaluator();
 83    }
 84   
 85    /**
 86    * Implements the compare method using the fitness function.
 87    * The comparation is implemented in a reverse way to make the
 88    * merging easier (the list of chromosomes is sorted in a
 89    * descending fitness value order).
 90    * @param a_o1 first IChromosome to compare
 91    * @param a_o2 second IChromosome to compare
 92    * @return @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 93    */
 94  14 public int compare(final Object a_o1, final Object a_o2) {
 95    //The two objects passed are always Chromosomes, so a cast must be made.
 96  14 IChromosome chr1 = (IChromosome) a_o1;
 97  14 IChromosome chr2 = (IChromosome) a_o2;
 98    //Reverse comparison.
 99  14 if (m_fEvaluator.isFitter(chr2.getFitnessValue(),
 100    chr1.getFitnessValue())) {
 101  9 return 1;
 102    }
 103  5 else if (m_fEvaluator.isFitter(chr1.getFitnessValue(),
 104    chr2.getFitnessValue())) {
 105  4 return -1;
 106    }
 107    else {
 108  1 return 0;
 109    }
 110    }
 111    }
 112    }