clear;clc;

% AND
data = [0 0 0; 0 1 0; 1 0 0; 1 1 1];
%data = [-1 -1 -1; -1 1 -1; 1 -1 -1; 1 1 1];
% OR
%data = [0 0 0; 0 1 1; 1 0 1; 1 1 1];
% XOR
%data = [0 0 0; 0 1 1; 1 0 1; 1 1 0];


data = 0.5 + (4-0.5).*rand(33,2);
data = [data ones(33,1)];

data2 = 5 + (10-5).*rand(33,2);
data2 = [data2 zeros(33,1)];

data31 = 6 + (9-6).*rand(33,1);
data32 = 0 + (5-0).*rand(33,1);
data3 = [data31 data32 2*ones(33,1)];

data = [data;data2;data3]



% Input = bias + the rest without last row (targets)
input = [-1*ones(size(data,1),1),data(:,1:end-1)];
target = data(:,end);

%Learning rate
eta = 0.5;
% Amount of features
inputNeurons = size(data,2)-1;
% Amount of inputs for training (4 cases)
inputCases = size(data,1);



disp('First weights');
weights = rand(inputNeurons+1,1)';
disp(weights);

% Plot elements
plot(input(:,2),input(:,3),'go')
hold on;

% Training 10 times
repeat=1;
counter=1;
while repeat==1 && counter<20
    repeat=0;
    fprintf('Counter: %i.\n',counter) 
    for j=1:inputCases
        y=activation(input(j,:)*weights','sigmoid');
        %if target(j)~=y
        if abs(target(j)-y)>0.5 % We have to use this with sigmoid since
        %it's regression
           fprintf('Target: %i, y: %i, j: %i\n',target(j),y,j);
          
           weights = weights + eta*(target(j)-y)*input(j,:);
           
           
           repeat=1;
        end
        
    end
    counter=counter+1;
    x = -3:13;
    y = (-weights(2)*x+weights(1))/weights(3);
    plot(x,y,'m');

       
end
disp('Final weights');
disp(weights);

% Ploting the mesh
for i=-0.5:0.3:10.5
   for j=-0.5:0.3:10.5
       % Change this if using +1 as bias
        res=activation(-weights(1)+weights(2)*i+weights(3)*j,'sigmoid');
        
        %if res==1
        if res>0.5 % regresion
            plot(i,j,'ro');
        else
            plot(i,j,'bx');
        end
   end 
end

x = -3:13;
% Decision boundary
% Change this if using +1 as bias
y = (-weights(2)*x+weights(1))/weights(3);
plot(x,y);
plot(input(:,2),input(:,3),'ko')
hold off;
