使用带有2个变量和内插函数的scipy中的optimize.minimize [英] Use optimize.minimize from scipy with 2 variables and interpolated function

查看:231
本文介绍了使用带有2个变量和内插函数的scipy中的optimize.minimize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到使用多维函数从scipy中进行optimize.minimize的方法.在几乎所有示例中,在对函数进行插值时都优化了分析函数.测试数据集如下所示:

I didn't find a way to perform optimize.minimize from scipy with a multidimensional function. In nearly all examples an analytical function is optimized while my function is interpolated. The test data set looks like this:

x = np.array([2000,2500,3000,3500])
y = np.array([10,15,25,50])
z = np.array([10,12,17,19,13,13,16,20,17,60,25,25,8,35,15,20])
data = np.array([x,y,z])

函数就像F(x,y)= z

While the function is like F(x,y) = z

我想知道的是,在f(2200,12)处会发生什么,并且x(2000:3500)和y(10:50)范围内的全局最大值是多少.插值工作正常.但是,到目前为止,找到全局最大值是行不通的.

What I want to know is what happens at f(2200,12) and what is the global maximum in the range of x (2000:3500) and y (10:50). The interpolation works fine. But finding the global maximum doesn't work so far.

插值

 self.F2 = interp2d(xx, -yy, z, kind, bounds_error=False)

收益

<scipy.interpolate.interpolate.interp2d object at 0x0000000002C3BBE0>

我尝试通过以下方式进行优化:

I tried to optimize via:

x0 = [(2000,3500),(10,50)]
res = scipy.optimize.minimize(self.F2, x0, method='Nelder-Mead')

引发异常:

TypeError: __call__() missing 1 required positional argument: 'y'

我认为优化器无法处理插值中的对象.在示例中,人们使用lambda从其函数中获取值.我该怎么办?

I think that the optimizer can't handle the object from the interpolation. In the examples the people used lambda to get values from their function. What do I have to do in my case?

最好, 亚历克斯

推荐答案

首先,要找到全局最大值(而不是最小值),您需要使用相反的符号对函数进行插值:

First, to find global maximum (instead of minimum) you need to interpolate your function with opposite sign:

F2 = interp2d(x, y, -z)

第二,minimize中的可调用对象采用参数元组,并且interp2d对象需要将输入坐标作为单独的位置参数给出.因此,我们不能直接在minimize中使用interp2d对象.我们需要一个包装器,该包装器将从minimize解压缩一个元组并将其提供给interp2d:

Second, the callable in minimize takes a tuple of arguments, and interp2d object needs input coordinates to be given as separate positional arguments. Therefore, we cannot use interp2d object in minimize directly; we need a wrapper that will unpack a tuple of arguments from minimize and feed it to interp2d:

f = lambda x: F2(*x)

第三,要使用minimize,您需要指定一个最小值的初始猜测值(在您的情况下为界限).任何合理的观点都可以做到:

And third, to use minimize you need to specify an initial guess for minimum (and bounds, in your case). Any reasonable point will do:

x0 = (2200, 12)
bounds = [(2000,3500),(10,50)]
print minimize(f, x0, method='SLSQP', bounds=bounds)

这将产生:

  status: 0
 success: True
    njev: 43
    nfev: 243
     fun: array([-59.99999488])
       x: array([ 2500.00002708,    24.99999931])
 message: 'Optimization terminated successfully.'
     jac: array([ 0.07000017,  1.        ,  0.        ])
     nit: 43

这篇关于使用带有2个变量和内插函数的scipy中的optimize.minimize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