code: def test_mo_cma_es(self):
      def distance(feasible_ind, original_ind):
          """A distance function to the feasibility region."""
          return sum(
              (f - o) ** 2 for f, o in zip(feasible_ind, original_ind)
          )
  
      def closest_feasible(individual):
          """A function returning a valid individual from an invalid one."""
          feasible_ind = numpy.array(individual)
          feasible_ind = numpy.maximum(BOUND_LOW, feasible_ind)
          feasible_ind = numpy.minimum(BOUND_UP, feasible_ind)
          return feasible_ind
  
      def valid(individual):
          """Determines if the individual is valid or not."""
          if any(individual < BOUND_LOW) or any(individual > BOUND_UP):
              return False
          return True
  
      NDIM = 5
      BOUND_LOW, BOUND_UP = 0.0, 1.0
      MU, LAMBDA = 10, 10
      NGEN = 500
  
      numpy.random.seed(128)
  
      # The MO-CMA-ES algorithm takes a full population as argument
      population = [
          creator.__dict__[INDCLSNAME](x)
          for x in numpy.random.uniform(BOUND_LOW, BOUND_UP, (MU, NDIM))
      ]
  
      toolbox = base.Toolbox()
      toolbox.register("evaluate", benchmarks.zdt1)
      toolbox.decorate(
          "evaluate",
          tools.ClosestValidPenalty(
              valid, closest_feasible, 1.0e6, distance
          ),
      )
  
      for ind in population:
          ind.fitness.values = toolbox.evaluate(ind)
  
      strategy = cma.StrategyMultiObjective(
          population, sigma=1.0, mu=MU, lambda_=LAMBDA
      )
  
      toolbox.register(
          "generate", strategy.generate, creator.__dict__[INDCLSNAME]
      )
      toolbox.register("update", strategy.update)
  
      for gen in range(NGEN):
          # Generate a new population
          population = toolbox.generate()
  
          # Evaluate the individuals
          fitnesses = toolbox.map(toolbox.evaluate, population)
          for ind, fit in zip(population, fitnesses):
              ind.fitness.values = fit
  
          # Update the strategy with the evaluated individuals
          toolbox.update(population)
  
      # Note that we use a penalty to guide the search to feasible solutions,
      # but there is no guarantee that individuals are valid.
      # We expect the best individuals will be within bounds or very close.
      num_valid = 0
      for ind in strategy.parents:
          dist = distance(closest_feasible(ind), ind)
          if numpy.isclose(dist, 0.0, rtol=1.0e-5, atol=1.0e-5):
              num_valid += 1
      self.assertGreaterEqual(num_valid, len(strategy.parents))
  
      # Note that NGEN=500 is enough to get consistent hypervolume > 116,
      # but not 119. More generations would help but would slow down testing.
      hv = hypervolume(strategy.parents, [11.0, 11.0])
      self.assertGreater(
          hv, HV_THRESHOLD, msg="Hypervolume is lower than expected"
      )
  
imports: from itertools import islice
  import random
  import unittest
  from deap import algorithms
  from deap import base
  from deap import benchmarks
  from deap.benchmarks.tools import hypervolume
  from deap import cma
  from deap import creator
  from deap import tools
label_desc: Use image node_id 2 for calling the TestMultiObjectiveNumpy obj's underlying member method code with example usage: obj.test_mo_cma_es() and returns: sum, feasible_ind, True, False