测试函数主要是用来评估优化算法特性的,这里我用python3绘制了部分测试函数的图像。具体的测试函数可以结合*来了解。想要显示某个测试函数的图片把代码结尾对应的注释去掉即可,具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def draw_pic(X, Y, Z, z_max, title, z_min = 0 ):
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride = 1 , cstride = 1 , cmap = plt.cm.hot)
# ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)
ax.set_zlim(z_min, z_max)
ax.set_title(title)
# plt.savefig("./myProject/Algorithm/pic/%s.png" % title) # 保存图片
plt.show()
def get_X_AND_Y(X_min, X_max, Y_min, Y_max):
X = np.arange(X_min, X_max, 0.1 )
Y = np.arange(Y_min, Y_max, 0.1 )
X, Y = np.meshgrid(X, Y)
return X, Y
# rastrigin测试函数
def Rastrigin(X_min = - 5.52 , X_max = 5.12 , Y_min = - 5.12 , Y_max = 5.12 ):
A = 10
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = 2 * A + X * * 2 - A * np.cos( 2 * np.pi * X) + Y * * 2 - A * np.cos( 2 * np.pi * Y)
return X, Y, Z, 100 , "Rastrigin function"
# Ackley测试函数
def Ackley(X_min = - 5 , X_max = 5 , Y_min = - 5 , Y_max = 5 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = - 20 * np.exp( - 0.2 * np.sqrt( 0.5 * (X * * 2 + Y * * 2 ))) - \
np.exp( 0.5 * (np.cos( 2 * np.pi * X) + np.cos( 2 * np.pi * Y))) + np.e + 20
return X, Y, Z, 15 , "Ackley function"
# Sphere测试函数
def Sphere(X_min = - 3 , X_max = 3 , Y_min = - 3 , Y_max = 3 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = X * * 2 + Y * * 2
return X, Y, Z, 20 , "Sphere function"
# beale测试函数
def Beale(X_min = - 4.5 , X_max = 4.5 , Y_min = - 4.5 , Y_max = 4.5 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = np.power( 1.5 - X + X * Y, 2 ) + np.power( 2.25 - X + X * (Y * * 2 ), 2 ) \
+ np.power( 2.625 - X + X * (Y * * 3 ), 2 )
return X, Y, Z, 150000 , "Beale function"
# Booth测试函数
def Booth(X_min = - 10 , X_max = 10 , Y_min = - 10 , Y_max = 10 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = np.power(X + 2 * Y - 7 , 2 ) + np.power( 2 * X + Y - 5 , 2 )
return X, Y, Z, 2500 , "Booth function"
# Bukin测试函数
def Bukin(X_min = - 15 , X_max = - 5 , Y_min = - 3 , Y_max = 3 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = 100 * np.sqrt(np. abs (Y - 0.01 * X * * 2 )) + 0.01 * np. abs (X + 10 )
return X, Y, Z, 200 , "Bukin function"
# Three-hump camel测试函数
def three_humpCamel(X_min = - 5 , X_max = 5 , Y_min = - 5 , Y_max = 5 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = 2 * X * * 2 - 1.05 * X * * 4 + ( 1 / 6 ) * X * * 6 + X * Y + Y * 2
return X, Y, Z, 2000 , "three-hump camel function"
# Hölder table测试函数
def Holder_table(X_min = - 10 , X_max = 10 , Y_min = - 10 , Y_max = 10 ):
X, Y = get_X_AND_Y(X_min, X_max, Y_min, Y_max)
Z = - np. abs (np.sin(X) * np.cos(Y) * np.exp(np. abs ( 1 - np.sqrt(X * * 2 + Y * * 2 ) / np.pi)))
return X, Y, Z, 0 , "Hölder table function" , - 20
z_min = None
# X, Y, Z, z_max, id="codetool">
以下是上述代码绘制的测试函数的图像:
感觉图像的颜色还不是很好看,等之后优化了来改 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。 原文链接:https://blog.csdn.net/wang454592297/article/details/80336753 延伸 · 阅读
精彩推荐
|