FAQ1-How to write a PSO as a function

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_39286218/article/details/80756559

Why to write a PSO as a function ?

文件目录:

main.m

PSO.m

OurTableDesignProblem.m

main.m
clear
clc

%Problem preparation
problem.nVar = 2;
problem.ub = [7,7];
problem.lb = [2,2];
problem.fobj = @OurTableDesignProblem;

%PSO parameters
noP = 5;
maxIter = 20;
visFlag = 1;
 
disp('PSO if optimizing the objective')
[GBEST,cgCurve] = PSO(noP,maxIter, problem, visFlag);
disp('End of the optimization process')
PSO.m
function[GBEST,cgCurve] = PSO(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
OurtableDesignProblem.m
function [ weight ] = OurTableDesignProblem ( x )  
  
length = x(1);  
width = x(2);  
  
weight = 1 * (length + width);  
  
end 

运行结果:

               

心得:




猜你喜欢

转载自blog.csdn.net/sinat_39286218/article/details/80756559
pso