最优化作业第六章——共轭梯度法和鲍尔法

共轭梯度法:

代码:

#导入模块
from sympy import *
import sympy as sp  #将导入的模块重新定义一个名字以便后续的程序进行使用
from numpy import *
import numpy as np

def main():
    #本例是利用共轭梯度法进行最优化
    x1,x2,alpha = symbols("x1,x2,alpha",real = True)
    f_fun = x1**2 + 25*x2**2
    x = np.mat(np.array([[2],[2]]))
    x0 = np.mat(np.array([[2],[2]]))
    f_diff_x01 = sp.diff(f_fun,x1).subs({x1:x0[0,0],x2:x0[1,0]})
    f_diff_x02 = sp.diff(f_fun,x2).subs({x1:x0[0,0],x2:x0[1,0]})
    f_diff_array = np.array([[f_diff_x01],[f_diff_x02]])
    f_diff_mat= np.mat(f_diff_array)
    d = -f_diff_mat
    x_fun = x + alpha*d
    f = (x_fun[0,0])**2 + 25*(x_fun[1,0])**2
    f_diff_alpha = sp.diff(f,alpha)
    alpha_solver = (solve([f_diff_alpha],[alpha]))[alpha]
    x_solver = x + alpha_solver*d
    f_diff_x11 = float(sp.diff(f_fun,x1).subs({x1:x_solver[0,0],x2:x_solver[1,0]}))
    f_diff_x12 = float(sp.diff(f_fun,x2).subs({x1:x_solver[0,0],x2:x_solver[1,0]}))
    f_diff_array = np.array([[f_diff_x11],[f_diff_x12]])
    f_diff_mat= np.mat(f_diff_array)
    print("-------------------第一次--------------------")
    print("alpha:\n%s,\nx(1):\n%s,\nf_diff_1:\n%s\n"%(alpha_solver ,x_solver,f_diff_mat))

    beta = float(((f_diff_x11)**2 + (f_diff_x12)**2)/((f_diff_x01)**2 + (f_diff_x02)**2))
    d = (-f_diff_mat+beta*d)
    print(beta,d)
    x_fun = x_solver + alpha*d

    f = (x_fun[0, 0]) ** 2 + 25 * (x_fun[1, 0]) ** 2

    f_diff_alpha = sp.diff(f,alpha)

    alpha_solver = (solve([f_diff_alpha],[alpha]))[alpha]
    x_solver = x + alpha_solver*d
    f_diff_x11 = float(sp.diff(f_fun,x1).subs({x1:x_solver[0,0],x2:x_solver[1,0]}))
    f_diff_x12 = float(sp.diff(f_fun,x2).subs({x1:x_solver[0,0],x2:x_solver[1,0]}))
    f_diff_array = np.array([[f_diff_x11],[f_diff_x12]])
    f_diff_mat= np.mat(f_diff_array)
    print("-------------------第二次--------------------")
    print("alpha:\n%s,\nx(1):\n%s,\nf_diff_1:\n%s\n"%(alpha_solver ,x_solver,f_diff_mat))


if __name__ == '__main__':
    main()

运行结果:


------------------------第1次迭代---------------------
alpha:
0.02003071803404582

x:
[[1.91987712786382]
 [-0.00307180340458224]]

负梯度:
[[3.83975425572763]
 [-0.153590170229112]]

beta:
0.0014743712744378474

d:
[[-3.84565174082538]
 [0.00615304278532730]]

判断条件:
14.76730268476948


------------------------第2次迭代---------------------
alpha:
0.4992332268370619

x:
[[-4.66293670342566e-15]
 [3.21053947316408e-15]]

负梯度:
[[-9.32587340685131e-15]
 [1.60526973658204e-13]]

beta:
0.0014743712744378474

d:
[[ 9.32587341e-15]
 [-1.60526974e-13]]

判断条件:
2.585588118666227e-26



进程已结束,退出代码0

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有情怀的机械男

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值