Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 192   Methods: 7
NCLOC: 93   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
ThresholdSelector.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    * Implementation of a NaturalSelector that ensures a certain threshold of the
 17    * best chromosomes are taken to the next generation.
 18    *
 19    * @author Klaus Meffert
 20    * @since 2.0
 21    */
 22    public class ThresholdSelector
 23    extends NaturalSelector {
 24    /** String containing the CVS revision. Read out via reflection!*/
 25    private final static String CVS_REVISION = "$Revision: 1.14 $";
 26   
 27    /**
 28    * Stores the chromosomes to be taken into account for selection
 29    */
 30    private List m_chromosomes;
 31   
 32    /**
 33    * Indicated whether the list of added chromosomes needs sorting
 34    */
 35    private boolean m_needsSorting;
 36   
 37    /**
 38    * Comparator that is only concerned about fitness values
 39    */
 40    private FitnessValueComparator m_fitnessValueComparator;
 41   
 42    private ThresholdSelectorConfigurable m_config
 43    = new ThresholdSelectorConfigurable();
 44   
 45    /**
 46    * Default constructor. Uses threshold of 30 percent.<p>
 47    * Attention: The configuration used is the one set with the static method
 48    * Genotype.setConfiguration.
 49    *
 50    * @author Klaus Meffert
 51    * @since 2.6
 52    */
 53  1 public ThresholdSelector() {
 54  1 this(Genotype.getStaticConfiguration(), 0.3d);
 55    }
 56   
 57    /**
 58    * @param a_config the configuration to use
 59    * @param a_bestChromosomes_Percentage indicates the number of best
 60    * chromosomes from the population to be selected for granted. All other
 61    * chromosomes will be selected in a random fashion. The value must be in
 62    * the range from 0.0 to 1.0.
 63    *
 64    * @author Klaus Meffert
 65    * @since 2.0
 66    */
 67  14 public ThresholdSelector(final Configuration a_config,
 68    final double a_bestChromosomes_Percentage) {
 69  14 super(a_config);
 70  14 if (a_bestChromosomes_Percentage < 0.0000000d
 71    || a_bestChromosomes_Percentage > 1.0000000d) {
 72  2 throw new IllegalArgumentException("Percentage must be between 0.0"
 73    + " and 1.0 !");
 74    }
 75  12 m_config.m_bestChroms_Percentage = a_bestChromosomes_Percentage;
 76  12 m_chromosomes = new Vector();
 77  12 m_needsSorting = false;
 78  12 m_fitnessValueComparator = new FitnessValueComparator();
 79    }
 80   
 81    /**
 82    * Select a given number of Chromosomes from the pool that will move on
 83    * to the next generation population. This selection will be guided by the
 84    * fitness values. The chromosomes with the best fitness value win.
 85   
 86    * @param a_howManyToSelect the number of Chromosomes to select
 87    * @param a_from_pop the population the Chromosomes will be selected from
 88    * @param a_to_pop the population the Chromosomes will be added to
 89    *
 90    * @author Klaus Meffert
 91    * @since 2.0
 92    */
 93  11 public void select(final int a_howManyToSelect, final Population a_from_pop,
 94    Population a_to_pop) {
 95  11 if (a_from_pop != null) {
 96  1 int size = a_from_pop.size();
 97  1 for (int i = 0; i < size; i++) {
 98  1 add(a_from_pop.getChromosome(i));
 99    }
 100    }
 101  11 int canBeSelected;
 102  11 if (a_howManyToSelect > m_chromosomes.size()) {
 103  3 canBeSelected = m_chromosomes.size();
 104    }
 105    else {
 106  8 canBeSelected = a_howManyToSelect;
 107    }
 108    // Sort the collection of chromosomes previously added for evaluation.
 109    // Only do this if necessary.
 110    // -------------------------------------------------------------------
 111  11 if (m_needsSorting) {
 112  9 Collections.sort(m_chromosomes, m_fitnessValueComparator);
 113  9 m_needsSorting = false;
 114    }
 115    // Select the best chromosomes for granted
 116  11 int bestToBeSelected = (int) Math.round(canBeSelected
 117    * m_config.m_bestChroms_Percentage);
 118  11 for (int i = 0; i < bestToBeSelected; i++) {
 119  15 a_to_pop.addChromosome( (IChromosome) m_chromosomes.get(i));
 120    }
 121    // Fill up the rest by randomly selecting chromosomes
 122  11 int missing = a_howManyToSelect - bestToBeSelected;
 123  11 RandomGenerator rn = getConfiguration().getRandomGenerator();
 124  11 int index;
 125  11 int size = m_chromosomes.size();
 126  11 for (int i = 0; i < missing; i++) {
 127  85 index = rn.nextInt(size);
 128  85 a_to_pop.addChromosome( (IChromosome) m_chromosomes.get(index));
 129    }
 130    }
 131   
 132    /**
 133    * @return false as we allow to return the same chromosome multiple times
 134    *
 135    * @author Klaus Meffert
 136    * @since 2.0
 137    */
 138  1 public boolean returnsUniqueChromosomes() {
 139  1 return false;
 140    }
 141   
 142  3 public void empty() {
 143  3 m_chromosomes.clear();
 144  3 m_needsSorting = false;
 145    }
 146   
 147    /**
 148    *
 149    * @param a_chromosomeToAdd Chromosome
 150    *
 151    * @author Klaus Meffert
 152    * @since 2.0
 153    */
 154  20 protected void add(final IChromosome a_chromosomeToAdd) {
 155  20 m_chromosomes.add(a_chromosomeToAdd);
 156  20 m_needsSorting = true;
 157    }
 158   
 159    /**
 160    * Comparator regarding only the fitness value. Best fitness value will
 161    * be on first position of resulting sorted list.
 162    *
 163    * @author Klaus Meffert
 164    * @since 2.0
 165    */
 166    private class FitnessValueComparator
 167    implements Comparator {
 168  11 public int compare(final Object a_first, final Object a_second) {
 169  11 IChromosome chrom1 = (IChromosome) a_first;
 170  11 IChromosome chrom2 = (IChromosome) a_second;
 171  11 if (getConfiguration().getFitnessEvaluator().isFitter(chrom2.
 172    getFitnessValue(), chrom1.getFitnessValue())) {
 173  8 return 1;
 174    }
 175  3 else if (getConfiguration().getFitnessEvaluator().isFitter(
 176    chrom1.getFitnessValue(), chrom2.getFitnessValue())) {
 177  2 return -1;
 178    }
 179    else {
 180  1 return 0;
 181    }
 182    }
 183    }
 184    class ThresholdSelectorConfigurable {
 185    /**
 186    * This percentage indicates the number of best chromosomes from the
 187    * population to be selected for granted. All other chromosomes will
 188    * be selected in a random fashion.
 189    */
 190    public double m_bestChroms_Percentage;
 191    }
 192    }