牛骨文教育服务平台(让学习变的简单)
博文笔记

粒子群优化算法(一):算法性能测试函数

创建时间:2017-03-31 投稿人: 浏览次数:1999

数学优化问题需要一些函数测试算法性能,例如使用Rastrigin function测试PSO算法。Rastrigin function定义:

f(x⃗ )=An+∑i=1n[x2i−cos(2πxi)]

A=10,xiϵ[−5.12,5.12]。这里的x⃗ 是矢量,所以f(x⃗ )是一个有维度的函数。Wikipedia: Rastrigin function里用加粗而非箭头表示矢量,我还眼瞎地以为是个bug。The Generalized 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.

二维空间,x=0,函数取到最小值f(x)=0 ;三维空间,x=0,y=0,函数取到最小值f(x,y)=0;以此类推。 之所以采用Rastrigin,是因为它的以下特点,对优化算法具有强烈的“媚惑”性:

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没有细琢磨,修图什么的用到再学,雏形出来就好。

Rastrigin function

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。