code: def main(seed=None):
      random.seed(seed)
  
      NGEN = 250
      MU = 100
      CXPB = 0.9
  
      stats = tools.Statistics(lambda ind: ind.fitness.values)
      # stats.register("avg", numpy.mean, axis=0)
      # stats.register("std", numpy.std, axis=0)
      stats.register("min", numpy.min, axis=0)
      stats.register("max", numpy.max, axis=0)
  
      logbook = tools.Logbook()
      logbook.header = "gen", "evals", "std", "min", "avg", "max"
  
      pop = toolbox.population(n=MU)
  
      # Evaluate the individuals with an invalid fitness
      invalid_ind = [ind for ind in pop if not ind.fitness.valid]
      fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
      for ind, fit in zip(invalid_ind, fitnesses):
          ind.fitness.values = fit
  
      # This is just to assign the crowding distance to the individuals
      # no actual selection is done
      pop = toolbox.select(pop, len(pop))
  
      record = stats.compile(pop)
      logbook.record(gen=0, evals=len(invalid_ind), **record)
      print(logbook.stream)
  
      # Begin the generational process
      for gen in range(1, NGEN):
          # Vary the population
          offspring = tools.selTournamentDCD(pop, len(pop))
          offspring = [toolbox.clone(ind) for ind in offspring]
  
          for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
              if random.random() <= CXPB:
                  toolbox.mate(ind1, ind2)
  
              toolbox.mutate(ind1)
              toolbox.mutate(ind2)
              del ind1.fitness.values, ind2.fitness.values
  
          # Evaluate the individuals with an invalid fitness
          invalid_ind = [
              ind for ind in offspring if not ind.fitness.valid
          ]
          fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
          for ind, fit in zip(invalid_ind, fitnesses):
              ind.fitness.values = fit
  
          # Select the next generation population
          pop = toolbox.select(pop + offspring, MU)
          record = stats.compile(pop)
          logbook.record(gen=gen, evals=len(invalid_ind), **record)
          print(logbook.stream)
  
      print(
          "Final population hypervolume is %f"
          % hypervolume(pop, [11.0, 11.0])
      )
  
      return pop, logbook
  
file_path: deap/examples/ga/nsga2.py
imports: import array
  import random
  import json
  import numpy
  from math import sqrt
  from deap import algorithms
  from deap import base
  from deap import benchmarks
  from deap.benchmarks.tools import diversity, convergence, hypervolume
  from deap import creator
  from deap import tools
label_desc: Use image node_id 2 for calling a global function with example usage: main(seed) and returns: pop,  logbook