-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpso.m
70 lines (61 loc) · 2.16 KB
/
pso.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
%% function pso()
% Particle Swarm Optimization algorithm implementation
dimension = length(lower_boundary);
best_global = zeros(1,dimension);
iteration = 0;
%% Hyperparams struct
hyperparams.num_particles = num_particles;
hyperparams.lb = lower_boundary;
hyperparams.ub = upper_boundary;
hyperparams.w = 0.9;
hyperparams.phip = 0.6;
hyperparams.phig = 0.8;
if length(hyperparams.lb) ~= length(hyperparams.ub)
disp("Dimensional error")
end
%% function initialize_particles(num_particles, lb, ub):
clear particles
for i = 1:hyperparams.num_particles
particles(i) = Particle;
% random_uniform here operates on arrays
particles(i).x = random_uniform(hyperparams.lb, hyperparams.ub);
delta = hyperparams.ub - hyperparams.lb;
particles(i).v = random_uniform(-delta, delta);
particles(i).best = zeros(1,length(particles(i).x));
end
%% main loop
while ~check_stopping_condition(iteration, max_iteration)
disp(num2str(iteration/max_iteration*100, "%.2f") + "%");
iteration = iteration + 1;
%[particles, best_iteration] = update_particles(particles,...
% best_global, hyperparams);
%% function update_particles(particles, best_global, hyperparams):
w = hyperparams.w;
phip = hyperparams.phip;
phig = hyperparams.phig;
best_iteration = zeros(1,dimension);
for i = 1:length(particles)
rp = random_uniform(0.0, 1.0);
rg = random_uniform(0.0, 1.0);
particles(i).v = w * particles(i).v + ...
phip * rp * (particles(i).best - particles(i).x) + ...
phig * rg * (best_global - particles(i).x);
particles(i).x = particles(i).x + particles(i).v;
particles(i).x = min(...
max(...
particles(i).x, ...
hyperparams.lb ...
), ...
hyperparams.ub ...
);
if custo(particles(i).x) < custo(particles(i).best)
particles(i).best = particles(i).x;
if custo(particles(i).x) < custo(best_iteration)
best_iteration = particles(i).x;
end
end
end
if custo(best_iteration) < custo(best_global)
best_global = best_iteration;
end
end