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