Skip to content
Snippets Groups Projects
Commit 427a85df authored by istkabra's avatar istkabra
Browse files

* Starting massive refactor

* InitialGenerationCreatorInterfaces are now working
  * Random
  * Curl
  * Straight
parent 5aa467b0
No related branches found
No related tags found
No related merge requests found
Showing
with 291 additions and 36 deletions
package Enums;
// TODO: Maybe actually replace these with a boolean and hard code...
public enum DirectionFRL {
Forward(0),
Right(1),
Left(2);
private final int value;
private DirectionFRL(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
package Enums;
// TODO: Maybe actually replace these with a boolean and hard code...
public enum DirectionNESW {
North(0),
East(1),
South(2),
West(3);
private final int value;
private DirectionNESW(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
package InitialGenerationCreators;
import Enums.DirectionFRL;
import Enums.DirectionNESW;
import Interfaces.InitialGenerationCreator;
import MainClasses.Candidate;
public class Curl<T extends Enum<?>> implements InitialGenerationCreator {
Class<T> possibleDirections;
public Curl(Class<T> possibleDirections) {
this.possibleDirections = possibleDirections;
}
@Override
public Candidate[] initializeDirections(int populationSize, int[] isHydrophobic) {
Candidate[] population = new Candidate[populationSize];
int proteinLength = isHydrophobic.length;
int[] candidateDirections;
if (isFRLEncoding(possibleDirections) && populationSize > 0) {
candidateDirections = curlFRL(proteinLength);
} else {
candidateDirections = curlNESW(proteinLength);
}
for (int i = 0; i < populationSize; i++) {
population[i] = new Candidate(isHydrophobic, candidateDirections);
}
return population;
}
private int[] curlNESW(int proteinLength) {
int[] candidateDirections = new int[proteinLength];
int stepSize = 1;
int stepsLeft = 1;
boolean stepSizeChange = false; // To increase stepSize every second time
DirectionNESW dir = DirectionNESW.North;
for (int j = 0; j < proteinLength; j++) {
candidateDirections[j] = dir.getValue();
stepsLeft--;
if (stepsLeft == 0) {
if (stepSizeChange) {
stepSize++;
stepsLeft = stepSize;
stepSizeChange = false;
} else {
stepsLeft = stepSize;
stepSizeChange = true;
}
// Rotate direction
if (dir == DirectionNESW.North) {
dir = DirectionNESW.East;
} else if (dir == DirectionNESW.East) {
dir = DirectionNESW.South;
} else if (dir == DirectionNESW.South) {
dir = DirectionNESW.West;
} else {
dir = DirectionNESW.North;
}
}
}
return candidateDirections;
}
private int[] curlFRL(int proteinLength) {
int[] candidateDirections = new int[proteinLength];
candidateDirections[0] = DirectionFRL.Forward.getValue();
int stepSize = 0;
int stepsLeft = 0;
boolean stepSizeChange = false; // To increase stepSize every second time
for (int j = 1; j < proteinLength; j++) {
if (stepsLeft == 0) { // Turn
candidateDirections[j] = DirectionFRL.Right.getValue();
if (stepSizeChange) {
stepSize++;
stepSizeChange = false;
} else {
stepSizeChange = true;
}
stepsLeft = stepSize;
} else { // Straight
candidateDirections[j] = DirectionFRL.Forward.getValue();
stepsLeft--;
}
}
return candidateDirections;
}
private <T extends Enum<?>> boolean isFRLEncoding(Class<T> possibleDirections) {
T[] possibleDirectionsEnum = possibleDirections.getEnumConstants();
for (int i = 0; i < possibleDirectionsEnum.length; i++) {
if (possibleDirectionsEnum[i].equals(DirectionFRL.Forward)) {
return true;
}
}
return false;
}
}
package InitialGenerationCreators;
import Enums.DirectionNESW;
import Interfaces.InitialGenerationCreator;
import MainClasses.Candidate;
import java.util.Random;
public class RandomDirection<T extends Enum<?>> implements InitialGenerationCreator {
Random rand;
Class<T> possibleDirections;
public RandomDirection(Random rand, Class<T> possibleDirections) {
this.rand = rand;
this.possibleDirections = possibleDirections;
}
@Override
public Candidate[] initializeDirections(int populationSize, int[] isHydrophobic) {
Candidate[] population = new Candidate[populationSize];
int proteinLength = isHydrophobic.length;
for (int i = 0; i < populationSize; i++) {
int[] candidateDirections = new int[proteinLength];
for (int j = 0; j < proteinLength; j++) {
candidateDirections[j] = this.randomDirection(this.possibleDirections);
}
population[i] = new Candidate(isHydrophobic, candidateDirections);
}
return population;
}
private int randomDirection(Class<T> dirEnum) {
return rand.nextInt(dirEnum.getEnumConstants().length);
}
}
package InitialGenerationCreators;
import Enums.DirectionFRL;
import Interfaces.InitialGenerationCreator;
import MainClasses.Candidate;
import java.util.Random;
public class StraightLine implements InitialGenerationCreator {
public StraightLine() {
}
@Override
public Candidate[] initializeDirections(int populationSize, int[] isHydrophobic) {
Candidate[] population = new Candidate[populationSize];
int proteinLength = isHydrophobic.length;
int[] candidateDirections = new int[proteinLength];
for (int j = 0; j < proteinLength; j++) {
candidateDirections[j] = 0; // Default starting direction is set by Enum
}
for (int i = 0; i < populationSize; i++) {
population[i] = new Candidate(isHydrophobic, candidateDirections);
}
return population;
}
}
package Interfaces;
public interface Evaluater {
}
package Interfaces;
import MainClasses.Candidate;
public interface InitialGenerationCreator {
Candidate[] initializeDirections(int populationSize, int[] isHydrophobic);
}
package Interfaces;
public interface Mutator {
}
package Interfaces;
public interface Selector {
}
package Interfaces;
public interface Visualizer {
}
package MainClasses;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
......
package MainClasses;
// benchmark sequences for the 2d HP model // benchmark sequences for the 2d HP model
// 0 = hydrophil, "white" // 0 = hydrophil, "white"
// 1 = hydrophob, "black" // 1 = hydrophob, "black"
......
package MainClasses;
import Enums.DirectionNESW;
import InitialGenerationCreators.Curl;
import InitialGenerationCreators.RandomDirection;
import InitialGenerationCreators.StraightLine;
import Interfaces.InitialGenerationCreator;
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;
...@@ -35,11 +43,17 @@ public class GeneticAlgorithm { ...@@ -35,11 +43,17 @@ public class GeneticAlgorithm {
tournament tournament
} }
InitialGenerationCreator initialGenCreator;
Selection selectionVariant; Selection selectionVariant;
int k; // Number of selected Candidates to face off in a tournament selection int k; // Number of selected Candidates to face off in a tournament selection
// Initialize with protein // Initialize with protein
public GeneticAlgorithm (Properties properties, int[] protein) { public GeneticAlgorithm (Properties properties, int[] protein) {
// this.initialGenCreator = new RandomDirection(this.rand, DirectionNESW.class);
// this.initialGenCreator = new StraightLine();
this.initialGenCreator = new Curl(DirectionNESW.class);
this.properties = properties; this.properties = properties;
this.isHydrophobic = protein; this.isHydrophobic = protein;
initializeProperties(); initializeProperties();
...@@ -53,7 +67,7 @@ public class GeneticAlgorithm { ...@@ -53,7 +67,7 @@ public class GeneticAlgorithm {
e.printStackTrace(); e.printStackTrace();
} }
this.population = generateInitalPopulation(); this.population = this.initialGenCreator.initializeDirections(this.populationSize, this.isHydrophobic);
this.totalFitness = 0; this.totalFitness = 0;
this.fitness = new double[populationSize]; this.fitness = new double[populationSize];
this.overallBestFitness = 0; this.overallBestFitness = 0;
...@@ -87,21 +101,6 @@ public class GeneticAlgorithm { ...@@ -87,21 +101,6 @@ public class GeneticAlgorithm {
this.pdraw = new ProteinDrawer(properties.getProperty("imageSequencePath")); this.pdraw = new ProteinDrawer(properties.getProperty("imageSequencePath"));
} }
// Generate initial population
private Candidate[] generateInitalPopulation() {
Candidate[] population = new Candidate[populationSize];
for (int i = 0; i < populationSize; i++) {
int[] canidateDirections = new int[isHydrophobic.length];
for (int j = 0; j < isHydrophobic.length; j++) {
canidateDirections[j] = rand.nextInt(4);
}
population[i] = new Candidate(isHydrophobic, canidateDirections);
}
return population;
}
public void simulateGenerations() { public void simulateGenerations() {
for (int gen = 0; gen < totalGenerations-1; gen++) { for (int gen = 0; gen < totalGenerations-1; gen++) {
evaluateGeneration(gen); evaluateGeneration(gen);
...@@ -140,7 +139,7 @@ public class GeneticAlgorithm { ...@@ -140,7 +139,7 @@ public class GeneticAlgorithm {
pdraw.drawProteinToFile(population[bestIndex].getVertexList(), population[bestIndex].calculateFitness(true), gen); pdraw.drawProteinToFile(population[bestIndex].getVertexList(), population[bestIndex].calculateFitness(true), gen);
// Save the overall best // Save the overall best
if (bestFitness > overallBestFitness) { if (bestFitness >= overallBestFitness) {
overallBestFitness = bestFitness; overallBestFitness = bestFitness;
overallBest = new Candidate(this.isHydrophobic, population[bestIndex].getOutgoing()); overallBest = new Candidate(this.isHydrophobic, population[bestIndex].getOutgoing());
} }
......
/* package MainClasses;/*
* @(#)JpegImagesToMovie.java 1.3 01/03/13 * @(#)MainClasses.JpegImagesToMovie.java 1.3 01/03/13
* Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form, * modify and redistribute this software in source and binary code form,
...@@ -324,7 +324,7 @@ public class JpegImagesToMovie implements ControllerListener, DataSinkListener { ...@@ -324,7 +324,7 @@ public class JpegImagesToMovie implements ControllerListener, DataSinkListener {
System.exit(0); System.exit(0);
} }
JpegImagesToMovie imageToMovie = new JpegImagesToMovie(); MainClasses.JpegImagesToMovie imageToMovie = new MainClasses.JpegImagesToMovie();
imageToMovie.doIt(width, height, frameRate, inputFiles, oml); imageToMovie.doIt(width, height, frameRate, inputFiles, oml);
System.exit(0); System.exit(0);
...@@ -332,7 +332,7 @@ public class JpegImagesToMovie implements ControllerListener, DataSinkListener { ...@@ -332,7 +332,7 @@ public class JpegImagesToMovie implements ControllerListener, DataSinkListener {
static void prUsage() { static void prUsage() {
System.err System.err
.println("Usage: java JpegImagesToMovie -w <width> -h <height> -f <frame rate> -o <output URL> <input JPEG file 1> <input JPEG file 2> ..."); .println("Usage: java MainClasses.JpegImagesToMovie -w <width> -h <height> -f <frame rate> -o <output URL> <input JPEG file 1> <input JPEG file 2> ...");
System.exit(-1); System.exit(-1);
} }
......
package MainClasses;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Random;
public class Main { public class Main {
...@@ -30,7 +28,7 @@ public class Main { ...@@ -30,7 +28,7 @@ public class Main {
e.printStackTrace(); e.printStackTrace();
} }
// ProteinDrawer pdraw = new ProteinDrawer("./visualization/individual/", "image.jpg"); // MainClasses.ProteinDrawer pdraw = new MainClasses.ProteinDrawer("./visualization/individual/", "image.jpg");
// //
// //
// int[] isHydrophobic = {0, 0, 0, 1, 0, 1, 1, 0, 0}; // 0 = no | 1 = yes // int[] isHydrophobic = {0, 0, 0, 1, 0, 1, 1, 0, 0}; // 0 = no | 1 = yes
......
package MainClasses;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
......
package MainClasses;
// Helper class representing a single amino acid // Helper class representing a single amino acid
class Vertex { class Vertex {
int x; int x;
......
package MainClasses;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
...@@ -12,6 +13,7 @@ import javax.media.MediaLocator; ...@@ -12,6 +13,7 @@ import javax.media.MediaLocator;
public class VideoCreator{ public class VideoCreator{
private static final int MAXIMUM_SIZE = 3000;
public static File dir = new File("./visualization/series"); // Default public static File dir = new File("./visualization/series"); // Default
public static final String[] extensions = new String[]{"jpg", "png"}; public static final String[] extensions = new String[]{"jpg", "png"};
public static final FilenameFilter imageFilter = new FilenameFilter() { public static final FilenameFilter imageFilter = new FilenameFilter() {
...@@ -32,8 +34,8 @@ public class VideoCreator{ ...@@ -32,8 +34,8 @@ public class VideoCreator{
if (dir.isDirectory()) { if (dir.isDirectory()) {
// reads input images and determines maximum required size // reads input images and determines maximum required size
int maxHeight = ProteinDrawer.maxWidth; int maxHeight = ProteinDrawer.maxHeight;
int maxWidth = ProteinDrawer.maxHeight; int maxWidth = ProteinDrawer.maxWidth;
if (maxHeight <= 0 || maxWidth <= 0) { if (maxHeight <= 0 || maxWidth <= 0) {
int counter = 1; int counter = 1;
...@@ -48,6 +50,14 @@ public class VideoCreator{ ...@@ -48,6 +50,14 @@ public class VideoCreator{
} }
} }
// Needed because video is not playable with bigger sizes, mostly a concern for straight line initialization
if (maxWidth > MAXIMUM_SIZE) {
maxWidth = MAXIMUM_SIZE;
}
if (maxHeight > MAXIMUM_SIZE) {
maxHeight = MAXIMUM_SIZE;
}
widthHeight[0] = maxWidth; widthHeight[0] = maxWidth;
widthHeight[1] = maxHeight; widthHeight[1] = maxHeight;
...@@ -74,7 +84,7 @@ public class VideoCreator{ ...@@ -74,7 +84,7 @@ public class VideoCreator{
return widthHeight; return widthHeight;
} }
// Main function // MainClasses.Main function
public static void createVideo(Properties properties) throws IOException { public static void createVideo(Properties properties) throws IOException {
String imagesPath = properties.getProperty("imageSequencePath"); String imagesPath = properties.getProperty("imageSequencePath");
String videoPathAndFile = properties.getProperty("videoPathAndFile"); String videoPathAndFile = properties.getProperty("videoPathAndFile");
......
logfilePath = log.txt logfilePath = log.txt
populationSize = 300 populationSize = 10
# Do NOT go OVER 1_000_000 generations, as the filenames don't play nice then # Do NOT go OVER 1_000_000 generations, as the filenames don't play nice then
noGenerations = 1000 noGenerations = 10
# proportional OR tournament # proportional OR tournament
selection = proportional selection = proportional
# Number of tournament participants # Number of tournament participants
...@@ -12,11 +12,16 @@ mutationChance = 1.0 ...@@ -12,11 +12,16 @@ mutationChance = 1.0
# Decline in mutation probability with generations -> ex with 0.05: 2nd 0.95, 3rd 0.9025, 4th 0.857 # Decline in mutation probability with generations -> ex with 0.05: 2nd 0.95, 3rd 0.9025, 4th 0.857
mutationDecline = 0.001 mutationDecline = 0.001
mutationAttemptsPerCanidate = 1 mutationAttemptsPerCanidate = 1
crossoverChance = 0.2 crossoverChance = 0.6
crossoverDecline = 0.01 crossoverDecline = 0.005
# For video # For video
imageSequencePath = ./visualization/series imageSequencePath = ./visualization/series
videoPathAndFile = ./visualization/video.mp4 videoPathAndFile = ./visualization/video.mp4
# ms between images # ms between images
imgInterval = 100 imgInterval = 100
\ No newline at end of file
# TODO:
createVideo = true
initializationMethod = curl, straight, random
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment