Created
May 26, 2018 20:56
-
-
Save dlaertius/5c6b1b2fee53477530415d3294c05c46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ga.main; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
//import java.util.Calendar; | |
/* | |
* Essa classe será responsável por armazenar em um arquivo e apresentar os dados de cada execução. | |
* | |
*/ | |
public class Press { | |
private static int[] maxGen; | |
private static long[] maxFitCalls; | |
private static double[] average; | |
private static double[] bestIndiFt; | |
private static String[] bestIndi; | |
public static boolean flagPartition = false; | |
public static double[] partitionAvg; | |
public static int[] maxPartiSize; | |
public static void initializePress () { | |
maxGen = new int[GA.nRuns]; | |
maxFitCalls = new long[GA.nRuns]; | |
average = new double[GA.nRuns]; | |
bestIndiFt = new double[GA.nRuns]; | |
bestIndi = new String[GA.nRuns]; | |
int recMet = Parameter.getRecombinationMethod(); | |
if (recMet == 2 || recMet == 3) { | |
flagPartition = true; | |
partitionAvg = new double[GA.nRuns]; | |
maxPartiSize = new int[GA.nRuns]; | |
} | |
} | |
public static void currentRunInfo (int runNumber, int generations, long ftCall, double avg, double bestInd, String indi) { | |
maxGen[runNumber] = generations; | |
maxFitCalls[runNumber] = ftCall; | |
average[runNumber] = avg; | |
bestIndiFt[runNumber] = bestInd; | |
bestIndi[runNumber] = indi; | |
} | |
public static void pxInfo(int runNumber, double mean, int maxPart) { | |
partitionAvg[runNumber] = mean; | |
maxPartiSize[runNumber] = maxPart; | |
} | |
public static void saveGraph2File (String fileName, String header, String graph) { | |
String pathName = "src/ga/logs/bpx-graphs/graph-" + fileName + ".txt"; | |
File log = new File(pathName); | |
String writeToFile = "#"; | |
//writeToFile += header + "|"; | |
writeToFile += graph; | |
try{ | |
if(!log.exists()){ | |
log.getParentFile().mkdirs(); | |
log.createNewFile(); | |
} | |
try (PrintWriter out = new PrintWriter(new FileWriter(log, true))) { | |
out.append(writeToFile); | |
out.close(); | |
//System.out.println("\n_:Data saved to file " + pathName); | |
}catch (Exception e) { System.err.println("Append Graph file failed..");} | |
}catch (Exception e) { System.err.println("Create Graph file failed.."); } | |
} | |
public static void save2File (String fileName, String header) { | |
String pathName = "src/ga/logs/" + fileName + ".txt"; | |
File log = new File(pathName); | |
String writeToFile = ""; | |
// writeToFile += "\n" + header + "\n"; | |
// writeToFile += "Date: " + Calendar.getInstance().getTime() + "\n"; | |
/* | |
writeToFile += "\n[Best Individuals Fitness]\n"; | |
for (int i = 0; i < bestIndiFt.length; i++) { | |
//writeToFile += "Run: " + (i+1) + " -> Individual: " + Arrays.toString(bestIndividuals[i].getGenes()) + "\n"; | |
writeToFile += "Fitness: " + bestIndiFt[i] + "\n"; | |
} | |
writeToFile += "\n[Best Individuals String] \n"; | |
for (int i = 0; i < GA.nRuns; i++) { | |
writeToFile += "Run " + (i+1) + ": " + bestIndi[i] + "\n"; | |
} | |
writeToFile += "\n[Max Generations Reached] \n"; | |
for (int i = 0; i < GA.nRuns; i++) { | |
writeToFile += "Run " + (i+1) + ": " + maxGen[i] + "\n"; | |
} | |
writeToFile += "\n[Evaluations Number] \n"; | |
for (int i = 0; i < GA.nRuns; i++) { | |
writeToFile += "Run " + (i+1) + ": " + maxFitCalls[i] + "\n"; | |
} | |
writeToFile += "\n[Average Last Population Fitness] \n"; | |
for (int i = 0; i < GA.nRuns; i++) { | |
writeToFile += "Run " + (i+1) + ": " + average[i] + "\n"; | |
} | |
if (flagPartition) { | |
writeToFile += "\n[Max Partition Found] \n"; | |
for (int i = 0; i < GA.nRuns; i++) { | |
writeToFile += "Run " + (i+1) + ": " + maxPartiSize[i] + "\n"; | |
} | |
writeToFile += "\n[Average Partitions Sum/Found] \n"; | |
for (int i = 0; i < GA.nRuns; i++) { | |
writeToFile += "Run " + (i+1) + ": " + partitionAvg[i] + "\n"; | |
} | |
} | |
*/ | |
/* | |
* File Header. | |
*/ | |
if (flagPartition) { | |
writeToFile += "[Best Fitness, Best Indiidual, Max. Generation, Max. Fitness Calls, Pop. Mean, Max. Partition, Partition Mean]\n"; | |
}else { | |
writeToFile += "[Best Fitness, Best Indiidual, Max. Generation, Max. Fitness Calls, Pop. Mean]\n"; | |
} | |
writeToFile += Arrays.toString(bestIndiFt) + "\n"; | |
writeToFile += Arrays.toString(bestIndi) + "\n"; | |
writeToFile += Arrays.toString(maxGen) + "\n"; | |
writeToFile += Arrays.toString(maxFitCalls) + "\n"; | |
writeToFile += Arrays.toString(average) + "\n"; | |
if (flagPartition) { | |
writeToFile += Arrays.toString(maxPartiSize) + "\n"; | |
writeToFile += Arrays.toString(partitionAvg) + "\n"; | |
} | |
try{ | |
if(!log.exists()){ | |
log.getParentFile().mkdirs(); | |
log.createNewFile(); | |
} | |
try (PrintWriter out = new PrintWriter(new FileWriter(log, true))) { | |
out.append(writeToFile); | |
out.close(); | |
System.out.println("\nlog:-\tData saved to file." + pathName); | |
}catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println("Append file failed."); | |
} | |
}catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println("Create file failed."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment