% fitness(P)
%   Returns a vector with the fitness scores for each member of the
%   population, P
%
% Inputs:
%   P - A 2D array of the population genomes
%   gene_len - number of binary digits per number (1/2 chrom_len)
% Output:
%   fit = A 1D array of fitness scores

function fit = fitness(P)

% Use globals so we can send the values into our sir.m funciton
global beta delta

% I like to initialize any arrays I use
fit = zeros(size(P,1), 1);

% Loop through each individual in the population
for i=1:size(P,1)
    
    % Convert the current chromosome to real values.  I chose to interperet
    % the chromosome as two binary representations of numbers.  You may
    % also want to consider a Grey Code representation (or others).
   [beta, delta] = gene_to_values(P(i,:));
   
   % Get the model output for the given values of beta and delta
   [t, y] = ode15s('sir', [0 14], [50000, 1, 0]); 
   
   % The fitness is related to the squared error between the model and
   % the data, so we use our squared_error.m function.
   error = squared_error([2 4 5 6 7 8 10 12 14] , ...
                       100.*[1 5 35 150 250 260 160 90 45], ...
                       t, ...
                       y(:,2));
    % The GA maximizes values, so we return the inverse of the error to
    % trick the GA into minimzing the error.
    fit(i) = error^-1;       
end