Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 158   Methods: 7
NCLOC: 60   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TruthTableFitnessFunction.java 83,3% 96,3% 100% 95%
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.fitness;
 11   
 12    import java.util.*;
 13    import org.jgap.*;
 14   
 15    /**
 16    * Fitness Function relying on a truth table.
 17    * <p>
 18    * To use this class, just implement a subclass of it, register the current
 19    * truth table and implement the evaluate method in a way that you can call
 20    * calcFitness with the required (parameter (given values: encoded with the
 21    * chromosome). If the truth table is dynamic just register it in evaluate,
 22    * otherwise do it after construction of this fitness function.
 23    *
 24    * @author Klaus Meffert
 25    * @since 2.4
 26    */
 27    public abstract class TruthTableFitnessFunction
 28    extends FitnessFunction {
 29    /** String containing the CVS revision. Read out via reflection!*/
 30    private final static String CVS_REVISION = "$Revision: 1.5 $";
 31   
 32    private Map m_truthTable;
 33   
 34    // Constants for calculating the fitness value
 35    // -------------------------------------------
 36    public static final int MAX_FITNESS = 9999999;
 37   
 38    private static final int RELATION_FITNESS = 100;
 39   
 40    public static final int WORST = MAX_FITNESS / RELATION_FITNESS;
 41   
 42    private Configuration m_conf;
 43   
 44    /**
 45    * Only use for dynamic instantiation as configuration retrieved from static
 46    * setting.
 47    *
 48    * @author Klaus Meffert
 49    * @since 2.4
 50    */
 51  1 public TruthTableFitnessFunction() {
 52  1 this(Genotype.getStaticConfiguration());
 53    }
 54   
 55    /**
 56    * Constructor without registering a truth table. Use this constructor if the
 57    * truth table is not fix over generations and needs to be set in method
 58    * evaluate.
 59    *
 60    * @param a_conf the configuration to use
 61    *
 62    * @author Klaus Meffert
 63    * @since 3.01
 64    */
 65  5 public TruthTableFitnessFunction(Configuration a_conf) {
 66  5 m_conf = a_conf;
 67    }
 68   
 69   
 70    /**
 71    * Constructor for registering a truth table. Use this constructor if the
 72    * truth table is fix over generations.
 73    *
 74    * @param a_conf the configuration to use
 75    * @param a_truthTable table of input/output pairs for feeding the formula
 76    * and determining the fitness value thru delta computation
 77    *
 78    * @author Klaus Meffert
 79    * @since 3.1
 80    */
 81  3 public TruthTableFitnessFunction(Configuration a_conf, final Map a_truthTable) {
 82  3 this(a_conf);
 83  3 setTruthTable(a_truthTable);
 84    }
 85   
 86  4 public void setTruthTable(final Map a_truthTable) {
 87  4 m_truthTable = a_truthTable;
 88    }
 89   
 90  11 public Map getTruthTable() {
 91  11 return m_truthTable;
 92    }
 93   
 94    /**
 95    * Implementation of the evaluate method from class FitnessFunction.
 96    * Calculates the fitness of a given Chromosome in a determined way.
 97    * @param a_chromosome the Chromosome to be evaluated
 98    * @return positive integer value representing the fitness of the Chromosome
 99    *
 100    * @author Klaus Meffert
 101    * @since 2.4
 102    */
 103    protected abstract double evaluate(IChromosome a_chromosome);
 104   
 105    /**
 106    * Fitness value calculation for a given table of input/output tupels
 107    * and a truth-table (also given as list of input/output tupels)
 108    *
 109    * @param a_actualInputOutput table of actual input/output pairs
 110    * @return delta between current values and given truth table
 111    *
 112    * @author Klaus Meffert
 113    * @since 2.4
 114    */
 115  3 public double calcFitness(final Map a_actualInputOutput) {
 116    // Determine delta values of all function values and add up their squares
 117  3 double outputValueGiven, outputValueWanted;
 118  3 Double inputValueWanted;
 119  3 double diffAbs = 0.0f;
 120  3 double delta;
 121  3 double deltaAbs;
 122  3 Set keySet = getTruthTable().keySet();
 123  3 Iterator keys = keySet.iterator();
 124  3 while (keys.hasNext()) {
 125  8 inputValueWanted = (Double) keys.next();
 126  8 outputValueWanted = ( (Double) getTruthTable().get(inputValueWanted)).
 127    doubleValue();
 128  8 Double output = ( (Double) a_actualInputOutput.get(inputValueWanted));
 129  8 if (output != null) {
 130  5 outputValueGiven = output.doubleValue();
 131    // determine current value (evolved formula) minus reference value
 132  5 if (Double.isNaN(outputValueWanted)) {
 133  0 return Double.NaN;
 134    }
 135  5 delta = outputValueGiven - outputValueWanted;
 136  5 deltaAbs = (float) Math.abs(delta);
 137    }
 138    else {
 139  3 deltaAbs = Math.abs(outputValueWanted);
 140    }
 141  8 diffAbs += deltaAbs;
 142    }
 143    /**@todo consider length of formula (i.e. number of terms, e.g.) for
 144    * fitness calculation*/
 145   
 146  3 return diffAbs;
 147    }
 148   
 149    /**
 150    * @return the Configuration object set
 151    *
 152    * @author Klaus Meffert
 153    * @since 3.1
 154    */
 155  2 public Configuration getConfiguration() {
 156  2 return m_conf;
 157    }
 158    }