Skip to content

Commit c41f733

Browse files
Add files via upload
1 parent 6f3e6ef commit c41f733

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function z = Parabola(x)
2+
z = sum(x.^2);
3+
end
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
%% Problem Definition
6+
7+
8+
CostFunction=@(x) Parabola(x); % Cost Function
9+
10+
nVar = 5; % 5 dimensional-Number of unknown (Decision Variables)
11+
12+
VarSize = [1 nVar]; % Matrix size of Decision Variables
13+
14+
VarMin = -10; % Lower bound of Decision Variables
15+
VarMax = 10; % Upper bound of Decision Variables
16+
17+
18+
%% Parameters of PSO
19+
20+
MaxIt = 1000; % Maximum Numbers of Iterations
21+
22+
nPop = 50; % Population size (Swarm Size)
23+
24+
w = 1; % Inertia Coefficient
25+
wdamp=0.99; % Damping Ratio of intertia weight
26+
c1 = 2; % Personal Acceleration Coefficient
27+
c2 = 2; % Social Acceleration Coefficient
28+
29+
30+
31+
%% Initialization
32+
33+
% The Particle Template
34+
empty_particle.Position = []; % position of particle
35+
empty_particle.Velocity = []; % velocity of particle
36+
empty_particle.Cost = []; % own measurement of particle(cost value)
37+
empty_particle.Best.Position = []; % personal best with its position
38+
empty_particle.Best.Cost = []; % personal best with its cost value
39+
40+
41+
particle = repmat(empty_particle, nPop,1); % Create random Population Array
42+
43+
%Initialization Global best
44+
GlobalBest.Cost=inf; % for minization fun. its infinity before 1st iteration
45+
46+
% Initialize Population Members
47+
48+
for i=1:nPop
49+
50+
% Generate Random Solutions
51+
particle(i).Position=unifrnd(VarMin,VarMax,VarSize);
52+
53+
% Initialize Velocity
54+
particle(i).Velocity=zeros(VarSize); %matrix with all o of varsize
55+
56+
% Evaluation at above particle i position
57+
particle(i).Cost=CostFunction(particle(i).Position);
58+
59+
% Update Personal Best value
60+
particle(i).Best.Position=particle(i).Position;
61+
particle(i).Best.Cost=particle(i).Cost;
62+
63+
% Update Global Best
64+
if particle(i).Best.Cost<GlobalBest.Cost
65+
66+
GlobalBest=particle(i).Best;
67+
68+
end
69+
end
70+
% Array to hold best cost value on each iteration
71+
BestCost=zeros(MaxIt,1);
72+
73+
%% Main Loop of PSO
74+
for it=1:MaxIt % first iter to last iteration
75+
76+
for i=1:nPop % for every particle thisloop is required
77+
78+
% Update Velocity %element wise multiplication is there
79+
particle(i).Velocity = w*particle(i).Velocity ...
80+
+c1*rand(VarSize).*(particle(i).Best.Position-particle(i).Position) ...
81+
+c2*rand(VarSize).*(GlobalBest.Position-particle(i).Position);
82+
83+
%Update Position
84+
particle(i).Position = particle(i).Position + particle(i).Velocity;
85+
86+
% Evaluation
87+
particle(i).Cost = CostFunction(particle(i).Position);
88+
89+
% Update Personal Best
90+
if particle(i).Cost<particle(i).Best.Cost
91+
92+
particle(i).Best.Position=particle(i).Position; %best updated with current
93+
particle(i).Best.Cost=particle(i).Cost;
94+
95+
% Update Global Best
96+
if particle(i).Best.Cost<GlobalBest.Cost
97+
98+
GlobalBest=particle(i).Best;
99+
100+
end
101+
102+
103+
end
104+
105+
end
106+
% store the Best cost value
107+
BestCost(it)=GlobalBest.Cost;
108+
109+
% Display iteration info.
110+
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
111+
112+
%Damping Intertia coefficient
113+
w=w*wdamp;
114+
end
115+
116+
%% Results
117+
118+
% Graph converges to origin
119+
figure;
120+
%plot(BestCost,'LineWidth',2); % for normal graph
121+
semilogy(BestCost,'LineWidth',2); %for exponential graph
122+
xlabel('Iteration');
123+
ylabel('Best Cost');
124+
grid on;
125+
126+
127+
128+
129+
130+

0 commit comments

Comments
 (0)