Skip to content
Snippets Groups Projects
Commit 64371653 authored by Lennart Eichhorn's avatar Lennart Eichhorn
Browse files

Added visualizer for logging to file.

parent e04d9b06
No related branches found
No related tags found
No related merge requests found
...@@ -3,5 +3,6 @@ package Enums; ...@@ -3,5 +3,6 @@ package Enums;
public enum VisualizerMethods { public enum VisualizerMethods {
Console, Console,
Image, Image,
Video Video,
Log
} }
...@@ -173,6 +173,9 @@ public class Config { ...@@ -173,6 +173,9 @@ public class Config {
case "video": case "video":
visualizers[i] = VisualizerMethods.Video; visualizers[i] = VisualizerMethods.Video;
break; break;
case "log":
visualizers[i] = VisualizerMethods.Log;
break;
} }
} }
......
...@@ -14,6 +14,7 @@ import Selectors.Tournament; ...@@ -14,6 +14,7 @@ import Selectors.Tournament;
import Visualization.Visualizers.BestFoldingToConsole; import Visualization.Visualizers.BestFoldingToConsole;
import Visualization.Visualizers.BestFoldingToImage; import Visualization.Visualizers.BestFoldingToImage;
import Visualization.Visualizers.GenerationProgressToLog;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -50,7 +51,6 @@ public class GeneticAlgorithm { ...@@ -50,7 +51,6 @@ public class GeneticAlgorithm {
this.config = config; this.config = config;
this.initializeSettings(); this.initializeSettings();
this.clearLog();
this.population = this.initialGenCreator.initializeDirections(config.getPopulationSize(), this.isHydrophobic.length); this.population = this.initialGenCreator.initializeDirections(config.getPopulationSize(), this.isHydrophobic.length);
this.totalFitness = 0; this.totalFitness = 0;
...@@ -70,12 +70,12 @@ public class GeneticAlgorithm { ...@@ -70,12 +70,12 @@ public class GeneticAlgorithm {
// Settings that are dependant on encoding // Settings that are dependant on encoding
if (config.getEncodingVariant().equals("NESW")) { if (config.getEncodingVariant().equals("NESW")) {
int nullCount = 0; int nullCount = 0;
for (int i = 0; i < config.getVisualizers().length; i++) { /*for (int i = 0; i < config.getVisualizers().length; i++) {
if (!config.getVisualizers()[i].equals(VisualizerMethods.Console) if (!config.getVisualizers()[i].equals(VisualizerMethods.Console)
&& !config.getVisualizers()[i].equals(VisualizerMethods.Image)) { && !config.getVisualizers()[i].equals(VisualizerMethods.Image)) {
nullCount++; nullCount++;
} }
} }*/
this.visualizers = new Visualizer[config.getVisualizers().length - nullCount]; this.visualizers = new Visualizer[config.getVisualizers().length - nullCount];
int j = 0; int j = 0;
for (VisualizerMethods vm : config.getVisualizers()) { for (VisualizerMethods vm : config.getVisualizers()) {
...@@ -85,6 +85,9 @@ public class GeneticAlgorithm { ...@@ -85,6 +85,9 @@ public class GeneticAlgorithm {
} else if (vm.equals(VisualizerMethods.Image)) { } else if (vm.equals(VisualizerMethods.Image)) {
this.visualizers[j] = new BestFoldingToImage(isHydrophobic, config); this.visualizers[j] = new BestFoldingToImage(isHydrophobic, config);
j++; j++;
}else if (vm.equals(VisualizerMethods.Log)) {
this.visualizers[j] = new GenerationProgressToLog(isHydrophobic, config);
j++;
} }
} }
...@@ -123,19 +126,6 @@ public class GeneticAlgorithm { ...@@ -123,19 +126,6 @@ public class GeneticAlgorithm {
} }
} }
private void clearLog() {
String content = "Generation\tAverage Fitness\tBest Fitness\tOverall Best Fitness\tBonds\tOverlaps\n";
try {
//TODO This does not belong here
Files.createDirectories(Paths.get(config.getLogfileDirectory()));
String logfilePath = config.getLogfileDirectory() + "/" + config.getJobName() + ".txt";
Files.write(Paths.get(logfilePath), content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void simulateGenerations() { public void simulateGenerations() {
for (int gen = 0; gen < config.getTotalGenerations()-1; gen++) { for (int gen = 0; gen < config.getTotalGenerations()-1; gen++) {
//TODO Remove with the new Generation class //TODO Remove with the new Generation class
...@@ -186,19 +176,6 @@ public class GeneticAlgorithm { ...@@ -186,19 +176,6 @@ public class GeneticAlgorithm {
} }
double averageFitness = this.totalFitness / config.getPopulationSize(); double averageFitness = this.totalFitness / config.getPopulationSize();
String log = String.format("%d\t%.4f\t%.4f\t%.4f\t %d\t%d\n",
gen, averageFitness, bestFitness,
this.overallBest.getFitness(),
-1,
-1);
try {
String logfilePath = config.getLogfileDirectory() + "/" + config.getJobName() + ".txt";
Files.write(Paths.get(logfilePath), log.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
return bestIndex; return bestIndex;
} }
......
package Visualization.Visualizers;
import Evaluators.EvaluatorNESW;
import Interfaces.Visualizer;
import MainClasses.Candidate;
import MainClasses.Config;
import MainClasses.GeneticAlgorithm;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class GenerationProgressToLog implements Visualizer {
Path logfilePath;
final int[] isHydrophobic;
Config config;
//TODO Make sure this is initialized or prepare for bugs
Candidate overallBestCandidate;
EvaluatorNESW bondsOverlapsEvaluator;
public GenerationProgressToLog(int[] isHydrophobic, Config config) {
this.isHydrophobic = isHydrophobic;
this.config = config;
String filename = config.getLogfileDirectory() + "/" + config.getJobName() + ".txt";
logfilePath = Paths.get(filename);
bondsOverlapsEvaluator = new EvaluatorNESW(1,isHydrophobic);
//Initialize the logfile
initializeLogfile();
}
@Override
public void drawProtein(Candidate[] generation, GeneticAlgorithm geneticAlgorithm) {
//TODO This should be done in the new Generation class
double averageFitness = 0.0;
Candidate bestCandidateOfGeneration = generation[0];
for(Candidate candidate: generation){
averageFitness += candidate.getFitness();
if(candidate.getFitness() > bestCandidateOfGeneration.getFitness()){
bestCandidateOfGeneration = candidate;
}
}
averageFitness /= generation.length;
//Find overall best Candidate
if(geneticAlgorithm.generation == 0) {
overallBestCandidate = bestCandidateOfGeneration;
}else if(bestCandidateOfGeneration.getFitness() > overallBestCandidate.getFitness()){
overallBestCandidate = bestCandidateOfGeneration;
}
int bonds = bondsOverlapsEvaluator.evaluateBonds(bestCandidateOfGeneration);
int overlaps = bondsOverlapsEvaluator.evaluateBonds(bestCandidateOfGeneration);
String log = String.format("%d\t%.4f\t%.4f\t%.4f\t %d\t%d\n",
geneticAlgorithm.generation,
averageFitness,
bestCandidateOfGeneration.getFitness(),
overallBestCandidate.getFitness(),
bonds,
overlaps);
try {
String logfilePath = config.getLogfileDirectory() + "/" + config.getJobName() + ".txt";
Files.write(Paths.get(logfilePath), log.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
private void initializeLogfile() {
String content = "Generation\tAverage Fitness\tBest Fitness\tOverall Best Fitness\tBonds\tOverlaps\n";
try {
Files.createDirectories(Paths.get(config.getLogfileDirectory()));
Files.write(logfilePath, content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
### Output settings ### Output settings
# Type(s) of visualization to enable, separated with comma [console / image / video] # Type(s) of visualization to enable, separated with comma [console / image / video]
visualizerType = image,video visualizerType = log
# Name of the job, filenames will be based on this name. If left empty a timestamp is used # Name of the job, filenames will be based on this name. If left empty a timestamp is used
jobName = example1 jobName = example1
# Directory for the image sequence # Directory for the image sequence
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment