clear;

X=[ 1.0000    0.5000         0         0         0
    0.5000    1.0000    0.5000         0         0
    0.5000    1.0000    0.5000         0         0
         0    0.5000    1.0000    0.5000         0
         0    0.5000    1.0000    0.5000         0
         0    0.5000    1.0000    0.5000         0
         0         0    0.5000    1.0000    0.5000
         0         0    0.5000    1.0000    0.5000
         0         0         0    0.5000    1.0000]';
     
Y=[ 0.5000    1.0000    0.5000         0         0
    0.5000    1.0000    0.5000         0         0
         0    0.5000    1.0000    0.5000         0
    0.5000    1.0000    0.5000         0         0
         0    0.5000    1.0000    0.5000         0
         0         0    0.5000    1.0000    0.5000
         0         0    0.5000    1.0000    0.5000
         0         0         0    0.5000    1.0000
         0         0         0    0.5000    1.0000]';

P=[1 0.6 0 0 0;
0.4 1 0.6 0 0;
0.8 1 0.2 0 0;
0.4 1 0.6 0 0;
0 0.6 1 0.4 0;
0.4 1 0.6 0 0;
0 0.8 1 0.2 0;
0 0.6 1 0.4 0;
0 0 0.8 1 0.2;
0 0 0.2 1 0.8;
0 0 0 0.4 1;
0 0 0 0.4 1]';

np=size(P,2);

dom=[1250, 1350, 1450, 1550, 1650];

Real=[1370 1318 1364 1434 1361 1406 1433 1516 1595 1692 1658 1660];

