
clc, clear all,close all;

z=1; %lixo
HL =1; %HL neurons
perf=1; % goal 
%rng(0)
MM=1;
while(perf > 0.001)
    
%% Load data:
%Teflon FREQ 0.6 step 0.06
HL=HL+1;
for(x=1:1:2)
    if(x==2)
        MM=2;
    end
load('T661.mat');

T_aux=UU1;
T66= T_aux(:,:,MM,:);
T66=reshape(T66,[100,174]);

%Teflon FREQ 0.6 step 0.04
load('T64.mat');

T_aux2=UU2;
T64= T_aux2(:,:,MM,:);
T64=reshape(T64,[100,396]); 

%Teflon FREQ 0.6 step 0.02
load('T62.mat');

T_aux3=UU3;
T62= T_aux3(:,:,MM,:);
T62=reshape(T62,[100,156]);

%Teflon FREQ 0.4 step 0.06
load('T46.mat');

T_aux5=UU4;
T46= T_aux5(:,:,MM,:);
T46=reshape(T46,[100,225]); 

%Teflon FREQ 0.4 step 0.04

load('T44.mat');

T_aux5=UU5;
T44= T_aux5(:,:,MM,:);
T44=reshape(T44,[100,330]); 

%Teflon FREQ 0.4 step 0.02

load('T42.mat');

T_aux5=UU6;
T42= T_aux5(:,:,MM,:);
T42=reshape(T42,[100,310]); 

%Teflon FREQ 0.2 step 0.06

load('T26.mat');

T_aux5=UU7;
T26= T_aux5(:,:,MM,:);
T26=reshape(T26,[100,196]); 

%Teflon FREQ 0.2 step 0.04

load('T24.mat');

T_aux5=UU8;
T24= T_aux5(:,:,MM,:);
T24=reshape(T24,[100,163]);

%Teflon FREQ 0.2 step 0.02

load('T22.mat');

T_aux5=UU9;
T22= T_aux5(:,:,MM,:);
T22=reshape(T22,[100,280]); 

%ALUM FREQ 0.6 step 0.06

load('A66.mat');

T_aux5=UU19;
A66= T_aux5(:,:,MM,:);
A66=reshape(A66,[100,104]); 

%ALUM FREQ 0.6 step 0.04

load('A64.mat');

T_aux5=UU20;
A64= T_aux5(:,:,MM,:);
A64=reshape(A64,[100,86]); 


%ALUM FREQ 0.6 step 0.02

load('A62.mat');

T_aux5=UU21;
A62= T_aux5(:,:,MM,:);
A62=reshape(A62,[100,124]); 

%ALUM FREQ 0.4 step 0.06

load('A46.mat');

T_aux5=UU22;
A46= T_aux5(:,:,MM,:);
A46=reshape(A46,[100,74]);

load('A461.mat');

T_aux5=UU221;
A461= T_aux5(:,:,MM,:);
A461=reshape(A461,[100,41]);

A46 = [ A46 A461]; 

%ALUM FREQ 0.4 step 0.04

load('A44.mat');

T_aux5=UU23;
A44= T_aux5(:,:,MM,:);
A44=reshape(A44,[100,135]); 

%ALUM FREQ 0.4 step 0.02

load('A42.mat');

T_aux5=UU24;
A42= T_aux5(:,:,MM,:);
A42=reshape(A42,[100,129]); 

%ALUM FREQ 0.2 step 0.06

load('A26.mat');

T_aux5=UU25;
A26= T_aux5(:,:,MM,:);
A26=reshape(A26,[100,60]);

load('A261.mat');

T_aux5=UU251;
A261= T_aux5(:,:,MM,:);
A261=reshape(A261,[100,29]);

A26= [A26 A261]; 

%ALUM FREQ 0.2 step 0.04

load('A24.mat');

T_aux5=UU26;
A24= T_aux5(:,:,MM,:);
A24=reshape(A24,[100,123]); 

%ALUM FREQ 0.2 step 0.02

load('A22.mat');

T_aux5=UU27;
A22= T_aux5(:,:,MM,:);
A22=reshape(A22,[100,140]); 

%Acryl FREQ 0.6 step 0.06

load('AC66.mat');

T_aux5=UU10;
AC66= T_aux5(:,:,MM,:);
AC66=reshape(AC66,[100,173]); 

%Acryl FREQ 0.6 step 0.04

load('AC64.mat');

T_aux5=UU11;
AC64= T_aux5(:,:,MM,:);
AC64=reshape(AC64,[100,134]); 


%Acryl FREQ 0.6 step 0.02

load('AC62.mat');

T_aux5=UU12;
AC62= T_aux5(:,:,MM,:);
AC62=reshape(AC62,[100,219]); 

%Acryl FREQ 0.4 step 0.06

load('AC46.mat');

T_aux5=UU13;
AC46= T_aux5(:,:,MM,:);
AC46=reshape(AC46,[100,74]);

