PSO algorithm value problem
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi
I have a pso code, but i have a little problem. Here is a code
clc;
clear;
close all;
%% Problem Definition
CostFunction = @(x) Sphere(x); % Cost Function
nVar = 5; % Number of Unknown (Deceision) Variables
VarSize = [1 nVar]; % Matrix Size of Decision Variables
VarMin = -10; % Lower bound of Decesion Variable
VarMax = 10; % Upper bound of Decesion Variable
%% Parameters of PSO
MaxIt = 100; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
wdamp = 0.99; % Damping Ratio of Inertia Coefficient
w = 1; % Inertia Coefficient
c1 = 2; % Personal Acceleration Coefficient
c2 = 2; % Social Acceleration Coefficient
%% 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).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 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);
% Update Position
particle(i).Postion = particle(i).Position + particle(i).Velocity;
% 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
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
% Damping Interia Coefficient
w = w * wdamp;
end
figure;
plot(BestCosts, 'LineWidth', 2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
Value of BestCosts is alwyas the same, but shouldnt. I dont know how to fix it. Please help me.
0 commentaires
Réponses (1)
Ayush
le 16 Août 2024
Hi Jakub,
I find that the issue you're facing with the PSO code is likely due to a typo in the update of the particle's position. In the line where you update the position of each particle, you have a typo: "Postion" instead of "Position". This typo means that the position is never actually updated, which would indeed result in the "BestCosts" array having the same value across iterations.
Here's the corrected line of code:
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
Hope it helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Particle Swarm dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!