背景
粒子群算法最初是从研究鸟群捕食时得到的灵感,对于鸟群而言,如何在一大片区域中用有限的时间,最快地找到食物丰盛的领地,是一个决定鸟群生存繁衍的大问题。科学家们发现,单只鸟所能搜索的范围十分有限,但整个鸟群却像是被控制一般,很快聚集在食物最丰盛的地方。我们将每只鸟抽象成一个“粒子”(particle),每个粒子都有自己的位置和速度,并记忆自己曾经飞过的最好位置和群体曾经飞过的最好位置,那么在不断迭代的过程中,整体鸟群就很可能向最优目标出飞去。
算法的整个流程图如下:
参数:
任务
实现下列代码:
where is the velocity of the particle of the iteration. is the position of the particle of the iteration. is called pbest in this paper, which means the previous position with the best fitness value discovered by the particle before or in the iteration. is called gbest in this paper, which means the previous position with the best fitness value discovered by the whole swarm before or in the iteration.
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 12 21:33:35 2018
@author: Liang Qingyuan
"""
import numpy as np
import random
import matplotlib.pyplot as plt
import math
#----------------------PSO参数设置---------------------------------
class PSO():
def __init__(self,pN,dim,max_iter):
self.w = 1 #惯性权重
self.c1 = 1 #pbest影响权重
self.c2 = 1 #gbest影响权重
self.r1= random.uniform(0,1)#粒子的个体学习因子
self.r2= random.uniform(0,1)#社会的学习因子
self.pN = pN #粒子数量
self.dim = dim #搜索维度
self.max_iter = max_iter #迭代次数
self.X = np.zeros((self.pN,self.dim)) #所有粒子的位置和速度
self.V = np.zeros((self.pN,self.dim))
self.pbest = np.zeros((self.pN,self.dim)) #个体经历的最佳位置和全局最佳位置
self.gbest = np.zeros((1,self.dim))
self.p_fit = np.zeros(self.pN) #每个个体的历史最佳适应值
self.fit = 1e10 #全局最佳适应值
#---------------------目标函数------------------------------------
def function(self,x):
sum = 0
length = len(x)
x = x**2
for i in range(length):
sum += x[i]
return math.sqrt(sum)
#---------------------初始化种群----------------------------------
def init_Population(self):
for i in range(self.pN):
for j in range(self.dim):
self.X[i][j] = random.uniform(-10,10)
#self.V[i][j] = random.uniform(0,1)
self.pbest[i] = self.X[i]
tmp = self.function(self.X[i])
self.p_fit[i] = tmp
if(tmp < self.fit):
self.fit = tmp
self.gbest = self.X[i]
#--------------------粒子迭代过程----------------------------------
def iterator(self):
fitness = []
f_gbest=[]
#开始迭代
for t in range(self.max_iter):
#更新速度和位置
for i in range(self.pN):
for j in range(self.dim):
self.V[i][j] = self.w*self.V[i][j] + self.c1*self.r1*(self.pbest[i][j] - self.X[i][j]) + \
self.c2*self.r2*(self.gbest[j] - self.X[i][j])
#更新下一次迭代时的位置
self.X[i] = self.X[i] + self.V[i]
#寻找最优解
temp = self.function(self.X[i])
if(temp<self.p_fit[i]): #更新个体最优#
self.p_fit[i] = temp #个体最优结果
self.pbest[i] = self.X[i] #个体最优位置
if(self.p_fit[i] < self.fit): #更新全局最优#
self.gbest = self.X[i] #全局最优位置
self.fit = self.p_fit[i] #全局最优结果
#确定目前最优值
fitness.append(self.fit)
f_gbest.append(self.gbest)
print("#####fit#####",self.fit) #输出最优值
print("#####f-gbest#####",self.gbest) #输出最优值位置
return fitness,f_gbest
#----------------------程序执行-----------------------
my_pso = PSO(pN=10,dim=5,max_iter=10000)
my_pso.init_Population()
fitness,f_gbest = my_pso.iterator()
#-----------------------效果展示--------------------------
plt.figure(1)
plt.title("Figure")
plt.xlabel("iterators", size=14)
plt.ylabel("fitness", size=14)
t = np.array([t for t in range(0,10000)])
fitness = np.array(fitness)
plt.plot(t,fitness, color='b',linewidth=1)
plt.show()
此次迭代过程:
最优值:0.7036775934527754
最优值时位置:[ -6.23916674 7.45154406 -19.60875577 -0.89308242 5.17591376]
参考文献:
https://blog.csdn.net/as645788/article/details/70821430