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

Refactored the OnlyBest selector

parent 5184f236
No related branches found
No related tags found
No related merge requests found
...@@ -126,7 +126,7 @@ public class GeneticAlgorithm { ...@@ -126,7 +126,7 @@ public class GeneticAlgorithm {
} else if (config.getSelectionMethod().equals(SelectionMethods.Tournament)) { } else if (config.getSelectionMethod().equals(SelectionMethods.Tournament)) {
this.selector = new Tournament(this.config, this.rand); this.selector = new Tournament(this.config, this.rand);
} else if (config.getSelectionMethod().equals(SelectionMethods.OnlyBest)) { } else if (config.getSelectionMethod().equals(SelectionMethods.OnlyBest)) {
this.selector = new OnlyBest(this.isHydrophobic); this.selector = new OnlyBest();
} }
} }
......
...@@ -7,31 +7,21 @@ import java.util.Random; ...@@ -7,31 +7,21 @@ import java.util.Random;
public class OnlyBest implements Selector { public class OnlyBest implements Selector {
int[] isHydrophobic; public OnlyBest() {
public OnlyBest(int[] isHydrophobic) {
this.isHydrophobic = isHydrophobic;
} }
@Override @Override
public Candidate[] selectNewPopulation(Candidate[] population, double[] fitness, double totalFitness) { public Candidate[] selectNewPopulation(Candidate[] generation) {
int populationSize = population.length; Candidate bestCandidate = generation[0];
for (Candidate candidate: generation) {
int bestIndex = 0; if (candidate.getFitness() >= bestCandidate.getFitness()) {
double bestFitness = 0; bestCandidate = candidate;
for (int i = 0; i < populationSize; i++) {
if (fitness[i] >= bestFitness) {
bestFitness = fitness[i];
bestIndex = i;
} }
} }
Candidate[] newPopulation = new Candidate[populationSize]; Candidate[] newPopulation = new Candidate[generation.length];
int[] bestFolding = population[bestIndex].getFolding(); for (int i = 0; i < generation.length; i++) {
newPopulation[i] = new Candidate(bestCandidate.getFolding());
for (int i = 0; i < populationSize; i++) {
newPopulation[i] = new Candidate(bestFolding);
} }
return newPopulation; return newPopulation;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment