/*
* File name: ExtractFitnessValues.java (package eas.users.lukas.evaluateMARB)
* Author(s): lko
* Java version: 7.0
* Generation date: 18.09.2012 (13:52:30)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.lukas.evaluateMARB;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import aufnahme.fmg.edu.kit.aifb.AufnahmeTyp.Population;
import aufnahme.fmg.edu.kit.aifb.PopulationTyp.Roboter;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.FileNamePostfixFilter;
import eas.plugins.standard.eaPlugin.xmlRecording.XMLAufnLesen;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
/**
* @author lko
*/
public class ExtractFitnessValues {
/**
* CAUTION: If changingParams == null, comment the field assignments in constructor of
* <code>AbstractMsg</code> before running this method:<BR><BR>
* // this.message = msg;<BR>
* // this.datum = dat;<BR>
* // this.zusatz = zus;
*
* @param path The path from where the statistics are supposed to be extracted.
* @param extractTo
* @param changingParams A list of those single parameters that change
* within this set of runs. If this parameter is set
* to <code>null</code>, the changing parameters are
* determined automatically which is time and space
* expensive.
*/
@SuppressWarnings("unused")
private static void extractAllStatisticsFilesIn(File path, HashSet<String> changingParams) {
ParCollection params = GlobalVariables.getPrematureParameters();
params.complete();
params.setLoggingLevel(-1);
String endungGZ = "gz";
String endung = "xml";
String[] gespGrGZ = path.list(new FileNamePostfixFilter(endungGZ));
String[] gespGr;
String[] gespGrFIT;
if (gespGrGZ == null) {
StaticMethods.log(
StaticMethods.LOG_ERROR,
"Ungueltiges Verzeichnis: " + path.getAbsolutePath(),
params);
throw new RuntimeException("Ungültiges Verzeichnis.\n" + path.getAbsolutePath());
}
// Entpacke Dateien.
for (String s : gespGrGZ) {
if (!new File(path.getAbsolutePath() + File.separator + StaticMethods.datNamOhneHintErw(s)).exists()) {
StaticMethods.logInfo("Entpacken " + s + ".", params);
StaticMethods.entpackeDatei(
path.getAbsolutePath() + File.separator + s,
path.getAbsolutePath() + File.separator + StaticMethods.datNamOhneHintErw(s),
false);
} else {
params.logInfo("Datei existiert bereits: " + StaticMethods.datNamOhneHintErw(s));
}
}
gespGr = path.list(new FileNamePostfixFilter(endung));
// Find all changing parameters.
HashSet<String> differingPars;
if (changingParams != null) {
differingPars = changingParams;
} else {
differingPars = new HashSet<String>();
ArrayList<SingleParameter> allPars0 = null;
for (String s : gespGr) {
if (new File(path + "\\" + s + "-fitness.txt").exists()) {
params.logInfo("Datei existiert bereits: " + s + "-fitness.txt");
} else {
XMLAufnLesen read = new XMLAufnLesen(new File(path + "\\" + s), params);
params.logInfo("Datei gelesen: " + s);
if (allPars0 == null) {
allPars0 = read.getParams().getAllPars();
}
ParCollection parColl = read.getParams();
ArrayList<SingleParameter> allParsI = parColl.getAllPars();
for (SingleParameter par0 : allPars0) {
for (SingleParameter parI : allParsI) {
if (par0.getParameterName().equals(parI.getParameterName())) {
if (!par0.getParValue().equals(parI.getParValue())) {
differingPars.add(par0.getParameterName());
}
}
}
}
}
}
}
params.logInfo("Folgende sich ändernde Parameter gefunden: " + differingPars);
int window = 50;
double[] formerValues = new double[window];
int count;
// Extrahiere Fitness in Einzeldateien.
for (String s : gespGr) {
if (new File(path + "\\" + s + "-fitness.txt").exists()) {
params.logInfo("Datei existiert bereits: " + s + "-fitness.txt");
} else {
XMLAufnLesen read = new XMLAufnLesen(new File(path + "\\" + s), params);
params.logInfo("Datei gelesen: " + s);
LinkedList<String> fitness = new LinkedList<String>();
String preAmble = "";
for (String par : differingPars) {
preAmble += par + "=" + read.getParams().getParValue(par) + ";";
}
count = 0;
double lastMovingAvgVal = 0;
int fitsumGes = 0;
for (Population pop : read.getAllPopulations()) {
String fit = "" + preAmble;
fit += pop.getCycle() + ";";
double fitSum = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (Roboter rob : pop.getRoboterArray()) {
if (rob.getFit() > max) {
max = rob.getFit();
}
if (rob.getFit() < min) {
min = rob.getFit();
}
fit += rob.getFit() +";";
fitSum += rob.getFit();
}
fitSum /= pop.getRoboterArray().length;
fit += fitSum;
formerValues[count % window] = fitSum;
if (count >= window) {
fit += ";" + average(formerValues) + ";" + (average(formerValues) - lastMovingAvgVal);
} else {
fit += ";" + 0 + ";0";
}
// Par;zyklus;rob0...robn;fitSum;movAvg;movAvgDelta;Integral;min;max
fitsumGes += fitSum;
fit += ";" + fitsumGes;
fit += ";" + min + ";" + max;
fitness.add(fit);
count++;
lastMovingAvgVal = average(formerValues);
}
StaticMethods.speichereTextAusArray(path.getAbsolutePath(), s + "-fitness.txt", fitness, params);
}
}
gespGrFIT = path.list(new FileNamePostfixFilter("-fitness.txt"));
LinkedList<String> liste = new LinkedList<String>();
for (String s : gespGrFIT) {
params.logInfo("Schreibe Daten in Gesamtdatei: " + s);
for (String line : StaticMethods.liesTextArray(new File(path + "\\"
+ s), params)) {
liste.add(line);
}
}
StaticMethods.speichereTextAusArray(path.getAbsolutePath(),
"allFitnesses.txt", liste, params);
}
private static double average(double[] array) {
double avg = 0;
for (double i : array) {
avg += i;
}
return avg / array.length;
}
public static void main(String[] args) {
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransNeu\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransNeuDetail\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransNeuDetail-Memory\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransNeuDetail-Memory-Static\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransNeuDetail-Static\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransWOC\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransWOCDetail\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransWOCDetail-Memory\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransWOCDetail-Memory-Static\\Statistiken"), null);
// extractAllStatisticsFilesIn(
// new File("C:\\Users\\lko\\Desktop\\VersucheMitNeuemTranslator\\simTransWOCDetail-Static\\Statistiken"), null);
}
}