code: def main(verbose=True):
      NPOP = 10  # Should be equal to the number of peaks
      CR = 0.6
      F = 0.4
      regular, brownian = 4, 2
  
      stats = tools.Statistics(lambda ind: ind.fitness.values)
      stats.register("avg", numpy.mean)
      stats.register("std", numpy.std)
      stats.register("min", numpy.min)
      stats.register("max", numpy.max)
  
      logbook = tools.Logbook()
      logbook.header = (
          "gen",
          "evals",
          "error",
          "offline_error",
          "avg",
          "max",
      )
  
      # Initialize populations
      populations = [
          toolbox.population(n=regular + brownian) for _ in range(NPOP)
      ]
  
      # Evaluate the individuals
      for idx, subpop in enumerate(populations):
          fitnesses = toolbox.map(toolbox.evaluate, subpop)
          for ind, fit in zip(subpop, fitnesses):
              ind.fitness.values = fit
  
      record = stats.compile(itertools.chain(*populations))
      logbook.record(
          gen=0,
          evals=mpb.nevals,
          error=mpb.currentError(),
          offline_error=mpb.offlineError(),
          **record,
      )
      if verbose:
          print(logbook.stream)
  
      g = 1
      while mpb.nevals < 5e5:
          # Detect a change and invalidate fitnesses if necessary
          bests = [toolbox.best(subpop)[0] for subpop in populations]
          if any(
              b.fitness.values != toolbox.evaluate(b) for b in bests
          ):
              for individual in itertools.chain(*populations):
                  del individual.fitness.values
  
          # Apply exclusion
          rexcl = (BOUNDS[1] - BOUNDS[0]) / (2 * NPOP ** (1.0 / NDIM))
          for i, j in itertools.combinations(range(NPOP), 2):
              if bests[i].fitness.valid and bests[j].fitness.valid:
                  d = sum(
                      (bests[i][k] - bests[j][k]) ** 2
                      for k in range(NDIM)
                  )
                  d = math.sqrt(d)
  
                  if d < rexcl:
                      if bests[i].fitness < bests[j].fitness:
                          k = i
                      else:
                          k = j
  
                      populations[k] = toolbox.population(
                          n=regular + brownian
                      )
  
          # Evaluate the individuals with an invalid fitness
          invalid_ind = [
              ind
              for ind in itertools.chain(*populations)
              if not ind.fitness.valid
          ]
          fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
          for ind, fit in zip(invalid_ind, fitnesses):
              ind.fitness.values = fit
  
          record = stats.compile(itertools.chain(*populations))
          logbook.record(
              gen=g,
              evals=mpb.nevals,
              error=mpb.currentError(),
              offline_error=mpb.offlineError(),
              **record,
          )
          if verbose:
              print(logbook.stream)
  
          # Evolve the sub-populations
          for idx, subpop in enumerate(populations):
              newpop = []
              (xbest,) = toolbox.best(subpop)
              # Apply regular DE to the first part of the population
              for individual in subpop[:regular]:
                  x1, x2, x3, x4 = toolbox.select(subpop)
                  offspring = toolbox.clone(individual)
                  index = random.randrange(NDIM)
                  for i, value in enumerate(individual):
                      if i == index or random.random() < CR:
                          offspring[i] = xbest[i] + F * (
                              x1[i] + x2[i] - x3[i] - x4[i]
                          )
                  offspring.fitness.values = toolbox.evaluate(offspring)
                  if offspring.fitness >= individual.fitness:
                      newpop.append(offspring)
                  else:
                      newpop.append(individual)
  
              # Apply Brownian to the last part of the population
              newpop.extend(
                  toolbox.brownian_individual(xbest)
                  for _ in range(brownian)
              )
  
              # Evaluate the brownian individuals
              for individual in newpop[-brownian:]:
                  individual.fitness.value = toolbox.evaluate(
                      individual
                  )
  
              # Replace the population
              populations[idx] = newpop
  
          g += 1
  
      return logbook
  
file_path: deap/examples/de/dynamic.py
imports: import array
  import itertools
  import math
  import operator
  import random
  import numpy
  from deap import base
  from deap.benchmarks import movingpeaks
  from deap import creator
  from deap import tools
label_desc: Use image node_id 2 for calling a global function with example usage: main(verbose) and returns: logbook