%In machine learning, support-vector machines (SVMs) are supervised 
%learning models with associated learning algorithms that analyze data used for classification and 
%regression analysis. Given a set of training examples, each marked as belonging to one or the other of two 
%categories, an SVM training algorithm builds a model that assigns new examples to one category or the other, 
%making it a non-probabilistic binary linear classifier (although methods such as Platt scaling exist to use SVM in a 
%probabilistic classification setting). An SVM model is a representation of the examples as points in space, mapped 
%so that the examples of the separate categories are divided by a clear gap that is as wide as possible. 
%New examples are then mapped into that same space and predicted to belong to a category based on the side of the gap 
%on which they fall.

clc
% clear all
close all
%% Importing Data From excel

FileName='C:\Users\h.safarzadeh\Desktop\matlab 2\DoE_FS_v4.4_WORK-HS - WIP_mod_NewFilt_KHigh_XArt_ACMT20.xlsx';
ProcessAnova=xlsread(FileName);
ProcessAnova=ProcessAnova(3:96,:);

AvgRNS=ProcessAnova(:,55); %Average Roundness
SRN=ProcessAnova(:,77); %Simulated roundness
% 


% Indipendent Input Values:
Gamma=normalize(ProcessAnova(:,7)); %Gamma 20°,30°,40°
Inf=normalize(ProcessAnova(:,9)); % Diam. Infeed/rev level 
AVWP=normalize(ProcessAnova(:,10)); % workpiece angular velocity rpm
WPd=normalize(ProcessAnova(:,12)); % workpiece diameter Ø5,Ø20,Ø60
Beta=ProcessAnova(:,22); % Beta angel
dll=normalize(ProcessAnova(:,35)); %Grinding Wheel Dulness
nr=normalize(ProcessAnova(:,36)); %N° OF TURNS

% Experiments results:
NLb=normalize(ProcessAnova(:,87)); %N° OF lobs
AvgRNS=(ProcessAnova(:,55)); %Average Roundness
Output=normalize([AvgRNS,NLb]);
% WhiteBox output results:
SLb=normalize(ProcessAnova(:,87)); %simulated N° OF lobs
DI=normalize(ProcessAnova(:,72)); %Detach Index
PA=normalize(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:
SVMin=table(Gamma,WPd,Inf,AVWP,Beta,nr,dll,AvgRNS);
HSVMin=table(Gamma,WPd,Inf,AVWP,Beta,nr,dll,SRN,DI,NLb,GR,AvgRNS);
varNames_only={'Gamma','WPd','Inf','AVWP','Beta','nr','dll','AvgRNS'};
varNames_hybrid={'Gamma','WPd','Inf','AVWP','Beta','nr','dll','SRN','DI','NLb','GR','AvgRNS'};
ARD_ONLY=table(Gamma,WPd,Inf,AVWP,Beta,nr,dll,AvgRNS,'VariableNames',varNames_only);
ARD_HYBRID=table(Gamma,WPd,Inf,AVWP,Beta,nr,dll,SRN,DI,NLb,GR,AvgRNS,'VariableNames',varNames_hybrid);
writetable(ARD_ONLY,'ARD_ONLY.csv')
writetable(ARD_HYBRID,'ARD_HYBRID.csv')




% Extracting 20% of data as test data to be tested by top 10 networks

rng(1)
QRL = size(SVMin, 1);
Q1 = floor(QRL * 0.20);
Q2 = QRL - Q1;
ind = randperm(QRL);
indtest = ind(1:Q1);
indtrain = ind(Q1 + (1:Q2));

SVMintest=SVMin(indtest,:) ;
HSVMintest=HSVMin(indtest,:) ;

xSVM = SVMintest(:,1:end-1);
xHSVM = HSVMintest(:,1:end-1); 

tSVM = SVMintest(:,end); 
tHSVM = SVMintest(:,end); 

SVMintrain=SVMin(indtrain,:) ;
HSVMintrain=HSVMin(indtrain,:) ;


%% White Box correlation
figure, plotregression(AvgRNS,SRN,'FP MODEL') 
FPerr = immse(AvgRNS,SRN)

%%
numNN = 100;

SVMperfs = zeros(1, numNN);
SVMtesttot = 0;
SVMalltot= 0;
SVMtraintot = 0;
SVML=0;
HSVML=0;

HSVMperfs = zeros(1, numNN);
HSVMtesttot = 0;
HSVMalltot= 0;
HSVMtraintot = 0;

rng(1)
for i = 1:numNN
rng(i)
QRL = size(SVMintrain, 1);
Q1 = floor(QRL * 0.70);
Q2 = QRL - Q1;
ind = randperm(QRL);
ind1 = ind(1:Q1);
ind2 = ind(Q1 + (1:Q2));
x1SVM = (SVMintrain(ind1,:));
x2SVM = (SVMintrain(ind2,:));

t1SVM = AvgRNS(ind1,:);
t2SVM = AvgRNS(ind2,:);

t1HSVM = AvgRNS(ind1,:);
t2HSVM = AvgRNS(ind2,:);

x1HSVM = (HSVMintrain(ind1,:));
x2HSVM = (HSVMintrain(ind2,:)); 

fprintf('RLTraining %d/%d\n', i, numNN);
SVMMdl{i} = fitrsvm(x1SVM,'AvgRNS','KernelFunction','polynomial','Standardize',true,'Epsilon',0.0005,'BoxConstraint',0.3);
%             'KernelScale','auto','Standardize',true);
%       'KernelScale','auto','Standardize',true,'OptimizeHyperparameters','auto');
SVML= resubLoss(SVMMdl{i}) ;

  
fprintf('HRLTraining %d/%d\n', i, numNN);
HSVMMdl{i} = fitrsvm(x1HSVM,'AvgRNS','KernelFunction','polynomial','Standardize',true,'Epsilon',0.0005,'BoxConstraint',0.3);
%       'KernelScale','auto','Standardize',true);;
%       'KernelScale','auto','Standardize',true,'OptimizeHyperparameters','auto');
      