T=[1984:1995];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Previsão da série temporal;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Kosko FAM e GFAM de Lukasiewicz;
WFAM_M = maxC_M(Y,X');   		% Max-min FAM; 
WFAM_P = maxC_P(Y,X');		% Max-prod FAM;
WGFAM_L = maxC_L(Y,X');		% Lukasiewicz GFAM;

% FAM Junbo;
WFAM_J = minI_M(Y,X');

% FLABM de Lukasiewicz, Godel e Goguen;
WFLBAM_L = WGFAM_L;   		% Lukasiewicz FLBAM; 
WFLBAM_M  = WFAM_M;		% Godel FLBAM;
WFLBAM_P  = WFAM_P;		% Goguen FLBAM;

% IFAM de Lukasiewicz, Godel e Goguen;
theta=min(Y,[],2);
WIFAM_L = minI_L(Y,X');	   		% Lukasiewicz IFAM; 
WIFAM_M  = minI_M(Y,X');		% Godel IFAM;
WIFAM_P  = minI_P(Y,X');		% Goguen IFAM;

% IFAM Dual de Lukasiewicz, Godel e Goguen;
dtheta=max(Y,[],2);
WdIFAM_L = maxJ_L(Y,X');	   		% Lukasiewicz Dual IFAM; 
WdIFAM_M  = maxJ_M(Y,X');		% Godel Dual IFAM;
WdIFAM_P  = maxJ_P(Y,X');		% Goguen Dual IFAM;

% IFAM Adjunta de Lukasiewicz, Godel e Goguen;
WaIFAM_L = minI_L(X,Y');	   		% Lukasiewicz Adjunta IFAM; 
WaIFAM_M  = minI_M(X,Y');		% Godel Adjunta IFAM;
WaIFAM_P  = minI_P(X,Y');		% Goguen Adjunta IFAM;

% IFAM Adjunta Dual de Lukasiewicz, Godel e Goguen;
WadIFAM_L = maxJ_L(X,Y');	   		% Lukasiewicz Dual Adjunta IFAM; 
WadIFAM_M  = maxJ_M(X,Y');		% Godel Dual Adjunta IFAM;
WadIFAM_P  = maxJ_P(X,Y');		% Goguen Dual Adjunta IFAM;


clear y_m; clear y_p; clear y_L;
for i=1:np
	prevFAM_M(i)=defuzz(dom,maxC_M(WFAM_M,P(:,i)),'mom');
	prevFAM_P(i)=defuzz(dom,maxC_P(WFAM_P,P(:,i)),'mom');
	prevGFAM_L(i)=defuzz(dom,maxC_L(WGFAM_L,P(:,i)),'mom');
	
	prevFAM_J(i)=defuzz(dom,maxC_M(WFAM_J,P(:,i)),'mom');
	
	prevFLBAM_L(i)=defuzz(dom,minI_L(WFLBAM_L,P(:,i)),'mom');
	prevFLBAM_M(i)=defuzz(dom,minI_M(WFLBAM_M,P(:,i)),'mom');
	prevFLBAM_P(i)=defuzz(dom,minI_P(WFLBAM_P,P(:,i)),'mom');
	
	prevIFAM_L(i)=defuzz(dom,max(theta,maxC_L(WIFAM_L,P(:,i))),'mom');
	prevIFAM_M(i)=defuzz(dom,max(theta,maxC_M(WIFAM_M,P(:,i))),'mom');
	prevIFAM_P(i)=defuzz(dom,max(theta,maxC_P(WIFAM_P,P(:,i))),'mom');
	
	prevdIFAM_L(i)=defuzz(dom,min(dtheta,minD_L(WdIFAM_L,P(:,i))),'mom');
	prevdIFAM_M(i)=defuzz(dom,min(dtheta,minD_M(WdIFAM_M,P(:,i))),'mom');
	prevdIFAM_P(i)=defuzz(dom,min(dtheta,minD_P(WdIFAM_P,P(:,i))),'mom');
	
	prevaIFAM_L(i)=defuzz(dom,minI_L(WaIFAM_L,P(:,i)),'mom');
	prevaIFAM_M(i)=defuzz(dom,minI_M(WaIFAM_M,P(:,i)),'mom');
	prevaIFAM_P(i)=defuzz(dom,minI_P(WaIFAM_P,P(:,i)),'mom');
	
	prevadIFAM_L(i)=defuzz(dom,maxJ_L(WadIFAM_L,P(:,i)),'mom');
	prevadIFAM_M(i)=defuzz(dom,maxJ_M(WadIFAM_M,P(:,i)),'mom');
	prevadIFAM_P(i)=defuzz(dom,maxJ_P(WadIFAM_P,P(:,i)),'mom');
end

ErroFAM_M=mean(100*abs(prevFAM_M-Real)./Real); disp(sprintf('Max-min FAM & %2.2f',ErroFAM_M))
ErroFAM_P=mean(100*abs(prevFAM_P-Real)./Real); disp(sprintf('Max-prod FAM & %2.2f',ErroFAM_P))
ErroGFAM_L=mean(100*abs(prevGFAM_L-Real)./Real); disp(sprintf('GFAM Lukasiewicz & %2.2f',ErroGFAM_L))

ErroFAM_J=mean(100*abs(prevFAM_J-Real)./Real); disp(sprintf('FAM de Junbo & %2.2f',ErroFAM_J))

ErroFLBAM_L=mean(100*abs(prevFLBAM_L-Real)./Real); disp(sprintf('FLBAM Lukasiewicz & %2.2f',ErroFLBAM_L))
ErroFLBAM_M=mean(100*abs(prevFLBAM_M-Real)./Real); disp(sprintf('FLBAM Godel & %2.2f',ErroFLBAM_M))
ErroFLBAM_P=mean(100*abs(prevFLBAM_P-Real)./Real); disp(sprintf('FLBAM Goguen & %2.2f',ErroFLBAM_P))

ErroIFAM_L=mean(100*abs(prevIFAM_L-Real)./Real); disp(sprintf('IFAM Lukasiewicz & %2.2f',ErroIFAM_L))
ErroIFAM_M=mean(100*abs(prevIFAM_M-Real)./Real); disp(sprintf('IFAM Godel & %2.2f',ErroIFAM_M))
ErroIFAM_P=mean(100*abs(prevIFAM_P-Real)./Real); disp(sprintf('IFAM Goguen & %2.2f',ErroIFAM_P))

ErrodIFAM_L=mean(100*abs(prevdIFAM_L-Real)./Real); disp(sprintf('IFAM Dual Lukasiewicz & %2.2f',ErrodIFAM_L))
ErrodIFAM_M=mean(100*abs(prevdIFAM_M-Real)./Real); disp(sprintf('IFAM Dual Godel & %2.2f',ErrodIFAM_M))
ErrodIFAM_P=mean(100*abs(prevdIFAM_P-Real)./Real); disp(sprintf('IFAM Dual Goguen & %2.2f',ErrodIFAM_P))

ErroaIFAM_L=mean(100*abs(prevaIFAM_L-Real)./Real); disp(sprintf('IFAM Adjunta Lukasiewicz & %2.2f',ErroaIFAM_L))
ErroaIFAM_M=mean(100*abs(prevaIFAM_M-Real)./Real); disp(sprintf('IFAM Adjunta Godel & %2.2f',ErroaIFAM_M))
ErroaIFAM_P=mean(100*abs(prevaIFAM_P-Real)./Real); disp(sprintf('IFAM Adjunta Goguen & %2.2f',ErroaIFAM_P))

ErroadIFAM_L=mean(100*abs(prevadIFAM_L-Real)./Real); disp(sprintf('IFAM Dual Adjunta Lukasiewicz & %2.2f',ErroadIFAM_L))
ErroadIFAM_M=mean(100*abs(prevadIFAM_M-Real)./Real); disp(sprintf('IFAM Dual Adjunta Godel & %2.2f',ErroadIFAM_M))
ErroadIFAM_P=mean(100*abs(prevadIFAM_P-Real)./Real); disp(sprintf('IFAM Dual Adjunta Goguen & %2.2f',ErroadIFAM_P))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
% Gráfico!

figure(1); clf;
plot(T,prevadIFAM_L,'-*g',...
      T,prevaIFAM_L,'-+y',...
      T,prevIFAM_M,'-sc',...
      T,prevdIFAM_L,'-om',...
      T,prevGFAM_L,'-vr',...
      T,prevIFAM_L,'-^b',...
      T,Real,'--k',...
      'linewidth',2);

legend('IFAM dual adjunta de Lukasiewicz','IFAM adjunta de Lukasiewicz','IFAM de Godel, FAM de Junbo e Max-min FAM com limiar','IFAM dual de Lukasiewicz e FLBAMs de Lukasiewicz, Godel e Goguen','FAMs max-min e max-prod e GFAM de Lukasiewicz','IFAM de Lukasiewicz','Valor Real', 'Location','NorthWest');
print -depsc manpower_tese.eps

%  plot(T,prevadIFAM_L,'--^g',...
%        T,prevaIFAM_L,'-.vy',...
%        T,prevIFAM_M,'-sc',...
%        T,prevdIFAM_L,'-.^m',...
%        T,prevGFAM_L,'--vr',...
%        T,prevIFAM_L,'--ob',...
%        T,Real,'-k',...
%        'linewidth',1.7);