%
% Computational Experiment with Gaussian Distribuction
% See Haykin's book.
%

clear

% Training Set,
n_P=500;
for i=1:n_P
        if rand<=0.5
            P(:,i)=randn(2,1);
            cP(i)=1;
        else
            P(:,i)=2*randn(2,1)+[2;1];
            cP(i)=0;
        end
end
c1=find(cP);
c2=find(1-cP);
plot(P(1,c1),P(2,c1),'*b',P(1,c2),P(2,c2),'xr','MarkerSize',12);
title('Training Set');
disp('Paused: Press any key');
pause

% Validation set,
n_V=250;
for i=1:n_V
        if rand<=0.5
            V(:,i)=randn(2,1);
            cV(i)=1;
        else
            V(:,i)=2*randn(2,1)+[2;1];
            cV(i)=0;
        end
end

% Test set,
n_T=250;
for i=1:n_T
        if rand<=0.5
            T(:,i)=randn(2,1);
            cT(i)=1;
        else
            T(:,i)=2*randn(2,1)+[2;1];
            cT(i)=0;
        end
end     

%
% Learning
%
[p,W,erro,EV,ET]=LearnSLMP(P,cP,V,cV,T,cT);
% Or [p,W,erro,EV,ET]=LearnSLMPA(P,cP,V,cV,T,cT);

figure(2);
plot(1:length(erro),erro,'--k',1:length(EV),EV,'-.b',1:length(ET),ET,'-r');
legend('Training Error','Validation Error','Test Error');

% Select the dendrites that minimizes the validation error.
[aux,ind]=min(EV);
disp(sprintf('\n We selected %d dendrites. In this case, the validation error is %0.4f (minimum) and the test error is %0.4f.\n',ind,aux,ET(ind)));

% Select the first dendrites,
W=W(:,1:ind,:);
p=p(1:ind);
% SLMP
y=SLMP(p,W,T);
% Region for the SLMP activation
figure(1);
verclasse(p,W,T,cT)
title('Test set and the region for the SLMP');