%% Initialization
clear ; close all; clc

%% Setup the parameters you will use for this exercise
input_layer_size  = 2;  
hidden_layer_size = 2;
num_labels = 2;      

%% =========== Part 1: Loading and Visualizing Data =============
% Training Data
% First distro((3,0),1)
x1G1 = randn(500,1)*74 + 60;
x2G1 = randn(500,1)*74 + 60;

%strx1R = "D2 4E 0C 35 FB 48 A1 7A CB 62 D6 6E E2 2A 6A EA CA 24 C3 7D 2C 2A AC A6 47 57 60 51 6E BD AF 1A BD B0 D3 FD 86 AE 93 C3 45 D8 2A 37 45 1A C0 03 D0 23 0E D3 6C 6A DB 93 18 24 12 6C 12 EE 41 3A 92 30 70 D3 B1 C2 66 BF 7C 95 CC 64 46 E8 4D 20 88 08 AA 8E F0 B8 81 B7 13 AC 7E 54 28 3D D3 B8 2A 50 FA ED 9E B9 AE 8B 7C B5 73 2B 05 2B 76 BD 8C FB 78 A2 65 D5 33 5B 42 FB 88 1D E2 F0 C8 1B 7B 1F E6 1F 36 F7 F4 5E B1 33 29 58 EF BC BA B8 89 6F F4 65 EA 61 80 2F F7 E8 C7 24 EF 72 5E E7 12 50 C0 D1 21 29 55 CD E6 19 10 D3 0F 3A 58 D3 C3 CE 60 90 E1 2A 2D 9A EC B4 5F C0 6B 2A 1A 35 67 EB 75 D2 64 8D 65 B2 31 C8 32 9D 0D 6B 70 F1 DA FB 5F 2D 5B 48 59 3C 69 85 FC 35 FB A0 66 02 3D 83 9C 70 4C 6E 6C F9 00 91 4D F6 C8 3B 11 CC 02 29 FB 7C C5 2A 42 A9 59 DD 28 80 49 7F E6 44 52 B1 64 48 00 B3 50 01 F2 44 CA D3 57 AE 2D 7C DB A6 B8 02 6E 9F 99 18 8F D2 80 E4 49 CB F8 16 C9 D9 21 3F A3 8F F2 D0 42 5D BE 6A BB 72 F4 A8 18 6A 5C 61 AB 6B 4C C1 B9 07 A9 83 7A 51 AC EF 28 69 9E 12 28 80 4C 05 6A DF EA 8F 40 61 63 D6 32 B0 F9 A8 1E A1 0F BA 00 CD FE D0 E2 57 93 32 CC 0F 85 01 E0 1A 08 39 99 9B FB EC 3C 12 88 93 3D 7F F3 2F D5 68 EC C5 C2 33 60 46 FA 18 79 DC 88 8F 3A C0 C5 96 29 50 60 05 DD EA AE 17 69 EE 04 95 DE 1C E5 A5 81 87 51 1A 50 64 AC 25 6B F6 DB 16 AD 03 0B EA 47 71 4C CF 67 99 DE 05 AB BF 65 1C 36 AE 3C 88 F7 9C C4 DC 6B 2B EB 4B 25 BE 34 31 3A 1B F4 BC 6C 27 23 2D DB E4 6B 3D D3 61 23 19 56 90 AD 3C 4A 5A FE 81 E4 96 7B 22 F8 52 0B F4 CE 77 29 FA 10 D3 BA 5E 22 48 4E D1 7D 32 19 C8 3F 4C";
%cx1R = strsplit(strx1R, " ");
%x1R = [];
%for i = 1:500
%    x1R = [x1R; hex2dec(cell2mat(cx1R(i)))];
%end
%
%strx2R = "CD C7 A3 2B 21 45 60 0D 47 AB CA FC 02 E0 70 0A B1 C0 19 4D 47 A6 C1 CD 85 4C 99 95 74 F9 C0 4D 87 8C C2 76 6C 65 57 74 02 E3 FB 28 E1 58 41 C5 15 BE 27 CE 30 CE 29 8E 4B 84 22 7B 53 B9 5B 0E 0E 9A 3A 1D AC 7E 71 C7 28 26 67 22 AB DC 43 5F 0B A6 C6 97 21 0C 5B BA 41 2B 34 61 0B 0D 44 69 6F AD 35 F9 4D F5 39 8E D3 72 50 89 D0 8D E1 29 6B 5E 64 57 05 41 71 5D 40 B6 E7 3F 9F FC 8D 79 73 81 2F B4 0E 56 34 06 2F 42 02 1C 64 54 4D 9E 83 4E 49 BD 04 CB 92 1C 3C 0E B5 50 EB CA B9 06 50 CD E7 1F 23 93 17 52 40 CD AB FA 41 CE D5 91 3B F9 B3 23 DB E6 2F 70 AD 6E 6F 16 DC C9 BA 77 03 82 D9 28 B4 A0 1C 2E 76 13 8B 03 8F 09 45 AA C2 5C 6D 8B C5 F3 80 C9 FC BE FC FF FB 06 BD 0E 9A 75 7F 1F DB A5 A3 C6 27 F3 DE 85 05 2E 24 C1 34 EA 4E 74 B5 1E 04 99 3D 81 82 7E DB 73 A8 79 57 E1 62 6A 1E 93 5B 79 B3 29 52 22 D0 37 58 DF D9 CD 27 29 24 80 3E 3F 5F 47 44 09 47 AE BE 8D A4 EE 23 27 20 6F B4 EE 49 A4 0B 84 E9 06 CD 4A 7B 68 AC 81 63 AD 81 04 15 EB 84 4A 02 F3 A9 6D E8 6A B8 88 EF 0D F8 06 92 47 9D 36 19 C7 34 58 07 3B 52 DA 16 8C B5 30 22 1F F7 D3 2F F0 EB B5 94 95 3B FF 23 04 C0 96 47 47 AD 90 03 A1 1F A0 0B CD 53 09 0E 21 E3 65 8C EB 72 B1 69 FA 77 72 6A 81 A0 FD 89 2F 55 28 88 87 B1 4B 65 0C 60 63 B7 9C 85 9B 66 01 57 13 08 BE 4D A1 DC D4 1E A6 CB DD A9 F2 A2 8B D2 96 DE 9D 95 42 F1 D1 A0 FF CD 01 CC 47 93 40 21 CB 7B 43 AA 09 8A 61 E7 38 F2 27 5B 1D 2A 4D 75 E5 DC F8 BD 8D 24 65 99 4E 62 47 65 AA B4 13 C9 57 BF 52 F2 E0 27 33 70 F0 A4 05 9F 6F D6 40 48 99 24 33 69 33 58 14 D7 E8 2D CB AF D1";
%cx2R = strsplit(strx2R, " ");
%x2R = [];
%for i = 1:500
%    x2R = [x2R; hex2dec(cell2mat(cx2R(i)))];
%end

% Normalised random distro
x1R = randn(500,1)*74.87 + 126.80;
x2R = randn(500,1)*73.514 + 120.95;

X = [x1G1 x2G1; x1R x2R];
y = [ones(500,1); 2*ones(500,1)];

% Randomise to break symmetry
r = randperm(size(X, 1));
X = X(r, :);
y = y(r);
% -------------------------------------------------------------

m = size(X, 1);


%% ================ Part 6: Initializing Pameters ================
initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);

% Unroll parameters
initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];


%% =================== Part 8: Training NN ===================
options = optimset('MaxIter', 50);

lambda = 0;

% Create "short hand" for the cost function to be minimized
costFunction = @(p) nnCostFunction(p, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, X, y, lambda);

% Now, costFunction is a function that takes in only one argument (the
% neural network parameters)

%tic
[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);
%toc

% Obtain Theta1 and Theta2 back from nn_params
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
                 hidden_layer_size, (input_layer_size + 1));

Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
                 num_labels, (hidden_layer_size + 1));


%% ================= Part 10: Implement Predict =================

pred = predict(Theta1, Theta2, X);

fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);


