PSO code cannot get converged solutions ??

9 views (last 30 days)
Ezzat
Ezzat on 9 Aug 2023
Answered: Shishir Reddy about 22 hours ago
I'm using PSO in a optimization function K. I developed a simple code similar to the original problem. but no convergence
Note in the explanation (groups = generations = swarm comprised of competitive particles)
function [SolnX Fn_Val NoIt] = FCPSO1()
ub = [-0.015, -0.01, -0.02, -0.008, -0.01, -0.005, -0.01]; %upper bound of the variables
lb = [0.012, 0.01, 0.005, 0.02, 0.008, 0.004, 0.02]; %lower bound of the variables
RD_Tol = 0.00001; % stopping tolerance
%delta = 0.2;
eps_V = 0.008;
sigma_Tol = 0.0001;
ncol = 7; % no. of variables in the particles (ncol) is 7
nrow = 5; % no. of particles in the swarm or generation (nrowl) is 5
ng = 5000; % max no. of swarms or generations
c1=2; c2=1; H=10; w=0.6;
c3 = 4-(c1+c2);
phi = c1+c2+c3;
exi = 2/abs(2-phi+ sqrt(phi^2-4*phi));
Vmax = (ub-lb)/H;
Vmin = -(ub-lb)/H;
%i_p = 1:np;
%i_g = 1:ng;
% Initialize Random positions and velocities for N particles X, V (First generation or Swarm)
% X{ip, ig} ; ip is counter for particles ; ig is Counter for generations
clc;
figure;
ax3 = axes();
h = animatedline(ax3);
%% Initialize PSO ---------------------------------------------------------
ig = 0; % ig is the generation count
while(ig<=ng)
ig = ig+1;
if ig==1
for irow=1:nrow
for icol=1:ncol
X(ig,icol,irow)= lb(icol)+(ub(icol)-lb(icol))*rand;
V(ig,icol,irow)= eps_V*X(ig,icol,irow);
X(ig,icol,irow)= X(ig,icol,irow) + V(ig,icol,irow);
end
end
end
if ig==1
for irow=1:nrow
for icol=1:ncol
Xbestp(ig,icol,irow) = X(ig,icol,irow);
end
RDD(irow) = Fn(X(ig,:,irow));
end
tMinRD = min(RDD);
%tMRDD = tMinRD;
irow_p =find(tMinRD==RDD);
Xbestgg= X(ig,:,irow_p);
Xbestg{ig}= Xbestgg;
end
%if ig==1 %% The first swarm case ig=1 %%
%% The best particle Xbestp{ip}, precisely for the first swarm, is simply the typical current particle for each ip %%
%% otherwise (ig > 1) it will be the "world team" elected of the best particles ip &&
%% over the entire globe up to the current generation (Xbestp is a complete generation or team) %%
%else %%
if ig>1
XXbestg_1 = cell2mat(Xbestg(ig-1)); %Best particle in the last generation
sumX = 0;
for irow=1:nrow
XX_ig_1= double(X(ig-1,:,irow));
sumX = sumX + XX_ig_1;
V_ig_1 = V(ig-1,:,irow);
X_ig_1 = X(ig-1,:,irow);
end
Xmean = sumX./nrow;
for irow=1:nrow
XXbestp_1 = Xbestp(ig-1,:,irow);
A = w*V_ig_1;
B = c1*rand()*(XXbestg_1-X_ig_1);
C = c2*rand()*(XXbestp_1-X_ig_1);
D = c3*rand()*(Xmean-X_ig_1);
V(ig,:,irow) = exi*(A + B + C + D);
V_ig_ip = V(ig,:,irow);
if sum(V_ig_ip) > sum(Vmax)
%V(ig,:,irow) = 0.8*V(ig,:,irow);
V(ig,:,irow) = Vmax;
elseif sum(V_ig_ip) < sum(Vmax)
V(ig,:,irow) = Vmin;
end
X(ig,:,irow) = X(ig-1,:,irow) + V(ig,:,irow);
X_ig_ip = double( X(ig,:,irow));
if sum(X_ig_ip) > sum(ub)
X(ig,:,irow) = ub - (ub-lb)*rand*0.5;
%X{ig,ip} = ub;
elseif sum(X_ig_ip) < sum(lb)
X(ig,:,irow) = lb + (ub-lb)*rand*0.5;
%X{ig,ip} = lb;
end
end
for irow=1:nrow
%RD{ig,ip} = Form_Rad(X{ig,ip}, pX, pZ,R_ref,'RF');
RDD(irow) = Fn(X(ig,:,irow));
tMinRD = min(RDD);
end
irow_p =find(tMinRD==RDD);
Xbestg{ig}= X(ig,:,irow_p);
warning('Error');
%% reversed suffixes to scan along groups and then along particles
for irow=1:nrow
for iig=1:ig
%Xiig = double(X{iig,ip});
%RD{iig,ip} = Form_Rad(Xiig, pX, pZ,R_ref,'R'); % Evaluate RD and order results as a matrix with current swarm iig and all ip's
RDD(irow) = Fn(X(iig,:,irow));
end
tMinRD = min(RDD);
irow_p_m = find(tMinRD==RDD);
if max(size(irow_p_m))>1
irow_p = irow_p_m(1,1);
else
irow_p = irow_p_m;
end
try
Xbestp(ig,:,irow_p)= X(ig,:,irow_p);
catch
warning('Error');
end
%% Get the best particle for the current particle order ip currently for all previous groups so far untill the current ig %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
w = 0.9999*w;
addpoints(h,ig,tMinRD);
drawnow();
%tMRD{ig} = tMinRD;
%SolnX = X(ig_g,:,ig_p);
%if ig>5
% for i=ig:-1:ig-5
% stdev_5 = std(tMinRD);
% end
%end
end
if (abs(tMinRD) < RD_Tol)
Fn_Val = tMinRD;
NoIt = ig;
SolnX = X(ig,:,irow_p);
return;
end
  2 Comments
