I am using differential_evolution from scipy.optimize for my optimization problem. My optimizer takes some arguments for the optimization.
Code -
res = optimize.differential_evolution(objective,bounds,args=arguments,disp=True,callback = callback_DE(arguments))
I also have a callback function. I want to send my arguments to my callback function and that is where my issue arises.
If I don't pass any arguments to my callback function, it works fine -
def callback_DE(x,convergence):
'''
some code
'''
However, if I give arguments
as a parameter in the function definition like -
def callback_DE(x,convergence,arguments):
'''
some code
'''
It throws an error.
What is the correct way to pass arguments to the callback function?
This is not possible. You can only use the two values that are given to you. The point of the callback is to follow your optimization and stop it early if you choose to do so based on some condition it satisfied, by returning True
.
See the description from the reference for more details:
callback : callable, callback(xk, convergence=val), optional
A function to follow the progress of the minimization. xk is the current value of x0. val represents the fractional value of the population convergence. When val is greater than one the function halts. If callback returns True, then the minimization is halted (any polishing is still carried out).
If you really need to use the arguments, you should just access them directly from inside the function.