getRandomTournamentWinner method

P getRandomTournamentWinner(
  1. List<P> pool
)

Picks two phenotypes from the pool at random, compares them, and returns the one with the better fitness.

TODO: add simulated annealing temperature (probability to pick the worse individual) - but is it needed when we have niching?

Implementation

P getRandomTournamentWinner(List<P> pool) {
  var random = math.Random();
  var first = pool[random.nextInt(pool.length)];
  P second;
  while (true) {
    second = pool[random.nextInt(pool.length)];
    if (second != first) break;
  }
  assert(first.result != null);
  assert(second.result != null);

  if (first.result!.paretoRank < second.result!.paretoRank) {
    return first;
  } else if (first.result!.paretoRank > second.result!.paretoRank) {
    return second;
  }

  assert(!fitnessSharing || first.resultWithFitnessSharingApplied != null);
  assert(!fitnessSharing || second.resultWithFitnessSharingApplied != null);

  if (first.resultWithFitnessSharingApplied != null &&
      second.resultWithFitnessSharingApplied != null) {
    // Fitness sharing was applied. Compare those numbers.
    if (first.resultWithFitnessSharingApplied! <
        second.resultWithFitnessSharingApplied!) {
      return first;
    } else {
      return second;
    }
  }

  if (first.result!.compareTo(second.result!) < 0) {
    return first;
  } else {
    return second;
  }
}