% An objective function for the low-autocorrelation problem.
% Author: Ofer M. Shir, 2004; oshir@liacs.nl.
%-----------------------------------------------------------------------
function [f] = autocorrelation(pop)
% Given a population of binary sequences, this function calculates the merit
% function according to the formula specified in the exercise description.  The
% input pop is the given matrix.
% The output f is the merit factor calculated (row vector).

n = size(pop,1);
m = size(pop,2);
E = zeros(1,m);

% Calculated efficiently in a matrix-notation; auxilary matrices - Y1,Y2 - are
% initialized in every iteration. They are shifted form of the original y
% vectors. The diagonal of the dot-squared Y2*Y1 matrix is exactly the inner
% sum of merit function.
for k=1:n-1
    Y1=pop(1:n-k,:);
    Y2=pop(k+1:n,:)';
    E=E+((diag(Y2*Y1)).^2)';
end

% The output:
f = (n*n*ones(1,m))./(2*E);
