[英]scipy.optimize.minimize does not converge in multivariable optimization
I want to find the values of board_trim and lm that will give me the lowest (closest to 0) value for Board_Moments .我想找到board_trim和LM的,这将使我的Board_Moments最低(接近0)值的值。
For this I use scipy.optimize.minimize , but it does not converge.为此,我使用scipy.optimize.minimize ,但它不会收敛。 I really can't figure it out.
我实在想不通。
with Parameters :带参数:
displacement = 70位移 = 70
b = 6.5 b = 6.5
deadrise = 20死亡人数 = 20
LCG = 10 LCG = 10
Vs_ms = 23.15 #ms Vs_ms = 23.15 #ms
rho = 1025 ρ = 1025
mu = 1.19e-6亩 = 1.19e-6
def Board_Moments(params):
board_trim, lm = params
displacement_N = displacement * 9.81 #kN
lp = Lp(Vs_ms, b, lm)
N = displacement_N * cos(d2r(board_trim)) #Drag Forces Perpendicular to the keel
#Taking moments about transom at height of CG
deltaM = (displacement_N * LCG) - (N * lp) #equilibrium condition
return deltaM
where lp:其中 lp:
def Lp(Vs_ms, b, lm):
cv = Cv(Vs_ms, b)
Lambda = Lambda_(lm, b)
Cp = 0.75 - (1 / (5.21 * (cv / Lambda)**2 + 2.39))
lp = Cp * lm
return lp
and和
def Cv(Vs_ms, b):
cv = Vs_ms / (9.81 * b)**0.5
return cv
and和
def Lambda_(lm, b):
lambda_ = lm / b
return lambda_
the optimization is done with:优化是通过以下方式完成的:
board_trim = 2 #initial estimate
lm = 17.754 #initial estimate
x0 = [board_trim, lm]
Deltam = minimize(Board_Moments, x0, method = 'Nelder-Mead')
print(Deltam)
The error I get:我得到的错误:
final_simplex: (array([[ 1.36119237e+01, 3.45635965e+23],
[-1.36046725e+01, 3.08439110e+23],
[ 2.07268577e+01, 2.59841956e+23]]), array([-7.64916992e+25,
-6.82618616e+25, -5.53373709e+25]))
fun: -7.649169916342451e+25
message: 'Maximum number of function evaluations has been exceeded.'
nfev: 401
nit: 220
status: 1
success: False
x: array([1.36119237e+01, 3.45635965e+23])
Any help would be much appreciated, thanks任何帮助将不胜感激,谢谢
You mention你提到
that will give me the lowest (closest to 0) value for Board_Moments.
这将为我提供 Board_Moments 的最低(最接近 0)值。
But minimize will search the absolute minimum.但最小化将搜索绝对最小值。 If you print the intermediate values for
deltaM
(which you should have done to debug your problem), you'll find they just get smaller and smaller, below zero (so -10, -100, -500 etc. That kind of progression).如果你打印
deltaM
的中间值(你应该这样做来调试你的问题),你会发现它们变得越来越小,低于零(所以 -10、-100、-500 等等。那种进展) .
To get as close to zero as possible, the solution is simply: return the absolute value of deltaM
from Board_Moments
:为了获得尽可能接近零越好,解决的方法就是:回归的绝对值
deltaM
从Board_Moments
:
def Board_Moments(params):
# code as before ...
deltaM = (displacement_N * LCG) - (N * lp) #equilibrium condition
# This print function would have shown the problem immediately
#print(deltaM)
# Use absolute (the built-in `abs` or `np.abs`;
# doesn't really matter for a single value)
# to get close to zero
return np.abs(deltaM)
For this particular case and fix, the result I get is:对于这种特殊情况和修复,我得到的结果是:
final_simplex: (array([[ 2.32386388, 15.3390523 ],
[ 2.32394414, 15.33905343],
[ 2.32390145, 15.33905283]]), array([5.33445927e-08, 7.27723091e-08, 1.09428584e-07]))
fun: 5.334459274308756e-08
message: 'Optimization terminated successfully.'
nfev: 107
nit: 59
status: 0
success: True
x: array([ 2.32386388, 15.3390523 ])
(and if you comment out the print function, you'll see it easily converge towards zero.) (如果你注释掉打印函数,你会看到它很容易收敛到零。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.