Python 实现粒子群算法

粒子群算法原理很简单,用matlab和python都很快实现编程。

程序:

参数部分,需要修改的可以修改。这个程序实现的是基本粒子群算法,对于提升粒子群算法的表现,

可以在上面进行更多的功能添加。

import numpy as np import random import matplotlib.pyplot as plt #———————-PSO参数设置——————————— class PSO():     def __init__(self,pN,dim,max_iter):         self.w = 0.8          self.c1 = 2           self.c2 = 2           self.r1= 0.6         self.r2=0.3         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             #全局最佳适应值         #———————目标函数Sphere函数—————————–     def function(self,x):         sum = 0         length = len(x)         x = x**2         for i in range(length):             sum += x[i]         return sum #———————初始化种群———————————-     def init_Population(self):         for i in range(self.pN):             for j in range(self.dim):                 self.X[i][j] = random.uniform(0,1)                 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 = []         for t in range(self.max_iter):             for i in range(self.pN):         #更新gbest\pbest                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]             for i in range(self.pN):                 self.V[i] = self.w*self.V[i] + self.c1*self.r1*(self.pbest[i] – self.X[i])\                        + self.c2*self.r2*(self.gbest – self.X[i])                 self.X[i] = self.X[i] + self.V[i]             fitness.append(self.fit)             print(self.fit)                   #输出最优值         return fitness #———————-程序执行———————– my_pso = PSO(pN=30,dim=5,max_iter=100) my_pso.init_Population() fitness = my_pso.iterator() #——————-画图——————– plt.figure(1) plt.title(“Figure1”) plt.xlabel(“iterators”, size=14) plt.ylabel(“fitness”, size=14) t = np.array([t for t in range(0,100)]) fitness = np.array(fitness) plt.plot(t,fitness, color=’b’,linewidth=3) plt.show()

计算结果:fitness = 8.64346866606e-07

    原文作者:蚁群算法
    原文地址: https://blog.csdn.net/kunshanyuz/article/details/63683145
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