From 5184f236181360923a4665bf018b00862e8af9bb Mon Sep 17 00:00:00 2001 From: Lennart Eichhorn <lennart@madmanfred.com> Date: Sun, 31 May 2020 17:12:02 +0200 Subject: [PATCH] Refactored the Tournament selector. --- .../java/MainClasses/GeneticAlgorithm.java | 2 +- src/main/java/Selectors/Tournament.java | 35 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/MainClasses/GeneticAlgorithm.java b/src/main/java/MainClasses/GeneticAlgorithm.java index 98840fa..fb2725b 100644 --- a/src/main/java/MainClasses/GeneticAlgorithm.java +++ b/src/main/java/MainClasses/GeneticAlgorithm.java @@ -124,7 +124,7 @@ public class GeneticAlgorithm { if (config.getSelectionMethod().equals(SelectionMethods.Proportional)) { this.selector = new FitnessProportional(this.config, this.rand); } else if (config.getSelectionMethod().equals(SelectionMethods.Tournament)) { - this.selector = new Tournament(this.rand, this.isHydrophobic, config.getK()); + this.selector = new Tournament(this.config, this.rand); } else if (config.getSelectionMethod().equals(SelectionMethods.OnlyBest)) { this.selector = new OnlyBest(this.isHydrophobic); } diff --git a/src/main/java/Selectors/Tournament.java b/src/main/java/Selectors/Tournament.java index e82b9fd..bf16a39 100644 --- a/src/main/java/Selectors/Tournament.java +++ b/src/main/java/Selectors/Tournament.java @@ -3,39 +3,32 @@ package Selectors; import Interfaces.Selector; import MainClasses.Candidate; +import MainClasses.Config; import java.util.Random; public class Tournament implements Selector { Random rand; - int[] isHydrophobic; - int k; + Config config; - public Tournament(Random rand, int[] isHydrophobic, int k) { + public Tournament(Config config, Random rand) { this.rand = rand; - this.isHydrophobic = isHydrophobic; - this.k = k; + this.config = config; } @Override - public Candidate[] selectNewPopulation(Candidate[] population, double[] fitness, double totalFitness) { - int populationSize = population.length; - - Candidate[] newPopulation = new Candidate[populationSize]; - double tournamentScoreMax = 0; - int tournamentChoosenIndex = 0; - - for (int i = 0; i < populationSize; i++) { - tournamentScoreMax = 0; - for (int ik = 0; ik < this.k; ik++) { - int nextIndex = rand.nextInt(populationSize); - double nextScore = fitness[nextIndex]; - if (tournamentScoreMax < nextScore){ - tournamentScoreMax = nextScore; - tournamentChoosenIndex = nextIndex; + public Candidate[] selectNewPopulation(Candidate[] generation) { + + Candidate[] newPopulation = new Candidate[generation.length]; + for (int i = 0; i < generation.length; i++) { + Candidate bestParticipant = generation[rand.nextInt(generation.length)]; + for (int participant = 0; participant < config.getK()-1; participant++) { + Candidate nextParticipant = generation[rand.nextInt(generation.length)]; + if (nextParticipant.getFitness() > bestParticipant.getFitness()){ + bestParticipant = nextParticipant; } } - newPopulation[i] = new Candidate(population[tournamentChoosenIndex].getFolding()); + newPopulation[i] = bestParticipant; } return newPopulation; -- GitLab