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

Removed protein information from Candidate.

parent c59d40ef
No related branches found
No related tags found
No related merge requests found
Showing
with 47 additions and 53 deletions
...@@ -8,9 +8,11 @@ import MainClasses.Vertex; ...@@ -8,9 +8,11 @@ import MainClasses.Vertex;
public class EvaluatorNESW implements Evaluator { public class EvaluatorNESW implements Evaluator {
final int POINTS_PER_BOND; final int POINTS_PER_BOND;
final int[] isHydrophobic;
public EvaluatorNESW(int pointsPerBond) { public EvaluatorNESW(int pointsPerBond, int[] isHydrophobic) {
this.POINTS_PER_BOND = pointsPerBond; this.POINTS_PER_BOND = pointsPerBond;
this.isHydrophobic = isHydrophobic;
} }
@Override @Override
...@@ -36,10 +38,10 @@ public class EvaluatorNESW implements Evaluator { ...@@ -36,10 +38,10 @@ public class EvaluatorNESW implements Evaluator {
for (int i = 0; i < candidate.vertexList.size() - 2; i++) { for (int i = 0; i < candidate.vertexList.size() - 2; i++) {
Vertex toCompare = candidate.vertexList.get(i); Vertex toCompare = candidate.vertexList.get(i);
if (toCompare.isHydrophobic) { if (isHydrophobic[i]==1) {
for (int j = i + 2; j < candidate.vertexList.size(); j++) { for (int j = i + 2; j < candidate.vertexList.size(); j++) {
Vertex vertex = candidate.vertexList.get(j); Vertex vertex = candidate.vertexList.get(j);
if (vertex.isHydrophobic) { if (isHydrophobic[j]==1) {
if (toCompare.neighbouringPosition(vertex)) { if (toCompare.neighbouringPosition(vertex)) {
bonds++; bonds++;
} }
......
...@@ -14,19 +14,18 @@ public class Curl<T extends Enum<?>> implements InitialGenerationCreator { ...@@ -14,19 +14,18 @@ public class Curl<T extends Enum<?>> implements InitialGenerationCreator {
} }
@Override @Override
public Candidate[] initializeDirections(int populationSize, int[] isHydrophobic) { public Candidate[] initializeDirections(int populationSize, int sequenceLength) {
Candidate[] population = new Candidate[populationSize]; Candidate[] population = new Candidate[populationSize];
int proteinLength = isHydrophobic.length;
int[] candidateDirections; int[] candidateDirections;
if (isFRLEncoding(possibleDirections) && populationSize > 0) { if (isFRLEncoding(possibleDirections) && populationSize > 0) {
candidateDirections = curlFRL(proteinLength); candidateDirections = curlFRL(sequenceLength);
} else { } else {
candidateDirections = curlNESW(proteinLength); candidateDirections = curlNESW(sequenceLength);
} }
for (int i = 0; i < populationSize; i++) { for (int i = 0; i < populationSize; i++) {
population[i] = new Candidate(isHydrophobic, candidateDirections); population[i] = new Candidate(candidateDirections);
} }
return population; return population;
......
...@@ -17,16 +17,15 @@ public class RandomDirection<T extends Enum<?>> implements InitialGenerationCre ...@@ -17,16 +17,15 @@ public class RandomDirection<T extends Enum<?>> implements InitialGenerationCre
} }
@Override @Override
public Candidate[] initializeDirections(int populationSize, int[] isHydrophobic) { public Candidate[] initializeDirections(int populationSize, int sequenceLength) {
Candidate[] population = new Candidate[populationSize]; Candidate[] population = new Candidate[populationSize];
int proteinLength = isHydrophobic.length;
for (int i = 0; i < populationSize; i++) { for (int i = 0; i < populationSize; i++) {
int[] candidateDirections = new int[proteinLength]; int[] candidateDirections = new int[sequenceLength];
for (int j = 0; j < proteinLength; j++) { for (int j = 0; j < sequenceLength; j++) {
candidateDirections[j] = this.randomDirection(this.possibleDirections); candidateDirections[j] = this.randomDirection(this.possibleDirections);
} }
population[i] = new Candidate(isHydrophobic, candidateDirections); population[i] = new Candidate(candidateDirections);
} }
return population; return population;
......
...@@ -12,17 +12,16 @@ public class StraightLine implements InitialGenerationCreator { ...@@ -12,17 +12,16 @@ public class StraightLine implements InitialGenerationCreator {
} }
@Override @Override
public Candidate[] initializeDirections(int populationSize, int[] isHydrophobic) { public Candidate[] initializeDirections(int populationSize, int sequenceLength) {
Candidate[] population = new Candidate[populationSize]; Candidate[] population = new Candidate[populationSize];
int proteinLength = isHydrophobic.length;
int[] candidateDirections = new int[proteinLength]; int[] candidateDirections = new int[sequenceLength];
for (int j = 0; j < proteinLength; j++) { for (int j = 0; j < sequenceLength; j++) {
candidateDirections[j] = 0; // Default starting direction is set by Enum candidateDirections[j] = 0; // Default starting direction is set by Enum
} }
for (int i = 0; i < populationSize; i++) { for (int i = 0; i < populationSize; i++) {
population[i] = new Candidate(isHydrophobic, candidateDirections); population[i] = new Candidate(candidateDirections);
} }
return population; return population;
......
...@@ -5,8 +5,4 @@ import MainClasses.Candidate; ...@@ -5,8 +5,4 @@ import MainClasses.Candidate;
public interface Evaluator { public interface Evaluator {
double evaluateFitness(Candidate candidate); double evaluateFitness(Candidate candidate);
int evaluateBonds(Candidate candidate);
int evaluateOverlaps(Candidate candidate);
} }
...@@ -4,5 +4,5 @@ import MainClasses.Candidate; ...@@ -4,5 +4,5 @@ import MainClasses.Candidate;
public interface InitialGenerationCreator { public interface InitialGenerationCreator {
Candidate[] initializeDirections(int populationSize, int[] isHydrophobic); Candidate[] initializeDirections(int populationSize, int sequenceLength);
} }
...@@ -14,13 +14,13 @@ public interface Visualizer { ...@@ -14,13 +14,13 @@ public interface Visualizer {
static ArrayList<Vertex> deepCopyVertexList (List<Vertex> vertexListOriginal) { static ArrayList<Vertex> deepCopyVertexList (List<Vertex> vertexListOriginal) {
ArrayList<Vertex> vertexList = new ArrayList<>(); ArrayList<Vertex> vertexList = new ArrayList<>();
for (Vertex v : vertexListOriginal) { for (Vertex v : vertexListOriginal) {
Vertex vNew = new Vertex(v.x, v.y, v.isHydrophobic, v.outgoingDirection); Vertex vNew = new Vertex(v.x, v.y, v.outgoingDirection);
vertexList.add(vNew); vertexList.add(vNew);
} }
return vertexList; return vertexList;
} }
static Cell[][] convertProteinTo2DArray(ArrayList<Vertex> vertexList) { static Cell[][] convertProteinTo2DArray(ArrayList<Vertex> vertexList, int[] isHydrophobic) {
// Determine size // Determine size
int minX = 0; int minX = 0;
int maxX = 0; int maxX = 0;
...@@ -61,7 +61,7 @@ public interface Visualizer { ...@@ -61,7 +61,7 @@ public interface Visualizer {
for (int i = 0; i < vertexList.size(); i++) { for (int i = 0; i < vertexList.size(); i++) {
Vertex vertex = vertexList.get(i); Vertex vertex = vertexList.get(i);
if (vertex.isHydrophobic) { if (isHydrophobic[i]==1) {
cellArray[vertex.y][vertex.x].addState(State.Hydrophobic); cellArray[vertex.y][vertex.x].addState(State.Hydrophobic);
} else { } else {
cellArray[vertex.y][vertex.x].addState(State.Hydrophilic); cellArray[vertex.y][vertex.x].addState(State.Hydrophilic);
......
...@@ -5,13 +5,11 @@ import java.util.Arrays; ...@@ -5,13 +5,11 @@ import java.util.Arrays;
public class Candidate { public class Candidate {
int[] isHydrophobic; // 0 = no | 1 = yes
public int[] outgoingDirection; // 0 = North | 1 = East | 2 = South | 3 = West public int[] outgoingDirection; // 0 = North | 1 = East | 2 = South | 3 = West
public ArrayList<Vertex> vertexList; public ArrayList<Vertex> vertexList;
public double fitness; public double fitness;
public Candidate(int[] isH, int[] oD) { public Candidate(int[] oD) {
this.isHydrophobic = isH;
this.outgoingDirection = oD; this.outgoingDirection = oD;
this.vertexList = constructVertexes(); this.vertexList = constructVertexes();
...@@ -23,9 +21,8 @@ public class Candidate { ...@@ -23,9 +21,8 @@ public class Candidate {
int currentX = 0; int currentX = 0;
int currentY = 0; int currentY = 0;
for (int currentVertex = 0; currentVertex < isHydrophobic.length; currentVertex++) { for (int currentVertex = 0; currentVertex < outgoingDirection.length; currentVertex++) {
vertexList.add(new Vertex(currentX, currentY, vertexList.add(new Vertex(currentX, currentY, outgoingDirection[currentVertex]));
isHydrophobic[currentVertex] == 1, outgoingDirection[currentVertex]));
// Update position // Update position
if (outgoingDirection[currentVertex] == 0) { if (outgoingDirection[currentVertex] == 0) {
......
...@@ -45,7 +45,7 @@ public class GeneticAlgorithm { ...@@ -45,7 +45,7 @@ public class GeneticAlgorithm {
this.initializeSettings(); this.initializeSettings();
this.clearLog(); this.clearLog();
this.population = this.initialGenCreator.initializeDirections(Config.POPULATION_SIZE, this.isHydrophobic); this.population = this.initialGenCreator.initializeDirections(Config.POPULATION_SIZE, this.isHydrophobic.length);
this.totalFitness = 0; this.totalFitness = 0;
this.fitness = new double[Config.POPULATION_SIZE]; this.fitness = new double[Config.POPULATION_SIZE];
this.overallBestFitness = 0; this.overallBestFitness = 0;
...@@ -71,10 +71,10 @@ public class GeneticAlgorithm { ...@@ -71,10 +71,10 @@ public class GeneticAlgorithm {
int j = 0; int j = 0;
for (VisualizerMethods vm : Config.VISUALIZERS) { for (VisualizerMethods vm : Config.VISUALIZERS) {
if (vm.equals(VisualizerMethods.Console)) { if (vm.equals(VisualizerMethods.Console)) {
this.visualizers[j] = new VisualizerNESWtoConsole(); this.visualizers[j] = new VisualizerNESWtoConsole(isHydrophobic);
j++; j++;
} else if (vm.equals(VisualizerMethods.Image)) { } else if (vm.equals(VisualizerMethods.Image)) {
this.visualizers[j] = new VisualizerNESWtoFile(Config.IMAGE_SEQUENCE_PATH); this.visualizers[j] = new VisualizerNESWtoFile(Config.IMAGE_SEQUENCE_PATH,isHydrophobic);
j++; j++;
} }
} }
...@@ -99,7 +99,7 @@ public class GeneticAlgorithm { ...@@ -99,7 +99,7 @@ public class GeneticAlgorithm {
} }
} }
this.evaluator = new EvaluatorNESW(Config.POINTS_PER_BOND); this.evaluator = new EvaluatorNESW(Config.POINTS_PER_BOND, isHydrophobic);
} else { } else {
// TODO: initialization for FRL settings // TODO: initialization for FRL settings
...@@ -154,29 +154,29 @@ public class GeneticAlgorithm { ...@@ -154,29 +154,29 @@ public class GeneticAlgorithm {
bestIndex = i; bestIndex = i;
} }
} }
int bonds = this.evaluator.evaluateBonds(this.population[bestIndex]);
int overlaps = this.evaluator.evaluateOverlaps(this.population[bestIndex]);
for (Visualizer v : this.visualizers) { for (Visualizer v : this.visualizers) {
v.setFilename(String.format("gen_%d.png", gen)); v.setFilename(String.format("gen_%d.png", gen));
v.drawProtein(this.population[bestIndex].getVertexList(), bestFitness, bonds, overlaps, gen); //TODO Print real bond and overlap amount
v.drawProtein(this.population[bestIndex].getVertexList(), bestFitness, -1, -1, gen);
} }
//TODO Print real bond and overlap amount
System.out.println("The fitness is: " + bestFitness System.out.println("The fitness is: " + bestFitness
+ " [hydrophobicBonds = " + bonds + " | overlaps = " + overlaps + "]"); + " [hydrophobicBonds = " + -1 + " | overlaps = " + -1 + "]");
// Save the overall best // Save the overall best
if (bestFitness >= this.overallBestFitness) { if (bestFitness >= this.overallBestFitness) {
this.overallBestFitness = bestFitness; this.overallBestFitness = bestFitness;
this.overallBest = new Candidate(this.isHydrophobic, this.population[bestIndex].getOutgoing()); this.overallBest = new Candidate(this.population[bestIndex].getOutgoing());
} }
double averageFitness = this.totalFitness / Config.POPULATION_SIZE; double averageFitness = this.totalFitness / Config.POPULATION_SIZE;
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),
this.evaluator.evaluateBonds(overallBest), -1,
this.evaluator.evaluateOverlaps(overallBest)); -1);
try { try {
Files.write(Paths.get(Config.LOGFILE), log.getBytes(), StandardOpenOption.APPEND); Files.write(Paths.get(Config.LOGFILE), log.getBytes(), StandardOpenOption.APPEND);
......
...@@ -4,13 +4,11 @@ package MainClasses; ...@@ -4,13 +4,11 @@ package MainClasses;
public class Vertex { public class Vertex {
public int x; public int x;
public int y; public int y;
public boolean isHydrophobic;
public int outgoingDirection; public int outgoingDirection;
public Vertex(int x, int y, boolean isHydrophobic, int outgoingDirection) { public Vertex(int x, int y, int outgoingDirection) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.isHydrophobic = isHydrophobic;
this.outgoingDirection = outgoingDirection; this.outgoingDirection = outgoingDirection;
} }
......
...@@ -33,7 +33,7 @@ public class FitnessProportional implements Selector { ...@@ -33,7 +33,7 @@ public class FitnessProportional implements Selector {
j++; j++;
picked -= proportionalFitness[j]; picked -= proportionalFitness[j];
} }
newPopulation[i] = new Candidate(this.isHydrophobic, population[j].getOutgoing()); newPopulation[i] = new Candidate(population[j].getOutgoing());
} }
return newPopulation; return newPopulation;
......
...@@ -31,7 +31,7 @@ public class OnlyBest implements Selector { ...@@ -31,7 +31,7 @@ public class OnlyBest implements Selector {
int[] bestFolding = population[bestIndex].getOutgoing(); int[] bestFolding = population[bestIndex].getOutgoing();
for (int i = 0; i < populationSize; i++) { for (int i = 0; i < populationSize; i++) {
newPopulation[i] = new Candidate(this.isHydrophobic, bestFolding); newPopulation[i] = new Candidate(bestFolding);
} }
return newPopulation; return newPopulation;
......
...@@ -35,7 +35,7 @@ public class Tournament implements Selector { ...@@ -35,7 +35,7 @@ public class Tournament implements Selector {
tournamentChoosenIndex = nextIndex; tournamentChoosenIndex = nextIndex;
} }
} }
newPopulation[i] = new Candidate(this.isHydrophobic, population[tournamentChoosenIndex].getOutgoing()); newPopulation[i] = new Candidate(population[tournamentChoosenIndex].getOutgoing());
} }
return newPopulation; return newPopulation;
......
...@@ -17,17 +17,19 @@ public class VisualizerNESWtoConsole implements Visualizer { ...@@ -17,17 +17,19 @@ public class VisualizerNESWtoConsole implements Visualizer {
int maxHeight; int maxHeight;
int maxWidth; int maxWidth;
final int[] isHydrophobic;
public VisualizerNESWtoConsole() { public VisualizerNESWtoConsole(int[] isHydrophobic) {
this.maxHeight = 0; this.maxHeight = 0;
this.maxWidth = 0; this.maxWidth = 0;
this.isHydrophobic = isHydrophobic;
} }
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) {
// Copy VertexList to be able to manipulate it // Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = Visualizer.deepCopyVertexList(vertexListOriginal); ArrayList<Vertex> vertexList = Visualizer.deepCopyVertexList(vertexListOriginal);
Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList); Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList, isHydrophobic);
for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) { for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) {
for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) { for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) {
......
...@@ -25,20 +25,22 @@ public class VisualizerNESWtoFile implements Visualizer { ...@@ -25,20 +25,22 @@ public class VisualizerNESWtoFile implements Visualizer {
int pixelsPerCell = 40; int pixelsPerCell = 40;
int margin = pixelsPerCell * 2; int margin = pixelsPerCell * 2;
int outline = 2; int outline = 2;
final int[] isHydrophobic;
public VisualizerNESWtoFile(String folder) { public VisualizerNESWtoFile(String folder, int[] isHydrophobic) {
this.folder = folder; this.folder = folder;
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;
} }
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) {
// Copy VertexList to be able to manipulate it // Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = Visualizer.deepCopyVertexList(vertexListOriginal); ArrayList<Vertex> vertexList = Visualizer.deepCopyVertexList(vertexListOriginal);
Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList); 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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment