Use optimize.minimize from scipy with 2 variables and interpolated function

后端 未结 2 1501
挽巷
挽巷 2021-02-11 02:27

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 in

2条回答
  •  悲&欢浪女
    2021-02-11 02:54

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

    F2 = interp2d(x, y, -z)
    

    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)
    

    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)
    

    This yields:

      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
    

提交回复
热议问题