I am just starting use DEAP. Previously, I used GA based on Matlab that after crossover and mutation is to select the better individuals with specified size, and then updating the population. But it is quite hard for me to understand that why in DEAP the evaluate the individuals with an invalid fitness is needed after the crossover and mutation procedure:
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
I tried that deleting those code, but it seems the algorithm will never convergence. And even I did't see those can updating the population/offspring, so what are those use for. Thanks in advance!!!
After few tests, the evaluation of the individuals with an in valid fitness is for evaluating the fitness of the individuals after crossover and mutation procedures. We can see from this code:
# Apply crossover and mutation on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CXPB:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
the fitness value of those individuals which crossover-ed and mutated are deleted using del
. So invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
is used to select those individuals for re-evaluating, this can significantly reduce the amount of computation. Moreover, we can also re-evaluating the whole offspring using:
fitnesses = map(toolbox.evaluate, offspring)
for ind, fit in zip(offspring, fitnesses):
ind.fitness.values = fit
as said, this can increasing the computation burden. To be conclude, this re-evaluation is needed for assigning the fitness to each individual, and ready for selection operation in offspring = toolbox.select(pop, len(pop))
which based on the fitness value of individuals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With