1
votes

I'm pretty new to DEAP and looking at several places and examples I've seen it creates classes for genetic algoritms using this method:

creator.create('FitnessMax', base.Fitness, weights=(1.0, -0.5,))
creator.create('Individual', list, fitness=creator.FitnessMax)

What I don't understand is the weights parameter. It is supposed that DEAP can be used to solve multiobjectives problems (maximize and minimize), that's why weights can be positive or negative.

But how is it linked to the fitness/objective function? Must the fitness function return several values, one for each weight?

1

1 Answers

3
votes

For multi-objective problems, your fitness function must return a tuple with the same number of results as the specified number of weights, e.g.:

creator.create('Fitness', base.Fitness, weights=(1.0, -0.5,))
creator.create('Individual', list, fitness=creator.Fitness)

[...]

toolbox.register('evaluate', fitness)

def function_minimize(individual):
    return individual[0] - sum(individual[1:])

def function_maximize(individual):
    return sum(individual)

def fitness(individual):
    return (function_maximize(individual), function_minimize(individual)),

Also, keep in mind that your selection method must support multi-objective problems, tournament selection for instance, doesn't, so if you use it, weights will be ignored). A selection method that supports this kind of problem is NSGA2:

toolbox.register('select', tools.selNSGA2)