1 10 package org.jgap.audit; 11 12 import org.jgap.*; 13 import java.util.*; 14 15 21 public class Evaluator { 22 25 26 27 private final static String CVS_REVISION = "$Revision: 1.10 $"; 28 29 32 private Map m_permutationData; 33 34 37 private Map m_permutationRuns; 38 39 42 private KeyedValues2D m_data; 43 44 private PermutingConfiguration m_permConf; 45 46 49 private Map m_genotypeData; 50 51 54 private List m_genotypeDataAvg; 55 56 public Evaluator(final PermutingConfiguration a_conf) { 57 if (a_conf == null) { 58 throw new IllegalArgumentException ("Configuration must not be null!"); 59 } 60 m_permConf = a_conf; 61 m_data = new KeyedValues2D(); 62 m_permutationData = new Hashtable(); 63 m_permutationRuns = new Hashtable(); 64 m_genotypeData = new Hashtable(); 65 m_genotypeDataAvg = new Vector(); 66 } 67 68 public boolean hasNext() { 69 return m_permConf.hasNext(); 70 } 71 72 public Configuration next() 73 throws InvalidConfigurationException { 74 return m_permConf.next(); 75 } 76 77 public void setValue(double a_value, Comparable a_rowKey, 78 Comparable a_columnKey) { 79 m_data.setValue(new Double (a_value), a_rowKey, a_columnKey); 80 } 82 83 public Number getValue(Comparable rowKey, Comparable columnKey) { 84 return m_data.getValue(rowKey, columnKey); 85 } 86 87 98 public void setValue(int a_permutation, int a_run, double a_value, 99 Comparable a_rowKey, Comparable a_columnKey) { 100 Object key = createKey(a_permutation, a_run); 101 KeyedValues2D a_data = (KeyedValues2D) m_permutationData.get( 102 key); 103 if (a_data == null) { 104 a_data = new KeyedValues2D(); 105 m_permutationData.put(key, a_data); 106 } 107 addRunNumber(a_permutation, a_run); 110 a_data.setValue(new Double (a_value), a_rowKey, a_columnKey); 111 } 112 113 protected void addRunNumber(int a_permutation, int a_run) { 114 Map v = (Map) m_permutationRuns.get(new Integer (a_permutation)); 115 if (v == null) { 116 v = new Hashtable(); 117 } 118 v.put(new Integer (a_run), new Integer (a_run)); 119 m_permutationRuns.put(new Integer (a_permutation), v); 120 } 121 122 public Number getValue(int a_permutation, int a_run, Comparable rowKey, 123 Comparable columnKey) { 124 KeyedValues2D a_data = (KeyedValues2D) m_permutationData.get( 125 createKey(a_permutation, a_run)); 126 if (a_data == null) { 127 return null; 128 } 129 return a_data.getValue(rowKey, columnKey); 130 } 131 132 public KeyedValues2D getData() { 133 return m_data; 134 } 135 136 protected Object createKey(int a_permutation, int a_run) { 137 return a_permutation + "_" + a_run; 138 } 139 140 151 public KeyedValues2D calcAvgFitness(int a_permutation) { 152 if (a_permutation == -1) { 153 Iterator it = m_permutationRuns.keySet().iterator(); 154 Integer permNumberI; 155 int permNumber; 156 KeyedValues2D result = new KeyedValues2D(); 157 while (it.hasNext()) { 158 permNumberI = (Integer ) it.next(); 159 permNumber = permNumberI.intValue(); 160 calcAvgFitnessHelper(permNumber, result); 161 } 162 return result; 163 } 164 else { 165 KeyedValues2D a_data = new KeyedValues2D(); 166 calcAvgFitnessHelper(a_permutation, a_data); 167 return a_data; 168 } 169 } 170 171 protected void calcAvgFitnessHelper(int a_permutation, 172 final KeyedValues2D result) { 173 Map runNumbers = (Map) m_permutationRuns.get(new Integer (a_permutation)); 176 if (runNumbers == null) { 177 return; 178 } 179 Iterator it = runNumbers.keySet().iterator(); 182 int numRuns = runNumbers.keySet().size(); 183 Integer runI; 184 while (it.hasNext()) { 185 runI = (Integer ) it.next(); 186 KeyedValues2D a_data = (KeyedValues2D) m_permutationData. 189 get(createKey(a_permutation, runI.intValue())); 190 for (int col = 0; col < a_data.getColumnCount(); col++) { 194 for (int row = 0; row < a_data.getRowCount(); row++) { 195 Double d = (Double ) result.getValue(a_data.getRowKey(row), 198 a_data.getColumnKey(col)); 199 double newValue; 200 if (d == null) { 201 newValue = 0.0d; 202 } 203 else { 204 newValue = d.doubleValue(); 205 } 206 newValue += 210 a_data.getValue(a_data.getRowKey(row), a_data.getColumnKey(col)). 211 doubleValue() / numRuns; 212 result.setValue(new Double (newValue), a_data.getRowKey(row), 215 a_data.getColumnKey(col)); 216 } 217 } 218 } 219 } 220 221 226 227 231 240 public KeyedValues2D calcAvgFitnessImpr(int a_permutation) { 241 242 243 Map runNumbers = (Map) m_permutationRuns.get(new Integer (a_permutation)); 244 if (runNumbers == null) { 245 return null; 246 } 247 Iterator it = runNumbers.keySet().iterator(); 251 Integer runI; 253 while (it.hasNext()) { 254 runI = (Integer ) it.next(); 255 KeyedValues2D a_data = (KeyedValues2D) m_permutationData. 258 get(createKey(a_permutation, runI.intValue())); 259 for (int col = 0; col < a_data.getColumnCount(); col++) { 260 for (int row = 0; row < a_data.getRowCount(); row++) { 261 } 262 } 263 } 264 return null; 265 } 266 267 272 public int getNumberOfRuns(int a_permutation) { 273 Map runNumbers = (Map) m_permutationRuns.get(new Integer (a_permutation)); 274 if (runNumbers == null) { 275 return 0; 276 } 277 else { 278 return runNumbers.keySet().size(); 279 } 280 } 281 282 291 public void storeGenotype(int a_permutation, int a_run, Genotype a_genotype) { 292 293 GenotypeData data = new GenotypeData(); 296 int generation = a_genotype.getConfiguration().getGenerationNr(); 297 data.generation = generation; 298 Population pop = a_genotype.getPopulation(); 299 data.hashCode = a_genotype.hashCode(); 300 int popSize = pop.size(); 301 data.chromosomeData = new ChromosomeData[popSize]; 302 data.size = popSize; 303 IChromosome chrom; 305 ChromosomeData chromData; 306 for (int i = 0; i < popSize; i++) { 307 chrom = pop.getChromosome(i); 308 chromData = new ChromosomeData(); 309 chromData.fitnessValue = chrom.getFitnessValue(); 310 chromData.size = chrom.size(); 311 chromData.index = i; 312 data.chromosomeData[i] = chromData; 313 } 314 String key = a_permutation + "_" + a_run; 315 m_genotypeData.put(key, data); 316 addRunNumber(a_permutation, a_run); 317 } 318 319 public GenotypeData retrieveGenotype(int a_permutation, int a_run) { 320 return (GenotypeData) m_genotypeData.get(a_permutation + "_" + a_run); 321 } 322 323 334 public GenotypeDataAvg calcPerformance(int a_permutation) { 335 int numRuns = getNumberOfRuns(a_permutation); 336 GenotypeData data; 337 GenotypeDataAvg dataAvg = new GenotypeDataAvg(); 338 dataAvg.permutation = a_permutation; 339 double sizeAvg = 0.0d; 340 double fitnessAvg = 0.0d; 341 double fitnessBest = -1.0d; 342 double fitnessBestOld = -1.0d; 343 double fitness; 344 int fitnessBestGen = -1; 345 double fitnessAvgChroms; 346 double fitnessDiversityChromsOld = -1.0d; 347 double fitnessBestDeltaAvg = 0.0d; 348 double fitnessDiversity; 349 double fitnessDiversityAvg = 0.0d; 350 int size; 351 ChromosomeData chrom; 352 for (int i = 0; i < numRuns; i++) { 353 data = retrieveGenotype(a_permutation, i); 354 if (i == 0) { 356 dataAvg.generation = data.generation; 357 } 358 sizeAvg += (double) data.size / numRuns; 360 size = data.size; 361 fitnessAvgChroms = 0.0d; 362 fitnessDiversity = 0.0d; 363 double fitnessBestLocal = -1.0d; 364 for (int j = 0; j < size; j++) { 365 chrom = data.chromosomeData[j]; 366 fitness = chrom.fitnessValue; 367 if (j > 0) { 369 fitnessDiversity += Math.abs(fitness - fitnessDiversityChromsOld) / 370 (size - 1); 371 } 372 fitnessDiversityChromsOld = fitness; 373 fitnessAvgChroms += fitness / size; 375 if (fitnessBest < fitness) { 377 fitnessBest = fitness; 378 fitnessBestGen = data.generation; 380 } 381 if (fitnessBestLocal < fitness) { 383 fitnessBestLocal = fitness; 384 } 385 } 386 fitnessAvg += fitnessAvgChroms / numRuns; 388 fitnessDiversityAvg += fitnessDiversity / numRuns; 390 if (i > 0) { 392 fitnessBestDeltaAvg += Math.abs(fitnessBestLocal - fitnessBestOld) / 393 (numRuns - 1); 394 } 395 fitnessBestOld = fitnessBestLocal; 396 } 397 dataAvg.sizeAvg = sizeAvg; 398 dataAvg.avgFitnessValue = fitnessAvg; 399 dataAvg.bestFitnessValue = fitnessBest; 400 dataAvg.bestFitnessValueGeneration = fitnessBestGen; 401 dataAvg.avgDiversityFitnessValue = fitnessDiversityAvg; 402 dataAvg.avgBestDeltaFitnessValue = fitnessBestDeltaAvg; 403 m_genotypeDataAvg.add(dataAvg); 405 return dataAvg; 406 } 407 408 414 public class GenotypeDataAvg { 415 public int permutation; 416 417 public int generation; 418 419 public double sizeAvg; 420 421 public double bestFitnessValue; 422 423 public double avgFitnessValue; 424 425 public int bestFitnessValueGeneration; 426 427 public double avgDiversityFitnessValue; 428 429 public double avgBestDeltaFitnessValue; 430 } 431 437 public class GenotypeData { 438 public int generation; 439 440 public int hashCode; 441 442 public int size; 443 444 public ChromosomeData[] chromosomeData; 445 } 446 public class ChromosomeData { 447 public int index; 448 449 public int size; 450 451 public double fitnessValue; 452 } 453 } 454 | Popular Tags |