from deap import tools
from deap import algorithms
def eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, stats=None,
halloffame=None, verbose=__debug__):
"""This algorithm is similar to DEAP eaSimple() algorithm, with the modification that
halloffame is used to implement an elitism mechanism. The individuals contained in the
halloffame are directly injected into the next generation and are not subject to the
genetic operators of selection, crossover and mutation.
"""
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
if halloffame is None:
raise ValueError("halloffame parameter must not be empty!")
halloffame.update(population) ## Updates the Hall of Fame with the initial population
hof_size = len(halloffame.items) if halloffame.items else 0 ## Determines the size of the Hall of Fame
record = stats.compile(population) if stats else {} ## If a stats object is provided (i.e., it is not 'None'), the compile ## method is called to compute the statistics. Otherwise, an empty dictionary {} is returned.
## The compile method takes the population as input and returns a dictionary containing various ## statistics
logbook.record(gen=0, nevals=len(invalid_ind), **record) ## we saved the generation, the number of evaluations and everything contained in the record ## produced by a statistics object using the star magic (**record)
if verbose:
print(logbook.stream) ## This variable is a boolean flag that determines whether verbose output should be printed during the evolutionary process. If ## verbose is True, it indicates that the user wants to see detailed information about each generation. The logbook. stream ## attribute contains a formatted representation of the logbook, which typically includes information about the current ## generation, such as the generation number, the number of evaluations, and any other statistics collected during the evolution ## process
# Begin the generational process
for gen in range(1, ngen + 1):
# Select the next generation individuals
offspring = toolbox.select(population, len(population) - hof_size) ## Selects individuals from the population for breeding, excluding those already in ## the Hall of Fame. (but how...)????????? This expression determines the number of ## individuals to select for the next generation. len(population) gives the total ## size of the population, and hof_size represents the size of the hall of fame. By ## subtracting the size of the hall of fame from the total population size, we ## ensure that the individuals selected for the next generation do not include that ## already present in the hall of fame.
# Vary the pool of individuals
offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) ## deap.algorithms.varAnd(population, toolbox, cxpb, mutpb)
## Part of an evolutionary algorithm applying only the variation part (crossover and ## mutation). The modified individuals have their fitness invalidated. The ## individuals are cloned so returned population is independent of the put population
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# add the best back to population:
offspring.extend(halloffame.items) ## Elitism occurs here
# Update the hall of fame with the generated individuals
halloffame.update(offspring)
# Replace the current population by the offspring
population[:] = offspring
# Append the current generation statistics to the logbook
record = stats.compile(population) if stats else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
return population, logbook
我正在学习GP,我遇到了。
__debug__
是由解释器设置的只读布尔变量。