scipy.optimize.basinhopping似乎正在调用它希望使用相同的输入参数并获得相同的输出来最小化多次(四次)次数的函数。虽然它能正确给出答案,但我不明白为什么它要调用4次,对于真正的问题,我试图对其进行优化,这会大大降低收敛速度。
我尝试使用其他的minimumr_kwargs = minimizer_kwargs,但这似乎无关紧要。我不想短路算法,但是要进行某种类型的(伪代码):
如果current_iter> 1,则跳过评估函数,返回先前的结果
下面是scipy网站上给出的简单示例的全部。我只添加了“ print('x is:',x,'f is:',f)”
import numpy as np
from scipy.optimize import basinhopping
def func2d(x):
f = np.cos(14.5 * x[0] - 0.3) + (x[1] + 0.2) * x[1] + (x[0] +0.2) * x[0]
print('x is:', x, 'f is:', f)
return f
x0 = [-1.0, 1.0]
ret = basinhopping(func2d, x0,niter=2)
print("global minimum: x = [%.4f, %.4f], f(x0) = %.4f" % \
(ret.x[0],ret.x[1],ret.fun))
输出将如下所示:
x is: [-1. 1.] f is: 1.3846475170452792
x is: [-0.99999999 1. ] f is: 1.3846476605383349
x is: [-1. 1.00000001] f is: 1.3846475498278341
x is: [-1. 1.] f is: 1.3846475170452792
x is: [-1.98463072 0.77505036] f is: 3.6028590636594706
x is: [-1.98463072 0.77505036] f is: 3.6028590636594706
x is: [-1.98463071 0.77505036] f is: 3.6028588520736204
x is: [-1.98463072 0.77505038] f is: 3.602859089738004
x is: [-1.39207434 0.91042634] f is: 2.605736986404326
x is: [-1.39207434 0.91042634] f is: 2.605736986404326
x is: [-1.39207433 0.91042634] f is: 2.60573716351191
x is: [-1.39207434 0.91042635] f is: 2.6057370165173777
为什么要调用相同的输入4次?