Re: [deap-users] Pass additional arguments to the fitness function (beyond just the individual)

1,191 views
Skip to first unread message

EBo

unread,
Feb 2, 2015, 12:39:58 PM2/2/15
to deap-...@googlegroups.com
While it is not the most pythonic way of doing things, since it is a
once up front type of thing, they could be set as globals, or part of a
variable init/return. Anyway, this is something that you *could* try,
but if one of the developers gives you a cleaner way I would go with
that.

On Feb 2 2015 10:25 AM, Jia Xu wrote:
> Hi,
>
> I looked through the docs and it's not clear to me how one might pass
> additional arguments to the fitness function. In my problem these
> arguments
> are constant and only need to be passed in at the start.
>
> I tried registering the evaluate function with additional parameters:
>
> problem.register("evaluate", fitness_function, param1, param2)
>
> And:
>
> fitness_function(x, param1, param2)
>
> But this does not work. What should I be doing?
>
> Best
> Jia

Félix-Antoine Fortin

unread,
Feb 2, 2015, 1:20:10 PM2/2/15
to deap
While there is no documentation explicitly on how to provide additional arguments to the fitness function, it is well covered for other functions. 

Have a look at the mutate function in the following section of the second tutorial.

toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
The signature of mutGaussian is :
deap.tools.mutGaussian(individualmusigmaindpb)

As you can see, we register the mu, sigma and indpb parameters before starting the evolution because, as for you fitness function, these parameters are constant.

The problem with the code you provided is that you have not provided the parameters named when registering its value with the toolbox. Let's say param1 equal to 0.5 and param2 to -20, you would write the evaluation registering like this:
problem.register("evaluate", fitness_function, param1=0.5, param2=-20)

Then you can use the registered function to evaluate your individual without providing the argument param1 and param2.
problem.evaluate(my_individual)

In your code, I assume "problem" is the toolbox. Otherwise, please read the first two tutorials on DEAP.

As François-Michel mentioned in a previous post, the toolbox works like functools.partial which is provided by the Python Standard Library. I recommend you to read about it. It might help you grok the toolbox better.

Regards,
Félix-Antoine

On Mon, Feb 2, 2015 at 12:25 PM, Jia Xu <jiax...@gmail.com> wrote:
Hi,

I looked through the docs and it's not clear to me how one might pass additional arguments to the fitness function. In my problem these arguments are constant and only need to be passed in at the start.

I tried registering the evaluate function with additional parameters:

problem.register("evaluate", fitness_function, param1, param2)

And:              

fitness_function(x, param1, param2)

But this does not work. What should I be doing?

Best
Jia

--
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.
For more options, visit https://groups.google.com/d/optout.

Jia Xu

unread,
Feb 2, 2015, 2:00:00 PM2/2/15
to deap-...@googlegroups.com
Sorry about the repost. The earlier answer worked. My attempt to delete this post did not work =)

Thanks for your help Felix-Antonine.

Best
Jia
Reply all
Reply to author
Forward
0 new messages