load('AC461.mat');

T_aux5=UU131;
AC461= T_aux5(:,:,MM,:);
AC461=reshape(AC461,[100,60]);

AC46= [AC46 AC461]; 


%Acryl FREQ 0.4 step 0.04

load('AC44.mat');

T_aux5=UU14;
AC44= T_aux5(:,:,MM,:);
AC44=reshape(AC44,[100,85]);

load('AC441.mat');

T_aux5=UU141;
AC441= T_aux5(:,:,MM,:);
AC441=reshape(AC441,[100,61]);

AC44 = [AC44 AC441]; 

%Acryl FREQ 0.4 step 0.02

load('AC42.mat');

T_aux5=UU15;
AC42= T_aux5(:,:,MM,:);
AC42=reshape(AC42,[100,158]); 

%Acryl FREQ 0.2 step 0.06

load('AC26.mat');

T_aux5=UU16;
AC26= T_aux5(:,:,MM,:);
AC26=reshape(AC26,[100,115]); 

%Acryl FREQ 0.2 step 0.04

load('AC24.mat');

T_aux5=UU17;
AC24= T_aux5(:,:,MM,:);
AC24=reshape(AC24,[100,138]); 

%Acryl FREQ 0.2 step 0.02

load('AC22.mat');

T_aux5=UU18;
AC22= T_aux5(:,:,MM,:);
AC22=reshape(AC22,[100,166]); 


%GREEN FREQ 0.6 step 0.06

load('G66.mat');

T_aux5=UU28;
G66= T_aux5(:,:,MM,:);
G66=reshape(G66,[100,113]); 

%GREEN FREQ 0.6 step 0.04

load('G64.mat');

T_aux5=UU29;
G64= T_aux5(:,:,MM,:);
G64=reshape(G64,[100,125]); 

%GREEN FREQ 0.6 step 0.02

load('G62.mat');

T_aux5=UU30;
G62= T_aux5(:,:,MM,:);
G62=reshape(G62,[100,154]); 

%GREEN FREQ 0.4 step 0.06

load('G46.mat');

T_aux5=UU31;
G46= T_aux5(:,:,MM,:);
G46=reshape(G46,[100,123]); 

%GREEN FREQ 0.4 step 0.04

load('G44.mat');

T_aux5=UU32;
G44= T_aux5(:,:,MM,:);
G44=reshape(G44,[100,133]); 


%GREEN FREQ 0.4 step 0.02

load('G42.mat');

T_aux5=UU33;
G42= T_aux5(:,:,MM,:);
G42=reshape(G42,[100,160]); 


%GREEN FREQ 0.2 step 0.06

load('G26.mat');

T_aux5=UU34;
G26= T_aux5(:,:,MM,:);
G26=reshape(G26,[100,128]); 

%GREEN FREQ 0.2 step 0.04

load('G24.mat');

T_aux5=UU35;
G24= T_aux5(:,:,MM,:);
G24=reshape(G24,[100,66]);

load('G241.mat');

T_aux5=UU351;
G241= T_aux5(:,:,MM,:);
G241=reshape(G241,[100,53]);

G24 = [G24 G241]; 


%GREEN FREQ 0.2 step 0.02

load('G22.mat');

T_aux5=UU36;
G22= T_aux5(:,:,MM,:);
G22=reshape(G22,[100,139]); 

%% ASSEMBLY 

%% TEFLON
aux1=randperm(174,80);
aux2=randperm(396,80);
aux3=randperm(156,80);
aux4=randperm(225,80);
aux5=randperm(330,80);
aux6=randperm(310,80);
aux7=randperm(196,80);
aux8=randperm(163,80);
aux9=randperm(280,80);

i=1;
for i=1:1:80
    T1(:,i)=T66(:,aux1(i));   
    T2(:,i)=T64(:,aux2(i)); 
    T3(:,i)=T62(:,aux3(i)); 
    T4(:,i)=T46(:,aux4(i)); 
    T5(:,i)=T44(:,aux5(i)); 
    T6(:,i)=T42(:,aux6(i)); 
    T7(:,i)=T26(:,aux7(i)); 
    T8(:,i)=T24(:,aux8(i)); 
    T9(:,i)=T22(:,aux9(i)); 
end

T= [T1 T2 T3 T4 T5 T6 T7 T8 T9];

%% ALUM
aux1=randperm(104,80);
aux2=randperm(86,80);
aux3=randperm(124,80);
aux4=randperm((74+41),80);
aux5=randperm(135,80);
aux6=randperm(129,80);
aux7=randperm(89,80);
aux8=randperm(123,80);
aux9=randperm(140,80);

