![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1525566/image.png)
PSO algorithm non-existent field
7 views (last 30 days)
Show older comments
%% this file is pso1.m ; the driver program
%% Problem Definiton
CostFunction = @(x) Sphere (x); % Cost Function
problem.nVar = 5; %Number of Unknown (Decision) Variables
problem.VarMin = -10; % Lower Bound of Decision Variables
problem.VarMax = 10; % Upper Bound of Decision Variables
%% Parameters of PSO
params.MaxIt = 1000; % Maximum Number of Iterations
params.nPop = 50; % Population Size (Swarm Size)
params.W = 1; %Intertia Coefficient
params.wdamp = 0.99; % Damping Ratio of Inertia Coefficient
params.c1 = 2; % Personal Acceleration Coefficient
params.c2 = 2; %Social Acceleration Coefficient
params.ShowIterInfo = true; %Flag for Showing Iteration Information
%%calling pso
out = PSO(problem,params);
BestSol = out.BestSol;
BestCosts = out.BestCosts;
%%Result
figure;
%plot(BestCosts, 'LineWidth',2);
semilogy(BestCosts, 'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
function out = PSO(problem,params)
%%this is the PSO.m file, with the function PSO
%% Problem Definiton
CostFunction = problem.Costfunction; % Cost Function
nVar = problem.nVar; %Number of Unknown (Decision) Variables
VarSize = [1 nVar]; %Matrix Size of Decision Variables
VarMin = problem.VarMin; % Lower Bound of Decision Variables
VarMax = problem.VarMax; % Upper Bound of Decision Variables
%% Parameters of PSO
MaxIt = params.MaxIt; % Maximum Number of Iterations
nPop = params.nPop; % Population Size (Swarm Size)
w = params.w; %Intertia Coefficient
wdamp = params.wdamp; % Damping Ratio of Inertia Coefficient
c1 = params.c1; % Personal Acceleration Coefficient
c2 = params.c2; %Social Acceleration Coefficient
%The Flag for Showing Iteration Information
ShowIterInfo = params.ShowIterInfo;
MaxVelocity = (VarMax - VarMin)*0.2;
MinVelocity = -MaxVelocity;
%%Initialization
% The Particle Template
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).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 of PSO
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;
%Result
figure;
%plot(BestCosts, 'LineWidth',2);
semilogy(BestCosts, 'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
this is the error i am getting
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1035580/image.png)
0 Comments
Answers (1)
Sanjana
on 31 Oct 2023
Hi Madhav,
I understand that you are trying to access the “Costfunction” defines in pso1.m file, by calling “PSO” function defined in PSO.m file,
Upon analyzing the code, you provided, the “problem” struct variable doesn’t seem to contain “Costfunction” field,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1525566/image.png)
To solve the above issue, add the below line to the code in “pso1.m” file, in order to add “Costfunction” field with value as the defined anonymous “CostFunction” in your code ,
CostFunction = @(x) Sphere (x); % Cost Function
problem.nVar = 5; %Number of Unknown (Decision) Variables
problem.VarMin = -10; % Lower Bound of Decision Variables
problem.VarMax = 10;
problem.CostFunction = CostFunction;
Hope this helps!
1 Comment
Walter Roberson
on 31 Oct 2023
You can improve performance a bit if you use
CostFunction = @Sphere;
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!