code: def _fast_non_dominated_sort(
      population: List[
          "multi_objective.trial.FrozenMultiObjectiveTrial"
      ],
      directions: List[optuna.study.StudyDirection],
  ) -> List[List["multi_objective.trial.FrozenMultiObjectiveTrial"]]:
      dominated_count: DefaultDict[int, int] = defaultdict(int)
      dominates_list = defaultdict(list)
  
      for p, q in itertools.combinations(population, 2):
          if p._dominates(q, directions):
              dominates_list[p.number].append(q.number)
              dominated_count[q.number] += 1
          elif q._dominates(p, directions):
              dominates_list[q.number].append(p.number)
              dominated_count[p.number] += 1
  
      population_per_rank = []
      while population:
          non_dominated_population = []
          i = 0
          while i < len(population):
              if dominated_count[population[i].number] == 0:
                  individual = population[i]
                  if i == len(population) - 1:
                      population.pop()
                  else:
                      population[i] = population.pop()
                  non_dominated_population.append(individual)
              else:
                  i += 1
  
          for x in non_dominated_population:
              for y in dominates_list[x.number]:
                  dominated_count[y] -= 1
  
          assert non_dominated_population
          population_per_rank.append(non_dominated_population)
  
      return population_per_rank
  
imports: from collections import defaultdict
  import hashlib
  import itertools
  from typing import Any
  from typing import cast
  from typing import DefaultDict
  from typing import Dict
  from typing import List
  from typing import Optional
  from typing import Sequence
  from typing import Tuple
  import optuna
  from optuna import multi_objective
  from optuna._deprecated import deprecated_class
  from optuna.distributions import BaseDistribution
  from optuna.multi_objective.samplers import BaseMultiObjectiveSampler
  from optuna.samplers._lazy_random_state import LazyRandomState
label_desc: Use image node_id 1 for calling a global function with example usage: _fast_non_dominated_sort(population, directions) and returns: population_per_rank