diff --git a/src/main/java/MainClasses/Config.java b/src/main/java/MainClasses/Config.java index 96f17dcfd9b8cb84480528d7c0ed6c403ae6e5cc..b5e374ab6610e75970212f39ae44e2fb0e6d84b5 100644 --- a/src/main/java/MainClasses/Config.java +++ b/src/main/java/MainClasses/Config.java @@ -47,6 +47,7 @@ public class Config { private int imageFps; private int imagesToFpsIncrease; private int imageFpsMax; + private int imageInterval; private boolean zoom; // For images @@ -188,6 +189,7 @@ public class Config { imageFps = Integer.parseInt(this.properties.getProperty("imgFps")); imagesToFpsIncrease = Integer.parseInt(this.properties.getProperty("imagesToFpsIncrease")); imageFpsMax = Integer.parseInt(this.properties.getProperty("imgFpsMax")); + imageInterval = Integer.parseInt(this.properties.getProperty("imageInterval")); zoom = this.properties.getProperty("zoom").equals("true"); } @@ -295,6 +297,10 @@ public class Config { return imageFpsMax; } + public int getImageInterval() { + return imageInterval; + } + public boolean isZoom() { return zoom; } diff --git a/src/main/java/MainClasses/GeneticAlgorithm.java b/src/main/java/MainClasses/GeneticAlgorithm.java index 45ec810daf0c9ccfdd2fb2475e56e3a8bf725d72..5324cd8779ac6488f6254f541088269274c97fda 100644 --- a/src/main/java/MainClasses/GeneticAlgorithm.java +++ b/src/main/java/MainClasses/GeneticAlgorithm.java @@ -143,6 +143,7 @@ public class GeneticAlgorithm { // mutateGeneration(Mutator e); private void evaluateGeneration() { for (int i = 0; i < population.length; i++) { + this.population[i] = this.evaluator.evaluateFitness(this.population[i]); } } diff --git a/src/main/java/Visualizers/BestFoldingToImage.java b/src/main/java/Visualizers/BestFoldingToImage.java index 1481b874b611fd36212303bb76ec4e031fe878f9..a271ce96d3330aaa5b56f3148271ee1d5e16b9a9 100644 --- a/src/main/java/Visualizers/BestFoldingToImage.java +++ b/src/main/java/Visualizers/BestFoldingToImage.java @@ -40,123 +40,133 @@ public class BestFoldingToImage implements Visualizer { } public void drawProtein(Candidate[] generation, GeneticAlgorithm geneticAlgorithm) { - String filename = config.getImageSequenceDirectory() + "/" + config.getJobName() + "_" + geneticAlgorithm.generation + ".png"; - - //TODO This should probably be in the new Generation Class - Candidate bestCandidateOfGeneration = generation[0]; - for (Candidate evaluatedCandidate : generation) { - if(bestCandidateOfGeneration.getFitness() < evaluatedCandidate.getFitness()){ - bestCandidateOfGeneration=evaluatedCandidate; + if(geneticAlgorithm.generation% config.getImageInterval() == 0 || geneticAlgorithm.generation == config.getTotalGenerations()) { + String filename = config.getImageSequenceDirectory() + "/" + config.getJobName() + "_" + + geneticAlgorithm.generation + ".png"; + //TODO This should probably be in the new Generation Class + Candidate bestCandidateOfGeneration = generation[0]; + for (Candidate evaluatedCandidate : generation) { + 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 width = (cellArray[0].length * pixelsPerCell) + margin * 2; + int height = (cellArray.length * pixelsPerCell) + margin * 2; + int width = (cellArray[0].length * pixelsPerCell) + margin * 2; - if (height > maxHeight) { - maxHeight = height; - } - if (width > maxWidth) { - maxWidth = width; - } + if (height > maxHeight) { + maxHeight = height; + } + if (width > maxWidth) { + maxWidth = width; + } - BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - Graphics2D g2 = image.createGraphics(); - g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - g2.setFont(config.getFont()); - FontMetrics metrics = g2.getFontMetrics(); - int ascent = metrics.getAscent(); + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D g2 = image.createGraphics(); + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + g2.setFont(config.getFont()); + FontMetrics metrics = g2.getFontMetrics(); + int ascent = metrics.getAscent(); - // Background - g2.setColor(config.getImageBackground()); - g2.fillRect(0, 0, width, height); + // Background + g2.setColor(config.getImageBackground()); + g2.fillRect(0, 0, width, height); - for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) { - int yIndexPosition = cellArray.length-1 - yIndex; + for (int yIndex = cellArray.length - 1; yIndex >= 0; yIndex--) { + int yIndexPosition = cellArray.length - 1 - yIndex; - for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) { - Color aminoColor = null; + for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) { + Color aminoColor = null; - State cellState = cellArray[yIndex][xIndex].getRelevantDrawState(); + State cellState = cellArray[yIndex][xIndex].getRelevantDrawState(); - switch (cellState) { - case Empty: - break; + switch (cellState) { + case Empty: + break; - case Hydrophobic: - case HydrophobicMulti: - aminoColor = config.getImageHydrophobic(); - break; + case Hydrophobic: + case HydrophobicMulti: + aminoColor = config.getImageHydrophobic(); + break; - case Hydrophilic: - case HydrophilicMulti: - aminoColor = config.getImageHydrophilic(); - break; + case Hydrophilic: + case HydrophilicMulti: + aminoColor = config.getImageHydrophilic(); + break; - case Mixed: - aminoColor = config.getImageMixed(); - break; + case Mixed: + aminoColor = config.getImageMixed(); + break; - case ConnectionVertical: - g2.setColor(config.getImageConnection()); - g2.fillRect((xIndex * pixelsPerCell) + margin + (pixelsPerCell/2)-outline, + case ConnectionVertical: + g2.setColor(config.getImageConnection()); + g2.fillRect( + (xIndex * pixelsPerCell) + margin + (pixelsPerCell / 2) - outline, (yIndexPosition * pixelsPerCell) + margin, outline * 2, pixelsPerCell); - break; + break; - case ConnectionHorizontal: - g2.setColor(config.getImageConnection()); - g2.fillRect((xIndex * pixelsPerCell) + margin, - (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2)-outline, + case ConnectionHorizontal: + g2.setColor(config.getImageConnection()); + g2.fillRect((xIndex * pixelsPerCell) + margin, + (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell / 2) + - outline, pixelsPerCell, outline * 2); - break; - } + break; + } - if (aminoColor != null) { - g2.setColor(config.getImageOutline()); - g2.fillRect((xIndex * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin, + if (aminoColor != null) { + g2.setColor(config.getImageOutline()); + g2.fillRect((xIndex * pixelsPerCell) + margin, + (yIndexPosition * pixelsPerCell) + margin, pixelsPerCell, pixelsPerCell); - g2.setColor(aminoColor); - g2.fillRect((xIndex * pixelsPerCell) + outline + margin, (yIndexPosition * pixelsPerCell) + outline + margin, + g2.setColor(aminoColor); + g2.fillRect((xIndex * pixelsPerCell) + outline + margin, + (yIndexPosition * pixelsPerCell) + outline + margin, pixelsPerCell - (outline * 2), pixelsPerCell - (outline * 2)); - g2.setColor(config.getImageAminoText()); - String label = ""; - for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) { - label += aminoIndex + " "; + g2.setColor(config.getImageAminoText()); + String label = ""; + for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) { + 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()); - //TODO Get the labels from the new Generation class? - EvaluatorNESW evaluator = new EvaluatorNESW(1,isHydrophobic); - int bonds = evaluator.evaluateBonds(bestCandidateOfGeneration); - int overlaps = evaluator.evaluateOverlaps(bestCandidateOfGeneration); - String label = "Gen: " + geneticAlgorithm.generation + g2.setColor(config.getImageText()); + //TODO Get the labels from the new Generation class? + EvaluatorNESW evaluator = new EvaluatorNESW(1, isHydrophobic); + int bonds = evaluator.evaluateBonds(bestCandidateOfGeneration); + int overlaps = evaluator.evaluateOverlaps(bestCandidateOfGeneration); + String label = "Gen: " + geneticAlgorithm.generation + " Fitness: " + String.format("%.4f", bestCandidateOfGeneration.getFitness()) + " H/H Bonds: " + bonds + " Overlaps: " + overlaps; - int labelWidth = metrics.stringWidth(label); - int x = margin / 4; - int y = margin / 4; - g2.drawString(label, x, y); - - try { - //ImageIO.write(image, "png", new File(folder + File.separator + filename)); - ImageIO.write(image, "png", new File(filename)); - } catch (IOException e) { - e.printStackTrace(); - System.exit(0); + int labelWidth = metrics.stringWidth(label); + int x = margin / 4; + int y = margin / 4; + g2.drawString(label, x, y); + + try { + //ImageIO.write(image, "png", new File(folder + File.separator + filename)); + ImageIO.write(image, "png", new File(filename)); + } catch (IOException e) { + e.printStackTrace(); + System.exit(0); + } } } diff --git a/src/main/resources/genetic.properties b/src/main/resources/genetic.properties index f5cc49353d68b8e68465837d2ff355f028e4d035..c92d9ca5add4438ec651bbc2b93cad69f60f750f 100644 --- a/src/main/resources/genetic.properties +++ b/src/main/resources/genetic.properties @@ -7,13 +7,13 @@ ### Algorithm settings # Size of the population in one generation - populationSize = 100 + populationSize = 1000 # Number of generations to run the algorithm for - noGenerations = 20 + noGenerations = 10000 # Type of population initialization [curl | straight | random] initializationMethod = random # 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 k = 5 # Type(s) of mutators to use in the algorithm, separated by comma [singlePoint / crossover / singlePointGlobal] @@ -24,21 +24,21 @@ ### Mutation settings # 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%] - mutationChance = 1.0 + mutationChance = 0.011 # 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 - mutationMultiplier = 0.001 + mutationMultiplier = 0.0 # How often a crossover should be attempted per candidate crossoverAttemptsPerCandidate = 1 # 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%] - 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 - crossoverMultiplier = 0.005 + crossoverMultiplier = 0.0 ### Output settings @@ -48,7 +48,7 @@ # video = Generate a video from the created images. Requires image # log = Generate tab seperated file with a bit of information about each generation # 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 jobName = # Directory for the image sequence @@ -57,6 +57,8 @@ videoDirectory = visualization # Directory for the log file, always active logfileDirectory = log + # Interval of generations between rendered images + imageInterval = 100 # Frames per second in the beginning of the video imgFps = 10 # Number of images until fps increases by one