PSO优化算法

时间:2022-10-10 16:01:24


1.问题描述:

 目标函数为:

function [Y]=ackley(x,flag)
% global P
global T
% x=mapminmax('apply',x(1,:)',P); %1X5
% x=x';
W2=[0.983513649425645,0.0156395494399536,0.612578690108221,0.348949772433000,0.266166079005764,0.124947353178640,0.923296305479215,-0.530232046763045,-0.327659275580791,-0.646199831676460;-0.309231346225098,0.596414396236188,0.397002323830802,0.206433928667196,0.720492746343545,0.199389937406352,-0.116765991949494,-0.370247577740941,-0.154903175718849,-0.507975216600424;0.128681017244323,-0.524317286229423,0.186056741679819,0.555730447171981,0.531275865167625,0.182558138013602,0.384961025796518,0.385276782693936,0.605236026395329,0.456005242346597];
W1=[-0.869936793889867,0.325779285347892,0.174037972470458,0.295236135069579,-0.273241174515210;-0.558186480531010,-0.186696321927505,0.562936588956597,0.974180561609398,0.674207406347660;0.155857579269969,-0.265768777238990,-0.874132188809125,-0.0827875095457866,0.370921943805725;0.0131249090186139,-0.201982618964050,0.519301705908160,-0.863972727171152,0.987929292098583;0.433834177489895,0.886924527100349,0.274107586179760,-0.303859698977215,0.422001002322857;-0.371103364530077,-0.560145560430240,-0.825019796560491,0.661826279134213,-0.310314984764697;-0.170958734188943,-0.423544695342573,0.691412124257754,-0.329984037233651,0.0755035626115093;0.136436814280577,-0.0655486161253700,0.0170589401427046,-0.423034172683370,-0.665773389641811;0.413456404045930,-0.682126171802981,0.517447538050501,-0.783398569576221,0.650999262711329;-0.733753096788900,0.0503955169192474,0.697086294190505,-0.636672620606997,0.121973937413338];
B1=[0.263524090679658;0.619752887788955;-0.323729827867708;0.820582965938063;0.392485096567817;-0.966033068034410;0.228122237322837;0.0332417952094948;-0.184288988372871;0.841419482048720];
B2=[0.222608902861025;-0.0339919509291149;-0.511468813194705];
F(:,1)=W2(1,:)*(2./(1+exp(-2*(W1*x(1,:)'+B1)))-1)+B2(1);
F(:,2)=W2(2,:)*(2./(1+exp(-2*(W1*x(1,:)'+B1)))-1)+B2(2);
F(:,3)=W2(3,:)*(2./(1+exp(-2*(W1*x(1,:)'+B1)))-1)+B2(3);
f(1,:)=mapminmax('reverse',F(1,:)',T); %1X5
if flag==0
    Y=norm(F);
else
    Y=f;
end

2.部分程序:

%------初始格式化--------------------------------------------------
clear all;
clc;
format long;
global P
global T
data=xlsread('500PD-fordelPD.xlsx','sheet2');
x=data(:,1:5);
y=data(:,6:8);
x=x';
y=y';
[XX,P]=mapminmax(x,-1,1);
[YY,T]=mapminmax(y,-1,1);
%------给定初始化条件----------------------------------------------
c1=2;             %学习因子1
c2=2;             %学习因子2
% w=0.7298;              %惯性权重
MaxDT=200;            %最大迭代次数
% D=2;                  %搜索空间维数(未知数个数)
N=100;                  %初始化群体个体数目
%eps=10^(-6);           %设置精度(在已知最小值时候用)
Vmax=1;
Vmin=-1;
Data=[];
for kkk=1:10
popmax=[4,10,10,0.4,308];
popmin=[2.2,3,3,0.06,298];
%------初始化种群的个体(可以在这里限定位置和速度的范围)------------
for i=1:N
        pop(i,:)=rand(1,5).*(popmax-popmin)+popmin;  %随机初始化位置

        V(i,:)=rand(1,5); %随机初始化速度
        pop(i,:)=mapminmax('apply',pop(i,:)',P); %1X5
        fitness(i)=ackley(pop(i,:),0);
end
%------先计算各个粒子的适应度,并初始化Pi和Pg----------------------
[fitnessgbest bestindex]=min(fitness);
gbest=pop(bestindex,:);
pbest=pop;
fitnesspbest=fitness;
popmax=10;
popmin=-10;
for i=1:MaxDT
     w=0.4+0.8*rand(1,1);%随机惯性权重
    for j=1:N
        V(j,:)=w*V(j,:)+c1*rand*(pbest(j,:)-pop(j,:))+c2*rand*(gbest-pop(j,:));
        V(j,find(V(j,:)>Vmax))=Vmax;
        V(j,find(V(j,:)<Vmin))=Vmin;
        pop(j,:)=pop(j,:)+V(j,:);
        pop(j,find(pop(j,:)>popmax))=popmax;
        pop(j,find(pop(j,:)<popmin))=popmin;

%         if rand>0.8
%             k=ceil(2*rand);
%             pop(j,k)=rand;
%         end        
%         muu=mapminmax('apply',pop(j,:)',P); %1X5
%         pop(j,:)=muu';
        fitness(j)=ackley(pop(j,:),0);

       if fitness(j)<fitnesspbest(j)
            pbest(j,:)=pop(j,:);
            fitnesspbest(j)=fitness(j);
       end

       if fitness(j)<fitnessgbest
           gbest=pop(j,:);
           fitnessgbest=fitness(j);  
       end

    end             %一次
  yy(i)=fitnessgbest;
  fprintf('已完成迭代次数:%s/200 适应度值:%s  目标采集次数:%s/10 \n',num2str(i),yy(i),num2str(kkk));
end
Data=[Data;gbest,ackley(gbest,1); ]
end
%Data反归一化
xfinal=mapminmax('reverse',Data(:,1:5)',P);
xfinal=xfinal';
Datafinal=[xfinal,Data(:,6:8)];

%------显示结果
plot(yy)
title(['适应度曲线 ' '终止次数=' num2str(MaxDT)]);
xlabel('进化代数');
ylabel('适应度')
%------算法结束---DreamSun GL & HF-----------------------------------

3.仿真结论:

PSO优化算法

D-57