-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSO.m
96 lines (78 loc) · 3.17 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
% Ali Mohammadi_INS/GNSS
% for Rep = 1:100
clc ;
clear all ;
close all ;
tic ;
npop = 50 ;
nvar = 18 ;
w = 1 ;
maxit = 400 ;
wdamp = 0.99 ;
c1 = 2 ;
c2 = 2 ;
xmin = [0 0 0 0 0 0 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10 1e-10] ; %%%% Define lowband
xmax = [1 1 1 100 100 100 1 1 1 1 1 1 1 1 1 1 1 1 ] ; %%%% Define upperband
dx = xmax - xmin ;
vmax = 0.1*dx ;
empty_particle.position = [] ;
empty_particle.velocity = [] ;
empty_particle.cost = [] ;
empty_particle.pbest = [] ;
empty_particle.pbestcost = [] ;
particle = repmat(empty_particle,npop,1) ;
gbest = zeros(maxit,nvar) ;
gbestcost = zeros(maxit,1) ;
for it = 1:maxit
if it == 1
gbestcost(1) = inf ;
for i = 1:npop
particle(i).velocity = zeros(1,nvar) ;
particle(i).position = xmin+(xmax-xmin).*rand(1,nvar) ;
%%%%%%%%%%%%%%%%%%%%
particle(i).cost = fitness1(particle(i).position) ;
%%%%%%%%%%%%%%%%%%%%
particle(i).pbest = particle(i).position ;
particle(i).pbestcost = particle(i).cost ;
if particle(i).pbestcost<gbestcost(it)
gbest(it,:) = particle(i).pbest ;
gbestcost(it) = particle(i).pbestcost ;
end
end
else
gbest(it,:) = gbest(it-1,:) ;
gbestcost(it) = gbestcost(it-1) ;
for i=1:npop
particle(i).velocity = w*particle(i).velocity...
+c1*rand*(particle(i).pbest-particle(i).position)...
+c2*rand*(gbest(it,:)-particle(i).position) ;
particle(i).velocity = min(max(particle(i).velocity,-vmax),vmax) ;
particle(i).position = particle(i).position+particle(i).velocity ;
particle(i).position = min(max(particle(i).position,xmin),xmax) ;
%%%%%%%%%%%%%%%%%%%
particle(i).cost = fitness1(particle(i).position) ;
%%%%%%%%%%%%%%%%%%%
if particle(i).cost<particle(i).pbestcost
particle(i).pbest = particle(i).position ;
particle(i).pbestcost = particle(i).cost ;
if particle(i).pbestcost<gbestcost(it)
gbest(it,:) = particle(i).pbest ;
gbestcost(it) = particle(i).pbestcost ;
end
end
end
end
disp(['Iter= ' num2str(it) ' // Best Cost = ' num2str(gbestcost(it))]) ;
w = w*wdamp ;
it
end
disp([ ' Best Solution = ' num2str(gbest(it,:))])
disp([ ' Best Fitness = ' num2str(gbestcost(it))])
Time = toc/3600
% gbest(it,:)
% figure(1) ;
% plot(gbestcost,'r','LineWidth',2) ;
% legend('PSO')
% hold on
fitness2(gbest(it,:))
gbest(it,:)