简体   繁体   English

scipy.optimize.minimize 在多变量优化中不收敛

[英]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_trimLM的,这将使我的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 :为了获得尽可能接近零越好,解决的方法就是:回归的绝对值deltaMBoard_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.

相关问题 scipy.optimize.minimize用于python中的约束优化 - scipy.optimize.minimize for constrained optimization in python 使用多个变量进行优化(使用scipy.optimize.minimize) - Optimization (with scipy.optimize.minimize) with multiple variables scipy.optimize.minimize 无法收敛有约束的矩阵输入 - scipy.optimize.minimize fails to converge for matrix input with constraints 为什么我的优化(scipy.optimize.minimize)不起作用并返回初始值? - Why does my optimization (scipy.optimize.minimize) not work and return the initial values instead? 使用scipy.optimize.minimize()进行约束优化的问题 - Problems with constrained optimization using scipy.optimize.minimize() 为什么 scipy.optimize.minimize 不适用于约束和初始值 0 - Why scipy.optimize.minimize does not work with a constraint and initial value 0 为什么 scipy.optimize.minimize 找不到最小值? - Why does scipy.optimize.minimize not find the minimum? 为什么“ scipy.optimize.minimize”会给我这么不好的印象? - Why does “scipy.optimize.minimize” gives me such a bad fit? scipy.optimize.minimize 不会在 maxiter 或回调处停止 - scipy.optimize.minimize does not stop at maxiter or callback 哪个变量通过scipy.optimize.minimize最小化/如何工作? - Which variable is minimized by scipy.optimize.minimize/How does it work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM