-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSO.m
142 lines (96 loc) · 4.27 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
136
137
138
139
140
141
142
%
% Copyright (c) 2016, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
function out = PSO(problem, params)
%% Problem Definiton
CostFunction = problem.CostFunction; % Cost Function
nVar = problem.nVar; % Number of Unknown (Decision) Variables
VarSize = [1 nVar]; % Matrix Size of Decision Variables
VarMin = problem.VarMin; % Lower Bound of Decision Variables
VarMax = problem.VarMax; % Upper Bound of Decision Variables
%% Parameters of PSO
MaxIt = params.MaxIt; % Maximum Number of Iterations
nPop = params.nPop; % Population Size (Swarm Size)
w = params.w; % Intertia Coefficient
wdamp = params.wdamp; % Damping Ratio of Inertia Coefficient
c1 = params.c1; % Personal Acceleration Coefficient
c2 = params.c2; % Social Acceleration Coefficient
% The Flag for Showing Iteration Information
ShowIterInfo = params.ShowIterInfo;
MaxVelocity = 0.2*(VarMax-VarMin);
MinVelocity = -MaxVelocity;
%% Initialization
% The Particle Template
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
% Create Population Array
particle = repmat(empty_particle, nPop, 1);
% Initialize Global Best
GlobalBest.Cost = inf;
% Initialize Population Members
for i=1:nPop
% Generate Random Solution
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
particle(i).Velocity = zeros(VarSize);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update the Personal Best
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);
%% Main Loop of PSO
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+ c1*rand(VarSize).*(particle(i).Best.Position - particle(i).Position) ...
+ c2*rand(VarSize).*(GlobalBest.Position - particle(i).Position);
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity, MinVelocity);
particle(i).Velocity = min(particle(i).Velocity, MaxVelocity);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Apply Lower and Upper Bound Limits
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update Personal Best
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
if ShowIterInfo
%disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
display(['Iteration: ', num2str(it)]);
display(['PSO-Solution : ', num2str(GlobalBest.Position)]);
display(['PSO-Cost: ' , num2str(GlobalBest.Cost)]);
end
ConvergencePSO.iteration(it) = it;
ConvergencePSO.ITAE(it) = GlobalBest.Cost;
% Damping Inertia Coefficient
w = w * wdamp;
end
out.pop = particle;
out.BestSol = GlobalBest;
out.BestCosts = BestCosts;
end