% Chad Myers
% ELE 571
% File: xortest.m
% ------------------------
% This file contains the xortest function which, given a weight matrix,
% distribution means and variances, tests the corresponding neural
% network's ability to discriminate between two classes (setup to similate
% the XOR problem).  The function returns the percentage of correctly
% classified inputs from a 100 element randomly chosen data set.

function correct = xortest(W_aug,means,sigma,rbf)  % xortest(weight matrix, distribution means,distribution variances,1 for RBF/0 for EBF)
class1_id = 1;
class2_id = -1;
n=100;

clusters=length(W_aug)-1;
W = W_aug(2:clusters+1,1);

fprintf('Testing...\n');
for i=1:n,
    temp1 = 2*rand(2);
    temp2 = rand(2);
    
    if(temp1(1) > 1) data(i,1) = temp1(2);
    else data(i,1) = -temp1(2);
    end
        
    if(temp2(1) > .5) data(i,2) = temp2(2);
    else data(i,2) = -temp2(2);
    end
    
    if(abs(data(i,2)) > .5*abs(data(i,1))) desired(i,1) = class1_id;
    else desired(i,1) = class2_id;
    end
end

 %compute activation function outputs
 for j=1:n,
     for k=1:clusters,
         if(rbf==1)
             phi(k,j)= Gaussian(data(j,1:2),means(k,1:2),[sigma(k),sigma(k)]);
         else
             phi(k,j)= Gaussian(data(j,1:2),means(k,1:2),sigma(k,1:2));
         end
     end
 end

output= W_aug(1,1) + phi'*W;
 
b=sign(output)+sign(desired);
a=abs(b(:,1));
correct=sum(a)/(2*n);
fprintf('Number of classified points: %d\n',n);
fprintf('Percentage of test points correctly classified: %f\n',correct*100);

