% HOW TO USE HBF STRATEGY WITH OTHER ALGORITHMS

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% select the parameters 
 it1=0;
T_h=100;
i=1;
% in each iteration i=i+1

 it=i-it1*T_h;

%_________This section has great effects on population diversity___________
%___________________Add lines /16-39/ after main loop______________________
 if mod(it,T_h)==0

% select some variables
%______________________________
 loc=randperm(n_var);
 loc1=loc(1:round(n_var*rand-1)+1);
%______________________________


% select a random population
%______________________________
 Xs=randperm(n_swarm);
%______________________________

% change the selected variables and replace the old population with new one
%______________________________
 pop(Xs(1),loc1)=rand(1,length(loc1)).*(max_var(loc1)-min_var(loc1))+min_var(loc1);
 [Ccost(1)]= feval(fname,pop(Xs(1),1:n_var),func_num);
 EFs=EFs+1;
 p_best(Xs(1),:)=[pop(Xs(1),:),Ccost(1)];
%______________________________

 it1=it1+1;
 end



%_____________________________ HBF strategy________________________________

vx=randn(1,n_var); 
beta=0.5+rand;

stepsize=.00001*(it/T_h)*1.*((1./abs(vx)).^(1/beta)).*sign(vx); % change it with any random step size

Xs=p_best(j,1:n_var)+(stepsize).*exp((it/10)); % term exp((it/10)) can be replaced by any adjustment coefficient
Xa=p_best(j,1:n_var)+1*(stepsize).*(1-it/T_h).*(max_var-min_var); % p_best can be replaced by gbest in other algorithms

if j<n_swarm
x=g_best(1,1:n_var);   %%%  Select g_best(1,1:n_param) or p_best(j,1:n_param) or select between population and these vectors randomly
else
x=pop(j,1:n_var);
end

A=0;  % greater value for "A" increase the chance for selecting from  Xs and Xa

h0=exp(2-2*it/T_h);        
H=abs(2*rand(1,numel(x))*h0-h0);
rr=rand(1,numel(x));   
cc=rand(1,numel(x));
xx=find(rr<=A+cc*it/T_h) ;
k1=zeros(1,n_var);
k2=zeros(1,n_var);

%  This section can be implemented in a loop for each decision variable %%%

% for i1=1:numel(xx)
% if H>.3*rand
% k1(xx(i1))=1;
% else
% k2(xx(i1))=1; 
% end
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if H>.3*rand
k1(xx)=1;
else
k2(xx)=1; 
end

xx=find(rr>A+cc*it/T_h) ;% hunting strategies condition
k3=zeros(1,n_var);
k3(xx)=1;
z=Xs.*k1+Xa.*k2+k3.*x;

loc=find(z-max_var>0);
z(loc)=max_var(loc);
loc=find(z-min_var<0);
z(loc)=min_var(loc);

zcost=feval(fname,z,func_num);
EFs=EFs+1;

if zcost<  p_best(j,n_var+1)
p_best(j,:)=[z,zcost]; % Can be replaced in p_best or target pop (i.e., x)
end