def selNSGA2(individuals, k, nd='standard'):
 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

def selNSGA2 ( individuals , k , nd = 'standard' ) : 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
apply nsga-ii selection operator on the *individuals* .