# Particle Swarm Optimization PSO

18 views (last 30 days)
M on 1 Dec 2022
Commented: Walter Roberson on 1 Dec 2022
The following is the PSO code, the varible that the PSO returns is "x" and its size is 7*1 , Can anyone help me how can I put contrains on x to make the x(7) is always equal 1???
% Project Code: YPEA102
% Project Title: Implementation of Particle Swarm Optimization in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
function [x,err]=pso(CostFunction)
% CostFunction= Cost Function
% nVar= Number of Decision Variables
nVar = 1
VarSize=[7 nVar]; % Size of Decision Variables Matrix
VarMin= 0; % Lower Bound of Variables
VarMax= 1; % Upper Bound of Variables
%% PSO Parameters
MaxIt=200; % Maximum Number of Iterations
nPop=300; % Population Size (Swarm Size)
% PSO Parameters
w=1; % Inertia Weight
wdamp=0.99; % Inertia Weight Damping Ratio
c1=1.5; % Personal Learning Coefficient
c2=2.0; % Global Learning Coefficient
% If you would like to use Constriction Coefficients for PSO,
% uncomment the following block and comment the above set of parameters.
% % Constriction Coefficients
% phi1=2.05;
% phi2=2.05;
% phi=phi1+phi2;
% chi=2/(phi-2+sqrt(phi^2-4*phi));
% w=chi; % Inertia Weight
% wdamp=1; % Inertia Weight Damping Ratio
% c1=chi*phi1; % Personal Learning Coefficient
% c2=chi*phi2; % Global Learning Coefficient
% Velocity Limits
VelMax=0.1*(VarMax-VarMin);
VelMin=-VelMax;
%% Initialization
empty_particle.Position=[];
empty_particle.Cost=[];
empty_particle.Velocity=[];
empty_particle.Best.Position=[];
empty_particle.Best.Cost=[];
particle=repmat(empty_particle,nPop,1);
GlobalBest.Cost=inf;
for i=1:nPop
% Initialize Position
particle(i).Position=unifrnd(VarMin,VarMax,VarSize);
% Initialize Velocity
particle(i).Velocity=zeros(VarSize);
% Evaluation
particle(i).Cost=CostFunction(particle(i).Position);
% Update Personal Best
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest=particle(i).Best;
end
end
BestCost=zeros(MaxIt,1);
GB_Previous=zeros(MaxIt,1);
%% PSO Main Loop
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+c1*rand(VarSize).*(particle(i).Best.Position-particle(i).Position) ...
+c2*rand(VarSize).*(GlobalBest.Position-particle(i).Position);
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity,VelMin);
particle(i).Velocity = min(particle(i).Velocity,VelMax);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Velocity Mirror Effect
IsOutside=(particle(i).Position<VarMin | particle(i).Position>VarMax);
particle(i).Velocity(IsOutside)=-particle(i).Velocity(IsOutside);
% Apply Position Limits
particle(i).Position = max(particle(i).Position,VarMin);
particle(i).Position = min(particle(i).Position,VarMax);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update Personal Best
if particle(i).Cost<particle(i).Best.Cost
particle(i).Best.Position=particle(i).Position;
particle(i).Best.Cost=particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GB_Previous=GlobalBest.Cost ;
GlobalBest=particle(i).Best;
end
end
end
BestCost(it)=GlobalBest.Cost;
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
w=w*wdamp;
if abs(GlobalBest.Cost-GB_Previous)<= 1e-15
break
end
end
BestSol = GlobalBest;
x=BestSol.Position;
err=BestSol.Cost;
%% Results
figure;
plot(BestCost,'LineWidth',2);
semilogy(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;

Walter Roberson on 1 Dec 2022
nVar = 1
VarSize=[7 nVar]; % Size of Decision Variables Matrix
Well that is wrong. You do not have 1 variable, you have 7 variables. You should be using
nVar = 7;
VarSize = [nVar, 1];
Then
VarMin= 0; % Lower Bound of Variables
VarMax= 1; % Upper Bound of Variables
When you want x(7) to be forced to be 7 you should use
VarMin = [zeros(nVar-1,1); 1];
VarMax = ones(nVar,1);
and change
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity,VelMin);
particle(i).Velocity = min(particle(i).Velocity,VelMax);
to
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity,VelMin(i));
particle(i).Velocity = min(particle(i).Velocity,VelMax(i));
M on 1 Dec 2022
@Walter Roberson I got the following error:
Index exceeds the number of array elements (7).
Error in pso (line 99)
particle(i).Velocity = max(particle(i).Velocity,VelMin(i));
Walter Roberson on 1 Dec 2022
You are right, you should not be indexing with (i) there, the original code for that section was fine.