1 10 package org.jgap.impl.fitness; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 27 public abstract class TruthTableFitnessFunction 28 extends FitnessFunction { 29 30 private final static String CVS_REVISION = "$Revision: 1.5 $"; 31 32 private Map m_truthTable; 33 34 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 51 public TruthTableFitnessFunction() { 52 this(Genotype.getStaticConfiguration()); 53 } 54 55 65 public TruthTableFitnessFunction(Configuration a_conf) { 66 m_conf = a_conf; 67 } 68 69 70 81 public TruthTableFitnessFunction(Configuration a_conf, final Map a_truthTable) { 82 this(a_conf); 83 setTruthTable(a_truthTable); 84 } 85 86 public void setTruthTable(final Map a_truthTable) { 87 m_truthTable = a_truthTable; 88 } 89 90 public Map getTruthTable() { 91 return m_truthTable; 92 } 93 94 103 protected abstract double evaluate(IChromosome a_chromosome); 104 105 115 public double calcFitness(final Map a_actualInputOutput) { 116 double outputValueGiven, outputValueWanted; 118 Double inputValueWanted; 119 double diffAbs = 0.0f; 120 double delta; 121 double deltaAbs; 122 Set keySet = getTruthTable().keySet(); 123 Iterator keys = keySet.iterator(); 124 while (keys.hasNext()) { 125 inputValueWanted = (Double ) keys.next(); 126 outputValueWanted = ( (Double ) getTruthTable().get(inputValueWanted)). 127 doubleValue(); 128 Double output = ( (Double ) a_actualInputOutput.get(inputValueWanted)); 129 if (output != null) { 130 outputValueGiven = output.doubleValue(); 131 if (Double.isNaN(outputValueWanted)) { 133 return Double.NaN; 134 } 135 delta = outputValueGiven - outputValueWanted; 136 deltaAbs = (float) Math.abs(delta); 137 } 138 else { 139 deltaAbs = Math.abs(outputValueWanted); 140 } 141 diffAbs += deltaAbs; 142 } 143 145 146 return diffAbs; 147 } 148 149 155 public Configuration getConfiguration() { 156 return m_conf; 157 } 158 } 159 | Popular Tags |