I believe it is a trivial question but I was not able to find any example and I am struggling to understand how to use this instruction for creating a callback for scipy.optimize
import scipy.optimize as sp
res = sp.minimize(FUN, 0.25,method='Nelder-Mead',bounds=None,callback=STOP)
where the callback is defined as:
def STOP(intermediate_result: sp.OptimizeResult):
print('intermediate_results',intermediate_result)
if intermediate_result.fun < 0.005:
return StopIteration
else:
return 0
The intermediate_resultappears to be only the optimal parameter (so it seems I am implemented the instruction callback(xk) and not callback(intermediate_result: OptimizeResult) )
I would like that my function STOP analyse the value of the returned function value and stop in case the value is smaller than 0.005
But generally I would like to understand what the guidance is saying:
All methods except TNC, SLSQP, and COBYLA support a callable with the signature:
callback(intermediate_result: OptimizeResult)
where intermediate_result is a keyword parameter containing an OptimizeResult with attributes x and fun, the present values of the parameter vector and objective function.
Could you please help me ?
When I tried that I got:
AttributeError: 'numpy.ndarray' object has no attribute 'fun'
I tried different way to change the keyword in the function and inside the optimizer but I have not clue of what I am doing
In previous version of SciPy the callback parameter was the current
xvalue as and.array(<= 1.9.3). In newer version it is a partialOptimizeResultwithfunandx(1.12.0).First upgrade to latest version of SciPy, then try this MCVE
Also notice that exception should be
raised notreturned.For older versions, you have to compute the solution by yourself and raising
StopIetrationwill stop the script (so you have to catch it):Where new version will stop the algorithm and return a solution with corresponding message:
To update your WSL to the latest scipy, issue the following command: