%UWAGA WYŁĄCZYĆ PROUNING DRZEWA !!!!
pathchk;
clear; 
global RandState;
RandState=1;

% si=100;
% Dat.Data=[randn(si,2)+1.2;randn(si,2)-1.2];
% %plot(x1(:,1),x1(:,2),'r.'); hold on; plot(x2(:,1),x2(:,2),'g.')
% Dat.Labels=false(si*2,2);
% Dat.Labels(1:si,1)=1;
% Dat.Labels(si+1:2*si,2)=1;
% Dat.AttrType=[0 0];
%save('g2g.dat','Dat.Data','-ascii');
%save('g2g.lab','Dat.Labels','-ascii');

Dat=ReadData('Data\simple\abc');
[q,Dat]=Learn(Apply(Standarize('Type',0)),Dat);
M2=StatClassifier('Type','linear');
M1=DecisionTreeClassifier('splitcriterion','deviance','splitmin',7); %'gdi''twoing''deviance'
    resCV=[];
    res=[];
[M1,Dat]=Learn(M1,Dat);
[M2,Dat]=Learn(M2,Dat);
if size(Dat.AttrType,2)==2
    %tDat=ReadData('Data\test');
    [x,y]=meshgrid(0:0.005:1,0:0.005:1);
    xx=reshape(x,prod(size(x)),1);
    yy=reshape(y,prod(size(y)),1);
    tDat.Data=[xx yy];
    tDat.Labels=false(prod(size(y)),2);
    tDat1=Perform(M1,tDat);
    tDat2=Perform(M2,tDat);
    %x=reshape(tDat.Data(:,1),3481^0.5,3481^0.5);
    %y=reshape(tDat.Data(:,2),3481^0.5,3481^0.5);
    
    pl=figure(2); clf;
    PlotData(Dat,['or';'xb';'ok';'og'],pl);    
    b1=reshape(Labels_Bin2Num(tDat1.Labels),size(x,1),size(x,2));        
    contour(x,y,b1-1);
    xlabel('x_1 [-]');
    ylabel('x_2 [-]');
    saveas(pl,'Crules','eps');
    
    
    pl=figure(3); clf;
    PlotData(Dat,['or';'xb';'ok';'og'],pl);
    b2=reshape(Labels_Bin2Num(tDat2.Labels),size(x,1),size(x,2));    
    contour(x,y,b2-1);
    xlabel('x_1 [-]');
    ylabel('x_2 [-]');
    saveas(pl,'Frules','eps')
end;


Dat=ReadData('Data\simple\CFCMex');
[q,Dat]=Learn(Apply(Standarize('Type',0)),Dat);
M2=PkNNClassifier('LearnModel',FCMselect('ProtoPerClass',[1 2]));
M1=PkNNClassifier('LearnModel',CFCMselect('ProtoPerClass',[1 2],'Gamma',2,'Mean',0.6));
    resCV=[];
    res=[];
[M1,Dat]=Learn(M1,Dat);
[M2,Dat]=Learn(M2,Dat);
if size(Dat.AttrType,2)==2
    %tDat=ReadData('Data\test');
    [x,y]=meshgrid(0:0.005:1,0:0.005:1);
    xx=reshape(x,prod(size(x)),1);
    yy=reshape(y,prod(size(y)),1);
    tDat.Data=[xx yy];
    tDat.Labels=false(prod(size(y)),2);
    tDat1=Perform(M1,tDat);
    tDat2=Perform(M2,tDat);
    %x=reshape(tDat.Data(:,1),3481^0.5,3481^0.5);
    %y=reshape(tDat.Data(:,2),3481^0.5,3481^0.5);
    
    pl=figure(4); clf;
    PlotData(Dat,['or';'xb';'ok';'og'],pl);   
    Proto1=get(M1,'Proto');
    plot(Proto1.Data(:,1),Proto1.Data(:,2),'sk','MarkerFaceColor','k')
    %PlotData(Proto1,['sy';'sy';'sk';'xg';'xy';'xc';'xm';'xb';'xm'],pl);    
    b1=reshape(Labels_Bin2Num(tDat1.Labels),size(x,1),size(x,2));        
    contour(x,y,b1-1);
    d=dist2(Proto1.Data,tDat.Data);    
    v=[0.01 0.05  0.15  0.25  0.35 0.5];
    d1=reshape(d(1,:),size(x,1),size(x,2));
    d2=reshape(d(2,:),size(x,1),size(x,2));
    d3=reshape(d(3,:),size(x,1),size(x,2));
    contour(x,y,d1.*((d1<d2)&(d1<d3)),v);
    contour(x,y,d2.*((d2<d1)&(d2<d3)),v);
    contour(x,y,d3.*((d3<d2)&(d3<d1)),v);    
    xlabel('x_1 [-]');
    ylabel('x_2 [-]');
    saveas(pl,'cfcm','eps');
    
    
    pl=figure(5); clf;
    PlotData(Dat,['or';'xb';'ok';'og'],pl);   
    Proto2=get(M2,'Proto');
    plot(Proto2.Data(:,1),Proto2.Data(:,2),'sk','MarkerFaceColor','k')
    %PlotData(Proto2,['sy';'sy';'sk';'xg';'xy';'xc';'xm';'xb';'xm'],pl);    
    b2=reshape(Labels_Bin2Num(tDat2.Labels),size(x,1),size(x,2));        
    contour(x,y,b2-1);
    d=dist2(Proto2.Data,tDat.Data);    
    d1=reshape(d(1,:),size(x,1),size(x,2));
    d2=reshape(d(2,:),size(x,1),size(x,2));
    d3=reshape(d(3,:),size(x,1),size(x,2));
    contour(x,y,d1.*((d1<d2)&(d1<d3)),v);
    contour(x,y,d2.*((d2<d1)&(d2<d3)),v);
    contour(x,y,d3.*((d3<d2)&(d3<d1)),v);
    xlabel('x_1 [-]');
    ylabel('x_2 [-]');
    saveas(pl,'fcm','eps');
end;


