Population pop = new BasicPopulation();
Species species = pop.createSpecies();
// Create 1000 genomes, assign the score to be the index number.
for (int i = 0; i < 1000; i++) {
Genome genome = new IntegerArrayGenome(1);
genome.setScore(i);
genome.setAdjustedScore(i);
pop.getSpecies().get(0).add(genome);
}
GenerateRandom rnd = new MersenneTwisterGenerateRandom();
// Create a trainer with a very simple score function. We do not care
// about the calculation of the score, as they will never be calculated.
// We only care that we are maximizing.
EvolutionaryAlgorithm train = new BasicEA(pop, new ScoreFunction() {
@Override
public double calculateScore(MLMethod method) {
return 0;
}
@Override
public boolean shouldMinimize() {
return false;
}
});
// Perform the test for round counts between 1 and 10.
for (int roundCount = 1; roundCount <= 10; roundCount++) {
TournamentSelection selection = new TournamentSelection(train, roundCount);
int sum = 0;
int count = 0;
for (int i = 0; i < 100000; i++) {
int genomeID = selection.performSelection(rnd, species);
Genome genome = species.getMembers().get(genomeID);
sum += genome.getAdjustedScore();
count++;
}
sum /= count;
System.out.println("Rounds: " + roundCount + ", Avg Score: " + sum);
}