clc
% clear all
close all
%% Importing Data From excel

FileName='C:\Users\h.safarzadeh\Desktop\matlab\DoE_FS_v4.4_WORK-HS - WIP_mod_NewFilt_KHigh_XArt_ACMT20.xlsx';
ProcessAnova=xlsread(FileName);
ProcessAnova=ProcessAnova(3:97,:);

AvgRNS=ProcessAnova(:,55); %Average Roundness
SRN=ProcessAnova(:,77); %Simulated roundness
% 


% Indipendent Input Values:
Gamma=ProcessAnova(:,7); %Gamma 20°,30°,40°
Inf=ProcessAnova(:,9); % Diam. Infeed/rev level 
AVWP=ProcessAnova(:,10); % workpiece angular velocity rpm
WPd=ProcessAnova(:,12); % workpiece diameter Ø5,Ø20,Ø60
Beta=ProcessAnova(:,22); % Beta angel
dll=ProcessAnova(:,35); %Grinding Wheel Dulness
nr=ProcessAnova(:,36); %N° OF TURNS

% Experiments results:
NLb=ProcessAnova(:,87); %N° OF lobs
AvgRNS=ProcessAnova(:,55); %Average Roundness
Output=normalize([AvgRNS,NLb]);
% WhiteBox output results:
SLb=ProcessAnova(:,87); %simulated N° OF lobs
DI=ProcessAnova(:,72); %Detach Index
PA=ProcessAnova(:,45); %PA values (stability indicator)
GR=PA.*nr; %Simulated Groth Rate = Growth Rate X Real Nrev
SRN=ProcessAnova(:,77); %Simulated roundness

n1=numel(AvgRNS);
C=2;
alpha = 1 ;
delta = 0.4 ;
for i=1:n1
        A=AvgRNS(i);
        S=SRN(i);
        AvgRNS(i)=A/((A^(alpha-delta))+C);
        SRN(i)=S/((S^(alpha-delta))+C);
end

%Black box & Grey box inputs:
BBin=[Gamma,WPd,Inf,AVWP,Beta,nr,dll];
GBin=[BBin,SRN,DI,NLb,GR];

BBinput=(normalize(BBin))'; %Black box input
GBinput=(normalize(GBin))'; %Grey box input

% Extracting 20% of data as test data to be tested by top 10 networks
rng(1)
QRL = size(BBinput, 2);
Q1 = floor(QRL * 0.20);
Q2 = QRL - Q1;
ind = randperm(QRL);
indtest = ind(1:Q1);
indtrain = ind(Q1 + (1:Q2));

BBintest=BBinput(:,indtest) ;
GBintest=GBinput(:,indtest);

xBB = BBintest(:,:);
xGB = GBintest(:,:); 

tBB = AvgRNS(indtest,:); 
tGB = AvgRNS(indtest,:); 

BBintrain=BBinput(:,indtrain) ;
GBintrain=GBinput(:,indtrain);

RNS=(AvgRNS(indtrain,:))';

%% White Box correlation
% figure, plotregression(AvgRNS,SRN,'FP MODEL')
% savefig('White Box correlation.fig')
% FPerr = immse(AvgRNS,SRN)

%% BB & GB
numNN = 100;  %Number trained networks

[BBm,~] = size(BBintrain);

BBnn=BBm*2-1;
BBnet = fitnet([BBnn BBnn],'trainbr');
BBnet.trainParam.epochs=2000;
BBnet.performFcn = 'mse';
BBnet.trainParam.goal=3e-3;	
% BBnet.performParam.regularization = 0.1;
BBnet.performParam.normalization = 'standard';
BBnet.trainParam.min_grad=0;
BBnet.divideFcn = '';
BBnet.trainParam.mu_dec=0.01;

BBnets2 = cell(1, numNN);
[GBm,~] = size(GBintrain);
GBnn=GBm*2-1;
GBnet = fitnet([GBnn GBnn],'trainbr');
GBnet.performFcn = 'mse';
GBnet.trainParam.goal=3e-3;	
% GBnet.performParam.regularization = 0.1;
GBnet.trainParam.min_grad=0;
GBnet.performParam.normalization = 'standard';
GBnet.divideFcn = '';
GBnet.trainParam.epochs=2000;
GBnet.trainParam.mu_dec=0.01;

BBperfs2 = zeros(1, numNN);
RBBM2 = zeros(1, numNN);
BBtesttot = 0;
BBalltot= 0;
BBtraintot = 0;
RBBMtot= 0;
BBperfstot= 0;
BBtestop= zeros(1, numNN);
BBnets2 = cell(1, numNN);

GBperfs2 = zeros(1, numNN);
RGBM2 = zeros(1, numNN);
GBtesttot = 0;
GBalltot= 0;
GBtraintot = 0;
RGBMtot= 0;
GBperfstot= 0;
GBtestop= zeros(1, numNN);
GBnets2 = cell(1, numNN);


