Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 254   Methods: 12
NCLOC: 159   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PermutingConfiguration.java 97,1% 100% 100% 99,3%
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.audit;
 11   
 12    import org.jgap.*;
 13    import java.util.*;
 14   
 15    /**
 16    * Configuration that allows for permutating several components of it for
 17    * evaluation/auditing purposes.
 18    *
 19    * @author Klaus Meffert
 20    * @since 2.2
 21    */
 22    public class PermutingConfiguration
 23    extends Configuration {
 24    /** String containing the CVS revision. Read out via reflection!*/
 25    private final static String CVS_REVISION = "$Revision: 1.7 $";
 26   
 27    private List m_randomGeneratorSlots;
 28   
 29    private int m_randomGeneratorIndex;
 30   
 31    private List m_naturalSelectorSlots;
 32   
 33    private int m_naturalSelectorIndex;
 34   
 35    private List m_geneticOperatorSlots;
 36   
 37    private int m_geneticOperatorIndex;
 38   
 39    private List m_fitnessFunctionSlots;
 40   
 41    private int m_fitnessFunctionIndex;
 42   
 43    private int m_componentIndex;
 44   
 45    /**
 46    * The resulting configuration as determined by permutation.
 47    */
 48    private Configuration m_configuration;
 49   
 50  19 public PermutingConfiguration() {
 51  19 super();
 52  19 init();
 53    }
 54   
 55  19 public void init() {
 56  19 m_randomGeneratorSlots = new Vector();
 57  19 m_naturalSelectorSlots = new Vector();
 58  19 m_geneticOperatorSlots = new Vector();
 59  19 m_fitnessFunctionSlots = new Vector();
 60  19 m_randomGeneratorIndex = 0;
 61  19 m_naturalSelectorIndex = 0;
 62  19 m_geneticOperatorIndex = 0;
 63  19 m_fitnessFunctionIndex = 0;
 64  19 m_componentIndex = 0;
 65    }
 66   
 67    /**
 68    * Initializes the configuration by preselecting important parameters from
 69    * the input configuration object
 70    * @param a_conf Configuration
 71    * @throws InvalidConfigurationException
 72    *
 73    * @author Klaus Meffert
 74    * @since 2.2
 75    */
 76  19 public PermutingConfiguration(Configuration a_conf)
 77    throws InvalidConfigurationException {
 78  19 this();
 79  19 setEventManager(a_conf.getEventManager());
 80  19 setFitnessEvaluator(a_conf.getFitnessEvaluator());
 81  19 if (a_conf.getFitnessFunction() != null) {
 82  17 setFitnessFunction(a_conf.getFitnessFunction());
 83    }
 84  19 setMinimumPopSizePercent(a_conf.getMinimumPopSizePercent());
 85  19 if (a_conf.getPopulationSize() > 0) {
 86  14 setPopulationSize(a_conf.getPopulationSize());
 87    }
 88  19 if (a_conf.getSampleChromosome() != null) {
 89  14 setSampleChromosome(a_conf.getSampleChromosome());
 90    }
 91  19 setRandomGenerator(a_conf.getRandomGenerator());
 92  19 if (a_conf.getChromosomePool() != null) {
 93  4 setChromosomePool(a_conf.getChromosomePool());
 94    }
 95    }
 96   
 97  4 public void addRandomGeneratorSlot(RandomGenerator a_randomGenerator) {
 98  4 m_randomGeneratorSlots.add(a_randomGenerator);
 99    }
 100   
 101  1 public void addNaturalSelector(NaturalSelector a_naturalSel, boolean a_egal) {
 102  1 throw new UnsupportedOperationException(
 103    "Use addNaturalSelectorSlot instead!");
 104    }
 105   
 106  4 public void addNaturalSelectorSlot(NaturalSelector a_naturalSelector) {
 107  4 m_naturalSelectorSlots.add(a_naturalSelector);
 108    }
 109   
 110  1 public synchronized void addGeneticOperator(GeneticOperator a_geneticOp) {
 111  1 throw new UnsupportedOperationException(
 112    "Use addGeneticOperatorSlot instead!");
 113    }
 114   
 115  4 public void addGeneticOperatorSlot(GeneticOperator a_geneticOperator) {
 116  4 m_geneticOperatorSlots.add(a_geneticOperator);
 117    }
 118   
 119  3 public void addFitnessFunctionSlot(FitnessFunction a_fitnessFunction) {
 120  3 m_fitnessFunctionSlots.add(a_fitnessFunction);
 121    }
 122   
 123  20 public Configuration next()
 124    throws InvalidConfigurationException {
 125  20 m_configuration = new Configuration();
 126  20 m_configuration.setEventManager(getEventManager());
 127  20 m_configuration.setFitnessEvaluator(getFitnessEvaluator());
 128  20 if (getFitnessFunction() != null) {
 129  2 m_configuration.resetProperty(Configuration.PROPERTY_FITFUNC_INST);
 130  2 setFitnessFunction(getFitnessFunction());
 131    }
 132  20 m_configuration.setMinimumPopSizePercent(getMinimumPopSizePercent());
 133  20 if (getPopulationSize() > 0) {
 134  1 m_configuration.setPopulationSize(getPopulationSize());
 135    }
 136  20 if (getSampleChromosome() != null) {
 137  1 m_configuration.setSampleChromosome(getSampleChromosome());
 138    }
 139  20 m_configuration.setRandomGenerator(getRandomGenerator());
 140  20 if (getChromosomePool() != null) {
 141  18 m_configuration.setChromosomePool(getChromosomePool());
 142    }
 143  20 List list;
 144  20 Iterator it;
 145    /**@todo make permutation below computed dynamic and not static*/
 146    /**@todo introduce new parameters: populationSize,
 147    * setPreservFittestIndividual, MAX_ALLOWED_EVOLUTIONS
 148    */
 149   
 150    // Permute GeneticOperator's.
 151    // --------------------------
 152  20 if (m_geneticOperatorIndex >=
 153    Math.pow(2, m_geneticOperatorSlots.size()) - 1) {
 154    // m_componentIndex++;
 155    // }
 156    // if (bitSet(m_componentIndex, 0)) {
 157  5 m_geneticOperatorIndex = 0;
 158  5 m_naturalSelectorIndex++;
 159    // m_randomGeneratorIndex = 0;
 160    // m_fitnessFunctionIndex = 0;
 161    }
 162  20 list = nextList(m_geneticOperatorIndex++, m_geneticOperatorSlots);
 163  20 it = list.iterator();
 164  20 GeneticOperator op;
 165  20 while (it.hasNext()) {
 166  26 op = (GeneticOperator) it.next();
 167  26 m_configuration.addGeneticOperator(op);
 168    }
 169    // Permute NaturalSelector's.
 170    // --------------------------
 171  20 if (m_naturalSelectorIndex >=
 172    Math.pow(2, m_naturalSelectorSlots.size()) - 1) {
 173    // m_componentIndex++;
 174    // }
 175    // if (bitSet(m_componentIndex, 1)) {
 176  1 m_naturalSelectorIndex = 0;
 177  1 m_randomGeneratorIndex++;
 178    // m_fitnessFunctionIndex = 0;
 179    }
 180  20 list = nextList(m_naturalSelectorIndex, m_naturalSelectorSlots);
 181  20 it = list.iterator();
 182  20 NaturalSelector ns;
 183  20 while (it.hasNext()) {
 184  26 ns = (NaturalSelector) it.next();
 185  26 m_configuration.addNaturalSelector(ns, true);
 186    /**@todo allow for "false"*/
 187    }
 188    // Permute RandomGenerator's.
 189    // --------------------------
 190    // if (true || bitSet(m_componentIndex, 2)) {
 191  20 m_randomGeneratorIndex++;
 192  20 if (m_randomGeneratorIndex >= m_randomGeneratorSlots.size()) {
 193  11 m_randomGeneratorIndex = 0;
 194  11 m_fitnessFunctionIndex++;
 195    }
 196    // }
 197  20 RandomGenerator rg = (RandomGenerator) m_randomGeneratorSlots.get(
 198    m_randomGeneratorIndex);
 199  20 m_configuration.setRandomGenerator(rg);
 200    // Permute FitnessFunction's.
 201    // --------------------------
 202    // if (true || bitSet(m_componentIndex, 3)) {
 203  20 m_fitnessFunctionIndex++;
 204  20 if (m_fitnessFunctionIndex >= m_fitnessFunctionSlots.size()) {
 205  20 m_fitnessFunctionIndex = 0;
 206    }
 207    // }
 208    /**@todo BulkFitnessOffsetRemover vs. FitnessFunction*/
 209   
 210    // System.err.println(m_fitnessFunctionIndex+" / "+index++);
 211  20 FitnessFunction ff = (FitnessFunction) m_fitnessFunctionSlots.get(
 212    m_fitnessFunctionIndex);
 213  20 m_configuration.reset();
 214  20 m_configuration.setFitnessFunction(ff);
 215  20 m_componentIndex++;
 216  20 return m_configuration;
 217    }
 218   
 219    /**
 220    * Returns a subset of a given list acording to the index given.
 221    * If a bit in the binary number represented by the index is set then the
 222    * element at this index in the list will be included in the result list
 223    * @param index int
 224    * @param list List
 225    * @return List
 226    */
 227  40 private List nextList(int index, List list) {
 228  40 if (index <= 0) {
 229  16 index = 1;
 230    }
 231    else {
 232  24 index++;
 233    }
 234  40 List newList = new Vector();
 235  40 for (int i = 0; i < list.size(); i++) {
 236  76 if ( (index & (int) Math.pow(2, i)) > 0) {
 237  52 newList.add(list.get(i));
 238    }
 239    }
 240  40 return newList;
 241    }
 242   
 243    // private boolean bitSet(int number, int bitIndex) {
 244    // return ( (number & (int) Math.pow(2, (bitIndex))) > 0);
 245    // }
 246   
 247  7 public boolean hasNext() {
 248  7 double r = (m_randomGeneratorSlots.size())
 249    * (m_fitnessFunctionSlots.size())
 250    * (Math.pow(2, m_naturalSelectorSlots.size()) - 1)
 251    * (Math.pow(2, m_geneticOperatorSlots.size()) - 1);
 252  7 return m_componentIndex < r;
 253    }
 254    }