code: def main(verbose=True):
      NSWARMS = 1
      NPARTICLES = 5
      NEXCESS = 3
      RCLOUD = 0.5  # 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",
      )
  
      # Generate the initial population
      population = [toolbox.swarm(n=NPARTICLES) for _ in range(NSWARMS)]
  
      # Evaluate each particle
      for swarm in population:
          for part in swarm:
              part.fitness.values = toolbox.evaluate(part)
  
              # Update swarm's attractors personal best and global best
              if not part.best or part.fitness > part.bestfit:
                  part.best = toolbox.clone(part[:])  # Get the position
                  part.bestfit.values = (
                      part.fitness.values
                  )  # Get the fitness
              if not swarm.best or part.fitness > swarm.bestfit:
                  swarm.best = toolbox.clone(
                      part[:]
                  )  # Get the position
                  swarm.bestfit.values = (
                      part.fitness.values
                  )  # Get the fitness
  
      record = stats.compile(itertools.chain(*population))
      logbook.record(
          gen=0,
          evals=mpb.nevals,
          nswarm=len(population),
          error=mpb.currentError(),
          offline_error=mpb.offlineError(),
          **record,
      )
  
      if verbose:
          print(logbook.stream)
  
      generation = 1
      while mpb.nevals < 5e5:
          # Check for convergence
          rexcl = (BOUNDS[1] - BOUNDS[0]) / (
              2 * len(population) ** (1.0 / NDIM)
          )
  
          not_converged = 0
          worst_swarm_idx = None
          worst_swarm = None
          for i, swarm in enumerate(population):
              # Compute the diameter of the swarm
              for p1, p2 in itertools.combinations(swarm, 2):
                  d = math.sqrt(
                      sum((x1 - x2) ** 2.0 for x1, x2 in zip(p1, p2))
                  )
                  if d > 2 * rexcl:
                      not_converged += 1
                      # Search for the worst swarm according to its global best
                      if (
                          not worst_swarm
                          or swarm.bestfit < worst_swarm.bestfit
                      ):
                          worst_swarm_idx = i
                          worst_swarm = swarm
                      break
  
          # If all swarms have converged, add a swarm
          if not_converged == 0:
              population.append(toolbox.swarm(n=NPARTICLES))
          # If too many swarms are roaming, remove the worst swarm
          elif not_converged > NEXCESS:
              population.pop(worst_swarm_idx)
  
          # Update and evaluate the swarm
          for swarm in population:
              # Check for change
              if (
                  swarm.best
                  and toolbox.evaluate(swarm.best)
                  != swarm.bestfit.values
              ):
                  # Convert particles to quantum particles
                  swarm[:] = toolbox.convert(
                      swarm, rcloud=RCLOUD, centre=swarm.best
                  )
                  swarm.best = None
                  del swarm.bestfit.values
  
              for part in swarm:
                  # Not necessary to update if it is a new swarm
                  # or a swarm just converted to quantum
                  if swarm.best and part.best:
                      toolbox.update(part, swarm.best)
                  part.fitness.values = toolbox.evaluate(part)
  
                  # Update swarm's attractors personal best and global best
                  if not part.best or part.fitness > part.bestfit:
                      part.best = toolbox.clone(part[:])
                      part.bestfit.values = part.fitness.values
                  if not swarm.best or part.fitness > swarm.bestfit:
                      swarm.best = toolbox.clone(part[:])
                      swarm.bestfit.values = part.fitness.values
  
          record = stats.compile(itertools.chain(*population))
          logbook.record(
              gen=generation,
              evals=mpb.nevals,
              nswarm=len(population),
              error=mpb.currentError(),
              offline_error=mpb.offlineError(),
              **record,
          )
  
          if verbose:
              print(logbook.stream)
  
          # Apply exclusion
          reinit_swarms = set()
          for s1, s2 in itertools.combinations(
              range(len(population)), 2
          ):
              # Swarms must have a best and not already be set to reinitialize
              if (
                  population[s1].best
                  and population[s2].best
                  and not (s1 in reinit_swarms or s2 in reinit_swarms)
              ):
                  dist = 0
                  for x1, x2 in zip(
                      population[s1].best, population[s2].best
                  ):
                      dist += (x1 - x2) ** 2.0
                  dist = math.sqrt(dist)
                  if dist < rexcl:
                      if (
                          population[s1].bestfit
                          <= population[s2].bestfit
                      ):
                          reinit_swarms.add(s1)
                      else:
                          reinit_swarms.add(s2)
  
          # Reinitialize and evaluate swarms
          for s in reinit_swarms:
              population[s] = toolbox.swarm(n=NPARTICLES)
              for part in population[s]:
                  part.fitness.values = toolbox.evaluate(part)
  
                  # Update swarm's attractors personal best and global best
                  if not part.best or part.fitness > part.bestfit:
                      part.best = toolbox.clone(part[:])
                      part.bestfit.values = part.fitness.values
                  if (
                      not population[s].best
                      or part.fitness > population[s].bestfit
                  ):
                      population[s].best = toolbox.clone(part[:])
                      population[s].bestfit.values = part.fitness.values
          generation += 1
  
file_path: deap/examples/pso/multiswarm.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