tic
clc;
clear all;
close all;
D=2;
V=[];
functional_values=[];
functional_values1=[];


 % % Initialization
no_of_fam=40;
r_s=0.5;
r_p=0.9;
r_k=0.2;

% % Parent vectors P1 and P2 and kid vectors K1 and K2 in the range w
w1=-100:5:100;
w2=-100:5:100;

% % w1=-4.5:0.45:4.5;
% % w2=-4.5:0.45:4.5;
% % w1=-15:1.5:15;
% % w2=-15:1.5:15;
% % w1= -10:0.5:0;
% % w2=-6.5:0.32:0;
% % w1=-15:0.5:-5;
% % w2=-3:0.3:3;

% % w1=-10:1:10;
% % w2=-10:1:10;

for epoch=1
% %     :10
w=[w1; w2]; 
% % w3; w4; w5; w6; w7; w8; w9];
for v=1:2
for u=1:no_of_fam 
    P1{v,u}=[w(v,u)+(w(v,u+1)-w(v,u)).*rand(1,1)];
    P2{v,u}=[w(v,u)+(w(v,u+1)-w(v,u)).*rand(1,1)];
    K1{v,u}=[w(v,u)+(w(v,u+1)-w(v,u)).*rand(1,1)];
    K2{v,u}=[w(v,u)+(w(v,u+1)-w(v,u)).*rand(1,1)];
end
end

for i=1:no_of_fam    
    f{i,1}=[P1{1,i}(1) w(1,i) w(1,i+1); P1{2,i}(1) w(2,i) w(2,i+1)];
    f{i,2}=[P2{1,i}(1) w(1,i) w(1,i+1); P2{2,i}(1) w(2,i) w(2,i+1)];
    f{i,3}=[K1{1,i}(1) w(1,i) w(1,i+1); K1{2,i}(1) w(2,i) w(2,i+1)];
    f{i,4}=[K2{1,i}(1) w(1,i) w(1,i+1); K2{2,i}(1) w(2,i) w(2,i+1)];
end
initial_f=f;


for iter=1:100
% % Objective function computation
for i=1:no_of_fam
    for j=1:4
        for t=1:D
            s{i,j}(t)=f{i,j}(t,1);
        end
% %         s1=f{i,j}(1,1);
% %         s2=f{i,j}(2,1);
        S(i,j)=ackley(s{i,j});
    end
end

% % Global Best Parent
S1=S(:,1:2);
[k,l]=min(S1(:));
V(iter)=k;
[row,col] = ind2sub(size(S(:,1:2)),l);
BEST_SOLUTION{iter}=f{row,col};


% % Parent follow behaviour
% % Roulette Wheel Simulation
SR(:,1:2)=1./S(:,1:2);
SR1(1:no_of_fam)=SR(:,1);
SR1((no_of_fam+1):(2*no_of_fam))=SR(:,2);
SR1_n=SR1./sum(SR1);
C_SR_n=cumsum(SR1_n);
for i=1:no_of_fam
for j=1:2            
if S(i,j)>k
r=rand;
[M1,N1]=find(C_SR_n>r);
p_chosen=N1(1);
if N1(1)<=no_of_fam
q=1;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,p_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
else
p_chosen=N1(1)-no_of_fam;
q=2;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,p_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)]; 
end
end       
end
end
end



% % Kid follow behaviour
% % Peer roulette wheel
k_SR(:,1:2)=1./S(:,3:4);
k_SR1(1:no_of_fam)=k_SR(:,1);
k_SR1((no_of_fam+1):(2*no_of_fam))=k_SR(:,2);
k_SR1_n=k_SR1./sum(k_SR1);
k_C_SR_n=cumsum(k_SR1_n);

% % Parent roulette wheel
p_SR(:,1:2)=1./S(:,1:2);
p_SR1(1:no_of_fam)=p_SR(:,1);
p_SR1((no_of_fam+1):(2*no_of_fam))=p_SR(:,2);
p_SR1_n=p_SR1./sum(p_SR1);
p_C_SR_n=cumsum(p_SR1_n);


for i=1:no_of_fam
for j=3:4
% % Global best family
if i==row
r1=rand;
if r1<r_p
r_p=r_p/2;
pk1=1/S(row,1);
pk2=1/S(row,2);
pk=[pk1 pk2];
pk_n=pk./sum(pk);
C_pk_n=cumsum(pk_n);
r2=rand;
[M2,N2]=find(C_pk_n>r2);
k_chosen=N2(1);
if N2(1)<no_of_fam
q=1;
% % chooses one of the parents
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,k_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
else
k_chosen=N2(1)-no_of_fam;
q=2;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,k_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
end
elseif j==3
k_chosen=row;
q=4;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,k_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
else 
k_chosen=row;
q=4;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,k_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
end
else
r3=rand;
if r3<r_k
r_k=r_k/2;
[M3,N3]=find(k_C_SR_n>r3);
kk_chosen=N3(1);
if N3(1)<=no_of_fam
q=3;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,kk_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
else
kk_chosen=N3(1)-no_of_fam;
q=4;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,kk_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
end
else
r4=rand;
[M,N]=find(p_C_SR_n>r4);
pk_chosen=N(1);
if N(1)<=no_of_fam
q=1;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,pk_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
else
pk_chosen=N(1)-no_of_fam;
q=2;
% % Interval Updation
for t=1:D
[x_n1(t),x_n2(t)]=interval_update1(f,r_s,pk_chosen,t,q);
f{i,j}(t,:)=[x_n1(t)+(x_n2(t)-x_n1(t)).*rand(1,1) x_n1(t) x_n2(t)];
end
end
end
end


end
end
for i=1:no_of_fam
for j=1:4
population{i,j}(:,1)=f{i,j}(1,1);
population{i,j}(:,2)=f{i,j}(2,1);
end
end
POP{iter}=population;
for i=1:no_of_fam
    for j=1:4
        for t=1:D
            s{i,j}(t)=population{i,j}(1,t);
        end
    SOLUTION_POP(i,j)=ackley(s{i,j});
    end
end
SOLUTION{iter}=SOLUTION_POP;

end


% % Best solution
% % Objective function computation
for i=1:no_of_fam
    for j=1:4
        for t=1:D
            s{i,j}(t)=f{i,j}(t,1);
        end
    S(i,j)=ackley(s{i,j});
    end
end

% % Global Best Parent
S1=S(:,1:4);
[k,l]=min(S1(:));
[row,col] = ind2sub(size(S(:,1:2)),l);
for v=1:D
solution_s(v)=f{row,col}(v,1);
end
area(epoch,:)=solution_s;

% % Convergence graph
for h=1:length(V)
GV(h)=V(h);
GV1(h)=1/V(h);
end
functional_values=[functional_values; GV];
functional_values1=[functional_values1; GV1];
% % figure;plot(GV);
[z1(epoch),z2(epoch)]=min(GV);

end

[X,Y]=min(z1);
z2=1/z1(Y);
Best_solu=Y

figure;
% % b=1:500;
for k=1:epoch
    plot(functional_values(k,:));
    hold on
end


% % figure;
% % for i=1:20
% % for j=1:4
% % plot(POP{1,1}(i,j));
% % hold on
% % end
% % end
















