docs: def tournament_selection(population,
                           fitnesses,
                           num_competitors=2,
                           diversity_weight=0.0):
      """Create a list of parents with tournament selection.
  
      Args:
          population: A list of solutions.
          fitnesses: A list of fitness values corresponding to solutions in population.
          num_competitors: Number of solutions to compare every round.
              Best solution among competitors is selected.
          diversity_weight: Weight of diversity metric.
              Determines how frequently diversity is used to select tournament winners.
              Note that fitness is given a weight of 1.0.
              diversity_weight == 1.0 gives equal weight to diversity and fitness.
      """
      # Optimization if diversity factor is disabled
      if diversity_weight <= 0.0:
          fitness_pop = zip(fitnesses,
                            population)  # Zip for easy fitness comparison
  
          # Get num_competitors random chromosomes, then add best to result,
          # by taking max fitness and getting chromosome from tuple.
          # Repeat until full.
          return [
              max(random.sample(fitness_pop, num_competitors))[1]
              for _ in range(len(population))
          ]
      else:
          indices = range(len(population))
  
          # Select tournament winners by either max fitness or diversity.
          # The metric to check is randomly selected, weighted by diversity_weight.
          # diversity_metric is calculated between the given solution,
          # and the list of all currently selected solutions.
          selected_solutions = []
          # Select as many solutions are there are in population
          for _ in range(len(population)):
              competitor_indices = random.sample(indices, num_competitors)
  
              # Select by either fitness or diversity,
              # Selected by weighted random selection
              # NOTE: We assume fitness has a weight of 1.0
              if random.uniform(0.0, 1.0) < (1.0 / (1.0 + diversity_weight)):
                  # Fitness
                  selected_solutions.append(
                      max(
                          zip([fitnesses[i] for i in competitor_indices],
                              [population[i] for i in competitor_indices]))[-1])
              else:
                  # Diversity
                  # Break ties by fitness
                  selected_solutions.append(
                      max(
                          zip([
                              _diversity_metric(population[i], selected_solutions
                                                ) for i in competitor_indices
                          ], [fitnesses[i] for i in competitor_indices],
                              [population[i] for i in competitor_indices]))[-1])
  
          return selected_solutions