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

CHanged static Config to object.

parent 9b225108
No related branches found
No related tags found
No related merge requests found
...@@ -13,59 +13,59 @@ import java.util.Properties; ...@@ -13,59 +13,59 @@ import java.util.Properties;
public class Config { public class Config {
String propertyPath; private String propertyPath;
Properties properties; private Properties properties;
static String ENCODING_VARIANT; private String encodingVariant;
static int SEED; private int seed;
static int POPULATION_SIZE; private int populationSize;
static int TOTAL_GENERATIONS; private int totalGenerations;
static InitializationMethods INITIALIZATION_METHOD; private InitializationMethods initializationMethod;
static SelectionMethods SELECTION_METHOD; private SelectionMethods selectionMethod;
static int K; // Number of selected Candidates to face off in a tournament selection private int k; // Number of selected Candidates to face off in a tournament selection
static MutatorMethods[] MUTATOR_METHODS; private MutatorMethods[] mutatorMethods;
static int POINTS_PER_BOND; // Points per hydrophobic bond, default Evaluator will work the same with any value private int pointsPerBond; // Points per hydrophobic bond, default Evaluator will work the same with any value
static int MUTATION_ATTEMPTS_PER_CANDIDATE; private int mutationAttemptsPerCandidate;
static double MUTATION_CHANCE; private double mutationChance;
static double MUTATION_MULTIPLIER; private double mutationMultiplier;
static double MUTATION_MINIMAL_CHANCE; // -> 0.01% is not worth mutating for private double mutationMinimalChance; // -> 0.01% is not worth mutating for
static int CROSSOVER_ATTEMPTS_PER_CANDIDATE; private int crossoverAttemptsPerCandidate;
static double CROSSOVER_CHANCE; private double crossoverChance;
static double CROSSOVER_MINIMAL_CHANCE; // -> 0.01% is not worth mutating for private double crossoverMinimalChance; // -> 0.01% is not worth mutating for
static double CROSSOVER_MULTIPLIER; private double crossoverMultiplier;
static String LOGFILE; private String logfile;
static VisualizerMethods[] VISUALIZERS; private VisualizerMethods[] visualizers;
static String IMAGE_SEQUENCE_PATH; private String imageSequencePath;
static String VIDEO_PATH_AND_FILE; private String videoPathAndFile;
static int IMAGE_FPS; private int imageFps;
static int IMAGES_TO_FPS_INCREASE; private int imagesToFpsIncrease;
static int IMAGE_FPS_MAX; private int imageFpsMax;
static boolean ZOOM; private boolean zoom;
// For images // For images
public static final Font font = new Font("Sans-Serif", Font.PLAIN, 15); private final Font font = new Font("Sans-Serif", Font.PLAIN, 15);
public static final Color imageBackground = new Color(255, 255, 255); private final Color imageBackground = new Color(255, 255, 255);
public static final Color imageConnection = new Color(0, 0, 0); private final Color imageConnection = new Color(0, 0, 0);
public static final Color imageOutline = new Color(0, 0, 0); private final Color imageOutline = new Color(0, 0, 0);
public static final Color imageHydrophobic = new Color(205, 0, 0); private final Color imageHydrophobic = new Color(205, 0, 0);
public static final Color imageHydrophilic = new Color(0, 0, 255); private final Color imageHydrophilic = new Color(0, 0, 255);
public static final Color imageMixed = new Color(205, 0, 205); private final Color imageMixed = new Color(205, 0, 205);
public static final Color imageAminoText = new Color(0, 190, 190); private final Color imageAminoText = new Color(0, 190, 190);
public static final Color imageText = new Color(0,0,0); private final Color imageText = new Color(0,0,0);
// For console output // For console output
public static final String consoleEmpty = " "; private final String consoleEmpty = " ";
public static final String consoleHydrophobic = "(o)"; private final String consoleHydrophobic = "(o)";
public static final String consoleHydrophilic = "(i)"; private final String consoleHydrophilic = "(i)";
public static final String consoleHydrophobicMulti = "{o}"; private final String consoleHydrophobicMulti = "{o}";
public static final String consoleHydrophilicMulti = "{i}"; private final String consoleHydrophilicMulti = "{i}";
public static final String consoleMixed = "{z}"; private final String consoleMixed = "{z}";
public static final String consoleConnectionVertical = " | "; private final String consoleConnectionVertical = " | ";
public static final String consoleConnectionHorizontal = "---"; private final String consoleConnectionHorizontal = "---";
public Config(String propertyPath) { public Config(String propertyPath) {
this.propertyPath = propertyPath; this.propertyPath = propertyPath;
...@@ -90,92 +90,259 @@ public class Config { ...@@ -90,92 +90,259 @@ public class Config {
private void initializeProperties() { private void initializeProperties() {
// Basic Initialization settings // Basic Initialization settings
ENCODING_VARIANT = this.properties.getProperty("encodingVariant"); encodingVariant = this.properties.getProperty("encodingVariant");
SEED = Integer.parseInt(this.properties.getProperty("seed")); seed = Integer.parseInt(this.properties.getProperty("seed"));
// Algorithm settings // Algorithm settings
POPULATION_SIZE = Integer.parseInt(this.properties.getProperty("populationSize")); populationSize = Integer.parseInt(this.properties.getProperty("populationSize"));
TOTAL_GENERATIONS = Integer.parseInt(this.properties.getProperty("noGenerations")); totalGenerations = Integer.parseInt(this.properties.getProperty("noGenerations"));
switch (this.properties.getProperty("initializationMethod")) { switch (this.properties.getProperty("initializationMethod")) {
case "curl": case "curl":
INITIALIZATION_METHOD = InitializationMethods.Curl; initializationMethod = InitializationMethods.Curl;
break; break;
case "straight": case "straight":
INITIALIZATION_METHOD = InitializationMethods.Straight; initializationMethod = InitializationMethods.Straight;
break; break;
case "random": case "random":
INITIALIZATION_METHOD = InitializationMethods.Random; initializationMethod = InitializationMethods.Random;
break; break;
} }
switch (this.properties.getProperty("selectionMethod")) { switch (this.properties.getProperty("selectionMethod")) {
case "proportional": case "proportional":
SELECTION_METHOD = SelectionMethods.Proportional; selectionMethod = SelectionMethods.Proportional;
break; break;
case "tournament": case "tournament":
SELECTION_METHOD = SelectionMethods.Tournament; selectionMethod = SelectionMethods.Tournament;
break; break;
case "onlybest": case "onlybest":
SELECTION_METHOD = SelectionMethods.OnlyBest; selectionMethod = SelectionMethods.OnlyBest;
break; break;
} }
K = Integer.parseInt(this.properties.getProperty("k")); k = Integer.parseInt(this.properties.getProperty("k"));
String[] mutatorsToUse = this.properties.getProperty("mutatorMethods").split(","); String[] mutatorsToUse = this.properties.getProperty("mutatorMethods").split(",");
MUTATOR_METHODS = new MutatorMethods[mutatorsToUse.length]; mutatorMethods = new MutatorMethods[mutatorsToUse.length];
for (int i = 0; i < mutatorsToUse.length; i++) { for (int i = 0; i < mutatorsToUse.length; i++) {
if (mutatorsToUse[i].equals("singlePoint")) { if (mutatorsToUse[i].equals("singlePoint")) {
MUTATOR_METHODS[i] = MutatorMethods.SinglePoint; mutatorMethods[i] = MutatorMethods.SinglePoint;
} else if (mutatorsToUse[i].equals("crossover")) { } else if (mutatorsToUse[i].equals("crossover")) {
MUTATOR_METHODS[i] = MutatorMethods.Crossover; mutatorMethods[i] = MutatorMethods.Crossover;
} }
} }
POINTS_PER_BOND = Integer.parseInt(this.properties.getProperty("pointsPerBond")); pointsPerBond = Integer.parseInt(this.properties.getProperty("pointsPerBond"));
// Mutation settings // Mutation settings
MUTATION_ATTEMPTS_PER_CANDIDATE = Integer.parseInt(this.properties.getProperty("mutationAttemptsPerCandidate")); mutationAttemptsPerCandidate = Integer.parseInt(this.properties.getProperty("mutationAttemptsPerCandidate"));
MUTATION_CHANCE = Double.parseDouble(this.properties.getProperty("mutationChance")); mutationChance = Double.parseDouble(this.properties.getProperty("mutationChance"));
MUTATION_MINIMAL_CHANCE = Double.parseDouble(this.properties.getProperty("mutationMinimalChance")); mutationMinimalChance = Double.parseDouble(this.properties.getProperty("mutationMinimalChance"));
MUTATION_MULTIPLIER = Double.parseDouble(this.properties.getProperty("mutationMultiplier")); mutationMultiplier = Double.parseDouble(this.properties.getProperty("mutationMultiplier"));
CROSSOVER_ATTEMPTS_PER_CANDIDATE = Integer.parseInt(this.properties.getProperty("crossoverAttemptsPerCandidate")); crossoverAttemptsPerCandidate = Integer.parseInt(this.properties.getProperty("crossoverAttemptsPerCandidate"));
CROSSOVER_CHANCE = Double.parseDouble(this.properties.getProperty("crossoverChance")); crossoverChance = Double.parseDouble(this.properties.getProperty("crossoverChance"));
CROSSOVER_MINIMAL_CHANCE = Double.parseDouble(this.properties.getProperty("crossoverMinimalChance")); crossoverMinimalChance = Double.parseDouble(this.properties.getProperty("crossoverMinimalChance"));
CROSSOVER_MULTIPLIER = Double.parseDouble(this.properties.getProperty("crossoverMultiplier")); crossoverMultiplier = Double.parseDouble(this.properties.getProperty("crossoverMultiplier"));
// Output settings // Output settings
LOGFILE = this.properties.getProperty("logfilePath"); logfile = this.properties.getProperty("logfilePath");
String[] visualizersToUse = this.properties.getProperty("visualizerType").split(","); String[] visualizersToUse = this.properties.getProperty("visualizerType").split(",");
VISUALIZERS = new VisualizerMethods[visualizersToUse.length]; visualizers = new VisualizerMethods[visualizersToUse.length];
for (int i = 0; i < visualizersToUse.length; i++) { for (int i = 0; i < visualizersToUse.length; i++) {
switch (visualizersToUse[i]) { switch (visualizersToUse[i]) {
case "console": case "console":
VISUALIZERS[i] = VisualizerMethods.Console; visualizers[i] = VisualizerMethods.Console;
break; break;
case "image": case "image":
VISUALIZERS[i] = VisualizerMethods.Image; visualizers[i] = VisualizerMethods.Image;
break; break;
case "video": case "video":
VISUALIZERS[i] = VisualizerMethods.Video; visualizers[i] = VisualizerMethods.Video;
break; break;
} }
} }
IMAGE_SEQUENCE_PATH = this.properties.getProperty("imageSequencePath"); imageSequencePath = this.properties.getProperty("imageSequencePath");
VIDEO_PATH_AND_FILE = this.properties.getProperty("videoPathAndFile"); videoPathAndFile = this.properties.getProperty("videoPathAndFile");
IMAGE_FPS = Integer.parseInt(this.properties.getProperty("imgFps")); imageFps = Integer.parseInt(this.properties.getProperty("imgFps"));
IMAGES_TO_FPS_INCREASE = Integer.parseInt(this.properties.getProperty("imagesToFpsIncrease")); imagesToFpsIncrease = Integer.parseInt(this.properties.getProperty("imagesToFpsIncrease"));
IMAGE_FPS_MAX = Integer.parseInt(this.properties.getProperty("imgFpsMax")); imageFpsMax = Integer.parseInt(this.properties.getProperty("imgFpsMax"));
ZOOM = this.properties.getProperty("zoom").equals("true"); zoom = this.properties.getProperty("zoom").equals("true");
} }
public Properties getProperties() { public Properties getProperties() {
return this.properties; return this.properties;
} }
public String getEncodingVariant() {
return encodingVariant;
}
public int getSeed() {
return seed;
}
public int getPopulationSize() {
return populationSize;
}
public int getTotalGenerations() {
return totalGenerations;
}
public InitializationMethods getInitializationMethod() {
return initializationMethod;
}
public SelectionMethods getSelectionMethod() {
return selectionMethod;
}
public int getK() {
return k;
}
public MutatorMethods[] getMutatorMethods() {
return mutatorMethods;
}
public int getPointsPerBond() {
return pointsPerBond;
}
public int getMutationAttemptsPerCandidate() {
return mutationAttemptsPerCandidate;
}
public double getMutationChance() {
return mutationChance;
}
public double getMutationMultiplier() {
return mutationMultiplier;
}
public double getMutationMinimalChance() {
return mutationMinimalChance;
}
public int getCrossoverAttemptsPerCandidate() {
return crossoverAttemptsPerCandidate;
}
public double getCrossoverChance() {
return crossoverChance;
}
public double getCrossoverMinimalChance() {
return crossoverMinimalChance;
}
public double getCrossoverMultiplier() {
return crossoverMultiplier;
}
public String getLogfile() {
return logfile;
}
public VisualizerMethods[] getVisualizers() {
return visualizers;
}
public String getImageSequencePath() {
return imageSequencePath;
}
public String getVideoPathAndFile() {
return videoPathAndFile;
}
public int getImageFps() {
return imageFps;
}
public int getImagesToFpsIncrease() {
return imagesToFpsIncrease;
}
public int getImageFpsMax() {
return imageFpsMax;
}
public boolean isZoom() {
return zoom;
}
public Font getFont() {
return font;
}
public Color getImageBackground() {
return imageBackground;
}
public Color getImageConnection() {
return imageConnection;
}
public Color getImageOutline() {
return imageOutline;
}
public Color getImageHydrophobic() {
return imageHydrophobic;
}
public Color getImageHydrophilic() {
return imageHydrophilic;
}
public Color getImageMixed() {
return imageMixed;
}
public Color getImageAminoText() {
return imageAminoText;
}
public Color getImageText() {
return imageText;
}
public String getConsoleEmpty() {
return consoleEmpty;
}
public String getConsoleHydrophobic() {
return consoleHydrophobic;
}
public String getConsoleHydrophilic() {
return consoleHydrophilic;
}
public String getConsoleHydrophobicMulti() {
return consoleHydrophobicMulti;
}
public String getConsoleHydrophilicMulti() {
return consoleHydrophilicMulti;
}
public String getConsoleMixed() {
return consoleMixed;
}
public String getConsoleConnectionVertical() {
return consoleConnectionVertical;
}
public String getConsoleConnectionHorizontal() {
return consoleConnectionHorizontal;
}
} }
...@@ -23,6 +23,8 @@ import java.util.Random; ...@@ -23,6 +23,8 @@ import java.util.Random;
public class GeneticAlgorithm { public class GeneticAlgorithm {
Random rand; Random rand;
Config config;
int[] isHydrophobic; int[] isHydrophobic;
Candidate[] population; Candidate[] population;
...@@ -39,77 +41,78 @@ public class GeneticAlgorithm { ...@@ -39,77 +41,78 @@ public class GeneticAlgorithm {
Visualizer[] visualizers; Visualizer[] visualizers;
// Initialize with protein // Initialize with protein
public GeneticAlgorithm (int[] protein) { public GeneticAlgorithm (int[] protein, Config config) {
this.isHydrophobic = protein; this.isHydrophobic = protein;
this.config = config;
this.initializeSettings(); this.initializeSettings();
this.clearLog(); this.clearLog();
this.population = this.initialGenCreator.initializeDirections(Config.POPULATION_SIZE, this.isHydrophobic.length); this.population = this.initialGenCreator.initializeDirections(config.getPopulationSize(), this.isHydrophobic.length);
this.totalFitness = 0; this.totalFitness = 0;
this.fitness = new double[Config.POPULATION_SIZE]; this.fitness = new double[config.getPopulationSize()];
this.overallBestFitness = 0; this.overallBestFitness = 0;
} }
private void initializeSettings() { private void initializeSettings() {
if (Config.SEED != -1) { if (config.getSeed() != -1) {
this.rand = new Random(Config.SEED); this.rand = new Random(config.getSeed());
} else { } else {
this.rand = new Random(); this.rand = new Random();
} }
// Settings that are dependant on encoding // Settings that are dependant on encoding
if (Config.ENCODING_VARIANT.equals("NESW")) { if (config.getEncodingVariant().equals("NESW")) {
int nullCount = 0; int nullCount = 0;
for (int i = 0; i < Config.VISUALIZERS.length; i++) { for (int i = 0; i < config.getVisualizers().length; i++) {
if (!Config.VISUALIZERS[i].equals(VisualizerMethods.Console) if (!config.getVisualizers()[i].equals(VisualizerMethods.Console)
&& !Config.VISUALIZERS[i].equals(VisualizerMethods.Image)) { && !config.getVisualizers()[i].equals(VisualizerMethods.Image)) {
nullCount++; nullCount++;
} }
} }
this.visualizers = new Visualizer[Config.VISUALIZERS.length - nullCount]; this.visualizers = new Visualizer[config.getVisualizers().length - nullCount];
int j = 0; int j = 0;
for (VisualizerMethods vm : Config.VISUALIZERS) { for (VisualizerMethods vm : config.getVisualizers()) {
if (vm.equals(VisualizerMethods.Console)) { if (vm.equals(VisualizerMethods.Console)) {
this.visualizers[j] = new VisualizerNESWtoConsole(isHydrophobic); this.visualizers[j] = new VisualizerNESWtoConsole(isHydrophobic, config);
j++; j++;
} else if (vm.equals(VisualizerMethods.Image)) { } else if (vm.equals(VisualizerMethods.Image)) {
this.visualizers[j] = new VisualizerNESWtoFile(Config.IMAGE_SEQUENCE_PATH,isHydrophobic); this.visualizers[j] = new VisualizerNESWtoFile(isHydrophobic, config);
j++; j++;
} }
} }
if (Config.INITIALIZATION_METHOD.equals(InitializationMethods.Curl)) { if (config.getInitializationMethod().equals(InitializationMethods.Curl)) {
this.initialGenCreator = new Curl<>(DirectionNESW.class); this.initialGenCreator = new Curl<>(DirectionNESW.class);
} else if (Config.INITIALIZATION_METHOD.equals(InitializationMethods.Straight)) { } else if (config.getInitializationMethod().equals(InitializationMethods.Straight)) {
this.initialGenCreator = new StraightLine(); this.initialGenCreator = new StraightLine();
} else if (Config.INITIALIZATION_METHOD.equals(InitializationMethods.Random)) { } else if (config.getInitializationMethod().equals(InitializationMethods.Random)) {
this.initialGenCreator = new RandomDirection<>(DirectionNESW.class, this.rand); this.initialGenCreator = new RandomDirection<>(DirectionNESW.class, this.rand);
} }
this.mutators = new Mutator[Config.MUTATOR_METHODS.length]; this.mutators = new Mutator[config.getMutatorMethods().length];
for (int i = 0; i < Config.MUTATOR_METHODS.length; i++) { for (int i = 0; i < config.getMutatorMethods().length; i++) {
if (Config.MUTATOR_METHODS[i].equals(MutatorMethods.SinglePoint)) { if (config.getMutatorMethods()[i].equals(MutatorMethods.SinglePoint)) {
this.mutators[i] = new SinglePoint<>(DirectionNESW.class, this.rand, this.mutators[i] = new SinglePoint<>(DirectionNESW.class, this.rand,
Config.MUTATION_ATTEMPTS_PER_CANDIDATE, Config.MUTATION_CHANCE, Config.MUTATION_MINIMAL_CHANCE, Config.MUTATION_MULTIPLIER); config.getMutationAttemptsPerCandidate(), config.getMutationChance(), config.getMutationMinimalChance(), config.getMutationMultiplier());
} else if (Config.MUTATOR_METHODS[i].equals(MutatorMethods.Crossover)) { } else if (config.getMutatorMethods()[i].equals(MutatorMethods.Crossover)) {
this.mutators[i] = new Crossover<>(DirectionNESW.class, this.rand, this.mutators[i] = new Crossover<>(DirectionNESW.class, this.rand,
Config.CROSSOVER_ATTEMPTS_PER_CANDIDATE, Config.CROSSOVER_CHANCE, Config.MUTATION_MINIMAL_CHANCE, Config.CROSSOVER_MULTIPLIER); config.getCrossoverAttemptsPerCandidate(), config.getCrossoverChance(), config.getMutationMinimalChance(), config.getCrossoverMultiplier());
} }
} }
this.evaluator = new EvaluatorNESW(Config.POINTS_PER_BOND, isHydrophobic); this.evaluator = new EvaluatorNESW(config.getPointsPerBond(), isHydrophobic);
} else { } else {
// TODO: initialization for FRL settings // TODO: initialization for FRL settings
} }
if (Config.SELECTION_METHOD.equals(SelectionMethods.Proportional)) { if (config.getSelectionMethod().equals(SelectionMethods.Proportional)) {
this.selector = new FitnessProportional(this.rand, this.isHydrophobic); this.selector = new FitnessProportional(this.rand, this.isHydrophobic);
} else if (Config.SELECTION_METHOD.equals(SelectionMethods.Tournament)) { } else if (config.getSelectionMethod().equals(SelectionMethods.Tournament)) {
this.selector = new Tournament(this.rand, this.isHydrophobic, Config.K); this.selector = new Tournament(this.rand, this.isHydrophobic, config.getK());
} else if (Config.SELECTION_METHOD.equals(SelectionMethods.OnlyBest)) { } else if (config.getSelectionMethod().equals(SelectionMethods.OnlyBest)) {
this.selector = new OnlyBest(this.isHydrophobic); this.selector = new OnlyBest(this.isHydrophobic);
} }
} }
...@@ -117,7 +120,7 @@ public class GeneticAlgorithm { ...@@ -117,7 +120,7 @@ public class GeneticAlgorithm {
private void clearLog() { private void clearLog() {
String content = "Generation\tAverage Fitness\tBest Fitness\tOverall Best Fitness\tBonds\tOverlaps\n"; String content = "Generation\tAverage Fitness\tBest Fitness\tOverall Best Fitness\tBonds\tOverlaps\n";
try { try {
Files.write(Paths.get(Config.LOGFILE), content.getBytes()); Files.write(Paths.get(config.getLogfile()), content.getBytes());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -125,7 +128,7 @@ public class GeneticAlgorithm { ...@@ -125,7 +128,7 @@ public class GeneticAlgorithm {
} }
public void simulateGenerations() { public void simulateGenerations() {
for (int gen = 0; gen < Config.TOTAL_GENERATIONS-1; gen++) { for (int gen = 0; gen < config.getTotalGenerations()-1; gen++) {
this.evaluateGeneration(gen); this.evaluateGeneration(gen);
this.population = this.selector.selectNewPopulation(this.population, this.fitness, this.totalFitness); this.population = this.selector.selectNewPopulation(this.population, this.fitness, this.totalFitness);
...@@ -135,7 +138,7 @@ public class GeneticAlgorithm { ...@@ -135,7 +138,7 @@ public class GeneticAlgorithm {
System.out.println(); System.out.println();
} }
evaluateGeneration(Config.TOTAL_GENERATIONS-1); evaluateGeneration(config.getTotalGenerations()-1);
} }
private int evaluateGeneration(int gen) { private int evaluateGeneration(int gen) {
...@@ -145,7 +148,7 @@ public class GeneticAlgorithm { ...@@ -145,7 +148,7 @@ public class GeneticAlgorithm {
double bestFitness = 0; double bestFitness = 0;
int bestIndex = 0; int bestIndex = 0;
this.totalFitness = 0; this.totalFitness = 0;
for (int i = 0; i < Config.POPULATION_SIZE; i++) { for (int i = 0; i < config.getPopulationSize(); i++) {
this.fitness[i] = this.evaluator.evaluateFitness(this.population[i]); this.fitness[i] = this.evaluator.evaluateFitness(this.population[i]);
this.totalFitness += this.fitness[i]; this.totalFitness += this.fitness[i];
...@@ -171,7 +174,7 @@ public class GeneticAlgorithm { ...@@ -171,7 +174,7 @@ public class GeneticAlgorithm {
this.overallBest = new Candidate(this.population[bestIndex].getFolding()); this.overallBest = new Candidate(this.population[bestIndex].getFolding());
} }
double averageFitness = this.totalFitness / Config.POPULATION_SIZE; double averageFitness = this.totalFitness / config.getPopulationSize();
String log = String.format("%d\t%.4f\t%.4f\t%.4f\t %d\t%d\n", String log = String.format("%d\t%.4f\t%.4f\t%.4f\t %d\t%d\n",
gen, averageFitness, bestFitness, gen, averageFitness, bestFitness,
this.evaluator.evaluateFitness(overallBest), this.evaluator.evaluateFitness(overallBest),
...@@ -179,7 +182,7 @@ public class GeneticAlgorithm { ...@@ -179,7 +182,7 @@ public class GeneticAlgorithm {
-1); -1);
try { try {
Files.write(Paths.get(Config.LOGFILE), log.getBytes(), StandardOpenOption.APPEND); Files.write(Paths.get(config.getLogfile()), log.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -13,16 +13,16 @@ public class Main { ...@@ -13,16 +13,16 @@ public class Main {
Config config = new Config(propertyPath); Config config = new Config(propertyPath);
int[] protein = Examples.convertStringToIntArray(Examples.SEQ36); int[] protein = Examples.convertStringToIntArray(Examples.SEQ36);
GeneticAlgorithm ga = new GeneticAlgorithm(protein); GeneticAlgorithm ga = new GeneticAlgorithm(protein, config);
ga.simulateGenerations(); ga.simulateGenerations();
// Create a new video if possible and desired // Create a new video if possible and desired
boolean imagesRefreshed = Arrays.asList(Config.VISUALIZERS).contains(VisualizerMethods.Image); boolean imagesRefreshed = Arrays.asList(config.getVisualizers()).contains(VisualizerMethods.Image);
boolean videoEnabled = Arrays.asList(Config.VISUALIZERS).contains(VisualizerMethods.Video); boolean videoEnabled = Arrays.asList(config.getVisualizers()).contains(VisualizerMethods.Video);
if (imagesRefreshed && videoEnabled){ if (imagesRefreshed && videoEnabled){
VideoCreator.createVideo(Config.IMAGE_SEQUENCE_PATH, Config.VIDEO_PATH_AND_FILE, VideoCreator.createVideo(config.getImageSequencePath(), config.getVideoPathAndFile(),
Config.IMAGE_FPS, Config.IMAGES_TO_FPS_INCREASE, Config.IMAGE_FPS_MAX, config.getImageFps(), config.getImagesToFpsIncrease(), config.getImageFpsMax(),
ga.getMaxH(), ga.getMaxW(), Config.ZOOM); ga.getMaxH(), ga.getMaxW(), config.isZoom());
} }
} }
} }
...@@ -100,8 +100,9 @@ public class VideoCreator{ ...@@ -100,8 +100,9 @@ public class VideoCreator{
// draws input image to the top left corner // draws input image to the top left corner
Graphics2D g2d = outputImage.createGraphics(); Graphics2D g2d = outputImage.createGraphics();
g2d.setColor(Config.imageBackground); //TODO The following two lines should be unnecessary
g2d.fillRect(0,0, maxWidth, maxHeight); //g2d.setColor(Config.imageBackground);
//g2d.fillRect(0,0, maxWidth, maxHeight);
g2d.drawImage(inputImage, 0, 0, inputImage.getWidth(), inputImage.getHeight(), null); g2d.drawImage(inputImage, 0, 0, inputImage.getWidth(), inputImage.getHeight(), null);
g2d.dispose(); g2d.dispose();
...@@ -162,8 +163,9 @@ public class VideoCreator{ ...@@ -162,8 +163,9 @@ public class VideoCreator{
// draws input image to the top left corner // draws input image to the top left corner
Graphics2D g2d = outputImage.createGraphics(); Graphics2D g2d = outputImage.createGraphics();
g2d.setColor(Config.imageBackground); //TODO The following two lines should be unnecessary
g2d.fillRect(0,0, maxWidthAfterIndex[0], maxHeightAfterIndex[0]); //g2d.setColor(Config.imageBackground);
//g2d.fillRect(0,0, maxWidthAfterIndex[0], maxHeightAfterIndex[0]);
int newHeight = 0; int newHeight = 0;
int newWidth = 0; int newWidth = 0;
......
...@@ -18,11 +18,13 @@ public class VisualizerNESWtoConsole implements Visualizer { ...@@ -18,11 +18,13 @@ public class VisualizerNESWtoConsole implements Visualizer {
int maxHeight; int maxHeight;
int maxWidth; int maxWidth;
final int[] isHydrophobic; final int[] isHydrophobic;
Config config;
public VisualizerNESWtoConsole(int[] isHydrophobic) { public VisualizerNESWtoConsole(int[] isHydrophobic, Config config) {
this.maxHeight = 0; this.maxHeight = 0;
this.maxWidth = 0; this.maxWidth = 0;
this.isHydrophobic = isHydrophobic; this.isHydrophobic = isHydrophobic;
this.config = config;
} }
public void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) { public void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) {
...@@ -46,32 +48,32 @@ public class VisualizerNESWtoConsole implements Visualizer { ...@@ -46,32 +48,32 @@ public class VisualizerNESWtoConsole implements Visualizer {
switch (cellState) { switch (cellState) {
case Empty: case Empty:
return Config.consoleEmpty; return config.getConsoleEmpty();
case Hydrophobic: case Hydrophobic:
return Config.consoleHydrophobic; return config.getConsoleHydrophobic();
case Hydrophilic: case Hydrophilic:
return Config.consoleHydrophilic; return config.getConsoleHydrophilic();
case HydrophobicMulti: case HydrophobicMulti:
return Config.consoleHydrophobicMulti; return config.getConsoleHydrophobicMulti();
case HydrophilicMulti: case HydrophilicMulti:
return Config.consoleHydrophilicMulti; return config.getConsoleHydrophilicMulti();
case Mixed: case Mixed:
return Config.consoleMixed; return config.getConsoleMixed();
case ConnectionVertical: case ConnectionVertical:
return Config.consoleConnectionVertical; return config.getConsoleConnectionVertical();
case ConnectionHorizontal: case ConnectionHorizontal:
return Config.consoleConnectionHorizontal; return config.getConsoleConnectionHorizontal();
} }
// Fallback // Fallback
return Config.consoleEmpty; return config.getConsoleEmpty();
} }
......
...@@ -26,14 +26,16 @@ public class VisualizerNESWtoFile implements Visualizer { ...@@ -26,14 +26,16 @@ public class VisualizerNESWtoFile implements Visualizer {
int margin = pixelsPerCell * 2; int margin = pixelsPerCell * 2;
int outline = 2; int outline = 2;
final int[] isHydrophobic; final int[] isHydrophobic;
Config config;
public VisualizerNESWtoFile(String folder, int[] isHydrophobic) { public VisualizerNESWtoFile(int[] isHydrophobic, Config config) {
this.folder = folder; this.folder = config.getImageSequencePath();
this.filename = "image.png"; // Default this.filename = "image.png"; // Default
this.maxHeight = 0; this.maxHeight = 0;
this.maxWidth = 0; this.maxWidth = 0;
this.isHydrophobic = isHydrophobic; this.isHydrophobic = isHydrophobic;
this.config = config;
} }
public void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) { public void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) {
...@@ -55,12 +57,12 @@ public class VisualizerNESWtoFile implements Visualizer { ...@@ -55,12 +57,12 @@ public class VisualizerNESWtoFile implements Visualizer {
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, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(Config.font); g2.setFont(config.getFont());
FontMetrics metrics = g2.getFontMetrics(); FontMetrics metrics = g2.getFontMetrics();
int ascent = metrics.getAscent(); int ascent = metrics.getAscent();
// Background // Background
g2.setColor(Config.imageBackground); 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--) {
...@@ -77,27 +79,27 @@ public class VisualizerNESWtoFile implements Visualizer { ...@@ -77,27 +79,27 @@ public class VisualizerNESWtoFile implements Visualizer {
case Hydrophobic: case Hydrophobic:
case HydrophobicMulti: case HydrophobicMulti:
aminoColor = Config.imageHydrophobic; aminoColor = config.getImageHydrophobic();
break; break;
case Hydrophilic: case Hydrophilic:
case HydrophilicMulti: case HydrophilicMulti:
aminoColor = Config.imageHydrophilic; aminoColor = config.getImageHydrophilic();
break; break;
case Mixed: case Mixed:
aminoColor = Config.imageMixed; aminoColor = config.getImageMixed();
break; break;
case ConnectionVertical: case ConnectionVertical:
g2.setColor(Config.imageConnection); 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.imageConnection); 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);
...@@ -105,14 +107,14 @@ public class VisualizerNESWtoFile implements Visualizer { ...@@ -105,14 +107,14 @@ public class VisualizerNESWtoFile implements Visualizer {
} }
if (aminoColor != null) { if (aminoColor != null) {
g2.setColor(Config.imageOutline); 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.imageAminoText); 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 + " ";
...@@ -125,7 +127,7 @@ public class VisualizerNESWtoFile implements Visualizer { ...@@ -125,7 +127,7 @@ public class VisualizerNESWtoFile implements Visualizer {
} }
} }
g2.setColor(Config.imageText); g2.setColor(config.getImageText());
String label = "Gen: " + gen String label = "Gen: " + gen
+ " Fitness: " + String.format("%.4f", fit) + " Fitness: " + String.format("%.4f", fit)
+ " H/H Bonds: " + bond + " H/H Bonds: " + bond
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment