-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHHO.m
421 lines (384 loc) · 15.2 KB
/
HHO.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
% Main paper:
% Segmentation of Brain MRI using an Altruistic Harris Hawks' Optimization algorithm
% Rajarshi Bandopadhyay, Rohit Kundu, Diego Oliva, Ram Sarkar
% _____________________________________________________
function [Rabbit_Energy,Rabbit_Location,metrics,time, Iout]=HHO(N,T,lb,ub,dim,Im)
tic
format long;
format compact;
rand('seed', sum(100 * clock));
%wnd = floor(1*Max_iter);
problem_size = dim;
[ih, ~] = imhist(Im(:,:,1));% histogram Check Normalization
%max_nfes = SearchAgents_no * Max_iter;
[sz1,sz2] = size(Im(:,:,1));% im size
fa=0;
% initialize the location and Energy of the rabbit
Rabbit_Location=zeros(1,dim);
Rabbit_Energy=inf;
%Initialize the locations of Harris' hawks
X=initialization(N,dim,ub,lb);
size(Rabbit_Location);
[szf,szc] = size(X);
%szf is the size of the X vector
for zz = 1:szf % Evaluation in one shot
indivi = sort(fix(X(zz,:)));
fitness(zz) = hybrid_loss(indivi,ih);
fitness = fitness';
end
converH=[];
%CNVG=zeros(1,T);
t=0; % Loop counter
while t<T
for i=1:size(X,1)
% Check boundries
FU=X(i,:)>ub;FL=X(i,:)<lb;X(i,:)=(X(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL;
% fitness of locations
indivi = sort(fix(X(i,:)));
fitness(i) = hybrid_loss(indivi,ih);
%indivi
%ih
%fitness=f_obj(X(i,:));
% Update the location of Rabbit
if fitness(i)<Rabbit_Energy
Rabbit_Energy=fitness(i);
Rabbit_Location=X(i,:);
end
end
E1=2*(1-(t/T)); % factor to show the decreaing energy of rabbit
% Update the location of Harris' hawks
for i=1:size(X,1)
E0=2*rand()-1; %-1<E0<1
Escaping_Energy=E1*(E0); % escaping energy of rabbit
if abs(Escaping_Energy)>=1
%% Exploration:
% Harris' hawks perch randomly based on 2 strategy:
q=rand();
rand_Hawk_index = floor(N*rand()+1);
X_rand = X(rand_Hawk_index, :);
if q<0.5
% perch based on other family members
X(i,:)=X_rand-rand()*abs(X_rand-2*rand()*X(i,:));
elseif q>=0.5
% perch on a random tall tree (random site inside group's home range)
X(i,:)=(Rabbit_Location(1,:)-mean(X))-rand()*((ub-lb)*rand+lb);
end
elseif abs(Escaping_Energy)<1
%% Exploitation:
% Attacking the rabbit using 4 strategies regarding the behavior of the rabbit
%% phase 1: surprise pounce (seven kills)
% surprise pounce (seven kills): multiple, short rapid dives by different hawks
r=rand(); % probablity of each event
temp_var=0;
if r>=0.5 && abs(Escaping_Energy)<0.5 % Hard besiege
X(i,:)=(Rabbit_Location)-Escaping_Energy*abs(Rabbit_Location-X(i,:));
end
if r>=0.5 && abs(Escaping_Energy)>=0.5 % Soft besiege
Jump_strength=2*(1-rand()); % random jump strength of the rabbit
X(i,:)=(Rabbit_Location-X(i,:))-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:));
end
%% phase 2: performing team rapid dives (leapfrog movements)
if r<0.5 && abs(Escaping_Energy)>=0.5, % Soft besiege % rabbit try to escape by many zigzag deceptive motions
Jump_strength=2*(1-rand());
X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:));
%a___=hybrid_loss(X1',ih);
%hybrid_loss(sort(fix(X(i,:))),ih);
X1=check_lb_n_ub(X1, lb, ub);
temp_var=E0;
[X1, X(i, :)]=altr(X1, X(i, :),temp_var , lb, ub, ih);
indivi2=hybrid_loss(sort(fix(X(i,:))),ih);
indivi = hybrid_loss(sort(fix(X1)),ih);
if indivi<indivi2 % improved move?
X(i,:)=X1;
else % hawks perform levy-based short rapid dives around the rabbit
X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:))+rand(1,dim).*Levy(dim);
X2=check_lb_n_ub(X2, lb, ub);
temp_var=E0;
[X2, X(i, :)]=altr(X2, X(i, :),temp_var , lb, ub, ih);
indivi2=sort(fix(X(i,:)));
indivi=sort(fix(X2));
if (hybrid_loss(indivi,ih)<hybrid_loss(indivi2,ih)), % improved move?
X(i,:)=X2;
end
end
end
if r<0.5 && abs(Escaping_Energy)<0.5, % Hard besiege % rabbit try to escape by many zigzag deceptive motions
% hawks try to decrease their average location with the rabbit
Jump_strength=2*(1-rand());
X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X));
temp_var=E0*(1/szf);
X1=check_lb_n_ub(X1, lb, ub);
[X1, X(i, :)]=altr(X1, X(i, :),temp_var , lb, ub, ih);
indivi2=sort(fix(X(i,:)));
indivi=sort(fix(X1));
if hybrid_loss(indivi,ih)<hybrid_loss(indivi2,ih) % improved move?
X(i,:)=X1;
else % Perform levy-based short rapid dives around the rabbit
X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X))+rand(1,dim).*Levy(dim);
X2=check_lb_n_ub(X2, lb, ub);
temp_var=E0*(1/szf);
[X2, X(i, :)]=altr(X2, X(i, :),temp_var , lb, ub, ih);
indivi=sort(fix(X2));indivi2=sort(fix(X(i,:)));
if (hybrid_loss(indivi,ih)<hybrid_loss(indivi2,ih)), % improved move?
X(i,:)=X2;
end
end
end
%%
end
end
t=t+1;
%CNVG(t)=Rabbit_Energy;
% Print the progress every 100 iterations
% if mod(t,100)==0
% display(['At iteration ', num2str(t), ' the best fitness is ', num2str(Rabbit_Energy)]);
% end
end
bsf_solution=Rabbit_Location;
bsf_solution = fix(bsf_solution);
BThresholds = sort(bsf_solution);
gBestR = BThresholds;
Iout = imageGRAY(Im,gBestR); %Segmented Image
%% Metric calculation
window = 3;
Imd = double(Im);
Ioutd = double(Iout);
psnr = PSNR(Im, Iout); %1
SSIM = ssim(Im, Iout); %2
FSIM = FeatureSIM(Im, Iout); %3
UIQI = img_qi(Im, Iout, window); %4
QILV = qilv(Im, Iout, [window, window]); %5
HPSI = HaarPSI(Imd, Ioutd); %6
metrics = [psnr, SSIM, FSIM, UIQI, QILV, HPSI];
time = toc;
%converH = converH';
bsf_solution = sort(BThresholds);
Rabbit_Location=bsf_solution;
end
% ___________________________________
function o=Levy(d)
beta=1.5;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
u=randn(1,d)*sigma;v=randn(1,d);step=u./abs(v).^(1/beta);
o=step;
end
function [X1]=check_lb_n_ub(X1, lb, ub)
for i = 1:size(X1,2)
if X1(i)<lb
X1(i)=lb;
end
if X1(i)>ub
X1(i)=ub;
end
end
end
function [X_exploit, X_original]=altr(X_exploit, X_original, r, lb, ub, ih)
xtra=1+ (floor(rand()*4));% an xtra no that can be added or subtracted to the X_exploit between 0 and 3
for i = 1:size(X_exploit)
b=hybrid_loss(sort(fix(X_exploit)),ih);
c=hybrid_loss(sort(fix(X_original)),ih);
X_exploit(i)=X_exploit(i)+xtra;
X_original(i)=X_original(i)-xtra;
X_exploit=check_lb_n_ub(X_exploit, lb, ub);
X_original=check_lb_n_ub(X_original, lb, ub);
b=b-hybrid_loss(sort(fix(X_exploit)),ih);
c=hybrid_loss(sort(fix(X_original)),ih)-c;
if r*b<=c || b<0
X_exploit(i)=X_exploit(i)-xtra;
X_original(i)=X_original(i)+xtra;
X_exploit=check_lb_n_ub(X_exploit, lb, ub);
X_original=check_lb_n_ub(X_original, lb, ub);
end
b=hybrid_loss(sort(fix(X_exploit)),ih);
c=hybrid_loss(sort(fix(X_original)),ih);
X_exploit(i)=X_exploit(i)-xtra;
X_original(i)=X_original(i)+xtra;
X_exploit=check_lb_n_ub(X_exploit, lb, ub);
X_original=check_lb_n_ub(X_original, lb, ub);
b=b-hybrid_loss(sort(fix(X_exploit)),ih);
c=hybrid_loss(sort(fix(X_original)),ih)-c;
if r*b<=c || b<0
X_exploit(i)=X_exploit(i)+xtra;
X_original(i)=X_original(i)-xtra;
X_exploit=check_lb_n_ub(X_exploit, lb, ub);
X_original=check_lb_n_ub(X_original, lb, ub);
end
end
%xtra
end
% % no_search_agents, no_itns, lb, ub, dim, fobj
% %function [Rabbit_Energy,Rabbit_Location,CNVG]=HHO(N,T,lb,ub,dim,fobj)
% function [Rabbit_Energy,Rabbit_Location,metrics,time, Iout]=HHO(N,T,lb,ub,dim,Im)
%
% %disp('HHO is now tackling your problem')
% tic
% format long;
% format compact;
% rand('seed', sum(100 * clock));
%
% %wnd = floor(1*Max_iter);
% problem_size = dim;
% [ih, ~] = imhist(Im(:,:,1));% histogram Check Normalization
% %max_nfes = SearchAgents_no * Max_iter;
% [sz1,sz2] = size(Im(:,:,1));% im size
% fa=0;
%
% % initialize the location and Energy of the rabbit
% Rabbit_Location=zeros(1,dim);
% Rabbit_Energy=inf;
%
% %Initialize the locations of Harris' hawks
% X=initialization(N,dim,ub,lb);
% size(Rabbit_Location);
% [szf,szc] = size(X);
%
% for zz = 1:szf % Evaluation in one shot
% indivi = sort(fix(X(zz,:)));
% fitness(zz) = hybrid_loss(indivi,ih);
% fitness = fitness';
% end
% converH=[];
% %CNVG=zeros(1,T);
%
% t=0; % Loop counter
%
% while t<T
% for i=1:size(X,1)
% % Check boundries
% FU=X(i,:)>ub;FL=X(i,:)<lb;X(i,:)=(X(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL;
% % fitness of locations
% indivi = sort(fix(X(i,:)));
% fitness(i) = hybrid_loss(indivi,ih);
% %fitness=f_obj(X(i,:));
% % Update the location of Rabbit
% if fitness(i)<Rabbit_Energy
% Rabbit_Energy=fitness(i);
% Rabbit_Location=X(i,:);
% end
% end
%
% E1=2*(1-(t/T)); % factor to show the decreaing energy of rabbit
% % Update the location of Harris' hawks
% for i=1:size(X,1)
% E0=2*rand()-1; %-1<E0<1
% Escaping_Energy=E1*(E0); % escaping energy of rabbit
%
% if abs(Escaping_Energy)>=1
% %% Exploration:
% % Harris' hawks perch randomly based on 2 strategy:
%
% q=rand();
% rand_Hawk_index = floor(N*rand()+1);
% X_rand = X(rand_Hawk_index, :);
% if q<0.5
% % perch based on other family members
% X(i,:)=X_rand-rand()*abs(X_rand-2*rand()*X(i,:));
% elseif q>=0.5
% % perch on a random tall tree (random site inside group's home range)
% X(i,:)=(Rabbit_Location(1,:)-mean(X))-rand()*((ub-lb)*rand+lb);
% end
%
% elseif abs(Escaping_Energy)<1
% %% Exploitation:
% % Attacking the rabbit using 4 strategies regarding the behavior of the rabbit
%
% %% phase 1: surprise pounce (seven kills)
% % surprise pounce (seven kills): multiple, short rapid dives by different hawks
%
% r=rand(); % probablity of each event
%
% if r>=0.5 && abs(Escaping_Energy)<0.5 % Hard besiege
% X(i,:)=(Rabbit_Location)-Escaping_Energy*abs(Rabbit_Location-X(i,:));
% end
%
% if r>=0.5 && abs(Escaping_Energy)>=0.5 % Soft besiege
% Jump_strength=2*(1-rand()); % random jump strength of the rabbit
% X(i,:)=(Rabbit_Location-X(i,:))-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:));
% end
%
% %% phase 2: performing team rapid dives (leapfrog movements)
% if r<0.5 && abs(Escaping_Energy)>=0.5, % Soft besiege % rabbit try to escape by many zigzag deceptive motions
%
% Jump_strength=2*(1-rand());
% X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:));
%
% %a___=hybrid_loss(X1',ih);
% %hybrid_loss(sort(fix(X(i,:))),ih);
% indivi2=hybrid_loss(sort(fix(X(i,:))),ih);
%
% %indivi = hybrid_loss(sort(fix(X1)),ih);
% if indivi2<indivi2 % improved move?
% X(i,:)=X1;
% else % hawks perform levy-based short rapid dives around the rabbit
% X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:))+rand(1,dim).*Levy(dim);
% indivi2=sort(fix(X(i,:)));
% %indivi=sort(fix(X2));
% if (hybrid_loss(indivi2,ih)<hybrid_loss(indivi2,ih)), % improved move?
% X(i,:)=X2;
% end
% end
% end
%
% if r<0.5 && abs(Escaping_Energy)<0.5, % Hard besiege % rabbit try to escape by many zigzag deceptive motions
% % hawks try to decrease their average location with the rabbit
% Jump_strength=2*(1-rand());
% X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X));
%
% indivi2=sort(fix(X(i,:)));
% %indivi=sort(fix(X1));
% if hybrid_loss(indivi2,ih)<hybrid_loss(indivi2,ih) % improved move?
% X(i,:)=X1;
% else % Perform levy-based short rapid dives around the rabbit
% X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X))+rand(1,dim).*Levy(dim);
% %indivi=sort(fix(X2));
% indivi2=sort(fix(X(i,:)));
% if (hybrid_loss(indivi2,ih)<hybrid_loss(indivi2,ih)), % improved move?
% X(i,:)=X2;
% end
% end
% end
% %%
% end
% end
% t=t+1;
% %CNVG(t)=Rabbit_Energy;
% % Print the progress every 100 iterations
% % if mod(t,100)==0
% % display(['At iteration ', num2str(t), ' the best fitness is ', num2str(Rabbit_Energy)]);
% % end
% end
% bsf_solution=Rabbit_Location;
% bsf_solution = fix(bsf_solution);
% BThresholds = sort(bsf_solution);
%
% gBestR = BThresholds;
% Iout = imageGRAY(Im,gBestR); %Segmented Image
%
% %% Metric calculation
%
% window = 3;
% Imd = double(Im);
% Ioutd = double(Iout);
% psnr = PSNR(Im, Iout); %1
% SSIM = ssim(Im, Iout); %2
% FSIM = FeatureSIM(Im, Iout); %3
% UIQI = img_qi(Im, Iout, window); %4
% QILV = qilv(Im, Iout, [window, window]); %5
% HPSI = HaarPSI(Imd, Ioutd); %6
% metrics = [psnr, SSIM, FSIM, UIQI, QILV, HPSI];
%
% time = toc;
% %converH = converH';
% bsf_solution = sort(BThresholds);
% Rabbit_Location=bsf_solution;
%
% end
%
% % ___________________________________
% function o=Levy(d)
% beta=1.5;
% sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
% u=randn(1,d)*sigma;v=randn(1,d);step=u./abs(v).^(1/beta);
% o=step;
% end
%