《PSO的matlab源程序》由会员分享,可在线阅读,更多相关《PSO的matlab源程序(7页珍藏版)》请在金锄头文库上搜索。
1、基本粒子群优化算法Matlab源程序 这个程序就是最基本的粒子群优化算法程序,用Matlab实现,非常简单。只有几十行代码。 下面是主函数的源程序,优化函数则以m文件的形式放在fitness.m里面,对不同的优化函数只要修改fitness.m就可以了通用性很强。 主函数源程序(main.m)%-基本粒子群优化算法(Particle Swarm Optimization)-%-名称:基本粒子群优化算法(PSO)%-作用:求解优化问题%-说明:全局性,并行性,高效的群体智能算法%-作者:孙明杰(dreamsun2001)%-单位:中国矿业大学理学院计算数学硕2005%-时间:2006年8月17日
2、%-%-初始格式化-clear all;clc;format long;%-给定初始化条件-c1=1.4962; %学习因子1c2=1.4962; %学习因子2w=0.7298; %惯性权重MaxDT=1000; %最大迭代次数D=10; %搜索空间维数(未知数个数)N=40; %初始化群体个体数目eps=10(-6); %设置精度(在已知最小值时候用)%-初始化种群的个体(可以在这里限定位置和速度的范围)-for i=1:N for j=1:D x(i,j)=randn; %随机初始化位置 v(i,j)=randn; %随机初始化速度 endend%-先计算各个粒子的适应度,并初始化Pi和P
3、g-for i=1:N p(i)=fitness(x(i,:),D); y(i,:)=x(i,:);endpg=x(1,:); %Pg为全局最优for i=2:N if fitness(x(i,:),D)fitness(pg,D) pg=x(i,:); endend%-进入主要循环,按照公式依次迭代,直到满足精度要求-for t=1:MaxDT for i=1:N v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:)+c2*rand*(pg-x(i,:); x(i,:)=x(i,:)+v(i,:); if fitness(x(i,:),D)p(i) p(i)=fitne
4、ss(x(i,:),D); y(i,:)=x(i,:); end if p(i) p_best_fitness(count_x)p_best_fitness(count_x) = current_fitness(count_x);for count_y = 1:dimensionsp_best(count_x,count_y) = particle_position(count_x,count_y);endendend%decide on the global best among all the particlesg_best_val,g_best_index = max(current_f
5、itness);%g_best contains the position of teh global bestfor count_y = 1:dimensionsg_best(count_y) = particle_position(g_best_index,count_y); end%update the position and velocity compponentsfor count_x = 1:no_of_particlesfor count_y = 1:dimensionsp_current(count_y) = particle_position(count_x,count_y);endfor count_y = 1:dimensionsparticle_velocity(count_y) = particle_velocity(count_y) + c1*rand*(p_best(count_y)-p_current(count_y) + c2*rand*(g_best(count_y)-p_current(count_y);particle_positon(count_x,count_y) = p_current(count_y) +particle_velocity(count_y);