1

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?

Eduardo Yáñez Parareda
  • 9,126
  • 4
  • 37
  • 50

1 Answers1

3

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)
Isma
  • 14,604
  • 5
  • 37
  • 51
  • I have a doubt, the function_maximize need to return a value between 0 and 1.0? and the function_minimize need to return a value between -0.5 and 0?. How can I do if my func_min return a value between 0 - 800 and my func_max return a value( in %) between 0 - 100? – CodeIK Nov 21 '19 at 14:43
  • 1
    Hi, those values are just the weights, the result of your maximize or minimize functions has nothing to do with it. For example, if your minimize and maximize function return the same value the weights are used to give more or less importance to one or the other. – Isma Nov 21 '19 at 19:52