function [] = part2(filename)
%Function: part2
%Arguments: filename
%filename: the classification file with the data points AND classification(-1 or 1)
%Returns: nothing
%By: Authors removed (but thanks for letting me post it... you know who you are :)
%
%Summary: part2 reads in a file that contains two files worth of data creates a neural net to
%train using this data.  The sections are commented below, however the general idea is that we
%break up the data by classification, run it through our part1 (our EM algorithm) to find a 
good
%solution and then train the perceptron using this data to see how quickly it will learn it
%
%NOTES: we had to randomize the given data because the sorted data allows the perceptron to
%have almost precognition.  Meaning the perceptron is intialized already knowing the data.
      
%load the classification data
data = load (filename,'-ascii');

%randomize the classification data so it's not ordered
list = randperm(length(data));
for value = 1:length(data)
    tempData(value,:) = data(list(value),:);
end
data = tempData;

%ctra = counter for matrix a 
%ctrb = counter for matrix b
%temp = length of the classification file
ctra = 1;
ctrb = 1;
loopFor = length(data(:,3));
for i = 1:loopFor
    if (data(i, 3) == 1)            % class 1
        a(ctra, 1) = data(i, 1);    % put the data points in a
        a(ctra, 2) = data(i, 2);
        ctra = ctra + 1;
    elseif (data(i, 3) == -1)       % class -1
        b(ctrb, 1) = data(i, 1);    % put the data points in b
        b(ctrb, 2) = data(i, 2);
        ctrb = ctrb + 1;
    else                            % class is not valid
        disp('There is a problem reading in the classification data')
    end
end

%get random centers for the 7 gaussians from a (donut)
for i = 1:7
    centerA(i,:) = a(ceil(rand*length(a)),:);
end    
tempCenter = sort(dist(centerA')); %uses dist to calculate the eucidean distances between all 
points
sigmaA = tempCenter(2,:)'; %store the intial sigmas.

%calculate the center and sigma for b (the hole)
maxValue = ceil(max(max(data)));  %calculate the maximum value of the data
minValue = floor(min(min(data))); %calculate the minimum value of the data
centerB = b(ceil(rand*length(b)),:); %randomly pick a data point as the center
sigmaB = rand*maxValue-abs(minValue); %calculate a random sigma

%concatenate sigmas and centers
sigma = sigmaA;
sigma(length(sigmaA)+1) = sigmaB;
center = centerA;
center(length(centerA)+1,:) = centerB;

%calculate the gaussians using a call to repeat which calls out EM algorithm (with 10 
iterations)
pjA = repmat(1/7,7,1);
SETA = repeat2(sigmaA, centerA, a, pjA, 7, 1);
SETB = repeat2(sigmaB, centerB, b, 1, 1, 1);

%concatenate the data
dataSet(:,1) = data(:,1);
dataSet(:,2) = data(:,2);

%concatenate the solution from the EM algorithm
SET = SETB;
SET(2:8,:) = SETA(1:7,:);

%elminate the fourth column (needed because repeat used a fourth column for priors
finalSET = SET(:,1:3);

%intialize weights and the threshold
weights = (rand(1,8)*.001);
threshold = 0;

for j = 1:8 %for each gaussian
    input(:,j) = gauss(finalSET(j,1), [finalSET(j,2) finalSET(j,3)], dataSet);
end

%run our neural net for 100 iterations
for loop = 1:100
    numberWrong = 0;
    number = 0;
    for point = 1:length(dataSet)
        netInput = weights.*input(point,:);
        netInput = sum(netInput)-threshold;
        number = number + 1;
        class(point) = sign(netInput);
        trainingLabel(point) = data(point,3);
    end 
    for point = 1:length(dataSet)
        if(class(point) ~= trainingLabel(point))
            numberWrong = numberWrong+1;
            weights = weights+(trainingLabel(point)-class(point)).*input(point,:);
            threshold = threshold+(trainingLabel(point)-class(point))*(-1);    
        end
    end
    answer(loop) = numberWrong;
end

%plot our resulting error graph
figure, plot(answer,'bx-');
title('Our error vs. iterations graph');
xlabel('iterations');
ylabel('our error');
