-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSO.m
86 lines (71 loc) · 2.75 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
function [bestPos bestFit Conver] =PSO(IntPop,It,LB,UB,S,Node)
format long g
Nvars=size(IntPop,2);
% Default Parameters
C1 = 2;% this values affect the localization accuracy
C2 = 2;%this values affect the localization accuracy
N = size(IntPop,1);
W = 0.2;
beta=0.4;
% Initial Position and Velocity
CurrentPosition=IntPop;
Velocity = W.*rand(N,Nvars) ; % Initial Velocity
% Evalute Initial Position
CurrentFitness = zeros(N,1); % Fitness Value
for it=1:size(CurrentPosition,1)
temp=CurrentPosition(it,:);
temp=CheckLimits(temp,LB,UB,Node);
CurrentPosition(it,:)=temp;
CurrentFitness(it,1)=fitness(temp,S) ;
end
% Update Local Best
LocalBestPosition = CurrentPosition; % Local Best
LocalBestFitness = CurrentFitness;
% Update Global Best
[temmp,index]=max(LocalBestFitness);
if temmp==0
GlobalBestFitness =0;
else
GlobalBestFitness =temmp;
end
GlobalBestPosition = LocalBestPosition(index,:); % Global Best
% Update Velocity and Position
R1 =rand(N,Nvars); % random Number 1
R2 =rand(N,Nvars); % random Number 2
Velocity = W.*Velocity + C1.*(R1.*(LocalBestPosition-CurrentPosition))...
+ C2.*(R2.*(GlobalBestFitness-CurrentPosition));
CurrentPosition = CurrentPosition + Velocity ;
% Iterate to Achive Best
Iter = 0;
while (Iter < It)
Iter = Iter+1;
%%Evalute Current Position
for it=1:size(CurrentPosition,1)
temp=CurrentPosition(it,:);
temp=CheckLimits(temp,LB,UB,Node);
CurrentPosition(it,:)=temp;
CurrentFitness(it,1)=fitness(temp,S);
end
% Update Local Best
indexes = find(CurrentFitness > LocalBestFitness);
LocalBestFitness(indexes) = CurrentFitness(indexes);
LocalBestPosition(indexes,:) = CurrentPosition(indexes,:);
% Update Global Best
[GlobalBestFitnessNew,index] = max(LocalBestFitness);
if GlobalBestFitnessNew > GlobalBestFitness
GlobalBestFitness = GlobalBestFitnessNew;
GlobalBestPosition = LocalBestPosition(index,:);
end
% Update Velocity and Position
R1 =rand(N,Nvars); % random Number 1
R2 =rand(N,Nvars); % random Number 2
Velocity = W.*Velocity + C1.*(R1.*(LocalBestPosition-CurrentPosition))...
+ C2.*(R2.*(GlobalBestFitness-CurrentPosition));
CurrentPosition = CurrentPosition + Velocity ;
Conver(Iter)=GlobalBestFitness;
% disp(sprintf('Iteration - %d best fitness - %5.4f',Iter,GlobalBestFitness))
end
% Output
bestPos = round(abs(GlobalBestPosition));
bestFit = GlobalBestFitness;
end