function [  ] = NNXOR(  )

close all, clear all, clc, format compact
% number of samples of each class
K = 100;
% define 4 clusters of input data
q = 1; % offset of classes
A = [rand(1,K)-q; rand(1,K)+q];
B = [rand(1,K)+q; rand(1,K)+q];
C = [rand(1,K)+q; rand(1,K)-q];
D = [rand(1,K)-q; rand(1,K)-q];
% plot clusters
figure(1)
plot(A(1,:),A(2,:),'k+')
hold on
grid on
plot(B(1,:),B(2,:),'bd')
plot(C(1,:),C(2,:),'k+')
plot(D(1,:),D(2,:),'bd')
% text labels for clusters
text(.5-q,.5+2*q,'Class A')
text(.5+q,.5+2*q,'Class B')
text(.5+q,.5-2*q,'Class A')
text(.5-q,.5-2*q,'Class B')




% encode clusters a and c as one class, and b and d as another class
a = -1; % a | b
b = 1; % -------
c = -1; % d | c
d = 1; %


% define inputs (combine samples from all four classes)
P = [A B C D];
% define targets
T = [repmat(a,1,length(A)) repmat(b,1,length(B)) repmat(c,1,length(C)) repmat(d,1,length(D)) ];
% view inputs |outputs
% [P' T']




net=newff(P,T,[2]);
% net.layers{2}.transferFcn = 'logsig';

% view(net)



% train net
% net.divideParam.trainRatio = 1; % training set [%]
% net.divideParam.valRatio = 0; % validation set [%]
% net.divideParam.testRatio = 0; % test set [%]

net.trainFcn = 'traingd';
net.performFcn = 'mse';




net.trainParam.epochs=200; %Maximum number of epochs to train
net.trainParam.goal=0; %Performance goal
net.trainParam.showCommandLine=1; %Generate command-line output
net.trainParam.showWindow=1; %Show training GUI
net.trainParam.lr=0.1; %Learning rate
net.trainParam.max_fail=200; %Maximum validation failures
net.trainParam.min_grad=1e-10; %Minimum performance gradient
net.trainParam.show=25; %Epochs between displays (NaN for no displays)
net.trainParam.time=inf; %Maximum time to train in seconds



% train a neural network
[net,tr,Y,E] = train(net,P,T);
% show network
view(net)


figure(2)
plot(T','linewidth',2)
hold on
plot(Y','r--')
grid on
legend('Targets','Network response','location','best')
ylim([-1.25 1.25])



% generate a grid
span = -1:.005:2;
[P1,P2] = meshgrid(span,span);
pp = [P1(:) P2(:)]';
% simulate neural network on a grid
aa = net(pp);
% translate output into [-1,1]
%aa = -1 + 2*(aa>0);
% plot classification regions
figure(1)
mesh(P1,P2,reshape(aa,length(span),length(span))-5);
colormap cool

view(2)

end

