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

Refactored the FitnessProportional selector.

parent c6fcb4a7
Branches
No related tags found
No related merge requests found
...@@ -122,7 +122,7 @@ public class GeneticAlgorithm { ...@@ -122,7 +122,7 @@ public class GeneticAlgorithm {
} }
if (config.getSelectionMethod().equals(SelectionMethods.Proportional)) { if (config.getSelectionMethod().equals(SelectionMethods.Proportional)) {
this.selector = new FitnessProportional(this.rand, this.isHydrophobic); this.selector = new FitnessProportional(this.config, this.rand);
} else if (config.getSelectionMethod().equals(SelectionMethods.Tournament)) { } else if (config.getSelectionMethod().equals(SelectionMethods.Tournament)) {
this.selector = new Tournament(this.rand, this.isHydrophobic, config.getK()); this.selector = new Tournament(this.rand, this.isHydrophobic, config.getK());
} else if (config.getSelectionMethod().equals(SelectionMethods.OnlyBest)) { } else if (config.getSelectionMethod().equals(SelectionMethods.OnlyBest)) {
......
...@@ -3,37 +3,40 @@ package Selectors; ...@@ -3,37 +3,40 @@ package Selectors;
import Interfaces.Selector; import Interfaces.Selector;
import MainClasses.Candidate; import MainClasses.Candidate;
import MainClasses.Config;
import java.util.Random; import java.util.Random;
public class FitnessProportional implements Selector { public class FitnessProportional implements Selector {
Random rand; Random rand;
int[] isHydrophobic; Config config;
public FitnessProportional(Random rand, int[] isHydrophobic) { public FitnessProportional(Config config, Random rand) {
this.rand = rand; this.rand = rand;
this.isHydrophobic = isHydrophobic; this.config = config;
} }
@Override @Override
public Candidate[] selectNewPopulation(Candidate[] population, double[] fitness, double totalFitness) { public Candidate[] selectNewPopulation(Candidate[] generation) {
int populationSize = population.length; int populationSize = generation.length;
// Pick set for next generation double totalFitness = 0.0;
double[] proportionalFitness = new double[populationSize]; for (Candidate candidate: generation) {
for (int i = 0; i < populationSize; i++) { totalFitness += candidate.getFitness();
proportionalFitness[i] = fitness[i] / totalFitness;
} }
Candidate[] newPopulation = new Candidate[populationSize]; Candidate[] newPopulation = new Candidate[populationSize];
for (int i = 0; i < populationSize; i++) { for (int i = 0; i < populationSize; i++) {
double picked = rand.nextDouble(); //Select a number between 0 and the maximum fitness
int j = -1; double requiredFitness = rand.nextDouble() * totalFitness;
while (picked > 0) { double currentFitness = 0.0;
j++; for (Candidate candidate : generation) {
picked -= proportionalFitness[j]; currentFitness += candidate.getFitness();
if(currentFitness >= requiredFitness) {
newPopulation[i] = candidate;
break;
}
} }
newPopulation[i] = new Candidate(population[j].getFolding());
} }
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