为了账号安全,请及时绑定邮箱和手机立即绑定

更新函数并从 scipy 获取迭代结果

更新函数并从 scipy 获取迭代结果

胡说叔叔 2023-08-08 17:25:49
def f(params):    pi12, pi21 = params    LL = 10*np.log(40*60/110**2) + 30*np.log(40*50/110**2) + 20*np.log(20/110*(50/110 + 60/110*pi12)) + \          50*np.log(50/110*(60/110 + 50/110*pi21)) - 110*np.log(40*60/110**2 + 40*50/110**2 + \         20/110*(50/110 + 60/110*pi12) + 50/110*(60/110 + 50/110*pi21))    return -LLdef callbackF(Xi):    global Nfeval    print('pass callback',str(Nfeval))    print(Nfeval, Xi[0], Xi[1], f(Xi))    Nfeval += 1initial_guess = [0, 0]b = (0.0, 1.0)b0 = b1 = bbnb = [b0, b1]res = minimize(f, initial_guess, bounds=bnb, method='bfgs', callback=callbackF, options={'disp':True})print (res)我试图捕获目标函数的最佳结果,但它总是显示 [1, 1]。理想情况下,我应该将两个参数限制在 0 和 1 之间,以最大化 -LL。我做错了什么吗?我想知道是否应该在每次迭代后更新目标函数,但我很困惑如何使其发挥作用。我检查了几个帖子但仍然不确定。其他阅读材料也非常受欢迎。多谢!pass callback 2525 0.6870283538140954 0.7403323855238932 143.98656641020855pass callback 2626 0.7935216169001177 0.7090801503785442 143.93658208323882pass callback 2727 0.8314173041320377 0.7666686643426496 143.84748818067345pass callback 2828 0.9264732632840973 0.8980814706430704 143.7237871814941pass callback 2929 0.9885339111975429 0.9836968132795704 143.69759782341296pass callback 3030 0.999243206123829 0.9988036732413753 143.69694856450647pass callback 3131 1.0000109917713558 0.9999811482899945 143.6969451785149pass callback 3232 1.0000049364520325 0.99999836986115 143.6969451768374pass callback 3333 1.000000303283094 1.000000360615374 143.6969451767528Optimization terminated successfully.         Current function value: 143.696945         Iterations: 9         Function evaluations: 44         Gradient evaluations: 11      fun: 143.6969451767528 hess_inv: array([[0.2255719 , 0.08676943],       [0.08676943, 0.21320636]])      jac: array([1.90734863e-06, 3.81469727e-06])  message: 'Optimization terminated successfully.'     nfev: 44      nit: 9     njev: 11   status: 0  success: True        x: array([1.0000003 , 1.00000036])
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

看起来优化器正在 [1,1] 处找到正确的最小值。这是您的函数图:

https://img1.sycdn.imooc.com//64d20a450001df9a04130361.jpg

这是我用来生成该图的代码。


from scipy.optimize import minimize

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import cm


def f(params):

    pi12, pi21 = params

    LL = 10*np.log(40*60/110**2) + 30*np.log(40*50/110**2) + 20*np.log(20/110*(50/110 + 60/110*pi12)) + \

         50*np.log(50/110*(60/110 + 50/110*pi21)) - 110*np.log(40*60/110**2 + 40*50/110**2 + \

         20/110*(50/110 + 60/110*pi12) + 50/110*(60/110 + 50/110*pi21))

    return -LL


def g(X,Y):

    return f([X,Y])


initial_guess = [0, 0]

b = (0.0, 1.0)

b0 = b1 = b

bnb = [b0, b1]

res = minimize(f, initial_guess, bounds=bnb, method='bfgs')


print (res)



X = np.arange(0, 1, 0.05)

Y = np.arange(0, 1, 0.05)

X, Y = np.meshgrid(X, Y)

Z = g(X,Y)


fig = plt.figure()

ax = fig.gca(projection='3d')


# Plot the surface.

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,

                       linewidth=0, antialiased=False)


plt.show()


查看完整回答
反对 回复 2023-08-08
  • 1 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信