粒子群优化算法(一):算法性能测试函数
数学优化问题需要一些函数测试算法性能,例如使用Rastrigin function测试PSO算法。Rastrigin function定义:
In order to define an instance of this function we need to provide the dimension of the problem (n). The optimum solution of the problem is the vector v = (0,…,0) with F(v) = 0.
二维空间,
From Wikipedia
It is a typical example of non-liner multimodal function. ”Finding the minimum of this function is fairly difficult problem due to its large search space and its large number of local minima.“
一个典型的非线性多峰函数。其大规模搜索区间和大量局部最小值,导致寻找全局最小值比较困难。
Wikipedia: Rastrigin function图像文件给出了matlat程序。我用python也画了一下,没有人家的好看,曲面平滑度不够。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure()
ax=Axes3D(fig)
X=np.arange(-5.12,5.12,0.1)
Y=np.arange(-5.12,5.12,0.1)
X,Y=np.meshgrid(X,Y)
Z=20+X*X+Y*Y-10*np.cos(2*np.pi*X)-10*np.cos(2*np.pi*Y)
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)#这句是X-Y平面加投影的
ax.set_zlim(0,100)
plt.show()
matplotlib的API没有细琢磨,修图什么的用到再学,雏形出来就好。