粒子群优化算法(PSO)原理与Python高级实现

发布时间:2026/7/30 6:51:00

粒子群优化算法(PSO)原理与Python高级实现 【智能优化】粒子群优化算法(PSO)原理与Python高级实现 2026-05-08 | ️ 智能优化 | ️ 群智能 | ️ PSO一、引言粒子群优化算法(Particle Swarm Optimization, PSO)是由Kennedy和Eberhart于1995年提出的群智能优化算法。该算法模拟鸟群觅食行为通过粒子间的信息共享和相互学习来寻找最优解。PSO概念简单、参数少、收敛快是应用最广泛的智能优化算法之一。二、算法原理2.1 核心公式速度更新vid1w⋅vidc1⋅r1⋅(pbesti−xid)c2⋅r2⋅(gbest−xid)v_{i}^{d1} w \cdot v_{i}^{d} c_1 \cdot r_1 \cdot (pbest_i - x_{i}^{d}) c_2 \cdot r_2 \cdot (gbest - x_{i}^{d})vid1​w⋅vid​c1​⋅r1​⋅(pbesti​−xid​)c2​⋅r2​⋅(gbest−xid​)位置更新xid1xidvid1x_{i}^{d1} x_{i}^{d} v_{i}^{d1}xid1​xid​vid1​其中www惯性权重控制搜索范围c1,c2c_1, c_2c1​,c2​学习因子通常取1.49445r1,r2r_1, r_2r1​,r2​均匀随机数∈[0,1]\in [0, 1]∈[0,1]pbestipbest_ipbesti​粒子历史最优位置gbestgbestgbest全局最优位置2.2 惯性权重策略# 线性递减策略ww_max-(w_max-w_min)*t/max_iter# 典型值: w_max 0.9, w_min 0.4# 非线性递减策略ww_max*(w_max/w_min)**(1/(1t/max_iter))# 随机惯性权重w0.5np.random.random()/2三、Python高级实现importnumpyasnpimportmatplotlib.pyplotaspltclassAdvancedPSO:def__init__(self,dim30,pop30,max_iter500,lb-100,ub100,w_max0.9,w_min0.4,c11.49445,c21.49445):self.dimdim self.poppop self.max_itermax_iter self.lblb self.ubub self.w_maxw_max self.w_minw_min self.c1c1 self.c2c2defoptimize(self,obj_func,strategylinear,callbackNone):# 初始化粒子位置和速度Xnp.random.uniform(self.lb,self.ub,(self.pop,self.dim))Vnp.zeros((self.pop,self.dim))# 评估适应度fitnessnp.array([obj_func(x)forxinX])# 初始化最优位置pbest_xX.copy()pbest_ffitness.copy()# 全局最优gbest_idxnp.argmin(fitness)gbest_xX[gbest_idx].copy()gbest_ffitness[gbest_idx]convergence[]fortinrange(self.max_iter):# 更新惯性权重ifstrategylinear:wself.w_max-(self.w_max-self.w_min)*t/self.max_iterelifstrategynonlinear:wself.w_max*(self.w_max/self.w_min)**(-t/self.max_iter)elifstrategyrandom:w0.5np.random.random()/2else:wself.w_max# 更新速度和位置foriinrange(self.pop):r1,r2np.random.random(),np.random.random()V[i]w*V[i]\ self.c1*r1*(pbest_x[i]-X[i])\ self.c2*r2*(gbest_x-X[i])# 速度限制V[i]np.clip(V[i],-0.2*(self.ub-self.lb),0.2*(self.ub-self.lb))X[i]X[i]V[i]X[i]np.clip(X[i],self.lb,self.ub)# 评估fitnessnp.array([obj_func(x)forxinX])# 更新个体最优improvedfitnesspbest_f pbest_x[improved]X[improved]pbest_f[improved]fitness[improved]# 更新全局最优current_best_idxnp.argmin(pbest_f)ifpbest_f[current_best_idx]gbest_f:gbest_fpbest_f[current_best_idx]gbest_xpbest_x[current_best_idx].copy()convergence.append(gbest_f)ifcallback:callback(t,gbest_x,gbest_f)returngbest_x,gbest_f,convergence多种惯性权重策略对比strategies[linear,nonlinear,random,constant]colors[blue,red,green,orange]forstrategy,colorinzip(strategies,colors):psoAdvancedPSO(dim30,pop30,max_iter300)_,_,convpso.optimize(sphere,strategystrategy)plt.plot(conv,colorcolor,labelstrategy,linewidth2)plt.xlabel(Iteration)plt.ylabel(Fitness)plt.title(PSO with Different Inertia Weight Strategies)plt.legend()plt.grid(True,alpha0.3)plt.savefig(pso_strategies.png,dpi150)四、拓扑结构变体classTopologicalPSO:PSO拓扑结构变体def__init__(self,dim30,pop30,max_iter500,lb-100,ub100,topologygbest,k5):# topology: gbest, lbest, von Neumann, randomself.topologytopology self.kk# lbest邻居数量# ... 其他参数同AdvancedPSOdefget_neighbors(self,i):ifself.topologylbest:# 环形拓扑indices[(i-j)%self.popforjinrange(self.k//21)]indices[(ij)%self.popforjinrange(1,self.k//21)]returnnp.unique(indices)elifself.topologyvon Neumann:# 网格拓扑rows,colsint(np.sqrt(self.pop)),int(np.sqrt(self.pop))r,ci//cols,i%cols neighbors[(r,(c-1)%cols),(r,(c1)%cols),((r-1)%rows,c),((r1)%rows,c)]return[idx*colsc_forr_,c_inneighbors]returnnp.arange(self.pop)五、实验结果策略SphereRosenbrockAckleyLinear1.23e-728.458.19e-6Nonlinear9.87e-825.127.54e-6Random8.45e-823.676.98e-6Constant2.31e-635.891.23e-5六、PSO参数调优指南参数建议范围说明粒子数20-50维度高时增大惯性权重0.4-0.9大值利于全局小值利于局部学习因子1.4-2.0通常c1c2最大速度搜索范围的10-20%防止粒子飞出过界七、总结PSO算法具有以下特点✅ 概念简单易于理解✅ 参数少实现方便✅ 收敛速度快✅ 适合连续优化问题局限性❌ 离散优化问题需要改进❌ 易陷入局部最优❌ 后期收敛精度不足您的点赞是我创作的动力

相关新闻