-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOM_MCPSO.m
262 lines (205 loc) · 8.75 KB
/
COM_MCPSO.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
clc;
clear;
close all;
tic
CostFunction = @(x) Sphere(x);
nVar = 30;
VarSize = [1 nVar];
VarMin = -10; % Lower Bound of Decision Variables
VarMax = 10; % Upper Bound of Decision Variables
nPops = 20; % Population size of slave swarms
nPopm = 20; % Population size of master swarms
ssn = 3; % Number of slave swarms
MaxIt = 1000;
ShowIterInfo = 1;
SelectLinearW = false; % true: Linear Inertia weight // False: Constriction factor Inertia weight
%% Parameters of PSO
% inertia weight and c1 c2
%% w with constrict factor chi
% Constriction Coefficients
if SelectLinearW
wmax = 0.9;
wmin = 0.4;
w = wmax;
c1 = 2.05; % Personal Acceleration Coefficient
c2 = 2.05; % Social Acceleration Coefficient
c3 = 2.00; % Acceleration constant in COL-MSPSO
else
kappa = 1;
phi1 = 2.05;
phi2 = 2.05;
phi = phi1 + phi2;
chi = 2*kappa/abs(2-phi-sqrt(phi^2-4*phi));
w = chi; % Intertia Coefficient
c1 = chi*phi1; % Personal Acceleration Coefficient
c2 = chi*phi2; % Social Acceleration Coefficient
c3 = 2.00;
end
wdamp = 1; % Damping Ratio of Inertia Coefficient
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
slave_particle = repmat(empty_particle, nPops, ssn); %The range of one slave swarm
master_particle = repmat(empty_particle, nPopm, 1); %The range of master swarm
% Initialize Global Best
GlobalBest1.Cost = inf; % Initialize GlobalBest of whole slave swarms
GlobalBest2.Cost = inf;
emptySlaveGlobalBest.Cost=inf;
emptySlaveGlobalBest.Position=[];
SlaveGlobalBest = repmat(emptySlaveGlobalBest,ssn,1); % Initialize GlobalBest of each slave swarm
BestCosts = zeros(MaxIt,1);
% Initialize cost
BestSlaveCosts = zeros(MaxIt,1);
InteriaWeight = zeros(MaxIt,1);
% for j=1:ssn
% SlaveGlobalBest(j).Cost = inf;
% SlaveGlobalBest(j).Position = 0;
% end
for j=1:ssn
for i=1:nPops
% Generate Random Solution
slave_particle(i,j).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
slave_particle(i,j).Velocity = zeros(VarSize);
% Evaluation
slave_particle(i,j).Cost = CostFunction(slave_particle(i,j).Position);
% Update the Personal Best
slave_particle(i,j).Best.Position = slave_particle(i,j).Position;
slave_particle(i,j).Best.Cost = slave_particle(i,j).Cost;
% Update Global Best
if slave_particle(i,j).Best.Cost < SlaveGlobalBest(j).Cost
SlaveGlobalBest(j) = slave_particle(i,j).Best;
end
end
if SlaveGlobalBest(j).Cost < GlobalBest1.Cost
GlobalBest1 = SlaveGlobalBest(j);
end
end
for i=1:nPopm
% Generate Random Solution
master_particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
master_particle(i).Velocity = zeros(VarSize);
% Evaluation
master_particle(i).Cost = CostFunction(master_particle(i).Position);
% Update the Personal Best
master_particle(i).Best.Position = master_particle(i).Position;
master_particle(i).Best.Cost = master_particle(i).Cost;
% Update Global Best
if master_particle(i).Best.Cost < GlobalBest2.Cost
GlobalBest2 = master_particle(i).Best;
end
end
%% Main Loop of PSO
% slave swarms evaluation
for it=1:MaxIt
for j = 1:ssn
for i=1:nPops
% Update Velocity
slave_particle(i,j).Velocity = w*slave_particle(i,j).Velocity ...
+ c1*rand(VarSize).*(slave_particle(i,j).Best.Position - slave_particle(i,j).Position) ...
+ c2*rand(VarSize).*(SlaveGlobalBest(j).Position - slave_particle(i,j).Position);
% Apply Velocity Limits
slave_particle(i,j).Velocity = max(slave_particle(i,j).Velocity, MinVelocity);
slave_particle(i,j).Velocity = min(slave_particle(i,j).Velocity, MaxVelocity);
% Update Position
slave_particle(i,j).Position = slave_particle(i,j).Position + slave_particle(i,j).Velocity;
% Apply Lower and Upper Bound Limits
slave_particle(i,j).Position = max(slave_particle(i,j).Position, VarMin);
slave_particle(i,j).Position = min(slave_particle(i,j).Position, VarMax);
% Evaluation
slave_particle(i,j).Cost = CostFunction(slave_particle(i,j).Position);
% Update Personal Best
if slave_particle(i,j).Cost < slave_particle(i,j).Best.Cost
slave_particle(i,j).Best.Position = slave_particle(i,j).Position;
slave_particle(i,j).Best.Cost = slave_particle(i,j).Cost;
% Update Global Best
if slave_particle(i,j).Best.Cost < SlaveGlobalBest(j).Cost
SlaveGlobalBest(j) = slave_particle(i,j).Best;
end
end
end
if SlaveGlobalBest(j).Cost < GlobalBest1.Cost
GlobalBest1 = SlaveGlobalBest(j);
end
end
%% master swarm evaluation
for i = 1:nPopm
% Calculate Phi for COM-MSPSO
if GlobalBest1.Cost < GlobalBest1.Cost
Phi=0;
elseif GlobalBest1.Cost == GlobalBest1.Cost
Phi = 0.5;
else
Phi = 1;
end
% Update Velocity
master_particle(i).Velocity = w*master_particle(i).Velocity ...
+ c1*rand(VarSize).*(master_particle(i).Best.Position - master_particle(i).Position) ...
+ Phi*c2*rand(VarSize).*(GlobalBest2.Position - master_particle(i).Position)...
+ (1-Phi)*c3*rand(VarSize).*(GlobalBest1.Position - master_particle(i).Position);
% Apply Velocity Limits
master_particle(i).Velocity = max(master_particle(i).Velocity, MinVelocity);
master_particle(i).Velocity = min(master_particle(i).Velocity, MaxVelocity);
% Update Position
master_particle(i).Position = master_particle(i).Position + master_particle(i).Velocity;
% Apply Lower and Upper Bound Limits
master_particle(i).Position = max(master_particle(i).Position, VarMin);
master_particle(i).Position = min(master_particle(i).Position, VarMax);
% Evaluation
master_particle(i).Cost = CostFunction(master_particle(i).Position);
if master_particle(i).Cost < master_particle(i).Best.Cost
master_particle(i).Best.Position = master_particle(i).Position;
master_particle(i).Best.Cost = master_particle(i).Cost;
% Update Global Best
if master_particle(i).Best.Cost < GlobalBest2.Cost
GlobalBest2 = master_particle(i).Best;
end
end
end
%% transfer results
% Decaying Inertia Coefficient
if SelectLinearW
w = wmax -((wmax-wmin)/MaxIt)*it;
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest2.Cost;
BestSlaveCosts(it) = GlobalBest1.Cost;
InteriaWeight(it) = w;
% Display Iteration Information
if ShowIterInfo
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it)) ': Best Slave Cost = ' num2str(BestSlaveCosts(it)) ': Inertia Weight = ' num2str(InteriaWeight(it))]);
end
end
out.pop = slave_particle;
out.BestSol = GlobalBest2;
out.BestCosts = BestCosts;
toc
%% Results
subplot(2,1,1);
% show BestCosts from master sawrm and all slave sawrms
semilogy(BestCosts,':r','LineWidth',1.5);
title('COM-MCPSO');
text(MaxIt,BestCosts(MaxIt),['(',num2str(MaxIt),',',num2str(BestCosts(MaxIt)),')'],'color','b');
%text(MaxIt,BestCosts(p),'o','color','g')
hold on
semilogy(BestSlaveCosts,'-b','LineWidth',1.5);
text(MaxIt,BestSlaveCosts(MaxIt),['(',num2str(MaxIt),',',num2str(BestSlaveCosts(MaxIt)),')'],'color','b'); %show the final cost
legend('BestMasterCosts','BestSlaveCosts');
xlabel('Iteration');
ylabel('Best Cost(log)');
grid on ;
hold off
% show Inertia Weight
subplot(2,1,2);
plot(InteriaWeight,'-k','LineWidth',1.5);
title('Inertia-Weight');
xlabel('Iteration');
ylabel('Inertia Weight');