function [Bestfitness,variables]=HE
name=input('Please enter the function name(must enter):example @function name\n');
Size=input('Please enter population size you want(could be empty):default 100\n'); %setup population size i.e. 50 or 100
if isempty(Size)
Size=100;
end
G=input('Please enter generation size you want(could be empty):default 100\n'); %setup generation i.e. 100,200,...,1000
if isempty(G)
G=100;
end
Codel=input('Please enter Code length you want(could be empty):default 12\n'); %setup code length i.e. 20,32
if isempty(Codel)
Codel=12;
end
umax=input('please enter upper bound of variable you want(could be empty):default 10^6\n'); %setup up bound i.e. 1000000
if isempty(umax)
umax=10^6;
end
umin=input('please enter lower bound of variable you want(could be empty):default -10^6\n'); %setup low bound i.e. 1000000
if isempty(umin)
umin=-10^6;
end
n=input('Please enter number of variables(must enter):example 2\n'); %setup numbers of variables i.e. if your equation with 2 variables then
%input with 2.
mm=cell(1,n);
mmb=cell(1,n);
Q=cell(1,5); % setup 5 closed cell for store Q{1,1};Q{1,2};Q{1,3};Q{1,4};Q{1,5} five matrix, every rows in matrix
%standard as indiviudal solutions of all variables.
Q{1,1}=round(4*rand(Size,n*Codel));% Generate a random matrix with numbers 1's,2's,3's,4's,5's.
EX=round(4*rand(Size,n*Codel));
for k=1:1:G % start to looping Generations
time(k)=k; % counting looping times
% Generate a random matrix have same size of Q{1,1}, and adding it at begining
%of program looping.
for i=1:1:Size % from matrix Q{1,1} to generate Q{1,2},Q{1,3},Q{1,4},Q{1,5}
for j=1:1:n*Codel
if Q{1,1}(i,j)==0
Q{1,2}(i,j)=1;Q{1,3}(i,j)=2;Q{1,4}(i,j)=3;Q{1,5}(i,j)=4;
elseif Q{1,1}(i,j)==1
Q{1,2}(i,j)=2;Q{1,3}(i,j)=3;Q{1,4}(i,j)=4;Q{1,5}(i,j)=0;
elseif Q{1,1}(i,j)==2
Q{1,2}(i,j)=3;Q{1,3}(i,j)=4;Q{1,4}(i,j)=0;Q{1,5}(i,j)=1;
elseif Q{1,1}(i,j)==3
Q{1,2}(i,j)=4;Q{1,3}(i,j)=0;Q{1,4}(i,j)=1;Q{1,5}(i,j)=2;
elseif Q{1,1}(i,j)==4
Q{1,2}(i,j)=0;Q{1,3}(i,j)=1;Q{1,4}(i,j)=2;Q{1,5}(i,j)=3;
end
if EX(i,j)==0
Q{1,6}(i,j)=1;Q{1,7}(i,j)=2;Q{1,8}(i,j)=3;Q{1,9}(i,j)=4;
elseif EX(i,j)==1
Q{1,6}(i,j)=2;Q{1,7}(i,j)=3;Q{1,8}(i,j)=4;Q{1,9}(i,j)=0;
elseif EX(i,j)==2
Q{1,6}(i,j)=3;Q{1,7}(i,j)=4;Q{1,8}(i,j)=0;Q{1,9}(i,j)=1;
elseif EX(i,j)==3
Q{1,6}(i,j)=4;Q{1,7}(i,j)=0;Q{1,8}(i,j)=1;Q{1,9}(i,j)=2;
elseif EX(i,j)==4
Q{1,6}(i,j)=0;Q{1,7}(i,j)=1;Q{1,8}(i,j)=2;Q{1,9}(i,j)=3;
end
end
end
E=[Q{1,1};Q{1,2};Q{1,3};Q{1,4};Q{1,5};Q{1,6};Q{1,7};Q{1,8};Q{1,9};EX];% Let E equal to combination of 6 Matrix.
for s=1:1:10*Size % Evaluate the objective function value and ranking from best individual to worst one.
m=E(s,:);
for v=1:1:n
y(v)=0;
mm{1,v}=m(Codel*(v-1)+1:1:v*Codel);
for i=1:1:Codel
y(v)=y(v)+mm{1,v}(i)*5^(Codel-i);
end
x(v)=(umax-umin)*y(v)/(5^Codel)+umin;
r(1,v)=x(v);
end
F(s)=name(r);
end
Ji=1./F;% setp up inverse measure for plot image using
BestJ(k)=max(Ji);
fi=F;
[Oderfi,Indexfi]=sort(fi);
[Oderfi1,Indexfi1]=sort(fi,'descend');
Bestfitness=Oderfi(1);
for i=1:1:Size
TempE(i,:)=E(Indexfi(i),:);
TempE1(i,:)=E(Indexfi1(i),:);
end
BestS=TempE(1,:);
bfi(k)=Bestfitness;
BS(k,:)=BestS;
for i=1:1:Size-4
for j=1:1:n*Codel
if TempE(i,j)==TempE(i+1,j) && TempE(i,j)~=4
TempE(i+1,j)=TempE(i+1,j)+1;
elseif TempE(i,j)==TempE(i+1,j) && TempE(i,j)==4
TempE(i+1,j)=0;
elseif TempE(i,j)==TempE(i+2,j) && TempE(i,j)~=4
TempE(i+2,j)=TempE(i+2,j)+1;
elseif TempE(i,j)==TempE(i+2,j) && TempE(i,j)==4
TempE(i+2,j)=0;
elseif TempE(i,j)==TempE(i+3,j) && TempE(i,j)~=4
TempE(i+3,j)=TempE(i+3,j)+1;
elseif TempE(i,j)==TempE(i+3,j) && TempE(i,j)==4
TempE(i+3,j)=0;
elseif TempE(i,j)==TempE(i+4,j) && TempE(i,j)~=4
TempE(i+4,j)=TempE(i+4,j)+1;
elseif TempE(i,j)==TempE(i+4,j) && TempE(i,j)==4
TempE(i+4,j)=0;
end
if TempE1(i,j)==TempE1(i+1,j) && TempE1(i,j)~=4
TempE1(i+1,j)=TempE1(i+1,j)+1;
elseif TempE1(i,j)==TempE1(i+1,j) && TempE1(i,j)==4
TempE1(i+1,j)=0;
elseif TempE1(i,j)==TempE1(i+2,j) && TempE1(i,j)~=4
TempE1(i+2,j)=TempE1(i+2,j)+1;
elseif TempE1(i,j)==TempE1(i+2,j) && TempE1(i,j)==4
TempE1(i+1,j)=0;
elseif TempE1(i,j)==TempE1(i+3,j) && TempE1(i,j)~=4
TempE1(i+3,j)=TempE1(i+3,j)+1;
elseif TempE1(i,j)==TempE1(i+3,j) && TempE1(i,j)==4
TempE1(i+3,j)=0;
elseif TempE1(i,j)==TempE1(i+4,j) && TempE1(i,j)~=4
TempE1(i+4,j)=TempE1(i+4,j)+1;
elseif TempE1(i,j)==TempE1(i+4,j) && TempE1(i,j)==4
TempE1(i+4,j)=0;
end
end
end
for i=1:1:Size-1
for j=1:1:n*Codel
if TempE1(1,j)==TempE(i+1,j)
TempE(i+1,j)=TempE(1,j);
elseif TempE1(2,j)==TempE(i+1,j)
TempE(i+1,j)=TempE(2,j);
elseif TempE1(3,j)==TempE(i+1,j)
TempE(i+1,j)=TempE(3,j);
elseif TempE1(4,j)==TempE(i+1,j)
TempE(i+1,j)=TempE(4,j);
end
if TempE(1,j)==TempE1(i+1,j)
TempE1(i+1,j)=TempE1(1,j);
elseif TempE(2,j)==TempE1(i+1,j)
TempE1(i+1,j)=TempE1(2,j);
elseif TempE(3,j)==TempE1(i+1,j)
TempE1(i+1,j)=TempE1(3,j);
elseif TempE(4,j)==TempE1(i+1,j)
TempE1(i+1,j)=TempE1(4,j);
end
end
end
Q{1,1}=TempE;
EX=TempE1;
for v=1:1:n
yb(v)=0;
mmb{1,v}=BestS(Codel*(v-1)+1:1:v*Codel);
for i=1:1:Codel
yb(v)=yb(v)+mmb{1,v}(i)*5^(Codel-i);
end
variables(v)=(umax-umin)*yb(v)/(5^Codel)+umin;
end
figure(1);
subplot(2,1,1);
set(gcf,'CurrentAxes',gca)
plot(time,BestJ);
xlabel('Times');ylabel('BestJ');
subplot(2,1,2);
set(gcf,'CurrentAxes',gca)
plot(time,bfi)
xlabel('Times');ylabel('Best optimization value');
end