|
百度网盘链接:
链接:https://pan.baidu.com/s/1UVCDcVFTqu0zC0lNYfdQLQ
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
程序如下:
- clc,clear,close all
- warning off
- % PIO算法
- R = 0.3; % 地磁场参数parameters of magnetic field
- T1=50; % Global search algebra,迭代次数
- T2=1000; % Local search algebra,迭代次数
- % BA算法参数
- % maxiter = 10; % 迭代次数
- sizepop = 10; % 蝙蝠种群数量
- pigeonnum=sizepop; % 鸽子种群数量
- % 频率范围
- popmin = [-1,-1]; % x下限
- popmax = [1, 1]; % x上限
- Qmin = 0.1; % 最小频率
- Qmax = 0.75; % 最大频率
- nvar = 2; % 未知量个数
- A = 0.1; % 音量 (不变或者减小)
- impluse = 0.65; % 脉冲率 (不变或增加)
- Vmin = -1; % 最小速度
- Vmax = 1; % 最大速度
- %% 初始化种群
- for i=1:pigeonnum
- x1 = popmin(1) + (popmax(1)-popmin(1))*rand;
- x2 = popmin(2) + (popmax(2)-popmin(2))*rand;
- pop(i,1) = x1;
- pop(i,2) = x2;
- fitness(i) = fun([x1,x2]);
- v(i,1)=rand;
- v(i,2)=rand;
- end
- clear x1 x2
- % 记录一组最优值
- [bestfitness,bestindex]=min(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应度值
- fitnesszbest=bestfitness; % 全局最佳适应度值
- %% 地图和指南针算子 magnetic compass and solar operator
- for t=1:T1 % 迭代次数
- for i=1:pigeonnum
- v(i,:)=v(i,:)*(1-exp(-R*t))+rand*(gbest(i,:)-pop(i,:));
- pop(i,:)=pop(i,:)+v(i,:); %check whether beyond the searching space
- for j=1:nvar
- if abs(i-1)<=eps
- if pop(i,j)<popmin(j)||pop(i,j)>popmax(j)
- pop(i,j)=popmin(j)+rand*(popmax(j)-popmin(j));
- pop(i,j)=rand;
- end
- else
- if pop(i,j)<popmin(j)||pop(i,j)>popmax(j)
- pop(i,j)=pop(i-1,j);
- v(i,j)=v(i-1,j);
- end
- end
- end
- fitness(i) = fun( pop(i,:) ); % 适应度函数
- % 比较 个体间比较
- if fitness(i)<fitnessgbest(i)
- fitnessgbest(i) = fitness(i);
- gbest(i,:) = pop(i,:);
- end
- if fitness(i)<bestfitness
- bestfitness = fitness(i);
- zbest = pop(i,:);
- end
- end
- % fitness_iter(t) = bestfitness;
- end
- pop = gbest; % 个体最佳
- fitness = fitnessgbest; %个体最佳适应度值
- %% 迭代寻优, BA算法
- for i=1:T2
- for j=1:sizepop
- Q = Qmin + (Qmax-Qmin)*rand;
- v(j,:) = v(j,:) + (pop(j,:)-zbest)*Q;
- % V--范围约束
- v(j, find(v(j,:)<Vmin))=Vmin;
- v(j, find(v(j,:)>Vmax))=Vmax;
-
- pop(j,:) = pop(j,:) + 0.5*v(j,:);
-
- % 脉冲率
- if rand>impluse
- pop(j,:) = zbest + A * randn(1,nvar);
- end
-
- % x1 越界限制
- for k=1:nvar
- if pop(j,k)>popmax(k)
- pop(j,k)=popmax(k);
- end
- if pop(j,k)<popmin(k)
- pop(j,k)=popmin(k);
- end
- 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
- disp('最优解')
- disp(zbest)
- fprintf('\n')
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- figure('color',[1,1,1])
- loglog(fitness_iter,'ro-','linewidth',2)
- axis tight
复制代码
|
|