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

Moved mutation and crossover logic from Candidate to Mutator.

parent e4731af4
Branches
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ import java.util.Arrays;
public class Candidate {
int[] isHydrophobic; // 0 = no | 1 = yes
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 double fitness;
......@@ -18,7 +18,7 @@ public class Candidate {
this.fitness = -1d; // Not calculated yet
}
private ArrayList<Vertex> constructVertexes() {
public ArrayList<Vertex> constructVertexes() {
ArrayList<Vertex> vertexList = new ArrayList<>();
int currentX = 0;
int currentY = 0;
......@@ -46,30 +46,6 @@ public class Candidate {
return vertexList;
}
public void mutateDir(int mutationPlace, int mutation) {
outgoingDirection[mutationPlace] = mutation;
this.vertexList = constructVertexes();
}
public void crossover(Candidate partner, int crossoverPlace) {
// Save this gene for a moment while skipping the part that stays
int[] originalDirections = new int[outgoingDirection.length];
for (int i = crossoverPlace; i < outgoingDirection.length; i++) {
originalDirections[i] = outgoingDirection[i];
}
// Edit these directions
for (int i = crossoverPlace; i < outgoingDirection.length; i++) {
outgoingDirection[i] = partner.outgoingDirection[i];
}
// Edit partners directions
for (int i = crossoverPlace; i < outgoingDirection.length; i++) {
partner.outgoingDirection[i] = originalDirections[i];
}
}
@Override
public String toString() {
return "Canidate{" +
......
......@@ -35,7 +35,21 @@ public class Crossover<T extends Enum<?>> implements Mutator {
if (this.crossoverChance > this.rand.nextDouble()) {
int crossoverPartner = this.rand.nextInt(populationSize);
int crossoverPlace = this.rand.nextInt(proteinLength);
candidate.crossover(population[crossoverPartner], crossoverPlace);
int[] originalDirections = new int[candidate.outgoingDirection.length];
for (int i = crossoverPlace; i < candidate.outgoingDirection.length; i++) {
originalDirections[i] = candidate.outgoingDirection[i];
}
// Edit these directions
for (int i = crossoverPlace; i < candidate.outgoingDirection.length; i++) {
candidate.outgoingDirection[i] = population[crossoverPartner].outgoingDirection[i];
}
// Edit partners directions
for (int i = crossoverPlace; i < candidate.outgoingDirection.length; i++) {
population[crossoverPartner].outgoingDirection[i] = originalDirections[i];
}
}
}
}
......
......@@ -35,9 +35,11 @@ public class SinglePoint<T extends Enum<?>> implements Mutator {
int mutationPlace = this.rand.nextInt(proteinLength);
// TODO: Use the enums or get rid of this hard coded 3 and 4...
if (this.isFRL) {
candidate.mutateDir(mutationPlace, this.rand.nextInt(3));
candidate.outgoingDirection[mutationPlace] = this.rand.nextInt(3);
candidate.vertexList = candidate.constructVertexes();
} else {
candidate.mutateDir(mutationPlace, this.rand.nextInt(4));
candidate.outgoingDirection[mutationPlace] = this.rand.nextInt(4);
candidate.vertexList = candidate.constructVertexes();
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment