-
Notifications
You must be signed in to change notification settings - Fork 0
/
HHOSVMForREG.m
214 lines (163 loc) · 7.9 KB
/
HHOSVMForREG.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
%
% Harris hawks optimization Algorithm - SVR
%
function [cost,c,g,e,bestFitness] = HHOSVMForREG(Ytrain, Xtrain,Ytest,Xtest, Crange, gamma, epsilon, population, max_iteration)
%Parameters
pop = population;
max_iter = max_iteration;
part_dim = 3;
Ub = [Crange(2), gamma(2), epsilon(2)];
Lb = [Crange(1), gamma(1), epsilon(1)];
% initialize the location and Energy of the rabbit
Rabbit_Location=zeros(1,part_dim);
Rabbit_Energy=inf;
%preallocation of butterfly position and fitness
X = zeros(pop,part_dim); %pre_allocation of X butterfly position
fitness = zeros(pop,1); %pre_allocation of global fitness function (MSE) value
CNVG=zeros(1,max_iter);
%%%%% Initialize position and evaluate initial fitness
for i=1:pop
X(i,1) = Crange(1)+(Crange(2)-Crange(1)) * rand(1);
X(i,2) = gamma(1)+(gamma(2)-gamma(1)) * rand(1);
X(i,3) = epsilon(1)+(epsilon(2)-Crange(1)) * rand(1);
end
t= 0;
while t < max_iter %its ietration number
Iteration = t
for i=1:pop
% Check boundries
X(i,:) = max(X(i,:), Lb);
X(i,:) = min(X(i,:), Ub);
% fitness of locations
X(i,1) = Crange(1)+(Crange(2)-Crange(1)) * rand(1);
X(i,2) = gamma(1)+(gamma(2)-gamma(1)) * rand(1);
X(i,3) = epsilon(1)+(epsilon(2)-Crange(1)) * rand(1);
C = X(i,1);
gam = X(i,2);
epsil = X(i,3);
svmoptions = ['-s 3 -t 2 -c ', num2str(C),' -g ',num2str(gam),' -p ',num2str(epsil)];
model = svmtrain(Ytrain,Xtrain,svmoptions);
[predict_label, accuracy, prob_estimates] = svmpredict( Ytest, Xtest,model);
fitness(i) = accuracy(2);
% Update the location of Rabbit
if fitness(i)<Rabbit_Energy
Rabbit_Energy=fitness(i);
Rabbit_Location=X(i,:);
end
end
E1=2*(1-(t/max_iter)); % factor to show the decreaing energy of rabbit
% Update the location of Harris' hawks
for i=1:pop
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(pop*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-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,:));
% Check boundries
X1 = max(X1, Lb);
X1 = min(X1, Ub);
C = X1(1);
gam = X1(2);
epsil = X1(3);
svmoptions = ['-s 3 -t 2 -c ', num2str(C),' -g ',num2str(gam),' -p ',num2str(epsil)];
model = svmtrain(Ytrain,Xtrain,svmoptions);
[predict_label, accuracy, prob_estimates] = svmpredict( Ytest, Xtest,model);
X1fitness = accuracy(2);
if X1fitness < fitness(i) % 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,part_dim).*Levy(part_dim);
% Check boundries
X2= max(X2, Lb);
X2= min(X2, Ub);
C = X2(1);
gam = X2(2);
epsil = X2(3);
svmoptions = ['-s 3 -t 2 -c ', num2str(C),' -g ',num2str(gam),' -p ',num2str(epsil)];
model = svmtrain(Ytrain,Xtrain,svmoptions);
[predict_label, accuracy, prob_estimates] = svmpredict( Ytest, Xtest,model);
X2fitness = accuracy(2);
if (X2fitness<fitness(i)), % 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));
% Check boundries
X1 = max(X1, Lb);
X1 = min(X1, Ub);
C = X1(1);
gam = X1(2);
epsil = X1(3);
svmoptions = ['-s 3 -t 2 -c ', num2str(C),' -g ',num2str(gam),' -p ',num2str(epsil)];
model = svmtrain(Ytrain,Xtrain,svmoptions);
[predict_label, accuracy, prob_estimates] = svmpredict( Ytest, Xtest,model);
X1fitness = accuracy(2);
if (X1fitness<fitness(i)) % 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,part_dim).*Levy(part_dim);
% Check boundries
X2 = max(X2, Lb);
X2 = min(X2, Ub);
C = X2(1);
gam = X2(2);
epsil = X2(3);
svmoptions = ['-s 3 -t 2 -c ', num2str(C),' -g ',num2str(gam),' -p ',num2str(epsil)];
model = svmtrain(Ytrain,Xtrain,svmoptions);
[predict_label, accuracy, prob_estimates] = svmpredict( Ytest, Xtest,model);
X2fitness = accuracy(2);
if (X2fitness<fitness(i)), % improved move?
X(i,:)=X2;
end
end
end
%
end
end
t= t+1;
CNVG(t)=Rabbit_Energy;
end
c = Rabbit_Location(1);
g = Rabbit_Location(2);
e = Rabbit_Location(3);
bestFitness = Rabbit_Energy;
cost = model;
save
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