-
Notifications
You must be signed in to change notification settings - Fork 1
/
MIPO.m
276 lines (229 loc) · 9.19 KB
/
MIPO.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
clc ;
clear all ;
close all ;
format shortg
prompt = {'Please enter the number of run:'} ;
title = 'MIPO Algorithm' ;
dims = [1 45] ;
nline = 1 ;
definput = {'1','a'} ;
answer = inputdlg(prompt,title,dims,definput) ;
Run_Num = answer(1,:) ;
Run_Num = str2num(Run_Num{:}) ;
n = 0 ;
Bests = zeros(1 , Run_Num ) ;
BestsPop = zeros(Run_Num , 2 ) ; %varaible number
BestsCnvg = zeros(Run_Num , 200 ) ; % numofruns
RunTime = zeros(1 , Run_Num ) ;
NoU_index = zeros(Run_Num , 1 ) ;
Stable = 0 ;
NoU = 0 ; % Number of Unstable filters
for n = 1:Run_Num
tic
n
%% IIR Filter Fitness
[Hfilt Wfilt] = IIR_main();
%%
% glocal = 0: Global search, 1: Local search
% localdist: maximum distance used for choosing local balls within it
stallgenlimit = 200 ;
TolFun = inf ;
% Na = 'testfunc';
localnum = 3 ;
glocal = 0 ;
% cont = num2str(1);
% fitnessfunc = [Na, cont];
plots = 1;
numofruns = 200 ;
numofdims = 2 ; % Exp. 1:2; Exp. 2:4; Exp. 3:9; Exp. 3.1=11;
numofballs = 50 ;
k1damp = .005 ; % Exp. 1: 0.11 Exp. 3: 0.004
k2damp = 0.9 ; % Exp. 1: 2 Exp. 3: 0.9
k1 = 0 ;%
k2 = 0 ;
Xmininit = repmat(-1.2, 1, numofdims); %Exp. 1:-2; Exp. 2:-2; Exp. 3:0;
Xmaxinit = repmat(+1.2, 1, numofdims); %Exp. 1:2; Exp. 2:2; Exp. 3:1;
% Xmininit = [0.1084 0.5419 1.0837 1.0837 0.5419 0.1084 -0.9853 -0.9738 -0.3864 -0.1112 -0.0113];
% Xmaxinit = [0.1084 0.5419 1.0837 1.0837 0.5419 0.1084 -0.9853 -0.9738 -0.3864 -0.1112 -0.0113];
%% Loop Main
% fitnessfunc = str2func(Fitfilter);
bestfit = Inf; % stores previous total best
Neval = 0; % number of evaluating fitness function%
% expanding Xmin, Xmax to cover all balls
Xmin = repmat(Xmininit, numofballs, 1);
Xmax = repmat(Xmaxinit, numofballs, 1);
% generating initial balls
% an option to generate custom initial balls can be added later
X = Xmin + (Xmax - Xmin) .* rand(numofballs, numofdims);
% initializing balls acceleration
A = zeros(numofballs, numofdims);
% initializes some variables for displaying the results
meanfits = zeros(numofruns, 1);
bests = zeros(numofruns, 1);
worsts = zeros(numofruns, 1);
%%%**********************************************************************
N = rand(size(Hfilt,1),1) ; % Between [0 1]
heights = Fitness(X,Hfilt,Wfilt,N) ;
%%%***********************************************************************
Neval = Neval + numofballs;
[tmpbestfit, tmpbestfitidx] = min(heights);
bestfit = tmpbestfit;
bestpop = X(tmpbestfitidx, :);
stallgenctrl = 0;
t = 1;
% if plots
% hold on;
% end
%% Main loop
while ((t <= numofruns) && (stallgenctrl <= stallgenlimit))
% calculating the acceleration for each ball
A(:, :) = 0;
% Choosing global or local algorithm
% glocal = 0: Global search, 1: Local search
if glocal
% local version
for i = 1:numofballs
dists = dist(X(i, :), X');
[~, localind] = sort(dists);
localind = localind(2:localnum + 1);
for j = 1:localnum
dheight = heights(localind(j)) - heights(i);
% uses better balls to estimate the slope and calculate the
% acceleration. In addition, it ensures (X(i, :) - X(j, :)) > 0
% for all dimensions.
if dheight < 0
A(i, :) = A(i, :) + sin(atan(dheight ./ (X(i, :) - X(localind(j), :))));
% A(i, :) = A(i, :) + sin(abs(dheight)./(sqrt(((dheight).^2)+((X(i, :) - X(j, :)).^2))));
end
end
end
else
% global version
for i = 1:numofballs
for j = 1:numofballs
dheight = heights(j) - heights(i);
% uses better balls to estimate the slope and calculate the
% acceleration. In addition, it ensures (X(i, :) - X(j, :)) > 0
% for all dimensions.
if dheight < 0
A(i, :) = A(i, :) + sin(atan(dheight ./ (X(i, :) - X(j, :))));
% A(i, :) = A(i, :) + sin(abs(dheight)./(sqrt(((dheight).^2)+((X(i, :) - X(j, :)).^2))));
end
end
end
end
% sigmoid method for changing coefficient:
% higher c1: faster convergence in first steps, worst local search
% lower c1: slower convergence and better global search in first steps,
% better local search
% c2 results to better local search
% k1 = c1 ./ (1 + exp((t - shift1) .* scale1));
% k2 = c2 ./ (1 + exp(-(t - shift2) .* scale2));
k1 = k1damp *( (numofruns - t) / numofruns) ;
k2 = k2damp *( t / numofruns) ;
% updating balls
besttoX = repmat(bestpop, numofballs, 1) - X;
X = X + k1 .* rand(numofballs, numofdims) .* A + ...
k2 .* rand(numofballs, numofdims) .* besttoX;
% ensures that all balls lie in the problem's boundaries
tmpmaxchk = X > Xmax;
tmpminchk = X < Xmin;
X = X .* ~(tmpmaxchk | tmpminchk) + Xmax .* tmpmaxchk + Xmin .* tmpminchk;
% evaluates fitness of each ball
%%%*******************************************************************
heights = Fitness(X,Hfilt,Wfilt,N);
%%%**********************************************************************
Neval = Neval + numofballs;
% finding and storing the global best ball and its fitness
[tmpbestfit, tmpbestfitidx] = min(heights);
if abs(tmpbestfit - bestfit) < TolFun
stallgenctrl = stallgenctrl + 1;
else
stallgenctrl = 0;
end
if tmpbestfit < bestfit
bestfit = tmpbestfit;
bestpop = X(tmpbestfitidx, :);
end
meanfits(t) = mean(heights);
bests(t) = bestfit;
worsts(t) = max(heights);
%%
if plots
disp(['Iteration ' num2str(t) ' :BestCost= ' num2str(bests(t))]);
% t
% plot(t, bests(t), '.r','LineWidth',1);
% % legend('bests - Mipo')
% xlabel('Iteration')
% ylabel('Fitness')
% plot(t, meanfits(t), '.b','LineWidth',4);
% legend('best','mean')
% xlabel('Iteration')
% ylabel('Fitness')
% % plot(t, worsts(t), '.r');
% figure(gcf);
% hold on
end
t = t + 1;
end
BestsCnvg(n,:) = bests ;
Bests(n) = bests(t-1) ;
BestsPop(n,:) = bestpop ;
RunTime(n) = toc ;
[Bsoa Asoa Z_f P_f] = Matching(bestpop) ;
Stable = abs(P_f) ;
if any(Stable > 1)
NoU = NoU + 1 ;
NoU_index(n) = n ;
end
end
% disp([' ']);
disp([' ']);
disp([' MIPO ']);
disp(['-----------------------------------------------']);
disp(['Number of run = ' num2str(Run_Num)]);
disp([' ']);
disp([' ']);
disp(['**************** Statistical indexes : Time ****************']);
disp(['------------------------------------------------']);
disp(['Per run = ' num2str(RunTime)]);
disp(['Average = ' num2str(mean(RunTime))]);
disp(['Standard deviation = ' num2str(std(RunTime))]);
disp(['Maximum = ' num2str(max(RunTime))]);
disp(['Minimum = ' num2str(min(RunTime))]);
% disp([' ']);
disp([' ']);
disp(['***************** Statistical indexes : Fitness ****************']);
disp(['-----------------------------------------------']);
disp(['Number of run = ' num2str(Run_Num)]);
disp(['Best cost per run = ' num2str(Bests)]);
disp(['Average = ' num2str(mean(Bests))]);
disp(['Standard deviation = ' num2str(std(Bests))]);
disp(['Maximum = ' num2str(max(Bests))]);
disp(['Minimum = ' num2str(min(Bests))]);
% disp([' ']);
disp([' ']);
disp(['***************** Instability ****************']);
disp(['-----------------------------------------------']);
disp(['Nomber of Unstable Filters = ' num2str(NoU)]);
disp([' ']);
%% Implementation of IIR MIPO *******************************
[minimum index] = min(Bests);
disp([ ' Best Solution = ' num2str(BestsPop(index,:))])
[Bsoa Asoa Z_f P_f] = Matching(BestsPop(index,:))
IIR_main();
figure(1);
plot(BestsCnvg(index,:),'.b','LineWidth',1);
legend('Bests')
xlabel('Iteration')
ylabel('Fitness')
figure(2);
zplane(Z_f,P_f); %%% Displays the poles and zeros of discrete-time systems.
legend('Zero','Pole');
xlabel('Real Part');
ylabel('Imaginary Plot');
% title('Pole-Zero Plot in MIPO');
figure(3);
H = abs(Hfilt);
Hdb=20*log10(H);
plot(Wfilt/512,Hdb);grid