-
Notifications
You must be signed in to change notification settings - Fork 0
/
pGAPSO_III.m
243 lines (226 loc) · 6.21 KB
/
pGAPSO_III.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
function result = pGAPSO_III(feat,label,opts)
%% Parameters settings
if isfield(opts,'N'), N = opts.N; end
if isfield(opts,'T'), max_Iter = opts.T; end
% Parameters of GA
CR = 0.8; % crossover rate
MR = 0.01; % mutation rate
if isfield(opts,'CR'), CR = opts.CR; end
if isfield(opts,'MR'), MR = opts.MR; end
% Parameters of PSO
lb = 0;
ub = 1;
thres = 0.5;
c1 = 2; % cognitive factor
c2 = 2; % social factor
w = 0.9; % inertia weight
Vmax = (ub - lb) / 2; % Maximum velocity
if isfield(opts,'c1'), c1 = opts.c1; end
if isfield(opts,'c2'), c2 = opts.c2; end
if isfield(opts,'w'), w = opts.w; end
if isfield(opts,'Vmax'), Vmax = opts.Vmax; end
if isfield(opts,'thres'), thres = opts.thres; end
% Number of solutions in GA and PSO
halfN = N / 2;
% Objective function
fun = @maxFitness;
% Number of dimensions
dim = 52;
% Initial GA
X_GA = InitializationGA(halfN,dim);
% Initial PSO
X_PSO = InitializationPSO(halfN,dim);
V = zeros(halfN,dim);
% Fitness
fit_GA = zeros(1,halfN);
fit_PSO = zeros(1,halfN);
fitG = - inf;
%% Evaluate GA and PSO
% Evaluate GA
for i = 1:halfN
fit_GA(i) = fun(feat,label,X_GA(i,:));
% GBest update
if fit_GA(i) > fitG
fitG = fit_GA(i);
Xgb = X_GA(i,:);
end
end
% Evaluate PSO
for i = 1:halfN
fit_PSO(i) = fun(feat,label,(X_PSO(i,:) > thres));
% GBest update
if fit_PSO(i) > fitG
fitG = fit_PSO(i);
Xgb = X_PSO(i,:);
end
end
% PBest
Xpb = X_PSO;
fitP = fit_PSO;
%% Pre
curve = - inf;
curve(1) = fitG;
t = 2;
%% Iterations
while t <= max_Iter
% Part of GA
% Get probability
prob = fit_GA / sum(fit_GA);
% Preparation
Xc1 = zeros(1,dim);
Xc2 = zeros(1,dim);
fitC1 = ones(1,1);
fitC2 = ones(1,1);
z = 1;
for i = 1:halfN
if rand() < CR
% Select two parents
k1 = RouletteWheelSelection(prob);
k2 = RouletteWheelSelection(prob);
% Store parents
P1 = X_GA(k1,:);
P2 = X_GA(k2,:);
% Single point crossover
ind = randi([1,dim - 1]);
% Crossover between two parents
Xc1(z,:) = [P1(1:ind),P2(ind + 1:dim)];
Xc2(z,:) = [P2(1:ind),P1(ind + 1:dim)];
% Mutation
for d = 1:dim
% First child
if rand() < MR
Xc1(z,d) = 1 - Xc1(z,d);
end
% Second child
if rand() < MR
Xc2(z,d) = 1 - Xc2(z,d);
end
end
% Fitness
fitC1(1,z) = fun(feat,label,Xc1(z,:));
% GBest update
if fitC1(1,z) > fitG
fitG = fitC1(1,z);
Xgb = Xc1(z,:);
end
fitC2(1,z) = fun(feat,label,Xc2(z,:));
% GBest update
if fitC2(1,z) > fitG
fitG = fitC2(1,z);
Xgb = Xc2(z,:);
end
z = z + 1;
end
end
% Part of PSO
for i = 1:halfN
for d = 1:dim
r1 = rand();
r2 = rand();
% Velocity update (2a)
VB = w * V(i,d) + c1 * r1 * (Xpb(i,d) - X_PSO(i,d)) + ...
c2 * r2 * (Xgb(d) - X_PSO(i,d));
% Velocity limit
VB(VB > Vmax) = Vmax; VB(VB < -Vmax) = -Vmax;
V(i,d) = VB;
% Position update (2b)
X_PSO(i,d) = X_PSO(i,d) + V(i,d);
end
% Boundary
XB = X_PSO(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X_PSO(i,:) = XB;
% Fitness
fit_PSO(i) = fun(feat,label,(X_PSO(i,:) > thres));
% Pbest update
if fit_PSO(i) > fitP(i)
fitP(i) = fit_PSO(i);
Xpb(i,:) = X_PSO(i,:);
end
% Gbest update
if fitP(i) > fitG
fitG = fitP(i);
Xgb = Xpb(i,:);
end
end
% Merge population
XX = [X_GA; Xc1; Xc2; (Xpb(:,:) > thres)];
FF = [fit_GA,fitC1,fitC2,fitP];
% Select N best solution
[FF,idx] = sort(FF, 'descend');
X = XX(idx(1:N),:);
fit = FF(1:N);
% Save
curve(t) = fitG;
fprintf('\nIteration %d Best (pGAPSO-III)= %.2f\n',t,curve(t))
% Stop the loop if fitG is not updated for 30 iterations
if t > 30 && t <= max_Iter && abs( curve(t) - curve(t - 30) ) <= 10^(-4)
break
end
t = t + 1;
% Distribute new solutions to GA and PSO randomly
[X_GA,X_PSO,fit_GA,fit_PSO] = RandomSolutionDistribution(X,fit);
% Pbest update
for i = 1:halfN
if fit_PSO(i) > fitP(i)
fitP(i) = fit_PSO(i);
Xpb(i,:) = X_PSO(i,:);
end
end
end
delete(gcp('nocreate')); % Closing parallel pool
% Select features based on selected index
Pos = 1:dim;
Sf = Pos((Xgb > thres) == 1);
% sFeat = feat(:,Sf);
% Store results
result.sf = Sf;
% result.ff = sFeat;
% result.nf = length(Sf);
result.c = curve;
% result.f = feat;
% result.l = label;
end
%% RouletteWheelSelection function
function Index = RouletteWheelSelection(prob)
% Cummulative summation
C = cumsum(prob);
% Random one value, most probability value [0~1]
P = rand();
% Route wheel
for i = 1:length(C)
if C(i) > P
Index = i;
break;
end
end
end
%% Initialization function of GA
function X = InitializationGA(N,dim)
X = zeros(N,dim);
for i = 1:N
for d = 1:dim
if rand() > 0.5
X(i,d) = 1;
end
end
end
end
%% Initialization function of PSO
function X = InitializationPSO(N,dim)
X = zeros(N,dim);
for i = 1:N
for d = 1:dim
X(i,d) = rand();
end
end
end
%% RandomSolutionDistribution function
function [X_GA,X_PSO,fit_GA,fit_PSO] = RandomSolutionDistribution(X,fit)
N = length(fit);
idx_PSO = randperm(N,N/2); % Index array of selected solutions in PSO
idx_GA = setdiff(1:N,idx_PSO); % Index array of selected solutions in GA
X_GA = X(idx_GA,:);
X_PSO = X(idx_PSO,:);
fit_GA = fit(idx_GA);
fit_PSO = fit(idx_PSO);
end