diff options
Diffstat (limited to 'src/jcgp/backend/modules/es/TournamentSelection.java')
-rw-r--r-- | src/jcgp/backend/modules/es/TournamentSelection.java | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java index 209caca..6d467f5 100644 --- a/src/jcgp/backend/modules/es/TournamentSelection.java +++ b/src/jcgp/backend/modules/es/TournamentSelection.java @@ -44,7 +44,7 @@ public class TournamentSelection extends EvolutionaryStrategy { * @param resources a reference to the experiment's resources. */ public TournamentSelection(final Resources resources) { - super(); + super(resources); tournamentSize = new IntegerParameter(1, "Tournament size") { @Override public void validate(Number newValue) { @@ -73,17 +73,22 @@ public class TournamentSelection extends EvolutionaryStrategy { } @Override - public void evolve(Population population, Mutator mutator, Resources resources) { + public void evolve(Population population, Mutator mutator) { /* Create an entirely new population by isolating random subsets of * the original population and choosing the fittest individual within * that subset. Each chosen individual is mutated and copied back into the * population. */ - Chromosome[] newPopulation = new Chromosome[resources.populationSize()]; + + // sort the population by fitness to make things easier + population.sort(); + + // this array holds the new population temporarily, until it is copied over + Chromosome[] newPopulation = new Chromosome[getResources().populationSize()]; // start by selecting all of the chromosomes that will be promoted - for (int i = 0; i < resources.populationSize(); i++) { - if (report.get()) resources.reportln("[ES] Starting tournament " + i); + for (int i = 0; i < getResources().populationSize(); i++) { + if (report.get()) getResources().reportln("[ES] Starting tournament " + i); /* the population is sorted in ascending order of fitness, * meaning the higher the index of the contender, the fitter @@ -91,21 +96,21 @@ public class TournamentSelection extends EvolutionaryStrategy { */ int[] contenders = new int[tournamentSize.get()]; for (int t = 0; t < tournamentSize.get() - 1; t++) { - contenders[t] = resources.getRandomInt(resources.populationSize()); + contenders[t] = getResources().getRandomInt(getResources().populationSize()); } - if (report.get()) resources.reportln("[ES] Selected contenders: " + Arrays.toString(contenders)); + if (report.get()) getResources().reportln("[ES] Selected contenders: " + Arrays.toString(contenders)); Arrays.sort(contenders); - if (report.get()) resources.reportln("[ES] Chr " + contenders[contenders.length - 1] + " wins the tournament, copying and mutating..."); + if (report.get()) getResources().reportln("[ES] Chr " + contenders[contenders.length - 1] + " wins the tournament, copying and mutating..."); // create a copy of the selected chromosome and mutate it - newPopulation[i] = new Chromosome(population.getChromosome(contenders[contenders.length - 1])); - mutator.mutate(newPopulation[i], resources); + newPopulation[i] = new Chromosome(population.get(contenders[contenders.length - 1])); + mutator.mutate(newPopulation[i]); } - if (report.get()) resources.reportln("[ES] Tournaments are finished, copying new chromosomes into population"); + if (report.get()) getResources().reportln("[ES] Tournaments are finished, copying new chromosomes into population"); // newPopulation has been generated, copy into the population - for (int c = 0; c < resources.populationSize(); c++) { - population.getChromosome(c).copyGenes(newPopulation[c]); + for (int c = 0; c < getResources().populationSize(); c++) { + population.get(c).copyGenes(newPopulation[c]); } - if (report.get()) resources.reportln("[ES] Generation is complete"); + if (report.get()) getResources().reportln("[ES] Generation is complete"); } } |