clc;
clear all
close all;
%Dimensionality reduction using LDA before subjected to ANN]
load TRAINLPC
s=[];
for i=1:1:3
    s=[s size(TRAINLPC{i},1)];
end
x=min(s);
DATA=[];
for i=1:1:3
    DATA=[DATA TRAINLPC{i}(1:1:x,:)];
end
U=isnan(DATA);
[R,S]=find(U==1);
for i=1:1:length(R)
    DATA(R(i),S(i))=eps;
end

%M4=mean(DATA(:,16:1:20)');
%M5=mean(DATA(:,21:1:25)');
%M6=mean(DATA(:,26:1:30)');
%M7=mean(DATA(:,31:1:35)');
%M8=mean(DATA(:,36:1:40)');
%M9=mean(DATA(:,41:1:45)');
%M10=mean(DATA(:,46:1:50)');
%SB=cov([M1' M2' M3' M4' M5' M6' M7' M8' M9' M10']');

%SW=cov(DATA(:,1:1:5)',1)+cov(DATA(:,6:1:10)',1)+cov(DATA(:,11:1:15)',1)+cov(DATA(:,16:1:20)',1)+cov(DATA(:,21:1:25)',1)+cov(DATA(:,26:1:30)',1)+...
   % cov(DATA(:,31:1:35)',1)+cov(DATA(:,36:1:40)',1)+cov(DATA(:,41:1:45)',1)+cov(DATA(:,46:1:50)',1);
SW=cov(DATA(:,1:1:5)',1)+cov(DATA(:,6:1:10)',1)+cov(DATA(:,11:1:15)',1);
[E1,V1]=eigs(SW,12);
for i=1:1:15
PR(:,i)=DATA(:,i)-E1*E1'*DATA(:,i);
end
M1=mean(PR(:,1:1:5)');
M2=mean(PR(:,6:1:10)');
M3=mean(PR(:,11:1:15)');
SB=cov([M1' M2' M3']',1);
[E,D]=eigs(SB,2);
%Projected vectors (P)
P=E'*DATA;
mi=min(min(P));
P=P-mi+eps;
ma=max(max(abs(P)));
P=P/ma;
H=10;
%Backpropagation neural network with 9X18X10
W1=rand(2,H);
B1=rand(H,1);
W2=rand(H,3);
B2=rand(3,1);
%TARGET=[repmat([1 zeros(1,9)]',1,5) repmat([0 1 zeros(1,8)]',1,5)  repmat([0 0 1 zeros(1,7)]',1,5) repmat([0 0 0 1 zeros(1,6)]',1,5) repmat([0 0 0 0 1 zeros(1,5)]',1,5) ...
 % repmat([0 0 0 0 0 1 zeros(1,4)]',1,5) repmat([0 0 0 0 0 0 1 zeros(1,3)]',1,5) repmat([0 0 0 0 0 0 0 1 zeros(1,2)]',1,5) repmat([0 0 0 0 0 0 0 0 1 zeros(1,1)]',1,5) repmat([zeros(1,9) 1 ]',1,5) ];
TARGET=[repmat([1 0 0]',1,5)  repmat([0 1 0]',1,5) repmat([0 0 1]',1,5) ];
 TARGET=TARGET*0.7+0.3;
%LP=0.01;    
SSE=[]
m=max(max(abs(P)));
P=P/m;
LPV=[0.01 0.001 ones(1,98)*0.0001 ];
for i=1:1:10
LP=LPV(i);
for epoch=1:1:500
for i=1:1:15
    input=P(:,i);
    target=TARGET(:,i);
    temp1=input'*W1+B1';
    temp2=1./(1+exp(-temp1));
    temp3=temp2*W2+B2';
    error=target'-temp3;
    for i=1:1:H
        for j=1:1:3
    W2(i,j)=W2(i,j)+LP*error(j)*temp2(i);    
        end
    end
    for i=1:1:3
    B2(i)=B2(i)+LP*error(i);
    end   
    for i=1:1:2
        for j=1:1:H
            for k=1:1:3
    W1(i,j)=W1(i,j)+LP*error(k)*W2(j,k)*temp2(j)*(temp2(j)-1)*input(i);
        end
        end
    end
    for i=1:1:H
        for k=1:1:3
    B1(i)=B1(i)+LP*error(k)*W2(i,k)*temp2(i)*(temp2(i)-1);
        end
    end
end
RES1=W1'*P+repmat(B1,1,15);
RES2=1./(1+exp(-RES1));
RES3=W2'*RES2+repmat(B2,1,15);
SSE=[SSE sum(sum((RES3-TARGET).^2))]
end   
end
figure
plot(SSE)
%Testing
load TESTLPC
DATA=[];
for i=1:1:3
    DATA=[DATA TESTLPC{i}(1:1:x,:)];
end
U=isnan(DATA);
[R,S]=find(U==1);
for i=1:1:length(R)
    DATA(R(i),S(i))=eps;
end
P=E'*DATA;
P=P-mi+eps;
P=P/ma;
RES1=W1'*P+repmat(B1,1,15);
RES2=1./(1+exp(-RES1));
RES3=W2'*RES2+repmat(B2,1,15);
[P,Q]=max(RES3);
%REF=[ones(1,5) ones(1,5)*2 ones(1,5)*3 ones(1,5)*4 ones(1,5)*5 ones(1,5)*6 ones(1,5)*7 ones(1,5)*8 ones(1,5)*9 ones(1,5)*10];
REF=[ones(1,5) ones(1,5)*2 ones(1,5)*3];
POS=(length(find((REF-Q)==0))/length(REF))*100;


