diff --git a/src/main/java/MainClasses/GeneticAlgorithm.java b/src/main/java/MainClasses/GeneticAlgorithm.java index 98840fa775ce875333f63e7b49bd00ba65ced474..fb2725b93bef99e62d91f0422064c11958b15cfb 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 e82b9fd78723bbf25ea2d50dcdb3ecb754a396ee..bf16a39b6251b3601c4064e969b65da99f0c86c9 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;