Torsten
Torsten on 9 Aug 2023
Edited: Torsten on 9 Aug 2023
You could give a mathematical formulation so that we knew what you are talking about when you mention "the original problem". Or maybe a link to an existing and working MATLAB code ?
Ezzat
Ezzat on 10 Aug 2023
I just refer to this problem is my interest but with different objective function which is connected with hardware response. so my aim is to repair this PSO code for the following objective function:
function [Fnn] = Fn(X)
Fnn = X(1).^2-2*X(2)+3*X(3)-4*X(4)+5*X(5)+20;
end

Sign in to comment.

Answers (1)

Shishir Reddy
Shishir Reddy 6 minutes ago
Hi Ezzat
Here are a few considerations and imporvements that can be considered for yor PSO implementation -
1. Ensure the velocity update uses individual random values for each term.
r1 = rand(1, ncol);
r2 = rand(1, ncol);
r3 = rand(1, ncol);
V(irow, :) = exi * (w * V(irow, :) + c1 * r1 .* (Xbestg - X(irow, :)) + c2 * r2 .* (Xbestp(irow, :) - X(irow, :)) + c3 * r3 .* (mean(X) - X(irow, :)));
2. Update positions and ensure each dimension is within bounds.
X(irow, :) = X(irow, :) + V(irow, :);
X(irow, :) = max(min(X(irow, :), ub), lb);
3. Update the global best position and value only if a new minimum is found.
[newMinRD, irow_p] = min(RDD);
if newMinRD < tMinRD
tMinRD = newMinRD;
Xbestg = X(irow_p, :);
end
4. Update personal bests based on current fitness evaluations.
if RDD(irow) < Fn(Xbestp(irow, :))
Xbestp(irow, :) = X(irow, :);
end
For more information regarding particle swarm optimization, kindly refer the following documentation -
I hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!