% Vorlesung Machine Learning and Data Mining
% Lösung zu Blatt 1-4
% Martin Wunderlich
% 11007857


% Runs the preceptron training. 
% Parameters: 
% P = Matrix with training data, incl. bias as pos 0 
% Y = Correct training output (teaching input) for P
% W = Initial weight vector
% eta = Learning rate
function runTraining(P, Y, W, eta)
	printf("\n\n============================\n")
	printf("Running training for ")
	eta

	cost = 1000;		% cost initialized to something large
	it = 0;			% iteration counter
	
	while( cost > 0 ) 
		it = it + 1;
		cost = 0;

		printf("============================\n")
		printf("ITERATION : ")
		printf(num2str(it));
		printf("\n============================\n")
		
		% storage for incorrectly classified variables 
		M = [];
		YM = [];

		for i = 1:columns(P)
			printf(strcat('Step ', num2str(i)))
			printf("\n")

			col = P(:,i);
			net = W .* col;
			h = sum(net);
			
			printf("Assigned class: ")
			sig = sign(h)

			%if( sig == labelA )
			%	printf("Class A assigned.\n")
			%elseif ( sig == labelB )
			%	printf("Class B assigned.\n")
			%endif

			if( sig == Y(i) )
				printf("Class correctly assigned.\n");
			else 
				printf("Class INcorrectly assigned.\n");
				% increment cost
				cost = cost + (-1*(Y(i)*h))
				% keep tack of incorrectly classified samples
				M = [M col];
				YM = [YM Y(i)];
			endif

		endfor

		printf("Total cost for current W: \n")
		cost
		if( cost == 0) 
			printf("All samples correctly classified!\n")
			break;
		endif

		printf("Incorrectly classified samples: \n")
		M

		% Update weight vector
		columnCount = columns(M);
		randomColumnIndex = round( rand(1) * (columnCount - 1)) + 1;

		m = M(:,randomColumnIndex);
		ym = YM(randomColumnIndex);

		yx = zeros(rows(W),1);
		yx = yx .+ (ym * m);
		yx = yx * eta;

		printf("Current weight vector: \n")
		W
		
		W = W .+ yx;
		printf("Adjusted weight vector: \n")
		W
	endwhile

	printf("DONE! Iterations in total for ")
	eta
	it
endfunction