Error in Particle Swarm Optimization

4 views (last 30 days)
Muhammad Hussain
Muhammad Hussain on 2 Jun 2019
Answered: Ayush on 16 Aug 2024
I am optimizing the function given below using Particle Swarm Optimization in Matlab. It is giving me error in body of function every time and it is not running. kindly help me and reply me as soon as possible. i will be very thankful to you.
I have write the code as follow;
function z = PSO (x,y)
z= (4-2.1*x^2+(x^4)/3)*x^2 + x*y + (-4+4*y^2)*y^2;
end
clc;
clear;
close all;
%Problem
CostFunction = @(x,y) PSO(x,y);
nvar =2
VarSize = [1 nVar]; % Matrix Size of Decision Variables
VarMin = -10; % Lower Bound of Decision Variables
VarMax = 10;
%Parameters
MaxIt = 50;
nPop = 25; % Population Size (Swarm Size)
w = 1; % Intertia Coefficient
wdamp = params.wdamp; % Damping Ratio of Inertia Coefficient
c1 = 2; % Personal Acceleration Coefficient
c2 = 2;
%Initialization
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
% Create Population Array
particle = repmat(empty_particle, nPop, 1);
%initialize Globle Best
GlobalBest.Cost = inf;
% Initialize Population Members
for i=1:nPop
% Generate Random Solution
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
particle(i).Velocity = zeros(VarSize);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update the 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
% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);
%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, MinVelocity);
particle(i).Velocity = min(particle(i).Velocity, MaxVelocity);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Apply Lower and Upper Bound 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
GlobalBest = particle(i).Best;
end
end
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
if ShowIterInfo
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
end
% Damping Inertia Coefficient
w = w * wdamp;
end
out.pop = particle;
out.BestSol = GlobalBest;
out.BestCosts = BestCosts;
end
the Error i am getting is as follow;
>> PSO Implementation
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform
elementwise matrix powers, use '.^'.
Error in PSO (line 3)
z= (4-2.1*x^2+(x^4)/3)*x^2 + x*y + (-4+4*y^2)*y^2;

Answers (1)

Ayush
Ayush on 16 Aug 2024
It looks like the error you are encountering is related to matrix operations in your PSO function. Specifically, the ^ operator in MATLAB is used for matrix exponentiation, and it requires the matrix to be square if used for non-elementwise operations. In your case, you need element-wise operations because x and y are scalars, but MATLAB interprets them as vectors or matrices when you use the ^ operator.
To resolve this issue, you should use element-wise operators for power and multiplication. Here’s some corrections I’ve made in your code:
  1. PSO function : I’ve used “.*” operation instead of “*” operation for element-wiser operations.
function z = PSO(x, y)
z = (4 - 2.1 * x.^2 + (x.^4) / 3) .* x.^2 + x .* y + (-4 + 4 * y.^2) .* y.^2;
end
I have also made corrections to other sections of your code, which I have detailed below:
  1. nvar and VarSize: You have defined nvar as 2, but you are using nVar (with an uppercase “V”) in VarSize. It is important to maintain consistency in variable names.
Correct VarSize initialization to use the correct variable name:
nVar = 2; % Number of Decision Variables
VarSize = [1 nVar]; % Matrix Size of Decision Variables
2. Undefined variables: I have utilized some placeholder values for these variables to ensure code executes correctly.
MinVelocity = -0.1; % Example value for minimum velocity
MaxVelocity = 0.1; % Example value for maximum velocity
ShowIterInfo = true; % Set to true if you want to display iteration information
params.wdamp = 0.99; % Example damping ratio
Here’s the corrected code:
function z = PSO(x, y)
z = (4 - 2.1 * x.^2 + (x.^4) / 3) .* x.^2 + x .* y + (-4 + 4 * y.^2) .* y.^2;
end
clc;
clear;
close all;
% Problem
CostFunction = @(x, y) PSO(x, y);
nVar = 2; % Number of Decision Variables
VarSize = [1 nVar]; % Matrix Size of Decision Variables
VarMin = -10; % Lower Bound of Decision Variables
VarMax = 10;
% Parameters
MaxIt = 50;
nPop = 25; % Population Size (Swarm Size)
w = 1; % Inertia Coefficient
wdamp = 0.99; % Damping Ratio of Inertia Coefficient
c1 = 2; % Personal Acceleration Coefficient
c2 = 2; % Global Acceleration Coefficient
% Initialization
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
% Create Population Array
particle = repmat(empty_particle, nPop, 1);
% Initialize Global Best
GlobalBest.Cost = inf;
% Initialize Population Members
for i = 1:nPop
% Generate Random Solution
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
particle(i).Velocity = zeros(VarSize);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position(1), particle(i).Position(2));
% Update the 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
% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);
% 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, -0.1);
particle(i).Velocity = min(particle(i).Velocity, 0.1);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Apply Lower and Upper Bound 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(1), particle(i).Position(2));
% 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
GlobalBest = particle(i).Best;
end
end
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
if ShowIterInfo
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
end
% Damping Inertia Coefficient
w = w * wdamp;
end
out.pop = particle;
out.BestSol = GlobalBest;
out.BestCosts = BestCosts;
For futher information, you can refer to the following documentation:
If you encounter any further issues, please feel free to leave a comment below.
Hope it helps!

Community Treasure Hunt

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

Start Hunting!