%%% This code is used in the Perceptron simulation.
%%% Related files: plotInput.m, trainPerceptron.m, 
%%% plotDecisionSurf.m, runPerceptron.m
%%% To run this program: execute initPerceptron and then repeatedly 
%%%  execute runPerceptron.

nPats = 30;
mid = nPats/2; %% The first half of the patterns, from 1 .. mid, will have
               %% a target output of 1. The rest will have target = -1.
inputDim = 3; %% 3 input neurons
outputDim = 1; %% 1 output neuron
lRate = 0.1;  %% the learning rate

%%% The weights: random initial values.
%%%  - each row is a weight vector for one output unit.
%%%  - the last element of each row represents the bias weight.

weights = rand(outputDim,inputDim+1);

%%% Input pattern matrix:
%%%   - each column is a training pattern
%%%   - the patterns are generated by Gaussian 
%%%     random variables
%%%   - the first half of the patterns have a 
%%%     small mean, and the rest a larger mean
%%%   - the last row is all 1's, to provide 
%%%     constant input for the "bias connection" (bias weight = - threshold)

mean1 = 0.25; %% first half of patterns have a small mean value
mean2 = 0.75; %% second half of patterns have a large mean value
var = 0.1;  %% variance of the gaussian pattern distribution
input = zeros(inputDim+1,nPats);
input(1:inputDim,1:mid) = randn(inputDim,mid)*var+mean1*ones(inputDim,mid);
input(1:inputDim,mid+1:nPats) = randn(inputDim,mid)*var+mean2*ones(inputDim,mid);
input(inputDim+1,:) = ones(1,nPats);

%%% Target output matrix:
%%%   - this determines the correct states of each of the
%%%     output units for each training pattern
%%%   - for the first half of training patterns, the desired output
%%%     will be all 1's, and for the rest it will be all -1's.

target = ones(outputDim,nPats);
target(1:outputDim,mid+1:nPats) = -1*target(1:outputDim,mid+1:nPats);

%%% To run this program, first initialize it as follows:
%  >>initPerceptron

%%% Then repeatedly plot the training Patterns, update the weights, and plot
%%% the classification boundary (decision surface) as follows:
%  >>runPerceptron


