code: def _calc_crowding_distance(
      population: list[FrozenTrial],
  ) -> defaultdict[int, float]:
      """Calculates the crowding distance of population.
  
      We define the crowding distance as the summation of the crowding distance of each dimension
      of value calculated as follows:
  
      * If all values in that dimension are the same, i.e., [1, 1, 1] or [inf, inf],
        the crowding distances of all trials in that dimension are zero.
      * Otherwise, the crowding distances of that dimension is the difference between
        two nearest values besides that value, one above and one below, divided by the difference
        between the maximal and minimal finite value of that dimension. Please note that:
          * the nearest value below the minimum is considered to be -inf and the
            nearest value above the maximum is considered to be inf, and
          * inf - inf and (-inf) - (-inf) is considered to be zero.
      """
  
      manhattan_distances: defaultdict[int, float] = defaultdict(float)
      if len(population) == 0:
          return manhattan_distances
  
      for i in range(len(population[0].values)):
          population.sort(key=lambda x: x.values[i])
  
          # If all trials in population have the same value in the i-th dimension, ignore the
          # objective dimension since it does not make difference.
          if population[0].values[i] == population[-1].values[i]:
              continue
  
          vs = (
              [-float("inf")]
              + [trial.values[i] for trial in population]
              + [float("inf")]
          )
  
          # Smallest finite value.
          v_min = next(x for x in vs if x != -float("inf"))
  
          # Largest finite value.
          v_max = next(x for x in reversed(vs) if x != float("inf"))
  
          width = v_max - v_min
          if width <= 0:
              # width == 0 or width == -inf
              width = 1.0
  
          for j in range(len(population)):
              # inf - inf and (-inf) - (-inf) is considered to be zero.
              gap = 0.0 if vs[j] == vs[j + 2] else vs[j + 2] - vs[j]
              manhattan_distances[population[j].number] += gap / width
      return manhattan_distances
  
file_path: optuna/optuna/samplers/nsgaii/_elite_population_selection_strategy.py
imports: from __future__ import annotations
  from collections import defaultdict
  from collections.abc import Callable
  from collections.abc import Sequence
  import itertools
  import optuna
  from optuna.samplers.nsgaii._dominates import _constrained_dominates
  from optuna.samplers.nsgaii._dominates import _validate_constraints
  from optuna.study import Study
  from optuna.study._multi_objective import _dominates
  from optuna.trial import FrozenTrial
label_desc: Use image node_id 1 for calling a global function with example usage: _calc_crowding_distance(population) and returns: manhattan_distances, manhattan_distances