23个基准测试函数
- 优化算法常用的适应度函数
- 23个基准测试函数的图像
- python版code
- 将以上代码打包为exe
优化算法常用的适应度函数
23个基准测试函数的图像
python版code
将23个基准测试函数封装成python库,并进行实例化、调用绘图函数。下面代码只需根据提示打印,输入想要绘制的函数名称或all, 即可绘制相应基准测试函数,并将生成的静态图保存至当前文件夹中的fig文件夹、动态图保存至gif文件夹。
# -*- coding: UTF-8 -*-
"""
@IDE : PyCharm
@project: 23个基准函数绘制
@file :plot_benchmark_functions.py
@date :2023-09-09 1:37
@Author : 十@八九
@email :
"""
import os
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation as animation
class PlotFunction:
def __int__(self):
self.select_func = None
return
def F1(self, X):
Results = np.sum(X ** 2)
return Results
def F2(self, X):
Results = np.sum(np.abs(X)) + np.prod(np.abs(X))
return Results
def F3(self, X):
dim = X.shape[0]
Results = 0
for i in range(dim):
Results = Results + np.sum(X[0:i + 1]) ** 2
return Results
def F4(self, X):
Results = np.max(np.abs(X))
return Results
def F5(self, X):
dim = X.shape[0]
Results = np.sum(100 * (X[1:dim] - (X[0:dim - 1] ** 2)) ** 2 + (X[0:dim - 1] - 1) ** 2)
return Results
def F6(self, X):
Results = np.sum(np.abs(X + 0.5) ** 2)
return Results
def F7(self, X):
dim = X.shape[0]
Temp = np.arange(1, dim + 1, 1)
Results = np.sum(Temp * (X ** 4)) + np.random.random()
return Results
def F8(self, X):
Results = np.sum(-X * np.sin(np.sqrt(np.abs(X))))
return Results
def F9(self, X):
dim = X.shape[0]
Results = np.sum(X ** 2 - 10 * np.cos(2 * np.pi * X)) + 10 * dim
return Results
def F10(self, X):
dim = X.shape[0]
Results = -20 * np.exp(-0.2 * np.sqrt(np.sum(X ** 2) / dim)) - np.exp(
np.sum(np.cos(2 * np.pi * X)) / dim) + 20 + np.exp(1)
return Results
def F11(self, X):
dim = X.shape[0]
Temp = np.arange(1, dim, 1)
Results = np.sum(X ** 2) / 4000 - np.prod(np.cos(X / np.sqrt(Temp))) + 1
return Results
def Ufun(self, x, a, k, m):
Results = k * ((x - a) ** m) * (x > a) + k * ((-x - a) ** m) * (x < -a)
return Results
def F12(self, X):
dim = X.shape[0]
Results = (np.pi / dim) * (10 * ((np.sin(np.pi * (1 + (X[0] + 1) / 4))) ** 2) + \
np.sum(((X[0:dim - 1] + 1) / 4) ** 2) * (
1 + 10 * ((np.sin(np.pi * (1 + X[1:dim] + 1) / 4)) ** 2)) + \
((X[dim - 1] + 1) / 4) ** 2) + np.sum(self.Ufun(X, 10, 100, 4))
return Results
def F13(self, X):
dim = X.shape[0]
Results = 0.1 * ((np.sin(3 * np.pi * X[0])) ** 2 + np.sum(
(X[0:dim - 1] - 1) ** 2 * (1 + (np.sin(3 * np.pi * X[1:dim])) ** 2)) + \
((X[dim - 1] - 1) ** 2) * (1 + (np.sin(2 * np.pi * X[dim - 1])) ** 2)) + np.sum(
self.Ufun(X, 5, 100, 4))
return Results
def F14(self, X):
aS = np.array(
[[-32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32], \
[-32, -32, -32, -32, -32, -16, -16, -16, -16, -16, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32]])
bS = np.zeros(25)
for i in range(25):
bS[i] = np.sum((X - aS[:, i]) ** 6)
Temp = np.arange(1, 26, 1)
Results = (1 / 500 + np.sum(1 / (Temp + bS))) ** (-1)
return Results
def F15(self, X):
aK = np.array([0.1957, 0.1947, 0.1735, 0.16, 0.0844, 0.0627, 0.0456, 0.0342, 0.0323, 0.0235, 0.0246])
bK = np.array([0.25, 0.5, 1, 2, 4, 6, 8, 10, 12, 14, 16])
bK = 1 / bK
Results = np.sum((aK - ((X[0] * (bK ** 2 + X[1] * bK)) / (bK ** 2 + X[2] * bK + X[3]))) ** 2)
return Results
def F16(self, X):
Results = 4 * (X[0] ** 2) - 2.1 * (X[0] ** 4) + (X[0] ** 6) / 3 + X[0] * X[1] - 4 * (X[1] ** 2) + 4 * (
X[1] ** 4)
return Results
def F17(self, X):
Results = (X[1] - (X[0] ** 2) * 5.1 / (4 * (np.pi ** 2)) + (5 / np.pi) * X[0] - 6) ** 2 + 10 * (
1 - 1 / (8 * np.pi)) * np.cos(X[0]) + 10
return Results
def F18(self, X):
Results = (1 + (X[0] + X[1] + 1) ** 2 * (
19 - 14 * X[0] + 3 * (X[0] ** 2) - 14 * X[1] + 6 * X[0] * X[1] + 3 * X[1] ** 2)) * \
(30 + (2 * X[0] - 3 * X[1]) ** 2 * (
18 - 32 * X[0] + 12 * (X[0] ** 2) + 48 * X[1] - 36 * X[0] * X[1] + 27 * (X[1] ** 2)))
return Results
def F19(self, X):
aH = np.array([[3, 10, 30], [0.1, 10, 35], [3, 10, 30], [0.1, 10, 35]])
cH = np.array([1, 1.2, 3, 3.2])
pH = np.array(
[[0.3689, 0.117, 0.2673], [0.4699, 0.4387, 0.747], [0.1091, 0.8732, 0.5547], [0.03815, 0.5743, 0.8828]])
Results = 0
for i in range(4):
Results = Results - cH[i] * np.exp(-(np.sum(aH[i, :] * ((X - pH[i, :])) ** 2)))
return Results
def F20(self, X):
aH = np.array([[10, 3, 17, 3.5, 1.7, 8], [0.05, 10, 17, 0.1, 8, 14], [3, 3.5, 1.7, 10, 17, 8],
[17, 8, 0.05, 10, 0.1, 14]])
cH = np.array([1, 1.2, 3, 3.2])
pH = np.array(
[[0.1312, 0.1696, 0.5569, 0.0124, 0.8283, 0.5886], [0.2329, 0.4135, 0.8307, 0.3736, 0.1004, 0.9991], \
[0.2348, 0.1415, 0.3522, 0.2883, 0.3047, 0.6650], [0.4047, 0.8828, 0.8732, 0.5743, 0.1091, 0.0381]])
Results = 0
for i in range(4):
Results = Results - cH[i] * np.exp(-(np.sum(aH[i, :] * ((X - pH[i, :])) ** 2)))
return Results
def F21(self, X):
aSH = np.array([[4, 4, 4, 4], [1, 1, 1, 1], [8, 8, 8, 8], [6, 6, 6, 6], [3, 7, 3, 7], \
[2, 9, 2, 9], [5, 5, 3, 3], [8, 1, 8, 1], [6, 2, 6, 2], [7, 3.6, 7, 3.6]])
cSH = np.array([0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5])
Results = 0
for i in range(5):
Results = Results - (np.dot((X - aSH[i, :]), (X - aSH[i, :]).T) + cSH[i]) ** (-1)
return Results
def F22(self, X):
aSH = np.array([[4, 4, 4, 4], [1, 1, 1, 1], [8, 8, 8, 8], [6, 6, 6, 6], [3, 7, 3, 7], \
[2, 9, 2, 9], [5, 5, 3, 3], [8, 1, 8, 1], [6, 2, 6, 2], [7, 3.6, 7, 3.6]])
cSH = np.array([0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5])
Results = 0
for i in range(7):
Results = Results - (np.dot((X - aSH[i, :]), (X - aSH[i, :]).T) + cSH[i]) ** (-1)
return Results
def F23(self, X):
aSH = np.array([[4, 4, 4, 4], [1, 1, 1, 1], [8, 8, 8, 8], [6, 6, 6, 6], [3, 7, 3, 7], \
[2, 9, 2, 9], [5, 5, 3, 3], [8, 1, 8, 1], [6, 2, 6, 2], [7, 3.6, 7, 3.6]])
cSH = np.array([0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5])
Results = 0
for i in range(10):
Results = Results - (np.dot((X - aSH[i, :]), (X - aSH[i, :]).T) + cSH[i]) ** (-1)
return Results
def rotate(self, angle):
self.ax.view_init(elev=30, azim=angle)
def plot_save(self, func, x1_range=(0, 10, 0.5), x2_range=(0, 10, 0.5), function_name='F1'):
fig = plt.figure(1) # 定义figure
# ax = Axes3D(fig) # 将figure变为3d
ax = plt.axes(projection='3d')
self.ax = ax
fig.add_axes(ax)
x1 = np.arange(x1_range[0], x1_range[1], x1_range[2])
x2 = np.arange(x2_range[0], x2_range[1], x2_range[2])
X1, X2 = np.meshgrid(x1, x2) # 生成网格
nSize = x1.shape[0]
Z = np.zeros([nSize, nSize])
for i in range(nSize):
for j in range(nSize):
if function_name in ['F15', 'F16', 'F21', 'F22', 'F23']:
X = [X1[i, j], X2[i, j], 0, 0, ]
elif function_name in ['F19']:
X = [X1[i, j], X2[i, j], 0]
elif function_name in ['F20']:
X = [X1[i, j], X2[i, j], 0, 0, 0, 0]
else:
X = [X1[i, j], X2[i, j]] # 构造F23输入
X = np.array(X) # 将格式由list转换为array
Z[i, j] = func(X) # 计算F23的值
# 绘制3D曲面
# rstride:行之间的跨度 cstride:列之间的跨度
# rstride:行之间的跨度 cstride:列之间的跨度
# cmap参数可以控制三维曲面的颜色组合
ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
ax.contour(X1, X2, Z, zdir='z', offset=-0.1) # 绘制等高线
ax.set_xlabel('X1') # x轴说明
ax.set_ylabel('X2') # y轴说明
ax.set_zlabel('Z') # z轴说明
ax.set_title(function_name + '_space')
plt.savefig('./fig/' + function_name + '.png')
anim = animation.FuncAnimation(fig, self.rotate, frames=range(0, 360, 10))
anim.save('./gif/' + function_name + '.gif', writer='pillow', fps=30)
if select_func == 'all':
plt.pause(5)
plt.close()
else:
plt.show()
return
def selectFunc(self, func_str='F1', x1_range=(0, 10, 0.5), x2_range=(0, 10, 0.5), function_name='F1'):
if func_str == 'F1':
self.plot_save(self.F1, x1_range=(-100, 100, 2), x2_range=(-100, 100, 2), function_name='F1')
elif func_str == 'F2':
self.plot_save(self.F2, x1_range=(-10, 10, 0.2), x2_range=(-10, 10, 0.2), function_name='F2')
elif func_str == 'F3':
self.plot_save(self.F3, x1_range=(-100, 100, 2), x2_range=(-100, 100, 2), function_name='F3')
elif func_str == 'F4':
self.plot_save(self.F4, x1_range=(-100, 100, 2), x2_range=(-100, 100, 2), function_name='F4')
elif func_str == 'F5':
self.plot_save(self.F5, x1_range=(-30, 30, 2), x2_range=(-30, 30, 2), function_name='F5')
elif func_str == 'F6':
self.plot_save(self.F6, x1_range=(-100, 100, 2), x2_range=(-100, 100, 2), function_name='F6')
elif func_str == 'F7':
self.plot_save(self.F7, x1_range=(-1.28, 1.28, 0.02), x2_range=(-1.28, 1.28, 0.02), function_name='F7')
elif func_str == 'F8':
self.plot_save(self.F8, x1_range=(-500, 500, 10), x2_range=(-500, 500, 10), function_name='F8')
elif func_str == 'F9':
self.plot_save(self.F9, x1_range=(-5.12, 5.12, 0.2), x2_range=(-5.12, 5.12, 0.2), function_name='F9')
elif func_str == 'F10':
self.plot_save(self.F10, x1_range=(-30, 30, 0.5), x2_range=(-30, 30, 0.5), function_name='F10')
elif func_str == 'F11':
self.plot_save(self.F11, x1_range=(-600, 600, 5), x2_range=(-600, 600, 5), function_name='F11')
elif func_str == 'F12':
self.plot_save(self.F12, x1_range=(-50, 50, 1), x2_range=(-50, 50, 1), function_name='F12')
elif func_str == 'F13':
self.plot_save(self.F13, x1_range=(-50, 50, 1), x2_range=(-50, 50, 1), function_name='F13')
elif func_str == 'F14':
self.plot_save(self.F14, x1_range=(-65, 65, 2), x2_range=(-65, 65, 2), function_name='F14')
elif func_str == 'F15':
self.plot_save(self.F15, x1_range=(-5, 5, 0.2), x2_range=(-5, 5, 0.2), function_name='F15')
elif func_str == 'F16':
self.plot_save(self.F16, x1_range=(-5, 5, 0.2), x2_range=(-5, 5, 0.2), function_name='F16')
elif func_str == 'F17':
self.plot_save(self.F17, x1_range=(-5, 5, 0.2), x2_range=(-5, 5, 0.2), function_name='F17')
elif func_str == 'F18':
self.plot_save(self.F18, x1_range=(-2, 2, 0.1), x2_range=(-2, 2, 0.1), function_name='F18')
elif func_str == 'F19':
self.plot_save(self.F19, x1_range=(1, 3, 0.1), x2_range=(1, 3, 0.1), function_name='F19')
elif func_str == 'F20':
self.plot_save(self.F20, x1_range=(0, 1, 0.05), x2_range=(0, 1, 0.05), function_name='F20')
elif func_str == 'F21':
self.plot_save(self.F21, x1_range=(0, 10, 0.5), x2_range=(0, 10, 0.5), function_name='F21')
elif func_str == 'F22':
self.plot_save(self.F22, x1_range=(0, 10, 0.5), x2_range=(0, 10, 0.5), function_name='F22')
elif func_str == 'F23':
self.plot_save(self.F22, x1_range=(0, 10, 0.5), x2_range=(0, 10, 0.5), function_name='F23')
return
if __name__ == '__main__':
dir_fig = "./fig"
if not os.path.exists(dir_fig):
os.makedirs(dir_fig)
dir_gif = "./gif"
if not os.path.exists(dir_gif):
os.makedirs(dir_gif)
print("23 benchmark functions:")
func_name = ['F' + str(i) for i in range(1, 24, 1)]
func_str = ', '.join(func_name) + ', all'
print(func_str)
select_func = input("Please select one of the above functions and enter it:")
plotFunction_o = PlotFunction()
if select_func == 'all':
for func in func_name:
plotFunction_o.selectFunc(func)
else:
plotFunction_o.selectFunc(select_func)
将以上代码打包为exe
新建文件夹,如 23 benchmark functions, 路径中不要含有中文
在cmd窗口中打开该文件夹,
输入:pip install pipenv
输入:pipenv shell
输入:pip install pyinstaller
pip install numpy
pip install matplotlib
打包成.exe: pyinstaller -F --distpath Release/ -c --clean plot_benchmark_functions.py