%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%the purpose of the following codes is to test the rbf algorithm 
%after you test the algorithm, you can comments these codes, and at the
%same time umcomments the function's name and its 'end' statement
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;

%a = [[0,0,1];[0,1,0];[1,0,0];[1,1,1]];
%b = [[0,0,0];[0,1,1];[1,0,1];[1,1,0]];
Data = load('Data/iris.txt');

train1_in=Data(21:50,2:5);
train2_in=Data(71:100,2:5);
train1_t = Data(21:50,1);
train2_t = Data(71:100,1);

test1_in=Data(1:20,2:5);
test2_in = Data(51:70,2:5);
test1_t = Data(1:20,1);
test2_t = Data(51:70,1);

inputs = [train1_in;train2_in];%Data(1:30, 2:5);
targets = [train1_t;train2_t];%Data(1:100,1);

inputs_t = [test1_in;test2_in];
targets_t = [test1_t; test2_t]
eta = 0.25;
nInteration = 20;
sigma = 1;
k = 2;
usekmeans = 1;
normalise = 0;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function: adjust the weights matrics via using RBF method
%               
% Inputs Parameters: 
%             inputs: inputs data
%             k:    the number of centres needed to be found
%             targets: targets data
%             sigama: the square of standard deviation of Gaussian
%             distribution
%             eta: learning rate
%             nInteration: the number of interation 
%             usekmeans: if we use k-means algorithm to find the center
%             points
%             normalise: if we use normalise method to adjust the center
%             points, which is the inputs of the perceptron
% Outputs Parameters:
%             weights trained by perceptron 
%
%The codes is written according to the Python code rbf.py and the
%algorithm written on the page of 104
%
%-----by Xueli Huang 9/20/2010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% function [ outputs] = rbftrain( inputs,targets,k,sigma = 0,usekmeans=0,normalise=0,eta = 0.25,nInteration = 10 )
%UNTITLED5 Summary of this function goes here
%   Detailed explanation goes here
%Initialise 
[ndata,nin]=size(inputs);
nout=size(targets,2);

%figure, hold on;
%help_ind = find(targets == 1);
%plot( inputs(:,1), inputs(:,2), '.g', 'markersize', 40 );
%plot( inputs(help_ind,1), inputs(help_ind,2), '.r', 'markersize', 40 );

%the last col is for centres
hidden = zeros(ndata,k+1);
% if sigma is not set, we use the most common choice to pick up the width
% of the Gaussian
if sigma == 0
    d = max(max(inputs)-min(inputs));
   sigma = d/sqrt(2*k);
end
    
centers= zeros(k,nin);
%begin to rbf train
%if we do not use the k-means algorithms to choose the centres, then
%randomly choose k pieces of data as weights 
if usekmeans == 0

        indeces=randperm(ndata);
    %for i = 1:k
   %     centers(i, :) = inputs(indeces(i),:);
   % end

    % centers = [1,1; 0,0];

else
    %use k-means algorithms to choose centres
    [cid,centers] = Kmeans(inputs,k);%(m_kmeansstrain(inputs,k))';
end
centers
%rbf training
for i = 1: k
    %Equation of 4.2 page 99
    tmp = repmat(centers(i,:), [size(inputs,1), 1] );
    %size(tmp)
    %size(inputs)
    mdiff = ((inputs - tmp).^2);%power(||x-w||,2)
    hidden(:,i) = exp(-sum(mdiff,2)/(2*(sigma.^2)));%Equation 4.2
    %hidden(:,i) = exp((-sum(((inputs - (repmat(weights1(:,i), 1,size(inputs,2))')  ).^2),2))/(2*sigma.^2));
end
%normalise or not, aims to normalise Gaussian function
if normalise
    hidden(:,k+1) =  hidden(:,k+1)/sum(hidden(:,k+1));
else
    hidden(:,k+1) = -1;
end
%size(hidden)

%figure, hold on;
%help_ind = find(targets == 1);
%plot( hidden(:,1), hidden(:,2), '.g', 'markersize', 40 );
%plot( hidden(help_ind,1), hidden(help_ind,2), '.r', 'markersize', 40 );


%use perceptron algrothm to ajust the weights
weights=m_pcn(hidden,targets,eta,nInteration);
weights
    
%rbf test
%step 1: data tranformation
hidden_t = zeros(size(inputs_t,1),k+1);
for i = 1: k
    %Equation of 4.2 page 99
    tmp = repmat(centers(i,:), [size(inputs_t,1), 1] );
    %size(tmp)
    %size(inputs)
    mdiff = ((inputs_t - tmp).^2);%power(||x-w||,2)
    hidden_t(:,i) = exp(-sum(mdiff,2)/(2*(sigma.^2)));%Equation 4.2
    %hidden(:,i) = exp((-sum(((inputs - (repmat(weights1(:,i), 1,size(inputs,2))')  ).^2),2))/(2*sigma.^2));
end
%step 2 perceptron test
y = zeros(size(inputs_t,1), 1);

hidden_t(:,size(hidden_t,2))=-1;
 for i = 1:size(hidden_t, 1)
        y(i) = sum( weights .* hidden_t(i,:) );        
        if y(i) > 1.5
            y(i) = 2;
        else
            y(i) = 1;
        end
 end

%accuracy
c1=0;
c2=0;
for i=1:20
    if(y(i) == targets_t(i))
        c1 = c1+1;
    end
    if(y(i+20)==targets_t(i+20))
        c2 = c2+1;
    end
end

a1=c1/20
a2 = c2/20
