%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Siddhartha Kasivajhula
% PSYCH 221/ EE 362 (Winter 2007-2008)
% Final Project: A Probabilistic Model of the Visual System
% 
% NoisyOrBN_interpret.m
% interprets the scene as defined in the model (please read the relevant sections in the paper for more on this)
%
% Note: in the actual model as defined in the paper, there could be objects with no connections to some features.
% To simplify this MATLAB implementation, I do not impose such a restriction. i.e. all objects are assigned weights randomly
% and so all features are likely to have non-zero weights for all objects.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function interpretation = NoisyOrBN_interpret(observation, known_objects)

% Size of feature space
N = size(known_objects, 2); 
N_test = size(observation, 2);
if(N ~= N_test)
	disp('Number of features is not consistent');
	return;
end

N_obj = size(known_objects, 1);
N_obs = size(observation, 1);

% Find the object that gets the maximum weight given the observation
% The weight of an object is simply the sum of the dot products of the
% object vectors with the observation vectors

weights = zeros(1, N_obj);
max_weight = 0;
max_weight_hypothesis = [];
max_weight_hyp_index = 0;
for i=1:N_obj
	for j=1:N_obs
		weights(i) = weights(i) + dot(known_objects(i,:), observation(j,:));
	end
	if(weights(i) > max_weight)
		max_weight = weights(i);
		max_weight_hypothesis = known_objects(i,:);
		max_weight_hyp_index = i;
	end
end

disp('Maximum weight hypothesis (object) index = '); disp(max_weight_hyp_index);
interpretation = max_weight_hypothesis;

