Search code examples
python-3.xgenetic-algorithmdeap

Python DEAP: Fitness seems to go down over time - how do I fix this?


My individual is a concatenation of the five parameters needed to tweak a multi-stage process. The first population is created by randomly assigning those parameters a value within their respective ranges and converting that into a binary string. A graph of the first 25 generations typically looks like this:

Graph of the first 25 generations

The DEAP code looks like this:

# Create the individuals
creator.create('FitnessMax', base.Fitness, weights=(1.0, ))
creator.create('Individual', list, fitness=creator.FitnessMax)

# Initialize the containers
toolbox = base.Toolbox()
toolbox.register('individual', get_random_contour_ind)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)

# Initialize the operators
toolbox.register('evaluate', get_total_max_ratio)
toolbox.register('mate', tools.cxUniform, indpb=0.10)
toolbox.register('mutate', tools.mutFlipBit, indpb=0.10)
toolbox.register('select', tools.selBest)

It takes a long time to run, but I've run the same DEAP code on other evaluators and am noticing the same pattern. How do I fix this?


Solution

  • The problem was that the evaluator is non-deterministic, so I took paddymccrudden's advice and added a decorator to enforce stochastic fitness. Now the fitness graph looks like this:

    25 generations of stochastic fitness enforcement

    As you can see the average fitness is increasing over the generations.