examples/deap/deap_efel.py
"""eFEL / DEAP / Neuron optimisation example""" import randomimport numpy import deapimport deap.gpimport deap.benchmarksfrom deap import basefrom deap import creatorfrom deap import toolsfrom deap import algorithms # Set random seedrandom.seed(1) # Set number of individuals in populationPOP_SIZE = 100 # Set number of individuals to create in every offspringOFFSPRING_SIZE = 100 # Total number of generation to runNGEN = 300 # Total population size of EAALPHA = POP_SIZE# Total parent population size of EAMU = OFFSPRING_SIZE# Total offspring size of EALAMBDA = OFFSPRING_SIZE # Crossover, mutation probabilitiesCXPB = 0.7MUTPB = 0.3 # Eta parameter of crossover / mutation parameters# Basically defines how much they 'spread' solution around# The higher this value, the more spreadETA = 10.0 # Selector to useSELECTOR = "NSGA2" # Number of parametersIND_SIZE = 2 # Bounds for the parametersLOWER = [1e-8, -100.0]UPPER = [1e-4, -20.0] # Create a fitness function# By default DEAP selector will try to optimise fitness values,# so we add a -1 weight value to minimisecreator.create("Fitness", base.Fitness, weights=[-1.0] * 2) # Create an individual that consists of a listcreator.create("Individual", list, fitness=creator.Fitness) # Define a function that will uniformly pick an individualIdentical blocks of code found in 2 locations. Consider refactoring.def uniform(lower_list, upper_list, dimensions): """Fill array """ if hasattr(lower_list, '__iter__'): return [random.uniform(lower, upper) for lower, upper in zip(lower_list, upper_list)] else: return [random.uniform(lower_list, upper_list) for _ in range(dimensions)] # Create a DEAP toolboxtoolbox = base.Toolbox() # Register the 'uniform' functiontoolbox.register("uniformparams", uniform, LOWER, UPPER, IND_SIZE) # Register the individual format# An indiviual is create by 'creator.Individual' and parameters are initially# picked by 'uniform'toolbox.register( "Individual", tools.initIterate, creator.Individual, toolbox.uniformparams) # Register the population format. It is a list of individualstoolbox.register("population", tools.initRepeat, list, toolbox.Individual) # Register the evaluation function for the individualsimport deap_efel_eval1toolbox.register("evaluate", deap_efel_eval1.evaluate) # Register the mate operatortoolbox.register( "mate", deap.tools.cxSimulatedBinaryBounded, eta=ETA, low=LOWER, up=UPPER) # Register the mutation operatortoolbox.register("mutate", deap.tools.mutPolynomialBounded, eta=ETA, low=LOWER, up=UPPER, indpb=0.1) # Register the variate operatortoolbox.register("variate", deap.algorithms.varAnd) # Register the selector (picks parents from population)toolbox.register( "select", tools.selNSGA2) # Generate the population objectpop = toolbox.population(n=MU) # Register the statistics we want to record during the optimisation# In this case only the minimal valuefirst_stats = tools.Statistics(key=lambda ind: ind.fitness.values[0])second_stats = tools.Statistics(key=lambda ind: ind.fitness.values[1])stats = tools.MultiStatistics(obj1=first_stats, obj2=second_stats)stats.register("min", numpy.min, axis=0) # Run the actual algorithm with all the object/parameters we set up aboveif __name__ == '__main__': pop, logbook = algorithms.eaMuPlusLambda( pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=None)