i=1;
for i=1:1:80
    A1(:,i)=A66(:,aux1(i));   
    A2(:,i)=A64(:,aux2(i)); 
    A3(:,i)=A62(:,aux3(i)); 
    A4(:,i)=A46(:,aux4(i)); 
    A5(:,i)=A44(:,aux5(i)); 
    A6(:,i)=A42(:,aux6(i)); 
    A7(:,i)=A26(:,aux7(i)); 
    A8(:,i)=A24(:,aux8(i)); 
    A9(:,i)=A22(:,aux9(i)); 
end

A= [A1 A2 A3 A4 A5 A6 A7 A8 A9];

%% Acryl
aux1=randperm(173,80);
aux2=randperm(134,80);
aux3=randperm(219,80);
aux4=randperm((74+60),80);
aux5=randperm((85+61),80);
aux6=randperm(158,80);
aux7=randperm(115,80);
aux8=randperm(138,80);
aux9=randperm(166,80);

i=1;
for i=1:1:80
    AC1(:,i)=AC66(:,aux1(i));   
    AC2(:,i)=AC64(:,aux2(i)); 
    AC3(:,i)=AC62(:,aux3(i)); 
    AC4(:,i)=AC46(:,aux4(i)); 
    AC5(:,i)=AC44(:,aux5(i)); 
    AC6(:,i)=AC42(:,aux6(i)); 
    AC7(:,i)=AC26(:,aux7(i)); 
    AC8(:,i)=AC24(:,aux8(i)); 
    AC9(:,i)=AC22(:,aux9(i)); 
end

AC= [AC1 AC2 AC3 AC4 AC5 AC6 AC7 AC8 AC9];


%% GREEN
aux1=randperm(113,80);
aux2=randperm(125,80);
aux3=randperm(154,80);
aux4=randperm(123,80);
aux5=randperm(133,80);
aux6=randperm(160,80);
aux7=randperm(128,80);
aux8=randperm((66+53),80);
aux9=randperm(139,80);

i=1;
for i=1:1:80
    G1(:,i)=G66(:,aux1(i));   
    G2(:,i)=G64(:,aux2(i)); 
    G3(:,i)=G62(:,aux3(i)); 
    G4(:,i)=G46(:,aux4(i)); 
    G5(:,i)=G44(:,aux5(i)); 
    G6(:,i)=G42(:,aux6(i)); 
    G7(:,i)=G26(:,aux7(i)); 
    G8(:,i)=G24(:,aux8(i)); 
    G9(:,i)=G22(:,aux9(i)); 
end

G= [G1 G2 G3 G4 G5 G6 G7 G8 G9];

%% Inputs
if(x==1)
Input= [ T AC A G];
end
if(x==2)
Input2= [ T AC A G];    
end
end

Inputt=[Input(:,1:720) Input(:,721:1440)  Input(:,1441:2160) Input(:,2161:2880)
        Input2(:,1:720)  Input2(:,721:1440) Input2(:,1441:2160) Input2(:,2161:2880)];

%% Targets
Targets= [ones(1,720) zeros(1,2160)
        zeros(1,720) ones(1,720) zeros(1,1440) 
        zeros(1,1440) ones(1,720) zeros(1,720) 
        zeros(1,2160) ones(1,720)];
   
     
%% Create a Pattern Recognition Network
net = patternnet([HL], 'trainlm'); % layers
net.divideParam.trainRatio = 0.5;
net.divideParam.valRatio = 0.25;
net.divideParam.testRatio = 0.25;
net.trainParam.min_grad = 1e-15;
net.trainParam.max_fail = 10;
net.trainparam.epochs=1000;
net.trainparam.goal=0;
%net.trainparam.lr=0.1;

%% Train the Network
% Train the Network
[net,tr] = train(net,Inputt,Targets);

%% Test the network
Output=net(Inputt);
errors = gsubtract(Targets,Output);
perf= perform(net,Targets,Output)


%% Recalculate Training, Validation and Test Performance
trainTargets = Targets .* tr.trainMask{1};
valTargets = Targets  .* tr.valMask{1};
testTargets = Targets  .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,Output)
valPerformance = perform(net,valTargets,Output)
testPerformance = perform(net,testTargets,Output)
%%TOTAL DATA ASSEMBLY%%%


%% Other test

%Input_test= [ T66 T64 T62 T46 T44 T42 T26 T24 T22 AC66 AC64 AC62 AC46 AC44 AC42 AC26 AC24 AC22 A66 A64 A62 A46 A44 A42 A26 A24 A22 G66 G64 G62 G46 G44 G42 G26 G24 G22];

%Output_test= [ones(1,2976) zeros(1,3720)
  %  zeros(1,2976) ones(1,1391) zeros(1,2329)
  %  zeros(1,2976+1391) ones(1,1158) zeros(1,1171)
  %  zeros(1,2976+1391+1158) ones(1,1171)];

%Outp=net(Input_test);
%Outp=round(Outp);
%nn=sum(Outp==Output_test);
%nn=sum(nn==4)
%new_perf=(nn/(6696))*100
 
end


