准粒群优化算法程序概要.doc

上传人:scccc 文档编号:13937515 上传时间:2022-01-27 格式:DOC 页数:5 大小:39KB
返回 下载 相关 举报
准粒群优化算法程序概要.doc_第1页
第1页 / 共5页
准粒群优化算法程序概要.doc_第2页
第2页 / 共5页
准粒群优化算法程序概要.doc_第3页
第3页 / 共5页
亲,该文档总共5页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《准粒群优化算法程序概要.doc》由会员分享,可在线阅读,更多相关《准粒群优化算法程序概要.doc(5页珍藏版)》请在三一文库上搜索。

1、%标准粒群优化算法程序 % 2007.1.9 By jxy% 测试函数:f(x,y)=100(xA2-yF2+(1-x)A2, -2.048x,y2.048 % 求解函数最小值global popsize; % 种群规模 %global popnum; % 种群数量 global pop; % 种群 %global c0; % 速度惯性系数 ,为 01 的随机数 global c1; % 个体最优导向系数 global c2; % 全局最优导向系数 global gbest_x; % 全局最优解 x 轴坐标 global gbest_y; % 全局最优解 y 轴坐标 global best_f

2、itness; % 最优解 global best_in_history; % 最优解变化轨迹 global x_min; %x 的下限 global x_max; %x 的上限 global y_min; %y 的下限 global y_max; %y 的上限 global gen; % 迭代次数 global exetime; % 当前迭代次数 global max_velocity; % 最大速度initial; % 初始化for exetime=1:gen outputdata; % 实时输出结果 adapting; % 计算适应值 errorcompute(); % 计算当前种群适值

3、标准差 updatepop; % 更新粒子位置pause(0.01);end clear i;clear exetime; clear x_max;clear x_min; clear y_min;clear y_max;% 程序初始化gen=100; % 设置进化代数 popsize=30; % 设置种群规模大小best_in_history(gen)=inf; %初始化全局历史最优解best_in_history( 1 ? =inf; %初始化全局历史最优解 max_velocity=0.3; %最大速度限制best_fitness=inf;%popnum=1; %设置种群数量pop(po

4、psize,8)=0; %初始化种群,创建popsize行5列的0矩阵%种群数组第1列为x轴坐标,第2列为y轴坐标,第3列为x轴速度分量,第4列为y轴速度分量 %第5列为个体最优位置的x轴坐标,第6列为个体最优位置的y轴坐标%第7列为个体最优适值,第8列为当前个体适应值for i=1:popsizepop(i,1)=4*rand()-2; %初始化种群中的粒子位置,值为-2 2,步长为其速度pop(i,2)=4*rand()-2; %初始化种群中的粒子位置,值为-2 2,步长为其速度pop(i,5)=pop(i,1); %初始状态下个体最优值等于初始位置pop(i,6)=pop(i,2); %

5、初始状态下个体最优值等于初始位置pop(i,3)=rand()*0.02-0.01; %初始化种群微粒速度,值为 -0.01 0.01,间隔为 0.0001pop(i,4)=rand()*0.02-0.01; %初始化种群微粒速度,值为 -0.01 0.01,间隔为 0.0001pop(i,7)=inf;pop(i,8)=inf;end c1=2;c2=2; x_min=-2; y_min=-2; x_max=2; y_max=2;gbest_x=pop(1,1); %全局最优初始值为种群第一个粒子的位置gbest_y=pop(1,2);%适值计算 % 测试函数为 f(x,y)=100(xA2

6、-y)A2+(1-x)A2, -2.048x,ypop(i,8) %若当前适应值优于个体最优值,则进行个体最优信息的更新pop(i,7)=pop(i,8); % 适值更新pop(i,5:6)=pop(i,1:2); % 位置坐标更新end end%计算完适应值后寻找当前全局最优位置并记录其坐标if best_fitnessmin(pop(:,7) best_fitness=min(pop(:,7); % 全局最优值 gbest_x=pop(find(pop(:,7)=min(pop(:,7),1); %全局最优粒子的位置gbest_y=pop(find(pop(:,7)=min(pop(:,7

7、),2);end best_in_history(exetime)=best_fitness; % 记录当前全局最优%实时输出结果%输出当前种群中粒子位置 subplot(1,2,1);for i=1:popsize plot(pop(i,1),pop(i,2),b*);hold on;endplot(gbest_x,gbest_y,r .,markersize,20);axis(-2,2,-2,2);hold off;subplot(1,2,2);axis(0,gen,-0.00005,0.00005);if exetime-10 line(exetime-1,exetime,best_in

8、_history(exetime-1),best_fitness);hold on;end%粒子群速度与位置更新%更新粒子速度更新速度for i=1:popsizepop(i,3)=rand()*pop(i,3)+c1*rand()*(pop(i,5)-pop(i,1)+c2*rand()*(gbest_x-pop(i,1); % pop(i,4)=rand()*pop(i,4)+c1*rand()*(pop(i,6)-pop(i,2)+c2*rand()*(gbest_x-pop(i,2);if abs(pop(i,3)max_velocityif pop(i,3)0 pop(i,3)=ma

9、x_velocity;else pop(i,3)=-max_velocity;endendif abs(pop(i,4)max_velocityif pop(i,4)0pop(i,4)=max_velocity;elsepop(i,4)=-max_velocity; endendend%更新粒子位置for i=1:popsizepop(i,1)=pop(i,1)+pop(i,3); pop(i,2)=pop(i,2)+pop(i,4); end% A SIMPLE IMPLEMENTATION OF THE % Particle Swarm Optimization IN MATLAB fun

10、ction xmin, fxmin, iter = PSO()% Initializing variablessuccess = 0; PopSize = 30; MaxIt = 100; iter = 0;fevals = 0; c1 = 2;c2 = 2; w = 0.6;% Success flag% Size of the swarm% Maximum number of iterations % Iterations counter% Function evaluations counter % PSO parameter C1 % PSO parameter C2% inertia

11、 weight% Objective Functionf = ADeJo ngHdim = 10; upbnd = 10;% Dimension of the problem% Upper bound for init. of the swarmlwbnd = -5;GM = 0;ErrGoal = 0.0001;% Lower bound for init. of the swarm% Global minimum (used in the stopping criterion)% Desired accuracy% Initializing swarm and velocitiespopu

12、l = rand(dim, PopSize)*(upbnd-lwbnd) + lwbnd; vel = rand(dim, PopSize);for i = 1:PopSize, fpopul(i) = feval(f, popul(:,i); fevals = fevals + 1;end bestpos = popul; fbestpos = fpopul;% Finding best particle in initial population fbestpart,g = min(fpopul);lastbpf = fbestpart;while (success = 0) & (ite

13、r MaxIt),iter = iter + 1;% VELOCITY UPDATEfor i=1:PopSize, A(:,i) = bestpos(:,g);endR1 = rand(dim, PopSize);R2 = rand(dim, PopSize);vel = w*vel + c1*R1.*(bestpos-popul) + c2*R2.*(A-popul);% SWARMUPDA TE popul = popul + vel;% Evaluate the new swarmfor i = 1:PopSize, fpopul(i) = feval(f,popul(:, i); f

14、evals = fevals + 1;end% Updating the best position for each particle changeColumns = fpopul fbestpos;fbestpos = fbestpos.*( 1-changeColumns) + fpopul.*changeColumns; bestpos(:, find(changeColumns) = popul(:, find(changeColumns);% Updating index g fbestpart, g = min(fbestpos); currentTime = etime(clock,startTime);% Checking stopping criterion if abs(fbestpart-GM) = ErrGoal success = 1;elselastbpf = fbestpart;endend% Output arguments xmin = popul(:,g); fxmin = fbestpos(g);fprintfL The best vector is : nA);fprintfL- %g A,xmi n); fprintf(AnA);%= function DeJong=DeJong(x)DeJong = sum(x.A2);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1