PSO algorithm value problem
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.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. 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. pso algorithm MATLAB Answers — New Questions