Skip to content

Commit c864955

Browse files
authored
First_Implementation_on_Swarm_Algorithms
0 parents  commit c864955

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

F1.m

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%Rastrigin
2+
function [Out] = F1(X)
3+
T1 = X.^2;
4+
T2 = -10*cos(2*pi*X)+10;
5+
Out = sum(T1+T2,2);
6+
%D=[-5.12,5.12]

F2.m

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%Ackley
2+
function [Out] = F2(X)
3+
T1 = -20*exp(-0.2*(sqrt(sum(X.^2,2)*1./size(X,2))));
4+
T2 = exp(sum(cos(X*2*pi),2)*1./size(X,2));
5+
Out = T1-T2+20+exp(1);
6+
%D=[-32,32]

F3.m

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%Schwefel 2.26
2+
function [Out] = F3(X)
3+
T1 = 418.9829.*size(X,2);
4+
T2 = sum(X.*sin(sqrt(abs(X))),2);
5+
Out = T1 - T2;
6+
%D=[-500,500]

SI_H1_PSO.m

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
%Number of Initial Population & Dimensions
6+
pop_size = 100;
7+
dim = 2;
8+
9+
%Iteration Condition
10+
max_iter = 200;
11+
12+
%Mutation Rate
13+
%m_rate = 0.05;
14+
15+
%Domain of Benchmarks
16+
from = -5.12;
17+
to = -1*from;
18+
19+
%Results for n Times Execution
20+
num_of_result = 5;
21+
%Columns of total Result : dim,(gbest_fitness),(time)
22+
total_result = zeros(num_of_result,dim+2);
23+
24+
for n=1:num_of_result
25+
tic;
26+
27+
%Nfe Condition
28+
max_nfe = 20000;
29+
30+
%Initialize Best Fitness and Position by a Large Value
31+
g_best = zeros(1, dim);
32+
g_best(1,:) = to;
33+
g_best_fitness = F1(g_best(1,:)); %nfe++
34+
nfe = 1;
35+
36+
%Initialize Population
37+
X = unifrnd(from,to,[pop_size dim]);
38+
%Initialize Personal Bests (Equal to First Positions)
39+
p_best = X;
40+
%Initialize Velocity (Equal to First Position)
41+
V = X;
42+
%V = zeros(pop_size,dim);
43+
44+
F_result = zeros(1, pop_size);
45+
46+
%c_max = 2.5;
47+
%c_min = 0.5;
48+
49+
%Calculate Fitness of X and Save the Best Fitness and its Position
50+
for i = 1:pop_size
51+
F_result(1,i) = F1(X(i,:));
52+
nfe = nfe + 1;
53+
if (F_result(1,i) <= g_best_fitness)
54+
g_best_fitness = F_result(1,i);
55+
g_best(1,:) = X(i,:);
56+
end
57+
end
58+
59+
%Main Loop
60+
for m=1:max_iter
61+
F1_result = zeros(1, pop_size);
62+
r1 = rand;
63+
r2 = rand;
64+
65+
%Cognitive Coefficient
66+
%c1 = (c_min - c_max) * (j/max_iter) + c_max;
67+
%Global Coefficient
68+
%c2 = (c_max - c_min) * (j/max_iter) + c_max;
69+
%Inertia Coefficient for Standard PSO
70+
%w = (c1*r1) + (c2*r2);
71+
72+
r_w = rand;
73+
w0 = 0 + (0.4-0)*rand;
74+
alpha0 = 0.5 + (1-0.5)*rand;
75+
76+
%Interia Coefficient for AWPSO
77+
w = w0 + r_w * (1 - w0);
78+
%Acceleration Factor
79+
alpha = alpha0 + m/max_iter;
80+
81+
%Convex (for Breeding PSO)
82+
%lambda1 = rand;
83+
%lambda2 = 1 - lambda1;
84+
%Average
85+
%lambda1 = 0.5;
86+
%lambda2 = lambda1;
87+
%Affine
88+
%lambda1 = 1.5;
89+
%lambda2 = -0.5;
90+
%Linear
91+
%lambda1 = rand;
92+
%lambda2 = rand;
93+
94+
for j=1:pop_size
95+
%Equation of Velocity (Update Velocity of each Particle)
96+
%V(j,:) = (w * V(j,:)) + (c1*r1*(p_best(j,:) - X(j,:)))...
97+
% + (c2*r2*(g_best(1,:) - X(j,:) )); %Standard PSO
98+
V(j,:) = (w * V(j,:)) + alpha*((r1*(p_best(j,:) - X(j,:)))...
99+
+ (r2*(g_best(1,:) - X(j,:) ))); %AWPSO
100+
101+
%Update Velocity of each Particle for Breeding PSO
102+
%for z=1:2:pop_size
103+
% V(z,:) = ((V(z,:) + V(z+1,:))/(norm(V(z,:) + V(z+1,:))))...
104+
% * norm(V(z,:));
105+
%end
106+
107+
%Control the Domain of the new Velocity
108+
for p=i:dim
109+
r_v = rand;
110+
if (V(j,p) < from)
111+
V(j,p) = from + r_v;
112+
elseif (V(j,p) > to)
113+
V(j,p) = to - r_v;
114+
end
115+
end
116+
117+
%Equation of new Position (Update Position of each Particle)
118+
X(j,:) = X(j,:) + V(j,:);
119+
120+
%Update Position of each Particle for Breeding PSO
121+
%for z=1:2:pop_size
122+
% x_temp_a = X(z,:);
123+
% x_temp_b = X(z+1,:);
124+
% X(z,:) = lambda1*(x_temp_a) + lambda2*(x_temp_b);
125+
% X(z+1,:) = lambda1*(x_temp_b) + lambda2*(x_temp_a);
126+
%end
127+
128+
%Mutation
129+
%y = unifrnd(from,to,[floor(m_rate*pop_size) dim]);
130+
%for i = 1:size(y,1)
131+
%rand_Mutation = randi(pop_size);
132+
%X(rand_Mutation,1:dim) = X(rand_Mutation,1:dim) + y(i,:);
133+
%end
134+
135+
%Control the Domain of the new Positions
136+
for t=1:dim
137+
r_x = rand;
138+
if (X(j,t) < from)
139+
X(j,t) = from + r_x;
140+
elseif (X(j,t) > to)
141+
X(j,t) = to - r_x;
142+
end
143+
end
144+
end
145+
146+
%Update the Personal Bests and Global Best
147+
for k = 1:pop_size
148+
149+
if (nfe >= max_nfe)
150+
break;
151+
end
152+
153+
F1_result(1,k) = F1(X(k,:));
154+
nfe = nfe + 1;
155+
if (F1_result(1,k) <= F1(p_best(k,:)))
156+
nfe = nfe +1;
157+
p_best(k,:) = X(k,:);
158+
end
159+
160+
if (F1_result(1,k) <= g_best_fitness)
161+
g_best_fitness = F1_result(1,k);
162+
nfe = nfe +1;
163+
g_best(1,:) = X(k,:);
164+
end
165+
end
166+
end
167+
168+
total_result(n,1) = toc;
169+
total_result(n,2) = g_best_fitness;
170+
total_result(n,3:end) = g_best;
171+
end
172+
173+
min_fitness = min(total_result(:,2));
174+
max_fitness = max(total_result(:,2));
175+
mean_fitness = mean(total_result(:,2));
176+
std_fitness = std(total_result(:,2));
177+
mean_time = mean(total_result(:,1));
178+
179+
disp(strcat('Popsize:', num2str(pop_size), ', Dimension:', num2str(dim)));
180+
disp(strcat('mean fitness: ', num2str(mean_fitness)));
181+
disp(strcat('max fitness: ', num2str(max_fitness)));
182+
disp(strcat('min fitness: ', num2str(min_fitness)));
183+
disp(strcat('std fitness: ', num2str(std_fitness)));
184+
disp(strcat('mean time: ', num2str(mean_time)));

0 commit comments

Comments
 (0)