|
2-基于PSO改进策略:
百度网盘链接:
链接:http://pan.baidu.com/s/1pKKiFRd 密码:dsz0
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
如:自适应权重PSO代码如下:
- clc,clear,close all
- warning off
- format longG
- tic;
- % PSO 参数
- c1 = 1.4995;
- c2 = 1.4995;
- Vmin = -1;
- Vmax = 1;
- maxiter = 20; % 迭代次数
- sizepop = 20; % 种群数量
- popmin1 = -1; popmax1 = 1; % x1
- popmin2 = -1; popmax2 = 1; % x2
- % 初始化种群
- for i=1:sizepop
- x1 = popmin1 + (popmax1-popmin1)*rand;
- x2 = popmin2 + (popmax2-popmin2)*rand;
- pop(i,1) = x1;
- pop(i,2) = x2;
- fitness(i) = fun([x1,x2]);
- V(i,1) = 0;
- V(i,2) = 0;
- end
- % 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应度值
- fitnesszbest=bestfitness; % 全局最佳适应度值
- wmax = 0.9; wmin = 0.4;
- % 迭代寻优
- for i=1:maxiter
- for j=1:sizepop
- % 自适应权重1
- % w = wmin + (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( max(fitness)-min(fitness) );
- % 自适应权重2
- % w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
- % 自适应权重3
- if fitnessgbest(j)<=mean(fitness)
- w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
- else
- w = wmax;
- end
- % 速度更新
- V(j,:) = w*V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
- % V--x1
- if V(j,1)>Vmax
- V(j,1)=Vmax;
- end
- if V(j,1)<Vmin
- V(j,1)=Vmin;
- end
- % V--x2
- if V(j,2)>Vmax
- V(j,2)=Vmax;
- end
- if V(j,2)<Vmin
- V(j,2)=Vmin;
- end
- % 个体更新
- % pop(j,:) = pop(j,:) + 0.5 * V(j,:);
- pop(j,1) = pop(j,1) + 0.5 * V(j,1);
- pop(j,2) = pop(j,2) + 0.5 * V(j,2);
-
- % x1 越界限制
- if pop(j,1)>popmax1
- pop(j,1)=popmax1;
- end
- if pop(j,1)<popmin1
- pop(j,1)=popmin1;
- end
- % x2 越界限制
- if pop(j,2)>popmax2
- pop(j,2)=popmax2;
- end
- if pop(j,2)<popmin2
- pop(j,2)=popmin2;
- end
-
- % 适应度更新
- fitness(j) = fun(pop(j,:));
-
- % 比较 个体间比较
- if fitness(j)<fitnessgbest(j)
- fitnessgbest(j) = fitness(j);
- gbest(j,:) = pop(j,:);
- end
- if fitness(j)<bestfitness
- bestfitness = fitness(j);
- zbest = pop(j,:);
- end
-
- end
- fitness_iter(i) = bestfitness;
-
- end
- toc ;
- times = toc;
- fprintf('\n')
- disp(['计算时间 Time = ', num2str(times) ])
- fprintf('\n')
- disp(['最优解 ', num2str(zbest)])
- fprintf('\n')
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
复制代码 适应度函数:
- function y = fun(x)
- % y=-20*exp(-0.2*sqrt((x(1)^2+x(2)^2)/2))-exp((cos(2*pi*x(1))+cos(2*pi*x(2)))/2)+20+2.71289;
- y = (x(1)-0.5)^2 + (x(2)-0.6)^2;
- % y = (x(1)-0.3)^2 ;
复制代码
案例分享:
【1】PSO_source_error_NRMSE_惯性因子gbest,改进的PSO算法结构体化编程:对参数范围进行缩放的改进策略
【2】
|
|