HSVML= resubLoss(HSVMMdl{i}) ;
%   ,'OptimizeHyperparameters','auto',...
%     'HyperparameterOptimizationOptions',struct('AcquisitionFunctionName',...
%     'expected-improvement-plus'));

SVMi = SVMMdl{i};
SVMtestop = predict(SVMi,x2SVM);
SVMall =predict(SVMi,SVMin);
SVMtrainop =predict(SVMi,x1SVM);
SVMperfs(i) = loss(SVMi, x2SVM, t2SVM);
RSVM= corrcoef(SVMtestop,t2SVM,'rows','complete');
RSVMM(i)=RSVM(2,1);
SVMtesttot = SVMtesttot + SVMtestop;
SVMalltot = SVMalltot + SVMall;
SVMtraintot = SVMtraintot + SVMtrainop;
SVMLtot=SVML+SVML;

  
HSVMi = HSVMMdl{i};
HSVMtestop = predict(HSVMi,x2HSVM);
HSVMall =predict(HSVMi,HSVMin);
HSVMtrainop =predict(HSVMi,x1HSVM);
HSVMperfs(i) = loss(HSVMi, x2HSVM, t2HSVM);
RHSVM= corrcoef(HSVMtestop,t2HSVM,'rows','complete');
RHSVMM(i)=RHSVM(2,1);
HSVMLtot=HSVML+HSVML;

end



SVMtestAve = SVMtesttot / numNN;
SVMallAve = SVMalltot / numNN;
SVMtrainAve = SVMtraintot / numNN;
SVMLAve=SVMLtot/numNN;
HSVMLAve=HSVMLtot/numNN;


HSVMtestAve = HSVMtesttot / numNN;
HSVMallAve = HSVMalltot / numNN;
HSVMtrainAve = HSVMtraintot / numNN;

save('RSVMM')
save('RHSVMM')
save('SVMperfs')
save('HSVMperfs')
save('SVMMdl')
save('HSVMMdl')



RSVMMAve=mean(RSVMM);
RHSVMMAve=mean(RHSVMM);
SVMperfsAve=mean(SVMperfs);
HSVMperfsAve=mean(HSVMperfs);


[coeff,scoreTrain,~,~,explainedSVM,mu] = pca(table2array((x1SVM(:,[1:6]))));
[coeff,scoreTrain,~,~,explainedHSVM,mu] = pca(table2array((x1HSVM(:,[1:11]))));


%%
[~,idxSVR] = sort((rmmissing(RSVMM)),'descend');
[~,idxHSVR] = sort((rmmissing(RHSVMM)),'descend');

xSVM = SVMintest(:,1:end-1);
xHSVM = HSVMintest(:,1:end-1); 

tSVM = SVMintest(:,end); 
tHSVM = SVMintest(:,end); 

HSVMTest = 0;
SVMTest = 0;

nn=10;  % Top nn° networks based on their R value

for i = 1:nn
    SVMti = SVMMdl{idxSVR(i)};
    SVMTop = predict(SVMti,xSVM);
    SVMTest = SVMTest + SVMTop;
    
    HSVMti = HSVMMdl{idxHSVR(i)};
    HSVMTop = predict(HSVMti,xHSVM);
    HSVMTest = HSVMTest + HSVMTop;
end


HSVMTestAve = HSVMTest/10;
SVMTestAve = SVMTest/10;
tHSVM = table2array(tHSVM);
tSVM = table2array(tSVM);

figure, plotregression(tSVM,SVMTestAve,'SVR ONLY TEST')
savefig('SVR ONLY TEST.fig')
figure, plotregression(tHSVM,HSVMTestAve,'SVR HYBRID TEST')
savefig('SVR HYBRID TEST.fig')
