用python中的scipy求解有界非线性最小化

Geo*_*rge 4 python optimization minimization scipy nonlinear-optimization

试图用一个变量解决一个简单的非线性最小化问题.

from scipy.optimize import minimize
import math

alpha = 0.05
waiting = 50
mean_period = 50
neighborhood_size = 5

def my_func(w):
    return -(2/(w+1) + alpha*math.floor(waiting/mean_period))*(1-(2/(w+1) + alpha*math.floor(waiting/mean_period)))**(neighborhood_size-1)

print minimize(my_func, mean_period, bounds=(2,200))
Run Code Online (Sandbox Code Playgroud)

这给了我

ValueError: length of x0 != length of bounds
Run Code Online (Sandbox Code Playgroud)

我输错了吗?我该如何格式化?

如果我删除边界,我得到:

status: 2
  success: False
     njev: 19
     nfev: 69
 hess_inv: array([[1]])
      fun: array([-0.04072531])
        x: array([50])
  message: 'Desired error not necessarily achieved due to precision loss.'
      jac: array([-1386838.30676792])
Run Code Online (Sandbox Code Playgroud)

该函数看起来像这样,因此我需要界限来限制我感兴趣的局部最大值的解决方案.

CT *_*Zhu 7

它应该是:

print minimize(my_func, mean_period, bounds=((2,200),))

  status: 0
 success: True
    nfev: 57
     fun: array([-0.08191999])
       x: array([ 12.34003932])
 message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
     jac: array([  2.17187379e-06])
     nit: 4
Run Code Online (Sandbox Code Playgroud)

对于每一个参数,你必须提供一个绑定的,所以在这里我们需要传递tuple,其中只包含一个tuple (2,200)minimize().