code: def _preserve_niche_individuals(
      target_population_size: int,
      elite_population_num: int,
      population: list[FrozenTrial],
      closest_reference_points: np.ndarray,
      distance_reference_points: np.ndarray,
      rng: np.random.RandomState,
  ) -> list[FrozenTrial]:
      """Determine who survives form the borderline front.
  
      Who survive form the borderline front is determined according to the sparsity of each closest
      reference point. The algorithm picks a reference point from those who have the least neighbors
      in elite population and adds one of borderline front member who has the same closest reference
      point.
  
      Args:
          target_population_size:
              The number of individuals to select.
          elite_population_num:
              The number of individuals which are already selected as the elite population.
          population:
              List of all the trials in the current surviving generation.
          distance_reference_points:
              A ``numpy.ndarray`` with rows of generation size. Each row is the distance from the
              corresponding individual to the closest reference point.
          closest_reference_points:
              A ``numpy.ndarray`` with rows of generation size. Each row is the index of the closest
              reference point to the corresponding individual.
          rng:
              Random number generator.
  
      Returns:
          A list of trials which are selected as the next generation.
      """
      if len(population) < target_population_size:
          raise ValueError(
              "The population size must be greater than or equal to the target population size."
          )
  
      # reference_point_to_borderline_population keeps pairs of a neighbor and the distance of
      # each reference point from borderline front population.
      reference_point_to_borderline_population = defaultdict(list)
      for i, reference_point_idx in enumerate(
          closest_reference_points[elite_population_num:]
      ):
          population_idx = i + elite_population_num
          reference_point_to_borderline_population[
              reference_point_idx
          ].append((distance_reference_points[population_idx], i))
  
      # reference_points_to_elite_population_count keeps how many elite neighbors each reference
      # point has.
      reference_point_to_elite_population_count: dict[
          int, int
      ] = defaultdict(int)
      for i, reference_point_idx in enumerate(
          closest_reference_points[:elite_population_num]
      ):
          reference_point_to_elite_population_count[
              reference_point_idx
          ] += 1
      # nearest_points_count_to_reference_points classifies reference points which have at least one
      # closest borderline population member by the number of elite neighbors they have.  Each key
      # corresponds to the number of elite neighbors and the value to the reference point indices.
      nearest_points_count_to_reference_points = defaultdict(list)
      for (
          reference_point_idx
      ) in reference_point_to_borderline_population:
          elite_population_count = (
              reference_point_to_elite_population_count[
                  reference_point_idx
              ]
          )
          nearest_points_count_to_reference_points[
              elite_population_count
          ].append(reference_point_idx)
  
      count = -1
      additional_elite_population: list[FrozenTrial] = []
      is_shuffled: defaultdict[int, bool] = defaultdict(bool)
      while len(additional_elite_population) < target_population_size:
          if len(nearest_points_count_to_reference_points[count]) == 0:
              count += 1
              rng.shuffle(
                  nearest_points_count_to_reference_points[count]
              )
              continue
  
          reference_point_idx = (
              nearest_points_count_to_reference_points[count].pop()
          )
          if count > 0 and not is_shuffled[reference_point_idx]:
              rng.shuffle(
                  reference_point_to_borderline_population[
                      reference_point_idx
                  ]
              )
              is_shuffled[reference_point_idx] = True
          elif count == 0:
              reference_point_to_borderline_population[
                  reference_point_idx
              ].sort(reverse=True)
  
          (
              _,
              selected_individual_id,
          ) = reference_point_to_borderline_population[
              reference_point_idx
          ].pop()
          additional_elite_population.append(
              population[selected_individual_id]
          )
          if reference_point_to_borderline_population[
              reference_point_idx
          ]:
              nearest_points_count_to_reference_points[
                  count + 1
              ].append(reference_point_idx)
  
      return additional_elite_population
  
imports: from __future__ import annotations
  from collections import defaultdict
  from collections.abc import Callable
  from collections.abc import Sequence
  import itertools
  import math
  import numpy
  from optuna.samplers._lazy_random_state import LazyRandomState
  from optuna.samplers.nsgaii._dominates import _constrained_dominates
  from optuna.samplers.nsgaii._dominates import _validate_constraints
  from optuna.samplers.nsgaii._elite_population_selection_strategy import _fast_non_dominated_sort
  from optuna.study import Study
  from optuna.study._multi_objective import _dominates
  from optuna.trial import FrozenTrial
label_desc: Use image node_id 6 for calling a global function with example usage: _preserve_niche_individuals(target_population_size, elite_population_num, population, closest_reference_points, distance_reference_points, rng) and returns: additional_elite_population