code: def selNSGA2(individuals, k, nd="standard"):
      """Apply NSGA-II selection operator on the *individuals*. Usually, the
      size of *individuals* will be larger than *k* because any individual
      present in *individuals* will appear in the returned list at most once.
      Having the size of *individuals* equals to *k* will have no effect other
      than sorting the population according to their front rank. The
      list returned contains references to the input *individuals*. For more
      details on the NSGA-II operator see [Deb2002]_.
  
      :param individuals: A list of individuals to select from.
      :param k: The number of individuals to select.
      :param nd: Specify the non-dominated algorithm to use: 'standard' or 'log'.
      :returns: A list of selected individuals.
  
      .. [Deb2002] Deb, Pratab, Agarwal, and Meyarivan, "A fast elitist
         non-dominated sorting genetic algorithm for multi-objective
         optimization: NSGA-II", 2002.
      """
      if nd == "standard":
          pareto_fronts = sortNondominated(individuals, k)
      elif nd == "log":
          pareto_fronts = sortLogNondominated(individuals, k)
      else:
          raise Exception(
              "selNSGA2: The choice of non-dominated sorting "
              'method "{0}" is invalid.'.format(nd)
          )
  
      for front in pareto_fronts:
          assignCrowdingDist(front)
  
      chosen = list(chain(*pareto_fronts[:-1]))
      k = k - len(chosen)
      if k > 0:
          sorted_front = sorted(
              pareto_fronts[-1],
              key=attrgetter("fitness.crowding_dist"),
              reverse=True,
          )
          chosen.extend(sorted_front[:k])
  
      return chosen
  
imports: import bisect
  from collections import defaultdict, namedtuple
  from itertools import chain
  import math
  from operator import attrgetter, itemgetter
  import random
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: selNSGA2(individuals, k, nd) and returns: chosen