+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2019-07(2)

2019-08(106)

2019-09(110)

2019-10(14)

2019-11(8)

一维搜索之黄金分割法

发布于2020-04-23 19:30     阅读(827)     评论(0)     点赞(0)     收藏(3)


0

1

2

3

4

黄金分割法(0.618法)优化的原理

例题

这里用黄金分割法求解 y=x27x+10 y = x^2-7x+10 的最优解,,初始区间 [a,b]=[2,8] [a,b]=[2,8], 收敛精度为 0.001。

k k a a xk1 x_{k}^{1} xk2 x_{k}^{2} b b f(xk1) f(x_{k}^{1}) f(xk2) f(x_{k}^{2}) [a,b] [a,b] b-a
1 2 8 [2,8] [2,8] 6
2 2 2 2 4.292 4.292 5.708 5.708 8 8 1.6227359999999997 -1.6227359999999997 2.6252640000000014 2.6252640000000014 [2,5.708] [2, 5.708] 3.708 3.708
3 3 2 2 3.416456 3.416456 4.291544 4.291544 5.708 5.708 2.2430204000639993 -2.2430204000639993 1.6234580960639988 -1.6234580960639988 [2,4.291544] [2, 4.291544 ] 2.291544 2.291544
4 2.875369808 2.875369808 3.416348349344 3.416348349344 3.750565458656 3.750565458656 4.291544 4.291544 2.243002401342528 -2.243002401342528 2.2429843830315406 -2.2429843830315406 [2.875369808,3.750565458656] [ 2.875369808 , 3.750565458656] 0.8751956506560004 0.8751956506560004
18 3.498963664784351 3.498963664784351 3.4996049019558986 3.4996049019558986 3.5000010589519333 3.5000010589519333 3.500642296123481 3.500642296123481 2.2499998438975357 -2.2499998438975357 2.2499999999988773 -2.2499999999988773 [3.4996049019558986,3.500642296123481] [ 3.4996049019558986 , 3.500642296123481 ] 0.0010373941675823062 0.0010373941675823062
19 3.4996049019558986 3.4996049019558986 3.500001186527915 3.500001186527915 3.5002460115514644 3.5002460115514644 3.500642296123481 3.500642296123481 2.249999999998593 -2.249999999998593 2.2499999394783163 -2.2499999394783163 [3.4996049019558986,3.5002460115514644] [ 3.4996049019558986 , 3.5002460115514644 ] 0.004395197314465804 0.004395197314465804

代码实现

import matplotlib.pyplot as plt
import numpy as np
from pylab import *

#目标函数
def function(x):
    y = x**2 - 7 * x + 10
    return y
#黄金分割法
def GoldenBorder(a, b, accuracy):
    x1 = a + 0.382*(b - a)
    x2 = a + 0.618*(b - a)
    fx1 = function(x1)
    fx2 = function(x2)
    print( "a=", a,"x1=", x1, "x2=", x2 , ",b=", b,",f(x1)=",fx1, ",f(x2)=", fx2,  )
    if fx1 < fx2:
        a = a
        b = x2
    else:
        a = x1
        b = b
    accuracy = b - a
    print("NewRange[", a, ", ",b,"]","accuracy=", b-a)
    return a, b ,accuracy

a = 2
b = 8
value = []
accuracy = b - a
epoch = 0
accuracies = []
ys = []
epoches=[]
xs = []
while accuracy > 0.001:
    epoch += 1
    print("-"*100)
    print("Epoch:",epoch)
    a, b, accuracy = GoldenBorder(a, b, accuracy)
    accuracies.append(accuracy)
    ys.append(function( a + (b - a) / 2 ))
    epoches.append(epoch)
    xs.append(a + (b - a) / 2)
x = a + (b - a) / 2    
y = function(x)     
print("-"*100)
print("最优点:x*=", x,",f(x*)=", y)
plt.title(' Golden Section Search ')

plt.subplot(2,1,1)
plt.plot(epoches,accuracies, ":o", label = 'accuracy', color="blue")
xticks(np.linspace(0,19,20,endpoint=True))
plt.ylabel('f(x)')
plt.legend()

plt.subplot(2,1,2)
plt.plot(ys, "-.o", label = 'f(x)', color="red")
xticks(np.linspace(0,19,20,endpoint=True))
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend()
plt.show()

在这里插入图片描述

X = np.linspace(3, 4, 20)
Epoch = np.linspace(0, 19,20)
X, Epoch= np.meshgrid(X, Epoch)
Y = X**2 - X * 7 + 10
fig = plt.figure(figsize = ((8,6)))
ax = fig.gca(projection='3d')
ax.plot_surface(Epoch ,X , Y, color="yellow", alpha=0.5)
ax.plot(epoches ,xs , ys,"-.o",color="blue")
xticks(np.linspace(0,19,20,endpoint=True))
ax.set_xlabel('epoch')                #设置坐标轴标签
ax.set_ylabel('x')
ax.set_zlabel(' f(x) ')
plt.show()

!在这里插入图片描述

原文链接:https://blog.csdn.net/weixin_44560088/article/details/105694069

0

1

2

3

4

5

6

7

8

9



所属网站分类: 技术文章 > 博客

作者:9384vfnv

链接: https://www.pythonheidong.com/blog/article/339728/594ad37fffc2de081b7c/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)