Reputation: 345
I am optimizing a function using scipy.optimize.minimize with Powell algorithm. There is an option called maxfev to set maximum function evaluations. However it doesn't seem to work always. Algorithm crosses the function evaluation limit. The code is as follows:
def func_eval(x):
import math as math
funcval=0
dimension=len(x)
tmp=0
tmp2=0
for i in range(dimension):
tmp +=x[i]
for i in range(dimension):
tmp2=(tmp-x[i])*x[i]
funcval += dimension*(math.pow(x[i],2)+0.4*tmp2)
return funcval
from scipy.optimize import minimize
x=[-11.04021,-92.72567,28.60728,68.65449,66.41443,-25.59824,73.97660,-69.85293,1.10955,17.56914]
res=minimize(func_eval,x,method='Powell',options={'maxfev':220})
print(res.fun)
print(res.nfev)
The output of second option i.e. number of func evaluations is 298 instead of 220. What is possibly going wrong with my code, or is it some issue with algorithm itself ?
Upvotes: 3
Views: 1029
Reputation: 23637
The function needs to be evaluated multiple times per iteration; once for each dimension. For some reason (I guess performance or other implementation detail) the evaluation count is only checked once per iteration. That is why the maximum can be exceeded.
How much the evaluation count overshoots the maximum depends on the dimensionality of x
, perhaps also on the function and the data.
Upvotes: 2