-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSO.m
76 lines (59 loc) · 1.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
% Particle Swarm Optimization
function [gBestScore,gBest,cg_curve]=PSO(noP,Max_iter,lb,ub,dim,fobj,da)
%PSO Infotmation
Vmax=ones(1,dim).*(ub-lb).*0.15; %速度最大值
w=0.8;
c1=1.2;
c2=1.2;
% Initializations
iter=Max_iter;
vel=zeros(noP,dim);
pBestScore=zeros(noP);
pBest=zeros(noP,dim);
cg_curve=zeros(1,iter);
% Random initialization for agents.
pos=initialization(noP,dim,ub,lb);
for i=1:noP
pBestScore(i)=inf;
end
% Initialize gBestScore for a minimization problem
gBestScore=inf;
for l=1:iter
% Return back the particles that go beyond the boundaries of the search
% space
for i=1:size(pos,1)
Flag4ub=pos(i,:)>ub;
Flag4lb=pos(i,:)<lb;
pos(i,:)=(pos(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
end
for i=1:size(pos,1)
%Calculate objective function for each particle
fitness= fobj(pos(i,:)',da);
if(pBestScore(i)>fitness)
pBestScore(i)=fitness;
pBest(i,:)=pos(i,:);
end
if(gBestScore>fitness)
gBestScore=fitness;
gBest=pos(i,:);
end
end
%Update the W of PSO
%Update the Velocity and Position of particles
for i=1:size(pos,1)
for j=1:size(pos,2)
vel(i,j)=w*vel(i,j)+c1*rand()*(pBest(i,j)-pos(i,j))+c2*rand()*(gBest(j)-pos(i,j));
if(vel(i,j)>Vmax(1,j))
vel(i,j)=Vmax(1,j);
end
if(vel(i,j)<-Vmax(1,j))
vel(i,j)=-Vmax(1,j);
end
pos(i,j)=pos(i,j)+vel(i,j);
end
end
cg_curve(l)=gBestScore;
disp(['PSO: 第', num2str(l), '迭代', ', the best fitness is ', num2str(gBestScore)])
disp(['第',num2str(l),'次寻优的最佳位置为:[',num2str(fix(gBest)),']'])
end
end