Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Particle Swarm Optimization Matlab code Using 50, 5000 Swarms
1. Particle Swarm Optimization(PSO) Matlab Code (50,5000 Swarms)
MuhammadRaza: 12063122-043@uog.edu.pk
AlternativeGmail:mraza.engg@gmail.com
Website:http://dg-algorithm.blogspot.com
BSc Student,Electrical EngineeringDept.,Universityof Gujrat,Pakistan
Introduction:
Proposed by James Kennedy & Russell Eberhart in 1995
Inspired by social behavior of birds and fishes
Combines self-experience with social experience
Population-based optimization
Concept:
Uses a number of particles that constitute a swarm moving around in the search space looking for
the best solution.
Each particle in search space adjusts its “flying” according to its own flying experience as well as
the flying experience of other particles
2. Each particle keeps track of its coordinates in the solution space which are associated with the best
solution (fitness) that has achieved so far by that particle. This value is called personal best, pbest.
Another best value that is tracked by the PSO is the best value obtained so far by any particle in
the neighborhood of that particle. This value is called gbest.
The basic concept of PSO lies in accelerating each particle toward its pbest and the gbest locations,
with a random weighted acceleration at each time step.
Objective Function:
An objective function which we want to minimize or maximize.
For example, in a manufacturing process, we might want to maximize the profit or minimize the
cost.
Terminology:
4. %% Particle SwarmOptimizationSimulation MatlabCode Using 50
Swarms/Particles
%%Particle SwarmOptimizationSimulation
% Findminimum of the objective function
%%Initialization
clear
clc
iterations=30;
inertia= 1.0;
correction_factor= 2.0;
swarms= 50;
% ---- initial swarmposition -----
5. swarm=zeros(50,7)
step= 1;
for i = 1 : 50
swarm(step,1:7) = i;
step= step+ 1;
end
swarm(:,7) = 1000 % Greaterthan maximumpossible value
swarm(:,5) = 0 % initial velocity
swarm(:,6) = 0 % initial velocity
%%Iterations
for iter= 1 : iterations
%-- positionof Swarms ---
for i = 1 : swarms
swarm(i,1) = swarm(i,1) + swarm(i,5)/1.2 %update uposition
swarm(i,2) = swarm(i,2) + swarm(i,6)/1.2 %update vposition
u = swarm(i,1)
v= swarm(i,2)
value = (u - 20)^2 + (v - 10)^2 %Objective function
if value < swarm(i,7) % AlwaysTrue
swarm(i,3) = swarm(i,1) %update bestpositionof u,
swarm(i,4) = swarm(i,2) %update bestpostionsof v,
swarm(i,7) = value % bestupdatedminimumvalue
end
end
6. [temp,gbest] =min(swarm(:,7)) % gbestposition
%--- updatingvelocityvectors
for i = 1 : swarms
swarm(i,5) = rand*inertia*swarm(i,5) + correction_factor*rand*(swarm(i,3)...
- swarm(i,1)) + correction_factor*rand*(swarm(gbest,3) - swarm(i,1)) % u velocityparameters
swarm(i,6) = rand*inertia*swarm(i,6) + correction_factor*rand*(swarm(i,4)...
- swarm(i,2)) + correction_factor*rand*(swarm(gbest,4) - swarm(i,2)) % v velocityparameters
end
%% Plottingthe swarm
clf
plot(swarm(:,1),swarm(:,2),'x') % drawingswarmmovements
axis([-250 -2 50])
pause(.1)
end
%% Particle SwarmOptimizationSimulation MatlabCode Using 5000 Particles
clear
clc
iterations=1000;
7. inertia= 1.0;
correction_factor= 2.0;
swarms= 5000;
% ---- initial swarmposition -----
swarm=zeros(5000,7);
step= 1;
for i = 1 : 5000
swarm(step,1:7) = i;
step= step+ 1;
end
swarm(:,7) = 1000; % Greaterthan maximumpossible value
swarm(:,5) = 0; % initial velocity
swarm(:,6) = 0; % initial velocity
%%Iterations
for iter= 1 : iterations
%-- positionof Swarms ---
for i = 1 : swarms
swarm(i,1) = swarm(i,1) + swarm(i,5)/1.2 ; %update uposition
swarm(i,2) = swarm(i,2) + swarm(i,6)/1.2; %update vposition
u = swarm(i,1);
v= swarm(i,2);
value = (u - 20)^2 + (v - 10)^2; %Objective function
if value < swarm(i,7) % AlwaysTrue
8. swarm(i,3) = swarm(i,1); %update bestpositionof u,
swarm(i,4) = swarm(i,2); %update bestpostionsof v,
swarm(i,7) = value; %bestupdatedminimumvalue
end
end
[temp,gbest] =min(swarm(:,7)); % gbestposition
%--- updatingvelocityvectors
for i = 1 : swarms
swarm(i,5) = rand*inertia*swarm(i,5) + correction_factor*rand*(swarm(i,3)...
- swarm(i,1)) + correction_factor*rand*(swarm(gbest,3) - swarm(i,1)); % u velocityparameters
swarm(i,6) = rand*inertia*swarm(i,6) + correction_factor*rand*(swarm(i,4)...
- swarm(i,2)) + correction_factor*rand*(swarm(gbest,4) - swarm(i,2)); % v velocityparameters
end
%% Plottingthe swarm
clf
plot(swarm(:,1),swarm(:,2),'x') % drawingswarmmovements
axis([-10005000 -1000 5000])
pause(.1)
end
%----------------------------------END--------------------------------------------%
Like us formore Matlab projects,simulation.
Website:http://dg-algorithm.blogspot.com
Like us onFacebook:https://www.facebook.com/matlab.online/
Like us onTwitter:https://twitter.com/matlab_online
Like us on Google Plus:https://plus.google.com/u/0/109734739693784042356