MCPcopy Create free account
hub / github.com/DEAP/deap / TestMultiObjectiveNumpy

Class TestMultiObjectiveNumpy

tests/test_convergence.py:371–442  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

369
370@unittest.skipUnless(numpy, "requires numpy")
371class TestMultiObjectiveNumpy(TearDownCreatorTestCase):
372 def setUp(self):
373 creator.create(FITCLSNAME, base.Fitness, weights=(-1.0, -1.0))
374 creator.create(INDCLSNAME, numpy.ndarray, fitness=creator.__dict__[FITCLSNAME])
375
376 def test_mo_cma_es(self):
377
378 def distance(feasible_ind, original_ind):
379 """A distance function to the feasibility region."""
380 return sum((f - o)**2 for f, o in zip(feasible_ind, original_ind))
381
382 def closest_feasible(individual):
383 """A function returning a valid individual from an invalid one."""
384 feasible_ind = numpy.array(individual)
385 feasible_ind = numpy.maximum(BOUND_LOW, feasible_ind)
386 feasible_ind = numpy.minimum(BOUND_UP, feasible_ind)
387 return feasible_ind
388
389 def valid(individual):
390 """Determines if the individual is valid or not."""
391 if any(individual < BOUND_LOW) or any(individual > BOUND_UP):
392 return False
393 return True
394
395 NDIM = 5
396 BOUND_LOW, BOUND_UP = 0.0, 1.0
397 MU, LAMBDA = 10, 10
398 NGEN = 500
399
400 numpy.random.seed(128)
401
402 # The MO-CMA-ES algorithm takes a full population as argument
403 population = [creator.__dict__[INDCLSNAME](x) for x in numpy.random.uniform(BOUND_LOW, BOUND_UP, (MU, NDIM))]
404
405 toolbox = base.Toolbox()
406 toolbox.register("evaluate", benchmarks.zdt1)
407 toolbox.decorate("evaluate", tools.ClosestValidPenalty(valid, closest_feasible, 1.0e+6, distance))
408
409 for ind in population:
410 ind.fitness.values = toolbox.evaluate(ind)
411
412 strategy = cma.StrategyMultiObjective(population, sigma=1.0, mu=MU, lambda_=LAMBDA)
413
414 toolbox.register("generate", strategy.generate, creator.__dict__[INDCLSNAME])
415 toolbox.register("update", strategy.update)
416
417 for gen in range(NGEN):
418 # Generate a new population
419 population = toolbox.generate()
420
421 # Evaluate the individuals
422 fitnesses = toolbox.map(toolbox.evaluate, population)
423 for ind, fit in zip(population, fitnesses):
424 ind.fitness.values = fit
425
426 # Update the strategy with the evaluated individuals
427 toolbox.update(population)
428

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…