-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSO.m
135 lines (100 loc) · 3.82 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function [X_opt, global_best_fitness, Elapsed_Time] = PSO(objective_function, LB, UB, nvars, popSize, max_iter, c1, c2, w)
n = popSize; % Population Size (Swarm Size)
bird_step =max_iter; % Maximum Number of Iterations
dim = nvars; % Number of Decision Variables
dt = 0.3;
VarMin= LB; % Lower Bound of Variables
VarMax= UB; % Upper Bound of Variables
fitness=zeros(1,bird_step);
% Velocity Limits
VelMax=1;
VelMin=-VelMax;
current_fitness =zeros(n,1);
globl_best_position = zeros(dim,n);
% Initialisation position
current_position = ((VarMax - VarMin)').*rand(dim,n) + VarMin';
velocity = 0.3*randn(dim, n) ; %Normally distributed with mean=0 standard deviation=0.3
local_best_position = current_position ;
% evaluate the initial population
for i = 1:n
current_fitness(i) = objective_function(current_position(:,i));
end
local_best_fitness = current_fitness ;
[global_best_fitness,g] = min(local_best_fitness) ;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g) ;
end
% VELOCITY UPDATE %
velocity = w *velocity...
+ c1*((rand(dim, n)).*(local_best_position-current_position))...
+ c2*((rand(dim, n)).*(globl_best_position-current_position));
% Apply Velocity Limits
velocity = max(velocity, VelMin);
velocity = min(velocity, VelMax);
% swarm update
current_position = current_position + velocity*dt ;
% Velocity Mirror Effect if over the boundary the velocity nagative
for i=1:3
IsOutside=(current_position(i)<VarMin(i) | current_position(i)>VarMax(i));
if IsOutside
velocity(i) = -velocity(i);
end
end
% Apply Position Limits
for i=1:3
current_position(i) = max(current_position(i),VarMin(i));
current_position(i) = min(current_position(i),VarMax(i));
end
%% Main Loop
iter = 0 ;
tic;
while ( iter < bird_step )
iter = iter + 1;
for i = 1:n
current_fitness(i) = objective_function(current_position(:,i)) ;
end
for i = 1 : n
if current_fitness(i) < local_best_fitness(i)
local_best_fitness(i) = current_fitness(i);
local_best_position(:,i) = current_position(:,i) ;
end
end
[current_global_best_fitness,g] = min(local_best_fitness);
fitness(iter) = current_global_best_fitness;
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g);
end
end
% VELOCITY UPDATE
velocity = w *velocity...
+ c1*((rand(dim, n)).*(local_best_position-current_position))...
+ c2*((rand(dim, n)).*(globl_best_position-current_position));
% Apply Velocity Limits
velocity = max(velocity, VelMin);
velocity = min(velocity, VelMax);
% SWARMUPDATE
current_position = current_position + velocity*dt;
% Velocity Mirror Effect if over the boundary the velocity nagative
for i=1:3
IsOutside=(current_position(i)<VarMin(i) | current_position(i)>VarMax(i));
if IsOutside
velocity(i) = -velocity(i);
end
end
% Apply Position Limits
for i=1:3
current_position(i) = max(current_position(i),VarMin(i));
current_position(i) = min(current_position(i),VarMax(i));
end
%current_position
disp(['iter: ', num2str(iter),' fitness: ',num2str(current_global_best_fitness)]);
%sprintf('The value of interation iter %3.0f ', iter );
%disp('The value of interation iter %3.0f\n');
end % end of while loop its mean the end of all step that the birds move it
Elapsed_Time = toc;
X_opt=globl_best_position(:,g);
current_position=globl_best_position(:,g);
disp(['Error: ',num2str(global_best_fitness),' Elapsed Time:',num2str(Elapsed_Time)]);
end