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

Added option to generate images every x generations

parent c7310342
No related branches found
No related tags found
No related merge requests found
...@@ -47,6 +47,7 @@ public class Config { ...@@ -47,6 +47,7 @@ public class Config {
private int imageFps; private int imageFps;
private int imagesToFpsIncrease; private int imagesToFpsIncrease;
private int imageFpsMax; private int imageFpsMax;
private int imageInterval;
private boolean zoom; private boolean zoom;
// For images // For images
...@@ -188,6 +189,7 @@ public class Config { ...@@ -188,6 +189,7 @@ public class Config {
imageFps = Integer.parseInt(this.properties.getProperty("imgFps")); imageFps = Integer.parseInt(this.properties.getProperty("imgFps"));
imagesToFpsIncrease = Integer.parseInt(this.properties.getProperty("imagesToFpsIncrease")); imagesToFpsIncrease = Integer.parseInt(this.properties.getProperty("imagesToFpsIncrease"));
imageFpsMax = Integer.parseInt(this.properties.getProperty("imgFpsMax")); imageFpsMax = Integer.parseInt(this.properties.getProperty("imgFpsMax"));
imageInterval = Integer.parseInt(this.properties.getProperty("imageInterval"));
zoom = this.properties.getProperty("zoom").equals("true"); zoom = this.properties.getProperty("zoom").equals("true");
} }
...@@ -295,6 +297,10 @@ public class Config { ...@@ -295,6 +297,10 @@ public class Config {
return imageFpsMax; return imageFpsMax;
} }
public int getImageInterval() {
return imageInterval;
}
public boolean isZoom() { public boolean isZoom() {
return zoom; return zoom;
} }
......
...@@ -143,6 +143,7 @@ public class GeneticAlgorithm { ...@@ -143,6 +143,7 @@ public class GeneticAlgorithm {
// mutateGeneration(Mutator e); // mutateGeneration(Mutator e);
private void evaluateGeneration() { private void evaluateGeneration() {
for (int i = 0; i < population.length; i++) { for (int i = 0; i < population.length; i++) {
this.population[i] = this.evaluator.evaluateFitness(this.population[i]); this.population[i] = this.evaluator.evaluateFitness(this.population[i]);
} }
} }
......
...@@ -40,123 +40,133 @@ public class BestFoldingToImage implements Visualizer { ...@@ -40,123 +40,133 @@ public class BestFoldingToImage implements Visualizer {
} }
public void drawProtein(Candidate[] generation, GeneticAlgorithm geneticAlgorithm) { public void drawProtein(Candidate[] generation, GeneticAlgorithm geneticAlgorithm) {
String filename = config.getImageSequenceDirectory() + "/" + config.getJobName() + "_" + geneticAlgorithm.generation + ".png"; if(geneticAlgorithm.generation% config.getImageInterval() == 0 || geneticAlgorithm.generation == config.getTotalGenerations()) {
String filename = config.getImageSequenceDirectory() + "/" + config.getJobName() + "_"
//TODO This should probably be in the new Generation Class + geneticAlgorithm.generation + ".png";
Candidate bestCandidateOfGeneration = generation[0]; //TODO This should probably be in the new Generation Class
for (Candidate evaluatedCandidate : generation) { Candidate bestCandidateOfGeneration = generation[0];
if(bestCandidateOfGeneration.getFitness() < evaluatedCandidate.getFitness()){ for (Candidate evaluatedCandidate : generation) {
bestCandidateOfGeneration=evaluatedCandidate; if (bestCandidateOfGeneration.getFitness() < evaluatedCandidate.getFitness()) {
bestCandidateOfGeneration = evaluatedCandidate;
}
} }
}
ArrayList<Vertex> vertexList = bestCandidateOfGeneration.getVertices(); ArrayList<Vertex> vertexList = bestCandidateOfGeneration.getVertices();
Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList, this.isHydrophobic); Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList, this.isHydrophobic);
int height = (cellArray.length * pixelsPerCell) + margin * 2; int height = (cellArray.length * pixelsPerCell) + margin * 2;
int width = (cellArray[0].length * pixelsPerCell) + margin * 2; int width = (cellArray[0].length * pixelsPerCell) + margin * 2;
if (height > maxHeight) { if (height > maxHeight) {
maxHeight = height; maxHeight = height;
} }
if (width > maxWidth) { if (width > maxWidth) {
maxWidth = width; maxWidth = width;
} }
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics(); Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
g2.setFont(config.getFont()); RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
FontMetrics metrics = g2.getFontMetrics(); g2.setFont(config.getFont());
int ascent = metrics.getAscent(); FontMetrics metrics = g2.getFontMetrics();
int ascent = metrics.getAscent();
// Background // Background
g2.setColor(config.getImageBackground()); g2.setColor(config.getImageBackground());
g2.fillRect(0, 0, width, height); g2.fillRect(0, 0, width, height);
for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) { for (int yIndex = cellArray.length - 1; yIndex >= 0; yIndex--) {
int yIndexPosition = cellArray.length-1 - yIndex; int yIndexPosition = cellArray.length - 1 - yIndex;
for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) { for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) {
Color aminoColor = null; Color aminoColor = null;
State cellState = cellArray[yIndex][xIndex].getRelevantDrawState(); State cellState = cellArray[yIndex][xIndex].getRelevantDrawState();
switch (cellState) { switch (cellState) {
case Empty: case Empty:
break; break;
case Hydrophobic: case Hydrophobic:
case HydrophobicMulti: case HydrophobicMulti:
aminoColor = config.getImageHydrophobic(); aminoColor = config.getImageHydrophobic();
break; break;
case Hydrophilic: case Hydrophilic:
case HydrophilicMulti: case HydrophilicMulti:
aminoColor = config.getImageHydrophilic(); aminoColor = config.getImageHydrophilic();
break; break;
case Mixed: case Mixed:
aminoColor = config.getImageMixed(); aminoColor = config.getImageMixed();
break; break;
case ConnectionVertical: case ConnectionVertical:
g2.setColor(config.getImageConnection()); g2.setColor(config.getImageConnection());
g2.fillRect((xIndex * pixelsPerCell) + margin + (pixelsPerCell/2)-outline, g2.fillRect(
(xIndex * pixelsPerCell) + margin + (pixelsPerCell / 2) - outline,
(yIndexPosition * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin,
outline * 2, pixelsPerCell); outline * 2, pixelsPerCell);
break; break;
case ConnectionHorizontal: case ConnectionHorizontal:
g2.setColor(config.getImageConnection()); g2.setColor(config.getImageConnection());
g2.fillRect((xIndex * pixelsPerCell) + margin, g2.fillRect((xIndex * pixelsPerCell) + margin,
(yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2)-outline, (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell / 2)
- outline,
pixelsPerCell, outline * 2); pixelsPerCell, outline * 2);
break; break;
} }
if (aminoColor != null) { if (aminoColor != null) {
g2.setColor(config.getImageOutline()); g2.setColor(config.getImageOutline());
g2.fillRect((xIndex * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin, g2.fillRect((xIndex * pixelsPerCell) + margin,
(yIndexPosition * pixelsPerCell) + margin,
pixelsPerCell, pixelsPerCell); pixelsPerCell, pixelsPerCell);
g2.setColor(aminoColor); g2.setColor(aminoColor);
g2.fillRect((xIndex * pixelsPerCell) + outline + margin, (yIndexPosition * pixelsPerCell) + outline + margin, g2.fillRect((xIndex * pixelsPerCell) + outline + margin,
(yIndexPosition * pixelsPerCell) + outline + margin,
pixelsPerCell - (outline * 2), pixelsPerCell - (outline * 2)); pixelsPerCell - (outline * 2), pixelsPerCell - (outline * 2));
g2.setColor(config.getImageAminoText()); g2.setColor(config.getImageAminoText());
String label = ""; String label = "";
for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) { for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) {
label += aminoIndex + " "; label += aminoIndex + " ";
}
int labelWidth = metrics.stringWidth(label);
int x =
(xIndex * pixelsPerCell) + margin + (pixelsPerCell / 2) - (labelWidth
/ 2) + outline;
int y = (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell / 2) + (
ascent / 2) - outline;
g2.drawString(label, x, y);
} }
int labelWidth = metrics.stringWidth(label);
int x = (xIndex * pixelsPerCell) + margin + (pixelsPerCell/2) - (labelWidth/2) + outline;
int y = (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2) + (ascent/2) - outline;
g2.drawString(label, x, y);
} }
} }
}
g2.setColor(config.getImageText()); g2.setColor(config.getImageText());
//TODO Get the labels from the new Generation class? //TODO Get the labels from the new Generation class?
EvaluatorNESW evaluator = new EvaluatorNESW(1,isHydrophobic); EvaluatorNESW evaluator = new EvaluatorNESW(1, isHydrophobic);
int bonds = evaluator.evaluateBonds(bestCandidateOfGeneration); int bonds = evaluator.evaluateBonds(bestCandidateOfGeneration);
int overlaps = evaluator.evaluateOverlaps(bestCandidateOfGeneration); int overlaps = evaluator.evaluateOverlaps(bestCandidateOfGeneration);
String label = "Gen: " + geneticAlgorithm.generation String label = "Gen: " + geneticAlgorithm.generation
+ " Fitness: " + String.format("%.4f", bestCandidateOfGeneration.getFitness()) + " Fitness: " + String.format("%.4f", bestCandidateOfGeneration.getFitness())
+ " H/H Bonds: " + bonds + " H/H Bonds: " + bonds
+ " Overlaps: " + overlaps; + " Overlaps: " + overlaps;
int labelWidth = metrics.stringWidth(label); int labelWidth = metrics.stringWidth(label);
int x = margin / 4; int x = margin / 4;
int y = margin / 4; int y = margin / 4;
g2.drawString(label, x, y); g2.drawString(label, x, y);
try { try {
//ImageIO.write(image, "png", new File(folder + File.separator + filename)); //ImageIO.write(image, "png", new File(folder + File.separator + filename));
ImageIO.write(image, "png", new File(filename)); ImageIO.write(image, "png", new File(filename));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(0); System.exit(0);
}
} }
} }
......
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
### Algorithm settings ### Algorithm settings
# Size of the population in one generation # Size of the population in one generation
populationSize = 100 populationSize = 1000
# Number of generations to run the algorithm for # Number of generations to run the algorithm for
noGenerations = 20 noGenerations = 10000
# Type of population initialization [curl | straight | random] # Type of population initialization [curl | straight | random]
initializationMethod = random initializationMethod = random
# Type of selection that should be used [proportional | tournament | onlybest] # Type of selection that should be used [proportional | tournament | onlybest]
selectionMethod = proportional selectionMethod = tournament
# Number of tournament participants, only relevant when selection is set to tournament # Number of tournament participants, only relevant when selection is set to tournament
k = 5 k = 5
# Type(s) of mutators to use in the algorithm, separated by comma [singlePoint / crossover / singlePointGlobal] # Type(s) of mutators to use in the algorithm, separated by comma [singlePoint / crossover / singlePointGlobal]
...@@ -24,21 +24,21 @@ ...@@ -24,21 +24,21 @@
### Mutation settings ### Mutation settings
# How often a single point mutation should be attempted per candidate # How often a single point mutation should be attempted per candidate
mutationAttemptsPerCandidate = 1 mutationAttemptsPerCandidate = 100
# Chance for a successful single point mutation [1.0 = 100%] # Chance for a successful single point mutation [1.0 = 100%]
mutationChance = 1.0 mutationChance = 0.011
# The mutation chance cannot drop below this value [1.0 = 100%] # The mutation chance cannot drop below this value [1.0 = 100%]
mutationMinimalChance = 0.001 mutationMinimalChance = 0.01
# Multiplicand for mutation probability with each generation -> ex with 0.05: 1st 1.0, 2nd 0.95, 3rd 0.9025, 4th 0.857 # Multiplicand for mutation probability with each generation -> ex with 0.05: 1st 1.0, 2nd 0.95, 3rd 0.9025, 4th 0.857
mutationMultiplier = 0.001 mutationMultiplier = 0.0
# How often a crossover should be attempted per candidate # How often a crossover should be attempted per candidate
crossoverAttemptsPerCandidate = 1 crossoverAttemptsPerCandidate = 1
# Chance for a successful crossover [1.0 = 100%] # Chance for a successful crossover [1.0 = 100%]
crossoverChance = 0.6 crossoverChance = 0.051
# The crossover chance cannot drop below this value [1.0 = 100%] # The crossover chance cannot drop below this value [1.0 = 100%]
crossoverMinimalChance = 0.001 crossoverMinimalChance = 0.05
# Multiplicand for mutation probability with each generation -> ex with -0.05: 1st 1.0, 2nd 1.05, 3rd 1,1025, 4th 1,157 # Multiplicand for mutation probability with each generation -> ex with -0.05: 1st 1.0, 2nd 1.05, 3rd 1,1025, 4th 1,157
crossoverMultiplier = 0.005 crossoverMultiplier = 0.0
### Output settings ### Output settings
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
# video = Generate a video from the created images. Requires image # video = Generate a video from the created images. Requires image
# log = Generate tab seperated file with a bit of information about each generation # log = Generate tab seperated file with a bit of information about each generation
# generation = print a short overview about each generation to stdout # generation = print a short overview about each generation to stdout
visualizerType = log,generation,image,video visualizerType = generation,image
# 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 = jobName =
# Directory for the image sequence # Directory for the image sequence
...@@ -57,6 +57,8 @@ ...@@ -57,6 +57,8 @@
videoDirectory = visualization videoDirectory = visualization
# Directory for the log file, always active # Directory for the log file, always active
logfileDirectory = log logfileDirectory = log
# Interval of generations between rendered images
imageInterval = 100
# Frames per second in the beginning of the video # Frames per second in the beginning of the video
imgFps = 10 imgFps = 10
# Number of images until fps increases by one # Number of images until fps increases by one
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment