Passing multiple parameters to the fitness function

244 views
Skip to first unread message

Fabrizio De Santis

unread,
Dec 19, 2020, 8:57:16 AM12/19/20
to deap-users
Hi, I'm new to Deap.
I'm trying to pass one more argument to my fitness function:
def eval_fitness(individual, mode):
When I call "toolbox.evaluate(individual, mode)" it works, but it doesn't work in toolbox.map(toolbox.evaluate, invalid_ind). It returns me the error: "eval_fitness() missing 1 required positional argument: 'mode'"
Note: The parameter (mode) helps me to determine on which set file to evaluate fitness.

Luca Baldini

unread,
Dec 19, 2020, 10:12:23 AM12/19/20
to deap-...@googlegroups.com
Map does not accept more than one argument. I usually use functools.partial that force one or more arguments to a given value. Check it out.

--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/deap-users/b5c0f667-ee33-45cf-909b-9620f4248650n%40googlegroups.com.

Derek Tishler

unread,
Dec 19, 2020, 10:35:55 AM12/19/20
to deap-users
If partials do not work for your case as suggested above and you want to pass multiple items via map; you can zip them up to send as 1 object, then unpack inside eval.

toolbox.map(toolbox.evaluate, zip(population, mode_per_individual, [True]*len(population)))
and then unpack inside evaluate
def evaluate(INPUTS):
    individual, mode, bool_status = INPUTS


This is the same idea as iteration via:
for item1,item2,item3 in zip(list1,lis2,list3):
    pass

Fabrizio De Santis

unread,
Dec 19, 2020, 2:21:41 PM12/19/20
to deap-users
Thanks for the answer!
I have two another questions.
My idea was to use one set (training) for the whole population and another different set (validation set) for the evalution of the best individual (hallofame[0]).
If I want to use a boolean parameter, should I call the function "toolbox.map(toolbox.evaluate, zip(invalid_ind, [True]*len(population)))"?
And if I want to call the function for a single individual, which function should i call? toolbox.evaluate(zip(hallofame[0], True))?

Derek Tishler

unread,
Dec 19, 2020, 3:06:21 PM12/19/20
to deap-users
toolbox.map(toolbox.evaluate, zip(invalid_ind, [True]*len(invalid_ind)))
Will pass a tuple like (individual, bool) to evaluate for each individual. To be clear [True]*len(invalid_ind) is creating a list of True with same length as invalid_ind  to easily pair via zip. To explain, python's zip function "Make(s) an iterator that aggregates elements from each of the iterables.". You can also 'unzip', zip(*) , which is handy in python for returning values in a similar fashion.

To call a single item, you will need to pass a single tuple/item for the input, for example:
toolbox.evaluate( (hallofame[0], True) )

Warriors Defence Academy

unread,
Mar 22, 2025, 3:32:52 PMMar 22
to deap-users
To pass multiple parameters to a fitness function in an optimization algorithm, you can use tuples, lists, or dictionaries. For example, using a tuple: def fitness_function(params): x, y, z = params; return x**2 + y**2 - z. Calling fitness_function((2, 3, 1)) returns 12. Alternatively, with a dictionary: def fitness_function(**kwargs): return kwargs["x"]**2 + kwargs["y"]**2 - kwargs["z"], then calling fitness_function(x=2, y=3, z=1) also returns 12. These methods offer flexibility, especially in genetic algorithms or parameter optimization tasks.  


Thanks You
Reply all
Reply to author
Forward
0 new messages