for i = 1:numNN
rng(i)
QBB = size(BBintrain, 2);
Q1 = floor(QBB * 0.70);
Q2 = QBB - Q1;
ind = randperm(QBB);
ind1 = ind(1:Q1);
ind2 = ind(Q1 + (1:Q2));
x1BB= (BBintrain(:,ind1));
t1BB= (RNS(:,ind1));
x2BB= (BBintrain(:,ind2));
t2BB= (RNS(:,ind2));
x1GB= (GBintrain(:,ind1));
t1GB= (RNS(:,ind1));
x2GB= (GBintrain(:,ind2));
t2GB= (RNS(:,ind2));
fprintf('BBTraining %d/%d\n', i, numNN)
BBnets2{i}= train(BBnet, x1BB, t1BB);
fprintf('GBTraining %d/%d\n', i, numNN)
GBnets2{i} = train(GBnet, x1GB, t1GB);
  
BBneti = BBnets2{i};
BBtestop= BBneti(x2BB);
BBall =BBneti(BBintrain);
BBperfs2(i) = mse(BBneti, t2BB, BBtestop);
BBperfstot=BBperfstot+BBperfs2(i);
RBB= corrcoef(BBtestop,t2BB,'rows','complete');
RBBM2(i)=RBB(2,1);
RBBMtot=RBBMtot+RBBM2(i);
  
GBneti = GBnets2{i};
GBtestop = GBneti(x2GB);
GBall =GBneti(GBintrain);
GBtrainop =GBneti(x1GB);
GBperfs2(i) = mse(GBneti, t2GB, GBtestop);
GBperfstot=GBperfstot+GBperfs2(i);
RGB= corrcoef(GBtestop,t2GB,'rows','complete');
RGBM2(i)=RGB(2,1);
RGBMtot=RGBMtot+RGBM2(i);
GBtesttot = GBtesttot + GBtestop;
GBBalltot = GBalltot + GBall;
GBtraintot = GBtraintot + GBtrainop;
  
  
  
end
% Next, each network is tested on the second dataset with both individual performances and 
%the performance for the average output calculated.



BBperfsAve=BBperfstot /  numNN;
% RBBMaVE=RBBMtot/ numNN;
% RBBM2(isnan(RBBM2)) = [];
RBBMAve=mean((rmmissing(RBBM2)));

save('RGBM2')
save('RBBM2')
save('BBperfs2')
save('GBperfs2')
save('BBnets2')
save('GBnets2')

GBperfsAve=GBperfstot /  numNN;
% RGBMaVE=RGBMtot/ numNN;
% RGBM2(isnan(RGBM2)) = [];
RGBMAve=mean((rmmissing(RGBM2)));


%% Comparison between BB & GB
tbl= anova1([BBperfs2',GBperfs2']);
%P value of <0.05, you reject the null hypothesis that all the data are sampled from populations with the same mean. 
figure, boxplot([BBperfs2',GBperfs2'],'notch','on','label',{'NN ONLY 2','NN HYBRID 2'})
hold on
plot(1,BBperfsAve, 'k*')
plot(2,GBperfsAve, 'k*')
hold off

title('Perfomances Comparison')
ylabel('mse')
savefig('MSE NN 2.fig')

tbl= anova1([RBBM2',RGBM2']);
%P value of <0.05, you reject the null hypothesis that all the data are sampled from populations with the same mean. 
figure, boxplot([RBBM2',RGBM2'],'notch','on','label',{'NN ONLY 2','NN HYBRID 2'})
hold on
plot(1,RBBMAve, 'k*')
plot(2,RGBMAve, 'k*')
hold off
title('Regression Comparison')
ylabel('R')
savefig('Regression NN 2.fig')

%%
[~,idxBB] = sort((rmmissing(RBBM2)),'descend');
[~,idxGB] = sort((rmmissing(RGBM2)),'descend');

GBTest = 0;
BBTest = 0;

nn=10;  % Top nn° networks based on their R value
for i = 1:nn
    BBneti = BBnets2{idxBB(i)};
    BBTop = BBneti(xBB);
    BBTest = BBTest + BBTop;
    
    GBneti = GBnets2{idxGB(i)};
    GBTop = GBneti(xGB);
    GBTest = GBTest + GBTop;
end

GBTestAve = GBTest/nn;
BBTestAve = BBTest/nn;


figure, plotregression(tBB,BBTestAve,'NN 2 ONLY TEST')
savefig('NN 2 ONLY TEST.fig')
figure, plotregression(tGB,GBTestAve,'NN 2 HYBRID TEST')
savefig('NN 2 HYBRID TEST.fig')

