HashSet<RobNeural> visited = new HashSet<RobNeural>();
LinkedList<RobNeural> tournament;
while (visited.size() <= population.size() - tournamentSize) {
tournament = new LinkedList<RobNeural>();
RobNeural bestRob = null;
double bestFit = Double.NEGATIVE_INFINITY;
while (tournament.size() < tournamentSize) {
RobNeural rob = population.get(rand.nextInt(population.size()));
if (!visited.contains(rob)) {
tournament.add(rob);
visited.add(rob);
if (rob.getFitness() > bestFit) {
bestFit = rob.getFitness();
bestRob = rob;
}
}
}
LinkedList<Thread> threads = new LinkedList<Thread>();
for (RobNeural rob : tournament) {
LinkedList<BigDecimal> genomeTrans = null;
if (bestRob.getGenomeTrans() != null) {
genomeTrans = new LinkedList<>(bestRob.getGenomeTrans());
}
threads.add(rob.setController(
new LinkedList<BigDecimal>(bestRob.getGenome()),
genomeTrans,
bestRob.getRand(),
true));
}
StaticMethods.joinThreads(threads);
for (RobNeural rob : tournament) {
rob.setFitness(bestRob.getFitness());
}
}
}