gpt4 book ai didi

python - 在 python 中使用 scipy 解决有界非线性最小化

转载 作者:太空狗 更新时间:2023-10-30 02:20:07 24 4
gpt4 key购买 nike

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

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))

这给了我

ValueError: length of x0 != length of bounds

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

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

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])

函数看起来像that因此我需要边界来将解决方案限制在我感兴趣的局部最大值内。

最佳答案

应该是:

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

对于每个参数你必须提供一个边界,因此这里我们需要传递一个tuple,它只包含一个tuple (2,200),到 minimize()

关于python - 在 python 中使用 scipy 解决有界非线性最小化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25269982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com