版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_39286218/article/details/80768863
文件目录:
main.m
PSO_1.m
PSO_2.m
OptimizationProble,m
main.m
clear
clc
%Problem preparation
problem.nVar = 5;
problem.ub = [5,5,5,5,5];
problem.lb = [-5,-5,-5,-5,-5];
problem.fobj = @OptimizationProblem;
%PSO parameters
noP = 10;
maxIter = 100;
visFlag = 0;
RunNo = 20;
BestSolutions_PSO1 = zeros(1, RunNo);
BestSolutions_PSO2 = zeros(1, RunNo);
for k = [1 : RunNo]
disp(['Run # ', num2str(k)])
%PSO_1
[GBEST,cgCurve] = PSO_1(noP,maxIter, problem, visFlag);
BestSolutions1(k) = GBEST.O;
%PSO_2
[GBEST,cgCurve] = PSO_2(noP,maxIter, problem, visFlag);
BestSolutions2(k) = GBEST.O;
end
PSO1_Ave = mean(BestSolutions1);
PSO1_Std = std(BestSolutions1);
PSO2_Ave = mean(BestSolutions2);
PSO2_Std = std(BestSolutions2);
disp('Average(Std) obtained by PSO1')
disp([num2str(PSO1_Ave), '(',num2str(PSO1_Std), ') '])
disp('Average(Std) obtained by PSO2')
disp([num2str(PSO2_Ave), '(',num2str(PSO2_Std), ') '])
disp('Wilcoxon ranksum test')
p = ranksum(BestSolutions1,BestSolutions2);
%significant level is 5%
if p < 0.05
disp(['P-value = ', num2str(p)])
disp('The results of the better algorithm IS statistically significant')
else
disp(['P-value = ', num2str(p)])
disp('The results of the better algorithm IS NOT statistically significant')
end
PSO_1.m
function[GBEST,cgCurve] = PSO_1(noP,maxIter, problem,dataVis)
%Define the details of the fobj
nVar = problem.nVar;
ub = problem.ub;
lb = problem.lb;
fobj = problem.fobj;
%Define the PSO's parametres
wMax = 0.9;
wMin = 0.2;
c1 = 2;
c2 = 2;
vMax = (ub - lb) .* 0.2;
vMin = -vMax;
%Initialize the particles
for k = 1 : noP
Swarm.Particles(k).X = (ub-lb) .* rand(1,nVar) + lb;
Swarm.Particles(k).V = zeros(1, nVar);
Swarm.Particles(k).PBEST.X = zeros(1, nVar);
Swarm.Particles(k).PBEST.O = inf;
Swarm.GBEST.X = zeros(1, nVar);
Swarm.GBEST.O = inf;
end
%Main loop
for t = 1 : maxIter
%Calculate the objective value
for k = 1 : noP
currentX = Swarm.Particles(k).X;
Swarm.Particles(k).O = fobj(currentX);
if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O %Update the PBEST
Swarm.Particles(k).PBEST.X = currentX;
Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
end
if Swarm.Particles(k).O < Swarm.GBEST.O %Update the GBEST
Swarm.GBEST.X = currentX;
Swarm.GBEST.O = Swarm.Particles(k).O;
end
end
%Update the X and V vectors
w = wMax - t .* ((wMax - wMin) / maxIter);
for k = 1 : noP
Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,nVar) .* (Swarm.Particles(k).PBEST.X-Swarm.Particles(k).X)...
+ c2 .* rand(1,nVar) .* (Swarm.GBEST.X-Swarm.Particles(k).X);
index1 = find(Swarm.Particles(k).V > vMax); %Check velocities
index2 = find(Swarm.Particles(k).V < vMin);
Swarm.Particles(k).V(index1) = vMax(index1);
Swarm.Particles(k).V(index2) = vMin(index2);
Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
index1 = find(Swarm.Particles(k).X > ub); %Check positions
index1 = find(Swarm.Particles(k).X < lb);
Swarm.Particles(k).X(index1) = ub(index1);
Swarm.Particles(k).X(index2) = lb(index2);
end
if dataVis==1
outmsg = ['Iteration#' , num2str(t) , 'Swarm.GBEST.O=' , num2str(Swarm.GBEST.O)];
disp(outmsg);
end
cgCurve(t) = Swarm.GBEST.O;
end
GBEST = Swarm.GBEST;
if dataVis==1
semilogy(cgCurve);
xlabel('Iteration#')
ylabel('Weight')
end
PSO_2
function[GBEST,cgCurve] = PSO_2(noP,maxIter, problem,dataVis)
%Define the details of the fobj
nVar = problem.nVar;
ub = problem.ub;
lb = problem.lb;
fobj = problem.fobj;
%Define the PSO's parametres
%wMax = 0.9;
%wMin = 0.2;
c1 = 2;
c2 = 2;
vMax = (ub - lb) .* 0.2;
vMin = -vMax;
%Initialize the particles
for k = 1 : noP
Swarm.Particles(k).X = (ub-lb) .* rand(1,nVar) + lb;
Swarm.Particles(k).V = zeros(1, nVar);
Swarm.Particles(k).PBEST.X = zeros(1, nVar);
Swarm.Particles(k).PBEST.O = inf;
Swarm.GBEST.X = zeros(1, nVar);
Swarm.GBEST.O = inf;
end
%Main loop
for t = 1 : maxIter
%Calculate the objective value
for k = 1 : noP
currentX = Swarm.Particles(k).X;
Swarm.Particles(k).O = fobj(currentX);
if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O %Update the PBEST
Swarm.Particles(k).PBEST.X = currentX;
Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
end
if Swarm.Particles(k).O < Swarm.GBEST.O %Update the GBEST
Swarm.GBEST.X = currentX;
Swarm.GBEST.O = Swarm.Particles(k).O;
end
end
%Update the X and V vectors
%w = wMax - t .* ((wMax - wMin) / maxIter);
w = 0.8;
for k = 1 : noP
Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,nVar) .* (Swarm.Particles(k).PBEST.X-Swarm.Particles(k).X)...
+ c2 .* rand(1,nVar) .* (Swarm.GBEST.X-Swarm.Particles(k).X);
index1 = find(Swarm.Particles(k).V > vMax); %Check velocities
index2 = find(Swarm.Particles(k).V < vMin);
Swarm.Particles(k).V(index1) = vMax(index1);
Swarm.Particles(k).V(index2) = vMin(index2);
Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
index1 = find(Swarm.Particles(k).X > ub); %Check positions
index1 = find(Swarm.Particles(k).X < lb);
Swarm.Particles(k).X(index1) = ub(index1);
Swarm.Particles(k).X(index2) = lb(index2);
end
if dataVis==1
outmsg = ['Iteration#' , num2str(t) , 'Swarm.GBEST.O=' , num2str(Swarm.GBEST.O)];
disp(outmsg);
end
cgCurve(t) = Swarm.GBEST.O;
end
GBEST = Swarm.GBEST;
if dataVis==1
semilogy(cgCurve);
xlabel('Iteration#')
ylabel('Weight')
end
OptimizationProblem.m
function [ o ] = OptimizationProblem ( x )
o = sum(x.^2);
end
运行结果:
Run # 1
Run # 2
Run # 3
Run # 4
Run # 5
Run # 6
Run # 7
Run # 8
Run # 9
Run # 10
Run # 11
Run # 12
Run # 13
Run # 14
Run # 15
Run # 16
Run # 17
Run # 18
Run # 19
Run # 20
Average(Std) obtained by PSO1
1.3199e-06(2.3819e-06)
Average(Std) obtained by PSO2
0.03286(0.028921)
Wilcoxon ranksum test
P-value = 6.7956e-08
The results of the better algorithm IS statistically significant
>>
心得: