Just changing the fitness function won't be enough, since you need to do an extra pass on all individuals to update their fitness based on the presence of nearby dominant individuals. However, you can make your own algorithm for this.
Once you defined the procedure to go through all individuals in the population to set their fitness, based on the procedure described in Petrowski 96
def update_fitness(population):
... # set fitness of non-dominant individuals to 0
return population
You can then redefine a standard algorithm such as eaMuPlusLambda
def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen):
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
population = update_fitness(population)
# Begin the generational process
for gen in range(1, ngen + 1):
# Vary the population
offspring = varOr(population, toolbox, lambda_, cxpb, mutpb)
# 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
# Select the next generation population
population[:] = toolbox.select(population + offspring, mu)
return population