code: def main(verbose=True):
      NPARTICLES = 100
      RS = (BOUNDS[1] - BOUNDS[0]) / (
          50 ** (1.0 / NDIM)
      )  # between 1/20 and 1/10 of the domain's range
      PMAX = 10
      RCLOUD = 1.0  # 0.5 times the move severity
  
      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",
          "nswarm",
          "evals",
          "error",
          "offline_error",
          "avg",
          "max",
      )
  
      swarm = toolbox.swarm(n=NPARTICLES)
  
      generation = 0
      while mpb.nevals < 5e5:
          # Evaluate each particle in the swarm
          for part in swarm:
              part.fitness.values = toolbox.evaluate(part)
              if not part.best or part.bestfit < part.fitness:
                  part.best = toolbox.clone(part[:])  # Get the position
                  part.bestfit.values = (
                      part.fitness.values
                  )  # Get the fitness
  
          # Sort swarm into species, best individual comes first
          sorted_swarm = sorted(
              swarm, key=lambda ind: ind.bestfit, reverse=True
          )
          species = []
          while sorted_swarm:
              found = False
              for s in species:
                  dist = math.sqrt(
                      sum(
                          (x1 - x2) ** 2
                          for x1, x2 in zip(
                              sorted_swarm[0].best, s[0].best
                          )
                      )
                  )
                  if dist <= RS:
                      found = True
                      s.append(sorted_swarm[0])
                      break
              if not found:
                  species.append([sorted_swarm[0]])
              sorted_swarm.pop(0)
  
          record = stats.compile(swarm)
          logbook.record(
              gen=generation,
              evals=mpb.nevals,
              nswarm=len(species),
              error=mpb.currentError(),
              offline_error=mpb.offlineError(),
              **record,
          )
  
          if verbose:
              print(logbook.stream)
  
          # Detect change
          if any(
              s[0].bestfit.values != toolbox.evaluate(s[0].best)
              for s in species
          ):
              # Convert particles to quantum particles
              for s in species:
                  s[:] = toolbox.convert(
                      s, rcloud=RCLOUD, centre=s[0].best
                  )
  
          else:
              # Replace exceeding particles in a species with new particles
              for s in species:
                  if len(s) > PMAX:
                      n = len(s) - PMAX
                      del s[PMAX:]
                      s.extend(toolbox.swarm(n=n))
  
              # Update particles that have not been reinitialized
              for s in species[:-1]:
                  for part in s[:PMAX]:
                      toolbox.update(part, s[0].best)
                      del part.fitness.values
  
          # Return all but the worst species' updated particles to the swarm
          # The worst species is replaced by new particles
          swarm = list(
              itertools.chain(
                  toolbox.swarm(n=len(species[-1])), *species[:-1]
              )
          )
          generation += 1
  
file_path: deap/examples/pso/speciation.py
imports: 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 4 for calling a global function with example usage: main(verbose) without return types